all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Evaluate current line in Python mode?
@ 2010-08-28 19:45 Michael Hannon
  2010-08-29  8:02 ` Andreas Röhler
  0 siblings, 1 reply; 8+ messages in thread
From: Michael Hannon @ 2010-08-28 19:45 UTC (permalink / raw
  To: gnu-emacs help

Greetings.  I use Emacs for, among other things, running Python code and R
code.  In both modes (Python and ESS "Emacs Speaks Statistics") there is a
function bound to the sequence:

    C-c C-n

Both functions advance to the next line of code in the buffer, i.e., skipping
blank lines, comment lines, etc.

In ESS mode the function bound to C-c C-n also (by default) sends the current
line to R for evaluation.  I find this to be very convenient as a way to watch
calculations "evolve".

It's possible to do something similar in Python mode by selecting the
current line and then sending the region (C-c C-r) to Python for evaluation,
but this is a bit cumbersome.

Can anybody suggest a way to graft the ESS-mode behavior onto Python mode?
I've appended the high-level descriptions of both functions.  I understand
that the source code is available, and that I'm free to hack away to my
heart's content.  I just don't have the skills at Emacs/Lisp required to do
such a thing in a finite amount of time.

Thanks and best wishes,

-- Mike


    Python mode
    ===========

C-c C-n runs the command python-next-statement, which is an
interactive compiled Lisp function.

It is bound to C-c C-n.

(python-next-statement &optional COUNT)

Go to start of next statement.
With argument COUNT, do it COUNT times.  Stop at end of buffer.
Return count of statements left to move.

----------

    ESS mode
    ========

C-c C-n runs the command ess-eval-line-and-step, which is an
interactive compiled Lisp function in `ess-inf.el'.

It is bound to C-c C-n, C-c C-e C-n, <menu-bar> <ESS> <ESS Eval> <Eval
line & step>.

(ess-eval-line-and-step &optional SIMPLE-NEXT EVEN-EMPTY INVISIBLY)

Evaluate the current line visibly and step to the "next" line.
"next" = the next line with non-comment code _unless_ SIMPLE-NEXT is non-nil,
possibly via prefix arg.  If 2nd arg EVEN-EMPTY [prefix as well],
also send empty lines.    When the variable `ess-eval-empty' is non-nil
both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true.


      



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluate current line in Python mode?
       [not found] <mailman.3.1283024747.17543.help-gnu-emacs@gnu.org>
@ 2010-08-28 20:10 ` Xah Lee
  2010-08-28 23:49 ` TheFlyingDutchman
  1 sibling, 0 replies; 8+ messages in thread
From: Xah Lee @ 2010-08-28 20:10 UTC (permalink / raw
  To: help-gnu-emacs

On Aug 28, 12:45 pm, Michael Hannon <jm_han...@yahoo.com> wrote:
> Greetings.  I use Emacs for, among other things, running Python code and R
> code.  In both modes (Python and ESS "Emacs Speaks Statistics") there is a
> function bound to the sequence:
>
>     C-c C-n
>
> Both functions advance to the next line of code in the buffer, i.e., skipping
> blank lines, comment lines, etc.
>
> In ESS mode the function bound to C-c C-n also (by default) sends the current
> line to R for evaluation.  I find this to be very convenient as a way to watch
> calculations "evolve".
>
> It's possible to do something similar in Python mode by selecting the
> current line and then sending the region (C-c C-r) to Python for evaluation,
> but this is a bit cumbersome.
>
> Can anybody suggest a way to graft the ESS-mode behavior onto Python mode?
> I've appended the high-level descriptions of both functions.  I understand
> that the source code is available, and that I'm free to hack away to my
> heart's content.  I just don't have the skills at Emacs/Lisp required to do
> such a thing in a finite amount of time.
>
> Thanks and best wishes,
>
> -- Mike
>
>     Python mode
>     ===========
>
> C-c C-n runs the command python-next-statement, which is an
> interactive compiled Lisp function.
>
> It is bound to C-c C-n.
>
> (python-next-statement &optional COUNT)
>
> Go to start of next statement.
> With argument COUNT, do it COUNT times.  Stop at end of buffer.
> Return count of statements left to move.
>
> ----------
>
>     ESS mode
>     ========
>
> C-c C-n runs the command ess-eval-line-and-step, which is an
> interactive compiled Lisp function in `ess-inf.el'.
>
> It is bound to C-c C-n, C-c C-e C-n, <menu-bar> <ESS> <ESS Eval> <Eval
> line & step>.
>
> (ess-eval-line-and-step &optional SIMPLE-NEXT EVEN-EMPTY INVISIBLY)
>
> Evaluate the current line visibly and step to the "next" line.
> "next" = the next line with non-comment code _unless_ SIMPLE-NEXT is non-nil,
> possibly via prefix arg.  If 2nd arg EVEN-EMPTY [prefix as well],
> also send empty lines.    When the variable `ess-eval-empty' is non-nil
> both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true.

i haven't coded python for few years and havn't used ess MODE, but if
all you need is to send the region to python to eval and display the
output in buffer, that's pretty easy. I wrote a tutorial about this
here:

〈Elisp Wrapper For Perl Scripts〉 http://xahlee.org/emacs/elisp_perl_wrapper.html

it's just 5 lines of elisp.

 Xah ∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluate current line in Python mode?
       [not found] <mailman.3.1283024747.17543.help-gnu-emacs@gnu.org>
  2010-08-28 20:10 ` Xah Lee
