Hi Nicolas, Nicolas Goaziou <n.goaz...@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.