~a-roehler/python-mode/XEmacs-compat-test

« back to all changes in this revision

Viewing changes to python-mode.el

  • Committer: Andreas Roehler
  • Date: 2012-02-01 15:24:57 UTC
  • Revision ID: andreas.roehler@online.de-20120201152457-igehalyflqzo2wzm
`py-execute-region-switch' fixed

`py-ask-about-save' previous prodeeding restored,
as used by `py-execute-import-or-reload', saving
possibly edited buffers before reload is reasonable

Show diffs side-by-side

added added

removed removed

Lines of Context:
863
863
  :type 'string
864
864
  :group 'python-mode)
865
865
 
866
 
;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
867
 
;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
 
866
;;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT
868
867
 
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.
5279
5278
 "
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))
5283
5281
 
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.
5288
5286
"
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))
5292
5289
 
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))
5332
5329
 
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)
5338
5335
                    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)))
5689
5687
 
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.
5692
5690
 
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))))
5722
5721
    (if file
5723
 
        (progn
5724
 
          (py-choose-shell)
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"
5734
 
                                           m m m))
5735
 
                               ;; (format "execfile(r'%s')\n" file)
5736
 
                               (py-which-execute-file-command file)))))
5737
 
      ;; else
 
5722
        (let ((proc (or
 
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"
 
5731
                                         m m m))
 
5732
                             ;; (format "execfile(r'%s')\n" file)
 
5733
                             (py-which-execute-file-command file))))
5738
5734
      (py-execute-buffer py-shell-name))))
5739
5735
 
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)))))
5754
5750
 
5755
 
(defun py-execute-buffer (&optional shell)
 
5751
 
 
5752
(defun py-execute-buffer-dedicated (&optional shell)
 
5753
  "Send the contents of the buffer to a unique Python interpreter.
 
5754
 
 
5755
If the file local variable `py-master-file' is non-nil, execute the
 
5756
named file instead of the buffer's file.
 
5757
 
 
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.
 
5759
 
 
5760
With \\[univeral-argument] user is prompted to specify another then default shell.
 
5761
See also `\\[py-execute-region]'. "
 
5762
  (interactive "P")
 
5763
  (py-execute-buffer-base shell t))
 
5764
 
 
5765
(defun py-execute-buffer-switch (&optional shell)
 
5766
  "Send the contents of the buffer to a Python interpreter and switches to output.
 
5767
 
 
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.
 
5772
 
 
5773
With \\[univeral-argument] user is prompted to specify another then default shell.
 
5774
See also `\\[py-execute-region]'. "
 
5775
  (interactive "P")
 
5776
  (py-execute-buffer-base shell dedicated t))
 
5777
 
 
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.
 
5781
 
 
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.
 
5785
 
 
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.
 
5787
 
 
5788
With \\[univeral-argument] user is prompted to specify another then default shell.
 
5789
See also `\\[py-execute-region]'. "
 
5790
  (interactive "P")
 
5791
  (py-execute-buffer-base shell t t))
 
5792
 
 
5793
(defun py-execute-buffer (&optional shell dedicated switch)
5756
5794
  "Send the contents of the buffer to a Python interpreter.
5757
5795
 
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.
5762
5800
 
 
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))
 
5805
 
 
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))))
5774
5817
 
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))))
5789
5832
 
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. "
5792
 
  (interactive "P")
5793
 
  (let ((shell (if (eq 4 (prefix-numeric-value arg))
5794
 
                   (read-from-minibuffer "Shell: " (default-value 'py-shell-name))
5795
 
                 py-shell-name)))
5796
 
    (save-excursion
5797
 
      (let ((old-buffer (current-buffer)))
5798
 
        (or py-master-file (py-fetch-py-master-file))
5799
 
        (if 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)))))
5805
 
 
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. "
7725
7752
        (when (interactive-p) (message "%s" load-path))))
7726
7753
 
7727
7754
(defvar skeleton-further-elements)
7728
 
 
7729
7755
(defun python-mode ()
7730
7756
  "Major mode for editing Python files.
7731
7757