2

Since I learned how to colorize man pages using LESS_TERMCAP I find them much more easy to read.

I would like to achieve the same with Info pages in emacs. For example, colorize function names, variables, example ELISP code, etc.

I tried customize-face RET info- TAB but I did not find any suitable option.

  • You're not actually colorizing keywords in manual pages that way; you're just colorizing bold, underlined, or "standout" mode text, which styles are by convention used for things like program names. The same isn't done in e.g. the Emacs Lisp manual, so the same technique won't work. On the other hand, there are reliable conventions for how variables, functions, macros, etc. are introduced, so you could key off those instead. – Aaron Miller Apr 7 '16 at 20:22
  • Thanks for the detailed explanation. Could you provide an example of how to colorize strings that begin with -- Function: in info-mode? I am new to color customization. – Marduk Apr 7 '16 at 20:27
3

(Expanding from the commentary on the question:)

You're not actually colorizing keywords in manual pages that way; you're just colorizing bold, underlined, or "standout" mode text, which styles are by convention used for things like program names. The same isn't done in e.g. the Emacs Lisp manual, so the same technique won't work. On the other hand, there are reasonably reliable conventions for how variables, functions, macros, etc. are introduced, so you could key off those instead.

Here's an example of how you might do that. If you're new to Emacs, this may look a little weird, but a lot of things are done this way: instead of relying on complex and fragile regular expressions or parsing an AST out of the buffer contents, you just use more or less the same editing commands you would if you were making your desired changes by hand, and wrap them in a save-excursion form so that Emacs puts the cursor (point), scroll, current buffer, and so forth back how they were before you started. It takes a little getting used to, but it's a lot easier than any other way I've ever tried.

The bindings for type-face, name-face, and args-face can be changed to any face that's defined; I just picked these off the top of my head. The font-lock-* faces in particular might be of interest to you, since those are used in most programming modes.

(defun niceify-info nil
  "Highlight function, variable, macro, etc. description headers
in Info with arbitrary faces."
  (let ((type-face 'italic)
        (name-face 'bold)
        (args-face 'italic)
        (what-it-was inhibit-read-only))
    (unwind-protect
         (let (from to line-start)
           (setq inhibit-read-only t)
           (save-match-data
             (save-excursion
               (beginning-of-buffer)
               (while (re-search-forward "^ -- " nil t)
                 (save-excursion
                   (beginning-of-line)
                   (setq line-start (point)))

                 (setq from (point))
                 (re-search-forward ":" nil t)
                 (setq to (point))
                 (add-face-text-property from to type-face)

                 (re-search-forward " " nil t)
                 (setq from (point))
                 (re-search-forward " " nil t)
                 (setq to (point))
                 (add-face-text-property from to name-face)

                 (setq from (point))
                 (end-of-line)
                 (add-face-text-property from (point) args-face)))))
      (set-buffer-modified-p nil)
      (setq inhibit-read-only what-it-was))))

;; apply this function whenever an info page is selected
(add-hook 'Info-selection-hook
          #'niceify-info)

NOTE WELL: The preceding is a hack. I tossed it off in a few minutes, and while it works for me and I think it'll work for you, I can't guarantee it will work well, or at all, for anyone. Use it at your own risk.

Bonus points: Add another hook, or expand this one, to identify and mark up instances of the `name' convention, used to call out the names of interesting things like functions or variables. Link them to documentation of the things they name, the same way that's already done in Help buffers.

More bonus points: Use the type of thing described on a line (function, variable, etc.) to select the face applied to its name (font-lock-function-name-face, font-lock-variable-name-face, etc.)

More bonus points still: Link those names to their corresponding documentation, too.

All the bonus points: Extend this into a reliable, configurable minor mode, and distribute it as an Emacs package via MELPA. (Unless I beat you to it.)

| improve this answer | |
  • Wow! Thank you very much! There is a lot I have to learn from your code. You will definitely beat me to turning this into an Emacs package and I think a lot of people will appreciate it. – Marduk Apr 7 '16 at 21:05
0

Add your keywords to the list of the major mode, this example uses standard faces :

(font-lock-add-keywords 'man-mode
  '(("Everything is cool" . font-lock-keyword-face)
    ("Oh, noes!" . font-lock-warning-face)))
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.