Endless Parentheses

Concise ramblings on Emacs productivity.

Super Smart Capitalization

capitalize-word and downcase-word are godsends. Like many other commands, they've spoiled me to the point that I can no longer write prose without Emacs. There's just one peeve that bothers me quite often.

Let's say I want to join the following two sentences, and point is at the “B”.

Languages are fleeting. But Emacs is forever.

You could do DEL DEL , M-l, but that's so long! When I downcase the word “But”, it's perfectly obvious that I'll want to get rid of that full stop, so why can't M-l do that for me?

(global-set-key "\M-c" 'endless/capitalize)
(global-set-key "\M-l" 'endless/downcase)
(global-set-key "\M-u" 'endless/upcase)

(defun endless/convert-punctuation (rg rp)
  "Look for regexp RG around point, and replace with RP.
Only applies to text-mode."
  (let ((f "\\(%s\\)\\(%s\\)")
        (space "?:[[:blank:]\n\r]*"))
    ;; We obviously don't want to do this in prog-mode.
    (if (and (derived-mode-p 'text-mode)
             (or (looking-at (format f space rg))
                 (looking-back (format f rg space))))
        (replace-match rp nil nil nil 1))))

(defun endless/capitalize ()
  "Capitalize region or word.
Also converts commas to full stops, and kills
extraneous space at beginning of line."
  (interactive)
  (endless/convert-punctuation "," ".")
  (if (use-region-p)
      (call-interactively 'capitalize-region)
    ;; A single space at the start of a line:
    (when (looking-at "^\\s-\\b")
      ;; get rid of it!
      (delete-char 1))
    (call-interactively 'subword-capitalize)))

(defun endless/downcase ()
  "Downcase region or word.
Also converts full stops to commas."
  (interactive)
  (endless/convert-punctuation "\\." ",")
  (if (use-region-p)
      (call-interactively 'downcase-region)
    (call-interactively 'subword-downcase)))

(defun endless/upcase ()
  "Upcase region or word."
  (interactive)
  (if (use-region-p)
      (call-interactively 'upcase-region)
    (call-interactively 'subword-upcase)))

The snippet above will automatically convert between commas and full-stops when you're (un)capitalizing prose. It comes up on every single writing session for me.

Now I can just hit M-l and get

Languages are fleeting, but Emacs is forever.

Tags: writing, optimization, init.el, emacs

Say thanks on Gratipay
comments powered by Disqus