all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Is there a function to sort file names by file version number, like "ls -v"?
@ 2013-05-23  8:21 Florian v. Savigny
  0 siblings, 0 replies; 6+ messages in thread
From: Florian v. Savigny @ 2013-05-23  8:21 UTC (permalink / raw)
  To: help-gnu-emacs


Hi there,

if you backup files using "cp --backup numbered", cp creates version
numbers like this:

xorg.conf.~1~
xorg.conf.~2~
...
xorg.conf.~9~
xorg.conf.~10~
xorg.conf.~11~
...
xorg.conf.~20~

etc.

As these names are not zero-padded, the normal, alphabetical sorting
will sort them like this:

xorg.conf.~10~
xorg.conf.~11~
...
xorg.conf.~1~
xorg.conf.~20~
xorg.conf.~21~
xorg.conf.~2~
...
xorg.conf.~9~

which is definitely not what is normally useful. For this reason, ls
seems to have the -v switch, which sorts them as shown in the first
listing.

I am wondering if there is somehow a comparison function (of the same
category as string-lessp) in Elisp which compares file name strings by
these version numbers, so I could sort the result of
directory-file-names in the same way as "ls -v".

Is there? (There must be! ;-))

Thank you very much in advance!

Best regards,

Florian




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there a function to sort file names by file version number, like "ls -v"?
       [not found] <mailman.229.1369297314.22516.help-gnu-emacs@gnu.org>
@ 2013-05-23 18:33 ` Sean McAfee
  2013-05-23 20:01   ` Florian v. Savigny
       [not found]   ` <mailman.283.1369339299.22516.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 6+ messages in thread
From: Sean McAfee @ 2013-05-23 18:33 UTC (permalink / raw)
  To: help-gnu-emacs

florian@fsavigny.de (Florian v. Savigny) writes:
> I am wondering if there is somehow a comparison function (of the same
> category as string-lessp) in Elisp which compares file name strings by
> these version numbers, so I could sort the result of
> directory-file-names in the same way as "ls -v".

I don't think so, but it's easy to write one:

(defun versioned-filename-lessp (a b)
  (labels ((parse (str)
             (string-match "\\(?:~\\([0-9]+\\)~\\)?\\'" str)
             (cons (substring str 0 (match-beginning 0))
                   (and (match-beginning 1)
                        (string-to-number (match-string 1 str))))))
    (save-match-data
      (let ((ap (parse a))
            (bp (parse b)))
        (or (string-lessp (car ap) (car bp))
            (and (not (string-lessp (car bp) (car ap)))
                 (cdr ap)
                 (cdr bp)
                 (< (cdr ap) (cdr bp))))))))


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there a function to sort file names by file version number, like "ls -v"?
  2013-05-23 18:33 ` Sean McAfee
@ 2013-05-23 20:01   ` Florian v. Savigny
       [not found]   ` <mailman.283.1369339299.22516.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 6+ messages in thread
From: Florian v. Savigny @ 2013-05-23 20:01 UTC (permalink / raw)
  To: Sean McAfee; +Cc: help-gnu-emacs


Hi Sean,

thanks so much for being so helpful!

  > I don't think so, but it's easy to write one:

