Emacs Lisp Multi-Pair Find & Replace Applications

Advertise Here

, , …,

This article shows example uses of find & replace in emacs lisp.

Most of the following examples uses a elisp library 〔xfrp_find_replace_pairs.el〕. It defines functions to lets you do multiple find & replace pairs in one-shot, so the find & replace pairs are more compact and readable. For the code, or how to program elisp without it, see: Emacs Lisp: Multi-Pair String Replacement: xfrp_find_replace_pairs.el.

CSS Compressor

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.

There are many libraries and tools to do that for JavaScript and CSS. (just web search for “css compactor” or “javascript compactor”) With emacs, you can have a lisp function that does a simple code compacting by just find replace. For example, here's my CSS compactor:

(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"]
 )) ) ) )

With the above, you can just press one button to compact your code in current buffer. (➲ How to Define Keyboard Shortcuts)

More Examples

Almost all of the following i use daily multiple times in the past 3 years.

Escape/UnEscape Backslash

These are very useful when you code elisp for text processing. For example, i often have a text copied from some HTML that i need to search for in my elisp code. After pasting the text, i need to quote them. (elisp does not support heredoc.)

(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 '(["\\\"" "\""])))

Unicode Replacement

Useful if you work with math a lot.

(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. e.g. alpha to α."
  (interactive "r")
(replace-pairs-region start end '(
["alpha" "α"]
["beta" "β"]
["gamma" "γ"]
["theta" "θ"]
["lambda" "λ"]
["delta" "δ"]
["epsilon" "ε"]
["omega" "ω"]
["Pi" "π"])))

See:

Curly Quote Fixes

(defun replace-curly-apostrophe-region (start end)
  "Replace some single curly quotes to apostrophe.
e.g. 「didn’t」 ⇒ 「didn't」."
  (interactive "r")
(replace-pairs-region start end '(
["‘tis" "'tis"]
["’s" "'s"]
["’d" "'d"]
["n’t" "n't"]
["’ve" "'ve"]
["’ll" "'ll"]
["’m" "'m"]
["’re" "'re"]
["s’ " "s' "])))
(defun replace-straight-quotes-region (p1 p2)
  "Fix straight double quotes to curly ones, double hyphen to emdash, etc.
e.g.
 「time flies -- proverb」 ⇒ 「time flies — proverb」
 「he said \"no\".」 ⇒ 「he said “no”.」"
  (interactive "r")
  (let (quoteReplaceMap)
    ;; a map that helps converting straight quotes to double quotes in texts
    ;; (e.g. novels). Note: order is important since this is huristic.
    (setq quoteReplaceMap
          '(
["--" " — "]
["  —  " " — "]
[">\"" ">“"]
["(\"" "(“"]
[" \"" " “"]
["\" " "” "]
["\"," "”,"]
["\"." "”."]
["\"?" "”?"]
["\";" "”;"]
["\":" "”:"]
["\")" "”)"]
["\"]" "”]"]
[".\"" ".”"]
[",\"" ",”"]
["!\"" "!”"]
["?\"" "?”"]
;; ";
["\n\"" "\n“"]
[">\'" ">‘"]
[" \'" " ‘"]
["\' " "’ "]
["\'," "’,"]
[".\'" ".’"]
["!\'" "!’"]
["?\'" "?’"]
["(\'" "(‘"]
["\')" "’)"]
["\']" "’]"]
[" ‘em" " 'em"]))

    (replace-pairs-region p1 p2 quoteReplaceMap)))

Make HTML Table

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 is fantastic!

blog comments powered by Disqus