Endless Parentheses

Ramblings on productivity and technical subjects.

profile for Malabarba on Stack Exchange

Ispell and Apostrophes

Typography post series

If you’ve been following our journey of typography, you must now have pretty apostrophes all over your org documents. But if that’s the case, you probably also noticed a drawback. Ispell doesn’t like them very much. Now how are we supposed to use our amazing auto-correct?

Don’t despair, dear reader, I have the solution. It wasn’t trivial to come up with, but it’s quite simple to understand.

  1. Tell Ispell that apostrophes are just like hard-quotes, where it comes to constituting a word.
  2. Convert all ’ to ' as they’re being sent to the Aspell subprocess.
  3. Convert them back when reading the subprocess output. We even take care to only do that in org-mode, so that it won’t get in the way of your LaTeX buffers.

The following code uses the new advice system in Emacs 24.4. It can certainly be made to work on older Emacs by changing it to use defadvice.

(require 'ispell)

;;; Tell ispell.el that ’ can be part of a word.
(setq ispell-local-dictionary-alist
      `((nil "[[:alpha:]]" "[^[:alpha:]]"
             "['\x2019]" nil ("-B") nil utf-8)))

;;; Don't send ’ to the subprocess.
(defun endless/replace-apostrophe (args)
  (cons (replace-regexp-in-string
         "’" "'" (car args))
        (cdr args)))
(advice-add #'ispell-send-string :filter-args
            #'endless/replace-apostrophe)

;;; Convert ' back to ’ from the subprocess.
(defun endless/replace-quote (args)
  (if (not (derived-mode-p 'org-mode))
      args
    (cons (replace-regexp-in-string
           "'" "’" (car args))
          (cdr args))))
(advice-add #'ispell-parse-output :filter-args
            #'endless/replace-quote)

This is, admittedly, a hacky solution. But it’s been working well for me for the last few weeks. Feel free to shout and scream at me if you run into issues.

Update <2015-05-18 Mon>

Fixed the third regexp.

comments powered by Disqus