2010-03-06 73 views
2

如果我在基本模式下的緩衝區中有一個Python(或任何其他語言)文件,是否有一個命令,我可以使緩衝區自動檢測它應該在什麼語言,並切換因此?在Emacs中自動檢測語言

回答

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 米pF3中號v

1

嗯,那不是上面這麼簡單的答案。

更簡單的答案是:Emacs只學習瞭如何識別22.1版本(2007年6月)中的Python文件。如果您使用的是較舊的emacs,那可以解釋它。使用較新的版本,至少如果文件名以「.py」結尾,它應該正常工作。

+1

您可以使用'auto-mode-alist'通過文件擴展名設置模式,至少返回到版本18.x,IIRC(18.58或18.85是我記得使用的第一個版本)。 – 2010-03-07 05:04:49