向emacs询问默认目录路径“;“一次”;

向emacs询问默认目录路径“;“一次”;,emacs,directory,elisp,Emacs,Directory,Elisp,我希望有一个变量,它保留用户输入的默认目录,并在整个emacs运行期间使用它 基本上,当用户执行自定义命令时,提示符将要求默认目录路径来执行该命令(仅一次),并且每当用户调用相同的命令时,emacs将使用相同的路径继续执行 如何用lisp编写这段代码 我基本上希望igrep库中的这段代码接受用户的输入一次,而不是再次询问: (defvar default-files-string-new "*.[sch]") (defun igrep-read-files (&optional prom

我希望有一个变量,它保留用户输入的默认目录,并在整个emacs运行期间使用它

基本上,当用户执行自定义命令时,提示符将要求默认目录路径来执行该命令(仅一次),并且每当用户调用相同的命令时,emacs将使用相同的路径继续执行

如何用lisp编写这段代码

我基本上希望igrep库中的这段代码接受用户的输入一次,而不是再次询问:

(defvar default-files-string-new "*.[sch]")
(defun igrep-read-files (&optional prompt-prefix)
  "Read and return a file name pattern from the minibuffer.
If `current-prefix-arg' is '(16) or '(64), read multiple file name
patterns and return them in a list.  Optional PROMPT-PREFIX is
prepended to the \"File(s): \" prompt."
  (let* ((default-files (igrep-default-files))
     (default-files-string (mapconcat 'identity default-files " "))
     (insert-default-directory igrep-insert-default-directory)
     (file (igrep-read-file-name
        (igrep-prefix prompt-prefix
                  (if default-files
                  (format "File(s) [default: %s]: "
                      default-files-string)
                "File(s): "))
        nil (if default-files default-files-string "") nil nil
        'igrep-files-history))
     (files (list file)))
    (if (or igrep-read-multiple-files
        (and (consp current-prefix-arg)
         (memq (prefix-numeric-value current-prefix-arg)
               '(16 64))))
    (let* ((key (igrep-default-key 'exit-minibuffer
                       minibuffer-local-completion-map
                       "\r"))
           (prompt
        (igrep-prefix prompt-prefix
                  (if igrep-verbose-prompts
                  (format "File(s): [Type `%s' when done] "
                      (key-description key))
                "File(s): "))))
      (while (and (setq file
                (igrep-read-file-name prompt
                          nil "" nil nil
                          'igrep-files-history))
              (not (equal file "")))
        (setq files (cons file files)))))
    (mapcar (lambda (file)
          (if (file-directory-p file)
          ;; really should map expand-file-name over default-files:
          (expand-file-name (if default-files default-files-string-new "*")
                    file)
        file))
        (nreverse files))))

查看一下
sendmail查询的代码
。 虽然做这种事不是很时髦。 通常,包编写器选择一个正常的默认值,并让用户
根据需要定制。

查看一下
发送邮件查询的代码。
虽然做这种事不是很时髦。
通常,包编写器选择一个正常的默认值,并让用户

根据用户的需要进行自定义。

您可以为sane默认值设置一个自定义变量,然后让用户在第一次调用时输入路径或接受默认值

(defcustom default-path "/tmp/foo" "Path")
(setq current-path nil)


(defun foo ()
  (interactive)
  (unless current-path
    (setq current-path
          (read-from-minibuffer 
           (format "Path [%s]" default-path) nil nil t nil default-path)))
  (message "Path is: %s" current-path))
第一次执行M-xfoo时,它会提示输入路径。一个常见的习惯用法是允许用户在想要更改值(第一次之后)时指定前缀参数。此代码将达到预期效果:

(defun foo (choose)
  (interactive "P")
  (when (or choose (not current-path))
    (setq current-path
          (read-from-minibuffer 
           (format "Path [%s]" default-path) nil nil t nil default-path)))
  (message "Path is: %s" current-path))
现在执行M-xfoo与以前相同,但是C-0mfoo将提示输入一个新值。 在您的示例中,类似这样的操作将起作用

(defun igrep-read-files (&optional prompt-prefix)   
  (interactive "P")  
  (when (or prompt-prefix (not current-path ))
    (setq current-path
          (read-file-name "Dir: " default-path nil t)))   
  (message (expand-file-name default-files-string-new current-path)))

您可以为sane默认值设置一个自定义变量,然后让用户在第一次调用时输入路径或接受默认值

(defcustom default-path "/tmp/foo" "Path")
(setq current-path nil)


(defun foo ()
  (interactive)
  (unless current-path
    (setq current-path
          (read-from-minibuffer 
           (format "Path [%s]" default-path) nil nil t nil default-path)))
  (message "Path is: %s" current-path))
第一次执行M-xfoo时,它会提示输入路径。一个常见的习惯用法是允许用户在想要更改值(第一次之后)时指定前缀参数。此代码将达到预期效果:

