Emphasize Text By Dragging Mouse in Org-mode

Long story short, I came up with an idea to emphasize content while I was reviewing an org-mode document a few months ago after I was tired of typing tedious * around the content repeatedly. And I've been longing for this feature since then, because I was unfamiliar with mouse-based key bindings, and neither did I have the time to investigate it.

Today I finally nailed it down while I was attending a boring meeting, and it turned out to be quite simple. The exploration journey was not much different than before. First, I managed to find out how to bind a command to a mouse event, taking advantage of the good-old C-h k and (info "emacs") (or here at gnu.org). And then wrote a few lines of code to perform the task standing on the shoulder of Advice.

So here is the advice:

(define-advice mouse-set-region (:after (click) org-highlight ())
  (when (and (derived-mode-p 'org-mode)
    (let ((origin (buffer-substring (region-beginning) (region-end)))
          (emphasis-char "*"))
      (delete-region (region-beginning) (region-end))
      (insert emphasis-char origin emphasis-char))))

See also

comments powered by Disqus