From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.devel Subject: separate name uniquification from `generate-new-buffer-name' Date: Tue, 25 May 2010 11:07:39 -0700 Message-ID: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: dough.gmane.org 1274810988 11375 80.91.229.12 (25 May 2010 18:09:48 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 25 May 2010 18:09:48 +0000 (UTC) To: Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Tue May 25 20:09:47 2010 connect(): No such file or directory Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1OGyZR-0001R6-Fb for ged-emacs-devel@m.gmane.org; Tue, 25 May 2010 20:09:41 +0200 Original-Received: from localhost ([127.0.0.1]:34282 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OGyZQ-00077Y-UW for ged-emacs-devel@m.gmane.org; Tue, 25 May 2010 14:09:40 -0400 Original-Received: from [140.186.70.92] (port=53575 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OGyYq-0006nR-HC for emacs-devel@gnu.org; Tue, 25 May 2010 14:09:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OGyYp-0007Y4-4g for emacs-devel@gnu.org; Tue, 25 May 2010 14:09:04 -0400 Original-Received: from rcsinet10.oracle.com ([148.87.113.121]:21020) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OGyYo-0007Xy-UV for emacs-devel@gnu.org; Tue, 25 May 2010 14:09:03 -0400 Original-Received: from rcsinet13.oracle.com (rcsinet13.oracle.com [148.87.113.125]) by rcsinet10.oracle.com (Switch-3.4.2/Switch-3.4.1) with ESMTP id o4PI90mh009132 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Tue, 25 May 2010 18:09:01 GMT Original-Received: from acsmt355.oracle.com (acsmt355.oracle.com [141.146.40.155]) by rcsinet13.oracle.com (Switch-3.4.2/Switch-3.4.1) with ESMTP id o4PEfIUw019376 for ; Tue, 25 May 2010 18:08:59 GMT Original-Received: from abhmt014.oracle.com by acsmt355.oracle.com with ESMTP id 297051811274810859; Tue, 25 May 2010 11:07:39 -0700 Original-Received: from dradamslap1 (/141.144.64.168) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Tue, 25 May 2010 11:07:38 -0700 X-Mailer: Microsoft Office Outlook 11 Thread-Index: Acr8NSlTBSfQZtxERpSzlupXxial4Q== X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.5931 X-Auth-Type: Internal IP X-Source-IP: rcsinet13.oracle.com [148.87.113.125] X-CT-RefId: str=0001.0A090204.4BFC123D.019D:SCFMA4539811,ss=1,fgs=0 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:125262 Archived-At: Something I would like to see is separation of -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 can be anything you like, not just <2> (optional arg MIN). * You can optionally make the new name use a single , instead of things like a<2><1> and a<2><1><4> (arg USE-BASE-P). * When using only a single (non-nil USE-BASE-P), you can optionally have it either fill in holes, picking the minimal 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, 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 `', 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' (only a single suffix). BASENAME is NAME truncated at the right starting with the first suffix `'. 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' 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 `' 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)))))))))