1

the code block I copy outside emacs, which is not auto-indent when yanking in org

type org-return-indent -> yank code block -> indent manually:


*** headline
    |


v



*** headline
    {
    code block
}



v



*** headline
    {
      code block
    }

expecting the yank and indent in a one step

  • 1
    AFAIK code blocks are delimited by #+BEGIN_SRC and #+END_SRC in org-mode. I.e., you would type <s and press the tabulator key to get a source code template and then paste your source text into this template. Could you please change your example to a minimal working example. Thanks in advance. – Tobias Mar 23 '17 at 15:26
  • personally not use #+BEGIN_SRC and #+END_SRC due to the running speed – ccd Mar 24 '17 at 1:27
  • See: emacs.stackexchange.com/a/38566/2418 - similar issue except for evil mode. – ideasman42 Nov 16 '19 at 6:02
1

Here is a elisp function that should do what you want

(defun yank-with-indent ()
  (interactive)
  (let ((indent
         (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
    (message indent)
    (yank)
    (narrow-to-region (mark t) (point))
    (pop-to-mark-command)
    (replace-string "\n" (concat "\n" indent))
    (widen)))
  • yes, very nice work! if can add a conditional judgment, which do not indent if the line have no character – ccd Mar 25 '17 at 2:00
2

@native-human congratulations for this very useful snippet! But after using it I found that it removes narrow-to-region and so breaks my org workflow. To fix it I added a few lines to preserve narrowing:

  (defun yank-with-indent ()
(interactive)
(let ((indent
       (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
  (message indent)
  (yank)
  (save-excursion
    (save-restriction
      (narrow-to-region (mark t) (point))
      (pop-to-mark-command)
      (replace-string "\n" (concat "\n" indent))
      (widen)))))

(define-key org-mode-map (kbd "C-c y") 'yank-with-indent)

0

Here is a different approach, maybe someone will find this useful. This is a minor mode that patches get-text-property to fake the property 'yank-handler, which indicates which function to use for inserting. Then I use insert-rectangle for that. That way, this also works with helm-ring.

(defvar my/is-rectangle-yanking nil)

(defun my/insert-rectangle-split (string)
  (let ((rows (split-string string "\n"))
        (my/is-rectangle-yanking t))
    (save-excursion (insert (make-string (length rows) ?\n)))
    (insert-rectangle rows)
    (delete-char 1)))

(defun my/get-text-property-rectangle-yank (fun pos prop &optional object)
  (let ((propv (funcall fun pos prop object)))
    (if propv propv
      (when (and (eq prop 'yank-handler) (stringp object) (not my/is-rectangle-yanking))
        '(my/insert-rectangle-split)))))

(define-global-minor-mode my/yank-as-rectangle
  "Minor mode to yank as rectangle.  This patches `get-text-property'
to put `insert-rectangle' as the insert function (`yank-handler')."
  :init-value nil  :lighter " Rec"
  (if (not my/yank-as-rectangle)
      (advice-remove 'get-text-property #'my/get-text-property-rectangle-yank)
    (advice-add 'get-text-property :around #'my/get-text-property-rectangle-yank)))

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.