Emacs ediff标记了不同的dired缓冲区中的文件

时间:2013-08-08 08:55:14

标签: emacs dired emacs-ediff

我有以下函数对我在dired buffer中标记的文件运行ediff:

(defun mkm/ediff-marked-pair ()
  "Run ediff-files on a pair of files marked in dired buffer"
  (interactive)
  (let ((marked-files (dired-get-marked-files nil)))
    (if (not (= (length marked-files) 2))
    (message "mark exactly 2 files")
      (ediff-files (nth 0 marked-files)
           (nth 1 marked-files)))))

它仅适用于同一目录中的文件,如何才能使其在我在不同的目录目录中标记的文件中工作?

3 个答案:

答案 0 :(得分:3)

这是我的解决方案,它适用于标记在同一个dired缓冲区中的文件以及不同缓冲区中的文件。

(defun mkm/ediff-marked-pair ()
  "Run ediff-files on a pair of files marked in dired buffer"
  (interactive)
  (let* ((marked-files (dired-get-marked-files nil nil))
         (other-win (get-window-with-predicate
                     (lambda (window)
                       (with-current-buffer (window-buffer window)
                         (and (not (eq window (selected-window)))
                              (eq major-mode 'dired-mode))))))
         (other-marked-files (and other-win
                                  (with-current-buffer (window-buffer other-win)
                                    (dired-get-marked-files nil)))))
    (cond ((= (length marked-files) 2)
           (ediff-files (nth 0 marked-files)
                        (nth 1 marked-files)))
          ((and (= (length marked-files) 1)
                (= (length other-marked-files) 1))
           (ediff-files (nth 0 marked-files)
                        (nth 0 other-marked-files)))
          (t (error "mark exactly 2 files, at least 1 locally")))))

答案 1 :(得分:2)

这是一个解决方案:

(defvar mkm/dired-file-1)

(defun mkm/ediff-push ()
  (interactive)
  (setq mkm/dired-file-1 (dired-get-filename)))
(defun mkm/ediff-pop ()
  (interactive)
  (ediff-files mkm/dired-file-1 (dired-get-filename)))
(add-hook 'dired-mode-hook
      (lambda()
            (define-key dired-mode-map (kbd "C-c u") 'mkm/ediff-push)
            (define-key dired-mode-map (kbd "C-c o") 'mkm/ediff-pop)))

答案 2 :(得分:2)

稍微增加mkm的答案。除了可能跨越dired缓冲区处理2个标记文件之外,它还处理有0或1个标记文件的情况。 0标记的文件将使用光标下的文件作为文件A,并提示要与之比较的文件。 1个标记的文件将使用标记的文件作为文件A,并提示要与之比较的文件。该点下的文件将用作提示中的默认值。我把它绑定到=。

(defun mkm/ediff-marked-pair ()
   "Run ediff-files on a pair of files marked in dired buffer"
   (interactive)
   (let* ((marked-files (dired-get-marked-files nil nil))
          (other-win (get-window-with-predicate
                      (lambda (window)
                        (with-current-buffer (window-buffer window)
                          (and (not (eq window (selected-window)))
                               (eq major-mode 'dired-mode))))))
          (other-marked-files (and other-win
                                   (with-current-buffer (window-buffer other-win)
                                     (dired-get-marked-files nil)))))
     (cond ((= (length marked-files) 2)
            (ediff-files (nth 0 marked-files)
                         (nth 1 marked-files)))
           ((and (= (length marked-files) 1)
                 (= (length other-marked-files) 1))
            (ediff-files (nth 0 marked-files)
                         (nth 0 other-marked-files)))
           ((= (length marked-files) 1)
            (let ((single-file (nth 0 marked-files))) 
              (ediff-files single-file
                           (read-file-name
                            (format "Diff %s with: " single-file)
                            nil (m (if (string= single-file (dired-get-filename))
                                       nil
                                     (dired-get-filename))) t))))
           (t (error "mark no more than 2 files")))))