From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: Executing many commands on a file
Date: Sat, 18 Jul 2015 00:27:44 +0200 [thread overview]
Message-ID: <87bnfaju73.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: mailman.7075.1437167238.904.help-gnu-emacs@gnu.org
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
next parent reply other threads:[~2015-07-17 22:27 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <mailman.7075.1437167238.904.help-gnu-emacs@gnu.org>
2015-07-17 22:27 ` Pascal J. Bourguignon [this message]
2015-07-17 21:07 Executing many commands on a file jonetsu
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=87bnfaju73.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.