Search code examples
emacssyntax-highlightingfont-lockemacs-faces

Change face of plain text between double quotation marks in Emacs


I am looking for a way to highlight or use different face of quoted text in plain text. It seems that there should be a sophisticated/enhanced text mode but I cannot find it.

If there isn't a easy solution, can you let me know where should I begin to write a function?

Thank you very much!

A noob who has been using Emacs from 19.xx


Solution

  • I'm not sure about a major-mode that already does this, but you can make one easily enough using define-derived-mode

    (define-derived-mode rich-text-mode text-mode "Rich Text"
      "text mode with string highlighting."
    
      ;;register keywords
      (setq rich-text-font-lock-keywords
            '(("\"\\(\\(?:.\\|\n\\)*?[^\\]\\)\"" 0 font-lock-string-face)))
      (setq font-lock-defaults rich-text-font-lock-keywords)
      (font-lock-mode 1))
    

    Alternatively, you can add a hook to text-mode:

    (defun add-quotes-to-font-lock-keywords ()
      (font-lock-add-keywords nil '(("\"\\(\\(?:.\\|\n\\)*?[^\\]\\)\"" 0 font-lock-string-face))))
    
    (add-hook 'text-mode-hook 'add-quotes-to-font-lock-keywords)
    

    Generally speaking, a good mode for editing any text is org-mode. It does not font-lock strings by default, though.