Emacs-24.2 中很重要的幾個函數--實現自定義語法高亮的關鍵

 Emacs-24.2 中很重要的幾個函數--實現自定義語法高亮的關鍵

經過這幾個函數就能夠清楚地看到 lisp 有哪些關鍵字,在以下文件 \emacs-24.2\lisp\font-lock.el :
;; Lisp.

(defconst lisp-font-lock-keywords-1
  (eval-when-compile
    `(;; Definitions.
      (,(concat "(\\(def\\("
		;; Function declarations.
		"\\(advice\\|alias\\|generic\\|macro\\*?\\|method\\|"
		"setf\\|subst\\*?\\|un\\*?\\|"
		"ine-\\(condition\\|"
		"\\(?:derived\\|\\(?:global\\(?:ized\\)?-\\)?minor\\|generic\\)-mode\\|"
		"method-combination\\|setf-expander\\|skeleton\\|widget\\|"
		"function\\|\\(compiler\\|modify\\|symbol\\)-macro\\)\\)\\|"
		;; Variable declarations.
		"\\(const\\(ant\\)?\\|custom\\|varalias\\|face\\|parameter\\|var\\)\\|"
		;; Structure declarations.
		"\\(class\\|group\\|theme\\|package\\|struct\\|type\\)"
		"\\)\\)\\>"
		;; Any whitespace and defined object.
		"[ \t'\(]*"
		"\\(setf[ \t]+\\sw+\\|\\sw+\\)?")
       (1 font-lock-keyword-face)
       (9 (cond ((match-beginning 3) font-lock-function-name-face)
		((match-beginning 6) font-lock-variable-name-face)
		(t font-lock-type-face))
	  nil t))
      ;; Emacs Lisp autoload cookies.  Supports the slightly different
      ;; forms used by mh-e, calendar, etc.
      ("^;;;###\\([-a-z]*autoload\\)" 1 font-lock-warning-face prepend)
      ;; Regexp negated char group.
      ("\\[\\(\\^\\)" 1 font-lock-negation-char-face prepend)))
  "Subdued level highlighting for Lisp modes.")

(defconst lisp-font-lock-keywords-2
  (append lisp-font-lock-keywords-1
   (eval-when-compile
     `(;; Control structures.  Emacs Lisp forms.
       (,(concat
	  "(" (regexp-opt
	       '("cond" "if" "while" "while-no-input" "let" "let*" "letrec"
		 "prog" "progn" "progv" "prog1" "prog2" "prog*"
		 "inline" "lambda" "save-restriction" "save-excursion"
		 "save-selected-window" "save-window-excursion"
		 "save-match-data" "save-current-buffer"
		 "combine-after-change-calls" "unwind-protect"
		 "condition-case" "condition-case-unless-debug"
		 "track-mouse" "eval-after-load" "eval-and-compile"
		 "eval-when-compile" "eval-when" "eval-next-after-load"
		 "with-case-table" "with-category-table"
		 "with-current-buffer" "with-demoted-errors"
		 "with-electric-help"
		 "with-local-quit" "with-no-warnings"
		 "with-output-to-string" "with-output-to-temp-buffer"
		 "with-selected-window" "with-selected-frame"
		 "with-silent-modifications" "with-syntax-table"
		 "with-temp-buffer" "with-temp-file" "with-temp-message"
		 "with-timeout" "with-timeout-handler" "with-wrapper-hook") t)
	  "\\>")
	  .  1)
       ;; Control structures.  Common Lisp forms.
       (,(concat
	  "(" (regexp-opt
	       '("when" "unless" "case" "ecase" "typecase" "etypecase"
		 "ccase" "ctypecase" "handler-case" "handler-bind"
		 "restart-bind" "restart-case" "in-package"
		 "break" "ignore-errors"
		 "loop" "do" "do*" "dotimes" "dolist" "the" "locally"
		 "proclaim" "declaim" "declare" "symbol-macrolet" "letf"
		 "lexical-let" "lexical-let*" "flet" "labels" "compiler-let"
		 "destructuring-bind" "macrolet" "tagbody" "block" "go"
		 "multiple-value-bind" "multiple-value-prog1"
		 "return" "return-from"
		 "with-accessors" "with-compilation-unit"
		 "with-condition-restarts" "with-hash-table-iterator"
		 "with-input-from-string" "with-open-file"
		 "with-open-stream" "with-output-to-string"
		 "with-package-iterator" "with-simple-restart"
		 "with-slots" "with-standard-io-syntax") t)
	  "\\>")
	  . 1)
       ;; Exit/Feature symbols as constants.
       (,(concat "(\\(catch\\|throw\\|featurep\\|provide\\|require\\)\\>"
		 "[ \t']*\\(\\sw+\\)?")
	(1 font-lock-keyword-face)
	(2 font-lock-constant-face nil t))
       ;; Erroneous structures.
       ("(\\(abort\\|assert\\|warn\\|check-type\\|cerror\\|error\\|signal\\)\\>" 1 font-lock-warning-face)
       ;; Words inside \\[] tend to be for `substitute-command-keys'.
       ("\\\\\\\\\\[\\(\\sw+\\)\\]" 1 font-lock-constant-face prepend)
       ;; Words inside `' tend to be symbol names.
       ("`\\(\\sw\\sw+\\)'" 1 font-lock-constant-face prepend)
       ;; Constant values.
       ("\\<:\\sw+\\>" 0 font-lock-builtin-face)
       ;; ELisp and CLisp `&' keywords as types.
       ("\\<\\&\\sw+\\>" . font-lock-type-face)
       ;; ELisp regexp grouping constructs
       ((lambda (bound)
          (catch 'found
            ;; The following loop is needed to continue searching after matches
            ;; that do not occur in strings.  The associated regexp matches one
            ;; of `\\\\' `\\(' `\\(?:' `\\|' `\\)'.  `\\\\' has been included to
            ;; avoid highlighting, for example, `\\(' in `\\\\('.
            (while (re-search-forward "\\(\\\\\\\\\\)\\(?:\\(\\\\\\\\\\)\\|\\((\\(?:\\?[0-9]*:\\)?\\|[|)]\\)\\)" bound t)
              (unless (match-beginning 2)
                (let ((face (get-text-property (1- (point)) 'face)))
                  (when (or (and (listp face)
                                 (memq 'font-lock-string-face face))
                            (eq 'font-lock-string-face face))
                    (throw 'found t)))))))
        (1 'font-lock-regexp-grouping-backslash prepend)
        (3 'font-lock-regexp-grouping-construct prepend))
;;;  This is too general -- rms.
;;;  A user complained that he has functions whose names start with `do'
;;;  and that they get the wrong color.
;;;      ;; CL `with-' and `do-' constructs
;;;      ("(\\(\\(do-\\|with-\\)\\(\\s_\\|\\w\\)*\\)" 1 font-lock-keyword-face)
      )))
  "Gaudy level highlighting for Lisp modes.")

(defvar lisp-font-lock-keywords lisp-font-lock-keywords-1
  "Default expressions to highlight in Lisp modes.")

(provide 'font-lock)

;;; font-lock.el ends here

首先,須要定義一些 face 也就是顏色模式, font-lock.el 中的定義以下: html

(defface font-lock-preprocessor-face
  '((t :inherit font-lock-builtin-face))
  "Font Lock mode face used to highlight preprocessor directives."
  :group 'font-lock-faces)

(defface font-lock-regexp-grouping-backslash
  '((t :inherit bold))
  "Font Lock mode face for backslashes in Lisp regexp grouping constructs."
  :group 'font-lock-faces)

(defface font-lock-regexp-grouping-construct
  '((t :inherit bold))
  "Font Lock mode face used to highlight grouping constructs in Lisp regexps."
  :group 'font-lock-faces)

也能夠本身定義一些簡單的,(來自http://wenku.baidu.com/view/881627a20029bd64783e2cc1.html),以下: shell

(defface cyan-face
'((t :foreground "cyan"))
"我自定義的cyan face")

(defface yellow-face
'((t :foreground "yellow"))
"我自定義的yellow face")

能夠經過這種方式來調用,也能夠參考 font-lock.el 中使用的方式來調用,簡單調用方式以下: express

(add-hook 'outline-minor-mode-hook
          '(lambda ()
             (hi-lock-set-pattern "^\\*[^*]*?$" 'cyan-face)
             (hi-lock-set-pattern "^\\*\\*[^*]*?$" 'yellow-face)
             (hi-lock-set-pattern "^\\*\\*\\*[^*]*?$" 'magenta-face)))
相關文章
相關標籤/搜索