让你的Emacs像vi一样高亮 “#if 0 ... #endif"


   1. 编辑你的.emacs配置文件
      vi ~/.emacs 或 emacs ~/.emacs
   2. 将以下函数粘贴到你的.emacs文件内
    
;;Highlight #if 0 to #endif
     (defun my-c-mode-font-lock-if0 (limit)
       (save-restriction
         (widen)
         (save-excursion
           (goto-char (point-min))
           (let ((depth 0) str start start-depth)
             (while (re-search-forward "^\\s-*#\\s-*\\(if\\|else\\|endif\\)" limit 'move)
               (setq str (match-string 1))
               (if (string= str "if")
                   (progn
                     (setq depth (1+ depth))
                     (when (and (null start) (looking-at "\\s-+0"))
                       (setq start (match-end 0)
                             start-depth depth)))
                 (when (and start (= depth start-depth))
                   (c-put-font-lock-face start (match-beginning 0) 'font-lock-comment-face)
                   (setq start nil))
                 (when (string= str "endif")
                   (setq depth (1- depth)))))
             (when (and start (> depth 0))
               (c-put-font-lock-face start (point) 'font-lock-comment-face)))))
       nil)

     (defun my-c-mode-common-hook ()
       (font-lock-add-keywords
        nil
        '((my-c-mode-font-lock-if0 (0 font-lock-comment-face prepend))) 'add-to-end))

     (add-hook 'c-mode-common-hook 'my-c-mode-common-hook)
     ;;Highlight end


   3. 重新启动Emacs看看^_^
      

你可能感兴趣的:(emacs)