diff options
| author | Fabián Ezequiel Gallina | 2012-05-17 00:03:21 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2012-05-17 00:03:21 -0300 |
| commit | 1fe1b5aa848d02ac3b1706ab3c332b8578567c54 (patch) | |
| tree | e1a03d7204895285773fe63db7d56c71b4eb6fe4 /lisp/progmodes/python.el | |
| parent | ecf24fd762c38e0a588198bc702b2f5866957f86 (diff) | |
| download | emacs-1fe1b5aa848d02ac3b1706ab3c332b8578567c54.tar.gz emacs-1fe1b5aa848d02ac3b1706ab3c332b8578567c54.zip | |
Implemented internal python shell.
This new kind of shell is intended to be used for generic
communication related to defined configurations. The main difference
with global or dedicated shells is that these ones are attached to a
configuration, not a buffer. This means that can be used for example
to retrieve the sys.path and other stuff, without messing with user
shells.
New Variables:
* python-shell-internal-buffer-name,
New functions:
* python-shell-internal-get-process-name
* run-python-internal
* python-shell-internal-get-or-create-process
* python-shell-internal-send-string (makes python-send-receive obsolete)
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 247deec6d3e..98f37fc2ac4 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -1020,6 +1020,9 @@ With negative argument, move backward repeatedly to start of sentence." | |||
| 1020 | :group 'python | 1020 | :group 'python |
| 1021 | :safe 'stringp) | 1021 | :safe 'stringp) |
| 1022 | 1022 | ||
| 1023 | (defvar python-shell-internal-buffer-name "Python Internal" | ||
| 1024 | "Default buffer name for the Internal Python interpreter.") | ||
| 1025 | |||
| 1023 | (defcustom python-shell-interpreter-args "-i" | 1026 | (defcustom python-shell-interpreter-args "-i" |
| 1024 | "Default arguments for the Python interpreter." | 1027 | "Default arguments for the Python interpreter." |
| 1025 | :type 'string | 1028 | :type 'string |
| @@ -1126,6 +1129,20 @@ in the `same-window-buffer-names' list." | |||
| 1126 | (format "*%s*" process-name))) | 1129 | (format "*%s*" process-name))) |
| 1127 | process-name)) | 1130 | process-name)) |
| 1128 | 1131 | ||
| 1132 | (defun python-shell-internal-get-process-name () | ||
| 1133 | "Calculate the appropiate process name for Internal Python process. | ||
| 1134 | The name is calculated from `python-shell-global-buffer-name' and | ||
| 1135 | a hash of all relevant global shell settings in order to ensure | ||
| 1136 | uniqueness for different types of configurations." | ||
| 1137 | (format "%s [%s]" | ||
| 1138 | python-shell-internal-buffer-name | ||
| 1139 | (md5 | ||
| 1140 | (concat | ||
| 1141 | (python-shell-parse-command) | ||
| 1142 | (mapconcat #'symbol-value python-shell-setup-codes "") | ||
| 1143 | (mapconcat #'indentity python-shell-process-environment "") | ||
| 1144 | (mapconcat #'indentity python-shell-exec-path ""))))) | ||
| 1145 | |||
| 1129 | (defun python-shell-parse-command () | 1146 | (defun python-shell-parse-command () |
| 1130 | "Calculate the string used to execute the inferior Python process." | 1147 | "Calculate the string used to execute the inferior Python process." |
| 1131 | (format "%s %s" python-shell-interpreter python-shell-interpreter-args)) | 1148 | (format "%s %s" python-shell-interpreter python-shell-interpreter-args)) |
| @@ -1222,6 +1239,42 @@ run). | |||
| 1222 | (pop-to-buffer proc-buffer-name)) | 1239 | (pop-to-buffer proc-buffer-name)) |
| 1223 | dedicated) | 1240 | dedicated) |
| 1224 | 1241 | ||
| 1242 | (defun run-python-internal () | ||
| 1243 | "Run an inferior Internal Python process. | ||
| 1244 | Input and output via buffer named after | ||
| 1245 | `python-shell-internal-buffer-name' and what | ||
| 1246 | `python-shell-internal-get-process-name' returns. This new kind | ||
| 1247 | of shell is intended to be used for generic communication related | ||
| 1248 | to defined configurations. The main difference with global or | ||
| 1249 | dedicated shells is that these ones are attached to a | ||
| 1250 | configuration, not a buffer. This means that can be used for | ||
| 1251 | example to retrieve the sys.path and other stuff, without messing | ||
| 1252 | with user shells. Runs the hook | ||
| 1253 | `inferior-python-mode-hook' (after the `comint-mode-hook' is | ||
| 1254 | run). \(Type \\[describe-mode] in the process buffer for a list | ||
| 1255 | of commands.)" | ||
| 1256 | (interactive) | ||
| 1257 | (save-excursion | ||
| 1258 | (let* ((cmd (python-shell-parse-command)) | ||
| 1259 | (proc-name (python-shell-internal-get-process-name)) | ||
| 1260 | (proc-buffer-name (format "*%s*" proc-name)) | ||
| 1261 | (process-environment | ||
| 1262 | (if python-shell-process-environment | ||
| 1263 | (python-util-merge 'list python-shell-process-environment | ||
| 1264 | process-environment 'string=) | ||
| 1265 | process-environment)) | ||
| 1266 | (exec-path | ||
| 1267 | (if python-shell-exec-path | ||
| 1268 | (python-util-merge 'list python-shell-exec-path | ||
| 1269 | exec-path 'string=) | ||
| 1270 | exec-path))) | ||
| 1271 | (when (not (comint-check-proc proc-buffer-name)) | ||
| 1272 | (let ((cmdlist (split-string-and-unquote cmd))) | ||
| 1273 | (set-buffer | ||
| 1274 | (apply 'make-comint proc-name (car cmdlist) nil | ||
| 1275 | (cdr cmdlist))) | ||
| 1276 | (inferior-python-mode)))))) | ||
| 1277 | |||
| 1225 | (defun python-shell-get-process () | 1278 | (defun python-shell-get-process () |
| 1226 | "Get inferior Python process for current buffer and return it." | 1279 | "Get inferior Python process for current buffer and return it." |
| 1227 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) | 1280 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) |
| @@ -1254,6 +1307,13 @@ run). | |||
| 1254 | dedicated-proc-buffer-name | 1307 | dedicated-proc-buffer-name |
| 1255 | global-proc-buffer-name)))) | 1308 | global-proc-buffer-name)))) |
| 1256 | 1309 | ||
| 1310 | (defun python-shell-internal-get-or-create-process () | ||
| 1311 | "Get or create an inferior Internal Python process." | ||
| 1312 | (let* ((proc-name (python-shell-internal-get-process-name)) | ||
| 1313 | (proc-buffer-name (format "*%s*" proc-name))) | ||
| 1314 | (run-python-internal) | ||
| 1315 | (get-buffer-process proc-buffer-name))) | ||
| 1316 | |||
| 1257 | (defun python-shell-send-string (string &optional process msg) | 1317 | (defun python-shell-send-string (string &optional process msg) |
| 1258 | "Send STRING to inferior Python PROCESS. | 1318 | "Send STRING to inferior Python PROCESS. |
| 1259 | When MSG is non-nil messages the first line of STRING." | 1319 | When MSG is non-nil messages the first line of STRING." |
| @@ -1307,6 +1367,21 @@ the output." | |||
| 1307 | (lambda (string) string) | 1367 | (lambda (string) string) |
| 1308 | (butlast (split-string output-buffer "\n")) "\n"))) | 1368 | (butlast (split-string output-buffer "\n")) "\n"))) |
| 1309 | 1369 | ||
| 1370 | (defun python-shell-internal-send-string (string) | ||
| 1371 | "Send STRING to the Internal Python interpreter. | ||
| 1372 | Returns the output. See `python-shell-send-string-no-output'." | ||
| 1373 | (python-shell-send-string-no-output | ||
| 1374 | ;; Makes this function compatible with the old | ||
| 1375 | ;; python-send-receive. (At least for CEDET). | ||
| 1376 | (replace-regexp-in-string "_emacs_out +" "" string) | ||
| 1377 | (python-shell-internal-get-or-create-process) nil)) | ||
| 1378 | |||
| 1379 | (define-obsolete-function-alias | ||
| 1380 | 'python-send-receive 'python-shell-internal-send-string "23.3" | ||
| 1381 | "Send STRING to inferior Python (if any) and return result. | ||
| 1382 | The result is what follows `_emacs_out' in the output. | ||
| 1383 | This is a no-op if `python-check-comint-prompt' returns nil.") | ||
| 1384 | |||
| 1310 | (defun python-shell-send-region (start end) | 1385 | (defun python-shell-send-region (start end) |
| 1311 | "Send the region delimited by START and END to inferior Python process." | 1386 | "Send the region delimited by START and END to inferior Python process." |
| 1312 | (interactive "r") | 1387 | (interactive "r") |