From mboxrd@z Thu Jan 1 00:00:00 1970 From: Xebar Saram Subject: Re: [RFC] Add commmand for wrapping sexp/region in src-blocks to Org? Date: Sat, 2 Aug 2014 11:44:28 +0300 Message-ID: References: <87k36wqirh.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=089e012954dc8c90c804ffa183b1 Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:42156) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XDUvX-0008Ed-Uu for emacs-orgmode@gnu.org; Sat, 02 Aug 2014 04:44:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XDUvW-0007bR-36 for emacs-orgmode@gnu.org; Sat, 02 Aug 2014 04:44:31 -0400 Received: from mail-oa0-x229.google.com ([2607:f8b0:4003:c02::229]:62794) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XDUvV-0007bL-Re for emacs-orgmode@gnu.org; Sat, 02 Aug 2014 04:44:30 -0400 Received: by mail-oa0-f41.google.com with SMTP id j17so3793938oag.0 for ; Sat, 02 Aug 2014 01:44:28 -0700 (PDT) In-Reply-To: <87k36wqirh.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 --089e012954dc8c90c804ffa183b1 Content-Type: text/plain; charset=UTF-8 Hi Thorsten and list How are you? small question. ive been using your amazing function daily last few days and been lovin' it :) i wonder if i could request a tiny related feature (this may be very easy to do already). i would like to assign hotkeys for 2 scenarios: 1) pre selected language for 1 line 2) pre selected language prompting for number of lines to wrap so IE id assign F9-b to auto wrap current line with bash syntax while F9-l would wrap in lisp also F10-b would prompt me how man lines to wrap in bash etc is that possible? thanks you again for all your wonderful work kind regards z On Tue, Jul 29, 2014 at 6:36 PM, Thorsten Jolitz wrote: > > Hi List, > > I often missed the following command for wrapping existing content into > src-blocks in the past, besides knowing about > `org-babel-demarcate-block' and easy template insertion. Would it make > sense to add it to Org-mode? > > I made it cover several use cases: > > - Wrap sexp at point - perfect when on first paren of a nested list, > or when a string should be wrapped in a programming-mode, or a > word in a text-mode. > > - Wrap region between point and +/- N lines forward/backward (very > fast because no point movement is involved). > > - Wrap active region. > > Usage: > > 1. wrap active region or (if none) sexp-at-point in emacs-lisp src-block > > ,---- > | M-x org-wrap-in-src-block > `---- > > 2. wrap active region or (if none) sexp-at-point in src-block, prompt > user for Org-Babel language > > ,---- > | C-u M-x org-wrap-in-src-block > `---- > > 3. prompt user for Org-Babel language and number of lines to wrap > (forward or backward) > > ,---- > | C-u C-u M-x org-wrap-in-src-block > `---- > > > #+begin_src emacs-lisp > (defun org-wrap-in-src-block (&optional lang lines) > "Wrap sexp-at-point or region in src-block. > > Use Org-Babel LANGuage for the src-block if given, Emacs-Lisp > otherwise. A region instead of the sexp-at-point is wrapped if > either > > - optional argument LINES is an (positive or negative) integer > - or the region is active > > In the first case the region is determined by moving +/- LINES > forward/backward from point using `forward-line', in the second > case the active region is used. > > When called with prefix argument 'C-u', prompt the user for the > Org-Babel language to use. When called with two prefix arguments > 'C-u C-u', prompt the user for both the Org-Babel language to use > and the number of lines to be wrapped." > (interactive > (cond > ((equal current-prefix-arg nil) nil) > ((equal current-prefix-arg '(4)) > (list > (ido-completing-read "Org-Babel language: " > (mapcar > (lambda (--lang) > (symbol-name (car --lang))) > org-babel-load-languages) > nil nil nil nil "emacs-lisp"))) > ((equal current-prefix-arg '(16)) > (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")) > (beg (or (and (not lines) > (region-active-p) > (region-beginning)) > (point))) > (marker (save-excursion (goto-char beg) (point-marker))) > (bol (save-excursion (goto-char beg) (bolp))) > (end (cond > (lines (save-excursion > (forward-line lines) (point))) > ((region-active-p)(region-end)) > (t (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 (or (and lines (< lines 0)) bol) "" "\n") > language > cut-strg > (if lines "" "\n"))) > (set-marker marker nil))) > #+end_src > > -- > cheers, > Thorsten > > > --089e012954dc8c90c804ffa183b1 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable
Hi Thorsten and list

How are you?
=

small question. ive been using your amazing function da= ily last few days and been lovin' it :)

i wond= er if i could request a tiny related feature (this may be very easy to do a= lready).
i would like to assign hotkeys for 2 scenarios:

1) pre selected language for 1 line
2) pre selected language p= rompting for number of lines to wrap

