From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Yuri D'Elia Newsgroups: gmane.emacs.help Subject: Re: Evaluating python code blocks in python-mode Date: Thu, 25 Jun 2015 12:25:13 +0200 Message-ID: <558BD709.60706@thregr.org> References: <5585BA55.8090205@easy-emacs.de> <558657BE.5090207@easy-emacs.de> <5586E3BE.8080502@thregr.org> <55870801.8050200@easy-emacs.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1435228025 25614 80.91.229.3 (25 Jun 2015 10:27:05 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 25 Jun 2015 10:27:05 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jun 25 12:26:56 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Z84Mx-0003TQ-DO for geh-help-gnu-emacs@m.gmane.org; Thu, 25 Jun 2015 12:26:55 +0200 Original-Received: from localhost ([::1]:54835 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z84Mw-00021Z-Px for geh-help-gnu-emacs@m.gmane.org; Thu, 25 Jun 2015 06:26:54 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:50103) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z84LX-0000tX-R3 for help-gnu-emacs@gnu.org; Thu, 25 Jun 2015 06:25:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Z84LS-0008Bi-FK for help-gnu-emacs@gnu.org; Thu, 25 Jun 2015 06:25:27 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:38851) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Z84LS-0008AP-5u for help-gnu-emacs@gnu.org; Thu, 25 Jun 2015 06:25:22 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1Z84LR-0002Br-6p for help-gnu-emacs@gnu.org; Thu, 25 Jun 2015 12:25:21 +0200 Original-Received: from 193.106.183.18 ([193.106.183.18]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 25 Jun 2015 12:25:21 +0200 Original-Received: from wavexx by 193.106.183.18 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 25 Jun 2015 12:25:21 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 44 Original-X-Complaints-To: usenet@ger.gmane.org X-Gmane-NNTP-Posting-Host: 193.106.183.18 X-Enigmail-Draft-Status: N1110 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Icedove/38.0 In-Reply-To: <55870801.8050200@easy-emacs.de> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:105151 Archived-At: 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.