Browse Source

Remove hidden links when comparing items or entries.

* org.el (org-sort-remove-invisible): Add a docstring.
(org-sort-entries): Remove hidden links when comparing
entries.

* org-list.el (org-sort-list): Remove hidden links when
comparing list items.

Thanks to François Pinard for suggesting this.
Bastien Guerry 6 years ago
parent
commit
49772f1f26
2 changed files with 18 additions and 8 deletions
  1. 10 4
      lisp/org-list.el
  2. 8 4
      lisp/org.el

+ 10 - 4
lisp/org-list.el

@@ -2786,7 +2786,10 @@ If the SORTING-TYPE is ?f or ?F, then GETKEY-FUNC specifies
 a function to be called with point at the beginning of the
 record.  It must return either a string or a number that should
 serve as the sorting key for that record.  It will then use
-COMPARE-FUNC to compare entries."
+COMPARE-FUNC to compare entries.
+
+Sorting is done against the visible part of the headlines, it
+ignores hidden links."
   (interactive "P")
   (let* ((case-func (if with-case 'identity 'downcase))
          (struct (org-list-struct))
@@ -2826,11 +2829,14 @@ COMPARE-FUNC to compare entries."
 		(when (looking-at "[ \t]*[-+*0-9.)]+\\([ \t]+\\[[- X]\\]\\)?[ \t]+")
 		  (cond
 		   ((= dcst ?n)
-		    (string-to-number (buffer-substring (match-end 0)
-							(point-at-eol))))
+		    (string-to-number
+		     (org-sort-remove-invisible
+		      (buffer-substring (match-end 0) (point-at-eol)))))
 		   ((= dcst ?a)
 		    (funcall case-func
-			     (buffer-substring (match-end 0) (point-at-eol))))
+			     (org-sort-remove-invisible
+			      (buffer-substring
+			       (match-end 0) (point-at-eol)))))
 		   ((= dcst ?t)
 		    (cond
 		     ;; If it is a timer list, convert timer to seconds

+ 8 - 4
lisp/org.el

@@ -8320,6 +8320,7 @@ Optional argument WITH-CASE means sort case-sensitively."
     (org-call-with-arg 'org-sort-entries with-case))))
 
 (defun org-sort-remove-invisible (s)
+  "Remove invisible links from string S."
   (remove-text-properties 0 (length s) org-rm-props s)
   (while (string-match org-bracket-link-regexp s)
     (setq s (replace-match (if (match-end 2)
@@ -8343,7 +8344,7 @@ Else, if the cursor is before the first entry, sort the top-level items.
 Else, the children of the entry at point are sorted.
 
 Sorting can be alphabetically, numerically, by date/time as given by
-a time stamp, by a property or by priority.
+a time stamp, by a property, by priority order, or by a custom function.
 
 The command prompts for the sorting type unless it has been given to the
 function through the SORTING-TYPE argument, which needs to be a character,
@@ -8369,7 +8370,10 @@ called with point at the beginning of the record.  It must return either
 a string or a number that should serve as the sorting key for that record.
 
 Comparing entries ignores case by default.  However, with an optional argument
-WITH-CASE, the sorting considers case as well."
+WITH-CASE, the sorting considers case as well.
+
+Sorting is done against the visible part of the headlines, it ignores hidden
+links."
   (interactive "P")
   (let ((case-func (if with-case 'identity 'downcase))
 	(cmstr
@@ -8481,11 +8485,11 @@ WITH-CASE, the sorting considers case as well."
 	   (cond
 	    ((= dcst ?n)
 	     (if (looking-at org-complex-heading-regexp)
-		 (string-to-number (match-string 4))
+		 (string-to-number (org-sort-remove-invisible (match-string 4)))
 	       nil))
 	    ((= dcst ?a)
 	     (if (looking-at org-complex-heading-regexp)
-		 (funcall case-func (match-string 4))
+		 (funcall case-func (org-sort-remove-invisible (match-string 4)))
 	       nil))
 	    ((= dcst ?t)
 	     (let ((end (save-excursion (outline-next-heading) (point))))