all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* separate name uniquification from `generate-new-buffer-name'
@ 2010-05-25 18:07 Drew Adams
  2010-05-25 19:30 ` Stefan Monnier
  0 siblings, 1 reply; 5+ messages in thread
From: Drew Adams @ 2010-05-25 18:07 UTC (permalink / raw)
  To: emacs-devel

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)))))))))





^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2021-12-28  3:05 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-05-25 18:07 separate name uniquification from `generate-new-buffer-name' Drew Adams
2010-05-25 19:30 ` Stefan Monnier
2010-05-25 19:52   ` Drew Adams
2021-12-28  3:05     ` Drew Adams
2010-05-26  0:19   ` Juanma Barranquero

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.