emacs-devel
[Top][All Lists]
Advanced

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

Re: how having the basename of a file or directory


From: Karl Fogel
Subject: Re: how having the basename of a file or directory
Date: Mon, 31 Jan 2011 11:43:23 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

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 <address@hidden> writes:
>Stefan Monnier <address@hidden> 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.



reply via email to

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