From 8e4fd1e172f5fc3daf8219965a588bf0125ba311 Mon Sep 17 00:00:00 2001 From: Glenn Morris Date: Tue, 3 Jul 2012 13:47:32 -0400 Subject: [PATCH] Speed up generate-new-buffer-name for invisible buffers (bug#1229) * src/buffer.c (Fgenerate_new_buffer_name): Speed up finding a new buffer for invisible buffers. * src/lisp.h (Frandom): Make it visible to C. --- src/ChangeLog | 6 ++++++ src/buffer.c | 22 +++++++++++++++++++--- src/lisp.h | 1 + 3 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/ChangeLog b/src/ChangeLog index e399398131..805b189ae8 100644 --- a/src/ChangeLog +++ b/src/ChangeLog @@ -1,3 +1,9 @@ +2012-07-03 Glenn Morris + + * lisp.h (Frandom): Make it visible to C. + * buffer.c (Fgenerate_new_buffer_name): Speed up finding a new + buffer for invisible buffers. (Bug#1229) + 2012-07-03 Dmitry Antipov Fix block vector allocation code to allow VECTOR_BLOCK_SIZE diff --git a/src/buffer.c b/src/buffer.c index 08118baa3d..83f0eb153c 100644 --- a/src/buffer.c +++ b/src/buffer.c @@ -838,10 +838,14 @@ If there is no live buffer named NAME, then return NAME. Otherwise modify name by appending `', incrementing NUMBER \(starting at 2) until an unused name is found, and then return that name. Optional second argument IGNORE specifies a name that is okay to use (if -it is in the sequence to be tried) even if a buffer with that name exists. */) +it is in the sequence to be tried) even if a buffer with that name exists. + +If NAME begins with a space (i.e., a buffer that is not normally +visible to users), then if buffer NAME already exists a random number +is first appended to NAME, to speed up finding a non-existent buffer. */) (register Lisp_Object name, Lisp_Object ignore) { - register Lisp_Object gentemp, tem; + register Lisp_Object gentemp, tem, tem2; ptrdiff_t count; char number[INT_BUFSIZE_BOUND (ptrdiff_t) + sizeof "<>"]; @@ -854,11 +858,23 @@ it is in the sequence to be tried) even if a buffer with that name exists. */) if (NILP (tem)) return name; + if (!strncmp (SSDATA (name), " ", 1)) /* see bug#1229 */ + { + /* Note fileio.c:make_temp_name does random differently. */ + sprintf (number, "-%"pD"d", Frandom (make_number (999999))); + tem2 = concat2 (name, build_string (number)); + tem = Fget_buffer (tem2); + if (NILP (tem)) + return tem2; + } + else + tem2 = name; + count = 1; while (1) { sprintf (number, "<%"pD"d>", ++count); - gentemp = concat2 (name, build_string (number)); + gentemp = concat2 (tem2, build_string (number)); tem = Fstring_equal (gentemp, ignore); if (!NILP (tem)) return gentemp; diff --git a/src/lisp.h b/src/lisp.h index 8cec160069..2059e2a3fb 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -2473,6 +2473,7 @@ EXFUN (Fputhash, 3); EXFUN (Fremhash, 2); EXFUN (Fidentity, 1); +EXFUN (Frandom, 1); EXFUN (Flength, 1); EXFUN (Fappend, MANY); EXFUN (Fconcat, MANY); -- 2.20.1