2013-05-11

Emacs: Change {Round, Square, Curly} Brackets in Text

when you have data in different languages, sometimes you need to convert round brackets to square brackets, or curly brackets.

When you need to do that, it's often tedious. You have to do replacement twice, once for the left bracket, then right bracket. Here's a command that helps:

(defun change-bracket-pairs (fromType toType)
  "Change bracket pairs from one type to another on text selection or text block.
For example, change all parenthesis () to square brackets [].

In lisp code, fromType is a string of a bracket pair. ⁖ \"()\", likewise for toType."
  (interactive
   (let (
         (bracketTypes '("[]" "()" "{}" "〈〉" "《》" "「」" "『』" "【】" "〖〗"))
         )
     (list
      (ido-completing-read "Replace this:" bracketTypes "PREDICATE" )
      (ido-completing-read "To:" bracketTypes "PREDICATE" ) ) ) )

  (let* (
         (bds (get-selection-or-unit 'block))
         (p1 (elt bds 1))
         (p2 (elt bds 2))
         (changePairs (vector
                 (vector (char-to-string (elt fromType 0)) (char-to-string (elt toType 0)))
                 (vector (char-to-string (elt fromType 1)) (char-to-string (elt toType 1)))
                 ))
         )
    (replace-pairs-region p1 p2 changePairs) ) )

you'll need get-selection-or-unit and xfrp_find_replace_pairs.el