[Top][All Lists]

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

Re: sort-lines including non ASCII

From: Michael Heerdegen
Subject: Re: sort-lines including non ASCII
Date: Fri, 08 Jul 2016 00:55:26 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.95 (gnu/linux)

Eli Zaretskii <address@hidden> writes:

> > BTW, a relevant question is: Is `compare-buffer-substring' faster than
> > `buffer-substring'+`string<'?
> Hard to say.  Measuring is the easiest way to answer that.

Here is a first try.  The speed difference is negligible here.

>From 6229d19438d641d3ec81dec962984b3a9f5f72e7 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <address@hidden>
Date: Fri, 8 Jul 2016 00:46:20 +0200
Subject: [PATCH] Make sort-lines accept a predicate

 lisp/sort.el | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/lisp/sort.el b/lisp/sort.el
index 4d7311f..266b916 100644
--- a/lisp/sort.el
+++ b/lisp/sort.el
@@ -1,4 +1,4 @@
-;;; sort.el --- commands to sort text in an Emacs buffer
+;;; sort.el --- commands to sort text in an Emacs buffer  -*- lexical-binding 
: t -*-
 ;; Copyright (C) 1986-1987, 1994-1995, 2001-2016 Free Software
 ;; Foundation, Inc.
@@ -99,7 +99,7 @@ sort-subr
          (setq sort-lists
                (sort sort-lists
                      (cond (predicate
-                            `(lambda (a b) (,predicate (car a) (car b))))
+                            (lambda (a b) (funcall predicate (car a) (car b))))
                            ((numberp (car (car sort-lists)))
                            ((consp (car (car sort-lists)))
@@ -197,7 +197,7 @@ sort-reorder-buffer
        (delete-region max (1+ max))))))
-(defun sort-lines (reverse beg end)
+(defun sort-lines (reverse beg end &optional predicate)
   "Sort lines in region alphabetically; argument means descending order.
 Called from a program, there are three arguments:
 REVERSE (non-nil means reverse order), BEG and END (region to sort).
@@ -210,7 +210,13 @@ sort-lines
       (goto-char (point-min))
       (let ;; To make `end-of-line' and etc. to ignore fields.
          ((inhibit-field-text-motion t))
-       (sort-subr reverse 'forward-line 'end-of-line)))))
+       (sort-subr
+         reverse #'forward-line #'end-of-line nil nil
+         (and predicate
+              (lambda (a b)
+                (funcall predicate
+                         (buffer-substring (car a) (cdr a))
+                         (buffer-substring (car b) (cdr b))))))))))
 (defun sort-paragraphs (reverse beg end)

I had to convert the file to lexical binding to avoid a quoted lambda,
or else we had been forbidden to name the optional argument "predicate"
(variable name clash).

> My opinion is the opposite: I think it's more important to have a
> command that could collate-order strings according to a user-specified
> locale, than make sort-lines more flexible on the Lisp level.

What would you do?  Just create an additional command?


reply via email to

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