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
[-- Attachment #1: Type: text/plain, Size: 485 bytes --] Hi Thorsten, On 2014-07-29 17:36, Thorsten Jolitz <tjolitz@gmail.com> writes: > 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 find this most useful and have added it in my configuration. It would make sense to add it to org-mode. Thanks, Alan -- OpenPGP Key ID : 040D0A3B4ED2E5C7 [-- Attachment #2: Type: application/pgp-signature, Size: 494 bytes --]
[-- Attachment #1: Type: text/plain, Size: 4344 bytes --] 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 <tjolitz@gmail.com> 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 > > > [-- Attachment #2: Type: text/html, Size: 5772 bytes --]
Hello, Thorsten Jolitz <tjolitz@gmail.com> writes: > 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 region between point and +/- N lines forward/backward (very > fast because no point movement is involved). This is not really useful as, with muscle memory, you'll probably mark the appropriate region instead of counting lines. Also it requires the point to be at the center of the area you want to wrap. > - Wrap active region. Why limiting it to source blocks? One may want to wrap region with, e.g., a quote block. Also, with a prefix argument, it would be nice to edit the block containing point (change its type), or remove it completely. Regards, -- Nicolas Goaziou
Xebar Saram <zeltakc@gmail.com> writes: Hi Xebar, > small question. sorry for the late answer > 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? I think so, I have some predefined calls to that function with global keybindings in my init file (right now I call it `tj/wrap-in-src-block', you might have to adapt this): (global-set-key (kbd "C-c w l") (lambda () (interactive) (let ((current-prefix-arg '(4))) (call-interactively 'tj/wrap-in-src-block)))) (global-set-key (kbd "C-c w n") (lambda () (interactive) (let ((current-prefix-arg '(16))) (call-interactively 'tj/wrap-in-src-block)))) (global-set-key (kbd "C-c w w") 'tj/wrap-in-src-block) > 1) pre selected language for 1 line e.g. (global-set-key (kbd "C-c w y") (lambda () (interactive) (tj/wrap-in-src-block "shell" 1))) > 2) pre selected language prompting for number of lines to wrap emacs-lisp is kind of preselected, but you could add this after the ((equal current-prefix-arg '(16)) ...) part ((equal current-prefix-arg '(64)) (list "shell" (read-number "Number of lines to wrap: " 1))) and then (global-set-key (kbd "C-c w z") (lambda () (interactive) (let ((current-prefix-arg '(64))) (call-interactively 'tj/wrap-in-src-block)))) everything untested, unfortunately ... -- cheers, Thorsten
Nicolas Goaziou <mail@nicolasgoaziou.fr> writes:
Hello,
> Why limiting it to ...
I'll leave it as personnal init.el customization, maybe it evolves over
time towards a function that deals with the general case. Thanks for the
comments though.
--
cheers,
Thorsten
[-- Attachment #1: Type: text/plain, Size: 2268 bytes --] thanks Thorsten its perfect z On Tue, Aug 5, 2014 at 4:15 PM, Thorsten Jolitz <tjolitz@gmail.com> wrote: > Xebar Saram <zeltakc@gmail.com> writes: > > Hi Xebar, > > > small question. > > sorry for the late answer > > > 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? > > I think so, I have some predefined calls to that function with global > keybindings in my init file (right now I call it `tj/wrap-in-src-block', > you might have to adapt this): > > (global-set-key (kbd "C-c w l") > (lambda () > (interactive) > (let ((current-prefix-arg '(4))) > (call-interactively > 'tj/wrap-in-src-block)))) > > (global-set-key (kbd "C-c w n") > (lambda () > (interactive) > (let ((current-prefix-arg '(16))) > (call-interactively > 'tj/wrap-in-src-block)))) > > (global-set-key (kbd "C-c w w") 'tj/wrap-in-src-block) > > > > 1) pre selected language for 1 line > > e.g. > (global-set-key (kbd "C-c w y") > (lambda () > (interactive) > (tj/wrap-in-src-block "shell" 1))) > > > > 2) pre selected language prompting for number of lines to wrap > > emacs-lisp is kind of preselected, but you could add this after the > ((equal current-prefix-arg '(16)) ...) part > > > ((equal current-prefix-arg '(64)) > (list > "shell" > (read-number "Number of lines to wrap: " 1))) > > and then > > (global-set-key (kbd "C-c w z") > (lambda () > (interactive) > (let ((current-prefix-arg '(64))) > (call-interactively > 'tj/wrap-in-src-block)))) > > everything untested, unfortunately ... > -- > cheers, > Thorsten > > > [-- Attachment #2: Type: text/html, Size: 3393 bytes --]
Xebar Saram <zeltakc@gmail.com> writes: > thanks Thorsten > > its perfect well, not perfect yet since, as Nicolas mentioned, it does not cover all possible use cases (wrap/unwrap/modify ALL kinds of Org blocks with or without header-line params and with or without affiliated keywords or with a combination of both). I got pretty far in implementing this, but spent too much time - maybe I can deliver the general 'all-inclusive' version later, I hope so, its useful. > On Tue, Aug 5, 2014 at 4:15 PM, Thorsten Jolitz <tjolitz@gmail.com> > wrote: > > Xebar Saram <zeltakc@gmail.com> writes: > > Hi Xebar, > > > small question. > > sorry for the late answer > > > > 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? > > > I think so, I have some predefined calls to that function with > global > keybindings in my init file (right now I call it > `tj/wrap-in-src-block', > you might have to adapt this): > > (global-set-key (kbd "C-c w l") > (lambda () > (interactive) > (let ((current-prefix-arg '(4))) > (call-interactively > 'tj/wrap-in-src-block)))) > > (global-set-key (kbd "C-c w n") > (lambda () > (interactive) > (let ((current-prefix-arg '(16))) > (call-interactively > 'tj/wrap-in-src-block)))) > > (global-set-key (kbd "C-c w w") 'tj/wrap-in-src-block) > > > > > 1) pre selected language for 1 line > > > e.g. > (global-set-key (kbd "C-c w y") > (lambda () > (interactive) > (tj/wrap-in-src-block "shell" 1))) > > > > > 2) pre selected language prompting for number of lines to wrap > > > emacs-lisp is kind of preselected, but you could add this after > the > ((equal current-prefix-arg '(16)) ...) part > > > ((equal current-prefix-arg '(64)) > (list > "shell" > > (read-number "Number of lines to wrap: " 1))) > > > and then > > (global-set-key (kbd "C-c w z") > (lambda () > (interactive) > (let ((current-prefix-arg '(64))) > (call-interactively > 'tj/wrap-in-src-block)))) > > everything untested, unfortunately ... > -- > cheers, > Thorsten -- cheers, Thorsten
[-- Attachment #1: Type: text/plain, Size: 3041 bytes --] that sounds great :) looking forward to the next iterations ;-) thanks again for all your hard work Z On Wed, Aug 6, 2014 at 3:14 PM, Thorsten Jolitz <tjolitz@gmail.com> wrote: > Xebar Saram <zeltakc@gmail.com> writes: > > > thanks Thorsten > > > > its perfect > > well, not perfect yet since, as Nicolas mentioned, it does not cover all > possible use cases (wrap/unwrap/modify ALL kinds of Org blocks with or > without header-line params and with or without affiliated keywords or > with a combination of both). > > I got pretty far in implementing this, but spent too much time - maybe I > can deliver the general 'all-inclusive' version later, I hope so, its > useful. > > > On Tue, Aug 5, 2014 at 4:15 PM, Thorsten Jolitz <tjolitz@gmail.com> > > wrote: > > > > Xebar Saram <zeltakc@gmail.com> writes: > > > > Hi Xebar, > > > > > small question. > > > > sorry for the late answer > > > > > > > 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? > > > > > > I think so, I have some predefined calls to that function with > > global > > keybindings in my init file (right now I call it > > `tj/wrap-in-src-block', > > you might have to adapt this): > > > > (global-set-key (kbd "C-c w l") > > (lambda () > > (interactive) > > (let ((current-prefix-arg '(4))) > > (call-interactively > > 'tj/wrap-in-src-block)))) > > > > (global-set-key (kbd "C-c w n") > > (lambda () > > (interactive) > > (let ((current-prefix-arg '(16))) > > (call-interactively > > 'tj/wrap-in-src-block)))) > > > > (global-set-key (kbd "C-c w w") 'tj/wrap-in-src-block) > > > > > > > > > 1) pre selected language for 1 line > > > > > > e.g. > > (global-set-key (kbd "C-c w y") > > (lambda () > > (interactive) > > (tj/wrap-in-src-block "shell" 1))) > > > > > > > > > 2) pre selected language prompting for number of lines to wrap > > > > > > emacs-lisp is kind of preselected, but you could add this after > > the > > ((equal current-prefix-arg '(16)) ...) part > > > > > > ((equal current-prefix-arg '(64)) > > (list > > "shell" > > > > (read-number "Number of lines to wrap: " 1))) > > > > > > and then > > > > (global-set-key (kbd "C-c w z") > > (lambda () > > (interactive) > > (let ((current-prefix-arg '(64))) > > (call-interactively > > 'tj/wrap-in-src-block)))) > > > > everything untested, unfortunately ... > > -- > > cheers, > > Thorsten > > > -- > cheers, > Thorsten > > > [-- Attachment #2: Type: text/html, Size: 4574 bytes --]