如果我在基本模式下的缓冲区中有Python(或任何其他语言)文件,是否有一个命令可以让缓冲区自动检测它应该是什么语言并相应切换?
答案 0 :(得分:6)
简单的答案是: Alt + x 正常模式 输入
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.
这只会有效,如果缓冲区中有一些提示(例如文件顶部的#!/bin/python
)或缓冲区支持的文件名(如果有的话)有python扩展名(还有其他方法;请参阅上面的定义和set-auto-mode
以获取有关它们的更多信息。
如果您知道自己想要什么模式,也可以专门设置模式。我的.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)
有了它,我可以通过以下方式将我的主要模式设置为具有次模式的查看模式的CPerl: F3 m p F3 中号 v
答案 1 :(得分:1)
一个更简单的答案是:Emacs只学习了如何在22.1版(2007年6月)中识别Python文件。如果您使用的是较旧的emacs,那就可以解释它。对于较新的版本,它应该只是工作,至少如果文件的名称以“.py”结尾。