This page shows examples of multi-pair find/replace in emacs lisp.
Most of the following examples uses a elisp library 〔xfrp_find_replace_pairs.el〕. See: Emacs Lisp: Multi-Pair String Replacement: xfrp_find_replace_pairs.el.
Multi-Pair Find/Replace is useful in many situations. For example, webmasters often need to compact JavaScript or CSS code, so the file size becomes smaller and decrease page load time.
(defun compact-css-region (p1 p2) "Remove unnecessary whitespaces of CSS source code in region. CSS is Cascading Style Sheet. WARNING: not robust. Designed for my personal use only." (interactive "r") (let () (save-restriction (narrow-to-region p1 p2) (replace-regexp-pairs-region (point-min) (point-max) '([" +" " "])) (replace-pairs-region (point-min) (point-max) '( ["\n" ""] [" /* " "/*"] [" */ " "*/"] [" {" "{"] ["{ " "{"] ["; " ";"] [": " ":"] [";}" "}"] ["}" "}\n"] )) ) ) )
You can define a hotkey for it. 〔☛ How to Define Keyboard Shortcuts〕 With the above, now you just press one button, and the CSS code in current buffer becomes compact!
this function is part of xah-css-mode.
These are very useful when you do text processing in emacs lisp. For example, if you need to process HTML, just copy raw HTML text, paste in emacs, then press a button to escape quote characters, and now you are ready to put them inside elisp string.
(defun escape-quotes-region (start end) "Replace \" by \\\" in region." (interactive "r") (replace-pairs-region start end '(["\"" "\\\""])))
(defun unescape-quotes-region (start end) "Replace \\\" by \" in region." (interactive "r") (replace-pairs-region start end '(["\\\"" "\""])))
Useful if you work with math a lot such as LaTeX.
(defun replace-mathematica-symbols-region (start end) "Replace Mathematica's special char encoding to Unicode of the same semantics. For example: \\=\\[Infinity] ⇒ ∞ \\=\\[Equal] ⇒ ==" (interactive "r") (replace-pairs-region start end '( ["\\[Infinity]" "∞"] ["\\[Equal]" "=="])))
(defun replace-greek-region (start end) "Replace math symbols. ⁖ alpha to α." (interactive "r") (replace-pairs-region start end '( ["alpha" "α"] ["beta" "β"] ["gamma" "γ"] ["theta" "θ"] ["lambda" "λ"] ["delta" "δ"] ["epsilon" "ε"] ["omega" "ω"] ["Pi" "π"])))
You can easily modify them for TeX/LaTeX.
The following is useful when you want to normalize the style of punctuation in essay or novel.
(defun replace-straight-quotes (p1 p2) "Replace straight double quotes to curly ones, and others. Works on current text selection, else the current text block between empty lines. Examples of changes: \"…\" ⇒ “…” ... ⇒ … I’m ⇒ I'm -- ⇒ — ~= ⇒ ≈ In lisp program, the arguments P1 and P2 are region boundaries. " ;; some examples for debug ;; do "‘em all -- done..." ;; I’am not ;; said "can’t have it, can’t, just can’t" ;; ‘I’ve can’t’ (interactive (let ( (bds (get-selection-or-unit 'block))) (list (elt bds 1) (elt bds 2) ) ) ) (let ( ) ;; Note: order is important since this is huristic. (save-restriction (narrow-to-region p1 p2) ;; dash and ellipsis etc (replace-pairs-region (point-min) (point-max) [ ["--" " — "] ["—" " — "] ["..." "…"] ["~=" "≈"] ]) (replace-pairs-region (point-min) (point-max) [ [" — " " — "] ; rid of extra space in em-dash ]) ;; fix GNU style ASCII quotes (replace-pairs-region (point-min) (point-max) [ ["``" "“"] ["''" "”"] ]) ;; fix straight double quotes (replace-pairs-region (point-min) (point-max) [ [">\"" ">“"] ["(\"" "(“"] [" \"" " “"] ["\" " "” "] ["\"," "”,"] ["\"." "”."] ["\"?" "”?"] ["\";" "”;"] ["\":" "”:"] ["\")" "”)"] ["\"]" "”]"] [".\"" ".”"] [",\"" ",”"] ["!\"" "!”"] ["?\"" "?”"] ["\"<" "”<"] ;; "; ["\"\n" "”\n"] ]) ;; fix straight double quotes by regex (replace-regexp-pairs-region (point-min) (point-max) [ ["\\`\"" "“"] ]) ;; fix single quotes to curly (replace-pairs-region (point-min) (point-max) [ [">\'" ">‘"] [" \'" " ‘"] ["\' " "’ "] ["\'," "’,"] [".\'" ".’"] ["!\'" "!’"] ["?\'" "?’"] ["(\'" "(‘"] ["\')" "’)"] ["\']" "’]"] ]) ;; fix apostrophe (replace-regexp-pairs-region (point-min) (point-max) [ ["\\bcan’t\\b" "can't"] ["\\bdon’t\\b" "don't"] ["\\bdoesn’t\\b" "doesn't"] ["\\bain’t\\b" "ain't"] ["\\bdidn’t\\b" "didn't"] ["\\baren’t\\b" "aren't"] ["\\bwasn’t\\b" "wasn't"] ["\\bweren’t\\b" "weren't"] ["\\bcouldn’t\\b" "couldn't"] ["\\bshouldn’t\\b" "shouldn't"] ["\\b’ve\\b" "'ve"] ["\\b’re\\b" "'re"] ["\\b‘em\\b" "'em"] ["\\b’ll\\b" "'ll"] ["\\b’m\\b" "'m"] ["\\b’d\\b" "'d"] ["\\b’s\\b" "'s"] ["s’ " "s' "] ["s’\n" "s'\n"] ["\"$" "”"] ]) ;; fix back. quotes in HTML code (replace-regexp-pairs-region (point-min) (point-max) [ ["” \\([a-z]+\\)=" "\" \\1="] ["=\”" "=\""] ["/” " "/\" "] ["\"\\([0-9]+\\)” " "\"\\1\" "] ]) ) ))
Emacs: Change Brackets () {} [] in Text
Emacs Lisp: Convert Punctuation Between English/Chinese Forms
Another different use, but essentially same technique of find/replace, is to turn a plain text table into a HTML table. See: Emacs Lisp: How to Write a make-html-table Command.
Emacs ♥