Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Scheme Programming Language

Simple LISP Grab Bag!


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:

& &amp;
< &lt;
> &gt;

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 Greek Letter replacment when
writing math. For example: alpha , beta , ...etc.

(defun replace-html-chars (start end)
  "Replace < to &lt; 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 "&amp;" nil t))
    (goto-char (point-min))
    (while (search-forward "<" nil t) (replace-match "&lt;" nil t))
    (goto-char (point-min))
    (while (search-forward ">" nil t) (replace-match "&gt;" 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
<p>how howdy, and blab blab blab</p> and your cursor is somewhere
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 "^<>()")
  (delete-char (save-excursion (skip-chars-forward "^<>()")))
)

-----------------
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=\"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=\"" 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
while, and, string-match. 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="
          (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 http://perldoc.perl.org/search.html?q=.

-----

  Xah
  x@xahlee.org
http://xahlee.org/

Xah Lee :

You can also use  ``make-char`` in order to create and insert the
characters themselve.

With best regards,

Victor

[f'up-to set to comp.emacs only]

Xah Lee <x@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.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc