* Bug: [patch] also tangle headlines that begin with lower case string "comment" [8.2.5h (release_8.2.5h-680-g12df70 @ /home/youngfrog/sources/org-mode/lisp/)]
@ 2014-03-05 15:44 Nicolas Richard
2014-03-05 19:08 ` Nicolas Goaziou
0 siblings, 1 reply; 5+ messages in thread
From: Nicolas Richard @ 2014-03-05 15:44 UTC (permalink / raw)
To: emacs-orgmode
Hi,
Trying to tangle code blocks in a headline line like:
#+begin_src org
,* Comment retrouver....
#+end_src
I struggled to understand what I was doing wrong because it didn't
tangle anything. In fact, the french word "Comment" was mistakenly
parsed as the org-comment-string, and so the headline was skipped. Let's
avoid that (see patch below).
I take the opportuinty to ask if we should try and make this function
use org-element instead.
My naïve approach doesn't work:
#+begin_src emacs-lisp
(save-excursion
(org-back-to-heading t)
(let ((elt (org-element-at-point)))
(while (and elt
(not
(org-element-property :commentedp elt)))
(setq elt
(org-element-property :parent elt)))
elt))
#+end_src
because an heading doesn't know what its parent heading is (the property
is nil). This can be fixed by doing:
#+begin_src emacs-lisp
(defun org-babel-under-commented-heading-p ()
"Return t if currently under a commented heading."
(unless (org-before-first-heading-p)
(save-excursion
(org-back-to-heading t)
(let ((elt (org-element-at-point)))
(while (and elt
(not
(org-element-property :commentedp elt)))
(setq elt
(and (org-up-heading-safe)
(org-element-at-point))))
elt))))
#+end_src
byt I'm not sure it is very pretty. Opinions ?
Anyway, here's the quicker fix :
From: Nicolas Richard <theonewiththeevillook@yahoo.fr>
Date: Wed, 5 Mar 2014 14:54:49 +0100
Subject: [PATCH] * ob-tangle.el (org-babel-under-commented-heading-p): Disable
case folding to avoid matching "Comment" at beg of headline.
---
lisp/ob-tangle.el | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/lisp/ob-tangle.el b/lisp/ob-tangle.el
index 0096ac9..9f0ea0d 100644
--- a/lisp/ob-tangle.el
+++ b/lisp/ob-tangle.el
@@ -361,7 +361,8 @@ that the appropriate major-mode is set. SPEC has the form:
"Return t if currently under a commented heading."
(unless (org-before-first-heading-p)
(if (let ((hd (nth 4 (org-heading-components))))
- (and hd (string-match (concat "^" org-comment-string) hd)))
+ (and hd (let (case-fold-search)
+ (string-match (concat "^" org-comment-string) hd))))
t
(save-excursion
(and (org-up-heading-safe)
--
Nico.
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: Bug: [patch] also tangle headlines that begin with lower case string "comment" [8.2.5h (release_8.2.5h-680-g12df70 @ /home/youngfrog/sources/org-mode/lisp/)]
2014-03-05 15:44 Bug: [patch] also tangle headlines that begin with lower case string "comment" [8.2.5h (release_8.2.5h-680-g12df70 @ /home/youngfrog/sources/org-mode/lisp/)] Nicolas Richard
@ 2014-03-05 19:08 ` Nicolas Goaziou
2014-03-05 21:55 ` Nicolas Richard
2014-03-05 21:56 ` Nicolas Richard
0 siblings, 2 replies; 5+ messages in thread
From: Nicolas Goaziou @ 2014-03-05 19:08 UTC (permalink / raw)
To: Nicolas Richard; +Cc: emacs-orgmode
Hello,
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
> I take the opportuinty to ask if we should try and make this function
> use org-element instead.
>
> My naïve approach doesn't work:
>
> #+begin_src emacs-lisp
> (save-excursion
> (org-back-to-heading t)
> (let ((elt (org-element-at-point)))
> (while (and elt
> (not
> (org-element-property :commentedp elt)))
> (setq elt
> (org-element-property :parent elt)))
> elt))
> #+end_src
>
> because an heading doesn't know what its parent heading is (the property
> is nil). This can be fixed by doing:
>
> #+begin_src emacs-lisp
> (defun org-babel-under-commented-heading-p ()
> "Return t if currently under a commented heading."
> (unless (org-before-first-heading-p)
> (save-excursion
> (org-back-to-heading t)
> (let ((elt (org-element-at-point)))
> (while (and elt
> (not
> (org-element-property :commentedp elt)))
> (setq elt
> (and (org-up-heading-safe)
> (org-element-at-point))))
> elt))))
> #+end_src
>
>
> byt I'm not sure it is very pretty. Opinions ?
Of course, `org-element-at-point' can parse headlines, but if speed is
a factor, since headline syntax is not context-dependent, it is often
worth considering using regexps.
Also, don't forget `org-with-limited-levels' when you need to tell
a headline from an inlinetask.
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Bug: [patch] also tangle headlines that begin with lower case string "comment" [8.2.5h (release_8.2.5h-680-g12df70 @ /home/youngfrog/sources/org-mode/lisp/)]
2014-03-05 19:08 ` Nicolas Goaziou
@ 2014-03-05 21:55 ` Nicolas Richard
2014-03-05 21:56 ` Nicolas Richard
1 sibling, 0 replies; 5+ messages in thread
From: Nicolas Richard @ 2014-03-05 21:55 UTC (permalink / raw)
To: Nicolas Goaziou; +Cc: Nicolas Richard, emacs-orgmode
Hi Nicolas,
Nicolas Goaziou <n.goaziou@gmail.com> writes:
> Of course, `org-element-at-point' can parse headlines, but if speed is
> a factor, since headline syntax is not context-dependent, it is often
> worth considering using regexps.
I don't know if speed is terribly important here, but since my suggested
approach uses a loop instead of recursion, it ends up being faster for
nested headlines (more than 5 levels). This obviously could be fixed in
the initial approach too.
Since we know where point is, we could also use
(org-element-headline-parser nil t) instead of (org-element-at-point).
That's a tiny bit faster.
For those interested, here's timing info:
(progn
(defun yf/org-babel-under-commented-heading-p ()
"Return t if currently under a commented heading."
(unless (org-before-first-heading-p)
(save-excursion
(org-back-to-heading t)
(let ((elt (org-element-headline-parser nil t)))
(while (and elt
(not
(org-element-property :commentedp elt)))
(setq elt
(and (org-up-heading-safe)
(org-element-headline-parser nil t))))
elt))))
(elp-instrument-list
'(org-babel-under-commented-heading-p
yf/org-babel-under-commented-heading-p))
(let ((org-element-use-cache t))
(with-temp-buffer
(insert "* foo bar
** bal
*** bal
**** bal
***** bal
****** bal
******* bal
******** bal
********* bal
********** bal
*********** bal
************ bal
************* bal
************** bal")
(org-mode)
(goto-char (point-min))
(forward-line 4) ;; <- 0 for top level, etc.
;; (profiler-reset)
(let ((n 100))
(garbage-collect)
(dotimes
(_ n)
(org-babel-under-commented-heading-p))
(dotimes
(_ n)
(yf/org-babel-under-commented-heading-p))
(elp-results)
)
;; (profiler-report)
)))
But as I said, I don't think speed matters much for this specific function.
> Also, don't forget `org-with-limited-levels' when you need to tell
> a headline from an inlinetask.
I have absolutely no idea if this is important here.
--
Nicolas.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Bug: [patch] also tangle headlines that begin with lower case string "comment" [8.2.5h (release_8.2.5h-680-g12df70 @ /home/youngfrog/sources/org-mode/lisp/)]
2014-03-05 19:08 ` Nicolas Goaziou
2014-03-05 21:55 ` Nicolas Richard
@ 2014-03-05 21:56 ` Nicolas Richard
2014-03-05 22:27 ` Nicolas Goaziou
1 sibling, 1 reply; 5+ messages in thread
From: Nicolas Richard @ 2014-03-05 21:56 UTC (permalink / raw)
To: Nicolas Goaziou; +Cc: Nicolas Richard, emacs-orgmode
Hi Nicolas,
Nicolas Goaziou <n.goaziou@gmail.com> writes:
> Of course, `org-element-at-point' can parse headlines, but if speed is
> a factor, since headline syntax is not context-dependent, it is often
> worth considering using regexps.
I don't know if speed is terribly important here, but since my suggested
approach uses a loop instead of recursion, it ends up being faster for
nested headlines (more than 5 levels). This obviously could be fixed in
the initial approach too.
Since we know where point is, we could also use
(org-element-headline-parser nil t) instead of (org-element-at-point).
That's a tiny bit faster.
For those interested, here's timing info:
(progn
(defun yf/org-babel-under-commented-heading-p ()
"Return t if currently under a commented heading."
(unless (org-before-first-heading-p)
(save-excursion
(org-back-to-heading t)
(let ((elt (org-element-headline-parser nil t)))
(while (and elt
(not
(org-element-property :commentedp elt)))
(setq elt
(and (org-up-heading-safe)
(org-element-headline-parser nil t))))
elt))))
(elp-instrument-list
'(org-babel-under-commented-heading-p
yf/org-babel-under-commented-heading-p))
(let ((org-element-use-cache t))
(with-temp-buffer
(insert "* foo bar
** bal
*** bal
**** bal
***** bal
****** bal
******* bal
******** bal
********* bal
********** bal
*********** bal
************ bal
************* bal
************** bal")
(org-mode)
(goto-char (point-min))
(forward-line 4) ;; <- 0 for top level, etc.
;; (profiler-reset)
(let ((n 100))
(garbage-collect)
(dotimes
(_ n)
(org-babel-under-commented-heading-p))
(dotimes
(_ n)
(yf/org-babel-under-commented-heading-p))
(elp-results)
)
;; (profiler-report)
)))
> Also, don't forget `org-with-limited-levels' when you need to tell
> a headline from an inlinetask.
I have absolutely no idea if this is important here.
--
Nicolas.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: Bug: [patch] also tangle headlines that begin with lower case string "comment" [8.2.5h (release_8.2.5h-680-g12df70 @ /home/youngfrog/sources/org-mode/lisp/)]
2014-03-05 21:56 ` Nicolas Richard
@ 2014-03-05 22:27 ` Nicolas Goaziou
0 siblings, 0 replies; 5+ messages in thread
From: Nicolas Goaziou @ 2014-03-05 22:27 UTC (permalink / raw)
To: Nicolas Richard; +Cc: emacs-orgmode
Nicolas Richard <theonewiththeevillook@yahoo.fr> writes:
> I don't know if speed is terribly important here, but since my suggested
> approach uses a loop instead of recursion, it ends up being faster for
> nested headlines (more than 5 levels). This obviously could be fixed in
> the initial approach too.
>
> Since we know where point is, we could also use
> (org-element-headline-parser nil t) instead of (org-element-at-point).
> That's a tiny bit faster.
`org-element-headline-parser' is a bit low-level. Also, it will still
retrieve all entry properties, which explains why regexps are faster.
Anyway, it doesn't matter much if speed is not an issue.
Regards,
--
Nicolas Goaziou
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2014-03-05 22:27 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-03-05 15:44 Bug: [patch] also tangle headlines that begin with lower case string "comment" [8.2.5h (release_8.2.5h-680-g12df70 @ /home/youngfrog/sources/org-mode/lisp/)] Nicolas Richard
2014-03-05 19:08 ` Nicolas Goaziou
2014-03-05 21:55 ` Nicolas Richard
2014-03-05 21:56 ` Nicolas Richard
2014-03-05 22:27 ` Nicolas Goaziou
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).