diff options
| author | Stefan Monnier | 2013-10-29 21:28:36 -0400 |
|---|---|---|
| committer | Stefan Monnier | 2013-10-29 21:28:36 -0400 |
| commit | 195ee2f0a990771753330ee912581da957eee034 (patch) | |
| tree | 1254bd3972fa69777897dd164e82946cb862624c /lisp | |
| parent | 4c9797cb77cee0d72084567ed8a7e97fcf41abff (diff) | |
| download | emacs-195ee2f0a990771753330ee912581da957eee034.tar.gz emacs-195ee2f0a990771753330ee912581da957eee034.zip | |
* lisp/progmodes/python.el (python-shell-get-buffer): New function.
(python-shell-get-process): Use it.
(python-shell-send-string): Always use utf-8 and add a cookie to tell
Python which encoding was used. Don't split-string since we only care
about the first line. Return the temp-file, if applicable.
(python-shell-send-region): Tell compile.el how to turn locations in
the temp-file into locations in the source buffer.
Diffstat (limited to 'lisp')
| -rw-r--r-- | lisp/ChangeLog | 10 | ||||
| -rw-r--r-- | lisp/progmodes/python.el | 41 |
2 files changed, 39 insertions, 12 deletions
diff --git a/lisp/ChangeLog b/lisp/ChangeLog index f15ba16da11..85fb2cbb73e 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog | |||
| @@ -1,3 +1,13 @@ | |||
| 1 | 2013-10-30 Stefan Monnier <monnier@iro.umontreal.ca> | ||
| 2 | |||
| 3 | * progmodes/python.el (python-shell-get-buffer): New function. | ||
| 4 | (python-shell-get-process): Use it. | ||
| 5 | (python-shell-send-string): Always use utf-8 and add a cookie to tell | ||
| 6 | Python which encoding was used. Don't split-string since we only care | ||
| 7 | about the first line. Return the temp-file, if applicable. | ||
| 8 | (python-shell-send-region): Tell compile.el how to turn locations in | ||
| 9 | the temp-file into locations in the source buffer. | ||
| 10 | |||
| 1 | 2013-10-29 Stefan Monnier <monnier@iro.umontreal.ca> | 11 | 2013-10-29 Stefan Monnier <monnier@iro.umontreal.ca> |
| 2 | 12 | ||
| 3 | * subr.el (undefined): Add missing behavior from the C code for | 13 | * subr.el (undefined): Add missing behavior from the C code for |
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index ce727391ce8..e0a7feb3a72 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -1968,8 +1968,8 @@ startup." | |||
| 1968 | (python-shell-parse-command) | 1968 | (python-shell-parse-command) |
| 1969 | (python-shell-internal-get-process-name) nil t)))) | 1969 | (python-shell-internal-get-process-name) nil t)))) |
| 1970 | 1970 | ||
| 1971 | (defun python-shell-get-process () | 1971 | (defun python-shell-get-buffer () |
| 1972 | "Get inferior Python process for current buffer and return it." | 1972 | "Get inferior Python buffer for current buffer and return it." |
| 1973 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) | 1973 | (let* ((dedicated-proc-name (python-shell-get-process-name t)) |
| 1974 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) | 1974 | (dedicated-proc-buffer-name (format "*%s*" dedicated-proc-name)) |
| 1975 | (global-proc-name (python-shell-get-process-name nil)) | 1975 | (global-proc-name (python-shell-get-process-name nil)) |
| @@ -1977,8 +1977,12 @@ startup." | |||
| 1977 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) | 1977 | (dedicated-running (comint-check-proc dedicated-proc-buffer-name)) |
| 1978 | (global-running (comint-check-proc global-proc-buffer-name))) | 1978 | (global-running (comint-check-proc global-proc-buffer-name))) |
| 1979 | ;; Always prefer dedicated | 1979 | ;; Always prefer dedicated |
| 1980 | (get-buffer-process (or (and dedicated-running dedicated-proc-buffer-name) | 1980 | (or (and dedicated-running dedicated-proc-buffer-name) |
| 1981 | (and global-running global-proc-buffer-name))))) | 1981 | (and global-running global-proc-buffer-name)))) |
| 1982 | |||
| 1983 | (defun python-shell-get-process () | ||
| 1984 | "Get inferior Python process for current buffer and return it." | ||
| 1985 | (get-buffer-process (python-shell-get-buffer))) | ||
| 1982 | 1986 | ||
| 1983 | (defun python-shell-get-or-create-process () | 1987 | (defun python-shell-get-or-create-process () |
| 1984 | "Get or create an inferior Python process for current buffer and return it." | 1988 | "Get or create an inferior Python process for current buffer and return it." |
| @@ -2034,26 +2038,32 @@ there for compatibility with CEDET.") | |||
| 2034 | 2038 | ||
| 2035 | (defun python-shell-send-string (string &optional process msg) | 2039 | (defun python-shell-send-string (string &optional process msg) |
| 2036 | "Send STRING to inferior Python PROCESS. | 2040 | "Send STRING to inferior Python PROCESS. |
| 2037 | When MSG is non-nil messages the first line of STRING." | 2041 | When MSG is non-nil messages the first line of STRING. |
| 2042 | If a temp file is used, return its name, otherwise return nil." | ||
| 2038 | (interactive "sPython command: ") | 2043 | (interactive "sPython command: ") |
| 2039 | (let ((process (or process (python-shell-get-or-create-process))) | 2044 | (let ((process (or process (python-shell-get-or-create-process))) |
| 2040 | (lines (split-string string "\n" t))) | 2045 | (_ (string-match "\\`\n*\\(.*\\)\\(\n.\\)?" string)) |
| 2041 | (and msg (message "Sent: %s..." (nth 0 lines))) | 2046 | (multiline (match-beginning 2))) |
| 2042 | (if (> (length lines) 1) | 2047 | (and msg (message "Sent: %s..." (match-string 1 string))) |
| 2048 | (if multiline | ||
| 2043 | (let* ((temporary-file-directory | 2049 | (let* ((temporary-file-directory |
| 2044 | (if (file-remote-p default-directory) | 2050 | (if (file-remote-p default-directory) |
| 2045 | (concat (file-remote-p default-directory) "/tmp") | 2051 | (concat (file-remote-p default-directory) "/tmp") |
| 2046 | temporary-file-directory)) | 2052 | temporary-file-directory)) |
| 2047 | (temp-file-name (make-temp-file "py")) | 2053 | (temp-file-name (make-temp-file "py")) |
| 2054 | (coding-system-for-write 'utf-8) | ||
| 2048 | (file-name (or (buffer-file-name) temp-file-name))) | 2055 | (file-name (or (buffer-file-name) temp-file-name))) |
| 2049 | (with-temp-file temp-file-name | 2056 | (with-temp-file temp-file-name |
| 2057 | (insert "# -*- coding: utf-8 -*-\n") | ||
| 2050 | (insert string) | 2058 | (insert string) |
| 2051 | (delete-trailing-whitespace)) | 2059 | (delete-trailing-whitespace)) |
| 2052 | (python-shell-send-file file-name process temp-file-name)) | 2060 | (python-shell-send-file file-name process temp-file-name) |
| 2061 | temp-file-name) | ||
| 2053 | (comint-send-string process string) | 2062 | (comint-send-string process string) |
| 2054 | (when (or (not (string-match "\n$" string)) | 2063 | (when (or (not (string-match "\n$" string)) |
| 2055 | (string-match "\n[ \t].*\n?$" string)) | 2064 | (string-match "\n[ \t].*\n?$" string)) |
| 2056 | (comint-send-string process "\n"))))) | 2065 | (comint-send-string process "\n")) |
| 2066 | nil))) | ||
| 2057 | 2067 | ||
| 2058 | (defvar python-shell-output-filter-in-progress nil) | 2068 | (defvar python-shell-output-filter-in-progress nil) |
| 2059 | (defvar python-shell-output-filter-buffer nil) | 2069 | (defvar python-shell-output-filter-buffer nil) |
| @@ -2179,11 +2189,18 @@ the python shell: | |||
| 2179 | (line-number-at-pos if-name-main-start)) ?\n))))) | 2189 | (line-number-at-pos if-name-main-start)) ?\n))))) |
| 2180 | (buffer-substring-no-properties (point-min) (point-max))))) | 2190 | (buffer-substring-no-properties (point-min) (point-max))))) |
| 2181 | 2191 | ||
| 2192 | (declare-function compilation-fake-loc "compile" | ||
| 2193 | (marker file &optional line col)) | ||
| 2194 | |||
| 2182 | (defun python-shell-send-region (start end) | 2195 | (defun python-shell-send-region (start end) |
| 2183 | "Send the region delimited by START and END to inferior Python process." | 2196 | "Send the region delimited by START and END to inferior Python process." |
| 2184 | (interactive "r") | 2197 | (interactive "r") |
| 2185 | (python-shell-send-string | 2198 | (let ((temp-file-name |
| 2186 | (python-shell-buffer-substring start end) nil t)) | 2199 | (python-shell-send-string |
| 2200 | (python-shell-buffer-substring start end) nil t))) | ||
| 2201 | (when temp-file-name | ||
| 2202 | (with-current-buffer (python-shell-get-buffer) | ||
| 2203 | (compilation-fake-loc (copy-marker start) temp-file-name))))) | ||
| 2187 | 2204 | ||
| 2188 | (defun python-shell-send-buffer (&optional arg) | 2205 | (defun python-shell-send-buffer (&optional arg) |
| 2189 | "Send the entire buffer to inferior Python process. | 2206 | "Send the entire buffer to inferior Python process. |