unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Michael Heerdegen <michael_heerdegen@web.de>
To: emacs-devel@gnu.org
Subject: Re: sort-lines including non ASCII
Date: Fri, 08 Jul 2016 00:55:26 +0200	[thread overview]
Message-ID: <87mvltdpnl.fsf@web.de> (raw)
In-Reply-To: 83furljkdb.fsf@gnu.org

[-- Attachment #1: Type: text/plain, Size: 275 bytes --]

Eli Zaretskii <eliz@gnu.org> 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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Make-sort-lines-accept-a-predicate.patch --]
[-- Type: text/x-diff, Size: 1937 bytes --]

From 6229d19438d641d3ec81dec962984b3a9f5f72e7 Mon Sep 17 00:00:00 2001
From: Michael Heerdegen <michael_heerdegen@web.de>
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)))
 			     'car-less-than-car)
 			    ((consp (car (car sort-lists)))
@@ -197,7 +197,7 @@ sort-reorder-buffer
 	(delete-region max (1+ max))))))
 
 ;;;###autoload
-(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))))))))))
 
 ;;;###autoload
 (defun sort-paragraphs (reverse beg end)
-- 
2.8.1


[-- Attachment #3: Type: text/plain, Size: 440 bytes --]


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?


Michael.

  reply	other threads:[~2016-07-07 22:55 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-07-05 20:58 sort-lines including non ASCII Uwe Brauer
2016-07-05 21:57 ` Óscar Fuentes
2016-07-07  7:35   ` Uwe Brauer
2016-07-06 14:34 ` Eli Zaretskii
2016-07-06 14:52   ` Michael Heerdegen
2016-07-07  7:34     ` Uwe Brauer
2016-07-07 15:17       ` Eli Zaretskii
2016-07-07 16:30         ` Michael Heerdegen
2016-07-07 16:56           ` Eli Zaretskii
2016-07-07 17:32             ` Michael Heerdegen
2016-07-07 19:53               ` Eli Zaretskii
2016-07-07 22:55                 ` Michael Heerdegen [this message]
2016-07-08 10:01                   ` Eli Zaretskii
2016-07-14 21:10                     ` Michael Heerdegen
2016-07-14 21:14                       ` Clément Pit--Claudel
2016-07-14 21:19                       ` Noam Postavsky
2016-07-14 21:26                         ` Michael Heerdegen
2016-07-14 21:57                           ` Noam Postavsky
2016-07-08 13:40               ` Richard Stallman
2016-07-08 14:36                 ` Michael Heerdegen
2016-07-09 16:58                   ` Richard Stallman
2016-07-12 23:06                     ` John Wiegley
2016-07-07  7:41   ` Uwe Brauer
2016-07-07 15:20     ` Eli Zaretskii
2016-07-07 16:13       ` Uwe Brauer
2016-07-07 16:35         ` Eli Zaretskii
2016-07-07  8:23 ` Teemu Likonen
2016-07-07 15:23   ` Eli Zaretskii
2016-07-08  4:17     ` Teemu Likonen
2016-07-08  6:32       ` Eli Zaretskii
2016-07-08  6:36         ` Eli Zaretskii
2016-07-08  6:50           ` Teemu Likonen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87mvltdpnl.fsf@web.de \
    --to=michael_heerdegen@web.de \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).