“Smart” Quotes in Emacs
smart-quotes.elsmartly 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-vat 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-babelcodeblocks? The buffer is inorg-mode, definitely not a programming mode, but the blocks inside#+begin/end_srcare:* About strings [...] Some “smart” statement about /strings/ #+begin_src s = "dumbly quoted string"; ... #+end_src
org-modehas 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-addto the rescue! This is Aspect-oriented programming, where a functiongis wrapped in another functionf. It has a:before-until“combinator”, which evalsgonly iffreturnsnilwhen 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-doublenow depends onorg-in-src-block-pbeing 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