Finding the Language of the Current Buffer in Emacs
23 April 2009 - Georgia Tech
I'm currently writing some code that looks at the current buffer and uses it to determine the language the buffer's written in if there is one.
(defun add-import-find-language ()
(cond
;; check major mode
((not (string= major-mode "fundamental-mode"))
(let ((current-mode (symbol-name major-mode)))
(string-match "^\\([[:alpha:]-]+\\)\\-mode" current-mode)
(match-string 1 current-mode)))
;; check extension
((or (buffer-file-name) (buffer-name))
(let ((bufname (or (buffer-file-name) (buffer-name))))
(dolist (pair auto-mode-alist)
(if (string-match (car pair) bufname)
(let ((current-mode (symbol-name (cdr pair))))
(string-match "^\\([[:alpha:]-]+\\)\\-mode" current-mode)
(return (match-string 1 current-mode)))))))
;; check buffer-local var language
(language
(let ((current-mode (symbol-name language)))
(string-match "^\\([[:alpha:]-]+\\)" current-mode)
(return (match-string 0 current-mode)))))
)
Note: Code highlighting currently disabled due to a bug in Jekyll.
The comments should make most everything pretty clear, but the
add-import-find-language function, which will be part of another side
project I've been tinkering with, steps through the major-mode, the "file
extension" (the caveat there being it checks both buffer-file-name and
buffer-name just in case the buffer was created without find-file or
similar), and finally checks for a buffer-local variable language to
determine the buffer's language, which it returns as a string.
There are no doubt edge-cases where this function fails to do the right thing. If you happen to find one, please file an issue and, if possible, also a link to your own fork of add-import.el with the fix(es).