diff.el 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. ;;; diff.el --- run `diff'
  2. ;; Copyright (C) 1992, 1994, 1996, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Frank Bresz
  4. ;; (according to authors.el)
  5. ;; Maintainer: FSF
  6. ;; Keywords: unix, vc, tools
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This package helps you explore differences between files, using the
  20. ;; UNIX command diff(1). The commands are `diff' and `diff-backup'.
  21. ;; You can specify options with `diff-switches'.
  22. ;;; Code:
  23. (eval-when-compile (require 'cl))
  24. (defgroup diff nil
  25. "Comparing files with `diff'."
  26. :group 'tools)
  27. ;;;###autoload
  28. (defcustom diff-switches (purecopy "-c")
  29. "A string or list of strings specifying switches to be passed to diff."
  30. :type '(choice string (repeat string))
  31. :group 'diff)
  32. ;;;###autoload
  33. (defcustom diff-command (purecopy "diff")
  34. "The command to use to run diff."
  35. :type 'string
  36. :group 'diff)
  37. ;; prompt if prefix arg present
  38. (defun diff-switches ()
  39. (if current-prefix-arg
  40. (read-string "Diff switches: "
  41. (if (stringp diff-switches)
  42. diff-switches
  43. (mapconcat 'identity diff-switches " ")))))
  44. (defun diff-sentinel (code &optional old-temp-file new-temp-file)
  45. "Code run when the diff process exits.
  46. CODE is the exit code of the process. It should be 0 only if no diffs
  47. were found.
  48. If optional args OLD-TEMP-FILE and/or NEW-TEMP-FILE are non-nil,
  49. delete the temporary files so named."
  50. (if old-temp-file (delete-file old-temp-file))
  51. (if new-temp-file (delete-file new-temp-file))
  52. (save-excursion
  53. (goto-char (point-max))
  54. (let ((inhibit-read-only t))
  55. (insert (format "\nDiff finished%s. %s\n"
  56. (cond ((equal 0 code) " (no differences)")
  57. ((equal 2 code) " (diff error)")
  58. (t ""))
  59. (current-time-string))))))
  60. ;;;###autoload
  61. (defun diff (old new &optional switches no-async)
  62. "Find and display the differences between OLD and NEW files.
  63. When called interactively, read NEW, then OLD, using the
  64. minibuffer. The default for NEW is the current buffer's file
  65. name, and the default for OLD is a backup file for NEW, if one
  66. exists. If NO-ASYNC is non-nil, call diff synchronously.
  67. When called interactively with a prefix argument, prompt
  68. interactively for diff switches. Otherwise, the switches
  69. specified in `diff-switches' are passed to the diff command."
  70. (interactive
  71. (let* ((newf (if (and buffer-file-name (file-exists-p buffer-file-name))
  72. (read-file-name
  73. (concat "Diff new file (default "
  74. (file-name-nondirectory buffer-file-name) "): ")
  75. nil buffer-file-name t)
  76. (read-file-name "Diff new file: " nil nil t)))
  77. (oldf (file-newest-backup newf)))
  78. (setq oldf (if (and oldf (file-exists-p oldf))
  79. (read-file-name
  80. (concat "Diff original file (default "
  81. (file-name-nondirectory oldf) "): ")
  82. (file-name-directory oldf) oldf t)
  83. (read-file-name "Diff original file: "
  84. (file-name-directory newf) nil t)))
  85. (list oldf newf (diff-switches))))
  86. (display-buffer
  87. (diff-no-select old new switches no-async)))
  88. (defun diff-file-local-copy (file-or-buf)
  89. (if (bufferp file-or-buf)
  90. (with-current-buffer file-or-buf
  91. (let ((tempfile (make-temp-file "buffer-content-")))
  92. (write-region nil nil tempfile nil 'nomessage)
  93. tempfile))
  94. (file-local-copy file-or-buf)))
  95. (defun diff-no-select (old new &optional switches no-async buf)
  96. ;; Noninteractive helper for creating and reverting diff buffers
  97. (unless (bufferp new) (setq new (expand-file-name new)))
  98. (unless (bufferp old) (setq old (expand-file-name old)))
  99. (or switches (setq switches diff-switches)) ; If not specified, use default.
  100. (unless (listp switches) (setq switches (list switches)))
  101. (or buf (setq buf (get-buffer-create "*Diff*")))
  102. (let* ((old-alt (diff-file-local-copy old))
  103. (new-alt (diff-file-local-copy new))
  104. (command
  105. (mapconcat 'identity
  106. `(,diff-command
  107. ;; Use explicitly specified switches
  108. ,@switches
  109. ,@(mapcar #'shell-quote-argument
  110. (nconc
  111. (when (or old-alt new-alt)
  112. (list "-L" (if (stringp old)
  113. old (prin1-to-string old))
  114. "-L" (if (stringp new)
  115. new (prin1-to-string new))))
  116. (list (or old-alt old)
  117. (or new-alt new)))))
  118. " "))
  119. (thisdir default-directory))
  120. (with-current-buffer buf
  121. (setq buffer-read-only t)
  122. (buffer-disable-undo (current-buffer))
  123. (let ((inhibit-read-only t))
  124. (erase-buffer))
  125. (buffer-enable-undo (current-buffer))
  126. (diff-mode)
  127. (set (make-local-variable 'revert-buffer-function)
  128. (lexical-let ((old old) (new new)
  129. (switches switches)
  130. (no-async no-async))
  131. (lambda (ignore-auto noconfirm)
  132. (diff-no-select old new switches no-async (current-buffer)))))
  133. (setq default-directory thisdir)
  134. (let ((inhibit-read-only t))
  135. (insert command "\n"))
  136. (if (and (not no-async) (fboundp 'start-process))
  137. (let ((proc (start-process "Diff" buf shell-file-name
  138. shell-command-switch command)))
  139. (set-process-filter proc 'diff-process-filter)
  140. (lexical-let ((old-alt old-alt) (new-alt new-alt))
  141. (set-process-sentinel
  142. proc (lambda (proc msg)
  143. (with-current-buffer (process-buffer proc)
  144. (diff-sentinel (process-exit-status proc)
  145. old-alt new-alt))))))
  146. ;; Async processes aren't available.
  147. (let ((inhibit-read-only t))
  148. (diff-sentinel
  149. (call-process shell-file-name nil buf nil
  150. shell-command-switch command)
  151. old-alt new-alt))))
  152. buf))
  153. (defun diff-process-filter (proc string)
  154. (with-current-buffer (process-buffer proc)
  155. (let ((moving (= (point) (process-mark proc))))
  156. (save-excursion
  157. ;; Insert the text, advancing the process marker.
  158. (goto-char (process-mark proc))
  159. (let ((inhibit-read-only t))
  160. (insert string))
  161. (set-marker (process-mark proc) (point)))
  162. (if moving (goto-char (process-mark proc))))))
  163. ;;;###autoload
  164. (defun diff-backup (file &optional switches)
  165. "Diff this file with its backup file or vice versa.
  166. Uses the latest backup, if there are several numerical backups.
  167. If this file is a backup, diff it with its original.
  168. The backup file is the first file given to `diff'.
  169. With prefix arg, prompt for diff switches."
  170. (interactive (list (read-file-name "Diff (file with backup): ")
  171. (diff-switches)))
  172. (let (bak ori)
  173. (if (backup-file-name-p file)
  174. (setq bak file
  175. ori (file-name-sans-versions file))
  176. (setq bak (or (diff-latest-backup-file file)
  177. (error "No backup found for %s" file))
  178. ori file))
  179. (diff bak ori switches)))
  180. (defun diff-latest-backup-file (fn) ; actually belongs into files.el
  181. "Return the latest existing backup of FILE, or nil."
  182. (let ((handler (find-file-name-handler fn 'diff-latest-backup-file)))
  183. (if handler
  184. (funcall handler 'diff-latest-backup-file fn)
  185. (file-newest-backup fn))))
  186. ;;;###autoload
  187. (defun diff-buffer-with-file (&optional buffer)
  188. "View the differences between BUFFER and its associated file.
  189. This requires the external program `diff' to be in your `exec-path'."
  190. (interactive "bBuffer: ")
  191. (with-current-buffer (get-buffer (or buffer (current-buffer)))
  192. (diff buffer-file-name (current-buffer) nil 'noasync)))
  193. (provide 'diff)
  194. ;;; diff.el ends here