emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] org-sort-entries errors out when headline is empty
@ 2018-03-13 16:51 Sebastian Reuße
  2018-03-13 16:51 ` [PATCH 1/2] Extend org-sort-entries test Sebastian Reuße
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Sebastian Reuße @ 2018-03-13 16:51 UTC (permalink / raw)
  To: emacs-orgmode

So, I’ve noticed that when sorting a subtree (alphabetically or numerically),
‘org-sort-entries’ will raise an error arising from the call to
‘org-sort-remove-invisible’ (lines 8761 and 8766 in bc7b24d0d).

I’m following up with a test. The error is triggered by the capture group in
‘org-complex-heading-regexp’ returning nil, which is then passed on to
‘org-sort-remove-invisible’, which expects a string and chokes on the nil.

As for a fix, the quick fix would be guarding against this case in
‘org-sort-remove-invisible’, but that seemed a bit meh since sanitation should
probably be handled higher up.

An alternative is to guard inside ‘org-sort-entries’. I’m following up with an
implementation of this.

I was also wondering if it might make sense to tweak
‘org-complex-heading-regexp’ so that the respective capture group captures an
empty string. This could be achieved by making the title group greedy again
(reverting 9d334897e). Whether we want this probably depends on the motivation
for 9d334897e. However, doing so would be more consistent with the org-elements
API, which does assign a tag list to the title property when the heading is
otherwise empty.

Any thoughts?

Kind regards,
SR

-- 
Insane cobra split the wood
Trader of the lowland breed
Call a jittney, drive away
In the slipstream we will stay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 1/2] Extend org-sort-entries test
  2018-03-13 16:51 [BUG] org-sort-entries errors out when headline is empty Sebastian Reuße
@ 2018-03-13 16:51 ` Sebastian Reuße
  2018-03-13 16:51 ` [PATCH 2/2] Guard against empty headings when sorting Sebastian Reuße
  2018-03-20  8:53 ` [BUG] org-sort-entries errors out when headline is empty Nicolas Goaziou
  2 siblings, 0 replies; 6+ messages in thread
From: Sebastian Reuße @ 2018-03-13 16:51 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Sebastian Reuße

* test-org.el (test-org/sort-entries): Test with empty headings.
---
 testing/lisp/test-org.el | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 33769aafa..8c98fce4d 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -2745,6 +2745,11 @@
 	  (org-test-with-temp-text "\n* def\n* xyz\n* abc\n"
 	    (org-sort-entries nil ?A)
 	    (buffer-string))))
+  (should
+   (equal "\n* \n* klm\n* xyz\n"
+	  (org-test-with-temp-text "\n* xyz\n* \n* klm\n"
+	    (org-sort-entries nil ?a)
+	    (buffer-string))))
   ;; Sort numerically.
   (should
    (equal "\n* 1\n* 2\n* 10\n"
@@ -2756,6 +2761,11 @@
 	  (org-test-with-temp-text "\n* 10\n* 1\n* 2\n"
 	    (org-sort-entries nil ?N)
 	    (buffer-string))))
