4

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?

egdmitry
  • 2,071
  • 15
  • 18

3 Answers3

15

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

whitespace-mode

Afterwards I got a $ indicator for newlines.

merlin2011
  • 71,677
  • 44
  • 195
  • 329
5

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)))
egdmitry
  • 2,071
  • 15
  • 18
  • 1
    Here are some settings you can use to customize whitespace-mode: lines lines-tail newline trailing space-before-tab space-after-tab empty indentation-space indentation indentation-tab tabs spaces. And, here is an example of just spaces, tabs and end of lines: `(setq whitespace-style '(face space-mark tab-mark newline-mark) )` I don't use global modes, and instead prefer mode hooks and `(whitespace-mode t)` – lawlist May 11 '14 at 06:03
  • `(global-whitespace-newline-mode)` placed in `.emacs` is finicky for some reason, it enables the newline-mode globally and for all symbols. When I run it manually everything is fine. I tried your advice and it works much better. What does `face` mean in that list? I couldn't find it in the documentation. – egdmitry May 11 '14 at 06:27
  • 1
    `M-x describe-variable RET whitespace-style`: *. . . face -- enable all visualization via faces (see below). . . .* – lawlist May 11 '14 at 06:32
  • Thanks you. Regarding the hooks usage. Do you explicitly state which modes you want the whiteline to be enabled for? I would imagine it's tedious to add each mode to the list once you pick up a new language. Or is there some trick to make it easier? – egdmitry May 11 '14 at 07:18
  • 1
    I have a list of mode-hooks that I regularly use and add or subtract the desired options as needed -- e.g., `(add-hook 'text-mode-hook (lambda () (whitespace-mode t) (linum-mode 1) (hl-line-mode 1) . . .))`. Alternatively, you can have a global mode / function and insert conditions -- e.g., `(when (eq major-mode 'c-mode) . . .` There are other ways, but those are the most common that I use regularly. You could even wrap your common ones into a function -- e.g., `(defun my-favorite-minor-modes () (whitespace-mode t) (linum-mode 1) (hl-line-mode 1))` and put that function inside a mode hook. – lawlist May 11 '14 at 07:33
  • I have played with this all a little bit more and it seems like emacs doesn't pick custom faces styles when `(global-whitespace-newline-mode)` is placed in `.emacs`. Only if I call this manually after emacs starts. Where the problem could be? – egdmitry May 11 '14 at 07:42
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/52451/discussion-between-egdmitry-and-lawlist) – egdmitry May 11 '14 at 07:49
4
  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.

Ben
  • 574
  • 3
  • 12
Drew
  • 29,895
  • 7
  • 74
  • 104
  • Thanks. Do you know why my initial attempt didn't work well? Why `font-lock-add-keywords` doesn't play well with `standard-display-ascii`? – egdmitry May 11 '14 at 20:50
  • No, I don't really know. Perhaps someone will explain it well. But you have a solution anyway, right? ;-) – Drew May 11 '14 at 21:11
  • I do :) I ended up going with my solution because it doesn't require any additional extensions and seems to work fine. Although I have this terrible feeling when I don't understand why something doesn't work — I feel like if I find more issues with this I will not be able to fix them because I don't know how it works already. – egdmitry May 11 '14 at 21:29
  • Then I don't understand your original question/problem. You said that it does *not* work, AFAICT. And evaluating your two sexps alone in fact does not work, AFAICT -- the "display ascii symbol" is not highlighted, just as you stated. I thought that was your problem. Just what is your problem, for which you are "going with your solution"? And what is your solution to it? You should *post your solution clearly as an answer*. And accept it if it is the solution you prefer. It's about helping others and making things clear. – Drew May 11 '14 at 21:56
  • I have posted my solution as an answer. I can't accept it yet. I will modify it if it's not clear enough. – egdmitry May 11 '14 at 22:14
  • Dunno why, but I didn't notice that that answer was from you. Sorry for the noise. – Drew May 11 '14 at 22:56
  • You solution seems to perform better. Although I have an issue with it. First, I could add it to my config: `(hc-highlight-chars ?\n 'whitespace-newline)` — am I doing this right? I am getting an error. Second, for some reason the newline symbol in the code comments gets comment's face. I thought hc-highlight-chars would override that? – egdmitry May 13 '14 at 02:16
  • 1
    1. The first arg to `hc-highlight-chars` is a *list* of a *string* of chars, not a char -- e.g., `("\n")` -- see the doc string. (I just clarified the doc string a little; see the latest version) So, for example, `(hc-highlight-chars '("\n") 'whitespace-newline)` 2. See option `hc-other-chars-font-lock-override` for interaction with other font-locking -- you want `t` as value. In general, see the doc. – Drew May 13 '14 at 04:46