home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / packages / diff.el < prev    next >
Encoding:
Text File  |  1992-08-29  |  9.2 KB  |  251 lines

  1. ;; Id: diff.el,v 1.3 1992/08/26 21:06:27 tfb Exp 
  2. ;; "DIFF" mode for handling output from unix diff utility.
  3. ;; Copyright (C) 1990 Free Software Foundation, Inc.
  4. ;; Written sunpitt!wpmstr!fbresz@Sun.COM 1/27/89
  5. ;; hacked on by tfb@aisb.ed.ac.uk (Tim Bradshaw)
  6.  
  7. ;; This file is part of GNU Emacs.
  8.  
  9. ;; GNU Emacs is free software; you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation; either version 1, or (at your option)
  12. ;; any later version.
  13.  
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17. ;; GNU General Public License for more details.
  18.  
  19. ;; You should have received a copy of the GNU General Public License
  20. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  21. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  22.  
  23. ;; todo: diff-switches flexibility:
  24. ;; (defconst diff-switches-function
  25. ;;   '(lambda (file)
  26. ;;     (if (string-match "\\.el$" file)
  27. ;;      "-c -F\"^(\""
  28. ;;       "-p"))
  29. ;;  "Function to return switches to pass to the `diff' utility, in \\[diff].
  30. ;; This function is called with one arg, a file name, and returns a string
  31. ;; containing 0 or more arguments which are passed on to `diff'.
  32. ;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
  33.  
  34. ;;; moved to loaddefs.el
  35. ;(defvar diff-switches nil
  36. ;  "*A list of switches to pass to the diff program.")
  37.  
  38. (defvar diff-search-pattern "^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
  39.   "Regular expression that delineates difference regions in diffs.")
  40.  
  41. (defvar diff-mode-map nil)
  42. (defvar diff-total-differences)
  43. (defvar diff-current-difference)
  44.  
  45. ;; Initialize the keymap if it isn't already
  46. (if diff-mode-map
  47.     nil
  48.   (setq diff-mode-map (make-keymap))
  49.   (suppress-keymap diff-mode-map)
  50.   (define-key diff-mode-map "?" 'describe-mode)
  51.   (define-key diff-mode-map "." 'diff-beginning-of-diff)
  52.   (define-key diff-mode-map " " 'scroll-up)
  53.   (define-key diff-mode-map "\177" 'scroll-down)
  54.   (define-key diff-mode-map "n" 'diff-next-difference)
  55.   (define-key diff-mode-map "p" 'diff-previous-difference)
  56.   (define-key diff-mode-map "j" 'diff-show-difference))
  57.  
  58. (defun diff (old new &optional switches)
  59.   "Find and display the differences between OLD and NEW files.
  60. Interactively you are prompted with the current buffer's file name for NEW
  61. and what appears to be its backup for OLD."
  62.   (interactive (diff-read-args "Diff original file (%s) "
  63.                    "Diff new file (%s) "
  64.                    "Switches for diff (%s) " 
  65.                    (buffer-file-name)))
  66.   (setq switches (diff-fix-switches (or switches diff-switches)))
  67.   (message "Comparing files %s %s..." new old)
  68.   (setq new (expand-file-name new)
  69.     old (expand-file-name old))
  70.   (let ((buffer-read-only nil))
  71.     (with-output-to-temp-buffer "*Diff Output*"
  72.       (buffer-disable-undo standard-output)
  73.       (save-excursion
  74.     (set-buffer standard-output)
  75.     (erase-buffer)
  76.     (apply 'call-process "diff" nil t nil
  77.            (append switches (list old new)))))
  78.     (set-buffer "*Diff Output*")
  79.     (goto-char (point-min))
  80.     (while switches
  81.       (if (string= (car switches) "-c")
  82.       ;; strip leading filenames from context diffs
  83.       (progn (forward-line 2) (delete-region (point-min) (point))))
  84.       (setq switches (cdr switches))))
  85.   (diff-mode)
  86.   (if (string= "0" diff-total-differences)
  87.       (message "There are no differences.")
  88.     (narrow-to-region (point) (progn
  89.                 (forward-line 1)
  90.                 (if 
  91.                     (re-search-forward diff-search-pattern nil 'move)
  92.                     (goto-char (match-beginning 0))
  93.                   (point))))
  94.                   
  95.     (setq diff-current-difference "1")))
  96.  
  97. ;;; arg reading from Dired originally
  98. (defun diff-read-args (oldprompt newprompt switchprompt 
  99.                  &optional file-for-backup)
  100.   ;; Grab the args for diff.  OLDPROMPT and NEWPROMPT are the prompts
  101.   ;; for the old & new filenames, SWITCHPROMPT for the list of
  102.   ;; switches.  If FILE_FOR_BACKUP is provided (it must be a string if
  103.   ;; so), then it will be used to try & work out a file & backup to
  104.   ;; diff, & in this case the prompting order is backwards.  %s in a
  105.   ;; prompt has a guess substituted into it.  This is nasty.
  106.   (let (oldf newf)
  107.     (if file-for-backup
  108.     (setq newf file-for-backup
  109.           newf (if (and newf (file-exists-p newf))
  110.                (read-file-name 
  111.             (format newprompt (file-name-nondirectory newf))
  112.             nil newf t)
  113.              (read-file-name (format newprompt "") nil nil t))
  114.           oldf (file-newest-backup newf)
  115.           oldf (if (and oldf (file-exists-p oldf))
  116.                (read-file-name 
  117.             (format oldprompt (file-name-nondirectory oldf)) 
  118.             nil oldf t)
  119.              (read-file-name (format oldprompt "") 
  120.                      (file-name-directory newf) nil t)))
  121.       ;; Else we aren't trying to be bright...
  122.       (setq oldf (read-file-name (format oldprompt "") nil nil t)
  123.         newf (read-file-name 
  124.           (format newprompt (file-name-nondirectory oldf))
  125.           nil (file-name-directory oldf) t)))
  126.     (list oldf newf (diff-read-switches switchprompt))))
  127.  
  128. (defun diff-read-switches (switchprompt)
  129.   ;; Read and return a list of switches
  130.   (if current-prefix-arg
  131.       (let ((default (mapconcat 'identity diff-switches " ")))
  132.     (diff-fix-switches
  133.      (read-string (format switchprompt default) default)))))
  134.  
  135. (defun diff-fix-switches (switch-spec)
  136.   ;; Parse a string into a list of switches or leave it be if it's 
  137.   ;; not a string
  138.   (if (stringp switch-spec)
  139.       (let (result (start 0))
  140.     (while (string-match "\\(\\S-+\\)" switch-spec start)
  141.       (setq result (cons (substring switch-spec (match-beginning 1)
  142.                     (match-end 1))
  143.                  result)
  144.         start (match-end 0)))
  145.     (nreverse result))
  146.     switch-spec))
  147.  
  148.  
  149. ;; Take a buffer full of Unix diff output and go into a mode to easily 
  150. ;; see the next and previous difference
  151. (defun diff-mode ()
  152.   "Diff Mode is used by \\[diff] for perusing the output from the diff program.
  153. All normal editing commands are turned off.  Instead, these are available:
  154. \\<diff-mode-map>
  155. \\[diff-beginning-of-diff]    Move point to start of this difference.
  156. \\[scroll-up]    Scroll to next screen of this difference.
  157. \\[scroll-down]    Scroll to previous screen of this difference.
  158. \\[diff-next-difference]    Move to Next Difference.
  159. \\[diff-previous-difference]    Move to Previous Difference.
  160. \\[diff-show-difference]    Jump to difference specified by numeric position.
  161. "
  162.   (interactive)
  163.   (use-local-map diff-mode-map)
  164.   (setq buffer-read-only t
  165.     major-mode 'diff-mode
  166.     mode-name "Diff"
  167.     mode-line-modified "--- "
  168.     mode-line-process
  169.     '(" " diff-current-difference "/" diff-total-differences))
  170.   (make-local-variable 'diff-current-difference)
  171.   (set (make-local-variable 'diff-total-differences)
  172.        (int-to-string (diff-count-differences))))
  173.  
  174. (defun diff-next-difference (n)
  175.   "In diff-mode go the the beginning of the next difference as delimited
  176. by diff-search-pattern."
  177.   (interactive "p")
  178.   (if (< n 0) (diff-previous-difference (- n))
  179.     (if (zerop n) ()
  180.       (goto-char (point-min))
  181.       (forward-line 1) ; to get past the match for the start of this diff
  182.       (widen)
  183.       (if (re-search-forward diff-search-pattern nil 'move n)
  184.       (let ((start (goto-char (match-beginning 0))))
  185.         (forward-line 1)
  186.         (if (re-search-forward diff-search-pattern nil 'move)
  187.         (goto-char (match-beginning 0)))
  188.         (narrow-to-region start (point))
  189.         (setq diff-current-difference
  190.           (int-to-string (+ n (string-to-int
  191.                        diff-current-difference)))))
  192.     (re-search-backward diff-search-pattern nil)
  193.     (narrow-to-region (point) (point-max))
  194.     (message "No following differences.")
  195.     (setq diff-current-difference diff-total-differences))
  196.       (goto-char (point-min)))))
  197.       
  198. (defun diff-previous-difference (n)
  199.   "In diff-mode go the the beginning of the previous difference as delimited
  200. by diff-search-pattern."
  201.   (interactive "p")
  202.   (if (< n 0) (diff-next-difference (- n))
  203.     (if (zerop n) ()
  204.       (goto-char (point-min))
  205.       (widen)
  206.       (if (re-search-backward diff-search-pattern nil 'move n)
  207.       (setq diff-current-difference
  208.         (int-to-string (- (string-to-int diff-current-difference) n)))
  209.     (message "No previous differences.")
  210.     (setq diff-current-difference "1"))
  211.       (narrow-to-region (point) (progn
  212.                   (forward-line 1)
  213.                   (if
  214.                       (re-search-forward diff-search-pattern nil 'move)
  215.                       (goto-char (match-beginning 0))
  216.                     (point))))
  217.       (goto-char (point-min)))))
  218.  
  219. (defun diff-show-difference (n)
  220.   "Show difference number N (prefix argument)."
  221.   (interactive "p")
  222.   (let ((cur (string-to-int diff-current-difference)))
  223.     (cond ((or (= n cur)
  224.            (zerop n)
  225.            (not (natnump n))) ; should signal an error perhaps.
  226.        ;; just redisplay.
  227.        (goto-char (point-min)))
  228.       ((< n cur)
  229.        (diff-previous-difference (- cur n)))
  230.       ((> n cur)
  231.        (diff-next-difference (- n cur))))))
  232.  
  233. (defun diff-beginning-of-diff ()
  234.   "Goto beginning of current difference."
  235.   (interactive)
  236.   (goto-char (point-min)))
  237.  
  238. ;; This function counts up the number of differences in the buffer.
  239. (defun diff-count-differences ()
  240.   "Count number of differences in the current buffer."
  241.   (message "Counting differences...")
  242.   (save-excursion
  243.     (save-restriction
  244.       (widen)
  245.       (goto-char (point-min))
  246.       (let ((cnt 0))
  247.     (while (re-search-forward diff-search-pattern nil t)
  248.       (setq cnt (1+ cnt)))
  249.     (message "Counting differences...done (%d)" cnt)
  250.     cnt))))
  251.