unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Dmitry Antipov <dmantipov@yandex.ru>
To: Emacs development discussions <emacs-devel@gnu.org>
Cc: martin rudalics <rudalics@gmx.at>,
	Stefan Monnier <monnier@IRO.UMontreal.CA>
Subject: get-live-buffer primitive
Date: Thu, 13 Sep 2012 11:52:43 +0400	[thread overview]
Message-ID: <505190CB.5010809@yandex.ru> (raw)

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

What about such a primitive? We already have live_buffer used in markers
code, so we can use this internal thing in Fget_live_buffer and avoid
duplicating some things in elisp...

Dmitry

[-- Attachment #2: get_live_buffer.patch --]
[-- Type: text/plain, Size: 4099 bytes --]

=== modified file 'lisp/window.el'
--- lisp/window.el	2012-09-09 06:43:47 +0000
+++ lisp/window.el	2012-09-13 07:37:03 +0000
@@ -219,16 +219,9 @@
   "Return buffer specified by BUFFER-OR-NAME.
 BUFFER-OR-NAME must be either a buffer or a string naming a live
 buffer and defaults to the current buffer."
-  (cond
-   ((not buffer-or-name)
-    (current-buffer))
-   ((bufferp buffer-or-name)
-    (if (buffer-live-p buffer-or-name)
-	buffer-or-name
-      (error "Buffer %s is not a live buffer" buffer-or-name)))
-   ((get-buffer buffer-or-name))
-   (t
-    (error "No such buffer %s" buffer-or-name))))
+  (let ((buffer (get-live-buffer buffer-or-name)))
+    (or buffer (error "No such live buffer %s" buffer-or-name))
+    buffer))
 
 (defun window-normalize-frame (frame)
   "Return frame specified by FRAME.

=== modified file 'src/buffer.c'
--- src/buffer.c	2012-09-11 04:22:03 +0000
+++ src/buffer.c	2012-09-13 07:47:23 +0000
@@ -469,6 +469,33 @@
   return Fcdr (assoc_ignore_text_properties (buffer_or_name, Vbuffer_alist));
 }
 
+DEFUN ("get-live-buffer", Fget_live_buffer, Sget_live_buffer, 1, 1, 0,
+       doc: /* Return live buffer named BUFFER-OR-NAME.
+If BUFFER-OR-NAME is nil, return current buffer, which is always live.
+If BUFFER-OR-NAME is a string and there is a live buffer with that name,
+return this buffer.  If there is no such buffer or the buffer is killed,
+return nil.  If BUFFER-OR-NAME is a buffer, return it as given if it is
+live, and nil otherwise.  */)
+  (Lisp_Object buffer_or_name)
+{
+  struct buffer *b;
+
+  if (STRINGP (buffer_or_name))
+    {
+      buffer_or_name = Fcdr (assoc_ignore_text_properties
+			     (buffer_or_name, Vbuffer_alist));
+      if (NILP (buffer_or_name))
+	return Qnil;
+    }
+  b = live_buffer (buffer_or_name);
+  if (b)
+    {
+      XSETBUFFER (buffer_or_name, b);
+      return buffer_or_name;
+    }
+  return Qnil;
+}
+
 DEFUN ("get-file-buffer", Fget_file_buffer, Sget_file_buffer, 1, 1, 0,
        doc: /* Return the buffer visiting file FILENAME (a string).
 The buffer's `buffer-file-name' must match exactly the expansion of FILENAME.
@@ -6260,6 +6287,7 @@
   defsubr (&Sbuffer_live_p);
   defsubr (&Sbuffer_list);
   defsubr (&Sget_buffer);
+  defsubr (&Sget_live_buffer);
   defsubr (&Sget_file_buffer);
   defsubr (&Sget_buffer_create);
   defsubr (&Smake_indirect_buffer);

=== modified file 'src/buffer.h'
--- src/buffer.h	2012-09-11 04:22:03 +0000
+++ src/buffer.h	2012-09-13 07:30:23 +0000
@@ -1025,6 +1025,30 @@
 extern void fix_overlays_before (struct buffer *, ptrdiff_t, ptrdiff_t);
 extern void mmap_set_vars (bool);
 
+/* If BUFFER is nil, return current buffer pointer.  Next, check
+   whether BUFFER is a buffer object and return buffer pointer
+   corresponding to BUFFER if BUFFER is live, or NULL otherwise.  */
+
+BUFFER_INLINE struct buffer *
+live_buffer (Lisp_Object buffer)
+{
+  struct buffer *b;
+
+  if (NILP (buffer))
+    {
+      b = current_buffer;
+      eassert (BUFFER_LIVE_P (b));
+    }
+  else
+    {
+      CHECK_BUFFER (buffer);
+      b = XBUFFER (buffer);
+      if (!BUFFER_LIVE_P (b))
+       b = NULL;
+    }
+  return b;
+}
+
 /* Set the current buffer to B.
 
    We previously set windows_or_buffers_changed here to invalidate

=== modified file 'src/marker.c'
--- src/marker.c	2012-09-11 04:22:03 +0000
+++ src/marker.c	2012-09-13 07:30:02 +0000
@@ -450,30 +450,6 @@
     }
 }
 
-/* If BUFFER is nil, return current buffer pointer.  Next, check
-   whether BUFFER is a buffer object and return buffer pointer
-   corresponding to BUFFER if BUFFER is live, or NULL otherwise.  */
-
-static inline struct buffer *
-live_buffer (Lisp_Object buffer)
-{
-  struct buffer *b;
-
-  if (NILP (buffer))
-    {
-      b = current_buffer;
-      eassert (BUFFER_LIVE_P (b));
-    }
-  else
-    {
-      CHECK_BUFFER (buffer);
-      b = XBUFFER (buffer);
-      if (!BUFFER_LIVE_P (b))
-       b = NULL;
-    }
-  return b;
-}
-
 /* Internal function to set MARKER in BUFFER at POSITION.  Non-zero
    RESTRICTED means limit the POSITION by the visible part of BUFFER.  */
 


             reply	other threads:[~2012-09-13  7:52 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-09-13  7:52 Dmitry Antipov [this message]
2012-09-13 12:44 ` get-live-buffer primitive Richard Stallman
2012-09-13 13:42   ` Stefan Monnier
2012-09-13 16:50     ` martin rudalics
2012-09-13 17:00       ` martin rudalics
2012-09-13 17:30         ` martin rudalics
2012-09-13 20:45     ` Richard Stallman
2012-09-14  9:00       ` martin rudalics
2012-09-13 12:58 ` 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=505190CB.5010809@yandex.ru \
    --to=dmantipov@yandex.ru \
    --cc=emacs-devel@gnu.org \
    --cc=monnier@IRO.UMontreal.CA \
    --cc=rudalics@gmx.at \
    /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).