Emacs Lisp: Change Current Word to Wikipedia Link

(defun xhm-word-to-wikipedia-linkify ()
  "Make the current word or text selection into a Wikipedia link.

For Example: 「Emacs」 ⇒ 「<a href=\"http://en.wikipedia.org/wiki/Emacs\">Emacs</a>」"
  (let (linkText bds p0 p1 p2 wikiTerm resultStr)

    (if (region-active-p)
          (setq p1 (region-beginning))
          (setq p2 (region-end)))
        (setq p0 (point))
        (skip-chars-backward "^ \t\n")
        (setq p1 (point))
        (goto-char p0)
        (skip-chars-forward "^ \t\n")
        (setq p2 (point))))

    (setq linkText (buffer-substring-no-properties p1 p2))
    (setq wikiTerm (replace-regexp-in-string " " "_" linkText))
    (setq resultStr (concat "<a href=\"http://en.wikipedia.org/wiki/" wikiTerm "\">" linkText "</a>"))
    (delete-region p1 p2)
    (insert resultStr)))

in Emacs: Xah HTML Mode

this and Emacs: Xah Emacs Lisp Mode i use heavily daily. The xah-html-mode is older, and few times more code and functions. But it's rather chaotic. the xah-elisp-mode is more clean.

Popular posts from this blog

Browser User Agent Strings 2012

11 Years of Writing About Emacs

does md5 creates more randomness?