Change the Emacs “send code to interpreter” C-c C-r command in IPython-mode

后端 未结 3 1654
迷失自我
迷失自我 2021-01-03 06:48

The question here is related (but NOT identical) and complementary to Change the "send code to interpreter" (C-c |) command in python-mode .

I work on a M

相关标签:
3条回答
  • 2021-01-03 06:57

    i did some hacking and here is what i have: by reading the code you should be able to tell how it works.

    (defun block-line-end ()
        (setq indentation (current-indentation))
        (forward-line)
        (while (> (current-indentation) indentation)
          (forward-line))
        (forward-line -1)
        (line-end-position))  
    (defun my-python-shell-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 (block-line-end)))))
          (python-shell-send-region beg end))
        (forward-line))
    
    0 讨论(0)
  • 2021-01-03 07:05

    Use (kbd "RET")

    Try this with python.el

    (eval-after-load "python"
      '(define-key python-mode-map [(control c)(kbd "RET")] 'python-shell-send-region))
    

    WRT python-mode.el:

    (eval-after-load "python-mode"
      '(define-key python-mode-map [(control c) (kbd "RET")] 'py-execute-region))
    

    BTW unless IPython-exclusiv features are needed, recommend to execute code from Emacs through a common Python. IPython implements a bunch of cool things, which might appear orthogonal to Emacs, which also implements a bunch of cool things.

    0 讨论(0)
  • 2021-01-03 07:14

    Here is something that was quickly cooked up to send a python line to python-shell. I tend to do it in R a lot as well. It's not completely automated yet but I might be useful.

    Here are some that still remain to be made:

    1. Send line to dedicated process if necessary

    2. Assign a local key

      (defun my-python-line ()
       (interactive)
        (save-excursion
        (setq the_script_buffer (format (buffer-name)))
        (end-of-line)
        (kill-region (point) (progn (back-to-indentation) (point)))
        ;(setq the_py_buffer (format "*Python[%s]*" (buffer-file-name)))
        (setq the_py_buffer "*Python*")
        (switch-to-buffer-other-window  the_py_buffer)
        (goto-char (buffer-end 1))
        (yank)
        (comint-send-input)
        (switch-to-buffer-other-window the_script_buffer)
        (yank))
      )
      
      (global-set-key  (kbd "C-c n") 'my-python-line)
      

    Update

    I have improved the code a little bit more:

    1. possible to use the code directly to even if a python shell is not running yet.

    2. A local set key have been set

      Only the dedicated process remain to be made. Feel free to improve in the answer below

      (defun my-python-line ()
      (interactive)
      (save-excursion
        (setq the_script_buffer (format (buffer-name)))
        (end-of-line)
        (kill-region (point) (progn (back-to-indentation) (point)))
        (if  (get-buffer  "*Python*")
        (message "")
        (run-python "ipython" nil nil))
        ;; (setq the_py_buffer (format "*Python[%s]*" (buffer-file-name)))
        (setq the_py_buffer "*Python*")
        (switch-to-buffer-other-window  the_py_buffer)
        (goto-char (buffer-end 1))
        (yank)
        (comint-send-input)
        (switch-to-buffer-other-window the_script_buffer)
        (yank))
        (end-of-line)
        (next-line)
        )
      
      (add-hook 'python-mode-hook
          (lambda ()
        (define-key python-mode-map "\C-cn" 'my-python-line)))
      
    0 讨论(0)
提交回复
热议问题