]> git.bundesbrandschatzamt.de Git - emacsconfig.git/commitdiff
modified bba-copy-and-yank-org-src-block
authorAndreas Gerler <baronNOSPAMbundesbrandschatzamt.de>
Sun, 19 Mar 2017 13:12:17 +0000 (14:12 +0100)
committerAndreas Gerler <baronNOSPAMbundesbrandschatzamt.de>
Sun, 19 Mar 2017 13:12:17 +0000 (14:12 +0100)
- if region marked copy only the BEGIN_SRC and END_SRC lines of
  previous src block. Useful if you have parameters in it.

Signed-off-by: Andreas Gerler <baronNOSPAMbundesbrandschatzamt.de>
bba.org

diff --git a/bba.org b/bba.org
index 4ece8e533a7327641144e22edda9c9924b0cfbe2..b6309e7d636b2a48869d4d03f4adb2cdfd67b8fb 100644 (file)
--- a/bba.org
+++ b/bba.org
@@ -1026,20 +1026,48 @@ This is helpful in training sessions.
 
 #+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()