* [PATCH] org-babel-execute-src-block-region @ 2015-11-02 2:17 Carlos Henrique Machado S Esteves 2015-11-02 18:19 ` Charles C. Berry 0 siblings, 1 reply; 9+ messages in thread From: Carlos Henrique Machado S Esteves @ 2015-11-02 2:17 UTC (permalink / raw) To: emacs-orgmode [-- Attachment #1.1: Type: text/plain, Size: 262 bytes --] Hello, I find it useful to be able to execute only a region of a source code block, so I've implemented a new function for that. I've tested it with MATLAB and Python, but it should work for any mode, since it calls org-babel-execute-src-block. Thanks, Carlos [-- Attachment #1.2: Type: text/html, Size: 341 bytes --] [-- Attachment #2: 0001-ob-core.el-Allow-execution-of-region-of-source-code-.patch --] [-- Type: text/x-patch, Size: 1353 bytes --] From d3f67fb0f37667af39a30c527221a43ea37999a9 Mon Sep 17 00:00:00 2001 From: Carlos HMS Esteves <ch.machado.esteves@gmail.com> Date: Sun, 1 Nov 2015 20:52:26 -0500 Subject: [PATCH] ob-core.el: Allow execution of region of source code block * ob-core.el (org-babel-execute-src-block-region): Execute only active region of the current source block. Same as `org-babel-execute-src-block', but use only the active region instead of the whole block. TINYCHANGE --- lisp/ob-core.el | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index 51a34f3..98ed69e 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -740,6 +740,15 @@ block." (setq call-process-region 'org-babel-call-process-region-original))))))))) +(defun org-babel-execute-src-block-region (beg end) + "Execute region in the current source code block. +`org-babel-execure-src-block' is called; the only change is that +only the active region is sent, instead of the whole block." + (interactive "r") + (let ((info (org-babel-get-src-block-info))) + (setcar (nthcdr 1 info) (buffer-substring beg end)) + (org-babel-execute-src-block nil info))) + (defun org-babel-expand-body:generic (body params &optional var-lines) "Expand BODY with PARAMS. Expand a block of code with org-babel according to its header -- 2.1.4 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-02 2:17 [PATCH] org-babel-execute-src-block-region Carlos Henrique Machado S Esteves @ 2015-11-02 18:19 ` Charles C. Berry 2015-11-10 18:19 ` Carlos Henrique Machado S Esteves 0 siblings, 1 reply; 9+ messages in thread From: Charles C. Berry @ 2015-11-02 18:19 UTC (permalink / raw) To: Carlos Henrique Machado S Esteves; +Cc: emacs-orgmode On Sun, 1 Nov 2015, Carlos Henrique Machado S Esteves wrote: > Hello, I find it useful to be able to execute only a region of a source > code block, so I've implemented a new function for that. I've tested it > with MATLAB and Python, but it should work for any mode, since it calls > org-babel-execute-src-block. > You might want to check that point and mark are both inside the src block. Otherwise, the results are unpredictable. Also note that: ess-mode, python-mode, sh-mode and octave-mode already provide this capability (and a lot more) for R, python, shell scripts, octave/matlab and some other languages from the edit buffer. `org-babel-demarcate-block' gives the user the ability to break up src blocks into smaller pieces so they can be run independently. Best, Chuck ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-02 18:19 ` Charles C. Berry @ 2015-11-10 18:19 ` Carlos Henrique Machado S Esteves 2015-11-11 0:08 ` Nicolas Goaziou 2015-11-11 5:10 ` Xebar Saram 0 siblings, 2 replies; 9+ messages in thread From: Carlos Henrique Machado S Esteves @ 2015-11-10 18:19 UTC (permalink / raw) To: Charles C. Berry; +Cc: emacs-orgmode [-- Attachment #1.1: Type: text/plain, Size: 1968 bytes --] Hello Chuck, Thank you for the feedback. > You might want to check that point and mark are both inside the src block. Otherwise, the results are unpredictable. You are right, I've updated the patch. > ess-mode, python-mode, sh-mode and octave-mode already provide this capability (and a lot more) for R, python, shell scripts, octave/matlab and some other languages from the edit buffer. I understand that. My idea is to avoid the need to C-c ' back and forth. Actually that's a problem I have with org-babel; I usually find myself coding inside the src block and losing many of the major mode functionalities; switching to the edit buffer often seem too much of a hassle. Any tips about how to get more major-mode functionalities inside the src-block? Or should I develop the muscle memory to switch back and forth to the edit buffer all the time? > `org-babel-demarcate-block' gives the user the ability to break up src blocks into smaller pieces so they can be run independently. Thanks for pointing that; I didn't know about `org-babel-demarcate-block'. Best, Carlos 2015-11-02 13:19 GMT-05:00 Charles C. Berry <ccberry@ucsd.edu>: > On Sun, 1 Nov 2015, Carlos Henrique Machado S Esteves wrote: > > Hello, I find it useful to be able to execute only a region of a source >> code block, so I've implemented a new function for that. I've tested it >> with MATLAB and Python, but it should work for any mode, since it calls >> org-babel-execute-src-block. >> >> > You might want to check that point and mark are both inside the src block. > Otherwise, the results are unpredictable. > > Also note that: > > ess-mode, python-mode, sh-mode and octave-mode already provide this > capability (and a lot more) for R, python, shell scripts, octave/matlab and > some other languages from the edit buffer. > > `org-babel-demarcate-block' gives the user the ability to break up src > blocks into smaller pieces so they can be run independently. > > Best, > > Chuck > > [-- Attachment #1.2: Type: text/html, Size: 2623 bytes --] [-- Attachment #2: 0001-ob-core.el-Allow-execution-of-region-of-source-code-.patch --] [-- Type: text/x-patch, Size: 1988 bytes --] From b677ad4416d9e8dd564a46fcb4136cf6f5a90b29 Mon Sep 17 00:00:00 2001 From: Carlos HMS Esteves <ch.machado.esteves@gmail.com> Date: Sun, 1 Nov 2015 20:52:26 -0500 Subject: [PATCH] ob-core.el: Allow execution of region of source code block * ob-core.el (org-babel-execute-src-block-region): Execute only active region of the current source block. Same as `org-babel-execute-src-block', but use only the active region instead of the whole block. TINYCHANGE --- lisp/ob-core.el | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/lisp/ob-core.el b/lisp/ob-core.el index b8ea12d..a2ef6c1 100644 --- a/lisp/ob-core.el +++ b/lisp/ob-core.el @@ -739,6 +739,34 @@ block." (setq call-process-region 'org-babel-call-process-region-original))))))))) +(defun org-babel-execute-src-block-region (beg end) + "Execute region in the current source code block. +`org-babel-execure-src-block' is called; the only change is that +only the active region is sent, instead of the whole block." + (interactive "r") + (if (org-babel-is-region-within-src-block beg end) + (let ((info (org-babel-get-src-block-info))) + (setcar (nthcdr 1 info) (buffer-substring beg end)) + (org-babel-execute-src-block nil info)) + (message "Region not in src-block!"))) + +(defun org-babel-is-region-within-src-block (beg end) + "Check if region is within a single src-block. +Block header and footer are ignored, so we are checking for the +source code only. +Used by `org-babel-execute-src-block-region' to check if region +is executable." + (save-excursion + (eq + (progn + (goto-char beg) + (forward-line -1) + (org-babel-where-is-src-block-head)) + (progn + (goto-char end) + (forward-line 1) + (org-babel-where-is-src-block-head))))) + (defun org-babel-expand-body:generic (body params &optional var-lines) "Expand BODY with PARAMS. Expand a block of code with org-babel according to its header -- 2.1.4 ^ permalink raw reply related [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-10 18:19 ` Carlos Henrique Machado S Esteves @ 2015-11-11 0:08 ` Nicolas Goaziou 2015-11-11 5:10 ` Xebar Saram 1 sibling, 0 replies; 9+ messages in thread From: Nicolas Goaziou @ 2015-11-11 0:08 UTC (permalink / raw) To: Carlos Henrique Machado S Esteves; +Cc: emacs-orgmode, Charles C. Berry Hello, Carlos Henrique Machado S Esteves <ch.machado.esteves@gmail.com> writes: > You are right, I've updated the patch. Thank you. Some comments follow. > * ob-core.el (org-babel-execute-src-block-region): Execute only active region of > the current source block. Same as `org-babel-execute-src-block', but > use only the active region instead of the whole block. It should be: * ob-core.el (org-babel-execute-src-block-region, org-babel-is-region-within-src-block): New functions. Note that you may want to rename the latter `org-babel--region-within-src-block-p' since it is an internal predicate. > +(defun org-babel-execute-src-block-region (beg end) > + "Execute region in the current source code block. > +`org-babel-execure-src-block' is called; the only change is that > +only the active region is sent, instead of the whole block." You need to reference BEG and END arguments. It could be as simple as "BEG and END mark the limit of the region." > + (interactive "r") > + (if (org-babel-is-region-within-src-block beg end) > + (let ((info (org-babel-get-src-block-info))) > + (setcar (nthcdr 1 info) (buffer-substring beg end)) Nitpick time: (setf (nth 1 info) (buffer-substring beg end)) is clearer, IMO. > + (org-babel-execute-src-block nil info)) > + (message "Region not in src-block!"))) Isn't it a user error instead? In this case, please remove exclamation mark the end of the message. > +(defun org-babel-is-region-within-src-block (beg end) > + "Check if region is within a single src-block. Non-nil if region is within the code part of a source block. > +Block header and footer are ignored, so we are checking for the > +source code only. > +Used by `org-babel-execute-src-block-region' to check if region > +is executable." > + (save-excursion > + (eq > + (progn > + (goto-char beg) > + (forward-line -1) > + (org-babel-where-is-src-block-head)) > + (progn > + (goto-char end) > + (forward-line 1) > + (org-babel-where-is-src-block-head))))) I think the following is more efficient (untested, though) (org-with-wide-buffer (goto-char beg) (let ((case-fold-search t) (element (org-element-at-point))) (and (eq (org-element-type element) 'src-block) (> (line-beginning-position) (org-element-property :post-affiliated element)) (> (progn (goto-char (org-element-property :end element)) (skip-chars-backward " \t\n") (line-beginning-position)) end)))) Regards, -- Nicolas Goaziou ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-10 18:19 ` Carlos Henrique Machado S Esteves 2015-11-11 0:08 ` Nicolas Goaziou @ 2015-11-11 5:10 ` Xebar Saram 2015-11-11 13:44 ` Ista Zahn 1 sibling, 1 reply; 9+ messages in thread From: Xebar Saram @ 2015-11-11 5:10 UTC (permalink / raw) To: Carlos Henrique Machado S Esteves; +Cc: org mode, Charles C. Berry [-- Attachment #1: Type: text/plain, Size: 2348 bytes --] Hi I actually find it annoying in ESS to keep pressing C-c ' each time i want to eval a single line of code in a code block. if this could work for R code this would be fantastic for me thx Z On Tue, Nov 10, 2015 at 8:19 PM, Carlos Henrique Machado S Esteves < ch.machado.esteves@gmail.com> wrote: > Hello Chuck, > > Thank you for the feedback. > > > You might want to check that point and mark are both inside the src > block. Otherwise, the results are unpredictable. > You are right, I've updated the patch. > > > ess-mode, python-mode, sh-mode and octave-mode already provide this > capability (and a lot more) for R, python, shell scripts, octave/matlab and > some other languages from the edit buffer. > I understand that. My idea is to avoid the need to C-c ' back and forth. > Actually that's a problem I have with org-babel; I usually find myself > coding inside the src block and losing many of the major mode > functionalities; switching to the edit buffer often seem too much of a > hassle. Any tips about how to get more major-mode functionalities inside > the src-block? Or should I develop the muscle memory to switch back and > forth to the edit buffer all the time? > > > `org-babel-demarcate-block' gives the user the ability to break up src > blocks into smaller pieces so they can be run independently. > Thanks for pointing that; I didn't know about `org-babel-demarcate-block'. > > Best, > > Carlos > > 2015-11-02 13:19 GMT-05:00 Charles C. Berry <ccberry@ucsd.edu>: > >> On Sun, 1 Nov 2015, Carlos Henrique Machado S Esteves wrote: >> >> Hello, I find it useful to be able to execute only a region of a source >>> code block, so I've implemented a new function for that. I've tested it >>> with MATLAB and Python, but it should work for any mode, since it calls >>> org-babel-execute-src-block. >>> >>> >> You might want to check that point and mark are both inside the src >> block. Otherwise, the results are unpredictable. >> >> Also note that: >> >> ess-mode, python-mode, sh-mode and octave-mode already provide this >> capability (and a lot more) for R, python, shell scripts, octave/matlab and >> some other languages from the edit buffer. >> >> `org-babel-demarcate-block' gives the user the ability to break up src >> blocks into smaller pieces so they can be run independently. >> >> Best, >> >> Chuck >> >> > [-- Attachment #2: Type: text/html, Size: 3409 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-11 5:10 ` Xebar Saram @ 2015-11-11 13:44 ` Ista Zahn 2015-11-11 17:43 ` Charles C. Berry 0 siblings, 1 reply; 9+ messages in thread From: Ista Zahn @ 2015-11-11 13:44 UTC (permalink / raw) To: Xebar Saram Cc: Carlos Henrique Machado S Esteves, emacs-orgmode Mailinglist, Charles Berry [-- Attachment #1: Type: text/plain, Size: 2757 bytes --] I recommend using polymode[1] as a more general solution to the need to interact with code blocks without pulling up a separate edit buffer. It seamlessly switches major modes when point is inside a code block. Best, Ista [1] https://github.com/vspinu/polymode On Nov 11, 2015 12:11 AM, "Xebar Saram" <zeltakc@gmail.com> wrote: > Hi > > I actually find it annoying in ESS to keep pressing C-c ' each time i want > to eval a single line of code in a code block. if this could work for R > code this would be fantastic for me > > thx > > Z > > On Tue, Nov 10, 2015 at 8:19 PM, Carlos Henrique Machado S Esteves < > ch.machado.esteves@gmail.com> wrote: > >> Hello Chuck, >> >> Thank you for the feedback. >> >> > You might want to check that point and mark are both inside the src >> block. Otherwise, the results are unpredictable. >> You are right, I've updated the patch. >> >> > ess-mode, python-mode, sh-mode and octave-mode already provide this >> capability (and a lot more) for R, python, shell scripts, octave/matlab and >> some other languages from the edit buffer. >> I understand that. My idea is to avoid the need to C-c ' back and forth. >> Actually that's a problem I have with org-babel; I usually find myself >> coding inside the src block and losing many of the major mode >> functionalities; switching to the edit buffer often seem too much of a >> hassle. Any tips about how to get more major-mode functionalities inside >> the src-block? Or should I develop the muscle memory to switch back and >> forth to the edit buffer all the time? >> >> > `org-babel-demarcate-block' gives the user the ability to break up src >> blocks into smaller pieces so they can be run independently. >> Thanks for pointing that; I didn't know about `org-babel-demarcate-block'. >> >> Best, >> >> Carlos >> >> 2015-11-02 13:19 GMT-05:00 Charles C. Berry <ccberry@ucsd.edu>: >> >>> On Sun, 1 Nov 2015, Carlos Henrique Machado S Esteves wrote: >>> >>> Hello, I find it useful to be able to execute only a region of a source >>>> code block, so I've implemented a new function for that. I've tested it >>>> with MATLAB and Python, but it should work for any mode, since it calls >>>> org-babel-execute-src-block. >>>> >>>> >>> You might want to check that point and mark are both inside the src >>> block. Otherwise, the results are unpredictable. >>> >>> Also note that: >>> >>> ess-mode, python-mode, sh-mode and octave-mode already provide this >>> capability (and a lot more) for R, python, shell scripts, octave/matlab and >>> some other languages from the edit buffer. >>> >>> `org-babel-demarcate-block' gives the user the ability to break up src >>> blocks into smaller pieces so they can be run independently. >>> >>> Best, >>> >>> Chuck >>> >>> >> > [-- Attachment #2: Type: text/html, Size: 4017 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-11 13:44 ` Ista Zahn @ 2015-11-11 17:43 ` Charles C. Berry 2015-11-11 18:55 ` Xebar Saram 0 siblings, 1 reply; 9+ messages in thread From: Charles C. Berry @ 2015-11-11 17:43 UTC (permalink / raw) To: Ista Zahn Cc: Xebar Saram, Carlos Henrique Machado S Esteves, emacs-orgmode Mailinglist On Wed, 11 Nov 2015, Ista Zahn wrote: > I recommend using polymode[1] as a more general solution to the need to > interact with code blocks without pulling up a separate edit buffer. It > seamlessly switches major modes when point is inside a code block. > > Best, > Ista > > [1] https://github.com/vspinu/polymode Ista, Thanks for this pointer. I've been meaning to check out polymode for some time, and your posting gave me the push to try it out. For anyone who wants to try it, after cloning into ~/elisp/polymode I only needed to add this in my init (ESS was already in my init). #+BEGIN_SRC emacs-lisp (setq load-path (append '("~/elisp/polymode" "~/elisp/polymode/modes") load-path)) (require 'poly-org) (add-to-list 'auto-mode-alist '("\\.org" . poly-org-mode)) #+END_SRC When point is inside a src-block the local mode prevails. So, C-c C-n inside an R src block sends the line point is on to the R process. C-c C-c sends the region (or function or paragraph) to the process when point is in the body. To execute `org-ctrl-c-ctrl-c' on the src block with the C-c C-c keying, it is necessary to move point to before or after the body. --- Now that I have tried this, I agree that it is better to let polymode handle this kind of interaction than to try to build it into Org mode. Best, Chuck ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-11 17:43 ` Charles C. Berry @ 2015-11-11 18:55 ` Xebar Saram 2015-11-11 21:53 ` Charles C. Berry 0 siblings, 1 reply; 9+ messages in thread From: Xebar Saram @ 2015-11-11 18:55 UTC (permalink / raw) To: Charles C. Berry Cc: Carlos Henrique Machado S Esteves, emacs-orgmode Mailinglist, Ista Zahn [-- Attachment #1: Type: text/plain, Size: 1995 bytes --] thanks Chuck this sounds very interesting! i installed polymode from melpa and added this config #+BEGIN_SRC emacs-lisp :results none (use-package polymode :ensure t :config (require 'poly-R) (require 'poly-org) (add-to-list 'auto-mode-alist '("\\.org" . poly-org-mode)) ) #+END_SRC i then loaded a .org file with R code and entered C-c C-n in the R code block yet nothing happens and the point moves to the end of the block are there additional config needed (keybinds, etc?), what am i missing? best z On Wed, Nov 11, 2015 at 7:43 PM, Charles C. Berry <ccberry@ucsd.edu> wrote: > On Wed, 11 Nov 2015, Ista Zahn wrote: > > I recommend using polymode[1] as a more general solution to the need to >> interact with code blocks without pulling up a separate edit buffer. It >> seamlessly switches major modes when point is inside a code block. >> >> Best, >> Ista >> >> [1] https://github.com/vspinu/polymode >> > > Ista, > > Thanks for this pointer. I've been meaning to check out polymode for > some time, and your posting gave me the push to try it out. > > For anyone who wants to try it, after cloning into ~/elisp/polymode I > only needed to add this in my init (ESS was already in my init). > > #+BEGIN_SRC emacs-lisp > (setq load-path > (append '("~/elisp/polymode" "~/elisp/polymode/modes") > load-path)) > (require 'poly-org) > (add-to-list 'auto-mode-alist '("\\.org" . poly-org-mode)) > #+END_SRC > > When point is inside a src-block the local mode prevails. So, C-c C-n > inside an R src block sends the line point is on to the R process. C-c > C-c sends the region (or function or paragraph) to the process when > point is in the body. To execute `org-ctrl-c-ctrl-c' on the src block > with the C-c C-c keying, it is necessary to move point to before or > after the body. > > --- > > Now that I have tried this, I agree that it is better to let polymode > handle this kind of interaction than to try to build it into Org mode. > > Best, > > Chuck > > [-- Attachment #2: Type: text/html, Size: 2996 bytes --] ^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] org-babel-execute-src-block-region 2015-11-11 18:55 ` Xebar Saram @ 2015-11-11 21:53 ` Charles C. Berry 0 siblings, 0 replies; 9+ messages in thread From: Charles C. Berry @ 2015-11-11 21:53 UTC (permalink / raw) To: Xebar Saram Cc: Carlos Henrique Machado S Esteves, emacs-orgmode Mailinglist, Ista Zahn On Wed, 11 Nov 2015, Xebar Saram wrote: > thanks Chuck > > this sounds very interesting! [...] > > i then loaded a .org file with R code and entered C-c C-n in the R code > block yet nothing happens and the point moves to the end of the block > > are there additional config needed (keybinds, etc?), what am i missing? > Is there an R session running? If so, look in the session buffer. The result should be there. Not sure what needs to be done to display that buffer automatically. In my trials, a session would be started when C-c C-n was invoked if there was not one already up and running. Chuck ^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2015-11-11 21:53 UTC | newest] Thread overview: 9+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2015-11-02 2:17 [PATCH] org-babel-execute-src-block-region Carlos Henrique Machado S Esteves 2015-11-02 18:19 ` Charles C. Berry 2015-11-10 18:19 ` Carlos Henrique Machado S Esteves 2015-11-11 0:08 ` Nicolas Goaziou 2015-11-11 5:10 ` Xebar Saram 2015-11-11 13:44 ` Ista Zahn 2015-11-11 17:43 ` Charles C. Berry 2015-11-11 18:55 ` Xebar Saram 2015-11-11 21:53 ` Charles C. Berry
Code repositories for project(s) associated with this external index https://git.savannah.gnu.org/cgit/emacs.git https://git.savannah.gnu.org/cgit/emacs/org-mode.git This is an external index of several public inboxes, see mirroring instructions on how to clone and mirror all data and code used by this external index.