Emacs: Remove Square Brackets 🚀

By Xah Lee. Date: .

Remove Wikipedia Citation Mark

In Wikipedia article, there are many citation marks like this: [1], [2], etc. If you quote Wikipedia in your blog, those citation marks don't make sense and are distracting.

Here's a command to remove them.

(defun xah-remove-square-brackets (&optional Begin End)
  "Delete any text of the form [1], [2] etc in current text block or selection.

For example
 as Blu-ray Disc [11][12],
becomes
 as Blu-ray Disc,

When called non-interactively, Begin End are region positions.

URL `http://xahlee.info/emacs/emacs/emacs_remove_square_brackets.html'
Created: 2017-06-10
Version: 2023-11-26"
  (interactive)
  (let (xp1 xp2 xchangedItems)
    (seq-setq (xp1 xp2) (if (and Begin End) (vector Begin End) (xah-get-pos-of-block-or)))
    (save-restriction
      (narrow-to-region xp1 xp2)
      (progn
        (goto-char (point-min))
        (while (re-search-forward "\\(\\[[0-9]+?\\]\\)" nil t)
          (setq xchangedItems (cons (match-string-no-properties 1) xchangedItems))
          (replace-match "" t)))
      (progn
        (goto-char (point-min))
        (while (search-forward "[citation needed]" nil t)
          (setq xchangedItems (cons "[citation needed]" xchangedItems))
          (backward-char 17)
          (delete-char 17)))
      (goto-char (point-max)))
    (if (> (length xchangedItems) 0)
        (mapcar
         (lambda (xx)
           (princ xx)
           (terpri))
         (reverse xchangedItems))
      (message "No change needed."))))

requires package Emacs: xah-get-thing.el 📦