You call that easy ... I'm wondering how long it took you? In any
case, I honestly admire you. (It only ever LOOKS easy when I try, but
it always ends up being complicated.)

  > (defun versioned-filename-lessp (a b)
  >   (labels ((parse (str)

I was wondering about these two functions, "labels" and "parse", but
emacs does not seem to know them either. Are you assuming that some
library is loaded, maybe? (I am using Emacs 24.3.1.)

Best regards, and thanks again,

Florian







^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there a function to sort file names by file version number, like "ls -v"?
       [not found]   ` <mailman.283.1369339299.22516.help-gnu-emacs@gnu.org>
@ 2013-05-23 20:18     ` Pascal J. Bourguignon
  2013-05-23 20:28     ` Sean McAfee
  1 sibling, 0 replies; 6+ messages in thread
From: Pascal J. Bourguignon @ 2013-05-23 20:18 UTC (permalink / raw)
  To: help-gnu-emacs

florian@fsavigny.de (Florian v. Savigny) writes:

> Hi Sean,
>
> thanks so much for being so helpful!
>
>   > I don't think so, but it's easy to write one:
>
> You call that easy ... I'm wondering how long it took you? In any
> case, I honestly admire you. (It only ever LOOKS easy when I try, but
> it always ends up being complicated.)
>
>   > (defun versioned-filename-lessp (a b)
>   >   (labels ((parse (str)
>
> I was wondering about these two functions, "labels" and "parse", but
> emacs does not seem to know them either. Are you assuming that some
> library is loaded, maybe? (I am using Emacs 24.3.1.)

Sure: (require 'cl) should be the first line of all ~/.emacs files.

-- 
__Pascal Bourguignon__                     http://www.informatimago.com/
A bad day in () is better than a good day in {}.
You can take the lisper out of the lisp job, but you can't take the lisp out
of the lisper (; -- antifuchs


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there a function to sort file names by file version number, like "ls -v"?
       [not found]   ` <mailman.283.1369339299.22516.help-gnu-emacs@gnu.org>
  2013-05-23 20:18     ` Pascal J. Bourguignon
@ 2013-05-23 20:28     ` Sean McAfee
  2013-05-24 15:07       ` Florian v. Savigny
  1 sibling, 1 reply; 6+ messages in thread
From: Sean McAfee @ 2013-05-23 20:28 UTC (permalink / raw)
  To: help-gnu-emacs

florian@fsavigny.de (Florian v. Savigny) writes:
> You call that easy ... I'm wondering how long it took you?

Oh, maybe 5-10 minutes, including testing.  I've written similar
routines in other languages before, which sped things up a bit.

>   > (defun versioned-filename-lessp (a b)
>   >   (labels ((parse (str)
>
> I was wondering about these two functions, "labels" and "parse", but
> emacs does not seem to know them either. Are you assuming that some
> library is loaded, maybe? (I am using Emacs 24.3.1.)

labels is a macro that defines local functions, in this case one called
"parse".  See:

http://www.gnu.org/software/emacs/manual/html_node/cl/Obsolete-Macros.html

(Apparently I ought to be using cl-labels instead of labels now.)

It lives in the "cl" (Common Lisp) package, and is loaded when first
used.  You can evaluate the expression (require 'cl) to pull in the
package explicitly.

If it's not clear, the parse routine I defined takes a filename as an
argument, and returns a cons cell that contains the decomposed contents
of the name.  A name with a tilde-delimited version suffix, like say
"foo~5~", is transformed into ("foo" . 5), and a name without such a
suffix, like say "bar", becomes just ("bar" . nil).  The main body of
versioned-filename-lessp just compares two such decompositions.


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: Is there a function to sort file names by file version number, like "ls -v"?
  2013-05-23 20:28     ` Sean McAfee
@ 2013-05-24 15:07       ` Florian v. Savigny
  0 siblings, 0 replies; 6+ messages in thread
From: Florian v. Savigny @ 2013-05-24 15:07 UTC (permalink / raw)
  To: Sean McAfee; +Cc: help-gnu-emacs


Hi Sean,

  > If it's not clear, the parse routine I defined takes a filename as
  > an argument, and returns a cons cell that contains the decomposed
  > contents of the name.  A name with a tilde-delimited version
  > suffix, like say "foo~5~", is transformed into ("foo" . 5), and a
  > name without such a suffix, like say "bar", becomes just ("bar"
  > . nil).  The main body of versioned-filename-lessp just compares
  > two such decompositions.

Sorry ... before I read that, I had already had edebug-defun walk me
through the process, which showed me without thinking how it
works. Thanks very much - you actually tought me a /general/ method
for sorting things!

Best regards,

Florian




^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2013-05-24 15:07 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-05-23  8:21 Is there a function to sort file names by file version number, like "ls -v"? Florian v. Savigny
     [not found] <mailman.229.1369297314.22516.help-gnu-emacs@gnu.org>
2013-05-23 18:33 ` Sean McAfee
2013-05-23 20:01   ` Florian v. Savigny
     [not found]   ` <mailman.283.1369339299.22516.help-gnu-emacs@gnu.org>
2013-05-23 20:18     ` Pascal J. Bourguignon
2013-05-23 20:28     ` Sean McAfee
2013-05-24 15:07       ` Florian v. Savigny

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.