In list environment, after I copy several paragraphs, the next item number will change into number 1, How can I deal with that?
For example,
1. aaa
2. aaa
3. aaa
bbb <---this is what I copied
ccc
1. aaa <---I want it becomes 4
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityIt is not a good idea to adjust the yanked region directly on yank
. The indent is syntactic relevant for the structure of an org-file. If you copy-paste already structured text the structure would get lost through the adjustments.
I propose that you leave yank
as it is and use the following command org-adjust-region
that re-adjusts all the stuff in the current region according to the preceeding stuff.
I have bound this function to C-+.
You can install the lisp snippet below in your init file. In your use case you just press C-+ right after pasting and see what happens...
(require 'org)
(defun org-adjust-region (b e)
"Readjust stuff in region according to the preceeding stuff."
(interactive "r") ;; current region
(save-excursion
(let ((e (set-marker (make-marker) e))
(_indent (lambda ()
(insert ?\n)
(backward-char)
(org-indent-line)
(delete-char 1)))
last-item-pos)
(goto-char b)
(beginning-of-line)
(while (< (point) e)
(indent-line-to 0)
(cond
((looking-at "[[:space:]]*$")) ;; ignore empty lines
((org-at-heading-p)
(error "Headings cannot be balanced (yet)."))
((org-at-item-p)
(funcall _indent)
(let ((struct (org-list-struct))
(mark-active nil))
(ignore-errors (org-list-indent-item-generic -1 t struct)))
(setq last-item-pos (point)))
((org-at-block-p)
(funcall _indent)
(goto-char (plist-get (cadr (org-element-special-block-parser e nil)) :contents-end))
(org-indent-line))
(t (funcall _indent)))
(forward-line))
(when last-item-pos
(goto-char last-item-pos)
(org-list-repair)
))))
(define-key org-mode-map (kbd "C-+") 'org-adjust-region)
Paragraph bbb\nccc
is not part of item 3:
1. aaa
2. aaa
3. aaa
bbb <---this is what I copied
ccc
4. [@4] aaa <---I want it becomes 4
Paragraph bbb\nccc
is a separate paragraph within item 3
1. aaa
2. aaa
3. aaa
bbb <---this is what I copied
ccc
4. aaa <---I want it becomes 4
org-list-repair
does not work as expected. The first list runs from 1 to 4 and the next one starts at 4?! I do not know how you get from the first text snippet to the second one. Selecting a region and running org-indent
does work for this small example but not in the general case where there are several items in the pasted text. That is the point where I started to write down my solution.
– Tobias
Jan 6 '16 at 8:20
plain-list
of items; the second element is a paragraph
; the third element is again a plain-list
. To verify use org-element-at-point
at pos 0
, 22
, and 59
. The second list starts at 4 because of the @
operator, and it's the point of the exercise. The second example is independent of the first and it seems we are trying to solve different problems. I simply show how to make the paragraph either a separate element or part of the item
starting at pos 15
.
– rasmus
Jan 9 '16 at 17:51
bbb
andccc
. Or are you asking about how to add space automatically upon copying? – wvxvw Jan 4 '16 at 16:40yank
and pad the strings you insert with as many spaces as the current indentation level... but this seems like a low of work. If you don't encounter this problem very often, you could just insert region (of blanks) before the inserted text: a bit slower, but easier to do. – wvxvw Jan 4 '16 at 18:24(org-get-indentation)
to find the indentation. then you could use a temp buffer to add the necessary amount of space for each line, before inserting it into your buffer. – rasmus Jan 5 '16 at 22:59