Help with Emacs Lisp interactive feature for replacing text

I have been using Emacs for a couple of months now and I want to get started with elisp programming. In particular, I would like to write my own interactive function. However, I got a little lost. (interactive ...) has many options, and I'm not sure which one I want. Then I do not know the names of the functions that I need. If someone kindly helped me turn my pseudo-code into real code, I would be very grateful! (And as always, any links to information spots would be nice. Now I just read this. )

Here is the pseudo code for what I would like to do:

 (defun my-func (buffer) ; I think I need the buffer as an arg? "does some replacements" (interactive ???) ; ? (let (replacements (list '("a-regexp-string" . "a-replacement-string-with-backreferences") ...)) ; more of the above (while replacements (let (current (car replacements)) ; get a regexp-replacement pair (some-regexp-replace-func buffer (car current) (cdr current)) ; do the replacement (setq replacements (cdr replacements)))))) 
+4
source share
1 answer

First, from the look of your function, you are likely to do this in the current buffer, so no, you do not need to have the "buffer" argument. If this is a bad guess, I can change the code. Further, in "let", if you assign variables, you need another set of parsers around each var / value pair. Finally, when navigating through the list, I prefer to use functionally programmable functions (mapcar, mapc, etc.). I will try to insert some comments here:

 (defun my-func () "Do some replacements" (interactive) (let ((replacements (list '("foo" . "bar") '("baz" . "quux")))) (save-excursion ; So point isn't moved after this function (mapc (lambda (x) ; Go through the list, with this 'inline' function ; being called with each element as the variable 'x' (goto-char (point-min)) ; Start at the beginning of the buffer (while (re-search-forward (car x) nil t) ; Search for the car of the replacement (replace-match (cdr x)))) ; And replace it with the cdr replacements)))) ; The list we're mapc'ing through 

As for reading, I would suggest the Elisp manual that comes with Emacs.

+5
source

All Articles