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
commit9ce938be0bd1381971dca8fa5c87d4f7dd449501 (patch)
tree7da94fd8bd254295757cae91fa8a34267c917d30 /lisp/progmodes/python.el
parent73ed683681d2941a1ae6f781f6c3c3f339d42fbd (diff)
downloademacs-9ce938be0bd1381971dca8fa5c87d4f7dd449501.tar.gz
emacs-9ce938be0bd1381971dca8fa5c87d4f7dd449501.zip
Shell integration improvements and cleanups
Removed functions python-shell-clear-latest-output and python-shell-send-and-clear-output in favor of python-shell-send-string-no-output. Also python-shell-send-string now supports multiline string statements so you won't have to worry calling python-shell-send-file again. All this changes should make integrations with other Python shells than standard more robust.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el144
1 files changed, 59 insertions, 85 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index c40c2e77d0d..6c4ecd97eab 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1094,32 +1094,49 @@ commands.)"
1094 dedicated-proc-buffer-name 1094 dedicated-proc-buffer-name
1095 global-proc-buffer-name)))) 1095 global-proc-buffer-name))))
1096 1096
1097(defun python-shell-send-string (string &optional process) 1097(defun python-shell-send-string (string &optional process msg)
1098 "Send STRING to inferior Python PROCESS." 1098 "Send STRING to inferior Python PROCESS.
1099When MSG is non-nil messages the first line of STRING."
1099 (interactive "sPython command: ") 1100 (interactive "sPython command: ")
1100 (let ((process (or process (python-shell-get-or-create-process)))) 1101 (let ((process (or process (python-shell-get-or-create-process)))
1101 (when (called-interactively-p 'interactive) 1102 (lines (split-string string "\n" t)))
1102 (message (format "Sent: %s..." string))) 1103 (when msg
1103 (comint-send-string process string) 1104 (message (format "Sent: %s..." (nth 0 lines))))
1104 (when (or (not (string-match "\n$" string)) 1105 (if (> (length lines) 1)
1105 (string-match "\n[ \t].*\n?$" string)) 1106 (let* ((temp-file-name (make-temp-file "py"))
1106 (comint-send-string process "\n")))) 1107 (file-name (or (buffer-file-name) temp-file-name)))
1108 (with-temp-file temp-file-name
1109 (insert string)
1110 (delete-trailing-whitespace))
1111 (python-shell-send-file file-name process temp-file-name))
1112 (comint-send-string process string)
1113 (when (or (not (string-match "\n$" string))
1114 (string-match "\n[ \t].*\n?$" string))
1115 (comint-send-string process "\n")))))
1116
1117(defun python-shell-send-string-no-output (string &optional process msg)
1118 "Send STRING to PROCESS and inhibit output.
1119When MSG is non-nil messages the first line of STRING.
1120Return the output."
1121 (let* ((output-buffer)
1122 (process (or process (python-shell-get-or-create-process)))
1123 (comint-preoutput-filter-functions
1124 (append comint-preoutput-filter-functions
1125 '(ansi-color-filter-apply
1126 (lambda (string)
1127 (setq output-buffer (concat output-buffer string))
1128 "")))))
1129 (python-shell-send-string string process msg)
1130 (accept-process-output process)
1131 (mapconcat
1132 (lambda (string) string)
1133 (butlast (split-string output-buffer "\n")) "\n")))
1107 1134
1108(defun python-shell-send-region (start end) 1135(defun python-shell-send-region (start end)
1109 "Send the region delimited by START and END to inferior Python process." 1136 "Send the region delimited by START and END to inferior Python process."
1110 (interactive "r") 1137 (interactive "r")
1111 (let* ((contents (buffer-substring start end)) 1138 (let ((deactivate-mark nil))
1112 (current-file (buffer-file-name)) 1139 (python-shell-send-string (buffer-substring start end) nil t)))
1113 (process (python-shell-get-or-create-process))
1114 (temp-file (make-temp-file "py")))
1115 (with-temp-file temp-file
1116 (insert contents)
1117 (delete-trailing-whitespace)
1118 (goto-char (point-min))
1119 (message (format "Sent: %s..."
1120 (buffer-substring (point-min)
1121 (line-end-position)))))
1122 (python-shell-send-file current-file process temp-file)))
1123 1140
1124(defun python-shell-send-buffer () 1141(defun python-shell-send-buffer ()
1125 "Send the entire buffer to inferior Python process." 1142 "Send the entire buffer to inferior Python process."
@@ -1148,10 +1165,12 @@ If TEMP-FILE-NAME is passed then that file is used for processing
1148instead, while internally the shell will continue to use 1165instead, while internally the shell will continue to use
1149FILE-NAME." 1166FILE-NAME."
1150 (interactive "fFile to send: ") 1167 (interactive "fFile to send: ")
1151 (let ((process (or process (python-shell-get-or-create-process))) 1168 (let* ((process (or process (python-shell-get-or-create-process)))
1152 (file-name (expand-file-name file-name)) 1169 (temp-file-name (when temp-file-name
1153 (temp-file-name (when temp-file-name 1170 (expand-file-name temp-file-name)))
1154 (expand-file-name temp-file-name)))) 1171 (file-name (or (expand-file-name file-name) temp-file-name)))
1172 (when (not file-name)
1173 (error "If FILE-NAME is nil then TEMP-FILE-NAME must be non-nil"))
1155 (find-file-noselect file-name) 1174 (find-file-noselect file-name)
1156 (with-current-buffer (process-buffer process) 1175 (with-current-buffer (process-buffer process)
1157 (setq inferior-python-mode-current-file 1176 (setq inferior-python-mode-current-file
@@ -1164,41 +1183,6 @@ FILE-NAME."
1164 (or temp-file-name file-name) file-name) 1183 (or temp-file-name file-name) file-name)
1165 process))) 1184 process)))
1166 1185
1167(defun python-shell-clear-latest-output ()
1168 "Clear latest output from the Python shell.
1169Return the cleaned output."
1170 (interactive)
1171 (when (and comint-last-output-start
1172 comint-last-prompt-overlay)
1173 (save-excursion
1174 (let* ((last-output-end
1175 (save-excursion
1176 (goto-char
1177 (overlay-start comint-last-prompt-overlay))
1178 (forward-comment -1)
1179 (point-marker)))
1180 (last-output
1181 (buffer-substring-no-properties
1182 comint-last-output-start last-output-end)))
1183 (when (< 0 (length last-output))
1184 (goto-char comint-last-output-start)
1185 (delete-region comint-last-output-start last-output-end)
1186 (delete-char -1)
1187 last-output)))))
1188
1189(defun python-shell-send-and-clear-output (string process)
1190 "Send STRING to PROCESS and clear the output.
1191Return the cleaned output."
1192 (interactive)
1193 (python-shell-send-string string process)
1194 (accept-process-output process)
1195 (with-current-buffer (process-buffer process)
1196 (let ((output (python-shell-clear-latest-output)))
1197 (forward-line -1)
1198 (kill-whole-line)
1199 (goto-char (overlay-end comint-last-prompt-overlay))
1200 output)))
1201
1202(defun python-shell-switch-to-shell () 1186(defun python-shell-switch-to-shell ()
1203 "Switch to inferior Python process buffer." 1187 "Switch to inferior Python process buffer."
1204 (interactive) 1188 (interactive)
@@ -1243,14 +1227,10 @@ Also binds <tab> to `python-shell-complete-or-indent' in the
1243It is specially designed to be added to the 1227It is specially designed to be added to the
1244`inferior-python-mode-hook'." 1228`inferior-python-mode-hook'."
1245 (when python-shell-completion-setup-code 1229 (when python-shell-completion-setup-code
1246 (let ((temp-file (make-temp-file "py")) 1230 (python-shell-send-string-no-output
1247 (process (get-buffer-process (current-buffer)))) 1231 python-shell-completion-setup-code
1248 (with-temp-file temp-file 1232 (get-buffer-process (current-buffer)))
1249 (insert python-shell-completion-setup-code) 1233 (message "Completion setup code sent.")
1250 (delete-trailing-whitespace)
1251 (goto-char (point-min)))
1252 (python-shell-send-file temp-file process)
1253 (message (format "Completion setup code sent.")))
1254 (add-to-list (make-local-variable 1234 (add-to-list (make-local-variable
1255 'comint-dynamic-complete-functions) 1235 'comint-dynamic-complete-functions)
1256 'python-shell-completion-complete-at-point) 1236 'python-shell-completion-complete-at-point)
@@ -1261,7 +1241,7 @@ It is specially designed to be added to the
1261 "Retrieve available completions for INPUT using PROCESS." 1241 "Retrieve available completions for INPUT using PROCESS."
1262 (with-current-buffer (process-buffer process) 1242 (with-current-buffer (process-buffer process)
1263 (split-string 1243 (split-string
1264 (or (python-shell-send-and-clear-output 1244 (or (python-shell-send-string-no-output
1265 (format python-shell-completion-strings-code input) 1245 (format python-shell-completion-strings-code input)
1266 process) "") 1246 process) "")
1267 ";\\|\"\\|'\\|(" t))) 1247 ";\\|\"\\|'\\|(" t)))
@@ -1664,13 +1644,10 @@ The skeleton will be bound to python-skeleton-NAME."
1664It is specially designed to be added to the 1644It is specially designed to be added to the
1665`inferior-python-mode-hook'." 1645`inferior-python-mode-hook'."
1666 (when python-ffap-setup-code 1646 (when python-ffap-setup-code
1667 (let ((temp-file (make-temp-file "py"))) 1647 (python-shell-send-string-no-output
1668 (with-temp-file temp-file 1648 python-ffap-setup-code
1669 (insert python-ffap-setup-code) 1649 (get-buffer-process (current-buffer)))
1670 (delete-trailing-whitespace) 1650 (message "FFAP setup code sent.")))
1671 (goto-char (point-min)))
1672 (python-shell-send-file temp-file (get-buffer-process (current-buffer)))
1673 (message (format "FFAP setup code sent.")))))
1674 1651
1675(defun python-ffap-module-path (module) 1652(defun python-ffap-module-path (module)
1676 "Function for `ffap-alist' to return path for MODULE." 1653 "Function for `ffap-alist' to return path for MODULE."
@@ -1681,7 +1658,7 @@ It is specially designed to be added to the
1681 (if (not process) 1658 (if (not process)
1682 nil 1659 nil
1683 (let ((module-file 1660 (let ((module-file
1684 (python-shell-send-and-clear-output 1661 (python-shell-send-string-no-output
1685 (format python-ffap-string-code module) process))) 1662 (format python-ffap-string-code module) process)))
1686 (when module-file 1663 (when module-file
1687 (substring-no-properties module-file 1 -1)))))) 1664 (substring-no-properties module-file 1 -1))))))
@@ -1749,13 +1726,10 @@ Runs COMMAND, a shell command, as if by `compile'. See
1749It is specially designed to be added to the 1726It is specially designed to be added to the
1750`inferior-python-mode-hook'." 1727`inferior-python-mode-hook'."
1751 (when python-eldoc-setup-code 1728 (when python-eldoc-setup-code
1752 (let ((temp-file (make-temp-file "py"))) 1729 (python-shell-send-string-no-output
1753 (with-temp-file temp-file 1730 python-eldoc-setup-code
1754 (insert python-eldoc-setup-code) 1731 (get-buffer-process (current-buffer)))
1755 (delete-trailing-whitespace) 1732 (message "Eldoc setup code sent.")))
1756 (goto-char (point-min)))
1757 (python-shell-send-file temp-file (get-buffer-process (current-buffer)))
1758 (message (format "Eldoc setup code sent.")))))
1759 1733
1760(defun python-eldoc--get-doc-at-point (&optional force-input force-process) 1734(defun python-eldoc--get-doc-at-point (&optional force-input force-process)
1761 "Internal implementation to get documentation at point. 1735 "Internal implementation to get documentation at point.
@@ -1786,7 +1760,7 @@ will be used. If not FORCE-PROCESS is passed what
1786 (forward-char) 1760 (forward-char)
1787 (delete-region (point-marker) (search-forward "self.")) 1761 (delete-region (point-marker) (search-forward "self."))
1788 (setq input (buffer-substring (point-min) (point-max))))) 1762 (setq input (buffer-substring (point-min) (point-max)))))
1789 (python-shell-send-and-clear-output 1763 (python-shell-send-string-no-output
1790 (format python-eldoc-string-code input) process)))) 1764 (format python-eldoc-string-code input) process))))
1791 (with-current-buffer (process-buffer process) 1765 (with-current-buffer (process-buffer process)
1792 (when comint-last-prompt-overlay 1766 (when comint-last-prompt-overlay