Fix line numbers on Python shell.

* lisp/progmodes/python.el (python-shell--save-temp-file): Do not
append coding cookie.
(python-shell-send-string): Generalize for
python-shell-send-region.
(python--use-fake-loc): Delete var.
(python-shell-buffer-substring): Cleanup fake-loc logic.
(python-shell-send-region): Remove fake-loc logic, simplify.
This commit is contained in:
Fabián Ezequiel Gallina 2014-12-23 00:45:22 -03:00
parent d0fd23c552
commit 75e114fa3b
2 changed files with 21 additions and 30 deletions

View file

@ -1,3 +1,14 @@
2014-12-23 Fabián Ezequiel Gallina <fgallina@gnu.org>
Fix line numbers on Python shell.
* progmodes/python.el (python-shell--save-temp-file): Do not
append coding cookie.
(python-shell-send-string): Generalize for
python-shell-send-region.
(python--use-fake-loc): Delete var.
(python-shell-buffer-substring): Cleanup fake-loc logic.
(python-shell-send-region): Remove fake-loc logic, simplify.
2014-12-22 Fabián Ezequiel Gallina <fgallina@gnu.org> 2014-12-22 Fabián Ezequiel Gallina <fgallina@gnu.org>
* progmodes/python.el (python-indent-post-self-insert-function): * progmodes/python.el (python-indent-post-self-insert-function):

View file

@ -2400,9 +2400,12 @@ there for compatibility with CEDET.")
(concat (file-remote-p default-directory) "/tmp") (concat (file-remote-p default-directory) "/tmp")
temporary-file-directory)) temporary-file-directory))
(temp-file-name (make-temp-file "py")) (temp-file-name (make-temp-file "py"))
;; XXX: Python's built-in compile function accepts utf-8 as
;; input so there's no need to enforce a coding cookie. In
;; the future making `coding-system-for-write' match the
;; current buffer's coding may be a good idea.
(coding-system-for-write 'utf-8)) (coding-system-for-write 'utf-8))
(with-temp-file temp-file-name (with-temp-file temp-file-name
(insert "# -*- coding: utf-8 -*-\n") ;Not needed for Python-3.
(insert string) (insert string)
(delete-trailing-whitespace)) (delete-trailing-whitespace))
temp-file-name)) temp-file-name))
@ -2412,8 +2415,9 @@ there for compatibility with CEDET.")
(interactive "sPython command: ") (interactive "sPython command: ")
(let ((process (or process (python-shell-get-or-create-process)))) (let ((process (or process (python-shell-get-or-create-process))))
(if (string-match ".\n+." string) ;Multiline. (if (string-match ".\n+." string) ;Multiline.
(let* ((temp-file-name (python-shell--save-temp-file string))) (let* ((temp-file-name (python-shell--save-temp-file string))
(python-shell-send-file temp-file-name process temp-file-name t)) (file-name (or (buffer-file-name) temp-file-name)))
(python-shell-send-file file-name process temp-file-name t))
(comint-send-string process string) (comint-send-string process string)
(when (or (not (string-match "\n\\'" string)) (when (or (not (string-match "\n\\'" string))
(string-match "\n[ \t].*\n?\\'" string)) (string-match "\n[ \t].*\n?\\'" string))
@ -2498,12 +2502,6 @@ Returns the output. See `python-shell-send-string-no-output'."
(define-obsolete-function-alias (define-obsolete-function-alias
'python-send-string 'python-shell-internal-send-string "24.3") 'python-send-string 'python-shell-internal-send-string "24.3")
(defvar python--use-fake-loc nil
"If non-nil, use `compilation-fake-loc' to trace errors back to the buffer.
If nil, regions of text are prepended by the corresponding number of empty
lines and Python is told to output error messages referring to the whole
source file.")
(defun python-shell-buffer-substring (start end &optional nomain) (defun python-shell-buffer-substring (start end &optional nomain)
"Send buffer substring from START to END formatted for shell. "Send buffer substring from START to END formatted for shell.
This is a wrapper over `buffer-substring' that takes care of This is a wrapper over `buffer-substring' that takes care of
@ -2516,8 +2514,7 @@ the python shell:
3. Wraps indented regions under an \"if True:\" block so the 3. Wraps indented regions under an \"if True:\" block so the
interpreter evaluates them correctly." interpreter evaluates them correctly."
(let ((substring (buffer-substring-no-properties start end)) (let ((substring (buffer-substring-no-properties start end))
(fillstr (unless python--use-fake-loc (fillstr (make-string (1- (line-number-at-pos start)) ?\n))
(make-string (1- (line-number-at-pos start)) ?\n)))
(toplevel-block-p (save-excursion (toplevel-block-p (save-excursion
(goto-char start) (goto-char start)
(or (zerop (line-number-at-pos start)) (or (zerop (line-number-at-pos start))
@ -2529,11 +2526,6 @@ the python shell:
(if fillstr (insert fillstr)) (if fillstr (insert fillstr))
(insert substring) (insert substring)
(goto-char (point-min)) (goto-char (point-min))
(unless python--use-fake-loc
;; python-shell--save-temp-file adds an extra coding line, which would
;; throw off the line-counts, so let's try to compensate here.
(if (looking-at "[ \t]*[#\n]")
(delete-region (point) (line-beginning-position 2))))
(when (not toplevel-block-p) (when (not toplevel-block-p)
(insert "if True:") (insert "if True:")
(delete-region (point) (line-end-position))) (delete-region (point) (line-end-position)))
@ -2557,26 +2549,14 @@ the python shell:
(line-number-at-pos if-name-main-start)) ?\n))))) (line-number-at-pos if-name-main-start)) ?\n)))))
(buffer-substring-no-properties (point-min) (point-max))))) (buffer-substring-no-properties (point-min) (point-max)))))
(declare-function compilation-fake-loc "compile"
(marker file &optional line col))
(defun python-shell-send-region (start end &optional nomain) (defun python-shell-send-region (start end &optional nomain)
"Send the region delimited by START and END to inferior Python process." "Send the region delimited by START and END to inferior Python process."
(interactive "r") (interactive "r")
(let* ((python--use-fake-loc (let* ((string (python-shell-buffer-substring start end nomain))
(or python--use-fake-loc (not buffer-file-name)))
(string (python-shell-buffer-substring start end nomain))
(process (python-shell-get-or-create-process)) (process (python-shell-get-or-create-process))
(_ (string-match "\\`\n*\\(.*\\)" string))) (_ (string-match "\\`\n*\\(.*\\)" string)))
(message "Sent: %s..." (match-string 1 string)) (message "Sent: %s..." (match-string 1 string))
(let* ((temp-file-name (python-shell--save-temp-file string)) (python-shell-send-string string process)))
(file-name (or (buffer-file-name) temp-file-name)))
(python-shell-send-file file-name process temp-file-name t)
(unless python--use-fake-loc
(with-current-buffer (process-buffer process)
(compilation-fake-loc (copy-marker start) temp-file-name
2)) ;; Not 1, because of the added coding line.
))))
(defun python-shell-send-buffer (&optional arg) (defun python-shell-send-buffer (&optional arg)
"Send the entire buffer to inferior Python process. "Send the entire buffer to inferior Python process.