我正在编写一个使用git-emacs包函数的小定制。 想法是每当按下一个键时,弹出一个当前可用git-repositories的缓冲区,允许我选择一个并自动导航到repo的git-status缓冲区。
我目前仍然坚持使用此代码:
(require 'popwin)
(require 'button)
(popwin-mode 1)
(setq git-repositories (list "~/testgit/repo1/" "~/repo2/" "~/gitlab/repo3"))
(defun select-git-repository-from-list()
(interactive)
;; fill buffer with data from git-repositories
(with-output-to-temp-buffer "*git-repositories*"
(with-current-buffer "*git-repositories*"
(dolist (repo git-repositories)
;; we use 'local' variable because if insert-text-button value
;; is set like (car blah), call to `car` is issued when button is pressed,
;; so the it does return first element in the list
(setq pos (position repo git-repositories))
(insert-text-button repo 'action (lambda (x) (delete-window) (git-status (nth pos git-repositories))))
(newline)
)
(push '("*git-repositories*" :height 10) popwin:special-display-config)))
)
(global-set-key (kbd "C-c \\") 'select-git-repository-from-list)
不能按预期工作:
这会创建一个迷你缓冲区,用文本按钮填充它,以触发(git-status dir)
。问题是当我按下第一个按钮时,成功调用了git-status。另外两个给我错误if: fatal: Not a git repository (or any of the parent directories): .git
。我猜道路径为空或已损坏,这就是找不到.git的原因。
所以,问题是: 该代码应该如何重写?
此致 麦克
答案 0 :(得分:1)
您在函数内的长注释提供了问题的线索:评估代码和变量的时刻并不是您所期望的。您可以通过以下方式解决问题:
(defun select-git-repository-from-list()
(interactive)
;; fill buffer with data from git-repositories
(with-output-to-temp-buffer "*git-repositories*"
(with-current-buffer "*git-repositories*"
(dolist (repo git-repositories)
(insert-text-button repo 'action `(lambda (x) (delete-window) (git-status ,repo)))
(newline))
(push '("*git-repositories*" :height 10) popwin:special-display-config))))
注意反引用:我引用一个列表(代码,如果是事实,因为它是一个lambda),但我有选择地评估其中的变量(这就是逗号的用途)。