emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

separate name uniquification from `generate-new-buffer-name'


From: Drew Adams
Subject: separate name uniquification from `generate-new-buffer-name'
Date: Tue, 25 May 2010 11:07:39 -0700

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





reply via email to

[Prev in Thread] Current Thread [Next in Thread]