如何显示两个程序之间的差异?

时间:2019-05-28 11:26:04

标签: emacs diff

我正在讲编程。我没有使用幻灯片,而是提供了一系列更长的程序,这些程序存储在文件名0001.py0002.py等中。

每个程序中的代码仅引入一行或几行代码,作为对前一个代码的修改。

我打算仅使用emacs进行演示。我对ediff很熟悉,但是在演讲期间现场使用它会有些痛苦(因为大约有50个小程序,每个增量需要一分钟的时间介绍。)

是否存在一个emacs软件包,该软件包可让我拆分窗口并突出显示(n).py和(n + 1).py之间的实际区别。 (我使用.py是为了具体,但希望该解决方案适用于任何文本文件。)

我在这里而不是在https://emacs.stackexchange.com上问是因为我对使用emacs,git或我可以串在一起进行实时演示的任何组合的解决方案感到满意,尤其是具有修改代码的能力回答问题时生活。

更新

根据phils的建议,M-x compare-windows几乎解决了这个问题,但是:

  1. 无论光标在两个缓冲区中的当前位置如何,如果它都能正常工作,那就太好了。
  2. 如果所有更改都显示在一个视图中,而不是遍历diff,那就太好了。关键是要说“看,在右边的程序中,我只添加了这一行和那一行,并查看该程序与上一个程序相比可以执行的所有操作”。 li>

更新2

换句话说,如何生成以下HTML中手动完成的操作以并排显示差异?

.myflex {
  display: flex;
  flex-direction: row;
}

.before,
.after {
  border: 1px solid black;
  padding: 20px;
  margin: 20px;
  border-radius: 2px;
}

.pink {
  background-color: pink;
}

.green {
  background-color: PaleGreen;
}
<div class="myflex">
  <div class="before">
    <pre>
<span class='pink'>And so without particularly analyzing all the contiguous sections of a</span>
<span class='pink'>cone and of the ranks of an army, or the ranks and positions in any</span>
while the less their direct participation in the action itself, the more
they command and the fewer of them there are; rising in this way from
the lowest ranks to the man at the top, who takes the least direct share
in the action and directs his activity chiefly to commanding.
        </pre>
  </div>
  <div class="after">
    <pre>
<span class='green'>We see a law by which men, to take associated action, combine</span>
<span class='green'>in such relations that the more directly they participate in performing</span>
<span class='green'>the action the less they can command and the more numerous they are,</span>
while the less their direct participation in the action itself, the more
they command and the fewer of them there are; rising in this way from
the lowest ranks to the man at the top, who takes the least direct share
in the action and directs his activity chiefly to commanding.
        </pre>
  </div>
</div>

1 个答案:

答案 0 :(得分:2)

我怀疑某些现有的diff功能可以做到这一点,因此会有更好的答案,但是我使用compare-windows一起破解了以下内容。

(defun my-compare-windows-complete ()
  "Highlight all differences between the two windows."
  (interactive)
  (require 'cl-lib)
  (require 'compare-w)
  (cl-letf ((w1 (get-buffer-window))
            (w1p (prog1 (point) (goto-char (point-min))))
            (w2 (progn (other-window 1) (get-buffer-window)))
            (w2p (prog1 (point) (goto-char (point-min))))
            (compare-windows-highlight 'persistent)
            ((symbol-function 'compare-windows-dehighlight) #'ignore)
            ((symbol-function 'ding) (lambda () (error "done"))))
    (ignore-errors
      (while (compare-windows t)))
    (goto-char w2p)
    (select-window w1)
    (goto-char w1p)))

之后,您可以使用 M-x compare-windows-dehighlight删除突出显示。

用于突出显示的面孔是:

  • compare-windows-removed(从diff-removed继承)
  • compare-windows-added(从diff-added继承)