@ 2010-08-28 23:49 ` TheFlyingDutchman
  1 sibling, 0 replies; 8+ messages in thread
From: TheFlyingDutchman @ 2010-08-28 23:49 UTC (permalink / raw
  To: help-gnu-emacs

I think this does what you asked for.  You can add this to your .emacs
(or init.el)  file.
It is currently set to work on F7 but that line can be changed move it
to another key.

But it appears from my usage that for a lot of
Python lines, like function definitions and for loops, evaluating just
one line
doesn't work and then that negatively impacts subsequent line
evaluations.


(add-hook 'python-mode-hook
     'my-python-next-statement)


(defun my-python-next-statement ()
   (interactive)
   (local-set-key (kbd "<f7>") 'my-python-next-statement)
   (if (string= mode-name "Python")
     (progn
       (python-next-statement)
       (beginning-of-line)
       (setq lineStart (point))
       (end-of-line)
       (setq lineEnd (point))
       (python-send-region lineStart lineEnd) )
     (message "function only applies to Python mode")
     (beep) ) )



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluate current line in Python mode?
  2010-08-28 19:45 Evaluate current line in Python mode? Michael Hannon
@ 2010-08-29  8:02 ` Andreas Röhler
  2010-08-29  8:37   ` Thierry Volpiatto
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Röhler @ 2010-08-29  8:02 UTC (permalink / raw
  To: help-gnu-emacs

Am 28.08.2010 21:45, schrieb Michael Hannon:
> Greetings.  I use Emacs for, among other things, running Python code and R
> code.  In both modes (Python and ESS "Emacs Speaks Statistics") there is a
> function bound to the sequence:
>
>      C-c C-n
>
> Both functions advance to the next line of code in the buffer, i.e., skipping
> blank lines, comment lines, etc.
>
> In ESS mode the function bound to C-c C-n also (by default) sends the current
> line to R for evaluation.  I find this to be very convenient as a way to watch
> calculations "evolve".
>
> It's possible to do something similar in Python mode by selecting the
> current line and then sending the region (C-c C-r) to Python for evaluation,
> but this is a bit cumbersome.
>
> Can anybody suggest a way to graft the ESS-mode behavior onto Python mode?

Try this:

(defun my-python-send-region (&optional beg end)
   (interactive)
   (let ((beg (cond (beg beg)
                    ((region-active-p)
                     (region-beginning))
                    (t (line-beginning-position))))
         (end (cond (end end)
                    ((region-active-p)
                     (copy-marker (region-end)))
                    (t (line-end-position)))))
     (python-send-region beg end)))

HTH
Comments welcome


Andreas

--
https://code.launchpad.net/~a-roehler/python-mode
https://code.launchpad.net/s-x-emacs-werkstatt/

> I've appended the high-level descriptions of both functions.  I understand
> that the source code is available, and that I'm free to hack away to my
> heart's content.  I just don't have the skills at Emacs/Lisp required to do
> such a thing in a finite amount of time.
>
> Thanks and best wishes,
>
> -- Mike
>
>
>      Python mode
>      ===========
>
> C-c C-n runs the command python-next-statement, which is an
> interactive compiled Lisp function.
>
> It is bound to C-c C-n.
>
> (python-next-statement&optional COUNT)
>
> Go to start of next statement.
> With argument COUNT, do it COUNT times.  Stop at end of buffer.
> Return count of statements left to move.
>
> ----------
>
>      ESS mode
>      ========
>
> C-c C-n runs the command ess-eval-line-and-step, which is an
> interactive compiled Lisp function in `ess-inf.el'.
>
> It is bound to C-c C-n, C-c C-e C-n,<menu-bar>  <ESS>  <ESS Eval>  <Eval
> line&  step>.
>
> (ess-eval-line-and-step&optional SIMPLE-NEXT EVEN-EMPTY INVISIBLY)
>
> Evaluate the current line visibly and step to the "next" line.
> "next" = the next line with non-comment code _unless_ SIMPLE-NEXT is non-nil,
> possibly via prefix arg.  If 2nd arg EVEN-EMPTY [prefix as well],
> also send empty lines.    When the variable `ess-eval-empty' is non-nil
> both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true.
>
>
>
>
>




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluate current line in Python mode?
  2010-08-29  8:02 ` Andreas Röhler
