From mboxrd@z Thu Jan 1 00:00:00 1970 From: Thorsten Jolitz Subject: Re: Elisp function for wrapping sexp or region in src-block (was Re: some lisp help) Date: Tue, 29 Jul 2014 15:52:21 +0200 Message-ID: <87tx60qnl6.fsf@gmail.com> References: <87d2cos6cz.fsf@gmail.com> <8761igs5e8.fsf@gmail.com> <87y4vcqolr.fsf_-_@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:57859) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC7pc-0006PI-2v for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 09:52:52 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XC7pT-0007LG-Lv for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 09:52:44 -0400 Received: from plane.gmane.org ([80.91.229.3]:40704) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC7pT-0007LA-Ex for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 09:52:35 -0400 Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1XC7pR-0005Vq-ET for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 15:52:33 +0200 Received: from e178189064.adsl.alicedsl.de ([85.178.189.64]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 29 Jul 2014 15:52:33 +0200 Received: from tjolitz by e178189064.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 29 Jul 2014 15:52:33 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Thorsten Jolitz writes: > Xebar Saram 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: #+begin_src emacs-lisp (defun tj/wrap-sexp-or-reg-in-src-block (&optional lang lines) "Wrap sexp at point or region (point +-lines) in src block" (interactive (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)) (bol (bolp)) (end (if lines (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 "%s#+begin_src %s\n%s%s#+end_src\n" (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