gpt4 book ai didi

git - 在 emacs 中打开每个缓冲区之前如何重新评估 shell 环境并重新评估 frame-title-format

转载 作者:太空狗 更新时间:2023-10-29 13:47:13 24 4
gpt4 key购买 nike

我已将我的 git source+repo 名称整合到我的 emacs 框架标题中,因此我可以确定我当前正在处理的分支/repo。为此,我通过 PROMPT_COMMAND 在 bash shell 中设置此信息,然后在 emacs 启动时在 init.el(或 .emacs)中获取信息) 通过 getenv 命令设置变量 frame-title-format

这是我在 ~/.bashrc 中所做的:


PROMPT_COMMAND="export GIT_REPO_NAME=\$(git remote -v 2> /dev/null | grep \"origin.\*fetch\" | awk '{print \$2}' | sed 's,https://github.com/\(.\*\)/\(.\*\),\1,g'); export GIT_BRANCH=\$(git branch 2> /dev/null | grep \"^*\" | awk '{print \$2}')"

下面是我在 ~/.emacs.d/init.el 中所做的:


;;; Rest of init.el code
(setq-default
frame-title-format
(concat
"%f"
(if (getenv "GIT_BRANCH")
(concat " in [ " (getenv "GIT_BRANCH") "/" (getenv "GIT_REPO_NAME") " ]"))))

;;; All the other goodies in init.el

这一切都很棒,但不幸的是,这并不总是准确的,因为帧标题是由 env 命令设置的,该命令最后在 中计算了我的 PROMPT_COMMAND >pwd 当 emacs 启动时。例如,如果在同一个 emacs session 中,我在属于另一个 repo/branch 的另一个目录中打开一个缓冲区,我的框架标题不正确。

所以我的问题是,我可以通过任何方式让 emacs 在 shell 中重新运行 git 命令以获取缓冲区所在目录的源/repo 名称,以便不同的缓冲区将显示正确的 git 分支/repo 名称?

考虑以下目录结构:


gitRepoA (branch X)
|-> buffer1.txt
|-> buffer2.txt
gitRepoB (branch Y)
|-> buffer1.txt
|-> buffer2.txt

假设我为(目录)gitRepoA 中的文件 buffer1.txt 启动了 emacs。然后我在 repo gitRepoA 的分支 X 中看到一个框架标题 buffer1.txt

如果我在同一个 emacs session 中打开 gitRepoBbuffer2.txt(当前 checkout branch Y),我希望看到buffer2.txt 在 gitRepoB 分支 Y 中。目前,当然,它会说 buffer2.txt in branch X in repo gitRepoA。完全错误。

如果我们能够解决这个问题,我想解决更普遍的问题,即在 git status 级别更改(比如提交,或 checkout 等)。

非常感谢任何帮助/见解。另外,我的问题的标题更合适,我很感激——我在用一个标题表达我的问题时遇到了麻烦。谢谢。

最佳答案

这是设置框架标题的完整示例,目前它仅适用于 git repo

(defun my-get-frame-title ()
"If inside a git repo return a string containing git repo, branch and file state else
return default frame title"
(let* ((file (buffer-file-name))
(git-directory (when file
(locate-dominating-file (file-truename default-directory) ".git"))))
(if git-directory
(concat (file-name-nondirectory (buffer-file-name))
" on " (vc-working-revision file)
" in " (file-name-nondirectory (directory-file-name git-directory))
" current state is " (symbol-name (vc-state file)))
(concat invocation-name "@" system-name))))

(setq-default
frame-title-format
'(:eval (my-get-frame-title)))

这会在 git 中设置框架标题 repo 格式 <filename> on <branch> in <repo> current state is <state>否则它会退回到 emacs 的默认框架标题

关于git - 在 emacs 中打开每个缓冲区之前如何重新评估 shell 环境并重新评估 frame-title-format,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21246756/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com