(defun foo (choose)
  (interactive "P")
  (when (or choose (not current-path))
    (setq current-path
          (read-from-minibuffer 
           (format "Path [%s]" default-path) nil nil t nil default-path)))
  (message "Path is: %s" current-path))
现在执行M-xfoo与以前相同,但是C-0mfoo将提示输入一个新值。 在您的示例中,类似这样的操作将起作用

(defun igrep-read-files (&optional prompt-prefix)   
  (interactive "P")  
  (when (or prompt-prefix (not current-path ))
    (setq current-path
          (read-file-name "Dir: " default-path nil t)))   
  (message (expand-file-name default-files-string-new current-path)))

您可以使用以下建议:

(defvar wd-alist nil)

(mapc
 (lambda (function)
   (eval
    `(defadvice ,function (around ,(intern (format "%s-wd" function)) activate)
       (let ((wd (cdr (assoc ',function wd-alist))))
         (unless wd
           (setq wd (read-file-name "Default directory: "))
           (push (cons ',function wd) wd-alist))
         (let ((default-directory wd))
           ad-do-it)))))
 '(grep-find))

变量
wd list
存储关联
(FUNCTION.PATH)
。列表
mapc
迭代是建议的函数。现在,当调用
find grep
时,它会请求工作目录(在交互式参数之后,因此您首先必须键入模式并输入…),并将其存储在
wd list
中以供进一步使用。现在,您的
查找grep
总是在该目录中完成。

您可以使用建议来完成此操作:

(defvar wd-alist nil)

(mapc
 (lambda (function)
   (eval
    `(defadvice ,function (around ,(intern (format "%s-wd" function)) activate)
       (let ((wd (cdr (assoc ',function wd-alist))))
         (unless wd
           (setq wd (read-file-name "Default directory: "))
           (push (cons ',function wd) wd-alist))
         (let ((default-directory wd))
           ad-do-it)))))
 '(grep-find))

变量
wd list
存储关联
(FUNCTION.PATH)
。列表
mapc
迭代是建议的函数。现在,当调用
find grep
时,它会请求工作目录(在交互式参数之后,因此您首先必须键入模式并输入…),并将其存储在
wd list
中以供进一步使用。现在,您的
查找grep
总是在该目录中完成。

您所说的“自定义命令”是什么意思?你写的函数?我是指我在.emacs中添加的函数。下面是我试图做的:我正在剪切一个“grep”命令,该命令默认为“用户”说要grep的主文件夹所在的位置,只需继续使用该变量,而不必不断递归地询问用户主文件夹的路径。你说的“自定义命令”是什么意思?你写的函数?我是指我在.emacs中添加的函数。下面是我正在尝试做的:我正在剪切一个“grep”命令,该命令默认为“用户”所说的要grep的主文件夹所在的位置,并且只需继续使用该变量,而无需不断递归地询问用户主文件夹的路径。感谢您的响应。我确实希望用户有一个默认的“sane”文件夹,他可以保留或更改。但在他接受/更改后,我希望设置变量,直到他一起终止emacs。我需要这样做的原因是,当我处理一个项目时,我在主目录下有源目录和头目录。我想总是搜索这两个文件夹,所以grep从父文件夹。父文件夹不太可能更改,它在整个项目实施过程中保持永久性。感谢您的回复。我确实希望用户有一个默认的“sane”文件夹,他可以保留或更改。但在他接受/更改后,我希望设置变量,直到他一起终止emacs。我需要这样做的原因是,当我处理一个项目时,我在主目录下有源目录和头目录。我想总是搜索这两个文件夹,所以grep从父文件夹。父文件夹不太可能更改,它在整个项目实施过程中保持永久性。嗨,Sean…感谢您的支持。我正在尝试修改igrep.el包中的igrep find功能。因此,我按照您的指示执行了以下操作:(defvar默认文件字符串new“*.[sch]”(defvar默认路径“~/source/”path”)(setq当前路径nil)(defun igrep读取文件(&可选提示前缀)(交互式)(除非当前路径(setq当前路径)(从微缓冲区读取)(格式为“路径[%s]”默认路径)nil nil t nil default path))`(,(展开文件名(concat current path default files string new)))但是我得到了这个错误:错误的类型参数:sequencep,~/source/Hi Sean。。。我试过调整后的例子。我仍然看到提示符要求输入目录名,甚至在我更改目录名时,下一次它会使用旧目录的默认目录“~/source/”Ok。这样就可以工作了:(defun自定义d-path“~/source/”path”)(setq当前路径nil)(defun igrep读取文件(&可选提示前缀)(交互式“P”)(除非当前路径(setq当前路径(读取文件名“Dir:”d-path nil t))`(,(展开文件名(concat当前路径默认文件字符串new))))但是,用户需要确保