If i have a Python (or any other language) file in a buffer in fundamental mode, is there a command 开发者_如何学编程that i can use to make the buffer auto detect what language it should be in and switch accordingly?
The simple answer is: Alt+xnormal-modeEnter
normal-mode is an interactive compiled Lisp function in `files.el'.
(normal-mode &optional find-file)
Choose the major mode for this buffer automatically.
Also sets up any specified local variables of the file.
Uses the visited file name, the -*- line, and the local variables spec.
This function is called automatically from `find-file'. In that case,
we may set up the file-specified mode and local variables,
depending on the value of `enable-local-variables'.
In addition, if `local-enable-local-variables' is nil, we do
not set local variables (though we do notice a mode specified with -*-.)
`enable-local-variables' is ignored if you run `normal-mode' interactively,
or from Lisp without specifying the optional argument find-file;
in that case, this function acts as if `enable-local-variables' were t.
That will only work, though, if there are some hints in the buffer (e.g. #!/bin/python
at the top of the file) or the file name that the buffer is backed by (if any) has a python extension (and there are other ways, too; see the definition above and for set-auto-mode
for more information on what they all are).
You can also set the mode specifically, if you know what mode it is you want. I have this in my .emacs:
;;;; ------------------------------------------------------------------------
;;;; --- F3 - multi-purpose prefix keymap
;;;; ------------------------------------------------------------------------
(setq my-F3-keymap (make-sparse-keymap))
(global-set-key [(f3)] my-F3-keymap)
;; --- m -- generic (major) modes ---
(require 'generic)
(require 'generic-x)
;; stolen shamelessly from generic-x Samba mode
(define-generic-mode 'generic-rc-mode
(list ?#)
nil
'(
("^\\s-*\\(.+\\)=\\([^\r\n]*\\)"
(1 'font-lock-variable-name-face)
(2 'font-lock-type-face))
)
(list "\\(/\\.[^/]+rc$\\|\\.rc$\\)" "weblogic[^/\\]*\\.conf\\'" "oc4j[^/\\]*\\.conf\\'" "tomcat[^/\\]*\\.conf\\'")
nil
"Generic RC mode.")
(setq my-F3:m-keymap (make-sparse-keymap))
(define-key my-F3-keymap [(m)] my-F3:m-keymap)
(define-key my-F3:m-keymap [(a)] 'apache-conf-generic-mode)
(define-key my-F3:m-keymap [(b)] 'bat-generic-mode)
(define-key my-F3:m-keymap [(f)] 'fundamental-mode)
(define-key my-F3:m-keymap [(i)] 'ini-generic-mode)
(define-key my-F3:m-keymap [(j)] 'javascript-generic-mode)
(define-key my-F3:m-keymap [(J)] 'java-properties-generic-mode)
(define-key my-F3:m-keymap [(l)] 'emacs-lisp-mode)
(define-key my-F3:m-keymap [(L)] 'lisp-interaction-mode)
(define-key my-F3:m-keymap [(n)] 'nxml-mode)
(define-key my-F3:m-keymap [(p)] 'cperl-mode)
(define-key my-F3:m-keymap [(r)] 'generic-rc-mode)
(define-key my-F3:m-keymap [(s)] 'sgml-mode)
(define-key my-F3:m-keymap [(S)] 'shell-script-mode)
(define-key my-F3:m-keymap [(t)] 'text-mode)
;; --- M -- generic (minor) modes ---
(setq my-F3:M-keymap (make-sparse-keymap))
(define-key my-F3-keymap [(M)] my-F3:M-keymap)
(define-key my-F3:M-keymap [(l)] 'longlines-mode)
(define-key my-F3:M-keymap [(v)] 'view-mode)
(define-key my-F3:M-keymap [(V)] 'visible-whitespace-mode)
With it I can set my major mode to CPerl with a minor-mode of View Mode by doing: F3mpF3Mv
Hm, that wasn't such a simple answer above.
A simpler answer is: Emacs only learned how to recognize Python files in version 22.1 (June 2007). If you're using an older emacs, that would explain it. With the newer versions, it should Just Work, at least if the file's name ends with ".py".
精彩评论