diff options
| author | Basil L. Contovounesios | 2023-04-01 15:14:34 +0100 |
|---|---|---|
| committer | Basil L. Contovounesios | 2023-04-08 11:51:43 +0100 |
| commit | 10b58633b566cf8f66f12e2126da3b43cd09dfc8 (patch) | |
| tree | 58db217f3824018d92b20d1f778140ee52a17f0f | |
| parent | 9848ae17161828190cc0ba31e89ae54a2f08a2ef (diff) | |
| download | emacs-10b58633b566cf8f66f12e2126da3b43cd09dfc8.tar.gz emacs-10b58633b566cf8f66f12e2126da3b43cd09dfc8.zip | |
Improve ibuffer-diff-with-file
* lisp/ibuf-ext.el (ibuffer-diff-with-file): Link to diff-command in
docstring. Make Diff buffer read-only from outset and inhibit as
needed to avoid surprises. Check whether diff-command
supports --label. Leave point at BOB and clean up any excess
newline inserted by ibuffer-diff-buffer-with-file-1. Prefer
pop-to-buffer-same-window over switch-to-buffer.
(ibuffer-diff-buffer-with-file-1): Add docstring. Remove unused
unwind-protect and copypasta from diff-no-select (bug#62599). Use
diff-file-local-copy, string-join, and redisplay in place of
analogues. Condition --label use on availability, and label buffers
consistently with diff-no-select. Leave empty line between runs.
Let diff-sentinel delete temporary files. Leave point at EOB for
next run.
| -rw-r--r-- | lisp/ibuf-ext.el | 93 |
1 files changed, 46 insertions, 47 deletions
diff --git a/lisp/ibuf-ext.el b/lisp/ibuf-ext.el index ed4c8a04db7..550b5ed0e6a 100644 --- a/lisp/ibuf-ext.el +++ b/lisp/ibuf-ext.el | |||
| @@ -1650,68 +1650,67 @@ a prefix argument reverses the meaning of that variable." | |||
| 1650 | (error "No buffer with name %s" name) | 1650 | (error "No buffer with name %s" name) |
| 1651 | (goto-char buf-point))))) | 1651 | (goto-char buf-point))))) |
| 1652 | 1652 | ||
| 1653 | (declare-function diff-check-labels "diff" (&optional force)) | ||
| 1654 | (declare-function diff-file-local-copy "diff" (file-or-buf)) | ||
| 1653 | (declare-function diff-sentinel "diff" | 1655 | (declare-function diff-sentinel "diff" |
| 1654 | (code &optional old-temp-file new-temp-file)) | 1656 | (code &optional old-temp-file new-temp-file)) |
| 1655 | 1657 | ||
| 1656 | (defun ibuffer-diff-buffer-with-file-1 (buffer) | 1658 | (defun ibuffer-diff-buffer-with-file-1 (buffer) |
| 1657 | (let ((bufferfile (buffer-local-value 'buffer-file-name buffer)) | 1659 | "Compare BUFFER with its associated file, if any. |
| 1658 | (tempfile (make-temp-file "buffer-content-"))) | 1660 | Unlike `diff-no-select', insert output into current buffer |
| 1659 | (when bufferfile | 1661 | without erasing it." |
| 1660 | (unwind-protect | 1662 | (when-let ((old (buffer-file-name buffer))) |
| 1661 | (progn | 1663 | (defvar diff-use-labels) |
| 1662 | (with-current-buffer buffer | 1664 | (let* ((new buffer) |
| 1663 | (write-region nil nil tempfile nil 'nomessage)) | 1665 | (oldtmp (diff-file-local-copy old)) |
| 1664 | (let* ((old (expand-file-name bufferfile)) | 1666 | (newtmp (diff-file-local-copy new)) |
| 1665 | (new (expand-file-name tempfile)) | 1667 | (switches diff-switches) |
| 1666 | (oldtmp (file-local-copy old)) | 1668 | (command |
| 1667 | (newtmp (file-local-copy new)) | 1669 | (string-join |
| 1668 | (switches diff-switches) | 1670 | `(,diff-command |
| 1669 | (command | 1671 | ,@(if (listp switches) switches (list switches)) |
| 1670 | (mapconcat | 1672 | ,@(and (eq diff-use-labels t) |
| 1671 | 'identity | 1673 | (list "--label" (shell-quote-argument old) |
| 1672 | `(,diff-command | 1674 | "--label" (shell-quote-argument (format "%S" new)))) |
| 1673 | ;; Use explicitly specified switches | 1675 | ,(shell-quote-argument (or oldtmp old)) |
| 1674 | ,@(if (listp switches) switches (list switches)) | 1676 | ,(shell-quote-argument (or newtmp new))) |
| 1675 | ,@(if (or old new) | 1677 | " ")) |
| 1676 | (list "-L" (shell-quote-argument old) | 1678 | (inhibit-read-only t)) |
| 1677 | "-L" (shell-quote-argument | 1679 | (insert ?\n command ?\n) |
| 1678 | (format "Buffer %s" (buffer-name buffer))))) | 1680 | (diff-sentinel (call-process shell-file-name nil t nil |
| 1679 | ,(shell-quote-argument (or oldtmp old)) | 1681 | shell-command-switch command) |
| 1680 | ,(shell-quote-argument (or newtmp new))) | 1682 | oldtmp newtmp) |
| 1681 | " "))) | 1683 | (goto-char (point-max))) |
| 1682 | (let ((inhibit-read-only t)) | 1684 | (redisplay))) |
| 1683 | (insert command "\n") | ||
| 1684 | (diff-sentinel | ||
| 1685 | (call-process shell-file-name nil | ||
| 1686 | (current-buffer) nil | ||
| 1687 | shell-command-switch command)) | ||
| 1688 | (insert "\n"))))) | ||
| 1689 | (sit-for 0) | ||
| 1690 | (when (file-exists-p tempfile) | ||
| 1691 | (delete-file tempfile))))) | ||
| 1692 | 1685 | ||
| 1693 | ;;;###autoload | 1686 | ;;;###autoload |
| 1694 | (defun ibuffer-diff-with-file () | 1687 | (defun ibuffer-diff-with-file () |
| 1695 | "View the differences between marked buffers and their associated files. | 1688 | "View the differences between marked buffers and their associated files. |
| 1696 | If no buffers are marked, use buffer at point. | 1689 | If no buffers are marked, use buffer at point. |
| 1697 | This requires the external program \"diff\" to be in your `exec-path'." | 1690 | This requires the external program `diff-command' to be in your |
| 1691 | `exec-path'." | ||
| 1698 | (interactive) | 1692 | (interactive) |
| 1699 | (require 'diff) | 1693 | (require 'diff) |
| 1700 | (let ((marked-bufs (ibuffer-get-marked-buffers))) | 1694 | (let ((marked-bufs (or (ibuffer-get-marked-buffers) |
| 1701 | (when (null marked-bufs) | 1695 | (list (ibuffer-current-buffer t)))) |
| 1702 | (setq marked-bufs (list (ibuffer-current-buffer t)))) | 1696 | (diff-buf (get-buffer-create "*Ibuffer Diff*"))) |
| 1703 | (with-current-buffer (get-buffer-create "*Ibuffer Diff*") | 1697 | (with-current-buffer diff-buf |
| 1704 | (setq buffer-read-only nil) | 1698 | (setq buffer-read-only t) |
| 1705 | (buffer-disable-undo (current-buffer)) | 1699 | (buffer-disable-undo) |
| 1706 | (erase-buffer) | 1700 | (let ((inhibit-read-only t)) |
| 1707 | (buffer-enable-undo (current-buffer)) | 1701 | (erase-buffer)) |
| 1702 | (buffer-enable-undo) | ||
| 1708 | (diff-mode) | 1703 | (diff-mode) |
| 1704 | (diff-check-labels) | ||
| 1709 | (dolist (buf marked-bufs) | 1705 | (dolist (buf marked-bufs) |
| 1710 | (unless (buffer-live-p buf) | 1706 | (unless (buffer-live-p buf) |
| 1711 | (error "Buffer %s has been killed" buf)) | 1707 | (error "Buffer %s has been killed" buf)) |
| 1712 | (ibuffer-diff-buffer-with-file-1 buf)) | 1708 | (ibuffer-diff-buffer-with-file-1 buf)) |
| 1713 | (setq buffer-read-only t))) | 1709 | (goto-char (point-min)) |
| 1714 | (switch-to-buffer "*Ibuffer Diff*")) | 1710 | (when (= (following-char) ?\n) |
| 1711 | (let ((inhibit-read-only t)) | ||
| 1712 | (delete-char 1)))) | ||
| 1713 | (pop-to-buffer-same-window diff-buf))) | ||
| 1715 | 1714 | ||
| 1716 | ;;;###autoload | 1715 | ;;;###autoload |
| 1717 | (defun ibuffer-copy-filename-as-kill (&optional arg) | 1716 | (defun ibuffer-copy-filename-as-kill (&optional arg) |