Emacs: HTML. Word to Wikipedia Link 📜

By Xah Lee. Date: . Last updated: .

Here's a command to turn a word or phrase into a HTML link to Wikipedia.

(defun xah-html-word-to-wikipedia-link (&optional Begin End)
  "Make the current word or selection into a Wikipedia link.
For Example:
 Emacs
becomes
 <a href=\"https://en.wikipedia.org/wiki/Emacs\">Emacs</a>

URL `http://xahlee.info/emacs/emacs/elisp_html_word_to_wikipedia_linkify.html'
Created: 2015-07-27
Version: 2025-04-06"
  (interactive)
  (let (xbeg xend xinput)
    (seq-setq (xbeg xend)
              (if (and Begin End)
                  (list Begin End)
                (if (region-active-p)
                    (list (region-beginning) (region-end))
                  (let ((xurlstops "^ \t\n\"`'|()[]{}<>‘’“”"))
                    (list
                     (save-excursion (skip-chars-backward xurlstops) (point))
                     (save-excursion (skip-chars-forward xurlstops) (point)))))))
    (setq xinput (buffer-substring-no-properties xbeg xend))
    (delete-region xbeg xend)
    (insert
     (format "<a href=\"https://en.wikipedia.org/wiki/%s\">%s</a>" (string-replace " " "_" xinput) (string-replace "_" " " xinput)))))