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 19:23:21 +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=001a11c21b064347f804ff5775d5 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:60716) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XCABQ-0007Z3-HN for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 12:23:25 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XCABO-0000DY-4z for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 12:23:24 -0400 Received: from mail-oi0-x234.google.com ([2607:f8b0:4003:c06::234]:47713) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XCABN-0000DN-VG for emacs-orgmode@gnu.org; Tue, 29 Jul 2014 12:23:22 -0400 Received: by mail-oi0-f52.google.com with SMTP id h136so7493575oig.25 for ; Tue, 29 Jul 2014 09:23:21 -0700 (PDT) In-Reply-To: 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 --001a11c21b064347f804ff5775d5 Content-Type: text/plain; charset=UTF-8 ok ignore above email, i just saw the previous email..damm gmail..grrr z On Tue, Jul 29, 2014 at 7:21 PM, Xebar Saram wrote: > hi i fell like an idiot but what is RFC, is that a repo of some sorts, > would you mind pasting a link? > > sorry about that > > Z > > > On Tue, Jul 29, 2014 at 5:48 PM, Xebar Saram wrote: > >> 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 >>> >>> >>> >> > --001a11c21b064347f804ff5775d5 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
ok ignore above email, i just saw the previous email..damm= gmail..grrr

z

On Tue, Jul 29, 2014 at 7:21 PM, Xebar Sara= m <zeltakc@gmail.com> wrote:
hi i fell like an idiot but= what is RFC, is that a repo of some sorts, would you mind pasting a link?<= div>
sorry about that

Z


On Tue, Jul 29, 2014 at 5:48 PM, Xebar Saram <z= eltakc@gmail.com> wrote:
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 at 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





--001a11c21b064347f804ff5775d5--