help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: bulk replacement on region, buffer, file?


From: Tom Roche
Subject: Re: bulk replacement on region, buffer, file?
Date: Mon, 14 Dec 2015 21:16:42 -0700
User-agent: GNU Emacs 24.4.1 (x86_64-pc-linux-gnu, GTK+ Version 3.14.5)

summary: elisp newbie needs help fixing code @ 
https://bitbucket.org/tlroche/elisp_bulk_replacement

details:

Apologies for the delay in replying:

Tom Roche[1]
>>> I would appreciate pointers to code that enables "bulk replacement" of 
>>> numerous string tuples ({to-replace, replace-with}) in a single call[, such 
>>> that it]

>>> 1. could be called on a region (if selected) or buffer (if not)

>>> 2. could read from a user-editable property file of replacement tuples [...]

>>> 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

Pascal J. Bourguignon[2]
>> 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[6]

I've got 3 buffers open (among many others :-), with

1. one buffer on file[3] containing some {to-replace, replace-with} tuples as 
sexp's, open locally @ filepath=`$HOME/.emacs.d/tlr_bulk_replacements.sexp`

2. another buffer containing text to be bulk-replaced (interspersed with other 
text). A sample from that buffer ("suitable for testing") is @ [4]

3. yet another buffer[5] containing

* the relevant bits of pjb-emacs.el[6]
* the path to the sexp's file as `BULK-REPLACE-TUPLES-FILEPATH`
* my attempt to transcribe the desired 'wrapping all together' function

However, when I run `M-x bulk-replace-current-buffer-with-tuples-from-file` 
(defined in the code) in the sample-text buffer[4], I get the error

*Messages*
> save-excursion: Symbol's value as variable is void: start

So how to make the code[5] set `start` and `end` appropriately, such that

* if function is called with a region set, `start`==region start && 
`end`==region end
* if function is called without a region set, `start`==buffer start && 
`end`==buffer end

? Your assistance is appreciated, Tom Roche <Tom_Roche@pobox.com>

[1]: http://lists.gnu.org/archive/html/help-gnu-emacs/2015-12/msg00077.html
[2]: http://lists.gnu.org/archive/html/help-gnu-emacs/2015-12/msg00079.html
[3]: 
https://bitbucket.org/tlroche/elisp_bulk_replacement/src/HEAD/sample_replacements.sexp
[4]: 
https://bitbucket.org/tlroche/elisp_bulk_replacement/src/HEAD/sample_input.txt
[5]: https://bitbucket.org/tlroche/elisp_bulk_replacement/src/HEAD/test_code.el
[6]: https://github.com/informatimago/emacs/blob/master/pjb-emacs.el



reply via email to

[Prev in Thread] Current Thread [Next in Thread]