Mercurial > emacs
comparison lisp/diff-mode.el @ 98174:841e92880f11
(diff-find-file-name): Rename `batch' to `noprompt' and
be more honest when we don't know.
(diff-tell-file-name): Don't prompt before the actual prompt.
(diff-mode): Don't prompt in add-log-buffer-file-name-function.
(diff-find-source-location): Add `noprompt' argument.
(diff-current-defun): Don't prompt.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Sun, 14 Sep 2008 21:16:30 +0000 |
parents | 4c6e2fb9905e |
children | d42aff5ca541 |
comparison
equal
deleted
inserted
replaced
98173:c0d486723fb3 | 98174:841e92880f11 |
---|---|
721 (interactive | 721 (interactive |
722 (let* ((old current-prefix-arg) | 722 (let* ((old current-prefix-arg) |
723 (fs (diff-hunk-file-names current-prefix-arg))) | 723 (fs (diff-hunk-file-names current-prefix-arg))) |
724 (unless fs (error "No file name to look for")) | 724 (unless fs (error "No file name to look for")) |
725 (list old (read-file-name (format "File for %s: " (car fs)) | 725 (list old (read-file-name (format "File for %s: " (car fs)) |
726 nil (diff-find-file-name old) t)))) | 726 nil (diff-find-file-name old 'noprompt) t)))) |
727 (let ((fs (diff-hunk-file-names old))) | 727 (let ((fs (diff-hunk-file-names old))) |
728 (unless fs (error "No file name to look for")) | 728 (unless fs (error "No file name to look for")) |
729 (push (cons fs name) diff-remembered-files-alist))) | 729 (push (cons fs name) diff-remembered-files-alist))) |
730 | 730 |
731 (defun diff-hunk-file-names (&optional old) | 731 (defun diff-hunk-file-names (&optional old) |
754 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" | 754 "^diff \\(-\\S-+ +\\)*\\(\\S-+\\)\\( +\\(\\S-+\\)\\)?" |
755 nil t) | 755 nil t) |
756 (list (if old (match-string 2) (match-string 4)) | 756 (list (if old (match-string 2) (match-string 4)) |
757 (if old (match-string 4) (match-string 2))))))))) | 757 (if old (match-string 4) (match-string 2))))))))) |
758 | 758 |
759 (defun diff-find-file-name (&optional old batch prefix) | 759 (defun diff-find-file-name (&optional old noprompt prefix) |
760 "Return the file corresponding to the current patch. | 760 "Return the file corresponding to the current patch. |
761 Non-nil OLD means that we want the old file. | 761 Non-nil OLD means that we want the old file. |
762 Non-nil BATCH means to prefer returning an incorrect answer than to prompt | 762 Non-nil NOPROMPT means to prefer returning nil than to prompt the user. |
763 the user. | |
764 PREFIX is only used internally: don't use it." | 763 PREFIX is only used internally: don't use it." |
765 (unless (equal diff-remembered-defdir default-directory) | 764 (unless (equal diff-remembered-defdir default-directory) |
766 ;; Flush diff-remembered-files-alist if the default-directory is changed. | 765 ;; Flush diff-remembered-files-alist if the default-directory is changed. |
767 (set (make-local-variable 'diff-remembered-defdir) default-directory) | 766 (set (make-local-variable 'diff-remembered-defdir) default-directory) |
768 (set (make-local-variable 'diff-remembered-files-alist) nil)) | 767 (set (make-local-variable 'diff-remembered-files-alist) nil)) |
799 ;; attention to the PCL-CVS hint. | 798 ;; attention to the PCL-CVS hint. |
800 (and (not prefix) | 799 (and (not prefix) |
801 (boundp 'cvs-pcl-cvs-dirchange-re) | 800 (boundp 'cvs-pcl-cvs-dirchange-re) |
802 (save-excursion | 801 (save-excursion |
803 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)) | 802 (re-search-backward cvs-pcl-cvs-dirchange-re nil t)) |
804 (diff-find-file-name old batch (match-string 1))) | 803 (diff-find-file-name old noprompt (match-string 1))) |
805 ;; Invent something, if necessary. | |
806 (when batch | |
807 (or (car fs) default-directory)) | |
808 ;; if all else fails, ask the user | 804 ;; if all else fails, ask the user |
809 (let ((file (read-file-name (format "Use file %s: " (or (first fs) "")) | 805 (unless noprompt |
810 nil (first fs) t (first fs)))) | 806 (let ((file (read-file-name (format "Use file %s: " |
811 (set (make-local-variable 'diff-remembered-files-alist) | 807 (or (first fs) "")) |
812 (cons (cons fs file) diff-remembered-files-alist)) | 808 nil (first fs) t (first fs)))) |
813 file))))) | 809 (set (make-local-variable 'diff-remembered-files-alist) |
810 (cons (cons fs file) diff-remembered-files-alist)) | |
811 file)))))) | |
814 | 812 |
815 | 813 |
816 (defun diff-ediff-patch () | 814 (defun diff-ediff-patch () |
817 "Call `ediff-patch-file' on the current buffer." | 815 "Call `ediff-patch-file' on the current buffer." |
818 (interactive) | 816 (interactive) |
1284 nil t)) | 1282 nil t)) |
1285 ;; add-log support | 1283 ;; add-log support |
1286 (set (make-local-variable 'add-log-current-defun-function) | 1284 (set (make-local-variable 'add-log-current-defun-function) |
1287 'diff-current-defun) | 1285 'diff-current-defun) |
1288 (set (make-local-variable 'add-log-buffer-file-name-function) | 1286 (set (make-local-variable 'add-log-buffer-file-name-function) |
1289 'diff-find-file-name)) | 1287 (lambda () (diff-find-file-name nil 'noprompt)))) |
1290 | 1288 |
1291 ;;;###autoload | 1289 ;;;###autoload |
1292 (define-minor-mode diff-minor-mode | 1290 (define-minor-mode diff-minor-mode |
1293 "Minor mode for viewing/editing context diffs. | 1291 "Minor mode for viewing/editing context diffs. |
1294 \\{diff-minor-mode-map}" | 1292 \\{diff-minor-mode-map}" |
1545 (if (> (- (car forw) orig) (- orig (car back))) back forw) | 1543 (if (> (- (car forw) orig) (- orig (car back))) back forw) |
1546 (or back forw)))) | 1544 (or back forw)))) |
1547 | 1545 |
1548 (defsubst diff-xor (a b) (if a (if (not b) a) b)) | 1546 (defsubst diff-xor (a b) (if a (if (not b) a) b)) |
1549 | 1547 |
1550 (defun diff-find-source-location (&optional other-file reverse) | 1548 (defun diff-find-source-location (&optional other-file reverse noprompt) |
1551 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED). | 1549 "Find out (BUF LINE-OFFSET POS SRC DST SWITCHED). |
1552 BUF is the buffer corresponding to the source file. | 1550 BUF is the buffer corresponding to the source file. |
1553 LINE-OFFSET is the offset between the expected and actual positions | 1551 LINE-OFFSET is the offset between the expected and actual positions |
1554 of the text of the hunk or nil if the text was not found. | 1552 of the text of the hunk or nil if the text was not found. |
1555 POS is a pair (BEG . END) indicating the position of the text in the buffer. | 1553 POS is a pair (BEG . END) indicating the position of the text in the buffer. |
1556 SRC and DST are the two variants of text as returned by `diff-hunk-text'. | 1554 SRC and DST are the two variants of text as returned by `diff-hunk-text'. |
1557 SRC is the variant that was found in the buffer. | 1555 SRC is the variant that was found in the buffer. |
1558 SWITCHED is non-nil if the patch is already applied." | 1556 SWITCHED is non-nil if the patch is already applied. |
1557 NOPROMPT, if non-nil, means not to prompt the user." | |
1559 (save-excursion | 1558 (save-excursion |
1560 (let* ((other (diff-xor other-file diff-jump-to-old-file)) | 1559 (let* ((other (diff-xor other-file diff-jump-to-old-file)) |
1561 (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder) | 1560 (char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder) |
1562 (point)))) | 1561 (point)))) |
1563 ;; Check that the hunk is well-formed. Otherwise diff-mode and | 1562 ;; Check that the hunk is well-formed. Otherwise diff-mode and |
1564 ;; the user may disagree on what constitutes the hunk | 1563 ;; the user may disagree on what constitutes the hunk |
1565 ;; (e.g. because an empty line truncates the hunk mid-course), | 1564 ;; (e.g. because an empty line truncates the hunk mid-course), |
1566 ;; leading to potentially nasty surprises for the user. | 1565 ;; leading to potentially nasty surprises for the user. |
1567 (_ (diff-sanity-check-hunk)) | 1566 (_ (diff-sanity-check-hunk)) |
1568 (hunk (buffer-substring (point) | 1567 (hunk (buffer-substring |
1569 (save-excursion (diff-end-of-hunk) (point)))) | 1568 (point) (save-excursion (diff-end-of-hunk) (point)))) |
1570 (old (diff-hunk-text hunk reverse char-offset)) | 1569 (old (diff-hunk-text hunk reverse char-offset)) |
1571 (new (diff-hunk-text hunk (not reverse) char-offset)) | 1570 (new (diff-hunk-text hunk (not reverse) char-offset)) |
1572 ;; Find the location specification. | 1571 ;; Find the location specification. |
1573 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")) | 1572 (line (if (not (looking-at "\\(?:\\*\\{15\\}.*\n\\)?[-@* ]*\\([0-9,]+\\)\\([ acd+]+\\([0-9,]+\\)\\)?")) |
1574 (error "Can't find the hunk header") | 1573 (error "Can't find the hunk header") |
1576 (if (match-end 3) (match-string 3) | 1575 (if (match-end 3) (match-string 3) |
1577 (unless (re-search-forward | 1576 (unless (re-search-forward |
1578 diff-context-mid-hunk-header-re nil t) | 1577 diff-context-mid-hunk-header-re nil t) |
1579 (error "Can't find the hunk separator")) | 1578 (error "Can't find the hunk separator")) |
1580 (match-string 1))))) | 1579 (match-string 1))))) |
1581 (file (or (diff-find-file-name other) (error "Can't find the file"))) | 1580 (file (or (diff-find-file-name other noprompt) |
1581 (error "Can't find the file"))) | |
1582 (buf (find-file-noselect file))) | 1582 (buf (find-file-noselect file))) |
1583 ;; Update the user preference if he so wished. | 1583 ;; Update the user preference if he so wished. |
1584 (when (> (prefix-numeric-value other-file) 8) | 1584 (when (> (prefix-numeric-value other-file) 8) |
1585 (setq diff-jump-to-old-file other)) | 1585 (setq diff-jump-to-old-file other)) |
1586 (with-current-buffer buf | 1586 (with-current-buffer buf |
1716 (kill-local-variable 'change-log-default-name) | 1716 (kill-local-variable 'change-log-default-name) |
1717 (save-excursion | 1717 (save-excursion |
1718 (when (looking-at diff-hunk-header-re) | 1718 (when (looking-at diff-hunk-header-re) |
1719 (forward-line 1) | 1719 (forward-line 1) |
1720 (re-search-forward "^[^ ]" nil t)) | 1720 (re-search-forward "^[^ ]" nil t)) |
1721 (destructuring-bind (buf line-offset pos src dst &optional switched) | 1721 (destructuring-bind (&optional buf line-offset pos src dst switched) |
1722 (diff-find-source-location) | 1722 ;; Use `noprompt' since this is used in which-func-mode and such. |
1723 (beginning-of-line) | 1723 (ignore-errors ;Signals errors in place of prompting. |
1724 (or (when (memq (char-after) '(?< ?-)) | 1724 (diff-find-source-location nil nil 'noprompt)) |
1725 ;; Cursor is pointing at removed text. This could be a removed | 1725 (when buf |
1726 ;; function, in which case, going to the source buffer will | 1726 (beginning-of-line) |
1727 ;; not help since the function is now removed. Instead, | 1727 (or (when (memq (char-after) '(?< ?-)) |
1728 ;; try to figure out the function name just from the code-fragment. | 1728 ;; Cursor is pointing at removed text. This could be a removed |
1729 (let ((old (if switched dst src))) | 1729 ;; function, in which case, going to the source buffer will |
1730 (with-temp-buffer | 1730 ;; not help since the function is now removed. Instead, |
1731 (insert (car old)) | 1731 ;; try to figure out the function name just from the |
1732 (funcall (buffer-local-value 'major-mode buf)) | 1732 ;; code-fragment. |
1733 (goto-char (+ (point-min) (cdr old))) | 1733 (let ((old (if switched dst src))) |
1734 (add-log-current-defun)))) | 1734 (with-temp-buffer |
1735 (with-current-buffer buf | 1735 (insert (car old)) |
1736 (goto-char (+ (car pos) (cdr src))) | 1736 (funcall (buffer-local-value 'major-mode buf)) |
1737 (add-log-current-defun)))))) | 1737 (goto-char (+ (point-min) (cdr old))) |
1738 (add-log-current-defun)))) | |
1739 (with-current-buffer buf | |
1740 (goto-char (+ (car pos) (cdr src))) | |
1741 (add-log-current-defun))))))) | |
1738 | 1742 |
1739 (defun diff-ignore-whitespace-hunk () | 1743 (defun diff-ignore-whitespace-hunk () |
1740 "Re-diff the current hunk, ignoring whitespace differences." | 1744 "Re-diff the current hunk, ignoring whitespace differences." |
1741 (interactive) | 1745 (interactive) |
1742 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder) | 1746 (let* ((char-offset (- (point) (progn (diff-beginning-of-hunk 'try-harder) |