-;;;###autoload
-(defun ediff-patch-file (source-filename &optional startup-hooks job-name)
- "Run Ediff by patching SOURCE-FILENAME."
- ;; This now returns the control buffer
- (interactive
- (list (ediff-read-file-name
- "File to patch"
- (if ediff-use-last-dir
- ediff-last-dir-patch
- default-directory)
- (ediff-get-default-file-name))))
-
- (setq source-filename (expand-file-name source-filename))
- (ediff-get-patch-buffer
- (if (eq job-name 'ediff-patch-buffer)
- (ediff-eval-in-buffer (get-file-buffer source-filename)
- default-directory)
- (file-name-directory source-filename)))
-
- (let* ((backup-extension
- ;; if the user specified a -b option, extract the backup
- ;; extension from there; else use ediff-backup-extension
- (substring ediff-patch-options
- (if (string-match "-b[ \t]+" ediff-patch-options)
- (match-end 0) 0)
- (if (string-match "-b[ \t]+[^ \t]+" ediff-patch-options)
- (match-end 0) 0)))
- (shell-file-name ediff-shell)
- ;; ediff-find-file may use a temp file to do the patch
- ;; so, we save source-filename and true-source-filename as a var
- ;; that initially is source-filename but may be changed to a temp
- ;; file for the purpose of patching.
- (true-source-filename source-filename)
- (target-filename source-filename)
- target-buf buf-to-patch file-name-magic-p ctl-buf backup-style)
-
- ;; if the user didn't specify a backup extension, use
- ;; ediff-backup-extension
- (if (string= backup-extension "")
- (setq backup-extension ediff-backup-extension))
- (if (string-match "-V" ediff-patch-options)
- (error
- "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
-
- ;; Make a temp file, if source-filename has a magic file handler (or if
- ;; it is handled via auto-mode-alist and similar magic).
- ;; Check if there is a buffer visiting source-filename and if they are in
- ;; sync; arrange for the deletion of temp file.
- (ediff-find-file 'true-source-filename 'buf-to-patch
- 'ediff-last-dir-patch 'startup-hooks)
-
- ;; Check if source file name has triggered black magic, such as file name
- ;; handlers or auto mode alist, and make a note of it.
- ;; true-source-filename should be either the original name or a
- ;; temporary file where we put the after-product of the file handler.
- (setq file-name-magic-p (not (equal (file-truename true-source-filename)
- (file-truename source-filename))))
-
- ;; Checkout orig file, if necessary, so that the patched file could be
- ;; checked back in.
- (if (ediff-file-checked-in-p (buffer-file-name buf-to-patch))
- (ediff-toggle-read-only buf-to-patch))
-
- (ediff-eval-in-buffer ediff-patch-diagnostics
- (message "Applying patch ... ")
- ;; fix environment for gnu patch, so it won't make numbered extensions
- (setq backup-style (getenv "VERSION_CONTROL"))
- (setenv "VERSION_CONTROL" nil)
- ;; always pass patch the -f option, so it won't ask any questions
- (shell-command-on-region
- (point-min) (point-max)
- (format "%s -f %s -b %s %s"
- ediff-patch-program ediff-patch-options
- backup-extension
- (expand-file-name true-source-filename))
- t)
- ;; restore environment for gnu patch
- (setenv "VERSION_CONTROL" backup-style))
- ;;(message "Applying patch ... done")(sit-for 0)
- (switch-to-buffer ediff-patch-diagnostics)
- (sit-for 0) ; synchronize - let the user see diagnostics
-
- (or (file-exists-p (concat true-source-filename backup-extension))
- (error "Patch failed or didn't modify the original file"))
-
- ;; If black magic is involved, apply patch to a temp copy of the
- ;; file. Otherwise, apply patch to the orig copy. If patch is applied
- ;; to temp copy, we name the result old-name_patched for local files
- ;; and temp-copy_patched for remote files. The orig file name isn't
- ;; changed, and the temp copy of the original is later deleted.
- ;; Without magic, the original file is renamed (usually into
- ;; old-name_orig) and the result of patching will have the same name as
- ;; the original.
- (if (not file-name-magic-p)
- (ediff-eval-in-buffer buf-to-patch
- (set-visited-file-name (concat source-filename backup-extension))
- (set-buffer-modified-p nil))
-
- ;; Black magic in effect.
- ;; If orig file was remote, put the patched file in the temp directory.
- ;; If orig file is local, put the patched file in the directory of
- ;; the orig file.
- (setq target-filename
- (concat
- (if (ediff-file-remote-p (file-truename source-filename))
- true-source-filename
- source-filename)
- "_patched"))
-
- (rename-file true-source-filename target-filename t)
-
- ;; arrange that the temp copy of orig will be deleted
- (rename-file (concat true-source-filename backup-extension)
- true-source-filename t))
-
- ;; make orig buffer read-only
- (setq startup-hooks
- (cons 'ediff-set-read-only-in-buf-A startup-hooks))
-
- ;; set up a buf for the patched file
- (setq target-buf (find-file-noselect target-filename))
-
- (setq ctl-buf
- (ediff-buffers-internal
- buf-to-patch target-buf nil
- startup-hooks (or job-name 'ediff-patch-file)))
-
- (bury-buffer ediff-patch-diagnostics)
- (message "Patch diagnostics are available in buffer %s"
- (buffer-name ediff-patch-diagnostics))
- ctl-buf))
-