I am trying to add a new comment character in fundamental mode, but I can't get it to work.

Let me rephrase that, because I think it's an xy-problem.

What I really would like to have is the possibility, in Fundamental mode, to give certain swaths of text a different face (1).

For instance, mark a region, then with a key-combo or menu-item, give that marked text a very light grey background.

I was starting out by the assumption that you can only do this by setting a custom comment character, and thus have the swath text be treated a s a comment and hence a different face.

Going on that assumption, in Fundamental mode, I would like to have all lines that start with the sequence "pipe character and a space" i.e. in regex something like '[[:space:]]*| .*' to be treated as comments (and thus have a different face).

But I cannot get it to work even in its simplest iteration; I thought this would work, but it doesn't:

(defun fundamental-mode-set-comment()
    (font-lock-add-keywords 'fundamental-mode
        '(("\\(| .*$\\)" 1 'font-lock-comment-face t))

(add-hook 'fundamental-mode-hook 'fundamental-mode-set-comment)

Even though my mode line says ('Fundamental' ...), i.e. Fundamental mode is in effect, I cannot get the face change to work.

Is there a way to get what I want without resorting to a new comment type? Am I going about this the right way, given (1)?

(I am using emacs 25.3.2.)

  • 1
    Fundamental mode doesn't have a hook variable. You will have to use some other mechanism to enable it. One thing is a global minor mode that checks if a buffer is in fundamental mode before enabling itself. Another is to use change-major-mode-hook. Nov 3 '20 at 22:59
  • Have you tried hi-lock-mode?
    – Drew
    Nov 4 '20 at 2:20
  • hi-lock does the job if you want to highlight with regex. If you only need to highlight same words, symbol-overlay does it and feels smoother than hi-lock.
    – JJPandari
    Nov 4 '20 at 6:53
  • @Drew I am presently trying out hi-lock-mode. The files I intend to use it on are basic text files, in Fundamental mode. Hi-lock comes close. However, when I close and re-open a file that has my hi-lock highlighting (I chose font-lock-comment-face for the highlighting), it now also has all quoted strings in font-lock-string-face, which looks horribly distracting.
    – Roadowl
    Nov 4 '20 at 20:19
  • Also, I find that after having hi-locked a certain amount of regions, it suddenly stops working for other regions that need to be hi-locked with the same regex. I think that's a bug. Closing/opening the file then usually works (although I have to re-hi-lock the entire buffer once to get all regions hi-locked).
    – Roadowl
    Nov 5 '20 at 1:21

Your Answer

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

Browse other questions tagged or ask your own question.