Elisp: How to Write Commands
Here's the basics of how to write a emacs command that user can call.
This is the typical template for user-defined emacs commands.
(defun my-command () "One sentence summary of what this command do. More details here. Be sure to mention the return value if relevant. Lines here should not be longer than 70 chars, and don't indent them." (interactive) (let (var1 var2 …) (setq var1 …) (setq var2 …) ;; do something … ))
In your doc string, if you want clickable URL, or clickable reference to other commands, etc., See: Elisp: Doc String Markup.
- Elisp: Cursor Position Functions
- Elisp: Text Editing Functions
- Elisp: Buffer and File Functions
- Elisp: File and Directory Functions
Get Buffer String
Mark, Region, Active Region
Cut, Copy, Paste, kill-ring
Working with Lines
Get User Input
Processing String in Temp Buffer
In Perl, there are maybe 20 functions that act on string. In elisp, there are only about 5, because elisp has a buffer data type that's more powerful and flexible, and you have over 3 thousand functions that acts on text in a buffer. When you have a string, and you need to do more than just getting substring or number of chars, put it in a temp buffer. Here's a example:
;; process string in a temp buffer (setq str "some big string here you need to process") (setq strNew (with-temp-buffer (insert str) ;; code to manipulate your string as buffer text ;; … (buffer-string) ; get result ))
Find Replace Text of a Buffer
Apply to dired's Marked Files
To apply a function to marked files in dired, use
dired-get-marked-files, like this:
;; 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)) )