git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH v2 2/3] git-blame.el: Use with-current-buffer where appropriate

From
Lawrence Mitchell <wence@gmx.li>
Date
Jun 14, 2012, 09:37 UTC
Message-ID
<1339666680-4381-2-git-send-email-wence@gmx.li>
In-Reply-To
<1339666680-4381-1-git-send-email-wence@gmx.li>

In git-blame-filter and git-blame-create-overlay we want to save (along with the values of point and mark) the current-buffer in scope when calling the functions. The idiom

    (save-excursion
      (set-buffer buf)
      ...)

will correctly restore the correct buffer, but will not save the values of point and mark in buf (only in the buffer current when the save-excursion call is executed). The intention of these functions is to save the current buffer from the calling scope and the values of point and mark in the buffer they are modifying. The correct idiom for this is

    (with-current-buffer buf
      (save-excursion
        ...))
Signed-off-by: Rüdiger Sonderfeld <ruediger@c-plusplus.de>
Signed-off-by: Lawrence Mitchell <wence@gmx.li>
---
 contrib/emacs/git-blame.el | 74 +++++++++++++++++++++++-----------------------
 1 file changed, 37 insertions(+), 37 deletions(-)

Updated commit message that actually correctly matches what Emacs does, plus don't just squash the byte-compiler warnings but actually fix the bug that it was pointing out to us.

For reference, here's the whitespace-squashed change:
| @@ -337,8 +337,8 @@ See also function `git-blame-mode'."
|  (defvar in-blame-filter nil)
 
|  (defun git-blame-filter (proc str)
| +  (with-current-buffer (process-buffer proc)
|      (save-excursion
| -    (set-buffer (process-buffer proc))
|        (goto-char (process-mark proc))
|        (insert-before-markers str)
|        (goto-char 0)
| @@ -346,7 +346,7 @@ See also function `git-blame-mode'."
|          (let ((more t)
|                (in-blame-filter t))
|            (while more
| -          (setq more (git-blame-parse)))))))
| +            (setq more (git-blame-parse))))))))
 
|  (defun git-blame-parse ()
|    (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
| @@ -385,8 +385,8 @@ See also function `git-blame-mode'."
|            info))))
 
|  (defun git-blame-create-overlay (info start-line num-lines)
| +  (with-current-buffer git-blame-file
|      (save-excursion
| -    (set-buffer git-blame-file)
|        (let ((inhibit-point-motion-hooks t)
|              (inhibit-modification-hooks t))
|          (goto-char (point-min))
| @@ -411,7 +411,7 @@ See also function `git-blame-mode'."
|                                             (cdr (assq 'color (cdr info))))))
|            (overlay-put ovl 'line-prefix
|                         (propertize (format-spec git-blame-prefix-format spec)
| -                                 'face 'git-blame-prefix-face))))))
| +                                   'face 'git-blame-prefix-face)))))))
 
