Function: dired-diff

dired-diff is an autoloaded, interactive and byte-compiled function defined in dired-aux.el.gz.

Signature

(dired-diff FILE &optional SWITCHES)

Documentation

Compare file at point with FILE using diff.

If called interactively, prompt for FILE. If the mark is active in Transient Mark mode, use the file at the mark as the default for FILE. (That's the mark set by C-SPC (set-mark-command), not by Dired's M-x dired-mark (dired-mark) command.) If the file at point has a backup file, use that as the default FILE. If the file at point is a backup file, use its original, if that exists and can be found. Note that customizations of backup-directory-alist and make-backup-file-name-function change where this function searches for the backup file, and affect its ability to find the original of a backup file.

FILE is the first argument given to the diff function. The file at point is the second argument given to diff.

With prefix arg, prompt for second argument SWITCHES, which is the string of command switches used as the third argument of diff.

View in manual

Probably introduced at or before Emacs version 24.3.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/dired-aux.el.gz
;;;###autoload
(defun dired-diff (file &optional switches)
  "Compare file at point with FILE using `diff'.
If called interactively, prompt for FILE.
If the mark is active in Transient Mark mode, use the file at the mark
as the default for FILE.  (That's the mark set by \\[set-mark-command],
not by Dired's \\[dired-mark] command.)
If the file at point has a backup file, use that as the default FILE.
If the file at point is a backup file, use its original, if that exists
and can be found.  Note that customizations of `backup-directory-alist'
and `make-backup-file-name-function' change where this function searches
for the backup file, and affect its ability to find the original of a
backup file.

FILE is the first argument given to the `diff' function.  The file at
point is the second argument given to `diff'.

With prefix arg, prompt for second argument SWITCHES, which is
the string of command switches used as the third argument of `diff'."
  (interactive
   (let* ((current (dired-get-filename t))
	  ;; Get the latest existing backup file or its original.
	  (oldf (if (backup-file-name-p current)
		    (file-name-sans-versions current)
		  (diff-latest-backup-file current)))
	  ;; Get the file at the mark.
	  (file-at-mark (if (and transient-mark-mode mark-active)
			    (save-excursion (goto-char (mark t))
					    (dired-get-filename t t))))
          (separate-dir (and oldf
                             (not (equal (file-name-directory oldf)
                                         (dired-current-directory)))))
	  (default-file (or file-at-mark
                            ;; If the file with which to compare
                            ;; doesn't exist, or we cannot intuit it,
                            ;; we forget that name and don't show it
                            ;; as the default, as an indication to the
                            ;; user that she should type the file
                            ;; name.
			    (and (if (and oldf (file-readable-p oldf)) oldf)
                                 (if separate-dir
                                     oldf
                                   (file-name-nondirectory oldf)))))
	  ;; Use it as default if it's not the same as the current file,
	  ;; and the target dir is current or there is a default file.
	  (default (if (and (not (equal default-file current))
			    (or (equal (dired-dwim-target-directory)
				       (dired-current-directory))
				default-file))
		       default-file))
	  (target-dir (if default
                          (if separate-dir
                              (file-name-directory default)
                            (dired-current-directory))
			(dired-dwim-target-directory)))
	  (defaults (append
                     (if (backup-file-name-p current)
                         ;; This is a backup file -- put the other
                         ;; main file, and the other backup files into
                         ;; the `M-n' list.
                         (delete (expand-file-name current)
                                 (cons (expand-file-name
                                        (file-name-sans-versions current))
                                       (file-backup-file-names
                                        (file-name-sans-versions current))))
                       ;; Non-backup file -- use the backup files as
                       ;; `M-n' candidates.
                       (file-backup-file-names current))
                     (dired-dwim-target-defaults (list current) target-dir))))
     (list
      (minibuffer-with-setup-hook
	  (lambda ()
            (setq-local minibuffer-default-add-function nil)
	    (setq minibuffer-default defaults))
	(read-file-name (format-prompt "Diff %s with" default current)
                        target-dir default t))
      (if current-prefix-arg
	  (read-string "Options for diff: "
		       (if (stringp diff-switches)
			   diff-switches
			 (mapconcat #'identity diff-switches " "))))))
   dired-mode)
  (let ((current (dired-get-filename t)))
    (when (or (equal (expand-file-name file)
		     (expand-file-name current))
	      (and (file-directory-p file)
		   (equal (expand-file-name current file)
			  (expand-file-name current))))
      (error "Attempt to compare the file to itself"))
    (if (and (backup-file-name-p current)
	     (equal file (file-name-sans-versions current)))
	(diff current file switches)
      (diff file current switches))))