864
864
:group 'python-mode)
866
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
867
;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
866
;;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
869
868
(defvar py-execute-directory nil
870
869
"Stores the file's directory-name py-execute-... functions act upon. ")
5278
5277
Ignores setting of `py-shell-switch-buffers-on-execute', buffer with region stays current.
5280
5279
(interactive "r\nP")
5281
(let ((py-shell-switch-buffers-on-execute nil))
5282
(py-execute-base start end py-shell-name)))
5280
(py-execute-base start end py-shell-name))
5284
5282
(defun py-execute-region-switch (start end &optional shell)
5285
5283
"Send the region to a Python interpreter.
5287
5285
Ignores setting of `py-shell-switch-buffers-on-execute', output-buffer will being switched to.
5289
5287
(interactive "r\nP")
5290
(let ((py-shell-switch-buffers-on-execute t))
5291
(py-execute-base start end py-shell-name)))
5288
(py-execute-base start end py-shell-name nil t))
5293
5290
(defun py-execute-region (start end &optional shell dedicated)
5294
5291
"Send the region to a Python interpreter.
5330
5327
(interactive "r")
5331
5328
(py-execute-base start end (default-value 'py-shell-name) t))
5333
(defun py-execute-base (start end &optional shell dedicated)
5330
(defun py-execute-base (start end &optional shell dedicated switch)
5334
5331
"Adapt the variables used in the process. "
5335
5332
(let* ((shell (if (eq 4 (prefix-numeric-value shell))
5336
5333
(read-from-minibuffer "Python shell: " (default-value 'py-shell-name))
5337
5334
(when (stringp shell)
5339
5336
(regbuf (current-buffer))
5337
(py-shell-switch-buffers-on-execute switch)
5340
5338
(py-execute-directory (or (ignore-errors (file-name-directory (buffer-file-name))) (getenv "HOME")))
5341
5339
(strg (buffer-substring-no-properties start end))
5342
5340
(name-raw (or shell (py-choose-shell)))
5687
5685
(setq py-master-file (match-string-no-properties 2))))))
5688
5686
(when (interactive-p) (message "%s" py-master-file)))
5690
(defun py-execute-import-or-reload (&optional py-shell-name dedicated)
5688
(defun py-execute-import-or-reload (&optional argprompt shell dedicated)
5691
5689
"Import the current buffer's file in a Python interpreter.
5693
5691
If the file has already been imported, then do reload instead to get
5718
5716
(buffer (or (get-file-buffer filename)
5719
5717
(find-file-noselect filename))))
5720
5718
(set-buffer buffer)))
5721
(let ((file (buffer-file-name (current-buffer))))
5719
(let ((shell (or shell (py-choose-shell argprompt shell dedicated)))
5720
(file (buffer-file-name (current-buffer))))
5725
(let ((proc (if dedicated
5726
(get-process (py-shell nil dedicated))
5727
(get-process (py-shell)))))
5728
;; Maybe save some buffers
5729
(save-some-buffers (not py-ask-about-save) nil)
5730
(py-execute-file proc file
5731
(if (string-match "\\.py$" file)
5732
(let ((m (py-qualified-module-name (expand-file-name file))))
5733
(format "import sys\nif sys.modules.has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
5735
;; (format "execfile(r'%s')\n" file)
5736
(py-which-execute-file-command file)))))
5723
(ignore-errors (get-process (file-name-directory shell)))
5724
(get-process (py-shell argprompt dedicated (or shell (default-value 'py-shell-name)))))))
5725
;; Maybe save some buffers
5726
(save-some-buffers (not py-ask-about-save) nil)
5727
(py-execute-file proc file
5728
(if (string-match "\\.py$" file)
5729
(let ((m (py-qualified-module-name (expand-file-name file))))
5730
(format "import sys\nif sys.modules.has_key('%s'):\n reload(%s)\nelse:\n import %s\n"
5732
;; (format "execfile(r'%s')\n" file)
5733
(py-which-execute-file-command file))))
5738
5734
(py-execute-buffer py-shell-name))))
5740
5736
(defun py-qualified-module-name (file)
5752
5748
(funcall rec (file-name-directory file)
5753
5749
(file-name-sans-extension (file-name-nondirectory file)))))
5755
(defun py-execute-buffer (&optional shell)
5752
(defun py-execute-buffer-dedicated (&optional shell)
5753
"Send the contents of the buffer to a unique Python interpreter.
5755
If the file local variable `py-master-file' is non-nil, execute the
5756
named file instead of the buffer's file.
5758
If a clipping restriction is in effect, only the accessible portion of the buffer is sent. A trailing newline will be supplied if needed.
5760
With \\[univeral-argument] user is prompted to specify another then default shell.
5761
See also `\\[py-execute-region]'. "
5763
(py-execute-buffer-base shell t))
5765
(defun py-execute-buffer-switch (&optional shell)
5766
"Send the contents of the buffer to a Python interpreter and switches to output.
5768
If the file local variable `py-master-file' is non-nil, execute the
5769
named file instead of the buffer's file.
5770
If there is a *Python* process buffer, it is used.
5771
If a clipping restriction is in effect, only the accessible portion of the buffer is sent. A trailing newline will be supplied if needed.
5773
With \\[univeral-argument] user is prompted to specify another then default shell.
5774
See also `\\[py-execute-region]'. "
5776
(py-execute-buffer-base shell dedicated t))
5778
(defalias 'py-execute-buffer-switch-dedicated 'py-execute-buffer-dedicated-switch)
5779
(defun py-execute-buffer-dedicated-switch (&optional shell)
5780
"Send the contents of the buffer to an unique Python interpreter.
5782
Ignores setting of `py-shell-switch-buffers-on-execute'.
5783
If the file local variable `py-master-file' is non-nil, execute the
5784
named file instead of the buffer's file.
5786
If a clipping restriction is in effect, only the accessible portion of the buffer is sent. A trailing newline will be supplied if needed.
5788
With \\[univeral-argument] user is prompted to specify another then default shell.
5789
See also `\\[py-execute-region]'. "
5791
(py-execute-buffer-base shell t t))
5793
(defun py-execute-buffer (&optional shell dedicated switch)
5756
5794
"Send the contents of the buffer to a Python interpreter.
5758
5796
If the file local variable `py-master-file' is non-nil, execute the
5760
5798
If there is a *Python* process buffer, it is used.
5761
5799
If a clipping restriction is in effect, only the accessible portion of the buffer is sent. A trailing newline will be supplied if needed.
5801
With \\[univeral-argument] user is prompted to specify another then default shell.
5763
5802
See also `\\[py-execute-region]'. "
5764
5803
(interactive "P")
5804
(py-execute-buffer-base shell dedicated switch))
5806
(defun py-execute-buffer-base (&optional shell dedicated switch)
5807
"Honor `py-master-file'. "
5765
5808
(save-excursion
5766
(let ((old-buffer (current-buffer)))
5767
(or py-master-file (py-fetch-py-master-file))
5809
(let ((py-master-file (or py-master-file (py-fetch-py-master-file)))
5810
(py-shell-switch-buffers-on-execute switch))
5768
5811
(if py-master-file
5769
5812
(let* ((filename (expand-file-name py-master-file))
5770
5813
(buffer (or (get-file-buffer filename)
5771
5814
(find-file-noselect filename))))
5772
5815
(set-buffer buffer)))
5773
(py-execute-region (point-min) (point-max) py-shell-name))))
5816
(py-execute-region (point-min) (point-max) shell))))
5775
5818
(defun py-execute-buffer-no-switch (&optional shell)
5776
5819
"Like `py-execute-buffer', but ignores setting of `py-shell-switch-buffers-on-execute'.
5787
5830
(set-buffer buffer)))
5788
5831
(py-execute-region-no-switch (point-min) (point-max) py-shell-name))))
5790
(defun py-execute-buffer-switch (&optional shell)
5791
"Like `py-execute-buffer', but ignores setting of `py-shell-switch-buffers-on-execute', output-buffer will being switched to. "
5793
(let ((shell (if (eq 4 (prefix-numeric-value arg))
5794
(read-from-minibuffer "Shell: " (default-value 'py-shell-name))
5797
(let ((old-buffer (current-buffer)))
5798
(or py-master-file (py-fetch-py-master-file))
5800
(let* ((filename (expand-file-name py-master-file))
5801
(buffer (or (get-file-buffer filename)
5802
(find-file-noselect filename))))
5803
(set-buffer buffer)))
5804
(py-execute-region-switch (point-min) (point-max) shell)))))
5806
5833
;;; Specifying shells start
5807
5834
(defun py-execute-region-python (start end)
5808
5835
"Send the region to a common shell calling the python interpreter. "