+  (should
+   (equal "\n* \n* 1\n* 2\n"
+	  (org-test-with-temp-text "\n* 1\n* \n* 2\n"
+	    (org-sort-entries nil ?n)
+	    (buffer-string))))
   ;; Sort by custom function.
   (should
    (equal "\n* b\n* aa\n* ccc\n"
-- 
2.16.2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH 2/2] Guard against empty headings when sorting
  2018-03-13 16:51 [BUG] org-sort-entries errors out when headline is empty Sebastian Reuße
  2018-03-13 16:51 ` [PATCH 1/2] Extend org-sort-entries test Sebastian Reuße
@ 2018-03-13 16:51 ` Sebastian Reuße
  2018-03-20  8:53 ` [BUG] org-sort-entries errors out when headline is empty Nicolas Goaziou
  2 siblings, 0 replies; 6+ messages in thread
From: Sebastian Reuße @ 2018-03-13 16:51 UTC (permalink / raw)
  To: emacs-orgmode; +Cc: Sebastian Reuße

* org.el (org-sort-entries): Guard against empty headings when sorting
alphabetically, numerically.

Due to how ‘org-complex-heading-regexp’ is defined, the title capture group
currently returns nil in empty headings, which we don’t want to pass on to
‘org-sort-remove-invisible’.
---
 lisp/org.el | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/lisp/org.el b/lisp/org.el
index 76bc60c88..07203c9e1 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -8756,15 +8756,17 @@ (defun org-sort-entries
 	  (lambda ()
 	    (cond
 	     ((= dcst ?n)
-	      (if (looking-at org-complex-heading-regexp)
-		  (string-to-number
-		   (org-sort-remove-invisible (match-string 4)))
-		nil))
+	      (let ((heading (and (looking-at org-complex-heading-regexp)
+				  (match-string 4))))
+		(if heading
+		    (string-to-number (org-sort-remove-invisible heading))
+		  0)))
 	     ((= dcst ?a)
-	      (if (looking-at org-complex-heading-regexp)
-		  (funcall case-func
-			   (org-sort-remove-invisible (match-string 4)))
-		nil))
+	      (let ((heading (and (looking-at org-complex-heading-regexp)
+				  (match-string 4))))
+		(if heading
+		    (funcall case-func (org-sort-remove-invisible heading))
+		  "")))
 	     ((= dcst ?k)
 	      (or (get-text-property (point) :org-clock-minutes) 0))
 	     ((= dcst ?t)
-- 
2.16.2

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [BUG] org-sort-entries errors out when headline is empty
  2018-03-13 16:51 [BUG] org-sort-entries errors out when headline is empty Sebastian Reuße
  2018-03-13 16:51 ` [PATCH 1/2] Extend org-sort-entries test Sebastian Reuße
  2018-03-13 16:51 ` [PATCH 2/2] Guard against empty headings when sorting Sebastian Reuße
@ 2018-03-20  8:53 ` Nicolas Goaziou
  2018-03-23 10:43   ` Sebastian Reuße
  2 siblings, 1 reply; 6+ messages in thread
From: Nicolas Goaziou @ 2018-03-20  8:53 UTC (permalink / raw)
  To: Sebastian Reuße; +Cc: emacs-orgmode

Hello,

Sebastian Reuße <seb@wirrsal.net> writes:

> An alternative is to guard inside ‘org-sort-entries’. I’m following up with an
> implementation of this.

Applied. Thank you!

I used `org-get-heading', which handles empty headlines without messing
with `org-complex-heading-regexp'.

> I was also wondering if it might make sense to tweak
> ‘org-complex-heading-regexp’ so that the respective capture group captures an
> empty string. This could be achieved by making the title group greedy again
> (reverting 9d334897e). Whether we want this probably depends on the motivation
> for 9d334897e. However, doing so would be more consistent with the org-elements
> API, which does assign a tag list to the title property when the heading is
> otherwise empty.

How should we treat the following:

  * :something:

Is it an empty headline with a "something" tag, or a ":something:"
headline with no tag? All things being equal, it doesn't matter much,
whichever answer is chosen, we can get the other one with a zero-width
space or some such.

However, it is too early to answer this, because tags are handled
inconsistently across the code base. See, e.g., the number of
occurrences of ":[[:alnum:]_@#%:]+:" in "org.el". The tags regexp is
hard-coded in too many places. This is why, even though 9d334897e was an
answer to the initial question, it is incomplete. Reverting it would be
as incomplete.

IMO, we should first clarify tags definition. There's already
`org-get-tags-at' and `org-get-local-tags' (which could be optimized,
BTW), but we may need a proper tags-regexp, e.g.,

  (concat org-outline-regexp-bol ".*[ \t]\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")

or use consistently `org-get-tags-at'.

Then we can answer the initial question.

WDYT?

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [BUG] org-sort-entries errors out when headline is empty
  2018-03-20  8:53 ` [BUG] org-sort-entries errors out when headline is empty Nicolas Goaziou
@ 2018-03-23 10:43   ` Sebastian Reuße
  2018-04-20  9:30     ` Nicolas Goaziou
  0 siblings, 1 reply; 6+ messages in thread
From: Sebastian Reuße @ 2018-03-23 10:43 UTC (permalink / raw)
  To: Nicolas Goaziou; +Cc: emacs-orgmode

Hello Nicolas,

Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:

> Sebastian Reuße <seb@wirrsal.net> writes:

>> I was also wondering if it might make sense to tweak
>> ‘org-complex-heading-regexp’ so that the respective capture group
>> captures an empty string. This could be achieved by making the title
>> group greedy again (reverting 9d334897e). Whether we want this
>> probably depends on the motivation for 9d334897e. However, doing so
>> would be more consistent with the org-elements API, which does assign
>> a tag list to the title property when the heading is otherwise empty.

> How should we treat the following:
>
>   * :something:
>
> Is it an empty headline with a "something" tag, or a ":something:"
> headline with no tag? All things being equal, it doesn't matter much,
> whichever answer is chosen, we can get the other one with a zero-width
> space or some such.

As for my 5 cents on the (probably not too consequential) design issue:
I’d probably choose to treat this as an empty headline with tags. Due to
the special syntax, tags are “more special” than headlines, and my
expectation would be that most folks would find it consistent to treat
them as tags in this case as well. In practical terms, a heading
enclosed in colons is probably a rarer thing to wind up with in an org
file than an empty heading with tags. But that’s just IMHO.

I agree it would be great if this were treated consistently all over
the codebase.

Kind regards,
SR

-- 
Insane cobra split the wood
Trader of the lowland breed
Call a jittney, drive away
In the slipstream we will stay

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [BUG] org-sort-entries errors out when headline is empty
  2018-03-23 10:43   ` Sebastian Reuße
@ 2018-04-20  9:30     ` Nicolas Goaziou
  0 siblings, 0 replies; 6+ messages in thread
From: Nicolas Goaziou @ 2018-04-20  9:30 UTC (permalink / raw)
  To: Sebastian Reuße; +Cc: emacs-orgmode

Hello,

Sebastian Reuße <seb@wirrsal.net> writes:

> As for my 5 cents on the (probably not too consequential) design issue:
> I’d probably choose to treat this as an empty headline with tags. Due to
> the special syntax, tags are “more special” than headlines, and my
> expectation would be that most folks would find it consistent to treat
> them as tags in this case as well. In practical terms, a heading
> enclosed in colons is probably a rarer thing to wind up with in an org
> file than an empty heading with tags. But that’s just IMHO.
>
> I agree it would be great if this were treated consistently all over
> the codebase.

I think tag handling is now consistent all over the code base.

Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2018-04-20  9:30 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-13 16:51 [BUG] org-sort-entries errors out when headline is empty Sebastian Reuße
2018-03-13 16:51 ` [PATCH 1/2] Extend org-sort-entries test Sebastian Reuße
2018-03-13 16:51 ` [PATCH 2/2] Guard against empty headings when sorting Sebastian Reuße
2018-03-20  8:53 ` [BUG] org-sort-entries errors out when headline is empty Nicolas Goaziou
2018-03-23 10:43   ` Sebastian Reuße
2018-04-20  9:30     ` Nicolas Goaziou

Code repositories for project(s) associated with this inbox:

	https://git.savannah.gnu.org/cgit/emacs/org-mode.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).