From mboxrd@z Thu Jan 1 00:00:00 1970 From: =?UTF-8?q?Sebastian=20Reu=C3=9Fe?= Subject: [PATCH] Fix alphabetic sorting for headlines, tags Date: Mon, 12 Feb 2018 09:46:04 +0100 Message-ID: <20180212084604.30122-1-seb@wirrsal.net> References: <87bmgvbiiq.fsf@nicolasgoaziou.fr> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:49763) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1el9kc-0004i2-G4 for emacs-orgmode@gnu.org; Mon, 12 Feb 2018 03:46:15 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1el9kZ-0004lO-O5 for emacs-orgmode@gnu.org; Mon, 12 Feb 2018 03:46:14 -0500 Received: from wirrsal.net ([188.68.36.149]:54374 helo=mail.wirrsal.net) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1el9kZ-0004kO-HA for emacs-orgmode@gnu.org; Mon, 12 Feb 2018 03:46:11 -0500 In-Reply-To: <87bmgvbiiq.fsf@nicolasgoaziou.fr> List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: mail@nicolasgoaziou.fr Cc: emacs-orgmode@gnu.org, =?UTF-8?q?Sebastian=20Reu=C3=9Fe?= * org.el (org-sort-entries): Use collated sorting. (org-tags-sort-function): Use collated sorting. (org-string-collate-greaterp): Add helper-function to use as defcustom option, since there is no =E2=80=98string-collate-greaterp=E2=80=99 in Em= acs. * org-compat.el (org-string-collate-lessp): Add proxy to fall-back on string-lessp when string-collate-lessp is missing (Emacs =E2=89=A4 24). * test-org.el (test-org/string-collate-lessp): Add test. (test-org/sort-entries): Add regression test for non-ASCII inputs. =E2=80=98org-sort-entries=E2=80=99 and =E2=80=98org-tags-sort-function=E2= =80=99 advertise alphabetic sorting, but actually sort based only on character code. This produces non-alphabetic orderings of strings in non-ASCII locales. E.=E2=80=AFg., German Umlauts =E2=80=9C=C3=84 =C3=9C =C3=96=E2=80=9D are = alphabetically sorted as if they were =E2=80=9CA U O=E2=80=9D, whereas sorting based on character-code wil= l place them after =E2=80=9CZ=E2=80=9D, which is unexpected. --- etc/ORG-NEWS | 5 +++++ lisp/org-compat.el | 6 ++++++ lisp/org.el | 12 +++++++++--- testing/lisp/test-org.el | 21 +++++++++++++++++++++ 4 files changed, 41 insertions(+), 3 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index aedede201..9c12f8e2a 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -239,6 +239,11 @@ of these exporters will properly export to =3Dirc:=3D= links, which will open properly in irc clients from web browsers. =20 *** ~org-comment-dwim~ (bound to =3DM-;=3D) now comments headings, if po= int is on a heading +*** Alphabetic sorting in headings and tags now uses the locale=E2=80=99= s sorting rules + +When sorting alphabetically, ~org-sort-entries~ and +~org-tags-sort-function~ now sort according to the locale=E2=80=99s coll= ation +rules instead of by code-point. * Version 9.1 =20 ** Incompatible changes diff --git a/lisp/org-compat.el b/lisp/org-compat.el index 2553286e1..acd5c3e1e 100644 --- a/lisp/org-compat.el +++ b/lisp/org-compat.el @@ -118,6 +118,12 @@ (defvar org-table1-hline-regexp) (push (expand-file-name file dir) files))))) (nconc result (nreverse files))))) =20 +;; `string-collate-lessp' is new in Emacs 25. +(defalias 'org-string-collate-lessp + (if (fboundp 'string-collate-lessp) + 'string-collate-lessp + 'string-lessp)) + =0C ;;; Obsolete aliases (remove them after the next major release). =20 diff --git a/lisp/org.el b/lisp/org.el index 688e48bcc..fbbeea80f 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -3558,8 +3558,8 @@ (defcustom org-tags-sort-function nil :group 'org-tags :type '(choice (const :tag "No sorting" nil) - (const :tag "Alphabetical" string<) - (const :tag "Reverse alphabetical" string>) + (const :tag "Alphabetical" org-string-collate-lessp) + (const :tag "Reverse alphabetical" org-string-collate-greaterp) (function :tag "Custom function" nil))) =20 (defvar org-tags-history nil @@ -8803,7 +8803,7 @@ (defun org-sort-entries (t (error "Invalid sorting type `%c'" sorting-type)))) nil (cond - ((=3D dcst ?a) 'string<) + ((=3D dcst ?a) 'org-string-collate-lessp) ((=3D dcst ?f) (or compare-func (and interactive? @@ -8913,6 +8913,12 @@ (defun org-context-p (&rest contexts) (org-in-item-p))) (goto-char pos)))) =20 +;; Defined to provide a value for defcustom, since there is no +;; string-collate-greaterp in Emacs. +(defun org-string-collate-greaterp (s1 s2) + "Return non-nil if S1 is greater than S2 in collation order." + (not (org-string-collate-lessp s1 s2))) + ;;;###autoload (defun org-run-like-in-org-mode (cmd) "Run a command, pretending that the current buffer is in Org mode. diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el index cb21cda47..dcf097e69 100644 --- a/testing/lisp/test-org.el +++ b/testing/lisp/test-org.el @@ -2737,6 +2737,22 @@ (org-test-with-temp-text "\n* def\n* xyz\n* abc\n" (org-sort-entries nil ?A) (buffer-string)))) + ;; Sort alphabetically (with non-ASCII input). Rebinds `string-collate= -lessp' + ;; to enforce a canonical locale during testing. + (let ((original-string-collate-lessp (symbol-function 'string-collate-= lessp))) + (cl-letf (((symbol-function 'string-collate-lessp) + (lambda (s1 s2) (funcall original-string-collate-lessp + s1 s2 "en_US.utf-8")))) + (should + (equal "\n* =C3=A4a\n* ab\n* z\n" + (org-test-with-temp-text "\n* ab\n* z\n* =C3=A4a\n" + (org-sort-entries nil ?a) + (buffer-string)))) + (should + (equal "\n* z\n* =C3=A4b\n* aa\n" + (org-test-with-temp-text "\n* =C3=A4b\n* z\n* aa\n" + (org-sort-entries nil ?A) + (buffer-string)))))) ;; Sort numerically. (should (equal "\n* 1\n* 2\n* 10\n" @@ -2927,6 +2943,11 @@ (org-sort-entries nil ?a) (buffer-string))))) =20 +(ert-deftest test-org/string-collate-greaterp () + "Test `org-string-collate-greaterp' specifications." + (should (org-string-collate-greaterp "def" "abc")) + (should-not (org-string-collate-greaterp "abc" "def"))) + (ert-deftest test-org/file-contents () "Test `org-file-contents' specifications." ;; Open files. --=20 2.16.1