使用字体锁定功能的功能需要重新启动字体锁定模式



我对字体锁定模式的参与感到困惑。我没有在init.el中启动字体锁定模式的语句,但显然它总是以较小的模式运行。此外,我具有以下功能:

(defun testregexfunc ()
  (interactive)
  (make-variable-buffer-local 'font-lock-extra-managed-props)
  (add-to-list 'font-lock-extra-managed-props 'invisible)
  (font-lock-add-keywords nil
                          '(("\(\[\)\([a-zA-Z0-9_]+\)\(\]\)"
                             (1 '(face nil invisible t))
                             (3 '(face nil invisible t))))))

它使用特定的字体锁。但是,只有在我进行M-x testregexfunc之后两次M-x font-lock-mode后才生效。第一次禁用字体锁定模式第二次启动它。但是现在它不是作为主要模式运行的,因为缓冲区仍然显示以前使用的任何模式。好的,所以我猜该功能设置了一些值,只有在模式重新启动后才生效。我认为也许我需要在类似的字体锁定模式中添加钩子:

(add-hook
 'font-lock-mode
 'testregexfunc)

不...什么都不做。我需要做什么才能重新启动字体锁定模式才能工作?

我从这里获得了该功能,并修改了一些功能。我不了解它的大部分定义,字体锁上的文档并没有真正对我有很大帮助:

https://emacs.stackexchange.com/questions/28154/ususe-font-lock-regexp-groups

我认为您要寻找的功能是font-lock-flushfont-lock-ensure,它们共同声明了缓冲区的字体锁定外,然后对其进行重新审核。因此,您可以如下更改功能,

(defun testregexfunc (arg)
  "Fontify buffer with new rules. With prefix arg restore default fontification."
  (interactive "P")
  (if arg
      (font-lock-refresh-defaults)      ;restore the defaults for the buffer
    (make-variable-buffer-local 'font-lock-extra-managed-props)
    (add-to-list 'font-lock-extra-managed-props 'invisible)
    (font-lock-add-keywords nil ;make the "[" and "]" invisible
                            '(("\(\[\)\([a-zA-Z0-9_]+\)\(\]\)"
                               (1 '(face nil invisible t))
                               (3 '(face nil invisible t)))))
    (font-lock-flush)                   ;declare the fontification out-of-date
    (font-lock-ensure)))                ;fontify the buffer using new rules

最新更新