Emacs ediff marked files in different dired buffers
Asked Answered
P

3

5

I have the following function which runs ediff on the files I have marked in a dired buffer:

(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)))))

It only works on files in the same directory, how could I make it work for files I mark in different dired directories?

Pawnbroker answered 8/8, 2013 at 8:55 Comment(0)
P
4

Here is my solution, it works for files marked in the same dired buffer and also for files in different buffers.

(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")))))
Pawnbroker answered 1/10, 2013 at 9:16 Comment(0)
L
3

Augmenting mkm's answer a bit. In addition to working on 2 marked files potentially across dired buffers, it handles the case when there are 0 or 1 marked files. 0 marked files will use the file under the cursor as file A, and prompt for a file to compare with. 1 marked files will use the marked file as file A, and prompt for a file to compare with. The file under the point is used as the default in the prompt. I bound this to =.

(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")))))
Liveried answered 20/9, 2014 at 2:31 Comment(0)
T
2

Here's a solution:

(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)))
Totaquine answered 8/8, 2013 at 9:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.