Mercurial > emacs
comparison lisp/progmodes/python.el @ 58415:86ead4686506
(run-python): Don't hard code *Python*. Don't modify process-environment.
(python-send-region, python-load-file): Don't assume that
python-buffer == (process-buffer (python-proc)).
(python-switch-to-python): Simplify.
author | Stefan Monnier <monnier@iro.umontreal.ca> |
---|---|
date | Mon, 22 Nov 2004 05:52:39 +0000 |
parents | 6ec84037b6bc |
children | d0581f1eef46 |
comparison
equal
deleted
inserted
replaced
58414:1f06a555fba2 | 58415:86ead4686506 |
---|---|
1 ;;; python.el --- silly walks for Python | 1 ;;; python.el --- silly walks for Python |
2 | 2 |
3 ;; Copyright (C) 2003, 04 Free Software Foundation, Inc. | 3 ;; Copyright (C) 2003, 2004 Free Software Foundation, Inc. |
4 | 4 |
5 ;; Author: Dave Love <fx@gnu.org> | 5 ;; Author: Dave Love <fx@gnu.org> |
6 ;; Maintainer: FSF | 6 ;; Maintainer: FSF |
7 ;; Created: Nov 2003 | 7 ;; Created: Nov 2003 |
8 ;; Keywords: languages | 8 ;; Keywords: languages |
1125 "Run an inferior Python process, input and output via buffer *Python*. | 1125 "Run an inferior Python process, input and output via buffer *Python*. |
1126 CMD is the Python command to run. NOSHOW non-nil means don't show the | 1126 CMD is the Python command to run. NOSHOW non-nil means don't show the |
1127 buffer automatically. | 1127 buffer automatically. |
1128 If there is a process already running in `*Python*', switch to | 1128 If there is a process already running in `*Python*', switch to |
1129 that buffer. Interactively, a prefix arg allows you to edit the initial | 1129 that buffer. Interactively, a prefix arg allows you to edit the initial |
1130 command line (default is `python-command'); `-i' etc. args will be added | 1130 command line (default is `python-command'); `-i' etc. args will be added |
1131 to this as appropriate. Runs the hook `inferior-python-mode-hook' | 1131 to this as appropriate. Runs the hook `inferior-python-mode-hook' |
1132 \(after the `comint-mode-hook' is run). | 1132 \(after the `comint-mode-hook' is run). |
1133 \(Type \\[describe-mode] in the process buffer for a list of commands.)" | 1133 \(Type \\[describe-mode] in the process buffer for a list of commands.)" |
1134 (interactive (list (if current-prefix-arg | 1134 (interactive (list (if current-prefix-arg |
1135 (read-string "Run Python: " python-command) | 1135 (read-string "Run Python: " python-command) |
1141 ;; invoked. Would support multiple processes better. | 1141 ;; invoked. Would support multiple processes better. |
1142 (unless (comint-check-proc python-buffer) | 1142 (unless (comint-check-proc python-buffer) |
1143 (let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) | 1143 (let* ((cmdlist (append (python-args-to-list cmd) '("-i"))) |
1144 (path (getenv "PYTHONPATH")) | 1144 (path (getenv "PYTHONPATH")) |
1145 (process-environment ; to import emacs.py | 1145 (process-environment ; to import emacs.py |
1146 (push (concat "PYTHONPATH=" data-directory | 1146 (cons (concat "PYTHONPATH=" data-directory |
1147 (if path (concat ":" path))) | 1147 (if path (concat ":" path))) |
1148 process-environment))) | 1148 process-environment))) |
1149 (set-buffer (apply 'make-comint "Python" (car cmdlist) nil | 1149 (set-buffer (apply 'make-comint "Python" (car cmdlist) nil |
1150 (cdr cmdlist))) | 1150 (cdr cmdlist))) |
1151 (setq python-buffer "*Python*")) | 1151 (setq python-buffer (buffer-name))) |
1152 (inferior-python-mode) | 1152 (inferior-python-mode) |
1153 ;; Load function defintions we need. | 1153 ;; Load function defintions we need. |
1154 ;; Before the preoutput function was used, this was done via -c in | 1154 ;; Before the preoutput function was used, this was done via -c in |
1155 ;; cmdlist, but that loses the banner and doesn't run the startup | 1155 ;; cmdlist, but that loses the banner and doesn't run the startup |
1156 ;; file. The code might be inline here, but there's enough that it | 1156 ;; file. The code might be inline here, but there's enough that it |
1202 (goto-char orig-start) | 1202 (goto-char orig-start) |
1203 ;; Wrong if we had indented code at buffer start. | 1203 ;; Wrong if we had indented code at buffer start. |
1204 (set-marker orig-start (line-beginning-position 0))) | 1204 (set-marker orig-start (line-beginning-position 0))) |
1205 (write-region "if True:\n" nil f nil 'nomsg)) | 1205 (write-region "if True:\n" nil f nil 'nomsg)) |
1206 (write-region start end f t 'nomsg) | 1206 (write-region start end f t 'nomsg) |
1207 (let ((proc (python-proc))) ;Make sure we're running a process. | 1207 (with-current-buffer (process-buffer (python-proc)) ;Runs python if needed. |
1208 (with-current-buffer python-buffer | 1208 (python-send-command command) |
1209 (python-send-command command) | 1209 ;; Tell compile.el to redirect error locations in file `f' to |
1210 ;; Tell compile.el to redirect error locations in file `f' to | 1210 ;; positions past marker `orig-start'. It has to be done *after* |
1211 ;; positions past marker `orig-start'. It has to be done *after* | 1211 ;; python-send-command's call to compilation-forget-errors. |
1212 ;; python-send-command's call to compilation-forget-errors. | 1212 (compilation-fake-loc orig-start f)))) |
1213 (compilation-fake-loc orig-start f))))) | |
1214 | 1213 |
1215 (defun python-send-string (string) | 1214 (defun python-send-string (string) |
1216 "Evaluate STRING in inferior Python process." | 1215 "Evaluate STRING in inferior Python process." |
1217 (interactive "sPython command: ") | 1216 (interactive "sPython command: ") |
1218 (comint-send-string (python-proc) string) | 1217 (comint-send-string (python-proc) string) |
1233 | 1232 |
1234 (defun python-switch-to-python (eob-p) | 1233 (defun python-switch-to-python (eob-p) |
1235 "Switch to the Python process buffer. | 1234 "Switch to the Python process buffer. |
1236 With prefix arg, position cursor at end of buffer." | 1235 With prefix arg, position cursor at end of buffer." |
1237 (interactive "P") | 1236 (interactive "P") |
1238 ;; Start python unless we have a buffer. | 1237 (pop-to-buffer (process-buffer (python-proc))) ;Runs python if needed. |
1239 (unless (and python-buffer | |
1240 (get-buffer python-buffer)) | |
1241 (run-python nil t)) | |
1242 (pop-to-buffer python-buffer) | |
1243 ;; Make extra sure python is running in this buffer. | |
1244 (python-proc) | |
1245 (when eob-p | 1238 (when eob-p |
1246 (push-mark) | 1239 (push-mark) |
1247 (goto-char (point-max)))) | 1240 (goto-char (point-max)))) |
1248 | 1241 |
1249 (defun python-send-region-and-go (start end) | 1242 (defun python-send-region-and-go (start end) |
1275 python-source-modes | 1268 python-source-modes |
1276 t)) ; because execfile needs exact name | 1269 t)) ; because execfile needs exact name |
1277 (comint-check-source file-name) ; Check to see if buffer needs saving. | 1270 (comint-check-source file-name) ; Check to see if buffer needs saving. |
1278 (setq python-prev-dir/file (cons (file-name-directory file-name) | 1271 (setq python-prev-dir/file (cons (file-name-directory file-name) |
1279 (file-name-nondirectory file-name))) | 1272 (file-name-nondirectory file-name))) |
1280 (let ((proc (python-proc))) ;Make sure we have a process. | 1273 (with-current-buffer (process-buffer (python-proc)) ;Runs python if needed. |
1281 (with-current-buffer python-buffer | 1274 ;; Fixme: I'm not convinced by this logic from python-mode.el. |
1282 ;; Fixme: I'm not convinced by this logic from python-mode.el. | 1275 (python-send-command |
1283 (python-send-command | 1276 (if (string-match "\\.py\\'" file-name) |
1284 (if (string-match "\\.py\\'" file-name) | 1277 (let ((module (file-name-sans-extension |
1285 (let ((module (file-name-sans-extension | 1278 (file-name-nondirectory file-name)))) |
1286 (file-name-nondirectory file-name)))) | 1279 (format "emacs.eimport(%S,%S)" |
1287 (format "emacs.eimport(%S,%S)" | 1280 module (file-name-directory file-name))) |
1288 module (file-name-directory file-name))) | 1281 (format "execfile(%S)" file-name))) |
1289 (format "execfile(%S)" file-name))) | 1282 (message "%s loaded" file-name))) |
1290 (message "%s loaded" file-name)))) | |
1291 | 1283 |
1292 ;; Fixme: If we need to start the process, wait until we've got the OK | 1284 ;; Fixme: If we need to start the process, wait until we've got the OK |
1293 ;; from the startup. | 1285 ;; from the startup. |
1294 (defun python-proc () | 1286 (defun python-proc () |
1295 "Return the current Python process. | 1287 "Return the current Python process. |