From mboxrd@z Thu Jan 1 00:00:00 1970 From: Xebar Saram Subject: Re: Elisp function for wrapping sexp or region in src-block (was Re: some lisp help) Date: Tue, 29 Jul 2014 17:48:50 +0300 Message-ID: References: <87d2cos6cz.fsf@gmail.com> <8761igs5e8.fsf@gmail.com> <87y4vcqolr.fsf_-_@gmail.com> <87tx60qnl6.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=047d7b4148fe480e2b04ff562374 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:42520) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC8hx-000071-C4 for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 10:48:54 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XC8hv-0002kP-Tf for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 10:48:53 -0400 Received: from mail-oa0-x22d.google.com ([2607:f8b0:4003:c02::22d]:39720) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XC8hv-0002kG-MU for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 10:48:51 -0400 Received: by mail-oa0-f45.google.com with SMTP id i7so10440997oag.18 for ; Tue, 29 Jul 2014 07:48:51 -0700 (PDT) In-Reply-To: <87tx60qnl6.fsf@gmail.com> 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: Thorsten Jolitz Cc: org mode --047d7b4148fe480e2b04ff562374 Content-Type: text/plain; charset=UTF-8 Hi again looks great but i think there may be a bug. if there is 1 line then issues the function works perfect (although no prompt, which for 1 line is ideal). but using C-u before the M-x dosent seem to do anything at all, it just wraps the current line. do i need any dependencies or doing something wrong? thx alot again, i hope other people will find it as useful as i do (i literally use it 20-30 times a day....) On Tue, Jul 29, 2014 at 4:52 PM, Thorsten Jolitz wrote: > 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 > > > --047d7b4148fe480e2b04ff562374 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi again=C2=A0

looks great but i think = there may be a bug. if there is 1 line then issues the function works perfe= ct (although no prompt, which for 1 line is ideal). but using C-u before th= e M-x dosent seem to do anything at all, it just wraps the current line. do= i need any dependencies or doing something wrong?

thx alot again, i hope other people will find it as use= ful as i do (i literally use it 20-30 times a day....)


On Tue, Jul 29, 2014 a= t 4:52 PM, Thorsten Jolitz <tjolitz@gmail.com> wrote:
Thorsten Jolitz <tjolitz@gmail.com> writes:

> Xebar Saram <zeltakc@gmail.com= > 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)
=C2=A0 "Wrap sexp at point or region (point +-lines) in src bloc= k"
=C2=A0 (interactive
=C2=A0 =C2=A0(when current-prefix-arg
=C2=A0 =C2=A0 =C2=A0(list
=C2=A0 =C2=A0 =C2=A0 (ido-completing-read "Org-Babel language: "<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0(mapcar
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 (lambda (--lang)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (symbol-name (car --lang)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 org-babel-load-languages)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0nil nil nil nil "emacs-lisp")
=C2=A0 =C2=A0 =C2=A0 (read-number "Number of lines to wrap: " 1))= ))
=C2=A0 (let* ((language (or lang "emacs-lisp"))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(marker (point-marker))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(beg (point))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(bol (bolp))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(end (if lines
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (save-excurs= ion
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (forw= ard-line lines) (point))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (save-excursion
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (forward-sex= p) (point))))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(cut-strg (buffer-substring beg end))) =C2=A0 =C2=A0 (delete-region beg end)
=C2=A0 =C2=A0 (goto-char (marker-position marker))
=C2=A0 =C2=A0 (insert
=C2=A0 =C2=A0 =C2=A0(format
=C2=A0 =C2=A0 =C2=A0 "%s#+begin_src %s\n%s%s#+end_src\n" =C2=A0 =C2=A0 =C2=A0 (if bol "" "\n")
=C2=A0 =C2=A0 =C2=A0 language
=C2=A0 =C2=A0 =C2=A0 cut-strg
=C2=A0 =C2=A0 =C2=A0 (if lines "" "\n")))
=C2=A0 =C2=A0 (set-marker marker nil)))
#+end_src

#+begin_src emacs-lisp
(global-set-key (kbd "C-c w l")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (lambda ()
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (interactive= )
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (let ((curre= nt-prefix-arg '(4)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0(call-interactively
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 'tj/wrap-sexp-or-reg-in-src-block))))
#+end_src


#+begin_src emacs-lisp
(global-set-key (kbd "C-c w w")
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 'tj/wrap-sexp-o= r-reg-in-src-block)
#+end_src

--
cheers,
Thorsten



--047d7b4148fe480e2b04ff562374--