From mboxrd@z Thu Jan 1 00:00:00 1970 From: Nicolas Goaziou Subject: Re: Bug: org-map-tree behaves strangely if first heading before point is hidden [8.3.5 (8.3.5-elpa @ /home/tom/.emacs.d/elpa/org-20160725/)] Date: Sun, 31 Jul 2016 00:26:34 +0200 Message-ID: <87fuqqdap1.fsf@saiph.selenimh> References: Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:54711) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bTciN-0006GA-MR for emacs-orgmode@gnu.org; Sat, 30 Jul 2016 18:26:40 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1bTciJ-0004H0-D4 for emacs-orgmode@gnu.org; Sat, 30 Jul 2016 18:26:38 -0400 Received: from relay4-d.mail.gandi.net ([217.70.183.196]:36961) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1bTciJ-0004Gr-64 for emacs-orgmode@gnu.org; Sat, 30 Jul 2016 18:26:35 -0400 In-Reply-To: (talwrii talwrii's message of "Fri, 29 Jul 2016 13:16:16 -0500") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: "Emacs-orgmode" To: talwrii talwrii Cc: emacs-orgmode@gnu.org Hello, talwrii talwrii writes: > Org mode file complete with code to reproduce the issue is attached. > > * Folded > ** Hidden > *** Children > *** More children > > ** Other child > > > * Bug description > When calling org-map-tree on a hidden tree, the results are different from > those if the tree were visible: this is not as expected. > > * Repro > The following code samples consistently reproduce this behaviour. > Run then with `C-c C-c` when the cursor is over the code block. > > The first folds up the first tree in this document and then calls `org-map-tree` with the point at "Hidden"; > the second does the same but expands the first tree. > > I consider the case with the expanded tree to retrun the correct result. > ** Folded > #+begin_src emacs-lisp :results raw > (save-excursion > ; Expand first tree > (goto-char 0) > (while (not (equal org-cycle-subtree-status 'folded)) > (org-cycle)) > > ; Run map over `Hidden` > (goto-char 10) > (setq accum nil) > (org-map-tree (lambda () (add-to-list 'accum (point)))) > accum > ) > > #+end_src > #+RESULTS: > (52 33 20 10 1) > ** Expanded > #+begin_src emacs-lisp :results raw > (save-excursion > > (goto-char 0) > (setq accum nil) > > (while (not (equal org-cycle-subtree-status 'children)) > (message (format "%S" org-cycle-subtree-status)) > (org-cycle)) > > (goto-char 10) > > (org-map-tree (lambda () (add-to-list 'accum (point)))) > accum > ) > #+end_src > > #+RESULTS: > (33 20 10) > > * Likely cause and fix > > Looking at `org-map-tree`: > > (defun org-map-tree (fun) > "Call FUN for every heading underneath the current one." > (org-back-to-heading) > (message (format "org-map-tree start %S" (point))) > (let ((level (funcall outline-level))) > (save-excursion > (funcall fun) > (while (and (progn > (prog1 > (outline-next-heading) > (message (format "org-map-tree outline-ext-point %S" (point))) > ) > (> (funcall outline-level) level)) > (not (eobp))) > (funcall fun))))) > > We see that it calls `org-back-to-heading`; this skips back to the first visible ancestor heading. > > `org-back-to-heading` has the optional argument `invisible-ok` which causes it to skip back to the first > ancestor heading, whether it is visible or not. > > (defun org-back-to-heading (&optional invisible-ok) > "Call `outline-back-to-heading', but provide a better error message." > (condition-case nil > (outline-back-to-heading invisible-ok) > (error (error "Before first headline at position %d in buffer %s" > (point) (current-buffer))))) > > I think that `org-map-subtree` should call `org-back-to-heading` with > the `invisible-ok` argument set to `'t` That's correct. > However, this is changes behavior, and people may be relying upon this behaviour in their scripts... though it would > be rather strange to have your code behave different based on whether subheadings are visible or not. So perhaps > we should just make this change. This is not a big issue because you can test within the function if the headline is collapsed or not. Anyway, just to be safe, I've changed it in master. Thank you. Regards, -- Nicolas Goaziou