summaryrefslogtreecommitdiff
path: root/src/buffer.c
diff options
context:
space:
mode:
authorBasil L. Contovounesios <contovob@tcd.ie>2020-12-19 12:39:45 +0000
committerBasil L. Contovounesios <contovob@tcd.ie>2020-12-20 17:32:24 +0000
commit1a0a11f7d2d1dbecb9f754b1e129d50e489058e6 (patch)
treeca78c82ccc83b90ae3e382097fdffc928097f881 /src/buffer.c
parent409a9dbe9da64b4d75fec1f511e168c94e60e35b (diff)
downloademacs-1a0a11f7d2d1dbecb9f754b1e129d50e489058e6.tar.gz
Inhibit buffer hooks in temporary buffers
Give get-buffer-create an optional argument to inhibit buffer hooks in internal or temporary buffers for efficiency (bug#34765). * etc/NEWS: Announce new parameter of get-buffer-create and generate-new-buffer, and that with-temp-buffer and with-temp-file now inhibit buffer hooks. * doc/lispref/buffers.texi (Buffer Names): Fix typo. (Creating Buffers): Document new parameter of get-buffer-create and generate-new-buffer. (Buffer List, Killing Buffers): Document when buffer hooks are inhibited. (Current Buffer): * doc/lispref/files.texi (Writing to Files): Document that with-temp-buffer and with-temp-file inhibit buffer hooks. * doc/lispref/internals.texi (Buffer Internals): Document inhibit_buffer_hooks flag. Remove stale comment. * doc/misc/gnus-faq.texi (FAQ 5-8): * lisp/simple.el (shell-command-on-region): Fix indentation. * lisp/files.el (kill-buffer-hook): Document when hook is inhibited. (create-file-buffer): * lisp/gnus/gnus-uu.el (gnus-uu-unshar-article): * lisp/international/mule.el (load-with-code-conversion): * lisp/mh-e/mh-xface.el (mh-x-image-url-fetch-image): * lisp/net/imap.el (imap-open): * lisp/net/mailcap.el (mailcap-maybe-eval): * lisp/progmodes/flymake-proc.el (flymake-proc--read-file-to-temp-buffer) (flymake-proc--copy-buffer-to-temp-buffer): Simplify. * lisp/subr.el (generate-new-buffer): Forward new optional argument to inhibit buffer hooks to get-buffer-create. (with-temp-file, with-temp-buffer, with-output-to-string): * lisp/json.el (json-encode-string): Inhibit buffer hooks in buffer used. * src/buffer.c (run_buffer_list_update_hook): New helper function. (Fget_buffer_create): Use it. Add optional argument to set inhibit_buffer_hooks flag instead of comparing the buffer name to Vcode_conversion_workbuf_name. All callers changed. (Fmake_indirect_buffer, Frename_buffer, Fbury_buffer_internal) (record_buffer): Use run_buffer_list_update_hook. (Fkill_buffer): Document when buffer hooks are inhibited. Use run_buffer_list_update_hook. (init_buffer_once): Inhibit buffer hooks in Vprin1_to_string_buffer. (Vkill_buffer_query_functions, Vbuffer_list_update_hook): Document when hooks are inhibited. * src/buffer.h (struct buffer): Update inhibit_buffer_hooks commentary. * src/coding.h (Vcode_conversion_workbuf_name): * src/coding.c (Vcode_conversion_workbuf_name): Make static again since it is no longer needed in src/buffer.c. (code_conversion_restore, code_conversion_save, syms_of_coding): Prefer boolean over integer constants. * src/fileio.c (Finsert_file_contents): Inhibit buffer hooks in " *code-converting-work*" buffer. * src/window.c (Fselect_window): Fix grammar. Mention window-selection-change-functions alongside buffer-list-update-hook. * test/src/buffer-tests.el: Fix requires. (buffer-tests-inhibit-buffer-hooks): New test.
Diffstat (limited to 'src/buffer.c')
-rw-r--r--src/buffer.c88
1 files changed, 50 insertions, 38 deletions
diff --git a/src/buffer.c b/src/buffer.c
index dfc34faf6e6..9e44345616e 100644
--- a/src/buffer.c
+++ b/src/buffer.c
@@ -37,7 +37,6 @@ along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>. */
#include "window.h"
#include "commands.h"
#include "character.h"
-#include "coding.h"
#include "buffer.h"
#include "region-cache.h"
#include "indent.h"
@@ -514,16 +513,33 @@ get_truename_buffer (register Lisp_Object filename)
return Qnil;
}
-DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 1, 0,
+/* Run buffer-list-update-hook if Vrun_hooks is non-nil, and BUF is NULL
+ or does not have buffer hooks inhibited. BUF is NULL when called by
+ make-indirect-buffer, since it does not inhibit buffer hooks. */
+
+static void
+run_buffer_list_update_hook (struct buffer *buf)
+{
+ if (! (NILP (Vrun_hooks) || (buf && buf->inhibit_buffer_hooks)))
+ call1 (Vrun_hooks, Qbuffer_list_update_hook);
+}
+
+DEFUN ("get-buffer-create", Fget_buffer_create, Sget_buffer_create, 1, 2, 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.
+that name and return it.
+
+If BUFFER-OR-NAME starts with a space, the new buffer does not keep undo
+information. If optional argument INHIBIT-BUFFER-HOOKS is non-nil, the
+new buffer does not run the hooks `kill-buffer-hook',
+`kill-buffer-query-functions', and `buffer-list-update-hook'. This
+avoids slowing down internal or temporary buffers that are never
+presented to users or passed on to other applications.
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)
+ (register Lisp_Object buffer_or_name, Lisp_Object inhibit_buffer_hooks)
{
register Lisp_Object buffer, name;
register struct buffer *b;
@@ -598,11 +614,7 @@ even if it is dead. The return value is never nil. */)
set_string_intervals (name, NULL);
bset_name (b, name);
- b->inhibit_buffer_hooks
- = (STRINGP (Vcode_conversion_workbuf_name)
- && strncmp (SSDATA (name), SSDATA (Vcode_conversion_workbuf_name),
- SBYTES (Vcode_conversion_workbuf_name)) == 0);
-
+ b->inhibit_buffer_hooks = !NILP (inhibit_buffer_hooks);
bset_undo_list (b, SREF (name, 0) != ' ' ? Qnil : Qt);
reset_buffer (b);
@@ -614,9 +626,8 @@ 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);
+
+ run_buffer_list_update_hook (b);
return buffer;
}
@@ -890,9 +901,7 @@ CLONE nil means the indirect buffer's state is reset to default values. */)
set_buffer_internal_1 (old_b);
}
- /* Run buffer-list-update-hook. */
- if (!NILP (Vrun_hooks))
- call1 (Vrun_hooks, Qbuffer_list_update_hook);
+ run_buffer_list_update_hook (NULL);
return buf;
}
@@ -1536,9 +1545,7 @@ This does not change the name of the visited file (if any). */)
&& !NILP (BVAR (current_buffer, auto_save_file_name)))
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 (current_buffer);
/* Refetch since that last call may have done GC. */
return BVAR (current_buffer, name);
@@ -1612,7 +1619,7 @@ exists, return the buffer `*scratch*' (creating it if necessary). */)
buf = Fget_buffer (scratch);
if (NILP (buf))
{
- buf = Fget_buffer_create (scratch);
+ buf = Fget_buffer_create (scratch, Qnil);
Fset_buffer_major_mode (buf);
}
return buf;
@@ -1636,7 +1643,7 @@ other_buffer_safely (Lisp_Object buffer)
buf = Fget_buffer (scratch);
if (NILP (buf))
{
- buf = Fget_buffer_create (scratch);
+ buf = Fget_buffer_create (scratch, Qnil);
Fset_buffer_major_mode (buf);
}
@@ -1713,7 +1720,9 @@ buffer to be killed as the current buffer. If any of them returns nil,
the buffer is not killed. The hook `kill-buffer-hook' is run before the
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.
+supposed to not change the current buffer. Neither hook is run for
+internal or temporary buffers created by `get-buffer-create' or
+`generate-new-buffer' with 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
@@ -1973,9 +1982,7 @@ cleaning up all windows currently displaying the buffer to be killed. */)
bset_width_table (b, Qnil);
unblock_input ();
- /* 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 (b);
return Qt;
}
@@ -2015,9 +2022,7 @@ record_buffer (Lisp_Object buffer)
fset_buffer_list (f, Fcons (buffer, Fdelq (buffer, f->buffer_list)));
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 (XBUFFER (buffer));
}
@@ -2054,9 +2059,7 @@ DEFUN ("bury-buffer-internal", Fbury_buffer_internal, Sbury_buffer_internal,
fset_buried_buffer_list
(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 (XBUFFER (buffer));
return Qnil;
}
@@ -5349,10 +5352,11 @@ init_buffer_once (void)
Fput (Qkill_buffer_hook, Qpermanent_local, Qt);
/* Super-magic invisible buffer. */
- Vprin1_to_string_buffer = Fget_buffer_create (build_pure_c_string (" prin1"));
+ Vprin1_to_string_buffer =
+ Fget_buffer_create (build_pure_c_string (" prin1"), Qt);
Vbuffer_alist = Qnil;
- Fset_buffer (Fget_buffer_create (build_pure_c_string ("*scratch*")));
+ Fset_buffer (Fget_buffer_create (build_pure_c_string ("*scratch*"), Qnil));
inhibit_modification_hooks = 0;
}
@@ -5397,7 +5401,7 @@ init_buffer (void)
#endif /* USE_MMAP_FOR_BUFFERS */
AUTO_STRING (scratch, "*scratch*");
- Fset_buffer (Fget_buffer_create (scratch));
+ Fset_buffer (Fget_buffer_create (scratch, Qnil));
if (NILP (BVAR (&buffer_defaults, enable_multibyte_characters)))
Fset_buffer_multibyte (Qnil);
@@ -6300,9 +6304,14 @@ Use Custom to set this variable and update the display. */);
DEFVAR_LISP ("kill-buffer-query-functions", Vkill_buffer_query_functions,
doc: /* List of functions called with no args to query before killing a buffer.
The buffer being killed will be current while the functions are running.
+See `kill-buffer'.
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 internal or temporary buffers created by
+`get-buffer-create' or `generate-new-buffer' with argument
+INHIBIT-BUFFER-HOOKS non-nil. */);
Vkill_buffer_query_functions = Qnil;
DEFVAR_LISP ("change-major-mode-hook", Vchange_major_mode_hook,
@@ -6315,9 +6324,12 @@ 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 internal or temporary
+buffers created by `get-buffer-create' or `generate-new-buffer' with
+argument INHIBIT-BUFFER-HOOKS non-nil.
+
+Functions run by this hook should avoid calling `select-window' with a
+nil NORECORD argument since it may lead to infinite recursion. */);
Vbuffer_list_update_hook = Qnil;
DEFSYM (Qbuffer_list_update_hook, "buffer-list-update-hook");