From: Alan Mackenzie <acm@muc.de>
To: emacs-devel@gnu.org
Subject: Re: Edebug corrupting point in buffers; we need buffer-point and set-buffer-point, perhaps.
Date: Mon, 31 Oct 2022 21:25:08 +0000 [thread overview]
Message-ID: <Y2A9NINYSfByTztE@ACM> (raw)
In-Reply-To: <Y1+00x9hKKFpAVO6@ACM>
Hello, Emacs.
On Mon, Oct 31, 2022 at 11:43:15 +0000, Alan Mackenzie wrote:
> A few weeks ago, I was attempting to edebug a program which itself
> scanned through a buffer B. That buffer was also displayed in a window
> (or possibly two windows). Each time the program went into edebug, the
> point in B got corrupted.
> Setting edebug-save-displayed-buffer-points didn't help.
> I now understand what was going wrong. Without
> edebug-save-displayed-buffer-points, B's point got set to a window point
> from a random window displaying B, caused by a set-window-configuration
> call from edebug.
> With edebug-save-displayed-buffer-points set, the function
> edebug-get-displayed-buffer points was recording window-points for each
> displayed window, but after the recursive edit was writing these
> window-points back into the buffer points. This is surely a bug. At the
> least, there is a race condition if two windows display the same buffer.
> In neither of the above scenarios does edebug do anything to restore the
> buffer points after the recursive edit. This seems to be a bad thing.
> I propose that edebug should get an option to preserve buffer points,
> alongside the existing options which preserve window points.
[ .... ]
I now have a patch which aims to fix the above problem. To use it,
select a window containing the buffer the function under test will be
scanning (or otherwise manipulating). Type C-x X F to add that buffer's
name to edebug-save-buffer-points.
During the current or future edebug session, this buffer will now be
protected from having its buffer point corrupted by edebug's other
mechanisms which manipulate window-points.
In fact, I'm not sure that edebug-save-displayed-buffer-points isn't
obsolete - it dates from 1994 (or earlier), a time before there were
frames or window configurations. It seems to me its function is covered
by the later edebug-save-windows.
Anyhow, here's the patch. What do you think?
diff --git a/lisp/emacs-lisp/edebug.el b/lisp/emacs-lisp/edebug.el
index 67704bdb51..c4099164bc 100644
--- a/lisp/emacs-lisp/edebug.el
+++ b/lisp/emacs-lisp/edebug.el
@@ -157,6 +157,23 @@ edebug-save-displayed-buffer-points
need it."
:type 'boolean)
+(defcustom edebug-save-buffer-points nil
+ "If non-nil save and restore the buffer points in some buffers.
+
+Saving and restoring the buffer point in a buffer is needed if you
+are debugging code which sets point in that buffer, particularly if
+there is also a window displaying that buffer. Otherwise the buffer
+point (being used by the program) will get overwritten by the
+window point.
+
+If the value is a list of buffer names (recommended), only those
+buffers will have their buffer points restored. Otherwise, t means
+restore all buffers\\=' points, and nil means none.
+
+A buffer\\='s name can be added to or removed from this list
+dynamically by the key binding \\[edebug-toggle-save-buffer-points]."
+ :type '(choice boolean (repeat string)))
+
(defcustom edebug-initial-mode 'step
"Initial execution mode for Edebug, if non-nil.
If this variable is non-nil, it specifies the initial execution mode
@@ -401,6 +420,32 @@ edebug-set-buffer-points
(goto-char (cdr buf-point))))
buffer-points)))
+(defun edebug-get-all-buffer-points ()
+ ;; Return an alist of (BUFFER . BUFFER-POINT) for all buffers.
+ (let (bp-list)
+ (dolist (buf (buffer-list))
+ (with-current-buffer buf
+ (push (cons buf (point)) bp-list)))
+ bp-list))
+
+(defun edebug-restore-buffer-points (buffer-points)
+ ;; Restore (some) buffer-points from the argument, an alist with
+ ;; elements of the form (BUFFER . BUFFER-POINT).
+ (cond
+ ((eq edebug-save-buffer-points t)
+ (dolist (elt buffer-points)
+ (unless (eq (car elt) (current-buffer))
+ (with-current-buffer (car elt)
+ (goto-char (cdr elt))))))
+ ((listp edebug-save-buffer-points)
+ (dolist (buf edebug-save-buffer-points)
+ (let ((elt (assq buf buffer-points)))
+ (when elt ; ?Always non-nil
+ (unless (eq (car elt) (current-buffer))
+ (with-current-buffer (car elt)
+ (goto-char (cdr elt))))))))
+ (t nil)))
+
(defun edebug-current-windows (which-windows)
;; Get either a full window configuration or some window information.
(if (listp which-windows)
@@ -2600,6 +2647,8 @@ edebug--display-1
(edebug-outside-mark (mark t))
edebug-outside-windows ; Window or screen configuration.
edebug-buffer-points
+ edebug-all-buffer-points
+
edebug-eval-buffer ; Declared here so we can kill it below.
(eval-result-list (and edebug-eval-list
@@ -2627,7 +2678,9 @@ edebug--display-1
(setq edebug-outside-windows
(edebug-current-windows edebug-save-windows)))
- (if edebug-save-displayed-buffer-points
+ (setq edebug-all-buffer-points (edebug-get-all-buffer-points))
+
+ (if edebug-save-displayed-buffer-points
(setq edebug-buffer-points (edebug-get-displayed-buffer-points)))
;; First move the edebug buffer point to edebug-point
@@ -2759,6 +2814,11 @@ edebug--display-1
(if edebug-save-displayed-buffer-points
(edebug-set-buffer-points edebug-buffer-points))
+ ;; Restore (non-displayed) buffer points.
+ (if edebug-save-buffer-points
+ (edebug-restore-buffer-points
+ edebug-all-buffer-points))
+
;; Unrestore trace window's window-point.
(if edebug-trace-window
(set-window-start edebug-trace-window
@@ -3013,6 +3075,26 @@ edebug-toggle-save-windows
(edebug-toggle-save-selected-window)
(edebug-toggle-save-all-windows)))
+(defun edebug-toggle-save-buffer-points (arg)
+ "Toggle the saving and restoring of a buffer\\='s buffer point.
+That buffer is the one in the selected window. With a prefix
+argument, switch the setting on or off for all buffers."
+ (interactive "P")
+ (let* ((cur-win-buf (window-buffer (selected-window)))
+ (win-buf-name (buffer-name cur-win-buf)))
+ (if arg
+ (setq edebug-save-buffer-points (not edebug-save-buffer-points))
+ (cond
+ ((eq t edebug-save-buffer-points)
+ ;; Save all buffers except the one in the current window.
+ (setq edebug-save-buffer-points
+ (mapcar #'buffer-name
+ (delq cur-win-buf (buffer-list)))))
+ ((memq win-buf-name edebug-save-buffer-points)
+ (setq edebug-save-buffer-points
+ (delq (buffer-name cur-win-buf) edebug-save-buffer-points)))
+ (t (push win-buf-name edebug-save-buffer-points))))))
+
(defun edebug-where ()
"Show the debug windows and where we stopped in the program."
(interactive)
@@ -3850,6 +3934,7 @@ edebug-mode-map
"p" #'edebug-bounce-point
"P" #'edebug-view-outside ; same as v
"W" #'edebug-toggle-save-windows
+ ;; "F" #'edebug-toggle-save-buffer-points
;; misc
"?" #'edebug-help
@@ -3904,6 +3991,7 @@ edebug-global-map
;; views
"w" #'edebug-where
"W" #'edebug-toggle-save-windows
+ "F" #'edebug-toggle-save-buffer-points
;; quitting
"q" #'top-level
> --
> Alan Mackenzie (Nuremberg, Germany).
next prev parent reply other threads:[~2022-10-31 21:25 UTC|newest]
Thread overview: 62+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-31 11:43 Edebug corrupting point in buffers; we need buffer-point and set-buffer-point, perhaps Alan Mackenzie
2022-10-31 13:16 ` Eli Zaretskii
2022-10-31 14:32 ` Alan Mackenzie
2022-10-31 14:50 ` Eli Zaretskii
2022-10-31 15:46 ` Alan Mackenzie
2022-10-31 17:33 ` Stefan Monnier
2022-10-31 17:55 ` Eli Zaretskii
2022-10-31 20:46 ` Alan Mackenzie
2022-11-01 6:21 ` Eli Zaretskii
2022-10-31 17:19 ` Stefan Monnier
2022-10-31 18:09 ` Eli Zaretskii
2022-10-31 20:35 ` Stefan Monnier
2022-10-31 17:21 ` Stefan Monnier
2022-10-31 18:10 ` Eli Zaretskii
2022-10-31 23:14 ` Stefan Monnier
2022-11-01 7:06 ` Eli Zaretskii
2022-10-31 21:25 ` Alan Mackenzie [this message]
2022-11-01 6:45 ` Eli Zaretskii
2022-11-01 11:41 ` Edebug corrupting point in buffers Alan Mackenzie
2022-11-01 11:53 ` Eli Zaretskii
2022-11-01 13:42 ` Alan Mackenzie
2022-11-01 14:42 ` Eli Zaretskii
2022-11-01 17:06 ` Alan Mackenzie
2022-11-01 17:12 ` Eli Zaretskii
2022-11-01 17:24 ` Alan Mackenzie
2022-11-01 17:57 ` Eli Zaretskii
2022-11-01 19:02 ` Alan Mackenzie
2022-11-01 19:47 ` Stefan Monnier
2022-11-01 20:53 ` Alan Mackenzie
2022-11-01 21:51 ` Stefan Monnier
2022-11-02 10:40 ` Alan Mackenzie
2022-11-02 13:12 ` Stefan Monnier
2022-11-02 13:28 ` Eli Zaretskii
2022-11-02 3:28 ` Eli Zaretskii
2022-11-02 12:53 ` Stefan Monnier
2022-11-02 17:40 ` Juri Linkov
2022-11-02 18:26 ` Eli Zaretskii
2022-11-02 18:36 ` Juri Linkov
2022-11-02 18:52 ` Eli Zaretskii
2022-11-03 17:25 ` Juri Linkov
2022-11-03 18:06 ` Eli Zaretskii
2022-11-03 18:31 ` Juri Linkov
2022-11-02 11:34 ` Alan Mackenzie
2022-11-02 14:00 ` Eli Zaretskii
2022-11-02 16:18 ` Alan Mackenzie
2022-11-02 16:57 ` Eli Zaretskii
2022-11-03 11:32 ` Alan Mackenzie
2022-11-03 13:29 ` Eli Zaretskii
2022-11-03 18:07 ` Alan Mackenzie
2022-11-03 18:15 ` Eli Zaretskii
2022-11-03 20:25 ` Alan Mackenzie
2022-11-05 11:24 ` Eli Zaretskii
2022-11-05 16:50 ` Alan Mackenzie
2022-11-06 8:10 ` Eli Zaretskii
2022-11-06 14:40 ` Alan Mackenzie
2022-11-03 19:29 ` Stefan Monnier
2022-11-03 19:36 ` Eli Zaretskii
2022-11-03 20:39 ` Stefan Monnier
2022-11-04 6:34 ` Eli Zaretskii
2022-11-04 6:37 ` Eli Zaretskii
2022-11-03 19:57 ` Alan Mackenzie
2022-11-03 20:35 ` Stefan Monnier
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y2A9NINYSfByTztE@ACM \
--to=acm@muc.de \
--cc=emacs-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).