From: "Drew Adams" <drew.adams@oracle.com>
To: <emacs-devel@gnu.org>
Subject: separate name uniquification from `generate-new-buffer-name'
Date: Tue, 25 May 2010 11:07:39 -0700 [thread overview]
Message-ID: <E0C38AE459B843F1A43C65ABB8DEEA5B@us.oracle.com> (raw)
Something I would like to see is separation of <N>-suffix name uniquifying from
`generate-new-buffer-name'. The latter could just use the more general
unique-naming function (unless C optimization is important in that particular
case).
I use such naming for windows and frames, and I'm sure there could be other use
cases - wherever you want a set of similar names with some simple way to
distinguish them.
Something like the function `unique-name' below, perhaps. It can be used for
other things besides buffer names, and its naming is more general (see doc
string):
* The minimum <N> can be anything you like, not just <2> (optional arg MIN).
* You can optionally make the new name use a single <N>, instead of things like
a<2><1> and a<2><1><4> (arg USE-BASE-P).
* When using only a single <N> (non-nil USE-BASE-P), you can optionally have it
either fill in holes, picking the minimal <N> that is free (and >= MIN), or have
it always use a number greater than all those in use (arg MAXP).
* (You can even use negative indexes, such as a<-3>, though I don't foresee any
particular use case for that.)
(defun unique-name (name existing-names &optional min use-base-p maxp)
"Return NAME or NAME<N>, a name that is not in EXISTING-NAMES.
Return NAME if NAME is not a member of EXISTING-NAMES.
Otherwise, return NAME or its base name, suffixed by `<N>', where N is
an integer.
The optional args are used only when NAME is in EXISTING-NAMES.
MIN is the minimum integer N to use in the new suffix. Default: 1.
Non-nil USE-BASE-P means use only the base name of NAME. The value
returned is of the form `BASENAME<N>' (only a single suffix).
BASENAME is NAME truncated at the right starting with the first suffix
`<M>'. The base name of `a<2>' and `a<2><3>' is `a'.
For example, if NAME is `a<2>', then with nil USE-BASE-P we might
return `a<2><1>' (depending on MIN, MAX etc.). With non-nil
USE-BASE-P we might return `a<3>', since the base name `a' gets
suffixed, not the full NAME `a<2>'.
Optional arg MAXP is used only if USE-BASE-P is non-nil.
If MAXP is nil then N is the smallest integer greater than or equal to
MIN such that `BASENAME<N>' is not in EXISTING-NAMES.
If MAXP is non-nil then N is the smallest integer greater than or
equal to MIN and greater than the largest integer M used in a suffix
`<M>' that immediately follows BASENAME in a name in EXISTING-NAMES.
As an example, `generate-new-buffer-name' could be defined this way:
(defun generate-new-buffer-name (buf)
(let ((buffs (mapcar #'buffer-name (buffer-list))))
(unique-name buf buffs 2)))"
(unless min (setq min 1))
(if (and (not (member name existing-names)) (not maxp))
name
(let ((indx min)
(baselen (string-match "\<\\(-?[0-9]+\\)\>" name))
try)
(when (and use-base-p baselen)
(setq name (substring name 0 baselen)))
(if maxp
(format
"%s<%d>" name
(1+ (apply
#'max
(mapcar (lambda (nn)
(if (string-match "\<\\(-?[0-9]+\\)\>" nn)
(string-to-number (match-string 1 nn))
min))
existing-names))))
(catch 'unique-name
(while t
(unless (member (setq try (concat name "<" indx ">"))
existing-names)
(throw 'unique-name try))
(setq indx (max min (1+ indx)))))))))
next reply other threads:[~2010-05-25 18:07 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2010-05-25 18:07 Drew Adams [this message]
2010-05-25 19:30 ` separate name uniquification from `generate-new-buffer-name' Stefan Monnier
2010-05-25 19:52 ` Drew Adams
2021-12-28 3:05 ` Drew Adams
2010-05-26 0:19 ` Juanma Barranquero
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=E0C38AE459B843F1A43C65ABB8DEEA5B@us.oracle.com \
--to=drew.adams@oracle.com \
--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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.