all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Yuri D'Elia <wavexx@thregr.org>
To: "Andreas Röhler" <andreas.roehler@easy-emacs.de>, help-gnu-emacs@gnu.org
Subject: Re: Evaluating python code blocks in python-mode
Date: Thu, 25 Jun 2015 12:25:33 +0200	[thread overview]
Message-ID: <558BD71D.1060605@thregr.org> (raw)
In-Reply-To: <55870801.8050200@easy-emacs.de>

On 06/21/2015 08:52 PM, Andreas Röhler wrote:
> It might be an option to use existing markers from folding-mode. OTOH 
> don't see a command there how to copy the section. Do you?

So this is what I came up with and I currently use:

http://www.thregr.org/~wavexx/tmp/python-mode-extra.el

Sorry for not inlining the file, but I think it's more readable.

The idea is that "C-c C-c" sends the current fold you're in. If you're in a nested fold, subfolds are send too. If you're not in a fold, the buffer (or narrowed region) is sent instead.

As an added bonus, I wanted to define a generic block delimiter, which is # --- in my case. `python-shell-send-fold-or-section' will send the current fold if there's any, up to the closest block delimiter (if any).

So for example, in a file like:

 print "1"
 # ---
 print "2"

"C-c C-c" on either print statement will be limited to that statement. Block delimiters can be nested inside folds, and work as you'd expect: if you're in the same fold as a delimiter, the delimiter is used. If you're upwards, the delimiter is ignored.

This makes it very easy to delimit "computation blocks", as you'd normally do in an interactive notebook, without the burden to define folds for each.

To top that, the region being sent is added with a volatile highlight (volatile-highlights.el is required) so you see what's being evaluated.

I love it.

It works in Fabián's python-mode, but should work with minimal changes with other modes too.

I actually redefine some bindings for python-mode to behave more like ESS (which IMHO has a better layout for interactive evaluation), resulting in something like:

(add-hook 'python-mode-hook
	  (lambda ()
	    (local-set-key (kbd "C-c C-j") 'python-shell-send-line)
	    (local-set-key (kbd "C-c C-n") 'python-shell-send-line and-step)
	    (local-set-key (kbd "C-c C-f") 'python-shell-send-defun)
	    (local-set-key (kbd "C-c C-b") 'python-shell-send-buffer)
	    (local-set-key (kbd "C-c C-c") 'python-shell-send-fold-or-section)))

`python-shell-send-fold-or-section' is what is defined in python-mode-extra.el, which was a little tricker to define than anticipated, but it seems to work correctly.

I re-use the markers defined in folding-mode, so if you want to customize the markers just use `folding-add-to-marks-list'. folding-mode doesn't need to be enabled. I actually don't use folding, I just wanted to re-use the same methods.




  parent reply	other threads:[~2015-06-25 10:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-20 10:50 Evaluating python code blocks in python-mode Yuri D'Elia
2015-06-20 19:09 ` Andreas Röhler
2015-06-21  6:20   ` Andreas Röhler
2015-06-21 16:17     ` Yuri D'Elia
2015-06-21 16:18     ` Yuri D'Elia
2015-06-21 16:35       ` Yuri D'Elia
2015-06-21 18:52         ` Andreas Röhler
2015-06-25 10:25           ` Yuri D'Elia
2015-06-25 10:25           ` Yuri D'Elia [this message]
2015-06-25 11:21             ` Andreas Röhler
2015-06-25 12:33               ` Yuri D'Elia

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=558BD71D.1060605@thregr.org \
    --to=wavexx@thregr.org \
    --cc=andreas.roehler@easy-emacs.de \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.