Xah Lee, 2005-11, 2010-08-08
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))) )
If you sometimes need to lookup a phrase, such as “lingua franca”, you can modify the code so that it will take the current text selection for input if there's one. Like this:
(transient-mark-mode 1) ; turn text selection highlighting on (delete-selection-mode 1) ; turn on behavior that delete or type-over selected text (defun word-definition-lookup () "Look up the word's definition in a browser.\n If a region is active (a phrase), lookup that phrase." (interactive) (let (myword myurl) (setq myword (if (and transient-mark-mode mark-active) (buffer-substring-no-properties (region-beginning) (region-end)) (thing-at-point 'symbol))) (setq myword (replace-regexp-in-string " " "%20" myword)) (setq myurl (concat "http://www.answers.com/main/ntquery?s=" myword)) (browse-url myurl) ))
This is useful because you can use it to lookup programing language references too, such as Perl, PHP, Java, or any other as long as you know the query url format.
For more detail, see: Dictionary and Reference Lookup with Emacs.
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.
(mapcar '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) (mapcar 'to-unix-eol (dired-get-marked-files)) )
This example shows a easy way to delete the current file.
(defun delete-current-file () "Delete the file associated with the current buffer. Delete the current buffer too." (interactive) (let (currentFile) (setq currentFile (buffer-file-name)) (when (yes-or-no-p (concat "Delete file: " currentFile)) (kill-buffer (current-buffer)) (delete-file currentFile) (message (concat "Deleted file: " currentFile)) ) ) )
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.
Sometimes, you need to insert a vertical column of sequential integers into a block of text, like this:
line number 1 yay line number 2 yay line number 3 yay line number 4 yay line number 5 yay
(defun insert-column-counter (n) "Insert a sequence of integers 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 line-move-visual-original) (setq line-move-visual-original line-move-visual) (setq line-move-visual nil) (setq colpos (- (point) (point-at-bol))) (while (<= i n) (insert (number-to-string i)) (forward-line) (beginning-of-line) (forward-char colpos) (setq i (1+ i)) ) (setq line-move-visual line-move-visual-original) ))
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:
| a | b | c |
| 1 | 2 | 3 |
| this | and | that |
(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 Elisp Lesson: Writing make-html-table.