Home > Archive > Functional > May 2007 > Simple LISP Grab Bag!
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Simple LISP Grab Bag!
|
|
| Xah Lee 2007-05-21, 10:05 pm |
| Simple Elisp Grab Bag!
Xah Lee, 20051101
This page shows several very simple elisp functions. They illustrate
the basic programing in elisp, but also, they are very useful
themselves.
[For a HTML version with syntax highlight and easy-reading format,
goto
http://xahlee.org/emacs/elisp_examples.html ]
To see a function's documentation, use describe-function (C-h f). A
variable's documentation is describe-variable (C-h v).
If you have no idea of elisp, goto: elisp basics.
-----------------
This code illustrates how to insert a string, and also position cursor
after the insertion. You can use this code to insert your signature,
function template, XML template, headers, footers, etc.
(defun insert-p ()
"insert <p></p> at cursor point."
(interactive)
(insert "<p></p>")
(backward-char 4))
This code shows how to place a string at the beginning and end of
selection.
(defun wrap-span-markup (start end)
"insert a markup <b></b> around a region."
(interactive "r")
(kill-region start end)
(insert "<b>")
(yank)
(insert "</b>"))
-----------------
This code illustrates how to do a sequence of replacement pairs on a
region. Very useful.
For example, you can use it to replace HTML character that needs be
encoded. For example:
=E2=80=9C&=E2=80=9D =E2=86=92 =E2=80=9C&=E2=80=9D
=E2=80=9C<=E2=80=9D =E2=86=92 =E2=80=9C<=E2=80=9D
=E2=80=9C>=E2=80=9D =E2=86=92 =E2=80=9C>=E2=80=9D
Or, you can customize the code to do replacement on Percent-encoding=E2=86=
=97.
For example:
=E2=80=9C =E2=80=9D =E2=86=92 =E2=80=9C%20=E2=80=9D
=E2=80=9C~=E2=80=9D =E2=86=92 =E2=80=9C%e7=E2=80=9D
=E2=80=9C_=E2=80=9D =E2=86=92 =E2=80=9C%5f=E2=80=9D
and so on. You can also use it to do Gr Letter replacment when
writing math. For example: alpha =E2=86=92 =CE=B1, beta =E2=86=92 =CE=B2, .=
..=2Eetc.
(defun replace-html-chars (start end)
"Replace < to < and other similar HTML chars that needs to be
encoded."
(interactive "r")
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward "&" nil t) (replace-match "&" nil t))
(goto-char (point-min))
(while (search-forward "<" nil t) (replace-match "<" nil t))
(goto-char (point-min))
(while (search-forward ">" nil t) (replace-match ">" nil t))
)
)
-----------------
This code illustrates how to delete a text enclosed by any pairs of
delimiters.
For example, if you are editing HTML code, suppose you have text
=E2=80=9C<p>how howdy, and blab blab blab</p>=E2=80=9D and your cursor is s=
omewhere
intween the tags. You want to quickly delete all texts inside the p
tags. The following function will do. It will also, delete any text
between parenthesis, so it is also helpful in writing lisp.
(defun delete-enclosed-text ()
"delete texts between any pair of delimiters."
(interactive)
(skip-chars-backward "^<>()=E2=80=9C=E2=80=9D")
(delete-char (save-excursion (skip-chars-forward "^<>()=E2=80=9C=E2=80=9D=
")))
)
-----------------
This code shows a slightly more complex example of manipulating text.
For a more robust code with explanations, see: Elisp Lesson: wrap-url
(defun wrap-url ()
"Make thing at cursor point into a html link.\n
Example: http://wikipedia.org/
becomes
<a href=3D\"http://en.wikipedia.org/\">http://wikipedia.org/</a>"
(interactive)
(re-search-backward "[\n\t ()]" nil t)
(looking-at "[\n\t ()]?\\([^\n\t ()]+\\)")
(let (
(p1 (match-beginning 1))
(p2 (match-end 1))
(url (match-string 1))
)
(delete-region p1 p2)
(goto-char p1)
(insert "<a href=3D\"" url "\">" url "</a>" )
)
)
-----------------
This example shows the setting of a variable then calling a built-in
function.
fill-paragraph is a function that add end-of-line characters to your
paragraph so that each line is no more than some 70 characters. fill-
column is a variable that has a value of 70 or so, and is used by fill-
paragraph.
(defun remove-hard-wrap ()
"remove line endings in a paragraph."
(interactive)
(let ((fill-column 90002000))
(fill-paragraph nil)))
-----------------
In this example, simple lisp constructions are shown, including
=E2=80=9Cwhile=E2=80=9D, =E2=80=9Cand=E2=80=9D, =E2=80=9Cstring-match=E2=80=
=9D. This is also a very convenient
function, which allows you to switch to the next buffer by pressing
meta and right arrow, without going thru a bunch of irrelevant buffers
that emacs created such as *scratch*, *Messages*. But when you need to
go to one of emacs's buffers, you can press meta shift and the right
arrow.
(defun next-user-buffer ()
"Switch to the next user buffer in cyclic order."
(interactive)
(next-buffer)
(let ((i 0))
(while (and (string-match "^*" (buffer-name)) (< i 10))
(setq i (1+ i)) (next-buffer) )))
(defun previous-user-buffer ()
"Switch to the next user buffer in cyclic order."
(interactive)
(previous-buffer)
(let ((i 0))
(while (and (string-match "^*" (buffer-name)) (< i 10))
(setq i (1+ i)) (previous-buffer) )))
(global-set-key (kbd "M-<left>") 'previous-user-buffer)
(global-set-key (kbd "M-<right>") 'next-user-buffer)
(global-set-key (kbd "M-S-<left>") 'previous-buffer)
(global-set-key (kbd "M-S-<right>") 'next-buffer)
-----------------
This example shows the use of thing-at-point and browse-url.
It will look up the word under the cursor in a online dictionary.
(defun word-definition-lookup ()
"Look up the word under cursor in a browser."
(interactive)
(browse-url
(concat "http://www.answers.com/main/ntquery?s=3D"
(thing-at-point 'word)))
)
This is useful because you can use it to lookup programing language
references too, such as Perl, Pretty Home Page, Java, or any esoteric
ones as long as you know the url for word search. For example, for
Perl, the url would be =E2=80=9Chttp://perldoc.perl.org/search.html?q=3D=E2=
=80=9D.
-----
Xah
xah@xahlee.org
=E2=88=91 http://xahlee.org/
| |
| Vityok 2007-05-22, 8:04 am |
|
Xah Lee =ED=E0=EF=E8=F1=E0=E2:
> Simple Elisp Grab Bag!
>
> Xah Lee, 20051101
>
> For example, you can use it to replace HTML character that needs be
> encoded. For example:
>
> "&" "&"
> "<" "<"
> ">" ">"
>
> Or, you can customize the code to do replacement on Percent-encoding .
> For example:
>
> " " "%20"
> "~" "%e7"
> "_" "%5f"
>
> and so on. You can also use it to do Gr Letter replacment when
> writing math. For example: alpha , beta , ...etc.
You can also use ``make-char`` in order to create and insert the
characters themselve.
With best regards,
Victor
| |
| Brian Palmer 2007-05-22, 8:04 am |
| [f'up-to set to comp.emacs only]
Xah Lee <xah@xahlee.org> writes:
> This code shows how to place a string at the beginning and end of
> selection.
>
> (defun wrap-span-markup (start end)
> "insert a markup <b></b> around a region."
> (interactive "r")
> (kill-region start end)
> (insert "<b>")
> (yank)
> (insert "</b>"))
This messes with the kill-ring unnecessarily. Better to
(defun w-s-m (start end &optional tag)
"Enclose a region with open and close tags (defaulting to b)."
(interactive "r")
(setq tag (or tag "b"))
(save-excursion
(goto-char end)
(insert "</" tag ">")
(goto-char start)
(insert "<" tag ">")))
--
I'm awfully glad I'm a Beta, because I don't work so hard.
| |
| Ian Zimmerman 2007-05-22, 7:06 pm |
|
I can't believe I find myself replying to the neo-troll, but this time
he posts actual misinformation.
Xah> This code shows how to place a string at the beginning and end of
Xah> selection.
Xah> (defun wrap-span-markup (start end)
Xah> "insert a markup <b></b> around a region."
Xah> (interactive "r")
Xah> (kill-region start end)
Xah> (insert "<b>")
Xah> (yank)
Xah> (insert "</b>"))
From the Elisp manual:
> Most of the kill commands are primarily for interactive use, and are
> not described here. What we do describe are the functions provided for
> use in writing such commands. You can use these functions to write
> commands for killing text. When you need to delete text for internal
> purposes within a Lisp function, you should normally use deletion
> functions, so as not to disturb the kill ring contents. *Note
> Deletion::.
--
This line is completely ham.
|
|
|
|
|