Elisp - 打印到*消息*时出现无效功能错误

时间:2014-09-16 09:58:55

标签: emacs elisp

我是一名Elisp新手调整我的.emacs文件,根据正在编辑的文件选择c-mode样式。在这样做的同时,我正在尝试打印到*Messages*,但它失败了。

确切错误为File mode specification error: (invalid-function (message "This results in an incorrect-function error"))

(add-hook 'c-mode-hook
          (lambda ()
            (message "Foo") ;; <- This gets printed                                                                                                                                                              
            (let ((filename (buffer-file-name)))
              ;; Enable kernel mode for the appropriate files                                                                                                                                                    
           ((message "This results in an incorrect-function error") ;; <- this fails
                 (if (and filename
                        (string-match (expand-file-name "~/src/linux-trees")
                                      filename))

                  ((message "Identified as Linux style") ;; <- this fails                                                                                                                                        
                   (setq indent-tabs-mode t)
                   (c-set-style "linux-tabs-only")
                   (setq ethan-wspace-errors (remove 'tabs ethan-wspace-errors)))
                ;; Otherwise use Google's coding style                                                                                                                                                           
                 ((message "Identified as Google style") ;; <- this fails                                                                                                                                    
                  google-set-c-style
                  google-make-newline-indent))))))

2 个答案:

答案 0 :(得分:2)

你有一些错位的括号,正如@ MrBones的评论所指出的那样,你需要在progn条款中包含if中的多个语句:

(defun fnx ()
  (message "Foo")
  (let ((filename (buffer-file-name)))
    (message "No longer an incorrect-function error") ; get rid of first "("
    (if (and filename (string-match filename
                                    (expand-file-name "~/src/linux-trees")))
        (progn                                  ; wrap multiple statements in a progn
          (message "Identified as Linux style") ; get rid of first '('
          (setq indent-tabs-mode t)
          (c-set-style "linux-tabs-only")
          (setq ethan-wspace-errors (remove 'tabs ethan-wspace-errors)))
      (progn                                    ; not strictly necessary in else clause
        (message "Identified as Google style")  ; get rid of first '('
        (google-set-c-style)                    ; don't forget parens
        (google-make-newline-indent)))))        ; don't forget parens

(注意:我以前从未使用google-...,所以我认为你已经有了正确的功能。)

最后,通常最好使用命名函数作为钩子而不是lambdas:

(add-hook 'c-mode-hook 'fnx)

答案 1 :(得分:1)

如果要将多个函数调用合并为一个,则需要使用progn之类的构造。

换句话说,你不能写:

((message "hi")
 (a-function-call))

相反,你必须写:

(progn
 (message "hi")
 (a-function-call))