Elisp: Word to Wikipedia HTML 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 ()
  "Make the current word or selection into a Wikipedia link.
For Example:
 Emacs
becomes
 <a href=\"http://en.wikipedia.org/wiki/Emacs\">Emacs</a>

URL `http://xahlee.info/emacs/emacs/elisp_html_word_to_wikipedia_linkify.html'
Version 2015-07-27 2021-09-17"
  (interactive)
  (let* ((xp0 (point))
         (xp1 (if (region-active-p)
                  (region-beginning)
                (progn (skip-chars-backward "^ \t\n") (point))))
         (xp2 (if (region-active-p)
                  (region-end)
                (progn (goto-char xp0)
                       (skip-chars-forward "^ \t\n")
                       ((point)))))
         (xinput (buffer-substring-no-properties xp1 xp2))
         (xlinkText (replace-regexp-in-string "_" " " xinput)))
    (delete-region xp1 xp2)
    (insert
     (format "<a href=\"http://en.wikipedia.org/wiki/%s\">%s</a>" (replace-regexp-in-string " " "_" xlinkText) xlinkText))))