+ (coding-system-for-read ediff-coding-system-for-read))
+ (find-file-noselect
+ (read-file-name
+ (format "Patch is in file:%s "
+ (cond ((and buffer-file-name
+ (equal (expand-file-name dir)
+ (file-name-directory buffer-file-name)))
+ (concat
+ " (default "
+ (file-name-nondirectory buffer-file-name)
+ ")"))
+ (t "")))
+ dir buffer-file-name 'must-match))
+ ))
+
+
+;; Try current buffer, then the other window's buffer. Else, give up.
+(defun ediff-prompt-for-patch-buffer ()
+ (get-buffer
+ (read-buffer
+ "Buffer that holds the patch: "
+ (cond ((save-excursion
+ (goto-char (point-min))
+ (re-search-forward ediff-context-diff-label-regexp nil t))
+ (current-buffer))
+ ((save-window-excursion
+ (other-window 1)
+ (save-excursion
+ (goto-char (point-min))
+ (and (re-search-forward ediff-context-diff-label-regexp nil t)
+ (current-buffer)))))
+ ((save-window-excursion
+ (other-window -1)
+ (save-excursion
+ (goto-char (point-min))
+ (and (re-search-forward ediff-context-diff-label-regexp nil t)
+ (current-buffer)))))
+ (t (ediff-other-buffer (current-buffer))))
+ 'must-match)))
+
+
+(defun ediff-get-patch-buffer (&optional arg patch-buf)
+ "Obtain patch buffer. If patch is already in a buffer---use it.
+Else, read patch file into a new buffer. If patch buffer is passed as an
+optional argument, then use it."
+ (let ((last-nonmenu-event t) ; Emacs: don't use dialog box
+ last-command-event) ; XEmacs: don't use dialog box
+
+ (cond ((ediff-buffer-live-p patch-buf))
+ ;; even prefix arg: patch in buffer
+ ((and (integerp arg) (eq 0 (mod arg 2)))
+ (setq patch-buf (ediff-prompt-for-patch-buffer)))
+ ;; odd prefix arg: get patch from a file
+ ((and (integerp arg) (eq 1 (mod arg 2)))
+ (setq patch-buf (ediff-prompt-for-patch-file)))
+ (t (setq patch-buf
+ (if (y-or-n-p "Is the patch already in a buffer? ")
+ (ediff-prompt-for-patch-buffer)
+ (ediff-prompt-for-patch-file)))))
+