4

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
  • 1
    All you need is to add space before bbb and ccc. Or are you asking about how to add space automatically upon copying? – wvxvw Jan 4 '16 at 16:40
  • @wvxvw add space automatically might be good – yuxuan Jan 4 '16 at 17:10
  • You would probably need to advise yank 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
  • you can use (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
3

It 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)
2

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
  • Item 4 starts a separate list with the method in the first text snippet. If you insert an additional item below item 2 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
  • Note also that you need to preserve the indentation of code blocks in the inserted text. I do not yet know what to do with headlines. Do we want to demote them? I do also not yet know what to do with drawers. – Tobias Jan 6 '16 at 8:58
  • tobias: re first message. yes they are separate lists. The first element is a 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.