[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Executing many commands on a file
From: |
Pascal J. Bourguignon |
Subject: |
Re: Executing many commands on a file |
Date: |
Sat, 18 Jul 2015 00:27:44 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
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