3

In Emacs, I'd like to highlight the parts of long lines that exceed 80 characters.

The package highlight-80+ is great for that. But how can I automatically enable it when a C++ source file is loaded?

I tried to add highlight-80+ to the C++ mode, but it didn't work:

(require 'highlight-80+)
(defun my-c++-mode-common-hook ()
  (highlight-80+-mode 1))
(add-hook 'c++-mode-common-hook 'my-c++-mode-common-hook)

When I load a .cc file it goes in C++ mode but highlight-80+ is not enabled, so the long lines are not marked.

N.N.
  • 8,336
  • 12
  • 54
  • 94
Frank
  • 64,140
  • 93
  • 237
  • 324
  • Frank, I think you are almost there. In last line of your code its not `C++-mode-common-hook` but `C++-mode-hook` or `c-mode-common-hook` – kindahero Mar 16 '12 at 00:50

3 Answers3

2

Note that the Highlight80Plus wiki says that it is built-in to emacs starting with 23. I believe it's referring to whitespace-mode; it does this and is built in to emacs.

There is a function in emacs-starter-kit that does something like this already but you could easily duplicate it,

(defun esk-turn-on-whitespace ()
  (whitespace-mode t))

(add-hook 'prog-mode-hook 'esk-turn-on-whitespace)
N.N.
  • 8,336
  • 12
  • 54
  • 94
milkypostman
  • 2,955
  • 27
  • 23
1

Can you try this:

(autoload 'highlight-80+)
(add-to-list 'auto-mode-alist '("\\.cpp$" . highlight-80+-mode))
N.N.
  • 8,336
  • 12
  • 54
  • 94
moul
  • 543
  • 3
  • 10
  • Thanks. But then it's not C++ mode anymore, but only highlight-80+ mode, as a major mode. – Frank Mar 16 '12 at 00:46
  • Sorry, can you try to `(add-hook...` after a `(eval-after-load "c++-mode"` ? – moul Mar 16 '12 at 01:16
  • so it will look: `(eval-after-load "c++-mode" (add-hook 'c++-mode-common-hook 'my-c++-mode-common-hook))` – moul Mar 16 '12 at 01:18
  • `eval-after-load` is not needed here because you can add to the symbol of any hook regardless of it exists yet. And: If you use `eval-after-load` you have to quote the form to be eval'ed then otherwise it will be eval'ed right away, thus `(eval-after-load "c++-mode" '(add-hook 'c++-mode-common-hook 'my-c++-mode-common-hook))` – Michael Markert Mar 16 '12 at 08:07
1

See the whitespace-mode, it does this kind of highlighting and more:

http://www.emacswiki.org/emacs/WhiteSpace

Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110