突出显示emacs中的列

时间:2016-01-22 18:48:12

标签: emacs

以下是我的目标示例:

enter image description here

如何自定义我的.emacs以突出显示具有一种背景/字体颜色的列1-10,具有不同背景/字体颜色的列11-20等等?我不确定一个好的起点是什么。我看过这篇文章,但它根本没有我需要的东西。 How can I make emacs highlight lines that go over 80 chars?

1 个答案:

答案 0 :(得分:1)

我确定有更优雅的解决方案,但我使用了column-marker library中的部分内容。以下是相关部分:

(defun column-marker-find (col)
  "Defines a function to locate a character in column COL. Returns the function symbol, named `column-marker-move-to-COL'."
  (let ((fn-symb  (intern (format "column-marker-move-to-%d" col))))
    (fset `,fn-symb
          `(lambda (end)
             (let ((start (point)))
               (when (> end (point-max)) (setq end (point-max)))
           (message "this -> %d" (point-max))

               ;; Try to keep `move-to-column' from going backward, though it still can.
               (unless (< (current-column) ,col) (forward-line 1))

               ;; Again, don't go backward.  Try to move to correct column.
               (when (< (current-column) ,col) (move-to-column ,col))

               ;; If not at target column, try to move to it.
               (while (and (< (current-column) ,col) (< (point) end)
                           (= 0 (+ (forward-line 1) (current-column)))) ; Should be bol.
                 (move-to-column ,col))

               ;; If at target column, not past end, and not prior to start,
               ;; then set match data and return t.  Otherwise go to start
               ;; and return nil.
               (if (and (<= ,col (current-column)) (<= (point) end) (> (point) start) )
                   (progn (set-match-data (list (1- (point)) (point)))
                          t)            ; Return t.
         (message "column %d" (current-column))
                 (goto-char start)
                 nil)))) ; Return nil.
    fn-symb)
  )


(defun column-marker-internal (sym col &optional face)
  "SYM is the symbol for holding the column marker context. COL is the column in which a marker should be set. Supplying nil or 0 for COL turns off the marker. FACE is the face to use.  If nil, then face `column-marker-1' is used."
  (when col                             ; Generate a new column marker
    (set sym `((,(column-marker-find col) (0 ',face prepend t))))
    (font-lock-add-keywords nil (symbol-value sym) t)
    )
  (font-lock-fontify-buffer)
  )

使用以下命令部署列突出显示:

  (defface column-marker '((t (:background "gray"))) "" :group 'faces)  
  (defvar column-marker-face 'column-marker "" )
  (setq num 0)
  (while (< num 10)
    (column-marker-internal 'column-marker (+ 11 num) column-marker-face)
    (setq num (1+ num)))
相关问题