* Executing many commands on a file
@ 2015-07-17 21:07 jonetsu
0 siblings, 0 replies; 2+ messages in thread
From: jonetsu @ 2015-07-17 21:07 UTC (permalink / raw
To: help-gnu-emacs
Hello,
After loading a file I have to do several M-x replace-string commands. Is there a way to chain those and have them executed as one command ?
And optionally, to have them executed when a file with a certain name pattern is loaded ?
Thanks.
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: Executing many commands on a file
[not found] <mailman.7075.1437167238.904.help-gnu-emacs@gnu.org>
@ 2015-07-17 22:27 ` Pascal J. Bourguignon
0 siblings, 0 replies; 2+ messages in thread
From: Pascal J. Bourguignon @ 2015-07-17 22:27 UTC (permalink / raw
To: help-gnu-emacs
jonetsu <jonetsu@teksavvy.com> writes:
> Hello,
>
>
> After loading a file I have to do several M-x replace-string
> commands. Is there a way to chain those and have them executed as one
> command ?
>
>
> And optionally, to have them executed when a file with a certain name pattern is loaded ?
Of course.
(defun replace-multiple-strings (replacements-alist)
"Replaces all occurences of the keys in `replacements-alist' by their corresponding value.
The search is performed in sequentially once from (point) to (point-max)."
(let ((re (concat "\\("
(mapconcat (lambda (entry) (regexp-quote (car entry)))
replacements-alist
"\\|")
"\\)")))
(while (re-search-forward re (point-max) t)
(let* ((key (buffer-substring-no-properties (match-beginning 1)
(match-end 1)))
(val (cdr (assoc* key replacements-alist
:test (function string=)))))
(if val
(progn
(delete-region (match-beginning 1) (match-end 1))
(insert val)
(goto-char (+ (match-beginning 1) (length val))))
(goto-char (match-end 1)))))))
(with-current-buffer "some buffer"
(goto-char (point-min))
(replace-multiple-strings '(("multi" . "uniq")
("def" . "redef"))))
You can have something done when you open certain files with a hook.
That could be a mode hook, or a file hook. For random name patterns, a
file hook:
(defun jonetsu-find-file-meat ()
(interactive)
(when (string-match "^/some/path/some[patern]+\\.txt$"
(buffer-file-name (current-buffer)))
(goto-char (point-min))
(replace-multiple-strings '(("multi" . "uniq")
("def" . "redef")))
(goto-char (point-min))))
(add-hook 'find-file-hook 'jonetsu-find-file-meat)
--
__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
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2015-07-17 22:27 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-07-17 21:07 Executing many commands on a file jonetsu
[not found] <mailman.7075.1437167238.904.help-gnu-emacs@gnu.org>
2015-07-17 22:27 ` Pascal J. Bourguignon
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.