Emacs: Add Brackets to CAPITALIZED words 💠

By Xah Lee. Date: . Last updated: .

This command add angle quotation marks ‹ › around CAPITALIZED WORDS, on selection or current text block.

This is useful if you write lisp documentation.

(defun xah-bracket-capitalized-words ()
  "Add ‹› to CAPITALIZED WORDS, on selection or current text block.
Example:
Change value in PLIST of PROP to VAL
becomes
Change value in ‹PLIST› of ‹PROP› to ‹VAL›

If the word already has bracket, or between >< , do nothing.

2021-04-10 command name was xah-bracket-caps.

URL `http://xahlee.info/emacs/emacs/elisp_bracket_caps.html'
Created: 2020-04-19
Version: 2025-03-25"
  (interactive)
  (let (xbeg xend (case-fold-search nil))
    (seq-setq (xbeg xend) (if (region-active-p) (list (region-beginning) (region-end)) (list (save-excursion (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point))) (save-excursion (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point))))))
    (save-restriction
      (narrow-to-region xbeg xend)
      (goto-char (point-min))
      (while (re-search-forward "\\b\\([A-Z][-_A-Z0-9]+\\)\\b" nil t)
        (goto-char (match-end 0))
        (when (and (not (eq (char-after) ?›))
                   (not (eq (char-after) ?<)))
          (insert "›")
          (goto-char (match-beginning 0))
          (insert "‹"))
        ;; (replace-match (concat "‹" (match-string-no-properties 1) "›") t )
        ))))