2

I use org-mode, and have huge text files with headings (as well as subheadings at different depths). Instead of clicking open each headline to check for its content, I would like a "follow" functionality, made such that when moving the cursor down or up the collapsed headlines in one window (commands C-n or C-p, respectively), the content under the current headline is shown in a second window, splitting the frame vertically or horizontally.

How can I make or invoke such a following mode for showing headline content?

myotis
  • 1,099
  • 1
  • 13
  • 27
  • 1
    You can use `occur` to search for `^*` (i.e. all headings) and navigate with M-g M-n and M-g M-p. – Juancho Sep 07 '17 at 14:50
  • Thanks @Juancho, when I do this I get an *Occur* window where I can navigate up and down, but the original headings in the first window does not open by this navigating. How can I make the original headings open? – myotis Sep 07 '17 at 14:55
  • 1
    This sounds a little like a customized agenda for the current file with follow mode turned on. Unfortunately `C-c a < m RET F`, though it will give you all the headings will not preserve the tree structure/order. – Andrew Swann Sep 08 '17 at 07:50

1 Answers1

1

@Juancho correctly suggested to use occur to provide the functionality you desire.

Use add-hook on the occur-mode-find-occurrence-hook.

  • To see how occur-mode-find-occurrence-hook works, try this:

    1. Add the code blocks below into your large org-mode file.

      This code block adds the functionality to occur.

      #+BEGIN_SRC elisp :results none :exports none 
      (defun my/occur-org-cycle ()
        (outline-hide-sublevels 1)
        (org-cycle) 
        (org-switch-to-buffer-other-window "*Occur*")
       )
      
      (add-hook 'occur-mode-find-occurrence-hook 'my/occur-org-cycle)
      #+END_SRC
      

      This code block removes the functionality from occur

      #+BEGIN_SRC elisp :results none :exports none 
       (remove-hook 'occur-mode-find-occurrence-hook 'my/occur-org-cycle)
      #+END_SRC
      
    2. Click inside the first code block and press C-c C-c to add a hook to occur. Answer yes if prompted.

    3. Call occur using M-s o ^*

    4. Navigate your headlines from the new *Occur* buffer and they should expand and collapse as you described.

    5. To revert back to the default behavior, click inside the second code block and execute by pressing C-c C-c. Answer yes if prompted.


To add this functionality permanently, you will need modify and add the code into your emacs configuration file. Unfortunately, I'm still working that step out.

Thanks for asking a great question!


This code was tested using
GNU Emacs 25.2.1 (x86_64-unknown-cygwin, GTK+ Version 3.22.10)
Org mode version 9.0.9

Melioratus
  • 4,504
  • 1
  • 25
  • 43
  • Thanks for your work! I will test your code as soon as possible. – myotis Sep 08 '17 at 22:31
  • @myotis my pleasure! Please let me know if you have any problems. I'm going to add the code to my init file because it's a nice feature when you have long org files. – Melioratus Sep 09 '17 at 00:06
  • Hi @Melioratus. Now I have tested your code and I am not able to make it work. I have turned your code on, turned on occur, and got a second window with the occur buffer. But when navigating the headlines in the new buffer, down (C-n) or up (C-p), nothing happens, no headlines are opened, none are collapsed. What have I done wrong? I am using Emacs 25.0.93.1 and Org mode version 9.0.1 – myotis Sep 10 '17 at 23:08
  • @myotis - My guess is you did everything perfectly! Try this: after executing first code block and opening occur, use `arrow keys` and `return key` to navigate in occur instead of `C-n` and `C-p`. If this works than our key bindings may be different. If it doesn't then please remove the `:results none` header from block so any execution errors will be displayed and execute the first code block again. Does this help? – Melioratus Sep 11 '17 at 01:13
  • a) the arrow key approach does not help. The `return key` moves point from a heading in the *occur* window, to the corresponding heading ing the file. b) the removing of `:results none` does not help either, it writes out a lot of stuff under a `#+RESULTS:` heading, but navigating the *occur* file with arrow keys/return key or C-n/C-p does not automatically open the header i the file, when the corresponding header in the occur buffer is navigatet to. – myotis Sep 11 '17 at 21:30
  • @myotis - Thanks for the update. Removing `:results none` should only print a small one row table under `#+RESULTS:` which I'm guessing it did not do. Was an error displayed? Using the `return` key will move cursor to other window but it should also expand the heading. Did the heading stay collapsed? Did previously expanded headings stay expanded? We might need to move our troubleshooting into chat instead of comments but not sure how to do that? Any suggestions? – Melioratus Sep 11 '17 at 22:52
  • Removing `:results none` writes out 20 lines with emacs commands, many of which are org commands. Yes, the return key moves the cursor to the other window, without opening the headline there. And then there is no point in the code, because I could just as well open the headlines directly in the file without making any occur window. I want the file to be collapsed to headlines only, and moving down the occur file headline will open the corresponding heading in the text file. While the other hading are collapsed. – myotis Sep 11 '17 at 23:06
  • @myotis - I understand your frustration. I updated my answer to force the cursor back into `*Occur*` buffer after the hook is run. Now when I run the answer, I navigate up and down with `C-n` and `C-p` and press `return` key. This does the following: 1. Pops cursor from occur buffer to heading in org buffer to the desired heading; 2. Collapses all the headings in the org buffer; 3. Expands only the current heading; 4. Pops back to occur buffer. This works with mouse clicks; `M-g M-n` and `M-g M-p` and using the arrow keys plus return. I'm guessing that upgrading to org v.9.0.9 will fix issue. – Melioratus Sep 11 '17 at 23:56
  • Thaks a lot for your efforts! It works great :) – myotis Sep 12 '17 at 00:03
  • @myotis - Thanks! What resolved issue? Did you upgrade to org v.9.0.9? – Melioratus Sep 12 '17 at 01:12