我希望有一个变量来保存用户输入的默认目录,并在整个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))))
答案 0 :(得分:2)
您可以为理智的默认设置设置自定义变量,然后让用户在第一次调用时输入路径或接受默认值。
(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-x foo时,会提示输入路径。一个常见的习惯用法是允许用户在想要更改值时(第一次之后)指定前缀参数。此代码将具有所需的效果:
(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-x foo和以前一样,但是C-0 M-x foo会提示输入一个新值。 在你的例子中,这样的东西会起作用。
(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)))
答案 1 :(得分:2)
你可以使用建议来做到这一点:
(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
中以供进一步使用。现在,您的find-grep
始终在该目录中完成。
答案 2 :(得分:0)
查看sendmail-query-once
的代码。
虽然做这种事情并不是很时髦。
通常包编写者选择一个理智的默认值并让用户
根据需要自定义。