将DOS Shell目录与Emacs的“工作目录”对齐

3
在Windows的Emacs中,我经常使用dos提示符(M-x shell)。大多数时候,我的emacs工作目录与提示符目录保持一致。例如,当我的提示符目录为c:\TEMP\project并且我执行C-x f时,它会提示从c:\temp\project获取文件,这是期望的行为。
有时,特别是在使用/D标志(cd /D E:\TEMP\other-project)时,emacs的工作目录和shell的工作目录之间的对齐会丢失。有没有办法恢复这种对齐?
我使用的是emacs i386-mingw-nt6.1.7601版本。
3个回答

4

M-x shell-resync-dirs RET

或者简单地使用默认快捷键:M-RET

Resync the buffer's idea of the current directory stack.
This command queries the shell with the command bound to
`shell-dirstack-query' (default "dirs"), reads the next
line output and parses it to form the new directory stack.
DON'T issue this command unless the buffer is at a shell prompt.
Also, note that if some other subprocess decides to do output
immediately after the query, its output will be taken as the
new directory stack -- you lose.  If this happens, just do the
command again.

在dos中可能会有所不同。
我认为您需要执行(setq shell-dirstack-query "cd"),因为dos中默认的dirs命令不存在。没有参数的cd似乎是所需的内容。
在我的win32计算机上,我看到所有命令都被回显,这会干扰shell-resync-dirs尝试读取正确目录的操作,因此您可能需要参考http://www.gnu.org/software/emacs/manual/html_node/efaq-w32/Shell-echo.html 然而,即使已经做了所有的事情,它仍然不可靠。我相信路径中的空格会导致错误。如果您的路径中没有空格,则可能没问题。
编辑: 是的,在那里有一个不太理想的正则表达式尝试解析目录:
;; regexp = optional whitespace, (non-whitespace), optional whitespace
(string-match "\\s *\\(\\S +\\)\\s *" dl i) ; pick off next dir

我不确定这里有什么可能的可移植性问题,但您可以将其更改为以下内容以适应您的目的:
;; regexp = optional whitespace, everything up until the last non-whitespace, optional whitespace
(string-match "\\s *\\(.+\\S +\\)\\s *" dl i) ; pick off next dir

那段代码在M-x find-function RET shell-resync-dirs RET中。

我看到有几个相关的Emacs错误报告(1160711608)。您可能想要跟进这些报告。


2

当对齐丢失时,M-x cd似乎可以恢复它:指定实际工作目录为shell,并且C-x C-f将其用作基础。


没问题,自动从提示符中读取当前工作目录并将其用作“C-x C-f”的参数会很不错。现在我会把它制作成一个宏。 - dr jerry

0
我在Windows上使用shell-resync-dirs时遇到了类似的问题。后来发现在shell-mode配置中将comint-process-echoes设置为t可以解决这个问题,由于某种原因,即使该变量为false,显示目录的命令也会被回显。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接