質問

I am trying to highlight several invisible symbols in Emacs, specifically \n. I am trying the following:

(standard-display-ascii ?\n "¬\n")
(font-lock-add-keywords nil '(("¬" . font-lock-comment-face)))

Unfortunately, it looks like only typed explicitly symbols will use the specified font-face. Is there a proper way to highlight the display-ascii symbol?

One more related question: replacing nil with 'lisp-interaction-mode in the second expression makes it not working anymore. Why is that?

役に立ちましたか?

解決 3

  1. Highlighting newline chars is trivial using the library highlight-chars.el:

    M-x hc-highlight-chars C-q C-j RET font-lock-comment-face RET
    

    You are prompted for the chars to highlight -- hit C-q C-j (inserts a newline char), then hit RET to enter the list of chars you inserted (just a newline char here).

    You are then prompted for the face to use to highlight those chars (in this case, just one char, newline) -- type font-lock-comment-face or whatever other face name you like.

    See description of the library here.

  2. And if you want to also change the display to show (the highlighted char) ¬, then just do also what you already tried:

    M-: (standard-display-ascii ?\n "¬\n")
    

The result of #1 + #2 is highlighted ¬ in place of the usual newline display.

他のヒント

I just booted up emacs, did the command M-x, and typed

whitespace-mode

Afterwards I got a $ indicator for newlines.

To only show newlines the following can be used:

(global-whitespace-newline-mode)

or an alternative:

(setq whitespace-style '(face newline-mark))
(whitespace-mode t)

And to use the custom symbol ¬ for it:

(setq whitespace-display-mappings
      '((newline-mark 10 [172 10])))

Then the whitespace-newline font-lock can be used to customize the style.

Edit:

For some reason placing this customization in .emacs config results in faces styles not applying to the symbol, I am not sure why (would be great if somebody could explain this). Using hooks works fine:

(add-hook 'prog-mode-hook
          (lambda () 
            (whitespace-newline-mode t)))
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top