【问题标题】:Persistent colors in an emacs text bufferemacs 文本缓冲区中的持久颜色
【发布时间】:2013-06-29 13:52:57
【问题描述】:

在使用正则表达式 (1) 突出显示 emacs 缓冲区中的文本后,很容易将设置写入文件 (2),但我缺少持久性的第三步。

(1)设置

执行M-s h r (highlight-regexp) 和\{.*\} 后跟italic 将以该样式突出显示大括号之间的所有内容。

(2) 写

随后调用C-x w bhi-lock-write-interactive-patterns)将写入字符串

# Hi-lock: (("\\{.*\\}" (0 (quote italic) t)))

在缓冲区中,在请求注释字符串之后(我使用了#)。

(3) 重复使用

要使此突出显示持久化(即,使其能够从磁盘保存/加载文件),需要什么第三步?

【问题讨论】:

    标签: emacs emacs24


    【解决方案1】:

    如果您 C-h f hi-lock-write-interactive-pattern,您将在帮助缓冲区中看到指向 hi-lock.el 的链接。通常 Lisp 库在文件开头有一些使用信息,检查起来很方便。

    在这种情况下,它告诉如何使它持久化:

    ;;    To enable the use of patterns found in files (presumably placed
    ;;    there by hi-lock) include the following in your init file:
    ;;
    ;;    (setq hi-lock-file-patterns-policy 'ask)
    ;;
    ;;    If you get tired of being asked each time a file is loaded replace
    ;;    `ask' with a function that returns t if patterns should be read.
    

    【讨论】:

    • 感谢您的提示。我的 .emacs 中已经有 (setq hi-lock-file-patterns-policy t),但只有 (setq hi-lock-file-patterns-policy 'ask) 触发询问。为什么不问就直接写 t 就不会触发高亮?
    • 显然只写“t”是错误的(在 Emacs 24 中也是如此)。需要改写 (setq hi-lock-file-patterns-policy (lambda (pattern) t))。参考:lists.gnu.org/archive/html/bug-gnu-emacs/2010-01/msg00226.html
    【解决方案2】:

    是否有可能创建一个函数,该函数具有与您要加载的文件相关的钩子——例如,文本模式钩子或特定的文件钩子(如果存在类似的东西)?

    ;; M-x ae-hi-lock-features
    
    (global-hi-lock-mode 1)
    
    (defface af-bold-yellow-box '((t  (:background  "black" 
                                       :foreground  "yellow"
                                       :underline "red"
                                       :height 200
                                       :bold t
                                      )))  "yellow-box-face")
    
    (defun z-hi-lock-quizzes ()
      ;; this next line is necessary to correct sloppy hi-locking
      (if (not hi-lock-mode) 
          (progn (hi-lock-mode -1) 
                 (hi-lock-mode  1)) 
        (hi-lock-mode) 
        (hi-lock-mode))
      (highlight-regexp "^%-\\*-mode:LaTeX.*$" (quote hi-conceal-content));
      (highlight-regexp "^%-@-(.+$"            (quote hi-lock-page-break));
      (highlight-regexp "food"            (quote af-bold-yellow-box));
    )
    
    (defun ae-hi-lock-features ()
       (interactive)
       (z-hi-lock-quizzes)
    ;;   ... call other functions ...
    )
    
    (add-hook 'text-mode-hook 'ae-hi-lock-features)
    

    【讨论】:

    • hi-lock-write-interactive-patterns 的存在似乎强烈暗示我在我的文本文件中只缺少一个或两个行。
    【解决方案3】:

    https://www.gnu.org/software/emacs/manual/html_node/emacs/Highlight-Interactively.html

    C-x w i

    从当前缓冲区中的 cmets 中提取正则表达式/人脸对 (hi-lock-find-patterns)。

    因此,您可以交互输入模式 使用高亮正则表达式,将它们存储到文件中 hi-lock-write-interactive-patterns,编辑它们(也许包括 比赛的不同括号部分的不同面孔),和 最后使用这个命令(hi-lock-find-patterns)来拥有 Hi Lock 突出显示已编辑的图案。

    【讨论】:

    • M-x hi-lock-write-interactive-patterns 是所需的功能,而(来自 emacs 信息)“C-x w i 运行命令 hi-lock-find-patterns。”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-14
    • 2011-03-05
    • 1970-01-01
    • 2016-01-22
    相关资源
    最近更新 更多