2

是否有任何编辑器/IDEdiff在我编辑文件时提供差异的实时视图(可能使用)。理想的设置是当我以这种差异模式打开文件进行编辑时,该文件缓冲在两个独立的窗格中(但并排放置),因此当我编辑其中一个的内容时,两者的差异为我突出显示。

4

1 回答 1

3

似乎没有现有的 Emacs 模式可以完全满足您的需求,但粗略的谷歌搜索发现了thisthis

从第二个开始(格式稍微固定):

(defun diff-buffer-against-file (context)
    "diff the current [edited] buffer and the file of the same name"
    (interactive "P")
    (let (($file buffer-file-name)
          ($tempFile "/tmp/emacs.diff")
          ($tempBuffer "emacs.diff"))
        (delete-other-windows)
        (push-mark (point) t)
        (generate-new-buffer $tempFile)
        (copy-to-buffer $tempBuffer (point-min) (point-max))
        (set-buffer $tempBuffer)
        (write-file $tempFile)
        (shell-command (concat (if context "diff -c " "diff ") $file " " $tempFile))
        (kill-buffer $tempFile)
        (pop-mark)))

(global-set-key "\C-cd" 'diff-buffer-against-file)

该操作看起来有点过于激烈,无法绑定到change hook,但是如果您喜欢这种事情,没有什么可以阻止您这样做。

编辑: Stefan 指出diff-buffer-with-file存在,并且具有您正在寻找的行为(它需要一个缓冲区,并且diffs 该缓冲区及其文件,在未聚焦的临时缓冲区中显示该输出),因此您甚至不需要定义以上。不过我确实试过了,而且天真

(defun diff-current (start end len) (diff-buffer-with-file (current-buffer)))
(add-hook 'after-change-functions 'diff-current)

使编辑对我的口味来说太不舒服了。您可能希望遵循 Stefan 的建议并使用超时而不是diff在每次更改时立即 ing。

于 2013-04-12T19:31:26.003 回答