Emacs Lisp Examples, page 2

By Xah Lee. Date: . Last updated: .

Reference Lookup

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 'symbol))))

For detail, see: Emacs: Lookup Google, Dictionary, Documentation.

Change Newline Character

This example shows how to define a function that takes a file path and process the file.

(defun to-unix-eol (fPath)
  "Change file's line ending to unix convention."
  (let (myBuffer)
    (setq myBuffer (find-file fPath))

    (set-buffer-file-coding-system 'unix) ; or 'mac or 'dos
;; do this or that

    (save-buffer)
    (kill-buffer myBuffer)
   )
)

For example, if the file ~/jane/readme.txt is a Windows file, you can change its line ending by executing the following: (to-unix-eol "~/jane/readme.txt").

You can also edit the line ;; do this or that to make this function do other things, such as deleting or replacing some words in the file.

The following example shows how to apply a file processing function to a list of files.

(mapc 'to-unix-eol
 (list
"~/jane/myfile1"
"~/jane/myfile2"
"~/jane/myfile3"
;   )
)

The following code is a example of making a file-processing function that applies to all marked files in dired.

(defun dired-2unix-marked-files ()
  "Change to unix line ending for marked (or next arg) files."
  (interactive)
  (mapc 'to-unix-eol (dired-get-marked-files))
)

Delete Current File

This example shows command that lets you delete the current file. Note here that elisp is used to: {manipulate buffer, manipulate file, prompt user}.

(defun delete-current-file ()
  "Delete the file associated with the current buffer.
Delete the current buffer too.
If no file is associated, just close buffer without prompt for save."
  (interactive)
  (let (currentFile)
    (setq currentFile (buffer-file-name))
    (when (yes-or-no-p (concat "Delete file?: " currentFile))
      (kill-buffer (current-buffer))
      (when (not (equal currentFile nil))
        (delete-file currentFile) ) ) ) )

Detail at Emacs: Delete Current File.

Highlighting Lines

This example shows you how to make lines containing the words “ERROR:” or “NOTE:” highlighted, whenever a file ending in “log” is opened.

(defun highlite-it ()
  "Highlight certain lines…"
  (interactive)
  (if (equal "log" (file-name-extension (buffer-file-name)))
      (progn
        (highlight-lines-matching-regexp "ERROR:" 'hi-red-b)
        (highlight-lines-matching-regexp "NOTE:" 'hi-blue-b))))

(add-hook 'find-file-hook 'highlite-it)

The add-hook line will make emacs call “highlite-it” whenever a file is opened. It works by adding the function “highlite-it” to the list in the variable find-file-hook.

find-file is the function that open files. find-file-hook is a variable containing list of functions that will run when find-file is run.

Convert Text to Table

This example shows you how to grab the current paragraph the cursor is on, and do some transformation on it.

This example turns a block of text into a HTML table. The command asks user to enter a string pattern as the separator. For example:

a  b  c
1  2  3
this  and  that

with double space as separator, becomes

<table border="1" cellpadding="5" cellspacing="0">
<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>this</td><td>and</td><td>that</td></tr>
</table>

and is rendered in browser like this:

abc
123
thisandthat
(defun make-html-table-string (textblock delim)
  "Turn a text string into a HTML table.
See make-html-table."
  (let ()
    (setq textblock (replace-regexp-in-string delim "</td><td>" textblock))
    (setq textblock (replace-regexp-in-string "\n" "</td></tr>\n<tr><td>" textblock))
    (setq textblock (substring textblock 0 -8)) ;; delet the beginning “<tr><td>” in last line
    (concat "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">\n<tr><td>" textblock "</table>")
    ))

(defun make-html-table (sep)
  "Turn the current paragraph into a HTML table.

The “current paragraph” is defined as having empty lines before and
after the block of text the cursor is on.

For example:

a*b*c
1*2*3
this*and*that

with “*” as separator, becomes

<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">
<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>this</td><td>and</td><td>that</td></tr>
</table>"
  (interactive "sEnter string pattern for column separation:")
  (let (bds p1 p2 myStr)
    (setq bds (bounds-of-thing-at-point 'paragraph))
    (setq p1 (+ (car bds) 1))
    (setq p2 (cdr bds))
    (setq myStr (buffer-substring-no-properties p1 p2))
    (delete-region p1 p2)
    (insert (make-html-table-string myStr sep) "\n")
  ))

For a detailed explanation on the code, see Emacs: Lines to HTML Table.

Insert Vertical Column of Numbers

Emacs 24 has a new command rectangle-number-linesCtrl+x r N】. It will insert a vertical column of numbers into a block of text, like this:

  1. x
  2. x
  3. x
  4. x

For emacs 23 users, you can define your own, like this:

(defun insert-column-counter (n)
  "Insert a sequence of numbers vertically.
For example, if your text is:

a b
c d
e f

and your cursor is after “a”, then calling this function with argument
3 will change it to become:

a1 b
c2 d
e3 f

If there are not enough existing lines after the cursor
when this function is called, it aborts at the last line.

This command is conveniently used together with `kill-rectangle' and `string-rectangle'."
  (interactive "nEnter the max integer: ")
  (let ((i 1) colpos )
    (setq colpos (- (point) (line-beginning-position)))
    (while (<= i n)
      (insert (number-to-string i))
      (forward-line) (beginning-of-line) (forward-char colpos)
      (setq i (1+ i))
      )
))
Like it? Buy Xah Emacs Tutorial. Thanks.