将命令作为参数传递给emacs中的函数

时间:2013-08-14 23:11:25

标签: emacs elisp

我是elisp的新手。那么请原谅我的新手问题。 我试图将一个“命令”传递给函数compile-pkg,但我在emacs中为函数compile-make()继续得到错误的参数。

(defun compile-pkg (&optional command startdir)
  "Compile a package, moving up to the parent directory
  containing configure.ac, if it exists. Start in startdir if defined,
  else start in the current directory."
  (interactive)

  (let ((dirname)
    (dir-buffer nil))
    (setq startdir (expand-file-name (if startdir startdir ".")))
    (setq command  (if command command compile-command))

    (setq dirname (upward-find-file "Makefile" startdir))
    (setq dirname (if dirname dirname (expand-file-name ".")))
    ; We've now worked out where to start. Now we need to worry about
    ; calling compile in the right directory
    (save-excursion
      (setq dir-buffer (find-file-noselect dirname))
      (set-buffer dir-buffer)
      (compile command)
      (kill-buffer dir-buffer))))

(defun upward-find-file (filename &optional startdir)
  "Move up directories until we find a certain filename. If we
  manage to find it, return the containing directory. Else if we
  get to the toplevel directory and still can't find it, return
  nil. Start at startdir or . if startdir not given"

  (let ((dirname (expand-file-name
          (if startdir startdir ".")))
    (found nil) ; found is set as a flag to leave loop if we find it
    (top nil))  ; top is set when we get
            ; to / so that we only check it once

    ; While we've neither been at the top last time nor have we found
    ; the file.
    (while (not (or found top))
      ; If we're at / set top flag.
      (if (string= (expand-file-name dirname) "/")
      (setq top t))

      ; Check for the file
      (if (file-exists-p (expand-file-name filename dirname))
      (setq found t)
    ; If not, move up a directory
    (setq dirname (expand-file-name ".." dirname))))
    ; return statement
    (if found (concat dirname "/") nil)))

(defun compile-make ()
  (compile-pkg "make"))

(global-set-key [f1] 'compile-make)

我做错了什么?

1 个答案:

答案 0 :(得分:2)

首先出现的问题是你需要compile-make作为命令,你可以通过简单的改变来做:

(defun compile-make ()
  (interactive)
  (compile-pkg "make"))

查看Emacs lisp教程,特别是Making a Function Interactive部分,了解为什么interactive是必要的。

注意:如果您剪切/粘贴您看到的确切错误消息,通常会有所帮助。我想它是:

Wrong type argument: commandp, compile-make