[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: bulk replacement on region, buffer, file?
From: |
Emanuel Berg |
Subject: |
Re: bulk replacement on region, buffer, file? |
Date: |
Thu, 10 Dec 2015 04:21:43 +0100 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) |
Tom Roche <Tom_Roche@pobox.com> writes:
> I would appreciate pointers to code that enables
> "bulk replacement" of numerous string tuples
> ({to-replace, replace-with}) in a single call.
> What I mean, why I ask:
>
> I frequently scrape blocks of text from PDFs into
> Emacs text buffers. After I do so, I usually want to
> replace lots of strings in the buffer. E.g. (using
> '|' to delimit the strings),
>
> |CO 2| -> |CO2|
> |- | -> ||
> |“| -> |"|
> |”| -> |"|
> |[weird unicodes used for bulleting]| -> |*|
I hear you - everything is fair in the struggle against
those goofy chars! Down with unicode!
(Except: putting them as a quote when they aren't!)
Aaanyway...
Probably best way is to use set functions - another
good way tho is recursion. And I'm not just saying
that...
(defun replace-strings (tuple-list)
(when tuple-list
(let*((tuple (car tuple-list))
(rest (cdr tuple-list))
(replace-match (car tuple))
(replace-string (cadr tuple)) )
(goto-char (point-min))
(while (re-search-forward replace-match (point-max) t) ; NOERROR
(replace-match replace-string) )
(replace-strings rest) )))
;; Eval this to fix the below typos:
(replace-strings '(("Robb Hall" "Rob Hall")
("Scott Ficsher" "Scott Fischer") ))
;; Robb Hall
;;
;; Scott Ficsher
;;
;; Robb Hall
;;
;; Scott Ficsher
--
underground experts united
http://user.it.uu.se/~embe8573