Added some snippets for elisp-mode from Xah Lee.

This commit is contained in:
Zhang Chiyuan 2009-02-25 05:23:31 +00:00
parent 28aacbc7c1
commit 84ac5bd613
10 changed files with 130 additions and 0 deletions

View File

@ -0,0 +1,11 @@
TITLE: Emacs Idiom Template Set. Version 1. 2009-02-22
DESCRIPTION: Some useful templates for emacs lisp. This template set is based on useful elisp idioms on common tasks.
LICENSING: GPL version 3.
AUTHOR: Xah Lee
Home Page: latest version at:
• Emacs Lisp Idiom Templates
http://xahlee.org/emacs/elisp_idiom_templates.html

View File

@ -0,0 +1,11 @@
#name : function template
#contributor : Xah Lee
# --
(defun $1 ()
"thisandthat."
(interactive)
(let (var1)
(setq var1 some)
$0
)
)

View File

@ -0,0 +1,16 @@
#name : process marked files in dired
#contributor : Xah Lee
# --
;; idiom for processing a list of files in dired's marked files
;; suppose myProcessFile is your function that takes a file path
;; and do some processing on the file
(defun dired-myProcessFile ()
"apply myProcessFile function to marked files in dired."
(interactive)
(require 'dired)
(mapc 'myProcessFile (dired-get-marked-files))
)
;; to use it, type M-x dired-myProcessFile

View File

@ -0,0 +1,17 @@
#name : a function that process a file
#contributor : Xah Lee
# --
(defun doThisFile (fpath)
"Process the file at path FPATH ..."
(let ()
;; create temp buffer without undo record or font lock. (more efficient)
;; first space in temp buff name is necessary
(set-buffer (get-buffer-create " myTemp"))
(insert-file-contents fpath nil nil nil t)
;; process it ...
;; (goto-char 0) ; move to begining of file's content (in case it was open)
;; ... do something here
;; (write-file fpath) ;; write back to the file
(kill-buffer " myTemp")))

View File

@ -0,0 +1,17 @@
#name : read lines of a file
#contributor : Xah Lee
# --
(defun read-lines (filePath)
"Return a list of lines in FILEPATH."
(with-temp-buffer
(insert-file-contents filePath)
(split-string
(buffer-string) "\n" t)) )
;; process all lines
(mapc
(lambda (aLine)
(message aLine) ; do your stuff here
)
(read-lines "inputFilePath")
)

View File

@ -0,0 +1,17 @@
#name : find and replace on region
#contributor : Xah Lee
# --
(defun replace-html-chars-region (start end)
"Replace “<” to “&lt;” and other chars in HTML.
This works on the current region."
(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))
)
)

View File

@ -0,0 +1,4 @@
#name : grab buffer substring
#contributor : Xah Lee
# --
(setq $0 (buffer-substring-no-properties myStartPos myEndPos))

View File

@ -0,0 +1,4 @@
#name : grab word under cursor
#contributor : Xah Lee
# --
(setq $0 (thing-at-point 'symbol))

View File

@ -0,0 +1,6 @@
#name : traversing a directory
#contributor : Xah Lee
# --
;; apply a function to all files in a dir
(require 'find-lisp)
(mapc 'my-process-file (find-lisp-find-files "~/myweb/" "\\.html$"))

View File

@ -0,0 +1,27 @@
#name : Command that works on region or word
#contributor : Xah Lee
# --
;; example of a command that works on current word or text selection
(defun down-case-word-or-region ()
"Lower case the current word or text selection."
(interactive)
(let (pos1 pos2 meat)
(if (and transient-mark-mode mark-active)
(setq pos1 (region-beginning)
pos2 (region-end))
(setq pos1 (car (bounds-of-thing-at-point 'symbol))
pos2 (cdr (bounds-of-thing-at-point 'symbol))))
; now, pos1 and pos2 are the starting and ending positions
; of the current word, or current text selection if exists
;; put your code here.
$0
;; Some example of things you might want to do
(downcase-region pos1 pos2) ; example of a func that takes region as args
(setq meat (buffer-substring-no-properties pos1 pos2)) ; grab the text.
(delete-region pos1 pos2) ; get rid of it
(insert "newText") ; insert your new text
)
)