so IE id assi= gn F9-b to auto wrap current line with bash syntax while F9-l would wrap in= lisp
also F10-b would prompt me how man lines to wrap in bash etc

is that possible? thanks you again for all your wonderful w= ork

kind regards

z



On Tue, Jul 29, 2014 at 6:36 PM, Thorsten Jolitz = <tjolitz@gmail.co= m> wrote:

Hi List,

I often missed the following command for wrapping existing content into
src-blocks in the past, besides knowing about
`org-babel-demarcate-block' and easy template insertion. Would it make<= br> sense to add it to Org-mode?

I made it cover several use cases:

=C2=A0 - Wrap sexp at point - perfect when on first paren of a nested list,=
=C2=A0 =C2=A0 or when a string should be wrapped in a programming-mode, or = a
=C2=A0 =C2=A0 word in a text-mode.

=C2=A0 - Wrap region between point and +/- N lines forward/backward (very =C2=A0 =C2=A0 fast because no point movement is involved).

=C2=A0 - Wrap active region.

Usage:

1. wrap active region or (if none) sexp-at-point in emacs-lisp src-block
,----
| M-x org-wrap-in-src-block
`----

2. wrap active region or (if none) sexp-at-point in src-block, prompt
=C2=A0 =C2=A0user for Org-Babel language

,----
| C-u M-x org-wrap-in-src-block
`----

3. prompt user for Org-Babel language and number of lines to wrap
(forward or backward)

,----
| C-u C-u M-x org-wrap-in-src-block
`----


#+begin_src emacs-lisp
(defun org-wrap-in-src-block (&optional lang lines)
=C2=A0 "Wrap sexp-at-point or region in src-block.

Use Org-Babel LANGuage for the src-block if given, Emacs-Lisp
otherwise. A region instead of the sexp-at-point is wrapped if
either

=C2=A0 =C2=A0- optional argument LINES is an (positive or negative) integer=
=C2=A0 =C2=A0- or the region is active

In the first case the region is determined by moving +/- LINES
forward/backward from point using `forward-line', in the second
case the active region is used.

When called with prefix argument 'C-u', prompt the user for the
Org-Babel language to use. When called with two prefix arguments
'C-u C-u', prompt the user for both the Org-Babel language to use and the number of lines to be wrapped."
=C2=A0 (interactive
=C2=A0 =C2=A0(cond
=C2=A0 =C2=A0 ((equal current-prefix-arg nil) nil)
=C2=A0 =C2=A0 ((equal current-prefix-arg '(4))
=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 ((equal current-prefix-arg '(16))
=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(beg (or (and (not lines)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0(region-active-p)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0(region-beginning))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (point))) =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(marker (save-excursion (goto-char beg) (= point-marker)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(bol (save-excursion (goto-char beg) (bol= p)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(end (cond
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(lines (save-excursi= on
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 (forward-line lines) (point)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0((region-active-p)(r= egion-end))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(t (save-excursion =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (forw= ard-sexp) (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 (or (and lines (< lines 0)) bol) "" &= quot;\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

--
cheers,
Thorsten



--089e012954dc8c90c804ffa183b1--