#+BEGIN_SRC emacs-lisp :tangle yes
(defun bba-copy-and-yank-org-src-block()
- "copy last org src block and insert it at point."
+ "copy last org src block and insert it at point.
+ If region is active copy only the src begin and src end lines."
(interactive)
- (push-mark)
- (re-search-backward "\#\\+BEGIN_SRC")
- (set-mark-command nil)
- (re-search-forward "\#\\+END_SRC")
- (forward-line)
- (beginning-of-line)
- (kill-ring-save 1 1 1)
- (set-mark-command '1)
- (set-mark-command '1)
- (insert (pop kill-ring))
- (re-search-backward "\#\\+BEGIN_SRC")
- (forward-line)
+ (if (use-region-p)
+ (progn
+ (let (-p1 -p2 srclength)
+ (setq -p1 (region-beginning) -p2 (region-end))
+ (setq mark-active nil)
+ (re-search-backward "\#\\+BEGIN_SRC")
+ (set-mark-command nil)
+ (forward-line)
+ (beginning-of-line)
+ (kill-ring-save 1 1 1)
+ (goto-char -p1)
+ (set-mark-command nil)
+ (insert (pop kill-ring))
+ (setq srclength (- (region-end) (region-beginning)))
+ (setq mark-active nil)
+ (re-search-backward "\#\\+END_SRC")
+ (set-mark-command nil)
+ (forward-line)
+ (beginning-of-line)
+ (kill-ring-save 1 1 1)
+ (kill-append "\n" nil)
+ (goto-char (+ -p2 srclength))
+ (insert (pop kill-ring))
+ )
+ )
+ (progn
+ (push-mark)
+ (re-search-backward "\#\\+BEGIN_SRC")
+ (set-mark-command nil)
+ (re-search-forward "\#\\+END_SRC")
+ (forward-line)
+ (beginning-of-line)
+ (kill-ring-save 1 1 1)
+ (set-mark-command '1)
+ (set-mark-command '1)
+ (insert (pop kill-ring))
+ (re-search-backward "\#\\+BEGIN_SRC")
+ (forward-line)
+ ))
)
(defun bba-copy-org-src-block()