From: Brice Waegeneire <brice@waegenei.re>
To: emacs-orgmode@gnu.org
Subject: [PATCH] Add NO-STATS switch to org-get-heading
Date: Fri, 13 Mar 2020 16:11:45 +0100 [thread overview]
Message-ID: <20200313151145.29469-1-brice@waegenei.re> (raw)
* lisp/org-element.el (org-element-context): Handle headlines only
containing a statistics cookie.
* lisp/org.el (test-org/get-heading): Add regex capture group 6 for
statistics cookie.
(org-get-heading): Add NO-STATS argument, if
non-nil, will not return the statistics cookie.
* testing/lisp/test-org.el (test-org/get-heading): Add 3 tests using the
NO-STATS argument.
---
lisp/org-element.el | 4 ++--
lisp/org.el | 13 +++++++++----
testing/lisp/test-org.el | 13 +++++++++++++
3 files changed, 24 insertions(+), 6 deletions(-)
diff --git a/lisp/org-element.el b/lisp/org-element.el
index 798c540e9..c1798e07a 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -5906,9 +5906,9 @@ Providing it allows for quicker computation."
(let ((case-fold-search nil))
(goto-char (org-element-property :begin element))
(looking-at org-complex-heading-regexp)
- (let ((end (match-end 4)))
+ (let ((end (or (match-end 6) (match-end 4))))
(if (not end) (throw 'objects-forbidden element)
- (goto-char (match-beginning 4))
+ (goto-char (or (match-beginning 4) (match-beginning 6)))
(when (looking-at org-comment-string)
(goto-char (match-end 0)))
(if (>= (point) end) (throw 'objects-forbidden element)
diff --git a/lisp/org.el b/lisp/org.el
index 31133c554..b6fc6ad26 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -4011,6 +4011,7 @@ group 2: The TODO keyword, maybe
group 3: Priority cookie
group 4: True headline
group 5: Tags
+group 6: Statistics cookie
Since TODO keywords are case-sensitive, `case-fold-search' is
expected to be bound to nil when matching against this regexp.")
@@ -4328,7 +4329,8 @@ related expressions."
"\\(?: +" org-todo-regexp "\\)?"
"\\(?: +\\(\\[#.\\]\\)\\)?"
"\\(?: +\\(.*?\\)\\)??"
- "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
+ "\\(?: +\\(?6:\\[[0-9%%/]+\\]\\)\\)?"
+ "\\(?:[ \t]+\\(?5::[[:alnum:]_@#%:]+:\\)\\)?"
"[ \t]*$")
org-complex-heading-regexp-format
(concat "^\\(\\*+\\)"
@@ -6897,12 +6899,14 @@ So this will delete or add empty lines."
(insert (make-string n ?\n))
(move-to-column column)))
-(defun org-get-heading (&optional no-tags no-todo no-priority no-comment)
+(defun org-get-heading (&optional no-tags no-todo no-priority
+ no-comment no-stats)
"Return the heading of the current entry, without the stars.
When NO-TAGS is non-nil, don't include tags.
When NO-TODO is non-nil, don't include TODO keywords.
When NO-PRIORITY is non-nil, don't include priority cookie.
When NO-COMMENT is non-nil, don't include COMMENT string.
+When NO-STATS is non-nil, don't include statistics cookie.
Return nil before first heading."
(unless (org-before-first-heading-p)
(save-excursion
@@ -6919,9 +6923,10 @@ Return nil before first heading."
(format "\\`%s[ \t]+" org-comment-string))
"" h))
(h h)))
- (tags (and (not no-tags) (match-string 5))))
+ (tags (and (not no-tags) (match-string 5)))
+ (stats (and (not no-stats) (match-string 6))))
(mapconcat #'identity
- (delq nil (list todo priority headline tags))
+ (delq nil (list todo priority headline stats tags))
" "))))))
(defun org-heading-components ()
diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index dc4a6a59f..f98918dae 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -1887,6 +1887,19 @@
(equal "TODO [#A] H"
(org-test-with-temp-text "* TODO [#A] COMMENT H"
(org-get-heading nil nil nil t))))
+ ;; With NO-STATS, ignore statistics cookie.
+ (should
+ (equal "H"
+ (org-test-with-temp-text "* H [1/3]"
+ (org-get-heading nil nil nil nil t))))
+ (should
+ (equal "H"
+ (org-test-with-temp-text "* H"
+ (org-get-heading nil nil nil nil t))))
+ (should
+ (equal "TODO [#A] H"
+ (org-test-with-temp-text "* TODO [#A] H [33%]"
+ (org-get-heading nil nil nil nil t))))
;; On an empty headline, return value is consistent.
(should (equal "" (org-test-with-temp-text "* " (org-get-heading))))
(should (equal "" (org-test-with-temp-text "* " (org-get-heading t))))
--
2.25.0
next reply other threads:[~2020-03-13 15:11 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-03-13 15:11 Brice Waegeneire [this message]
2020-03-13 17:22 ` [PATCH] Add NO-STATS switch to org-get-heading Nicolas Goaziou
2020-03-13 17:37 ` Nicolas Goaziou
2020-03-13 17:41 ` Brice Waegeneire
2020-03-13 18:50 ` Adam Porter
2020-03-15 0:24 ` stardiviner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.orgmode.org/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20200313151145.29469-1-brice@waegenei.re \
--to=brice@waegenei.re \
--cc=emacs-orgmode@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public 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).