diff options
| author | Fabián Ezequiel Gallina | 2014-12-23 00:45:22 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2014-12-23 00:45:22 -0300 |
| commit | 75e114fa3b0b45a6356ae3fb580e8c928b45c258 (patch) | |
| tree | d6b4a36fd3152f851c55ac62bc79f87d2a950f87 /lisp/progmodes/python.el | |
| parent | d0fd23c552225314b7d0754a62fba670e3cfb9a4 (diff) | |
| download | emacs-75e114fa3b0b45a6356ae3fb580e8c928b45c258.tar.gz emacs-75e114fa3b0b45a6356ae3fb580e8c928b45c258.zip | |
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.
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 40 |
1 files changed, 10 insertions, 30 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 6d3916c07a5..632659c28bb 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -2400,9 +2400,12 @@ there for compatibility with CEDET.") | |||
| 2400 | (concat (file-remote-p default-directory) "/tmp") | 2400 | (concat (file-remote-p default-directory) "/tmp") |
| 2401 | temporary-file-directory)) | 2401 | temporary-file-directory)) |
| 2402 | (temp-file-name (make-temp-file "py")) | 2402 | (temp-file-name (make-temp-file "py")) |
| 2403 | ;; XXX: Python's built-in compile function accepts utf-8 as | ||
| 2404 | ;; input so there's no need to enforce a coding cookie. In | ||
| 2405 | ;; the future making `coding-system-for-write' match the | ||
| 2406 | ;; current buffer's coding may be a good idea. | ||
| 2403 | (coding-system-for-write 'utf-8)) | 2407 | (coding-system-for-write 'utf-8)) |
| 2404 | (with-temp-file temp-file-name | 2408 | (with-temp-file temp-file-name |
| 2405 | (insert "# -*- coding: utf-8 -*-\n") ;Not needed for Python-3. | ||
| 2406 | (insert string) | 2409 | (insert string) |
| 2407 | (delete-trailing-whitespace)) | 2410 | (delete-trailing-whitespace)) |
| 2408 | temp-file-name)) | 2411 | temp-file-name)) |
| @@ -2412,8 +2415,9 @@ there for compatibility with CEDET.") | |||
| 2412 | (interactive "sPython command: ") | 2415 | (interactive "sPython command: ") |
| 2413 | (let ((process (or process (python-shell-get-or-create-process)))) | 2416 | (let ((process (or process (python-shell-get-or-create-process)))) |
| 2414 | (if (string-match ".\n+." string) ;Multiline. | 2417 | (if (string-match ".\n+." string) ;Multiline. |
| 2415 | (let* ((temp-file-name (python-shell--save-temp-file string))) | 2418 | (let* ((temp-file-name (python-shell--save-temp-file string)) |
| 2416 | (python-shell-send-file temp-file-name process temp-file-name t)) | 2419 | (file-name (or (buffer-file-name) temp-file-name))) |
| 2420 | (python-shell-send-file file-name process temp-file-name t)) | ||
| 2417 | (comint-send-string process string) | 2421 | (comint-send-string process string) |
| 2418 | (when (or (not (string-match "\n\\'" string)) | 2422 | (when (or (not (string-match "\n\\'" string)) |
| 2419 | (string-match "\n[ \t].*\n?\\'" string)) | 2423 | (string-match "\n[ \t].*\n?\\'" string)) |
| @@ -2498,12 +2502,6 @@ Returns the output. See `python-shell-send-string-no-output'." | |||
| 2498 | (define-obsolete-function-alias | 2502 | (define-obsolete-function-alias |
| 2499 | 'python-send-string 'python-shell-internal-send-string "24.3") | 2503 | 'python-send-string 'python-shell-internal-send-string "24.3") |
| 2500 | 2504 | ||
| 2501 | (defvar python--use-fake-loc nil | ||
| 2502 | "If non-nil, use `compilation-fake-loc' to trace errors back to the buffer. | ||
| 2503 | If nil, regions of text are prepended by the corresponding number of empty | ||
| 2504 | lines and Python is told to output error messages referring to the whole | ||
| 2505 | source file.") | ||
| 2506 | |||
| 2507 | (defun python-shell-buffer-substring (start end &optional nomain) | 2505 | (defun python-shell-buffer-substring (start end &optional nomain) |
| 2508 | "Send buffer substring from START to END formatted for shell. | 2506 | "Send buffer substring from START to END formatted for shell. |
| 2509 | This is a wrapper over `buffer-substring' that takes care of | 2507 | This is a wrapper over `buffer-substring' that takes care of |
| @@ -2516,8 +2514,7 @@ the python shell: | |||
| 2516 | 3. Wraps indented regions under an \"if True:\" block so the | 2514 | 3. Wraps indented regions under an \"if True:\" block so the |
| 2517 | interpreter evaluates them correctly." | 2515 | interpreter evaluates them correctly." |
| 2518 | (let ((substring (buffer-substring-no-properties start end)) | 2516 | (let ((substring (buffer-substring-no-properties start end)) |
| 2519 | (fillstr (unless python--use-fake-loc | 2517 | (fillstr (make-string (1- (line-number-at-pos start)) ?\n)) |
| 2520 | (make-string (1- (line-number-at-pos start)) ?\n))) | ||
| 2521 | (toplevel-block-p (save-excursion | 2518 | (toplevel-block-p (save-excursion |
| 2522 | (goto-char start) | 2519 | (goto-char start) |
| 2523 | (or (zerop (line-number-at-pos start)) | 2520 | (or (zerop (line-number-at-pos start)) |
| @@ -2529,11 +2526,6 @@ the python shell: | |||
| 2529 | (if fillstr (insert fillstr)) | 2526 | (if fillstr (insert fillstr)) |
| 2530 | (insert substring) | 2527 | (insert substring) |
| 2531 | (goto-char (point-min)) | 2528 | (goto-char (point-min)) |
| 2532 | (unless python--use-fake-loc | ||
| 2533 | ;; python-shell--save-temp-file adds an extra coding line, which would | ||
| 2534 | ;; throw off the line-counts, so let's try to compensate here. | ||
| 2535 | (if (looking-at "[ \t]*[#\n]") | ||
| 2536 | (delete-region (point) (line-beginning-position 2)))) | ||
| 2537 | (when (not toplevel-block-p) | 2529 | (when (not toplevel-block-p) |
| 2538 | (insert "if True:") | 2530 | (insert "if True:") |
| 2539 | (delete-region (point) (line-end-position))) | 2531 | (delete-region (point) (line-end-position))) |
| @@ -2557,26 +2549,14 @@ the python shell: | |||
| 2557 | (line-number-at-pos if-name-main-start)) ?\n))))) | 2549 | (line-number-at-pos if-name-main-start)) ?\n))))) |
| 2558 | (buffer-substring-no-properties (point-min) (point-max))))) | 2550 | (buffer-substring-no-properties (point-min) (point-max))))) |
| 2559 | 2551 | ||
| 2560 | (declare-function compilation-fake-loc "compile" | ||
| 2561 | (marker file &optional line col)) | ||
| 2562 | |||
| 2563 | (defun python-shell-send-region (start end &optional nomain) | 2552 | (defun python-shell-send-region (start end &optional nomain) |
| 2564 | "Send the region delimited by START and END to inferior Python process." | 2553 | "Send the region delimited by START and END to inferior Python process." |
| 2565 | (interactive "r") | 2554 | (interactive "r") |
| 2566 | (let* ((python--use-fake-loc | 2555 | (let* ((string (python-shell-buffer-substring start end nomain)) |
| 2567 | (or python--use-fake-loc (not buffer-file-name))) | ||
| 2568 | (string (python-shell-buffer-substring start end nomain)) | ||
| 2569 | (process (python-shell-get-or-create-process)) | 2556 | (process (python-shell-get-or-create-process)) |
| 2570 | (_ (string-match "\\`\n*\\(.*\\)" string))) | 2557 | (_ (string-match "\\`\n*\\(.*\\)" string))) |
| 2571 | (message "Sent: %s..." (match-string 1 string)) | 2558 | (message "Sent: %s..." (match-string 1 string)) |
| 2572 | (let* ((temp-file-name (python-shell--save-temp-file string)) | 2559 | (python-shell-send-string string process))) |
| 2573 | (file-name (or (buffer-file-name) temp-file-name))) | ||
| 2574 | (python-shell-send-file file-name process temp-file-name t) | ||
| 2575 | (unless python--use-fake-loc | ||
| 2576 | (with-current-buffer (process-buffer process) | ||
| 2577 | (compilation-fake-loc (copy-marker start) temp-file-name | ||
| 2578 | 2)) ;; Not 1, because of the added coding line. | ||
| 2579 | )))) | ||
| 2580 | 2560 | ||
| 2581 | (defun python-shell-send-buffer (&optional arg) | 2561 | (defun python-shell-send-buffer (&optional arg) |
| 2582 | "Send the entire buffer to inferior Python process. | 2562 | "Send the entire buffer to inferior Python process. |