Re: [code] Add basename to lfs

From: Pedro Andres Aranda Gutierrez <paaguti.att.gmail.com>
Date: Sat, 20 Jan 2018 17:51:15 +0100

Hi,

I know these oneliners ;-) But having them in lfs makes them more usable in
other tasks.

One example is writing snippets.
%<lfs.basename(buffer.filename)> is 'self documented' code ;-) which the
actual code is not,

I bet I forget the one liner's meaning sooner that the basename function
name ;)
Maybe I'm getting old ;-P

Best, /PA

On 20 January 2018 at 15:48, Mitchell <m.att.foicica.com> wrote:

> Hi Pedro,
>
>
> On Sat, 20 Jan 2018, Pedro Andres Aranda Gutierrez wrote:
>
> Hi
>>
>> here is a patch to move the extraction of the basename to lfs. I use it in
>> several modules and snippets.
>>
>> diff -r c42366c06ff4 core/lfs_ext.lua
>> --- a/core/lfs_ext.lua Wed Jan 17 12:33:34 2018 -0500
>> +++ b/core/lfs_ext.lua Sat Jan 20 08:40:44 2018 +0100
>> @@ -127,3 +127,16 @@
>> end
>> return filename
>> end
>> +
>> +---
>> +-- Returns the basename of string *filename*.
>> +-- returned path is not guaranteed to exist.
>> +-- @param filename The relative or absolute path of a file
>> +-- @return string the path stripped of all directories (and drives in
>> WIN32)
>> +-- @name basename
>> +function lfs.basename(filename)
>> + fpattern = WIN32 and "^(.*[\\:])([^\\]+)$" or "^(.*/)([^/]+)$"
>> + path, fname = (filename or ""):match(fpattern)
>> +
>> + return fname or fspec
>> +end
>>
>> returning path instead of fname implements the dirname functionality of
>> bash and other shells. I don't have it, because I have not found a use
>> case
>> for it.
>>
>
> This is a one-liner in Lua:
>
> (buffer.filename or ''):match('[^/\\]+$')
>
> I don't see a need to add it as an explicit extension function to `lfs`.
>
> For extra credit, fetching the path part is also a one-liner:
>
> (buffer.filename or ''):match('^.+[/\\]')
>
> Textadept makes extensive use of these calls internally for dealing with
> file parts.
>
> Cheers,
> Mitchell
> --
> You are subscribed to code.att.foicica.com.
> To change subscription settings, send an e-mail to code+help.att.foicica.com.
> To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
>
>

-- 
Fragen sind nicht da um beantwortet zu werden,
Fragen sind da um gestellet zu werden
Georg Kreisler
-- 
You are subscribed to code.att.foicica.com.
To change subscription settings, send an e-mail to code+help.att.foicica.com.
To unsubscribe, send an e-mail to code+unsubscribe.att.foicica.com.
Received on Sat 20 Jan 2018 - 11:51:15 EST

This archive was generated by hypermail 2.2.0 : Sun 21 Jan 2018 - 06:35:25 EST