Emacs中的文件扩展名钩子

时间:2011-07-30 23:48:41

标签: emacs elisp

我想为特定的文件扩展名(即非模式)运行一个钩子。我对elisp没有任何经验,所以我用货物编码了这个:

(defun set_tab_mode ()
    (when (looking-at-p "\\.cat")
    (insert "OK")
    (orgtbl-mode)))

(add-hook 'find-file-hook 'set_tab_mode)

(应该为带有后缀.cat的文件设置orgtbl次要模式并插入文本“OK”,即它不仅是模式设置问题)。 不幸的是它不起作用。

2 个答案:

答案 0 :(得分:23)

您可以在auto-mode-alist中使用lambda:

(add-to-list 'auto-mode-alist
             '("\\.cat\\'" . (lambda ()
                               ;; add major mode setting here, if needed, for example:
                               ;; (text-mode)
                               (insert "OK")
                               (turn-on-orgtbl))))

答案 1 :(得分:19)

试试这个:

(defun my-set-tab-mode ()
  (when (and (stringp buffer-file-name)
             (string-match "\\.cat\\'" buffer-file-name))
    (insert "OK")
    (orgtbl-mode)))

(add-hook 'find-file-hook 'my-set-tab-mode)
相关问题