“Smart” Quotes in Emacs
smart-quotes.el
smartly enables automatic insertion of correctly curled single or double quotation marks inemacs
.This is typographically nice, but can be deadly in Unicode-enabled programming languages where a
"string starting with a dumb-quote, but terminated by a smart one”
is unterminated for the parser but looks terminated for the human reader. And pressingC-v
at front/end of every string gets exhausting.Disabling smart quotes for certain modes is easy:
(defun my/smquoteoff () (smart-quote-mode 0)) (add-hook 'emacs-lisp-mode 'my/smquoteoff) (add-hook 'python-mode 'my/smquoteoff)But what about literate programming, as for example in
org-babel
codeblocks? The buffer is inorg-mode
, definitely not a programming mode, but the blocks inside#+begin/end_src
are:* About strings [...] Some “smart” statement about /strings/ #+begin_src s = "dumbly quoted string"; ... #+end_src
org-mode
has a predicate to check if point is inside such a code block, namedorg-in-src-block-p
, but how to use it without rewritingsmart-quotes.el
?Emacs' lisp
advice-add
to the rescue! This is Aspect-oriented programming, where a functiong
is wrapped in another functionf
. It has a:before-until
“combinator”, which evalsg
only iff
returnsnil
when run withg
's parameters.In this case:
(defun _my/verb-double-quote (&rest r) (if (org-in-src-block-p) (progn (insert-char #x22) t) nil)) (advice-add 'smart-quotes-insert-double :before-until #'_my/verb-double-quote)smart-quotes-insert-double
now depends onorg-in-src-block-p
being false, otherwise ASCII char0x22
, i.e"
, is inserted.Single quotes are left as an ‘exercise for the reader’.
Mon, 18 Mar 2024
[/quotes]
permanent link