1

I have this code:

(add-hook
  'org-mode-hook
  (lambda ()
     (when (string-match "notes\\.org$" buffer-file-name)
       (face-remap-add-relative 'default :background "yellow"))))

and it should change the background color of the buffer where is notes.org file.

However, it doesn't work.

I have tried this as well:

(add-hook
  'org-mode-hook
  (lambda ()
     (when (string-match "notes\\.org$" buffer-file-name)
       (set-face-attribute 'default nil :background "yellow"))))

But it is still not working.

The echo area says if: Wrong type argument: stringp, nil

Any idea what am I doing wrong?

8
  • How about trying: ... (when (and (not (null buffer-file-name)) (string-match-p "notes\\.org$" buffer-file-name)) ...) ...
    – lawlist
    Jul 17, 2022 at 15:22
  • The same error is popping up.
    – fegax
    Jul 17, 2022 at 15:31
  • 1
    (add-hook 'org-mode-hook (lambda () (when (and (not (null buffer-file-name)) (string-match-p "notes\\.org$" buffer-file-name)) (face-remap-add-relative 'default '(:foreground "black" :background "yellow")))))
    – lawlist
    Jul 17, 2022 at 15:36
  • 1
    @lawlist: (not (null X)) is the same as X. ;-)
    – Drew
    Jul 17, 2022 at 16:09
  • 1
    @lawlist: Please add your answer. It depends on what the question is really about. Is it about fixing OP's code or about answering how to debug such a problem. I guess OP should decide what question is really being asked.
    – Drew
    Jul 17, 2022 at 16:11

2 Answers 2

1

The following answer provides a working solution to the underlying issue of the O.P., and contains a couple of suggestions made by @Drew in both his alternative answer and in a comment underneath the original question. In terms of how to debug the issue posed by the O.P., please refer to the alternative answer by @Drew.

(add-hook 'org-mode-hook
          (lambda ()
            (when (and buffer-file-name
                       (string-match-p "notes\\.org$" buffer-file-name))
              (face-remap-add-relative 'default
                                       '(:foreground "black" :background "yellow")))))
1
  • Thanks, it works. Just one important thing for future visitors. You have to restart Emacs - or at least in my case when using Spacemacs. For some reason just reloading the .spacemacs file via SPC f e R doesn't work. You have to restart or reopen Emacs. For example, if you change "yellow" to "blue" and only reload config it will stay like that even if you close the buffer and reopen it. But after closing and starting Emacs again it will change to blue ;)
    – fegax
    Jul 17, 2022 at 20:18
1

buffer-file-name is returning nil, and string-match expects (needs) a string instead. So string-match raised an error telling you that.

buffer-file-name is returning nil because the current buffer is not visiting a file. The buffer in which you've turned on Org mode is not a file buffer. (E.g., it's a buffer such as *scratch*, which is not associated with a file.)

(Also, in general, if you're not going to use the match-data then you should use string-match-p, not string-match, as it's more efficient.)

For debugging, do this: M-x set-variable RET debug-on-error RET t RET. That shows you a backtrace, which lets you know which function raised the error and shows you the context/history of the evaluation.

Clearly you hadn't understood that the problem wasn't reported by function string-match. You thought it had something to do with your code about faces. Use non-nil debug-on-error to help with this kind of thing. You can customize it to t, so it's on by default: M-x customize-option RET debug-on-error t.

3
  • I have this code inside (defun dotspacemacs/user-config () in .spacemacs file and I am refreshing/reloading this file while being inside by pressing SPC f e R and then this error shows up in the minibuffer. So, I want the background color to change only for notes.org and I need to compare it somehow with the current buffer name. According to you the condition is bad because the buffername returns nil and the name "notes\\.org$" is string => so the comparison fails and the function stops. Right? So, how to make it "not stop" and continue working?
    – fegax
    Jul 17, 2022 at 15:22
  • Could you help with my code? Perhaps try it in your config for some file if the backgroudn color can be changed like this or not. Thank you in advance.
    – fegax
    Jul 17, 2022 at 15:33
  • This site isn't really about fix-my-code, in some general way. The hook is apparently being invoked while a non-file buffer is current. Why that is so is up to you to investigate. If it's enough to just make it a no-op when a non-file is current then that fixes your problem. If not, figure out which buffer is current when the problem arises, and why that's so. In general, if invoking a hook function you need to be aware of, or in control of, which buffer is current.
    – Drew
    Jul 17, 2022 at 16:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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