From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Karl Fogel Newsgroups: gmane.emacs.devel Subject: Re: how having the basename of a file or directory Date: Mon, 31 Jan 2011 11:43:23 -0500 Message-ID: <87lj2158ic.fsf@red-bean.com> References: <8739o9ilfs.fsf@gmail.com> <877hdl4grd.fsf@uwakimon.sk.tsukuba.ac.jp> <87y661h2ju.fsf@gmail.com> <87y6613ujh.fsf@gmail.com> Reply-To: Karl Fogel NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1296492776 9467 80.91.229.12 (31 Jan 2011 16:52:56 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Mon, 31 Jan 2011 16:52:56 +0000 (UTC) Cc: "Stephen J. Turnbull" , Stefan Monnier , emacs-devel@gnu.org To: Thierry Volpiatto Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jan 31 17:52:48 2011 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 1Pjwzf-0007p4-OO for ged-emacs-devel@m.gmane.org; Mon, 31 Jan 2011 17:52:47 +0100 Original-Received: from localhost ([127.0.0.1]:34268 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pjwzf-00055u-6T for ged-emacs-devel@m.gmane.org; Mon, 31 Jan 2011 11:52:47 -0500 Original-Received: from [140.186.70.92] (port=58341 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Pjwqk-0000TY-TU for emacs-devel@gnu.org; Mon, 31 Jan 2011 11:43:38 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Pjwqh-0000H6-LS for emacs-devel@gnu.org; Mon, 31 Jan 2011 11:43:34 -0500 Original-Received: from osh-net-219-98.onshore.net ([66.146.219.98]:48741 helo=sanpietro.red-bean.com) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Pjwqh-0000CD-Ia for emacs-devel@gnu.org; Mon, 31 Jan 2011 11:43:31 -0500 Original-Received: from localhost ([127.0.0.1]:37606 helo=floss ident=kfogel) by sanpietro.red-bean.com with esmtp (Exim 4.72) (envelope-from ) id 1PjwqZ-00017d-PA; Mon, 31 Jan 2011 10:43:23 -0600 In-Reply-To: <87y6613ujh.fsf@gmail.com> (Thierry Volpiatto's message of "Mon, 31 Jan 2011 17:30:26 +0100") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 66.146.219.98 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:135318 Archived-At: I think Thierry's point is that it would be useful for Emacs to have a function called "basename", that does what functions of similar name do in other programming language standard libraries and in the shell. FWIW, I agree. Many times I've done an Apropos search on "basename", only to remember that there's some complicated recipe involving `file-name-nondirectory' (which I then go look up). Now, what the exact behavior of `basename' should be is open to debate. Perhaps it should be: (defun basename (path) (file-name-nondirectory (directory-file-name path))) Or perhaps it should be: (defun basename (path) (thing-thierry-recently-defined)) Or perhaps something else. Can we treat the two questions separately, though? If we agree that having something called `basename' in Emacs Lisp would be good, then we can probably quickly agree on exactly how it should behave, so let's start with the first question: should we have `basename'? -Karl Thierry Volpiatto writes: >Stefan Monnier writes: > >>> Yes thanks, that's work too, but it would be nice to do not have to take >>> care of that: >> >>> (file-name-nondirectory "path/to/a/directory") >>> should return ==> directory >> >> It does. >> >>> (file-name-nondirectory "path/to/a/file") >>> should return ==> file >> >> It does. >> >>> So modifying `file-name-nondirectory' or creating a basename function >>> or macro like: >> >>> (defun basename (fname) >>> (if (file-directory-p fname) >>> (let ((dirname (directory-file-name fname))) >>> (file-name-nondirectory dirname)) >>> (file-name-nondirectory fname))) >> >> That won't work on (basename "/non/existing/thing/"). >> You really want to use (file-name-nondirectory (directory-file-name fname)) >Indeed, yes, thanks Stefan, but something like > > >(defun basename (fname) > (if (or (file-directory-p fname) > (string-match "/$" fname)) > (let ((dirname (directory-file-name fname))) > (file-name-nondirectory dirname)) > (file-name-nondirectory fname))) > >would work and be useful.