help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: Sorting buffer with string-collate-lessp


From: Tassilo Horn
Subject: Re: Sorting buffer with string-collate-lessp
Date: Tue, 26 May 2015 16:49:27 +0200
User-agent: Gnus/5.130014 (Ma Gnus v0.14) Emacs/25.0.50 (gnu/linux)

Rasmus <rasmus@gmx.us> writes:

Hi Rasmus,

> How can I easily sort a buffer using string-collate-lessp?
>
> Info: I would like to sort a buffer using string-collate-lessp (line
> by line).  Sort-lines is the obvious candidate but it uses string<.  I
> tried to write my own sort-lines using sort-subr, as it has a
> predicate argument.  However, for buffers, it needs something like
> compare-buffer-substrings, which takes no predicate and is in the
> C-level and pretty long.
>
> I could write a wrapper that convert each buffer-chunk into its
> buffer-substring first, and then compares it with
> string-collate-lessp, I guess, but that seems like a lot of boiler
> plate.  So maybe a better solution exists?

I think you can use `cl-left' to temporarily change the definition of
`string<' to `string-collate-lessp', so this should work in theory.

--8<---------------cut here---------------start------------->8---
(cl-letf (((symbol-function 'string<) #'string-collate-lessp))
  (sort-lines nil (point-min) (point-max)))
--8<---------------cut here---------------end--------------->8---

However, I've tried it with (lambda (a b) (string-collate-lessp b a))
which should use `string-collate-lessp' and sort in reverse (note the
switched arguments) and that didn't change anything.

So either my `cl-letf' usage is wrong or `sort-lines' doesn't really use
`string<'.  (Actually, `string<' is an alias to `string-lessp', so I
also tried changing that accordingly, but still no effect...)

Bye,
Tassilo



reply via email to

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