Filing org-mode items under a new tree - emacs

Say you have a tree of the following form:
* Top
** Item A
*** Lower
** Item B
** Item C
What I would like to do is to be able to make a region containing Item A and Item B, and have a command run to change it into this:
* Top
**
*** Item A
**** Lower
*** Item B
** Item C
With the cursor at the blank item. I am wondering if something similar is already written into org-mode. If not, I can write it myself, but in that case I am wondering how I should loop through the lines in my region to shift them, avoiding items which are on a lower level than my starting one.

A naive first try
Starting with the outline
* Top
** Item A
*** Lower
** Item B
** Item C
If I place my cursor after Top and press Alt + Return (org-insert-heading) followed by Alt + Shift + right (org-demote-subtree) I get the structure
* Top
**
*** Item A
**** Lower
*** Item B
*** Item C
Unfortunately this demotes Item C, which you don't seem to want. However, you can just navigate to this item and press Alt + Shift + left. However, I imagine that this is a simplified example and want a more powerful method which doesn't involve moving around the file too much. Can we do better?
Transient mark mode
Reading through structure editing in the Org mode manual I found the note
When there is an active region (Transient Mark mode), promotion and demotion work on all headlines in the region. To select a region of headlines, it is best to place both point and mark at the beginning of a line, mark at the beginning of the first headline, and point at the line just after the last headline to change.
So perhaps this gives us a way forward. However, I haven't been able to do anything sensible with this information. Hopefully someone else can step and and show us how it is done.
Narrowing to a region
One other way we can move more is to "narrow" the buffer to only the region we want to work on.
Select the lines * Top, ** Item B in full and everything in between (with your mouse or by, for example, using Ctrl + Space and Ctrl + c + Ctrl + n (outline-next-visible-heading) a few times). We can now focus our attention on only this highlighted (or marked) region by "narrowing" the buffer to only this region, to the exclusion of all other text in the buffer (we won't delete the text, even though it will look that way, we'll just hide it for a bit. We narrow to the region by using Ctrl + x + n + n (narrow-to-region) (note the helpful message that appears if you haven't explicitly enabled this feature). We can always get back to the full buffer by using Ctrl + x + n + w (widen).
In our narrowed buffer we can now only see:
* Top
** Item A
*** Lower
** Item B
If we now repeat the steps from our naive try (see above) and then returning to the full (widened) buffer we see that we have the outline:
* Top
**
*** Item A
**** Lower
*** Item B
** Item C
which is the final result we were after! My apologise if this is an unnecessarily long answer. I learnt a lot researching it and wanted to document it all here.

Based off Chris's answer, I came up with the following code:
(defun org-file-region (start end)
"Moves the lines in region to be underneath a new header"
(interactive "r")
(extend-region-to-lines start end)
(narrow-to-region (save-excursion (goto-char start)
(line-beginning-position))
(save-excursion (goto-char end)
(line-end-position)))
(org-do-demote)
(org-insert-heading)
(setq mark-active nil)
(org-do-promote)
(setq mark-active t)
(widen))
Note that only org-do-demote will operate on the region; the other demotion functions do not.

Related

org-metaup/down deletes line instead of swapping it up/down

I am using emacs 24.5.1 and do not understand how to shift outlines up/down in org-mode
Let's say I have the following list:
* Title 1
** section a
** section b
** section c
I would like to swap sections b and c. According to the manual this is achieved by using the keys M-up/down for items or M-S-up/down for subtree items. If I try M-down on "section b" for instance, I expect the following:
* Title 1
** section a
** section c
** section b
But instead I get:
* Title 1
** section a
** section c
The command deletes the current line ! It seems to work only with tickbox lists...
I checked my key-bindings with C-h k and they are calling the right commands. They are also displayed properly in the org-mode menu. I also tried to execute the commands
(org-shiftmetadown)
(org-move-subtree-down)
(org-move-item-down)
directly in the minibuffer but always get the same behaviour (line is deleted). I thought about a conflict with cua-mode but disabling it does not help.
I am missing something ? How to move outline items up/dow in org-mode ?
It shows M-up is bounded to command org-metaup in my case. And it moves subtree or table row up depending the context. Could you please try the command org-metaup and see if it works?

move buffer to next subtree in org-mode when narrowed - C-x n s

Is there a way to move to next/previous heading, same level when (C-c C-f/b) when buffer is narrowed to subtree (C-x n s)?
Say I have the following outline
* First
** item A
** item B
* Second
** item A2
** item B2
and I got to * First and narrow with C-x n s now I only see:
* First
** item A
** item B
If I try to move to next/previous heading with C-c C-f/b I can't. I would like to be able to move back and forth between subtrees in a folded (narrowed) three. How can I do this?

How to fold all blocks below certain level in emacs org-mode? [duplicate]

This question already has answers here:
show org-mode outline up to a certain heading level
(4 answers)
Closed 6 years ago.
I often need to see information up to (down to, really) certain level of detail. Is there a way to unfold blocks to a certain level, and hide the rest? For example:
* name
Joe
** home number
111-111-1111
*** hobby
fishing
*** likes
books
** work number
222-222-2222
If third-level and below blocks are rarely used, then I would like to see them folded as such:
* name
Joe
** home number
111-11-1111
*** hobby...
*** likes...
** work number
222-22-2222
There's hide-sublevels. The exact number to use for the level you need will typically vary depending on the major mode, tho. I usually use C-1 M-x hide-sublevels (aka C-1 C-c C-q), but for some major modes I use much deeper levels.
If you place the cursor at the beginning of the buffer, a simple <TAB> will cycle the visibility of the entire subtree.
Alternatively, calling it with a prefix argument will set the global subtree visibility to the depth specified by the argument (C-u <TAB>). For example; C-u 3 <TAB> would show everything up to the third level of the tree and hide everything below that.
Also see http://orgmode.org/manual/Visibility-cycling.html#Visibility-cycling

How to move sub-headings to under other headings in emacs org-mode

My list looks like this:
* TODAY
** TODO Item 1
** TODO Item 2
* TOMORROW
** TODO Item 3
** TODO Item 4
...as a single list, based on some advice I received here.
I'd like to move TODO Item 2 from under TODAY to under TOMORROW.
The manual says:
M-up
M-down
Move the item including subitems up/down (swap with previous/next item of same indentation). If the list is ordered, renumbering is automatic.
But while I can change the places of Item 1 and Item 2, I cannot move Item 2 outside of the Today heading---I cannot move it down under TOMORROW to precede Item 3. The buffer tells me:
cannot move past superior level or buffer limit org mode
What is the keystroke that lets me move sub-items "past superior level" to under new headings?
You could use C-c C-w (org-refile) and specify the heading where you want to send your TODO item.
As you'll see in this function's documentation (C-h forg-refile), you can customize the org-refile-targets and org-reverse-note-order variables to determine the list of possible target headings and the position of the moved item within the target heading.
Here are 2 options:
Promote Item 2 to the top level with M-left, then move it down below "TOMORROW" with M-down, and finally demote it under "TOMORROW" with M-right.
Cut the "Item 2" subtree with org-cut-special (C-c C-x C-w), then paste it under "TOMORROW" using org-paste-special (C-c C-x C-y).
the simplest way, Just use M-shift-up or M-shift-down you can move item past superior level.
I have to do this a lot when making beamer presentations. The simplest way for me is to just fold then item as you have in your example list and essentially kill and yank the region. That is, go to the beginning of the ** TODO Item 2 line, set the mark (C-[SPACE]), move the point to the beginning of the next line (C-n), kill the region (C-w), move the point to where you want under the * Tomorrow heading, and yank it back (C-y). It is important to get the region rather than just the line (with C-k) so that you get all the bits that are folded.
Similar to sanityinc's above, the solution I've adopted is to...
Move cursor to *TOMORROW
Demote it to ** with M-right
Move cursor up to TODO item 2
Pull it down beneath **TOMORROW with M-down
Return cursor to **TOMORROW and re-promote it with M-left
Constructing a macro for this would probably be better, but it's intuitive and not as bad as it looks. Still, it would be more convenient if C-M-down or something did this.
Bit late to the party, but to expand Mittenchops answers I created the following macro.
(defun org-move-subtree-to-next-superior()
"Moving subtree to next superior."
(interactive)
(org-promote-subtree)
(org-move-subtree-down)
(org-demote-subtree))
(global-set-key (kbd "C-c C-x t") 'org-move-subtree-to-next-superior)

emacs + vimpulse-visual-mode + "linewise" text selection

Is it possible to make visual-line-mode (one after pressing V from normal mode) conduct as if first mark was in the beginning of the first line of selection and second mark - end of the last line?
For an example, currently after V, j and M-x comment-dwim:
here<cursor>is a
simple example
becomes
here;; is a
;; simp
le example
whereas desired result is often:
;; here is a
;; simple example
Of course, one can write a wrapper for comment-dwim, but I suspect/hope that there is a more correct solution.
Thank you in advance.
Doesn't V (vimpulse-visual-toggle-line) already do that?
Linewise selection will select whole lines. (I use this all the time)
The behaviour you're talking about will occur if you're using v (vimpulse-visual-toggle-char).
comment-dwim calls comment-or-uncomment-region to perform the actual commenting on the marked region. There is no option to extend the region to beginning of the first line and/or end of the last line. You will have to write a wrapper or advice comment-or-uncomment-region to achieve the effect you want.