summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSebastian Reuße <seb@wirrsal.net>2018-02-12 09:46:04 +0100
committerNicolas Goaziou <mail@nicolasgoaziou.fr>2018-02-12 14:57:54 +0100
commit551d2f1fe7ad797806ed51c63d45553ef1d6a777 (patch)
treeb297199ce8f48a7e99ee089f5c2ac6077e88e377
parentb505a5b6ad9d12ddb3b3c9a73962eaf5e6a9fa41 (diff)
downloadorg-mode-551d2f1fe7ad797806ed51c63d45553ef1d6a777.tar.gz
Fix alphabetic sorting for headlines, tags
* 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 ‘string-collate-greaterp’ in Emacs. * org-compat.el (org-string-collate-lessp): Add proxy to fall-back on string-lessp when string-collate-lessp is missing (Emacs ≤ 24). * test-org.el (test-org/string-collate-lessp): Add test. (test-org/sort-entries): Add regression test for non-ASCII inputs. ‘org-sort-entries’ and ‘org-tags-sort-function’ advertise alphabetic sorting, but actually sort based only on character code. This produces non-alphabetic orderings of strings in non-ASCII locales. E. g., German Umlauts “Ä Ü Ö” are alphabetically sorted as if they were “A U O”, whereas sorting based on character-code will place them after “Z”, which is unexpected.
-rw-r--r--etc/ORG-NEWS5
-rw-r--r--lisp/org-compat.el6
-rw-r--r--lisp/org.el12
-rw-r--r--testing/lisp/test-org.el22
4 files changed, 42 insertions, 3 deletions
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index cbd3a47..0c71efa 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -243,6 +243,11 @@ of these exporters will properly export to =irc:= links, which will
open properly in irc clients from web browsers.
*** ~org-comment-dwim~ (bound to =M-;=) now comments headings, if point is on a heading
+*** Alphabetic sorting in headings and tags now uses the locale’s sorting rules
+
+When sorting alphabetically, ~org-sort-entries~ and
+~org-tags-sort-function~ now sort according to the locale’s collation
+rules instead of by code-point.
* Version 9.1
** Incompatible changes
diff --git a/lisp/org-compat.el b/lisp/org-compat.el
index 2553286..acd5c3e 100644
--- a/lisp/org-compat.el
+++ b/lisp/org-compat.el
@@ -118,6 +118,12 @@ output directories whose names match REGEXP."
(push (expand-file-name file dir) files)))))
(nconc result (nreverse files)))))
+;; `string-collate-lessp' is new in Emacs 25.
+(defalias 'org-string-collate-lessp
+ (if (fboundp 'string-collate-lessp)
+ 'string-collate-lessp
+ 'string-lessp))
+
;;; Obsolete aliases (remove them after the next major release).
diff --git a/lisp/org.el b/lisp/org.el
index 5abf1e3..7f0f11f 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -3558,8 +3558,8 @@ is better to limit inheritance to certain tags using the variables
: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)))
(defvar org-tags-history nil
@@ -8791,7 +8791,7 @@ function is being called interactively."
(t (error "Invalid sorting type `%c'" sorting-type))))
nil
(cond
- ((= dcst ?a) 'string<)
+ ((= dcst ?a) 'org-string-collate-lessp)
((= dcst ?f)
(or compare-func
(and interactive?
@@ -8901,6 +8901,12 @@ Possible values in the list of contexts are `table', `headline', and `item'."
(org-in-item-p)))
(goto-char pos))))
+;; 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 98f465a..a108b6c 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -2745,6 +2745,23 @@ http://article.gmane.org/gmane.emacs.orgmode/21459/"
(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 "C"))))
+ (should
+ (equal "\n* ¥\n* §\n"
+ (org-test-with-temp-text "\n* §\n* ¥"
+ (org-sort-entries nil ?a)
+ (buffer-string))))
+ (should
+ (equal "\n* §\n* ¥\n"
+ (org-test-with-temp-text "\n* §\n* ¥"
+ (org-sort-entries nil ?A)
+ (buffer-string))))))
;; Sort numerically.
(should
(equal "\n* 1\n* 2\n* 10\n"
@@ -2935,6 +2952,11 @@ SCHEDULED: <2017-05-06 Sat>
(org-sort-entries nil ?a)
(buffer-string)))))
+(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.