Frage

I have entered the following code in my .emacs file to highlight unwanted white spaces.

(require 'whitespace)
(setq whitespace-style '(face empty tabs lines-tail trailing))
(global-whitespace-mode t)

This shows (1) empty lines at the beginning & end of buffer (2) tabs (3) lines which go over the 80 character limit (4) trailing white spaces

I would like emacs to automatically highlight '2 or more empty lines'. Any ideas on how to implement this? I did find a blog post explaining a way to do this with the help of regexp, but I am not sure how to implement this in .emacs file.

Edit 1: Found a way to delete extra blank lines but this still doesn't help me with highlighting multiple blank lines automatically. delete extra blank lines in emacs

Edit 2: Adding the following to .emacs seems to work, but only after I save and reopen file in a buffer.

(add-hook 'change-major-mode-hook '(lambda () (highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)))

Edit 3: After adding (global-hi-lock-mode 1) to .emacs file just before the line in Edit 2, it seems to highlight 1 or more empty lines within the buffer. I am not sure how to modify the regexp so that it will only accept 2 or more empty lines.

War es hilfreich?

Lösung

Just use library Highlight (highlight.el). That's what it's for.

Use command hlt-highlight-regexp-region (C-x X h x) or hlt-highlight-regexp-to-end (C-x X h e). (To unhighlight a regexp, use C-x X u x or C-x X u e.)

Interactively, you input the regexp to use as usual in Emacs (with C-q C-j to match a newline character, and no need for double backslashes), so you type \(^\s-*$\) C-q C-j.

Andere Tipps

Your highlight-regexp-solution can be made into a minor-mode with the following elisp (e.g., in your .emacs file).

You can activate the minor mode by right-clicking onto one of the mode-names in the mode-line and then selecting nl2-mode. You can deactivate the minor mode by clicking on nl2 in the mode line and selecting Turn off minor mode.

To understand the code see the help for define-minor-mode and define-key (e.g., C-h f define-minor-mode RET). Note, that in emacs also mouse clicks in menus count as key strokes.

(define-minor-mode nl2-mode
  "Highlight two successive newlines."
  :global t
  :lighter " nl2"
  (if nl2-mode
      (highlight-regexp "\\(^\\s-*$\\)\n" 'hi-yellow)
    (unhighlight-regexp "\\(^\\s-*$\\)\n")))

(define-key mode-line-mode-menu [nl2-mode]
  `(menu-item ,(purecopy "nl2-mode") nl2-mode
  :help "Highlight two succesive newlines."
  :button (:toggle . (bound-and-true-p nl2-mode))))

There are several facts that make highlighting two consecutive empty lines more complicated (font-lock tends to only highlight non-empty regions, linebreaks are limits for the region to re-fontify, re-fontification after buffer changes are required). The following code shows one way. Maybe, there are easier ways.

(require 'font-lock)
(global-font-lock-mode)

(defface jit-lock-nl2-face '((default :background "yellow"))
  "Face to indicate two or more successive newlines."
  :group 'jit-lock)

(defun jit-nl2-extend (start end &optional old)
  "Extend region to be re-fontified"
  (save-excursion
    (save-match-data
      ;; trailing:
      (goto-char end)
      (skip-chars-forward "[[:blank:]]\n")
      (setq jit-lock-end (point))
      ;; leading:
      (goto-char start)
      (beginning-of-line)
      (skip-chars-backward "[[:blank:]]\n")
      (unless (bolp) (forward-line))
      (setq jit-lock-start (point)))))

(defun jit-nl2 (jit-lock-start jit-lock-end)
  "Highlight two or more successive newlines."
  (save-excursion
    (save-match-data
      (jit-nl2-extend jit-lock-start jit-lock-end)
      ;; cleanup
      (remove-text-properties jit-lock-start jit-lock-end '(font-lock-face jit-lock-nl2-face))
      ;; highlight
      (while (< (point) jit-lock-end)
    (if (looking-at "[[:blank:]]*\n\\([[:blank:]]*\n\\)+")
        (progn (put-text-property (match-beginning 0) (match-end 0) 'font-lock-face 'jit-lock-nl2-face)
           (goto-char (match-end 0)))
      (forward-line))))))


(add-hook 'after-change-major-mode-hook (lambda ()
                      (add-hook 'jit-lock-after-change-extend-region-functions 'jit-nl2-extend)
                      (jit-lock-register 'jit-nl2)
                      (jit-lock-mode 1)
                      ))
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top