|
From: | Xebar Saram |
Subject: | Re: [O] Elisp function for wrapping sexp or region in src-block (was Re: some lisp help) |
Date: | Tue, 29 Jul 2014 17:48:50 +0300 |
Thorsten Jolitz <address@hidden> writes:
> Xebar Saram <address@hidden> writes:
This is what I got in my init.el now (improved version with global
keybindings). And I like, will probably become one of those commands I
use all the time.
I already used it to wrap the following code in 3 src-blocks:
"Wrap sexp at point or region (point +-lines) in src block"
#+begin_src emacs-lisp
(defun tj/wrap-sexp-or-reg-in-src-block (&optional lang lines)
(interactive(bol (bolp))
(when current-prefix-arg
(list
(ido-completing-read "Org-Babel language: "
(mapcar
(lambda (--lang)
(symbol-name (car --lang)))
org-babel-load-languages)
nil nil nil nil "emacs-lisp")
(read-number "Number of lines to wrap: " 1))))
(let* ((language (or lang "emacs-lisp"))
(marker (point-marker))
(beg (point))
(end (if lines"%s#+begin_src %s\n%s%s#+end_src\n"
(save-excursion
(forward-line lines) (point))
(save-excursion
(forward-sexp) (point))))
(cut-strg (buffer-substring beg end)))
(delete-region beg end)
(goto-char (marker-position marker))
(insert
(format
(if bol "" "\n")
language
cut-strg
(if lines "" "\n")))
(set-marker marker nil)))
#+end_src
#+begin_src emacs-lisp
(global-set-key (kbd "C-c w l")
(lambda ()
(interactive)
(let ((current-prefix-arg '(4)))
(call-interactively
'tj/wrap-sexp-or-reg-in-src-block))))
#+end_src
#+begin_src emacs-lisp
(global-set-key (kbd "C-c w w")
'tj/wrap-sexp-or-reg-in-src-block)
#+end_src
--
cheers,
Thorsten
[Prev in Thread] | Current Thread | [Next in Thread] |