@ 2010-08-29  8:37   ` Thierry Volpiatto
  2010-08-30 10:43     ` Andreas Röhler
  0 siblings, 1 reply; 8+ messages in thread
From: Thierry Volpiatto @ 2010-08-29  8:37 UTC (permalink / raw
  To: help-gnu-emacs

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> Am 28.08.2010 21:45, schrieb Michael Hannon:
>> Greetings.  I use Emacs for, among other things, running Python code and R
>> code.  In both modes (Python and ESS "Emacs Speaks Statistics") there is a
>> function bound to the sequence:
>>
>>      C-c C-n
>>
>> Both functions advance to the next line of code in the buffer, i.e., skipping
>> blank lines, comment lines, etc.
>>
>> In ESS mode the function bound to C-c C-n also (by default) sends the current
>> line to R for evaluation.  I find this to be very convenient as a way to watch
>> calculations "evolve".
>>
>> It's possible to do something similar in Python mode by selecting the
>> current line and then sending the region (C-c C-r) to Python for evaluation,
>> but this is a bit cumbersome.
>>
>> Can anybody suggest a way to graft the ESS-mode behavior onto Python mode?
>
> Try this:
>
> (defun my-python-send-region (&optional beg end)
>   (interactive)
>   (let ((beg (cond (beg beg)
>                    ((region-active-p)
>                     (region-beginning))
>                    (t (line-beginning-position))))
>         (end (cond (end end)
>                    ((region-active-p)
>                     (copy-marker (region-end)))
>                    (t (line-end-position)))))
>     (python-send-region beg end)))
>
> HTH
> Comments welcome

Isn't it simpler:

(defun my-python-send-region (beg end)
  (interactive "r")
  (if (eq beg end)
      (python-send-region (point-at-bol) (point-at-eol))
      (python-send-region beg end)))

Otherwise, with cond you can write

(cond (beg)
      (end))

instead of

(cond (beg beg)
      (end end))

>
> Andreas
>
> --
> https://code.launchpad.net/~a-roehler/python-mode
> https://code.launchpad.net/s-x-emacs-werkstatt/
>
>> I've appended the high-level descriptions of both functions.  I understand
>> that the source code is available, and that I'm free to hack away to my
>> heart's content.  I just don't have the skills at Emacs/Lisp required to do
>> such a thing in a finite amount of time.
>>
>> Thanks and best wishes,
>>
>> -- Mike
>>
>>
>>      Python mode
>>      ===========
>>
>> C-c C-n runs the command python-next-statement, which is an
>> interactive compiled Lisp function.
>>
>> It is bound to C-c C-n.
>>
>> (python-next-statement&optional COUNT)
>>
>> Go to start of next statement.
>> With argument COUNT, do it COUNT times.  Stop at end of buffer.
>> Return count of statements left to move.
>>
>> ----------
>>
>>      ESS mode
>>      ========
>>
>> C-c C-n runs the command ess-eval-line-and-step, which is an
>> interactive compiled Lisp function in `ess-inf.el'.
>>
>> It is bound to C-c C-n, C-c C-e C-n,<menu-bar>  <ESS>  <ESS Eval>  <Eval
>> line&  step>.
>>
>> (ess-eval-line-and-step&optional SIMPLE-NEXT EVEN-EMPTY INVISIBLY)
>>
>> Evaluate the current line visibly and step to the "next" line.
>> "next" = the next line with non-comment code _unless_ SIMPLE-NEXT is non-nil,
>> possibly via prefix arg.  If 2nd arg EVEN-EMPTY [prefix as well],
>> also send empty lines.    When the variable `ess-eval-empty' is non-nil
>> both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true.
>>
>>
>>
>>
>>
>
>
>

-- 
Thierry Volpiatto
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluate current line in Python mode?
  2010-08-29  8:37   ` Thierry Volpiatto
@ 2010-08-30 10:43     ` Andreas Röhler
  2010-08-30 12:36       ` Thierry Volpiatto
  0 siblings, 1 reply; 8+ messages in thread
From: Andreas Röhler @ 2010-08-30 10:43 UTC (permalink / raw
  To: help-gnu-emacs

Am 29.08.2010 10:37, schrieb Thierry Volpiatto:
> Andreas Röhler<andreas.roehler@easy-emacs.de>  writes:
>
>> Am 28.08.2010 21:45, schrieb Michael Hannon:
>>> Greetings.  I use Emacs for, among other things, running Python code and R
>>> code.  In both modes (Python and ESS "Emacs Speaks Statistics") there is a
>>> function bound to the sequence:
>>>
>>>       C-c C-n
>>>
>>> Both functions advance to the next line of code in the buffer, i.e., skipping
>>> blank lines, comment lines, etc.
>>>
>>> In ESS mode the function bound to C-c C-n also (by default) sends the current
>>> line to R for evaluation.  I find this to be very convenient as a way to watch
>>> calculations "evolve".
>>>
>>> It's possible to do something similar in Python mode by selecting the
>>> current line and then sending the region (C-c C-r) to Python for evaluation,
>>> but this is a bit cumbersome.
>>>
>>> Can anybody suggest a way to graft the ESS-mode behavior onto Python mode?
>>
>> Try this:
>>
>> (defun my-python-send-region (&optional beg end)
>>    (interactive)
>>    (let ((beg (cond (beg beg)
>>                     ((region-active-p)
>>                      (region-beginning))
>>                     (t (line-beginning-position))))
>>          (end (cond (end end)
>>                     ((region-active-p)
>>                      (copy-marker (region-end)))
>>                     (t (line-end-position)))))
>>      (python-send-region beg end)))
>>
>> HTH
>> Comments welcome
>
> Isn't it simpler:
>
> (defun my-python-send-region (beg end)
>    (interactive "r")
>    (if (eq beg end)
>        (python-send-region (point-at-bol) (point-at-eol))
>        (python-send-region beg end)))
>

"r" complains if no mark is set
Did choose the default getting rid of that constraint

> Otherwise, with cond you can write
>
> (cond (beg)
>        (end))
>
> instead of
>
> (cond (beg beg)
>        (end end))
>
>>

Indeed, will change this.

Thanks




>> Andreas
>>
>> --
>> https://code.launchpad.net/~a-roehler/python-mode
>> https://code.launchpad.net/s-x-emacs-werkstatt/
>>
>>> I've appended the high-level descriptions of both functions.  I understand
>>> that the source code is available, and that I'm free to hack away to my
>>> heart's content.  I just don't have the skills at Emacs/Lisp required to do
>>> such a thing in a finite amount of time.
>>>
>>> Thanks and best wishes,
>>>
>>> -- Mike
>>>
>>>
>>>       Python mode
>>>       ===========
>>>
>>> C-c C-n runs the command python-next-statement, which is an
>>> interactive compiled Lisp function.
>>>
>>> It is bound to C-c C-n.
>>>
>>> (python-next-statement&optional COUNT)
>>>
>>> Go to start of next statement.
>>> With argument COUNT, do it COUNT times.  Stop at end of buffer.
>>> Return count of statements left to move.
>>>
>>> ----------
>>>
>>>       ESS mode
>>>       ========
>>>
>>> C-c C-n runs the command ess-eval-line-and-step, which is an
>>> interactive compiled Lisp function in `ess-inf.el'.
>>>
>>> It is bound to C-c C-n, C-c C-e C-n,<menu-bar>   <ESS>   <ESS Eval>   <Eval
>>> line&   step>.
>>>
>>> (ess-eval-line-and-step&optional SIMPLE-NEXT EVEN-EMPTY INVISIBLY)
>>>
>>> Evaluate the current line visibly and step to the "next" line.
>>> "next" = the next line with non-comment code _unless_ SIMPLE-NEXT is non-nil,
>>> possibly via prefix arg.  If 2nd arg EVEN-EMPTY [prefix as well],
>>> also send empty lines.    When the variable `ess-eval-empty' is non-nil
>>> both SIMPLE-NEXT and EVEN-EMPTY are interpreted as true.
>>>
>>>
>>>
>>>
>>>
>>
>>
>>
>




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluate current line in Python mode?
  2010-08-30 10:43     ` Andreas Röhler
@ 2010-08-30 12:36       ` Thierry Volpiatto
  2010-08-30 16:17         ` Andreas Röhler
  0 siblings, 1 reply; 8+ messages in thread
From: Thierry Volpiatto @ 2010-08-30 12:36 UTC (permalink / raw
  To: help-gnu-emacs

Andreas Röhler <andreas.roehler@easy-emacs.de> writes:

> "r" complains if no mark is set
So you could use (&optional beg end)

> Did choose the default getting rid of that constraint
Which default?

-- 
Thierry Volpiatto
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Evaluate current line in Python mode?
  2010-08-30 12:36       ` Thierry Volpiatto
@ 2010-08-30 16:17         ` Andreas Röhler
  0 siblings, 0 replies; 8+ messages in thread
From: Andreas Röhler @ 2010-08-30 16:17 UTC (permalink / raw
  To: help-gnu-emacs

Am 30.08.2010 14:36, schrieb Thierry Volpiatto:
> Andreas Röhler<andreas.roehler@easy-emacs.de>  writes:
>
>> "r" complains if no mark is set
> So you could use (&optional beg end)
>
>> Did choose the default getting rid of that constraint
> Which default?
>

That depends.
Whats set if the values, "r" would expect, are not delivered.



^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2010-08-30 16:17 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-28 19:45 Evaluate current line in Python mode? Michael Hannon
2010-08-29  8:02 ` Andreas Röhler
2010-08-29  8:37   ` Thierry Volpiatto
2010-08-30 10:43     ` Andreas Röhler
2010-08-30 12:36       ` Thierry Volpiatto
2010-08-30 16:17         ` Andreas Röhler
     [not found] <mailman.3.1283024747.17543.help-gnu-emacs@gnu.org>
2010-08-28 20:10 ` Xah Lee
2010-08-28 23:49 ` TheFlyingDutchman

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.