From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Executing many commands on a file Date: Sat, 18 Jul 2015 00:27:44 +0200 Organization: Informatimago Message-ID: <87bnfaju73.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1437172223 9402 80.91.229.3 (17 Jul 2015 22:30:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 17 Jul 2015 22:30:23 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sat Jul 18 00:30:17 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZGE92-0006Ke-I8 for geh-help-gnu-emacs@m.gmane.org; Sat, 18 Jul 2015 00:30:16 +0200 Original-Received: from localhost ([::1]:46339 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZGE91-0008CR-Ra for geh-help-gnu-emacs@m.gmane.org; Fri, 17 Jul 2015 18:30:15 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 62 Original-X-Trace: individual.net Zktz3jVGgZUwd8vgqq+p7QG5/fSXCOiJi1dPVPah8XoJHCv9Cp Cancel-Lock: sha1:ZmY4YmYyMmU2ZTczYzM4MjU1NGRiNjBmY2EzYzgxYWQ2Y2IyMmYyYw== sha1:MqqNpxctHkEhQYSJbABYECfLbHI= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:213507 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:105793 Archived-At: jonetsu 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