From: tfb@aisb.ed.ac.uk (Tim Bradshaw)
Newsgroups: alt.lucid-emacs.bug
Subject: A diff.el that works, I hope
Date: 27 Aug 92 17:43:25 GMT
Organization: Not

Here is a version of the diff.el distributed with lemacs-19.2 which
has some bug-fixes and some compatibility stuff for dired and ange-ftp
put in.  This seems to work for me, if you make changes or find bugs
can you let me know?

--Cut--
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  diff.el
# Wrapped by tfb@banks on Wed Aug 26 22:49:55 1992
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f diff.el -a "${1}" != "-c" ; then 
  echo shar: Will not over-write existing file \"diff.el\"
else
echo shar: Extracting \"diff.el\" \(9371 characters\)
sed "s/^X//" >diff.el <<'END_OF_diff.el'
X;;;; * Fixed diff with optional switches
X;; $Id: diff.el,v 1.3 1992/08/26 21:06:27 tfb Exp $
X;; "DIFF" mode for handling output from unix diff utility.
X;; Copyright (C) 1990 Free Software Foundation, Inc.
X;; Written sunpitt!wpmstr!fbresz@Sun.COM 1/27/89
X
X;; This file is part of GNU Emacs.
X
X;; GNU Emacs is free software; you can redistribute it and/or modify
X;; it under the terms of the GNU General Public License as published by
X;; the Free Software Foundation; either version 1, or (at your option)
X;; any later version.
X
X;; GNU Emacs is distributed in the hope that it will be useful,
X;; but WITHOUT ANY WARRANTY; without even the implied warranty of
X;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
X;; GNU General Public License for more details.
X
X;; You should have received a copy of the GNU General Public License
X;; along with GNU Emacs; see the file COPYING.  If not, write to
X;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
X
X;; todo: diff-switches flexibility:
X;; (defconst diff-switches-function
X;;   '(lambda (file)
X;;     (if (string-match "\\.el$" file)
X;; 	 "-c -F\"^(\""
X;;       "-p"))
X;;  "Function to return switches to pass to the `diff' utility, in \\[diff].
X;; This function is called with one arg, a file name, and returns a string
X;; containing 0 or more arguments which are passed on to `diff'.
X;; NOTE: This is not an ordinary hook; it may not be a list of functions.")
X
X;;; moved to loaddefs.el
X;(defvar diff-switches nil
X;  "*A list of switches to pass to the diff program.")
X
X(defvar diff-search-pattern "^\\([0-9]\\|\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\*\\)"
X  "Regular expression that delineates difference regions in diffs.")
X
X;; Initialize the keymap if it isn't already
X(if (boundp 'diff-mode-map)
X    nil
X  (setq diff-mode-map (make-keymap))
X  (suppress-keymap diff-mode-map)
X  (define-key diff-mode-map "?" 'describe-mode)
X  (define-key diff-mode-map "." 'diff-beginning-of-diff)
X  (define-key diff-mode-map " " 'scroll-up)
X  (define-key diff-mode-map "\177" 'scroll-down)
X  (define-key diff-mode-map "n" 'diff-next-difference)
X  (define-key diff-mode-map "p" 'diff-previous-difference)
X  (define-key diff-mode-map "j" 'diff-show-difference))
X
X(defun diff (old new &optional switches)
X  "Find and display the differences between OLD and NEW files.
XInteractively you are prompted with the current buffer's file name for NEW
Xand what appears to be its backup for OLD."
X  (interactive (diff-read-args "Diff original file (%s) "
X			       "Diff new file (%s) "
X			       "Switches for diff (%s) " 
X			       (buffer-file-name)))
X  (setq switches (diff-fix-switches (or switches diff-switches)))
X  (message "Comparing files %s %s..." new old)
X  (setq new (expand-file-name new)
X	old (expand-file-name old))
X  (let ((buffer-read-only nil))
X    (with-output-to-temp-buffer "*Diff Output*"
X      (buffer-disable-undo standard-output)
X      (save-excursion
X	(set-buffer standard-output)
X	(erase-buffer)
X	(apply 'call-process "diff" nil t nil
X	       (append switches (list old new)))))
X    (set-buffer "*Diff Output*")
X    (goto-char (point-min))
X    (while switches
X      (if (string= (car switches) "-c")
X	  ;; strip leading filenames from context diffs
X	  (progn (forward-line 2) (delete-region (point-min) (point))))
X      (setq switches (cdr switches))))
X  (diff-mode)
X  (if (string= "0" diff-total-differences)
X      (message "There are no differences.")
X    (narrow-to-region (point) (progn
X				(forward-line 1)
X				(if 
X				    (re-search-forward diff-search-pattern nil 'move)
X				    (goto-char (match-beginning 0))
X				  (point))))
X				  
X    (setq diff-current-difference "1")))
X
X;;; arg reading from Dired originally
X(defun diff-read-args (oldprompt newprompt switchprompt 
X				 &optional file-for-backup)
X  ;; Grab the args for diff.  OLDPROMPT and NEWPROMPT are the prompts
X  ;; for the old & new filenames, SWITCHPROMPT for the list of
X  ;; switches.  If FILE_FOR_BACKUP is provided (it must be a string if
X  ;; so), then it will be used to try & work out a file & backup to
X  ;; diff, & in this case the prompting order is backwards.  %s in a
X  ;; prompt has a guess substituted into it.  This is nasty.
X  (let (oldf newf)
X    (if file-for-backup
X	(setq newf file-for-backup
X	      newf (if (and newf (file-exists-p newf))
X		       (read-file-name 
X			(format newprompt (file-name-nondirectory newf))
X			nil newf t)
X		     (read-file-name (format newprompt "") nil nil t))
X	      oldf (file-newest-backup newf)
X	      oldf (if (and oldf (file-exists-p oldf))
X		       (read-file-name 
X			(format oldprompt (file-name-nondirectory oldf)) 
X			nil oldf t)
X		     (read-file-name (format oldprompt "") 
X				     (file-name-directory newf) nil t)))
X      ;; Else we aren't trying to be bright...
X      (setq oldf (read-file-name (format oldprompt "") nil nil t)
X	    newf (read-file-name 
X		  (format newprompt (file-name-nondirectory oldf))
X		  nil (file-name-directory oldf) t)))
X	(list oldf newf (diff-read-switches switchprompt))))
X
X(defun diff-read-switches (switchprompt)
X  ;; Read and return a list of switches
X  (if current-prefix-arg
X      (let ((default (mapconcat 'identity diff-switches " ")))
X	(diff-fix-switches
X	 (read-string (format switchprompt default) default)))))
X
X(defun diff-fix-switches (switch-spec)
X  ;; Parse a string into a list of switches or leave it be if it's 
X  ;; not a string
X  (if (stringp switch-spec)
X      (let (result (start 0))
X	(while (string-match "\\(\\S-+\\)" switch-spec start)
X	  (setq result (cons (substring switch-spec (match-beginning 1)
X					(match-end 1))
X			     result)
X		start (match-end 0)))
X	(nreverse result))
X    switch-spec))
X
X
X;; Take a buffer full of Unix diff output and go into a mode to easily 
X;; see the next and previous difference
X(defun diff-mode ()
X  "Diff Mode is used by \\[diff] for perusing the output from the diff program.
XAll normal editing commands are turned off.  Instead, these are available:
X\\<diff-mode-map>
X\\[diff-beginning-of-diff]	Move point to start of this difference.
X\\[scroll-up]	Scroll to next screen of this difference.
X\\[scroll-down]	Scroll to previous screen of this difference.
X\\[diff-next-difference]	Move to Next Difference.
X\\[diff-previous-difference]	Move to Previous Difference.
X\\[diff-show-difference]	Jump to difference specified by numeric position.
X"
X  (interactive)
X  (use-local-map diff-mode-map)
X  (setq buffer-read-only t
X	major-mode 'diff-mode
X	mode-name "Diff"
X	mode-line-modified "--- "
X	mode-line-process
X	'(" " diff-current-difference "/" diff-total-differences))
X  (make-local-variable 'diff-current-difference)
X  (set (make-local-variable 'diff-total-differences)
X       (int-to-string (diff-count-differences))))
X
X(defun diff-next-difference (n)
X  "In diff-mode go the the beginning of the next difference as delimited
Xby diff-search-pattern."
X  (interactive "p")
X  (if (< n 0) (diff-previous-difference (- n))
X    (if (zerop n) ()
X      (goto-char (point-min))
X      (forward-line 1) ; to get past the match for the start of this diff
X      (widen)
X      (if (re-search-forward diff-search-pattern nil 'move n)
X	  (let ((start (goto-char (match-beginning 0))))
X	    (forward-line 1)
X	    (if (re-search-forward diff-search-pattern nil 'move)
X		(goto-char (match-beginning 0)))
X	    (narrow-to-region start (point))
X	    (setq diff-current-difference
X		  (int-to-string (+ n (string-to-int
X				       diff-current-difference)))))
X	(re-search-backward diff-search-pattern nil)
X	(narrow-to-region (point) (point-max))
X	(message "No following differences.")
X	(setq diff-current-difference diff-total-differences))
X      (goto-char (point-min)))))
X      
X(defun diff-previous-difference (n)
X  "In diff-mode go the the beginning of the previous difference as delimited
Xby diff-search-pattern."
X  (interactive "p")
X  (if (< n 0) (diff-next-difference (- n))
X    (if (zerop n) ()
X      (goto-char (point-min))
X      (widen)
X      (if (re-search-backward diff-search-pattern nil 'move n)
X	  (setq diff-current-difference
X		(int-to-string (- (string-to-int diff-current-difference) n)))
X	(message "No previous differences.")
X	(setq diff-current-difference "1"))
X      (narrow-to-region (point) (progn
X				  (forward-line 1)
X				  (if
X				      (re-search-forward diff-search-pattern nil 'move)
X				      (goto-char (match-beginning 0))
X				    (point))))
X      (goto-char (point-min)))))
X
X(defun diff-show-difference (n)
X  "Show difference number N (prefix argument)."
X  (interactive "p")
X  (let ((cur (string-to-int diff-current-difference)))
X    (cond ((or (= n cur)
X	       (zerop n)
X	       (not (natnump n))) ; should signal an error perhaps.
X	   ;; just redisplay.
X	   (goto-char (point-min)))
X	  ((< n cur)
X	   (diff-previous-difference (- cur n)))
X	  ((> n cur)
X	   (diff-next-difference (- n cur))))))
X
X(defun diff-beginning-of-diff ()
X  "Goto beginning of current difference."
X  (interactive)
X  (goto-char (point-min)))
X
X;; This function counts up the number of differences in the buffer.
X(defun diff-count-differences ()
X  "Count number of differences in the current buffer."
X  (message "Counting differences...")
X  (save-excursion
X    (save-restriction
X      (widen)
X      (goto-char (point-min))
X      (let ((cnt 0))
X	(while (re-search-forward diff-search-pattern nil t)
X	  (setq cnt (1+ cnt)))
X	(message "Counting differences...done (%d)" cnt)
X	cnt))))
END_OF_diff.el
if test 9371 -ne `wc -c <diff.el`; then
    echo shar: \"diff.el\" unpacked with wrong size!
fi
# end of overwriting check
fi
echo shar: End of shell archive.
exit 0
Newsgroups: alt.lucid-emacs.bug