From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: bulk replacement on region, buffer, file? Date: Thu, 10 Dec 2015 04:13:07 +0100 Organization: Informatimago Message-ID: <87twnrngho.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1449717325 2589 80.91.229.3 (10 Dec 2015 03:15:25 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 10 Dec 2015 03:15:25 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Dec 10 04:15:21 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1a6rhQ-0000Bv-Pe for geh-help-gnu-emacs@m.gmane.org; Thu, 10 Dec 2015 04:15:20 +0100 Original-Received: from localhost ([::1]:39082 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1a6rhQ-0005Yq-2x for geh-help-gnu-emacs@m.gmane.org; Wed, 09 Dec 2015 22:15:20 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 86 Original-X-Trace: individual.net 00JXQiH7I8zjv+qtvBTCCAbnfWw40okPfKWzLHlyo4WCUovvjg Cancel-Lock: sha1:MWU1ZDk1MzgyOWJlZDlmYTkyOTJkMThhZGRjZjhlNTJjM2MxNDg0ZA== sha1:SJmQpP56+8srMYtlcB8qUDL/nJs= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:216060 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:108350 Archived-At: Tom Roche 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]| -> |*| > > which I do manually by calling `M-x replace-string` or similar > interactive or regexp function. I'd prefer instead to call something > that > > 1. could be called on a region (if selected) or buffer (if not) You can use functions that are not designed to work on a region, restricting them to a narrowed region with narrow-to-region. (This is why it is important to always use point-min and point-max, and not eg. 0 and buffer-size, because point-min and point-max take into account the narrowing). (save-excursion (narrow-to-region start end) ...) > 2. could read from a user-editable property file of replacement tuples > (like those above), similar to `abbrev_defs` but without some > constraints of the latter that annoy in this usecase. E.g. (unless I'm > missing something), I cannot use `abbrev` to replace the > space-delimited 'CO 2' with 'CO2'. You can read lisp sexps from files with: (with-file "~/.your-replacements.sexp" (goto-char (point-min)) ; in case the file is already open. (read (current-buffer))) > 3. would, for every {to-replace, replace-with} tuple in the file, > > * if `to-replace` found, replace every instance with `replace-with` > * if `to-replace` not found, goto next tuple > > Is there elisp to do this? Yes. I use: (progn (goto-char (point-min)) (replace-multiple-strings '(("CO 2" . "CO2") ("- " . "") ("“" . "\"") ("”" . "\"") ("[weird unicodes used for bulleting]" . "*")))) So wrapping all together: (save-excursion (narrow-to-region start end) (goto-char (point-min)) (replace-multiple-strings (with-file "~/.your-replacements.sexp" (goto-char (point-min)) ; in case the file is already open. (read (current-buffer))))) with-file and replace-multiple-strings are found in pjb-emacs.el https://github.com/informatimago/emacs/blob/master/pjb-emacs.el -- __Pascal Bourguignon__ http://www.informatimago.com/ “The factory of the future will have only two employees, a man and a dog. The man will be there to feed the dog. The dog will be there to keep the man from touching the equipment.” -- Carl Bass CEO Autodesk