From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: bulk replacement on region, buffer, file?
Date: Thu, 10 Dec 2015 04:13:07 +0100 [thread overview]
Message-ID: <87twnrngho.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.1767.1449714096.31583.help-gnu-emacs@gnu.org
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]| -> |*|
>
> 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
next parent reply other threads:[~2015-12-10 3:13 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.1767.1449714096.31583.help-gnu-emacs@gnu.org>
2015-12-10 3:13 ` Pascal J. Bourguignon [this message]
2015-12-15 4:16 ` bulk replacement on region, buffer, file? Tom Roche
2015-12-10 2:21 Tom Roche
2015-12-10 3:21 ` Emanuel Berg
2015-12-10 16:40 ` Bob Proulx
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87twnrngho.fsf@kuiper.lan.informatimago.com \
--to=pjb@informatimago.com \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.