;;; -*- lexical-binding: t -*- ;; ;; this file: ;; https://dataswamp.org/~incal/emacs-init/replace-list.el (require 'cl-lib) (require 'subr-x) (defun replace-regexp-buffer (regexp replace) (save-excursion (goto-char (point-max)) (while (re-search-backward regexp nil t) (replace-match replace) ))) (defun replace-list (list-pairs) (cl-loop for (search . replace) in list-pairs do (replace-regexp-buffer search replace) )) ;; (replace-list '(("james" . "James") ("blish" . "Blish"))) (defun replace-list-ask (list-pairs) (save-excursion (cl-loop for (search . replace) in list-pairs do (goto-char (point-min)) (query-replace-regexp search replace) ))) (defun replace-list-ask-2 (list-pairs) (cl-loop for (search . replace) in list-pairs do (query-replace-regexp search replace nil (point-min) (point-max)) )) (defun replace-list-ask-3 (dict) (let ((re (string-join (mapcar #'car dict) "\\|"))) (save-excursion (goto-char (point-min)) (while (re-search-forward re nil t) (let*((match-string (match-string-no-properties 0)) (default (cdr (assoc match-string dict))) ) (when default (query-replace match-string default nil (match-beginning 0) (match-end 0) ))))))) ;; (replace-list-ask '(("james" . "James") ("blish" . "Blish"))) ;; (replace-list-ask-2 '(("james" . "James") ("blish" . "Blish"))) ;; (replace-list-ask-3 '(("james" . "James") ("blish" . "Blish"))) ;; james Blish james blish (provide 'replace-list)