Reputation: 3520
Is there an emacs command which opens the file that describes the current major mode? It would be useful to edit syntax highlighting and indentation on the fly.
Upvotes: 2
Views: 268
Reputation: 17707
(find-library (file-name-sans-extension (symbol-file major-mode)))
Will open the file which defines the current major-mode. That literally answers your question.
However, you should not edit this file "on the fly". Customizations in Emacs are not made by editing the library source directly. Instead code is executed in major-mode hooks or eval-after-load
blocks.
Upvotes: 6
Reputation: 3870
Someone else might come along with some predefined function that does exactly what you want, but I'm not aware of one. You can build your own without too much trouble.
major-mode
is bound to a symbol defining the current major mode(symbol-file major-mode)
will then return the path to the file defining that symbolHowever, it's a bit more complicated than that. Most likely, most of your major modes are loaded from byte compiled elisp files, so the path you get back will be to an .elc file that you don't really want to open. You can detect that and chop off the final character, but you also need to handle gzipped files. Fortunately, Emacs can open a gzipped file transparently, so something like the following should basically work. I typed it in and checked it on one or two modes, but basically consider it untested.
(defun load-major-mode-file ()
(interactive)
(let* ((loaded-file (symbol-file major-mode))
(root-el-file (if (string-match "\\.elc$" loaded-file) (substring loaded-file 0 -1) loaded-file)))
(if (file-exists-p root-el-file)
(find-file root-el-file)
(if (file-exists-p (concat root-el-file ".gz"))
(find-file (concat root-el-file ".gz"))
(message "couldn't open major-mode file.")))))
Upvotes: 2