fgallina / python.el

Python's flying circus support for Emacs
GNU General Public License v3.0
267 stars 53 forks source link

Feature Request: Eval Line #36

Closed pank closed 13 years ago

pank commented 13 years ago

Hi, ESS has and extremely useful function called Eval Line (C-c C-j). I miss it in python.el.

Personally, I think it would be nice and sufficient if python.el Eval Region (C-c C-r) would default to the current line if no region is selected.

Thanks, Rasmus

fgallina commented 13 years ago

I don't see much cases where making eval of a single sentence is useful. Normally that sentence depends on another one, on some import, etc. So I guess explicit marking of the region is actually a cool thing. For now I think I wouldn't add such thing but you can have something like this in your .emacs

(defun python-shell-send-sentence ()
  "Send the current sentence to inferior Python process."
  (interactive)
  (let ((start (save-excursion 
                 (python-nav-sentence-start) 
                 (point-marker)))
        (end (save-excursion 
               (python-nav-sentence-end) 
               (point-marker))))
    (when (and start end)
      (python-shell-send-region start end))))

(add-hook 'python-mode-hook
          #'(lambda ()
              (define-key python-mode-map "\C-c\C-j" 'python-shell-send-sentence)))

I didn't tested this but might work.

I'm closing this for now, but I'm open for suggestions or arguments in favor of adding this to python.el itself. Feel free to comment here :)

Thanks for the feature request, Fabián.

pank commented 13 years ago

Just to confirm, your snip works wonderfully.

Personally, I find the it tremendously useful when writing code. The reason might be that I am not a professional programmer. I use Python for utility scripts (I just created a nice podcast downloader!) and math.

Why do I find it useful to easily send one line? Assume that I am writing a math script. I def a function for something. I need to test during "development" using some trivial case, so I might use some simple vectors L=array([1,2,3]). or I might define url = "http://foo.bar/feed.xml" to test my podcast downloader.
These are the obvious candidates. Suppose I something silly like L=fun(L) and fun is screwed up, I'd want to reload L easily and try again.

As said, the snip you provided works in my very limited test-case, so you could add it to the python.el comments, or some wiki or wherever it fits best. (Maybe a wiki page with snips would be good thing; previously, you have also provided me with a snip for IPython3 and python.el).

Thanks, Rasmus