aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2014-12-27 13:31:20 -0300
committerFabián Ezequiel Gallina2014-12-27 13:31:20 -0300
commit35e1f9d9fcbaab51808e05f514e63927f959ae51 (patch)
treee9d7d0ef8b1d61d728d4d076e46088a3b9b29d73 /lisp/progmodes/python.el
parentf315b69922db769f3358e15616aa76c965be8a89 (diff)
parenta5f38fa1cc8eafe13f2073ebfaa8205b5e919d17 (diff)
downloademacs-35e1f9d9fcbaab51808e05f514e63927f959ae51.tar.gz
emacs-35e1f9d9fcbaab51808e05f514e63927f959ae51.zip
Merge from origin/emacs-24
a5f38fa Fix ChangeLog typo c6400e1 Fix composition of characters from Syriac and Arabis scripts. 7e9dfde python.el: Fix message when sending region. 800260c python.el: Cleanup temp files even with eval errors. ed65b91 Fix for previous commit 2dd5163 python.el: Handle file encoding for shell. 7aa506e Spelling fixes 4cd6d77 * automated/tramp-tests.el (tramp-test17-insert-directory): Do not expect a given order of "." and "..". a41d07b Fix rendering of composed caharacters on the mode line. (Bug#19435) b70977c Small doc markup fixes 73c050c * doc/lispref/modes.texi (Defining Minor Modes, SMIE Lexer): Markup fixes. 1783e6c ChangeLog fix c741b1b TUTORIAL.es: Improve style consistency f89efea TUTORIAL.es: spelling fixes 0d48826 Avoid compiler warning. Conflicts: doc/lispref/ChangeLog doc/lispref/control.texi etc/ChangeLog lisp/ChangeLog src/ChangeLog test/ChangeLog
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el158
1 files changed, 111 insertions, 47 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index b1c6b01c4dc..4dd7cbcd629 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -395,7 +395,18 @@
395 (* ?\\ ?\\) (any ?\' ?\"))) 395 (* ?\\ ?\\) (any ?\' ?\")))
396 (* ?\\ ?\\) 396 (* ?\\ ?\\)
397 ;; Match single or triple quotes of any kind. 397 ;; Match single or triple quotes of any kind.
398 (group (or "\"" "\"\"\"" "'" "'''")))))) 398 (group (or "\"" "\"\"\"" "'" "'''")))))
399 (coding-cookie . ,(rx line-start ?# (* space)
400 (or
401 ;; # coding=<encoding name>
402 (: "coding" (or ?: ?=) (* space) (group-n 1 (+ (or word ?-))))
403 ;; # -*- coding: <encoding name> -*-
404 (: "-*-" (* space) "coding:" (* space)
405 (group-n 1 (+ (or word ?-))) (* space) "-*-")
406 ;; # vim: set fileencoding=<encoding name> :
407 (: "vim:" (* space) "set" (+ space)
408 "fileencoding" (* space) ?= (* space)
409 (group-n 1 (+ (or word ?-))) (* space) ":")))))
399 "Additional Python specific sexps for `python-rx'") 410 "Additional Python specific sexps for `python-rx'")
400 411
401 (defmacro python-rx (&rest regexps) 412 (defmacro python-rx (&rest regexps)
@@ -2614,11 +2625,7 @@ there for compatibility with CEDET.")
2614 (concat (file-remote-p default-directory) "/tmp") 2625 (concat (file-remote-p default-directory) "/tmp")
2615 temporary-file-directory)) 2626 temporary-file-directory))
2616 (temp-file-name (make-temp-file "py")) 2627 (temp-file-name (make-temp-file "py"))
2617 ;; XXX: Python's built-in compile function accepts utf-8 as 2628 (coding-system-for-write (python-info-encoding)))
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.
2621 (coding-system-for-write 'utf-8))
2622 (with-temp-file temp-file-name 2629 (with-temp-file temp-file-name
2623 (insert string) 2630 (insert string)
2624 (delete-trailing-whitespace)) 2631 (delete-trailing-whitespace))
@@ -2716,16 +2723,28 @@ the python shell:
2716 \"if __name__ == '__main__'\" block will be removed. 2723 \"if __name__ == '__main__'\" block will be removed.
2717 2. When a subregion of the buffer is sent, it takes care of 2724 2. When a subregion of the buffer is sent, it takes care of
2718 appending extra empty lines so tracebacks are correct. 2725 appending extra empty lines so tracebacks are correct.
2719 3. Wraps indented regions under an \"if True:\" block so the 2726 3. When the region sent is a substring of the current buffer, a
2727 coding cookie is added.
2728 4. Wraps indented regions under an \"if True:\" block so the
2720 interpreter evaluates them correctly." 2729 interpreter evaluates them correctly."
2721 (let ((substring (buffer-substring-no-properties start end)) 2730 (let* ((substring (buffer-substring-no-properties start end))
2722 (fillstr (make-string (1- (line-number-at-pos start)) ?\n)) 2731 (buffer-substring-p (save-restriction
2723 (toplevel-block-p (save-excursion 2732 (widen)
2724 (goto-char start) 2733 (not (equal (list (point-min) (point-max))
2725 (or (zerop (line-number-at-pos start)) 2734 (list start end)))))
2726 (progn 2735 (encoding (python-info-encoding))
2727 (python-util-forward-comment 1) 2736 (fillstr (concat
2728 (zerop (current-indentation))))))) 2737 (when buffer-substring-p
2738 (format "# -*- coding: %s -*-\n" encoding))
2739 (make-string
2740 (- (line-number-at-pos start)
2741 (if buffer-substring-p 2 1)) ?\n)))
2742 (toplevel-block-p (save-excursion
2743 (goto-char start)
2744 (or (zerop (line-number-at-pos start))
2745 (progn
2746 (python-util-forward-comment 1)
2747 (zerop (current-indentation)))))))
2729 (with-temp-buffer 2748 (with-temp-buffer
2730 (python-mode) 2749 (python-mode)
2731 (if fillstr (insert fillstr)) 2750 (if fillstr (insert fillstr))
@@ -2741,36 +2760,52 @@ the python shell:
2741 (when (python-nav-if-name-main) 2760 (when (python-nav-if-name-main)
2742 (cons (point) 2761 (cons (point)
2743 (progn (python-nav-forward-sexp-safe) 2762 (progn (python-nav-forward-sexp-safe)
2763 ;; Include ending newline
2764 (forward-line 1)
2744 (point))))))) 2765 (point)))))))
2745 ;; Oh destructuring bind, how I miss you. 2766 ;; Oh destructuring bind, how I miss you.
2746 (if-name-main-start (car if-name-main-start-end)) 2767 (if-name-main-start (car if-name-main-start-end))
2747 (if-name-main-end (cdr if-name-main-start-end))) 2768 (if-name-main-end (cdr if-name-main-start-end))
2769 (fillstr (make-string
2770 (- (line-number-at-pos if-name-main-end)
2771 (line-number-at-pos if-name-main-start)) ?\n)))
2748 (when if-name-main-start-end 2772 (when if-name-main-start-end
2749 (goto-char if-name-main-start) 2773 (goto-char if-name-main-start)
2750 (delete-region if-name-main-start if-name-main-end) 2774 (delete-region if-name-main-start if-name-main-end)
2751 (insert 2775 (insert fillstr))))
2752 (make-string 2776 ;; Ensure there's only one coding cookie in the generated string.
2753 (- (line-number-at-pos if-name-main-end) 2777 (goto-char (point-min))
2754 (line-number-at-pos if-name-main-start)) ?\n))))) 2778 (when (looking-at-p (python-rx coding-cookie))
2779 (forward-line 1)
2780 (when (looking-at-p (python-rx coding-cookie))
2781 (delete-region
2782 (line-beginning-position) (line-end-position))))
2755 (buffer-substring-no-properties (point-min) (point-max))))) 2783 (buffer-substring-no-properties (point-min) (point-max)))))
2756 2784
2757(defun python-shell-send-region (start end &optional nomain) 2785(defun python-shell-send-region (start end &optional send-main)
2758 "Send the region delimited by START and END to inferior Python process." 2786 "Send the region delimited by START and END to inferior Python process.
2759 (interactive "r") 2787When optional argument SEND-MAIN is non-nil, allow execution of
2760 (let* ((string (python-shell-buffer-substring start end nomain)) 2788code inside blocks delimited by \"if __name__== '__main__':\".
2789When called interactively SEND-MAIN defaults to nil, unless it's
2790called with prefix argument."
2791 (interactive "r\nP")
2792 (let* ((string (python-shell-buffer-substring start end (not send-main)))
2761 (process (python-shell-get-or-create-process)) 2793 (process (python-shell-get-or-create-process))
2762 (_ (string-match "\\`\n*\\(.*\\)" string))) 2794 (original-string (buffer-substring-no-properties start end))
2763 (message "Sent: %s..." (match-string 1 string)) 2795 (_ (string-match "\\`\n*\\(.*\\)" original-string)))
2796 (message "Sent: %s..." (match-string 1 original-string))
2764 (python-shell-send-string string process))) 2797 (python-shell-send-string string process)))
2765 2798
2766(defun python-shell-send-buffer (&optional arg) 2799(defun python-shell-send-buffer (&optional send-main)
2767 "Send the entire buffer to inferior Python process. 2800 "Send the entire buffer to inferior Python process.
2768With prefix ARG allow execution of code inside blocks delimited 2801When optional argument SEND-MAIN is non-nil, allow execution of
2769by \"if __name__== '__main__':\"." 2802code inside blocks delimited by \"if __name__== '__main__':\".
2803When called interactively SEND-MAIN defaults to nil, unless it's
2804called with prefix argument."
2770 (interactive "P") 2805 (interactive "P")
2771 (save-restriction 2806 (save-restriction
2772 (widen) 2807 (widen)
2773 (python-shell-send-region (point-min) (point-max) (not arg)))) 2808 (python-shell-send-region (point-min) (point-max) send-main)))
2774 2809
2775(defun python-shell-send-defun (arg) 2810(defun python-shell-send-defun (arg)
2776 "Send the current defun to inferior Python process. 2811 "Send the current defun to inferior Python process.
@@ -2797,30 +2832,33 @@ When argument ARG is non-nil do not include decorators."
2797 delete) 2832 delete)
2798 "Send FILE-NAME to inferior Python PROCESS. 2833 "Send FILE-NAME to inferior Python PROCESS.
2799If TEMP-FILE-NAME is passed then that file is used for processing 2834If TEMP-FILE-NAME is passed then that file is used for processing
2800instead, while internally the shell will continue to use FILE-NAME. 2835instead, while internally the shell will continue to use
2801If DELETE is non-nil, delete the file afterwards." 2836FILE-NAME. If TEMP-FILE-NAME and DELETE are non-nil, then
2837TEMP-FILE-NAME is deleted after evaluation is performed."
2802 (interactive "fFile to send: ") 2838 (interactive "fFile to send: ")
2803 (let* ((process (or process (python-shell-get-or-create-process))) 2839 (let* ((process (or process (python-shell-get-or-create-process)))
2840 (encoding (with-temp-buffer
2841 (insert-file-contents
2842 (or temp-file-name file-name))
2843 (python-info-encoding)))
2844 (file-name (expand-file-name
2845 (or (file-remote-p file-name 'localname)
2846 file-name)))
2804 (temp-file-name (when temp-file-name 2847 (temp-file-name (when temp-file-name
2805 (expand-file-name 2848 (expand-file-name
2806 (or (file-remote-p temp-file-name 'localname) 2849 (or (file-remote-p temp-file-name 'localname)
2807 temp-file-name)))) 2850 temp-file-name)))))
2808 (file-name (or (when file-name
2809 (expand-file-name
2810 (or (file-remote-p file-name 'localname)
2811 file-name)))
2812 temp-file-name)))
2813 (when (not file-name)
2814 (error "If FILE-NAME is nil then TEMP-FILE-NAME must be non-nil"))
2815 (python-shell-send-string 2851 (python-shell-send-string
2816 (format 2852 (format
2817 (concat "__pyfile = open('''%s''');" 2853 (concat
2818 "exec(compile(__pyfile.read(), '''%s''', 'exec'));" 2854 "import codecs, os;"
2819 "__pyfile.close()%s") 2855 "__pyfile = codecs.open('''%s''', encoding='''%s''');"
2820 (or temp-file-name file-name) file-name 2856 "__code = __pyfile.read().encode('''%s''');"
2821 (if delete (format "; import os; os.remove('''%s''')" 2857 "__pyfile.close();"
2822 (or temp-file-name file-name)) 2858 (when (and delete temp-file-name)
2823 "")) 2859 (format "os.remove('''%s''');" temp-file-name))
2860 "exec(compile(__code, '''%s''', 'exec'));")
2861 (or temp-file-name file-name) encoding encoding file-name)
2824 process))) 2862 process)))
2825 2863
2826(defun python-shell-switch-to-shell () 2864(defun python-shell-switch-to-shell ()
@@ -4121,6 +4159,32 @@ operator."
4121 (* whitespace) line-end)) 4159 (* whitespace) line-end))
4122 (string-equal "" (match-string-no-properties 1)))) 4160 (string-equal "" (match-string-no-properties 1))))
4123 4161
4162(defun python-info-encoding-from-cookie ()
4163 "Detect current buffer's encoding from its coding cookie.
4164Returns the enconding as a symbol."
4165 (let ((first-two-lines
4166 (save-excursion
4167 (save-restriction
4168 (widen)
4169 (goto-char (point-min))
4170 (forward-line 2)
4171 (buffer-substring-no-properties
4172 (point)
4173 (point-min))))))
4174 (when (string-match (python-rx coding-cookie) first-two-lines)
4175 (intern (match-string-no-properties 1 first-two-lines)))))
4176
4177(defun python-info-encoding ()
4178 "Return encoding for file.
4179Try `python-info-encoding-from-cookie', if none is found then
4180default to utf-8."
4181 ;; If no enconding is defined, then it's safe to use UTF-8: Python 2
4182 ;; uses ASCII as default while Python 3 uses UTF-8. This means that
4183 ;; in the worst case escenario python.el will make things work for
4184 ;; Python 2 files with unicode data and no encoding defined.
4185 (or (python-info-encoding-from-cookie)
4186 'utf-8))
4187
4124 4188
4125;;; Utility functions 4189;;; Utility functions
4126 4190