Browse Source

interactive demarcation of code blocks

  Thanks to Richard Riley for the initial idea and implementation

* lisp/ob.el (org-babel-demarcate-block): interactive demarcation of
  code blocks
* lisp/ob-keys.el (org-babel-key-bindings): key bindings for block
  demarcation
* doc/orgcard.tex: documentation in the ref card
Eric Schulte 10 years ago
parent
commit
88b8b83951
3 changed files with 34 additions and 0 deletions
  1. 1 0
      doc/orgcard.tex
  2. 2 0
      lisp/ob-keys.el
  3. 31 0
      lisp/ob.el

+ 1 - 0
doc/orgcard.tex

@@ -483,6 +483,7 @@ formula, \kbd{:=} a field formula.
 \key{go to the head of the current code block}{C-c C-v u}
 \key{go to the next code block}{C-c C-v n}
 \key{go to the previous code block}{C-c C-v p}
+\key{demarcate a code block}{C-c C-v d}
 \key{execute the next key sequence in the code edit buffer}{C-c C-v x}
 \key{execute all code blocks in current buffer}{C-c C-v b}
 \key{execute all code blocks in current subtree}{C-c C-v s}

+ 2 - 0
lisp/ob-keys.el

@@ -67,6 +67,8 @@ functions which are assigned key bindings, and see
     ("b" . org-babel-execute-buffer)
     ("\C-s" . org-babel-execute-subtree)
     ("s" . org-babel-execute-subtree)
+    ("\C-d" . org-babel-demarcate-block)
+    ("d" . org-babel-demarcate-block)
     ("\C-t" . org-babel-tangle)
     ("t" . org-babel-tangle)
     ("\C-f" . org-babel-tangle-file)

+ 31 - 0
lisp/ob.el

@@ -1129,6 +1129,37 @@ With optional prefix argument ARG, jump backward ARG many source blocks."
     (error (error "No previous code blocks")))
   (goto-char (match-beginning 0)) (org-show-context))
 
+;;;###autoload
+(defun org-babel-demarcate-block (&optional arg)
+  "Wrap or split the code in the region or on the point.
+When called from inside of a code block the current block is
+split.  When called from outside of a code block a new code block
+is created.  In both cases if the region is demarcated and if the
+region is not active then the point is demarcated."
+  (interactive "P")
+  (let ((info (org-babel-get-src-block-info)))
+    (if info
+        (mapc
+         (lambda (place)
+           (save-excursion
+             (goto-char place)
+             (let ((lang (nth 0 info))
+                   (indent (make-string (nth 6 info) ? ))
+                   (stars (concat (make-string (org-current-level) ?*) " ")))
+               (insert (concat (if (looking-at "^") "" "\n")
+                               indent "#+end_src\n"
+                               (if arg stars indent) "\n"
+                               indent "#+begin_src " lang
+                               (if (looking-at "[\n\r]") "" "\n")))
+               (when arg (previous-line) (move-end-of-line 1)))))
+         (sort (if (region-active-p) (list (mark) (point)) (list (point))) #'>))
+      (insert (concat (if (looking-at "^") "" "\n")
+                      (if arg (concat stars "\n") "")
+                      "#+begin_src " (read-from-minibuffer "Lang: ") "\n"
+                      (delete-and-extract-region (or (mark) (point)) (point))
+                      "\n#+end_src"))
+      (previous-line) (move-end-of-line 1))))
+
 (defvar org-babel-lob-one-liner-regexp)
 (defun org-babel-where-is-src-block-result (&optional insert info hash indent)
   "Find where the current source block results begin.