help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Evaluate current line in Python mode?


From: Thierry Volpiatto
Subject: Re: Evaluate current line in Python mode?
Date: Sun, 29 Aug 2010 10:37:36 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

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 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]