Change face of plain text between double quotation marks in Emacs

回眸只為那壹抹淺笑 提交于 2020-01-01 19:32:11

问题


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


回答1:


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.




回答2:


For the regexp, I think you want to exclude " itself in the string content, except when escaped. Something like this --- ", followed by either a non " or an escaped character, followed by ":

\"\\([^\"]\\|\\\\\\(.\\|[\n]\\)\\)*\"

But be aware that matching quotations "..." is notorious. I do exactly that in Info+, but there are a few Info nodes where this highlighting gets thrown off by the occasional lone \" or ?\" in manuals that refer to such programming constructs.



来源:https://stackoverflow.com/questions/7155528/change-face-of-plain-text-between-double-quotation-marks-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!