0

I want to programmatically check whether the current buffer is in org mode. I know that major modes should usually define the variable <modename>-mode, i.e., org-mode should be a defined variable.

However, if I execute describe-variable in an org buffer, I don't see any org-mode. Now, I'm not sure whether describe-variable works for buffer-local variables, so I typed (message org-mode) into my org buffer and ran eval-last-sexp. This gives me the error (void-variable org-mode), so I guess that org mode does not define any org-mode variable.

What is the correct way to check whether a buffer is in org mode?

3

I know that major modes should usually define the variable -mode, i.e., org-mode should be a defined variable.

Not quite: a major mode should set the variable mode-name to the "pretty name" for the mode (i.e., the one that will appear in the modeline), and the variable major-mode to the major mode's command symbol (i.e., org-mode in this case). You can test if the current buffer is in org-mode with:

(string-equal major-mode "org-mode")

;; or equivalently:

(string-equal mode-name "Org")

These conventions are explained in the Emacs Lisp manual, (elisp) Major Mode Conventions.

  • 1
    Also ...: (eq major-mode 'org-mode). Unlike org-mode, some modes may change their mode-name depending upon certain criteria ... and the major-mode remains the same ... – lawlist Oct 15 at 20:44

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.