如何在Emacs'中突出显示所有功能的名称LISP模式?

时间:2013-08-17 13:25:57

标签: emacs lisp elisp syntax-highlighting font-lock

如何在Emacs的lisp模式中突出显示所有功能的名称?我希望他们加粗。

换句话说,从(到第一个space的所有字词。 不关心例外(a . b)

就像GitHub:

enter image description here

2 个答案:

答案 0 :(得分:6)

下面的代码突出显示已知Emacs-Lisp函数的名称。

请注意,即使某个给定函数名称的出现不代表该函数,它也会这样做。例如,名称可以用作变量名称。在实践中不是一个大问题,但很高兴知道。

;; `setq' is a variable here, but it is highlighted anyway.
(let ((setq  (foobar)))...)

要在Emacs-Lisp模式下自动打开突出显示,请执行以下操作:

(font-lock-add-keywords 'emacs-lisp-mode
                        '((my-fl . 'font-lock-constant-face)) ; Or whatever face you want.                            'APPEND)

(defun my-fl (_limit)
  (let ((opoint  (point))
        (found   nil))
    (with-syntax-table emacs-lisp-mode-syntax-table
      (while (not found)
        (cond ((condition-case ()
                   (save-excursion
                     (skip-chars-forward "'")
                     (setq opoint  (point))
                     (let ((obj  (read (current-buffer))))
                       (and (symbolp obj)  (fboundp obj)
                            (progn (set-match-data (list opoint (point))) t))))
                 (error nil))
               (forward-sexp 1)
               (setq opoint  (point)
                     found   t))
              (t
               (if (looking-at "\\(\\sw\\|\\s_\\)")
                   (forward-sexp 1)
                 (forward-char 1)))))
      found)))

注意:如果你想看到只有这个突出显示的效果,那么首先在Emacs-Lisp模式缓冲区中执行此操作,以摆脱其他Emacs-Lisp字体锁突出显示:

M-: (setq font-lock-keywords  ()) RET

更新---

我为此创建了一个次模式命令和库:

它允许您突出显示已定义的Emacs-Lisp符号:函数和变量,仅函数或仅变量。或者,您只能突出显示已定义的 符号。

答案 1 :(得分:5)

使用此:

(defface font-lock-func-face 
    '((nil (:foreground "#7F0055" :weight bold))
      (t (:bold t :italic t)))
  "Font Lock mode face used for function calls."
  :group 'font-lock-highlighting-faces)

(font-lock-add-keywords 
 'emacs-lisp-mode
 '(("(\\s-*\\(\\_<\\(?:\\sw\\|\\s_\\)+\\)\\_>"
    1 'font-lock-func-face)))

一个有趣的事情:这与let绑定混淆,就像Github一样。 但那就是你要求的,对吧:)?