aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el58
1 files changed, 22 insertions, 36 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index bd8c734e0b9..b1c6b01c4dc 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1206,12 +1206,18 @@ the line will be re-indented automatically if needed."
1206 (eolp) 1206 (eolp)
1207 ;; Avoid re-indenting on extra colon 1207 ;; Avoid re-indenting on extra colon
1208 (not (equal ?: (char-before (1- (point))))) 1208 (not (equal ?: (char-before (1- (point)))))
1209 (not (python-syntax-comment-or-string-p)) 1209 (not (python-syntax-comment-or-string-p)))
1210 ;; Never re-indent at beginning of defun 1210 ;; Just re-indent dedenters
1211 (not (save-excursion 1211 (let ((dedenter-pos (python-info-dedenter-statement-p))
1212 (python-nav-beginning-of-statement) 1212 (current-pos (point)))
1213 (python-info-looking-at-beginning-of-defun)))) 1213 (when dedenter-pos
1214 (python-indent-line))))) 1214 (save-excursion
1215 (goto-char dedenter-pos)
1216 (python-indent-line)
1217 (unless (= (line-number-at-pos dedenter-pos)
1218 (line-number-at-pos current-pos))
1219 ;; Reindent region if this is a multiline statement
1220 (python-indent-region dedenter-pos current-pos)))))))))
1215 1221
1216 1222
1217;;; Navigation 1223;;; Navigation
@@ -2608,9 +2614,12 @@ there for compatibility with CEDET.")
2608 (concat (file-remote-p default-directory) "/tmp") 2614 (concat (file-remote-p default-directory) "/tmp")
2609 temporary-file-directory)) 2615 temporary-file-directory))
2610 (temp-file-name (make-temp-file "py")) 2616 (temp-file-name (make-temp-file "py"))
2617 ;; XXX: Python's built-in compile function accepts utf-8 as
2618 ;; input so there's no need to enforce a coding cookie. In
2619 ;; the future making `coding-system-for-write' match the
2620 ;; current buffer's coding may be a good idea.
2611 (coding-system-for-write 'utf-8)) 2621 (coding-system-for-write 'utf-8))
2612 (with-temp-file temp-file-name 2622 (with-temp-file temp-file-name
2613 (insert "# -*- coding: utf-8 -*-\n") ;Not needed for Python-3.
2614 (insert string) 2623 (insert string)
2615 (delete-trailing-whitespace)) 2624 (delete-trailing-whitespace))
2616 temp-file-name)) 2625 temp-file-name))
@@ -2620,8 +2629,9 @@ there for compatibility with CEDET.")
2620 (interactive "sPython command: ") 2629 (interactive "sPython command: ")
2621 (let ((process (or process (python-shell-get-or-create-process)))) 2630 (let ((process (or process (python-shell-get-or-create-process))))
2622 (if (string-match ".\n+." string) ;Multiline. 2631 (if (string-match ".\n+." string) ;Multiline.
2623 (let* ((temp-file-name (python-shell--save-temp-file string))) 2632 (let* ((temp-file-name (python-shell--save-temp-file string))
2624 (python-shell-send-file temp-file-name process temp-file-name t)) 2633 (file-name (or (buffer-file-name) temp-file-name)))
2634 (python-shell-send-file file-name process temp-file-name t))
2625 (comint-send-string process string) 2635 (comint-send-string process string)
2626 (when (or (not (string-match "\n\\'" string)) 2636 (when (or (not (string-match "\n\\'" string))
2627 (string-match "\n[ \t].*\n?\\'" string)) 2637 (string-match "\n[ \t].*\n?\\'" string))
@@ -2697,12 +2707,6 @@ Returns the output. See `python-shell-send-string-no-output'."
2697(define-obsolete-function-alias 2707(define-obsolete-function-alias
2698 'python-send-string 'python-shell-internal-send-string "24.3") 2708 'python-send-string 'python-shell-internal-send-string "24.3")
2699 2709
2700(defvar python--use-fake-loc nil
2701 "If non-nil, use `compilation-fake-loc' to trace errors back to the buffer.
2702If nil, regions of text are prepended by the corresponding number of empty
2703lines and Python is told to output error messages referring to the whole
2704source file.")
2705
2706(defun python-shell-buffer-substring (start end &optional nomain) 2710(defun python-shell-buffer-substring (start end &optional nomain)
2707 "Send buffer substring from START to END formatted for shell. 2711 "Send buffer substring from START to END formatted for shell.
2708This is a wrapper over `buffer-substring' that takes care of 2712This is a wrapper over `buffer-substring' that takes care of
@@ -2715,8 +2719,7 @@ the python shell:
2715 3. Wraps indented regions under an \"if True:\" block so the 2719 3. Wraps indented regions under an \"if True:\" block so the
2716 interpreter evaluates them correctly." 2720 interpreter evaluates them correctly."
2717 (let ((substring (buffer-substring-no-properties start end)) 2721 (let ((substring (buffer-substring-no-properties start end))
2718 (fillstr (unless python--use-fake-loc 2722 (fillstr (make-string (1- (line-number-at-pos start)) ?\n))
2719 (make-string (1- (line-number-at-pos start)) ?\n)))
2720 (toplevel-block-p (save-excursion 2723 (toplevel-block-p (save-excursion
2721 (goto-char start) 2724 (goto-char start)
2722 (or (zerop (line-number-at-pos start)) 2725 (or (zerop (line-number-at-pos start))
@@ -2728,11 +2731,6 @@ the python shell:
2728 (if fillstr (insert fillstr)) 2731 (if fillstr (insert fillstr))
2729 (insert substring) 2732 (insert substring)
2730 (goto-char (point-min)) 2733 (goto-char (point-min))
2731 (unless python--use-fake-loc
2732 ;; python-shell--save-temp-file adds an extra coding line, which would
2733 ;; throw off the line-counts, so let's try to compensate here.
2734 (if (looking-at "[ \t]*[#\n]")
2735 (delete-region (point) (line-beginning-position 2))))
2736 (when (not toplevel-block-p) 2734 (when (not toplevel-block-p)
2737 (insert "if True:") 2735 (insert "if True:")
2738 (delete-region (point) (line-end-position))) 2736 (delete-region (point) (line-end-position)))
@@ -2756,26 +2754,14 @@ the python shell:
2756 (line-number-at-pos if-name-main-start)) ?\n))))) 2754 (line-number-at-pos if-name-main-start)) ?\n)))))
2757 (buffer-substring-no-properties (point-min) (point-max))))) 2755 (buffer-substring-no-properties (point-min) (point-max)))))
2758 2756
2759(declare-function compilation-fake-loc "compile"
2760 (marker file &optional line col))
2761
2762(defun python-shell-send-region (start end &optional nomain) 2757(defun python-shell-send-region (start end &optional nomain)
2763 "Send the region delimited by START and END to inferior Python process." 2758 "Send the region delimited by START and END to inferior Python process."
2764 (interactive "r") 2759 (interactive "r")
2765 (let* ((python--use-fake-loc 2760 (let* ((string (python-shell-buffer-substring start end nomain))
2766 (or python--use-fake-loc (not buffer-file-name)))
2767 (string (python-shell-buffer-substring start end nomain))
2768 (process (python-shell-get-or-create-process)) 2761 (process (python-shell-get-or-create-process))
2769 (_ (string-match "\\`\n*\\(.*\\)" string))) 2762 (_ (string-match "\\`\n*\\(.*\\)" string)))
2770 (message "Sent: %s..." (match-string 1 string)) 2763 (message "Sent: %s..." (match-string 1 string))
2771 (let* ((temp-file-name (python-shell--save-temp-file string)) 2764 (python-shell-send-string string process)))
2772 (file-name (or (buffer-file-name) temp-file-name)))
2773 (python-shell-send-file file-name process temp-file-name t)
2774 (unless python--use-fake-loc
2775 (with-current-buffer (process-buffer process)
2776 (compilation-fake-loc (copy-marker start) temp-file-name
2777 2)) ;; Not 1, because of the added coding line.
2778 ))))
2779 2765
2780(defun python-shell-send-buffer (&optional arg) 2766(defun python-shell-send-buffer (&optional arg)
2781 "Send the entire buffer to inferior Python process. 2767 "Send the entire buffer to inferior Python process.