unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: martin rudalics <rudalics@gmx.at>
To: Eli Zaretskii <eliz@gnu.org>
Cc: 34765@debbugs.gnu.org, alexanderm@web.de, monnier@IRO.UMontreal.CA
Subject: bug#34765: 26.1; with-temp-buffer should not run buffer-list-update-hook
Date: Fri, 26 Apr 2019 13:00:06 +0200	[thread overview]
Message-ID: <49c11920-0909-dcc2-4a39-4cdcfaf20453@gmx.at> (raw)
In-Reply-To: <838svxxk41.fsf@gnu.org>

[-- Attachment #1: Type: text/plain, Size: 462 bytes --]

>>   > How 'bout adding an optional argument to `generate-new-buffer` to set
>>   > `inhibit_buffer_hooks`?
>>
>> How about moving 'generate-new-buffer' to C to set that flag without
>> exposing it to Lisp (and to avoid things like
>>
>>               ;; We can't use `generate-new-buffer' because files.el
>>               ;; is not yet loaded.
>>
>> in 'load-with-code-conversion')?
>
> Fine with me.

I attach a preliminary patch.

martin



[-- Attachment #2: inhibit-buffer-hooks.diff --]
[-- Type: text/plain, Size: 11521 bytes --]

diff --git a/lisp/files.el b/lisp/files.el
index c05d70a00e..d919793251 100644
--- a/lisp/files.el
+++ b/lisp/files.el
@@ -1777,6 +1777,9 @@ kill-buffer-hook
 The buffer being killed is current while the hook is running.
 See `kill-buffer'.
 
+This hook is not run for buffers created by 'generate-new-buffer'
+with the second argument 'inhibit-buffer-hooks' non-nil.
+
 Note: Be careful with let-binding this hook considering it is
 frequently used for cleanup.")
 
@@ -1880,11 +1883,6 @@ create-file-buffer
 			     (concat "|" lastname)
 			   lastname))))
 
-(defun generate-new-buffer (name)
-  "Create and return a buffer with a name based on NAME.
-Choose the buffer's name using `generate-new-buffer-name'."
-  (get-buffer-create (generate-new-buffer-name name)))
-
 (defcustom automount-dir-prefix (purecopy "^/tmp_mnt/")
   "Regexp to match the automounter prefix in a directory name."
   :group 'files
diff --git a/lisp/international/mule.el b/lisp/international/mule.el
index ba30fee496..d168ee9a16 100644
--- a/lisp/international/mule.el
+++ b/lisp/international/mule.el
@@ -306,10 +306,7 @@ load-with-code-conversion
       (and (null noerror)
 	   (signal 'file-error (list "Cannot open load file" file)))
     ;; Read file with code conversion, and then eval.
-    (let* ((buffer
-            ;; We can't use `generate-new-buffer' because files.el
-            ;; is not yet loaded.
-            (get-buffer-create (generate-new-buffer-name " *load*")))
+    (let* ((buffer (generate-new-buffer " *load*"))
 	   (load-in-progress t)
 	   (source (save-match-data (string-match "\\.el\\'" fullname))))
       (unless nomessage
diff --git a/lisp/subr.el b/lisp/subr.el
index f68f9dd419..5f2c43c78b 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3530,8 +3530,7 @@ with-temp-file
   (let ((temp-file (make-symbol "temp-file"))
 	(temp-buffer (make-symbol "temp-buffer")))
     `(let ((,temp-file ,file)
-	   (,temp-buffer
-	    (get-buffer-create (generate-new-buffer-name " *temp file*"))))
+	   (,temp-buffer (generate-new-buffer " *temp file*" t)))
        (unwind-protect
 	   (prog1
 	       (with-current-buffer ,temp-buffer
@@ -3569,7 +3568,7 @@ with-temp-buffer
 See also `with-temp-file' and `with-output-to-string'."
   (declare (indent 0) (debug t))
   (let ((temp-buffer (make-symbol "temp-buffer")))
-    `(let ((,temp-buffer (generate-new-buffer " *temp*")))
+    `(let ((,temp-buffer (generate-new-buffer " *temp*" t)))
        ;; FIXME: kill-buffer can change current-buffer in some odd cases.
        (with-current-buffer ,temp-buffer
          (unwind-protect
@@ -3604,8 +3603,7 @@ with-silent-modifications
 (defmacro with-output-to-string (&rest body)
   "Execute BODY, return the text it sent to `standard-output', as a string."
   (declare (indent 0) (debug t))
-  `(let ((standard-output
-	  (get-buffer-create (generate-new-buffer-name " *string-output*"))))
+  `(let ((standard-output (generate-new-buffer " *string-output*" t)))
      (unwind-protect
 	 (progn
 	   (let ((standard-output standard-output))
diff --git a/src/buffer.c b/src/buffer.c
index ab47748191..3e135148fb 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -500,19 +500,43 @@ get_truename_buffer (register Lisp_Object filename)
   return Qnil;
 }
 
-DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
-       doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
-If BUFFER-OR-NAME is a string and a live buffer with that name exists,
-return that buffer.  If no such buffer exists, create a new buffer with
-that name and return it.  If BUFFER-OR-NAME starts with a space, the new
-buffer does not keep undo information.
 
-If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
-even if it is dead.  The return value is never nil.  */)
-  (register Lisp_Object buffer_or_name)
+/**
+ * run_buffer_list_update_hook:
+ *
+ * Run any functions on 'buffer-list-update-hook'.  Do not run the
+ * functions when BUFFER is a buffer and its inhibit_buffer_hooks
+ * structure element is set.  Do not run any functions either when we
+ * are not allowed to run hooks.
+ */
+static void
+run_buffer_list_update_hook (Lisp_Object buffer)
 {
-  register Lisp_Object buffer, name;
-  register struct buffer *b;
+  if (!NILP (Vrun_hooks)
+      && (!BUFFERP (buffer) || !XBUFFER (buffer)->inhibit_buffer_hooks))
+    {
+      ptrdiff_t count = SPECPDL_INDEX ();
+
+      record_unwind_current_buffer ();
+      call1 (Vrun_hooks, Qbuffer_list_update_hook);
+      unbind_to (count, Qnil);
+    }
+}
+
+/**
+ * get_buffer_create:
+ *
+ * Return buffer specified by BUFFER-OR-NAME, creating a new one if
+ * needed.  See Fget_buffer_create below for more information.
+ *
+ * Second argument INHIBIT_BUFFER_HOOKS true means to not run
+ * 'buffer-list-update-hook'.
+ */
+static Lisp_Object
+get_buffer_create (Lisp_Object buffer_or_name, bool inhibit_buffer_hooks)
+{
+  Lisp_Object buffer, name;
+  struct buffer *b;
 
   buffer = Fget_buffer (buffer_or_name);
   if (!NILP (buffer))
@@ -600,14 +624,29 @@ even if it is dead.  The return value is never nil.  */)
   /* Put this in the alist of all live buffers.  */
   XSETBUFFER (buffer, b);
   Vbuffer_alist = nconc2 (Vbuffer_alist, list1 (Fcons (name, buffer)));
-  /* And run buffer-list-update-hook.  */
-  if (!NILP (Vrun_hooks) && !b->inhibit_buffer_hooks)
-    call1 (Vrun_hooks, Qbuffer_list_update_hook);
+  if (!inhibit_buffer_hooks)
+    /* Run buffer-list-update-hook.  */
+    run_buffer_list_update_hook (buffer);
 
   return buffer;
 }
 
 
+DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
+       doc: /* Return the buffer specified by BUFFER-OR-NAME, creating a new one if needed.
+If BUFFER-OR-NAME is a string and a live buffer with that name exists,
+return that buffer.  If no such buffer exists, create a new buffer with
+that name and return it.  If BUFFER-OR-NAME starts with a space, the new
+buffer does not keep undo information.
+
+If BUFFER-OR-NAME is a buffer instead of a string, return it as given,
+even if it is dead.  The return value is never nil.  */)
+     (Lisp_Object buffer_or_name)
+{
+  return get_buffer_create (buffer_or_name, false);
+}
+
+
 /* Return a list of overlays which is a copy of the overlay list
    LIST, but for buffer B.  */
 
@@ -871,8 +910,7 @@ CLONE nil means the indirect buffer's state is reset to default values.  */)
     }
 
   /* Run buffer-list-update-hook.  */
-  if (!NILP (Vrun_hooks))
-    call1 (Vrun_hooks, Qbuffer_list_update_hook);
+  run_buffer_list_update_hook (Qnil);
 
   return buf;
 }
@@ -1135,6 +1173,31 @@ is first appended to NAME, to speed up finding a non-existent buffer.  */)
     }
 }
 
+DEFUN ("generate-new-buffer", Fgenerate_new_buffer, Sgenerate_new_buffer,
+       1, 2, 0,
+       doc: /* Create and return a buffer with a name based on NAME.
+Choose the buffer's name using `generate-new-buffer-name'.
+
+Optional second argument INHIBIT-BUFFER-HOOKS non-nil means to not run
+any buffer hooks ('kill-buffer-hook', 'buffer-list-update-hook' or
+'kill-buffer-query-functions') for this buffer.  This argument should
+be set only for internal buffers that are never presented to users or
+passed on to other applications.  */)
+  (Lisp_Object name, Lisp_Object inhibit_buffer_hooks)
+{
+  Lisp_Object buffer_name = Fgenerate_new_buffer_name (name, Qnil);
+  Lisp_Object buffer = get_buffer_create (buffer_name,
+					  !NILP (inhibit_buffer_hooks));
+
+  if (!NILP (inhibit_buffer_hooks))
+    {
+     struct buffer *b = XBUFFER (buffer);
+
+     b->inhibit_buffer_hooks = true;
+    }
+
+  return buffer;
+}
 \f
 DEFUN ("buffer-name", Fbuffer_name, Sbuffer_name, 0, 1, 0,
        doc: /* Return the name of BUFFER, as a string.
@@ -1499,8 +1562,7 @@ This does not change the name of the visited file (if any).  */)
     call0 (intern ("rename-auto-save-file"));
 
   /* Run buffer-list-update-hook.  */
-  if (!NILP (Vrun_hooks) && !current_buffer->inhibit_buffer_hooks)
-    call1 (Vrun_hooks, Qbuffer_list_update_hook);
+  run_buffer_list_update_hook (buf);
 
   /* Refetch since that last call may have done GC.  */
   return BVAR (current_buffer, name);
@@ -1677,6 +1739,10 @@ buffer is actually killed.  The buffer being killed will be current
 while the hook is running.  Functions called by any of these hooks are
 supposed to not change the current buffer.
 
+Neither 'kill-buffer-query-functions' nor 'kill-buffer-hook' are run
+for buffers created by 'generate-new-buffer' with the second argument
+'inhibit-buffer-hooks' non-nil.
+
 Any processes that have this buffer as the `process-buffer' are killed
 with SIGHUP.  This function calls `replace-buffer-in-windows' for
 cleaning up all windows currently displaying the buffer to be killed. */)
@@ -1938,8 +2004,7 @@ cleaning up all windows currently displaying the buffer to be killed. */)
   bset_undo_list (b, Qnil);
 
   /* Run buffer-list-update-hook.  */
-  if (!NILP (Vrun_hooks) && !b->inhibit_buffer_hooks)
-    call1 (Vrun_hooks, Qbuffer_list_update_hook);
+  run_buffer_list_update_hook (buffer);
 
   return Qt;
 }
@@ -1980,8 +2045,7 @@ record_buffer (Lisp_Object buffer)
   fset_buried_buffer_list (f, Fdelq (buffer, f->buried_buffer_list));
 
   /* Run buffer-list-update-hook.  */
-  if (!NILP (Vrun_hooks) && !XBUFFER (buffer)->inhibit_buffer_hooks)
-    call1 (Vrun_hooks, Qbuffer_list_update_hook);
+  run_buffer_list_update_hook (buffer);
 }
 
 
@@ -2019,8 +2083,7 @@ DEFUN ("bury-buffer-internal", Fbury_buffer_internal, Sbury_buffer_internal,
     (f, Fcons (buffer, Fdelq (buffer, f->buried_buffer_list)));
 
   /* Run buffer-list-update-hook.  */
-  if (!NILP (Vrun_hooks) && !XBUFFER (buffer)->inhibit_buffer_hooks)
-    call1 (Vrun_hooks, Qbuffer_list_update_hook);
+  run_buffer_list_update_hook (buffer);
 
   return Qnil;
 }
@@ -6255,7 +6318,10 @@ Use Custom to set this variable and update the display.  */);
 The buffer being killed will be current while the functions are running.
 
 If any of them returns nil, the buffer is not killed.  Functions run by
-this hook are supposed to not change the current buffer.  */);
+this hook are supposed to not change the current buffer.
+
+This hook is not run for buffers created by 'generate-new-buffer' with
+the second argument 'inhibit-buffer-hooks' non-nil.  */);
   Vkill_buffer_query_functions = Qnil;
 
   DEFVAR_LISP ("change-major-mode-hook", Vchange_major_mode_hook,
@@ -6268,9 +6334,9 @@ The function `kill-all-local-variables' runs this before doing anything else.  *
 	       doc: /* Hook run when the buffer list changes.
 Functions (implicitly) running this hook are `get-buffer-create',
 `make-indirect-buffer', `rename-buffer', `kill-buffer', `bury-buffer'
-and `select-window'.  Functions run by this hook should avoid calling
-`select-window' with a nil NORECORD argument or `with-temp-buffer'
-since either may lead to infinite recursion.  */);
+and `select-window'.  This hook is not run for buffers created by
+'generate-new-buffer' with the second argument 'inhibit-buffer-hooks'
+non-nil.  */);
   Vbuffer_list_update_hook = Qnil;
   DEFSYM (Qbuffer_list_update_hook, "buffer-list-update-hook");
 
@@ -6281,6 +6347,7 @@ since either may lead to infinite recursion.  */);
   defsubr (&Sget_buffer_create);
   defsubr (&Smake_indirect_buffer);
   defsubr (&Sgenerate_new_buffer_name);
+  defsubr (&Sgenerate_new_buffer);
   defsubr (&Sbuffer_name);
   defsubr (&Sbuffer_file_name);
   defsubr (&Sbuffer_base_buffer);


  reply	other threads:[~2019-04-26 11:00 UTC|newest]

Thread overview: 72+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-05 22:57 bug#34765: 26.1; with-temp-buffer should not run buffer-list-update-hook Alexander Miller
2019-03-06  9:39 ` martin rudalics
2019-03-06 11:29 ` Alexander Miller
2019-03-06 14:13   ` martin rudalics
2019-03-06 15:41     ` Eli Zaretskii
2019-03-06 17:57       ` martin rudalics
2019-04-23  9:21         ` martin rudalics
2019-04-23 10:36           ` Eli Zaretskii
2019-04-24  7:27             ` martin rudalics
2019-04-24 11:12               ` Eli Zaretskii
2019-04-24 12:55                 ` Stefan Monnier
2019-04-25  8:06                 ` martin rudalics
2019-04-25  8:50                   ` Eli Zaretskii
2019-04-25 10:31                     ` martin rudalics
2019-04-25 10:49                       ` Eli Zaretskii
2019-04-26  7:40                         ` martin rudalics
2019-04-26  8:09                           ` Eli Zaretskii
2019-04-25 13:01           ` Stefan Monnier
2019-04-25 14:34             ` Eli Zaretskii
2019-04-26  7:41               ` martin rudalics
2019-04-26  8:10                 ` Eli Zaretskii
2019-04-26  7:41             ` martin rudalics
2019-04-26  8:10               ` Eli Zaretskii
2019-04-26 11:00                 ` martin rudalics [this message]
2019-04-26 11:26                   ` Eli Zaretskii
2019-04-27  8:30                     ` martin rudalics
2019-04-26 17:14                   ` Basil L. Contovounesios
2019-04-27  8:31                     ` martin rudalics
2019-05-20 13:42                       ` Basil L. Contovounesios
2019-05-21  7:32                         ` martin rudalics
2019-05-21  7:58                           ` Basil L. Contovounesios
2019-05-21 10:04                             ` martin rudalics
2019-05-22  7:29                               ` Eli Zaretskii
2019-05-22  8:32                                 ` martin rudalics
2019-05-22 10:06                                   ` Eli Zaretskii
2019-05-23  8:38                                     ` martin rudalics
2019-05-23 14:37                                       ` Eli Zaretskii
2019-05-24  8:01                                         ` martin rudalics
2019-05-22 14:12                                   ` Stefan Monnier
2019-05-22 15:50                                     ` Eli Zaretskii
2019-05-22  7:21                             ` Eli Zaretskii
2020-08-26 11:06                   ` Lars Ingebrigtsen
2020-09-05  7:05                     ` Eli Zaretskii
2020-10-07  3:27                       ` Lars Ingebrigtsen
2020-10-07  7:58                         ` martin rudalics
2020-11-29 21:03                           ` Basil L. Contovounesios
2020-11-30  9:05                             ` martin rudalics
2020-11-30 18:07                               ` Basil L. Contovounesios
2020-11-30 19:01                                 ` martin rudalics
2020-11-30 20:33                                   ` Basil L. Contovounesios
2020-12-01  9:34                                     ` martin rudalics
2020-11-30 19:42                                 ` Eli Zaretskii
2020-11-30 20:34                                   ` Basil L. Contovounesios
2020-12-07 22:16                                     ` Basil L. Contovounesios
2020-12-07 22:37                                       ` Basil L. Contovounesios
2020-12-08  8:09                                         ` martin rudalics
2020-12-14 21:03                                           ` Basil L. Contovounesios
2020-12-15 16:03                                             ` Eli Zaretskii
2020-12-15 16:24                                               ` Basil L. Contovounesios
2020-12-18 14:57                                       ` Basil L. Contovounesios
2020-12-18 15:36                                       ` Stefan Monnier
2020-12-18 18:49                                         ` Basil L. Contovounesios
2020-12-19 10:33                                           ` Eli Zaretskii
2020-12-19 14:15                                             ` Basil L. Contovounesios
2020-12-19 16:06                                               ` Eli Zaretskii
2020-12-19 21:10                                                 ` Basil L. Contovounesios
2020-12-20 15:05                                                   ` Eli Zaretskii
2019-03-07  6:18 ` Alexander Miller
2019-03-07  8:29   ` martin rudalics
2019-03-07  9:44     ` Alexander Miller
2019-03-07 13:46       ` martin rudalics
2020-12-20 17:57 ` Basil L. Contovounesios

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=49c11920-0909-dcc2-4a39-4cdcfaf20453@gmx.at \
    --to=rudalics@gmx.at \
    --cc=34765@debbugs.gnu.org \
    --cc=alexanderm@web.de \
    --cc=eliz@gnu.org \
    --cc=monnier@IRO.UMontreal.CA \
    /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).