5

I have a project, Tufte CSS, that I hand-write in HTML. I want to use smart quotes (aka fancy quotes or curved quotes) in the text of that document, but dumb quotes (aka straight quotes) in the HTML tags of that document.

How do I achieve this? I currently use web-mode and typopunct.el, but the latter automatically inserts smart quotes everywhere, including around HTML attributes, which it thereby causes not to function.

2 Answers 2

3

It turns out, this is a solved problem. Typopunct is already able to use curvy quotes in text and straight quotes inside tags for a variety of modes. I happen to edit HTML in a mode typopunct doesn't cover out of the box, but that's no problem. See typopunct-mode.el:

(defcustom typopunct-mode-exeptions-alist
  '((sgml-mode . typopunct-point-in-xml-tag-p)
    (nxml-mode . typopunct-point-in-xml-tag-p)
    (html-mode . typopunct-point-in-xml-tag-p))
  "Alist for mode specific expections.

This alist specifies major mode specific expectional cases when the
function `typopunct-insert-quotation-mark' should *not* insert
typographical quotation marks.

Each element is a pair of a major mode (a symbol) and a predicate
function that should return non nil, when
`typopunct-insert-quotation-mark' should insert an ASCII `\"'."
  :group 'typopunct
  :type '(alist :key-type symbol :value-type function))

Great! Typopunct already has a function to check if it's inside an XML-style angle-bracket tag, and is configured to not insert smart quotes if that's the case while in certain modes.

Instead of html-mode, I use web-mode. So, I added the following to my init.el:

(add-to-list 'typopunct-mode-exeptions-alist 
             '(web-mode . typopunct-point-in-xml-tag-p))

...and everything is right in the world. Remember, kids: it's just elisp, and most of the time the problem you're having is a problem someone else has had before.

1
  • I'll add a web-mode-point-in-xml-tag to web-mode.el so that typopunct can use it
    – fxbois
    Aug 18, 2015 at 15:44
0

You can try the following:

(defun insert-html-quote ()
  (interactive)

  (save-restriction
    (save-excursion

      (let (backward-nearest-< backward-nearest->)

        (save-excursion
          (search-backward "<")
          (setq backward-nearest-< (point)))

        (save-excursion
          (search-backward ">")
          (setq backward-nearest-> (point)))

        (if (> backward-nearest-< backward-nearest->)
            (insert "\"\"")
          (insert "“”"))))

    (forward-char)))

To bind it to quote char of html-mode-map:

(define-key html-mode-map (kbd "\"") 'insert-html-quote)
1
  • Even after I switched the define-key to web-mode-map I can't get this to work...I think I need to get deeper into the innards of typopunct.
    – Dave
    Aug 15, 2015 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.