aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2012-05-17 00:03:06 -0300
committerFabián Ezequiel Gallina2012-05-17 00:03:06 -0300
commit62feb9156582ff25dd6ce05499506248e2b1521b (patch)
treed83360a4234faf939887987bfcd3295ff990f21c /lisp/progmodes/python.el
parent9ce938be0bd1381971dca8fa5c87d4f7dd449501 (diff)
downloademacs-62feb9156582ff25dd6ce05499506248e2b1521b.tar.gz
emacs-62feb9156582ff25dd6ce05499506248e2b1521b.zip
Better non-standard shell integration support
Added python-shell-prompt-output-regexp to match the prompts added before output in shells like iPython. With the value of this variable the output generated for python-shell-send-string-no-ouput is cleaned up. Moved completion variables and bindings setup for shell to inferior-python-mode definition. Renamed python-shell-completion-strings-code to python-shell-completion-string-code. improved python-shell-completion--get-completions string splitting. Cleaned up some unecessary messages. Better code sending need test for python-shell-completion-setup python-ffap-setup and python-eldoc-setup. Added example for iPython integration in the commentary section.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el78
1 files changed, 55 insertions, 23 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 6c4ecd97eab..6a5435afb24 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -58,7 +58,19 @@
58;; IPython) it should be easy to integrate another way to calculate 58;; IPython) it should be easy to integrate another way to calculate
59;; completions. You just need to specify your custom 59;; completions. You just need to specify your custom
60;; `python-shell-completion-setup-code' and 60;; `python-shell-completion-setup-code' and
61;; `python-shell-completion-strings-code' 61;; `python-shell-completion-string-code'
62
63;; Here is a complete example of the settings you would use for
64;; iPython
65
66;; (setq
67;; python-shell-interpreter "ipython"
68;; python-shell-interpreter-args ""
69;; python-shell-prompt-regexp "In \\[[0-9]+\\]: "
70;; python-shell-prompt-output-regexp "Out\\[[0-9]+\\]: "
71;; python-shell-completion-setup-code ""
72;; python-shell-completion-string-code
73;; "';'.join(__IP.complete('''%s'''))\n")
62 74
63;; Pdb tracking: when you execute a block of code that contains some 75;; Pdb tracking: when you execute a block of code that contains some
64;; call to pdb (or ipdb) it will prompt the block of code and will 76;; call to pdb (or ipdb) it will prompt the block of code and will
@@ -948,6 +960,13 @@ The regex should not contain a caret (^) at the beginning."
948 :group 'python 960 :group 'python
949 :safe 'stringp) 961 :safe 'stringp)
950 962
963(defcustom python-shell-prompt-output-regexp nil
964 "Regex matching output prompt of python shell.
965The regex should not contain a caret (^) at the beginning."
966 :type 'string
967 :group 'python
968 :safe 'stringp)
969
951(defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ " 970(defcustom python-shell-prompt-pdb-regexp "[(<]*[Ii]?[Pp]db[>)]+ "
952 "Regex matching pdb input prompt of python shell. 971 "Regex matching pdb input prompt of python shell.
953The regex should not contain a caret (^) at the beginning." 972The regex should not contain a caret (^) at the beginning."
@@ -1003,7 +1022,11 @@ OUTPUT is a string with the contents of the buffer."
1003(make-variable-buffer-local 'inferior-python-mode-current-file) 1022(make-variable-buffer-local 'inferior-python-mode-current-file)
1004 1023
1005(define-derived-mode inferior-python-mode comint-mode "Inferior Python" 1024(define-derived-mode inferior-python-mode comint-mode "Inferior Python"
1006 "Major mode for Python inferior process." 1025 "Major mode for Python inferior process.
1026Adds `python-shell-completion-complete-at-point' to the
1027`comint-dynamic-complete-functions' list. Also binds <tab> to
1028`python-shell-complete-or-indent' in the
1029`inferior-python-mode-map'."
1007 (set-syntax-table python-mode-syntax-table) 1030 (set-syntax-table python-mode-syntax-table)
1008 (setq mode-line-process '(":%s")) 1031 (setq mode-line-process '(":%s"))
1009 (setq comint-prompt-regexp (format "^\\(?:%s\\|%s\\|%s\\)" 1032 (setq comint-prompt-regexp (format "^\\(?:%s\\|%s\\|%s\\)"
@@ -1021,6 +1044,10 @@ OUTPUT is a string with the contents of the buffer."
1021 'completion-at-point) 1044 'completion-at-point)
1022 (add-hook 'completion-at-point-functions 1045 (add-hook 'completion-at-point-functions
1023 'python-shell-completion-complete-at-point nil 'local) 1046 'python-shell-completion-complete-at-point nil 'local)
1047 (add-to-list (make-local-variable 'comint-dynamic-complete-functions)
1048 'python-shell-completion-complete-at-point)
1049 (define-key inferior-python-mode-map (kbd "<tab>")
1050 'python-shell-completion-complete-or-indent)
1024 (compilation-shell-minor-mode 1)) 1051 (compilation-shell-minor-mode 1))
1025 1052
1026(defun run-python (dedicated cmd) 1053(defun run-python (dedicated cmd)
@@ -1128,6 +1155,21 @@ Return the output."
1128 ""))))) 1155 "")))))
1129 (python-shell-send-string string process msg) 1156 (python-shell-send-string string process msg)
1130 (accept-process-output process) 1157 (accept-process-output process)
1158 ;; Cleanup output prompt regexp
1159 (when (and (not (string= "" output-buffer))
1160 (> (length python-shell-prompt-output-regexp) 0))
1161 (setq output-buffer
1162 (with-temp-buffer
1163 (insert-string output-buffer)
1164 (goto-char (point-min))
1165 (forward-comment 1)
1166 (buffer-substring-no-properties
1167 (or
1168 (and (looking-at python-shell-prompt-output-regexp)
1169 (re-search-forward
1170 python-shell-prompt-output-regexp nil t 1))
1171 (point-marker))
1172 (point-max)))))
1131 (mapconcat 1173 (mapconcat
1132 (lambda (string) string) 1174 (lambda (string) string)
1133 (butlast (split-string output-buffer "\n")) "\n"))) 1175 (butlast (split-string output-buffer "\n")) "\n")))
@@ -1214,37 +1256,28 @@ else:
1214 return completions" 1256 return completions"
1215 "Code used to setup completion in inferior Python processes.") 1257 "Code used to setup completion in inferior Python processes.")
1216 1258
1217(defvar python-shell-completion-strings-code 1259(defvar python-shell-completion-string-code
1218 "';'.join(__COMPLETER_all_completions('''%s'''))\n" 1260 "';'.join(__COMPLETER_all_completions('''%s'''))\n"
1219 "Python code used to get a string of completions separated by semicolons.") 1261 "Python code used to get a string of completions separated by semicolons.")
1220 1262
1221(defun python-shell-completion-setup () 1263(defun python-shell-completion-setup ()
1222 "Send `python-shell-completion-setup-code' to inferior Python process. 1264 "Send `python-shell-completion-setup-code' to inferior Python process.
1223Also binds <tab> to `python-shell-complete-or-indent' in the
1224`inferior-python-mode-map' and adds
1225`python-shell-completion-complete-at-point' to the
1226`comint-dynamic-complete-functions' list.
1227It is specially designed to be added to the 1265It is specially designed to be added to the
1228`inferior-python-mode-hook'." 1266`inferior-python-mode-hook'."
1229 (when python-shell-completion-setup-code 1267 (when (> (length python-shell-completion-setup-code) 0)
1230 (python-shell-send-string-no-output 1268 (python-shell-send-string-no-output
1231 python-shell-completion-setup-code 1269 python-shell-completion-setup-code
1232 (get-buffer-process (current-buffer))) 1270 (get-buffer-process (current-buffer)))
1233 (message "Completion setup code sent.") 1271 (message "Completion setup code sent.")))
1234 (add-to-list (make-local-variable
1235 'comint-dynamic-complete-functions)
1236 'python-shell-completion-complete-at-point)
1237 (define-key inferior-python-mode-map (kbd "<tab>")
1238 'python-shell-completion-complete-or-indent)))
1239 1272
1240(defun python-shell-completion--get-completions (input process) 1273(defun python-shell-completion--get-completions (input process)
1241 "Retrieve available completions for INPUT using PROCESS." 1274 "Retrieve available completions for INPUT using PROCESS."
1242 (with-current-buffer (process-buffer process) 1275 (with-current-buffer (process-buffer process)
1243 (split-string 1276 (let ((completions (python-shell-send-string-no-output
1244 (or (python-shell-send-string-no-output 1277 (format python-shell-completion-string-code input)
1245 (format python-shell-completion-strings-code input) 1278 process)))
1246 process) "") 1279 (when (> (length completions) 2)
1247 ";\\|\"\\|'\\|(" t))) 1280 (split-string completions "^'\\|^\"\\|;\\|'$\\|\"$" t)))))
1248 1281
1249(defun python-shell-completion--get-completion (input completions) 1282(defun python-shell-completion--get-completion (input completions)
1250 "Get completion for INPUT using COMPLETIONS." 1283 "Get completion for INPUT using COMPLETIONS."
@@ -1509,8 +1542,6 @@ the if condition."
1509 :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*" 1542 :regexp "\\(?:^\\|[^/]\\)\\<\\([[:word:]/]+\\)\\W*"
1510 ;; Only expand in code. 1543 ;; Only expand in code.
1511 :enable-function (lambda () 1544 :enable-function (lambda ()
1512 (message "ppss %s" (not (nth 8 (syntax-ppss))))
1513 (message "autoinsert %s" python-skeleton-autoinsert)
1514 (and 1545 (and
1515 (not (nth 8 (syntax-ppss))) 1546 (not (nth 8 (syntax-ppss)))
1516 python-skeleton-autoinsert))) 1547 python-skeleton-autoinsert)))
@@ -1643,7 +1674,8 @@ The skeleton will be bound to python-skeleton-NAME."
1643 "Send `python-ffap-setup-code' to inferior Python process. 1674 "Send `python-ffap-setup-code' to inferior Python process.
1644It is specially designed to be added to the 1675It is specially designed to be added to the
1645`inferior-python-mode-hook'." 1676`inferior-python-mode-hook'."
1646 (when python-ffap-setup-code 1677
1678 (when (> (length python-ffap-setup-code) 0)
1647 (python-shell-send-string-no-output 1679 (python-shell-send-string-no-output
1648 python-ffap-setup-code 1680 python-ffap-setup-code
1649 (get-buffer-process (current-buffer))) 1681 (get-buffer-process (current-buffer)))
@@ -1725,7 +1757,7 @@ Runs COMMAND, a shell command, as if by `compile'. See
1725 "Send `python-eldoc-setup-code' to inferior Python process. 1757 "Send `python-eldoc-setup-code' to inferior Python process.
1726It is specially designed to be added to the 1758It is specially designed to be added to the
1727`inferior-python-mode-hook'." 1759`inferior-python-mode-hook'."
1728 (when python-eldoc-setup-code 1760 (when (> (length python-eldoc-setup-code) 0)
1729 (python-shell-send-string-no-output 1761 (python-shell-send-string-no-output
1730 python-eldoc-setup-code 1762 python-eldoc-setup-code
1731 (get-buffer-process (current-buffer))) 1763 (get-buffer-process (current-buffer)))