diff --git a/contrib/emacs/git-blame.el b/contrib/emacs/git-blame.el
index 5428ff7..bb6d7bb 100644
--- a/contrib/emacs/git-blame.el
+++ b/contrib/emacs/git-blame.el
@@ -337,16 +337,16 @@ See also function `git-blame-mode'."
 (defvar in-blame-filter nil)
 
 (defun git-blame-filter (proc str)
-  (save-excursion
-    (set-buffer (process-buffer proc))
-    (goto-char (process-mark proc))
-    (insert-before-markers str)
-    (goto-char 0)
-    (unless in-blame-filter
-      (let ((more t)
-            (in-blame-filter t))
-        (while more
-          (setq more (git-blame-parse)))))))
+  (with-current-buffer (process-buffer proc)
+    (save-excursion
+      (goto-char (process-mark proc))
+      (insert-before-markers str)
+      (goto-char 0)
+      (unless in-blame-filter
+        (let ((more t)
+              (in-blame-filter t))
+          (while more
+            (setq more (git-blame-parse))))))))
 
 (defun git-blame-parse ()
   (cond ((looking-at "\\([0-9a-f]\\{40\\}\\) \\([0-9]+\\) \\([0-9]+\\) \\([0-9]+\\)\n")
@@ -385,33 +385,33 @@ See also function `git-blame-mode'."
           info))))
 
 (defun git-blame-create-overlay (info start-line num-lines)
-  (save-excursion
-    (set-buffer git-blame-file)
-    (let ((inhibit-point-motion-hooks t)
-          (inhibit-modification-hooks t))
-      (goto-char (point-min))
-      (forward-line (1- start-line))
-      (let* ((start (point))
-             (end (progn (forward-line num-lines) (point)))
-             (ovl (make-overlay start end))
-             (hash (car info))
-             (spec `((?h . ,(substring hash 0 6))
-                     (?H . ,hash)
-                     (?a . ,(git-blame-get-info info 'author))
-                     (?A . ,(git-blame-get-info info 'author-mail))
-                     (?c . ,(git-blame-get-info info 'committer))
-                     (?C . ,(git-blame-get-info info 'committer-mail))
-                     (?s . ,(git-blame-get-info info 'summary)))))
-        (push ovl git-blame-overlays)
-        (overlay-put ovl 'git-blame info)
-        (overlay-put ovl 'help-echo
-                     (format-spec git-blame-mouseover-format spec))
-        (if git-blame-use-colors
-            (overlay-put ovl 'face (list :background
-                                         (cdr (assq 'color (cdr info))))))
-        (overlay-put ovl 'line-prefix
-                     (propertize (format-spec git-blame-prefix-format spec)
-                                 'face 'git-blame-prefix-face))))))
+  (with-current-buffer git-blame-file
+    (save-excursion
+      (let ((inhibit-point-motion-hooks t)
+            (inhibit-modification-hooks t))
+        (goto-char (point-min))
+        (forward-line (1- start-line))
+        (let* ((start (point))
+               (end (progn (forward-line num-lines) (point)))
+               (ovl (make-overlay start end))
+               (hash (car info))
+               (spec `((?h . ,(substring hash 0 6))
+                       (?H . ,hash)
+                       (?a . ,(git-blame-get-info info 'author))
+                       (?A . ,(git-blame-get-info info 'author-mail))
+                       (?c . ,(git-blame-get-info info 'committer))
+                       (?C . ,(git-blame-get-info info 'committer-mail))
+                       (?s . ,(git-blame-get-info info 'summary)))))
+          (push ovl git-blame-overlays)
+          (overlay-put ovl 'git-blame info)
+          (overlay-put ovl 'help-echo
+                       (format-spec git-blame-mouseover-format spec))
+          (if git-blame-use-colors
+              (overlay-put ovl 'face (list :background
+                                           (cdr (assq 'color (cdr info))))))
+          (overlay-put ovl 'line-prefix
+                       (propertize (format-spec git-blame-prefix-format spec)
+                                   'face 'git-blame-prefix-face)))))))
 
 (defun git-blame-add-info (info key value)
   (nconc info (list (cons (intern key) value))))
-- 
1.7.11.rc2.9.g10afb6c
Previous: Lawrence MitchellNext: Lawrence Mitchell
Message 17 of 18 in “git-blame.el: Fix compilation warnings.”
  1. git-blame.el: Fix compilation warnings.Rüdiger Sonderfeld, Jan 12, 2012
  2. Jonathan NiederJan 12, 2012
  3. Rüdiger SonderfeldJan 12, 2012
  4. Sending patches with KMail (Re: [PATCH] git-blame.el: Fix compilation warnings.)Jonathan Nieder, Jan 13, 2012
  5. Junio C HamanoJan 14, 2012
  6. Jonathan NiederJan 14, 2012
  7. Jonathan NiederJan 14, 2012
  8. Junio C HamanoJan 15, 2012
  9. Rüdiger SonderfeldJan 14, 2012
  10. git-blame.el: use mapc instead of mapcarJonathan Nieder, Jun 10, 2012
  11. 1/3 git-blame.el: Do not use goto-line in lisp codeLawrence Mitchell, Jun 10, 2012
  12. 2/3 git-blame.el: Use with-current-buffer where appropriateLawrence Mitchell, Jun 10, 2012
  13. 3/3 git-blame.el: Do not use bare 0 to mean (point-min)Lawrence Mitchell, Jun 10, 2012
  14. Jonathan NiederJun 14, 2012
  15. Lawrence MitchellJun 14, 2012
  16. 1/3 git-blame.el: Do not use goto-line in lisp codeLawrence Mitchell, Jun 14, 2012
  17. 2/3 git-blame.el: Use with-current-buffer where appropriateLawrence Mitchell, Jun 14, 2012
  18. 3/3 git-blame.el: Do not use bare 0 to mean (point-min)Lawrence Mitchell, Jun 14, 2012

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.