aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorJan D2015-04-26 13:55:01 +0200
committerJan D2015-04-26 13:55:01 +0200
commitf92ac2e82ed199d6f25d2a59508e08addb1150ac (patch)
treed7d7756e3dbce10d8f73c27815d815499f78c2bd /lisp/progmodes/python.el
parent5a094119ce79723108abd90a1fcc33721e964823 (diff)
parenta40869789fc5502e3d4e393b7c31d78cb7f29aa1 (diff)
downloademacs-f92ac2e82ed199d6f25d2a59508e08addb1150ac.tar.gz
emacs-f92ac2e82ed199d6f25d2a59508e08addb1150ac.zip
Merge branch 'master' into cairo
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el367
1 files changed, 252 insertions, 115 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 67b44aa1bbe..eb17d075921 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -482,19 +482,10 @@ The type returned can be `comment', `string' or `paren'."
482 'python-info-ppss-comment-or-string-p 482 'python-info-ppss-comment-or-string-p
483 #'python-syntax-comment-or-string-p "24.3") 483 #'python-syntax-comment-or-string-p "24.3")
484 484
485(defun python-docstring-at-p (pos)
486 "Check to see if there is a docstring at POS."
487 (save-excursion
488 (goto-char pos)
489 (if (looking-at-p "'''\\|\"\"\"")
490 (progn
491 (python-nav-backward-statement)
492 (looking-at "\\`\\|class \\|def "))
493 nil)))
494
495(defun python-font-lock-syntactic-face-function (state) 485(defun python-font-lock-syntactic-face-function (state)
486 "Return syntactic face given STATE."
496 (if (nth 3 state) 487 (if (nth 3 state)
497 (if (python-docstring-at-p (nth 8 state)) 488 (if (python-info-docstring-p state)
498 font-lock-doc-face 489 font-lock-doc-face
499 font-lock-string-face) 490 font-lock-string-face)
500 font-lock-comment-face)) 491 font-lock-comment-face))
@@ -853,7 +844,9 @@ keyword
853 ;; Inside a string. 844 ;; Inside a string.
854 ((let ((start (python-syntax-context 'string ppss))) 845 ((let ((start (python-syntax-context 'string ppss)))
855 (when start 846 (when start
856 (cons :inside-string start)))) 847 (cons (if (python-info-docstring-p)
848 :inside-docstring
849 :inside-string) start))))
857 ;; Inside a paren. 850 ;; Inside a paren.
858 ((let* ((start (python-syntax-context 'paren ppss)) 851 ((let* ((start (python-syntax-context 'paren ppss))
859 (starts-in-newline 852 (starts-in-newline
@@ -998,6 +991,12 @@ possibilities can be narrowed to specific indentation points."
998 ;; Copy previous indentation. 991 ;; Copy previous indentation.
999 (goto-char start) 992 (goto-char start)
1000 (current-indentation)) 993 (current-indentation))
994 (`(:inside-docstring . ,start)
995 (let* ((line-indentation (current-indentation))
996 (base-indent (progn
997 (goto-char start)
998 (current-indentation))))
999 (max line-indentation base-indent)))
1001 (`(,(or :after-block-start 1000 (`(,(or :after-block-start
1002 :after-backslash-first-line 1001 :after-backslash-first-line
1003 :inside-paren-newline-start) . ,start) 1002 :inside-paren-newline-start) . ,start)
@@ -1147,14 +1146,15 @@ Called from a program, START and END specify the region to indent."
1147 (not line-is-comment-p)) 1146 (not line-is-comment-p))
1148 (python-info-current-line-empty-p))))) 1147 (python-info-current-line-empty-p)))))
1149 ;; Don't mess with strings, unless it's the 1148 ;; Don't mess with strings, unless it's the
1150 ;; enclosing set of quotes. 1149 ;; enclosing set of quotes or a docstring.
1151 (or (not (python-syntax-context 'string)) 1150 (or (not (python-syntax-context 'string))
1152 (eq 1151 (eq
1153 (syntax-after 1152 (syntax-after
1154 (+ (1- (point)) 1153 (+ (1- (point))
1155 (current-indentation) 1154 (current-indentation)
1156 (python-syntax-count-quotes (char-after) (point)))) 1155 (python-syntax-count-quotes (char-after) (point))))
1157 (string-to-syntax "|"))) 1156 (string-to-syntax "|"))
1157 (python-info-docstring-p))
1158 ;; Skip if current line is a block start, a 1158 ;; Skip if current line is a block start, a
1159 ;; dedenter or block ender. 1159 ;; dedenter or block ender.
1160 (save-excursion 1160 (save-excursion
@@ -1580,11 +1580,13 @@ forward only one sexp, else move backwards."
1580 (while (and (funcall search-fn paren-regexp nil t) 1580 (while (and (funcall search-fn paren-regexp nil t)
1581 (python-syntax-context 'paren))))))) 1581 (python-syntax-context 'paren)))))))
1582 1582
1583(defun python-nav--forward-sexp (&optional dir safe) 1583(defun python-nav--forward-sexp (&optional dir safe skip-parens-p)
1584 "Move to forward sexp. 1584 "Move to forward sexp.
1585With positive optional argument DIR direction move forward, else 1585With positive optional argument DIR direction move forward, else
1586backwards. When optional argument SAFE is non-nil do not throw 1586backwards. When optional argument SAFE is non-nil do not throw
1587errors when at end of sexp, skip it instead." 1587errors when at end of sexp, skip it instead. With optional
1588argument SKIP-PARENS-P force sexp motion to ignore parenthesized
1589expressions when looking at them in either direction."
1588 (setq dir (or dir 1)) 1590 (setq dir (or dir 1))
1589 (unless (= dir 0) 1591 (unless (= dir 0)
1590 (let* ((forward-p (if (> dir 0) 1592 (let* ((forward-p (if (> dir 0)
@@ -1596,11 +1598,13 @@ errors when at end of sexp, skip it instead."
1596 ;; Inside of a string, get out of it. 1598 ;; Inside of a string, get out of it.
1597 (let ((forward-sexp-function)) 1599 (let ((forward-sexp-function))
1598 (forward-sexp dir))) 1600 (forward-sexp dir)))
1599 ((or (eq context-type 'paren) 1601 ((and (not skip-parens-p)
1600 (and forward-p (looking-at (python-rx open-paren))) 1602 (or (eq context-type 'paren)
1601 (and (not forward-p) 1603 (if forward-p
1602 (eq (syntax-class (syntax-after (1- (point)))) 1604 (eq (syntax-class (syntax-after (point)))
1603 (car (string-to-syntax ")"))))) 1605 (car (string-to-syntax "(")))
1606 (eq (syntax-class (syntax-after (1- (point))))
1607 (car (string-to-syntax ")"))))))
1604 ;; Inside a paren or looking at it, lisp knows what to do. 1608 ;; Inside a paren or looking at it, lisp knows what to do.
1605 (if safe 1609 (if safe
1606 (python-nav--lisp-forward-sexp-safe dir) 1610 (python-nav--lisp-forward-sexp-safe dir)
@@ -1636,7 +1640,7 @@ errors when at end of sexp, skip it instead."
1636 (cond ((and (not (eobp)) 1640 (cond ((and (not (eobp))
1637 (python-info-current-line-empty-p)) 1641 (python-info-current-line-empty-p))
1638 (python-util-forward-comment dir) 1642 (python-util-forward-comment dir)
1639 (python-nav--forward-sexp dir)) 1643 (python-nav--forward-sexp dir safe skip-parens-p))
1640 ((eq context 'block-start) 1644 ((eq context 'block-start)
1641 (python-nav-end-of-block)) 1645 (python-nav-end-of-block))
1642 ((eq context 'statement-start) 1646 ((eq context 'statement-start)
@@ -1656,7 +1660,7 @@ errors when at end of sexp, skip it instead."
1656 (cond ((and (not (bobp)) 1660 (cond ((and (not (bobp))
1657 (python-info-current-line-empty-p)) 1661 (python-info-current-line-empty-p))
1658 (python-util-forward-comment dir) 1662 (python-util-forward-comment dir)
1659 (python-nav--forward-sexp dir)) 1663 (python-nav--forward-sexp dir safe skip-parens-p))
1660 ((eq context 'block-end) 1664 ((eq context 'block-end)
1661 (python-nav-beginning-of-block)) 1665 (python-nav-beginning-of-block))
1662 ((eq context 'statement-end) 1666 ((eq context 'statement-end)
@@ -1674,47 +1678,69 @@ errors when at end of sexp, skip it instead."
1674 (python-nav-beginning-of-statement)) 1678 (python-nav-beginning-of-statement))
1675 (t (goto-char next-sexp-pos)))))))))) 1679 (t (goto-char next-sexp-pos))))))))))
1676 1680
1677(defun python-nav-forward-sexp (&optional arg) 1681(defun python-nav-forward-sexp (&optional arg safe skip-parens-p)
1678 "Move forward across expressions. 1682 "Move forward across expressions.
1679With ARG, do it that many times. Negative arg -N means move 1683With ARG, do it that many times. Negative arg -N means move
1680backward N times." 1684backward N times. When optional argument SAFE is non-nil do not
1685throw errors when at end of sexp, skip it instead. With optional
1686argument SKIP-PARENS-P force sexp motion to ignore parenthesized
1687expressions when looking at them in either direction (forced to t
1688in interactive calls)."
1681 (interactive "^p") 1689 (interactive "^p")
1682 (or arg (setq arg 1)) 1690 (or arg (setq arg 1))
1691 ;; Do not follow parens on interactive calls. This hack to detect
1692 ;; if the function was called interactively copes with the way
1693 ;; `forward-sexp' works by calling `forward-sexp-function', losing
1694 ;; interactive detection by checking `current-prefix-arg'. The
1695 ;; reason to make this distinction is that lisp functions like
1696 ;; `blink-matching-open' get confused causing issues like the one in
1697 ;; Bug#16191. With this approach the user gets a symmetric behavior
1698 ;; when working interactively while called functions expecting
1699 ;; paren-based sexp motion work just fine.
1700 (or
1701 skip-parens-p
1702 (setq skip-parens-p
1703 (memq real-this-command
1704 (list
1705 #'forward-sexp #'backward-sexp
1706 #'python-nav-forward-sexp #'python-nav-backward-sexp
1707 #'python-nav-forward-sexp-safe #'python-nav-backward-sexp))))
1683 (while (> arg 0) 1708 (while (> arg 0)
1684 (python-nav--forward-sexp 1) 1709 (python-nav--forward-sexp 1 safe skip-parens-p)
1685 (setq arg (1- arg))) 1710 (setq arg (1- arg)))
1686 (while (< arg 0) 1711 (while (< arg 0)
1687 (python-nav--forward-sexp -1) 1712 (python-nav--forward-sexp -1 safe skip-parens-p)
1688 (setq arg (1+ arg)))) 1713 (setq arg (1+ arg))))
1689 1714
1690(defun python-nav-backward-sexp (&optional arg) 1715(defun python-nav-backward-sexp (&optional arg safe skip-parens-p)
1691 "Move backward across expressions. 1716 "Move backward across expressions.
1692With ARG, do it that many times. Negative arg -N means move 1717With ARG, do it that many times. Negative arg -N means move
1693forward N times." 1718forward N times. When optional argument SAFE is non-nil do not
1719throw errors when at end of sexp, skip it instead. With optional
1720argument SKIP-PARENS-P force sexp motion to ignore parenthesized
1721expressions when looking at them in either direction (forced to t
1722in interactive calls)."
1694 (interactive "^p") 1723 (interactive "^p")
1695 (or arg (setq arg 1)) 1724 (or arg (setq arg 1))
1696 (python-nav-forward-sexp (- arg))) 1725 (python-nav-forward-sexp (- arg) safe skip-parens-p))
1697 1726
1698(defun python-nav-forward-sexp-safe (&optional arg) 1727(defun python-nav-forward-sexp-safe (&optional arg skip-parens-p)
1699 "Move forward safely across expressions. 1728 "Move forward safely across expressions.
1700With ARG, do it that many times. Negative arg -N means move 1729With ARG, do it that many times. Negative arg -N means move
1701backward N times." 1730backward N times. With optional argument SKIP-PARENS-P force
1731sexp motion to ignore parenthesized expressions when looking at
1732them in either direction (forced to t in interactive calls)."
1702 (interactive "^p") 1733 (interactive "^p")
1703 (or arg (setq arg 1)) 1734 (python-nav-forward-sexp arg t skip-parens-p))
1704 (while (> arg 0)
1705 (python-nav--forward-sexp 1 t)
1706 (setq arg (1- arg)))
1707 (while (< arg 0)
1708 (python-nav--forward-sexp -1 t)
1709 (setq arg (1+ arg))))
1710 1735
1711(defun python-nav-backward-sexp-safe (&optional arg) 1736(defun python-nav-backward-sexp-safe (&optional arg skip-parens-p)
1712 "Move backward safely across expressions. 1737 "Move backward safely across expressions.
1713With ARG, do it that many times. Negative arg -N means move 1738With ARG, do it that many times. Negative arg -N means move
1714forward N times." 1739forward N times. With optional argument SKIP-PARENS-P force sexp
1740motion to ignore parenthesized expressions when looking at them in
1741either direction (forced to t in interactive calls)."
1715 (interactive "^p") 1742 (interactive "^p")
1716 (or arg (setq arg 1)) 1743 (python-nav-backward-sexp arg t skip-parens-p))
1717 (python-nav-forward-sexp-safe (- arg)))
1718 1744
1719(defun python-nav--up-list (&optional dir) 1745(defun python-nav--up-list (&optional dir)
1720 "Internal implementation of `python-nav-up-list'. 1746 "Internal implementation of `python-nav-up-list'.
@@ -2971,25 +2997,25 @@ This function takes the list of setup code to send from the
2971 2997
2972(defcustom python-shell-completion-setup-code 2998(defcustom python-shell-completion-setup-code
2973 "try: 2999 "try:
2974 import __builtin__ 3000 import readline
2975except ImportError:
2976 # Python 3
2977 import builtins as __builtin__
2978try:
2979 import readline, rlcompleter
2980except: 3001except:
2981 def __PYTHON_EL_get_completions(text): 3002 def __PYTHON_EL_get_completions(text):
2982 return [] 3003 return []
2983else: 3004else:
2984 def __PYTHON_EL_get_completions(text): 3005 def __PYTHON_EL_get_completions(text):
3006 try:
3007 import __builtin__
3008 except ImportError:
3009 # Python 3
3010 import builtins as __builtin__
2985 builtins = dir(__builtin__) 3011 builtins = dir(__builtin__)
2986 completions = [] 3012 completions = []
3013 is_ipython = ('__IPYTHON__' in builtins or
3014 '__IPYTHON__active' in builtins)
3015 splits = text.split()
3016 is_module = splits and splits[0] in ('from', 'import')
2987 try: 3017 try:
2988 splits = text.split() 3018 if is_ipython and is_module:
2989 is_module = splits and splits[0] in ('from', 'import')
2990 is_ipython = ('__IPYTHON__' in builtins or
2991 '__IPYTHON__active' in builtins)
2992 if is_module:
2993 from IPython.core.completerlib import module_completion 3019 from IPython.core.completerlib import module_completion
2994 completions = module_completion(text.strip()) 3020 completions = module_completion(text.strip())
2995 elif is_ipython and '__IP' in builtins: 3021 elif is_ipython and '__IP' in builtins:
@@ -2997,13 +3023,20 @@ else:
2997 elif is_ipython and 'get_ipython' in builtins: 3023 elif is_ipython and 'get_ipython' in builtins:
2998 completions = get_ipython().Completer.all_completions(text) 3024 completions = get_ipython().Completer.all_completions(text)
2999 else: 3025 else:
3026 # Try to reuse current completer.
3027 completer = readline.get_completer()
3028 if not completer:
3029 # importing rlcompleter sets the completer, use it as a
3030 # last resort to avoid breaking customizations.
3031 import rlcompleter
3032 completer = readline.get_completer()
3000 i = 0 3033 i = 0
3001 while True: 3034 while True:
3002 res = readline.get_completer()(text, i) 3035 completion = completer(text, i)
3003 if not res: 3036 if not completion:
3004 break 3037 break
3005 i += 1 3038 i += 1
3006 completions.append(res) 3039 completions.append(completion)
3007 except: 3040 except:
3008 pass 3041 pass
3009 return completions" 3042 return completions"
@@ -3042,10 +3075,14 @@ When a match is found, native completion is disabled."
3042 "Enable readline based native completion." 3075 "Enable readline based native completion."
3043 :type 'boolean) 3076 :type 'boolean)
3044 3077
3045(defcustom python-shell-completion-native-output-timeout 0.01 3078(defcustom python-shell-completion-native-output-timeout 5.0
3046 "Time in seconds to wait for completion output before giving up." 3079 "Time in seconds to wait for completion output before giving up."
3047 :type 'float) 3080 :type 'float)
3048 3081
3082(defcustom python-shell-completion-native-try-output-timeout 1.0
3083 "Time in seconds to wait for *trying* native completion output."
3084 :type 'float)
3085
3049(defvar python-shell-completion-native-redirect-buffer 3086(defvar python-shell-completion-native-redirect-buffer
3050 " *Python completions redirect*" 3087 " *Python completions redirect*"
3051 "Buffer to be used to redirect output of readline commands.") 3088 "Buffer to be used to redirect output of readline commands.")
@@ -3059,7 +3096,9 @@ When a match is found, native completion is disabled."
3059 3096
3060(defun python-shell-completion-native-try () 3097(defun python-shell-completion-native-try ()
3061 "Return non-nil if can trigger native completion." 3098 "Return non-nil if can trigger native completion."
3062 (let ((python-shell-completion-native-enable t)) 3099 (let ((python-shell-completion-native-enable t)
3100 (python-shell-completion-native-output-timeout
3101 python-shell-completion-native-try-output-timeout))
3063 (python-shell-completion-native-get-completions 3102 (python-shell-completion-native-get-completions
3064 (get-buffer-process (current-buffer)) 3103 (get-buffer-process (current-buffer))
3065 nil "int"))) 3104 nil "int")))
@@ -3067,27 +3106,73 @@ When a match is found, native completion is disabled."
3067(defun python-shell-completion-native-setup () 3106(defun python-shell-completion-native-setup ()
3068 "Try to setup native completion, return non-nil on success." 3107 "Try to setup native completion, return non-nil on success."
3069 (let ((process (python-shell-get-process))) 3108 (let ((process (python-shell-get-process)))
3070 (python-shell-send-string 3109 (python-shell-send-string "
3071 (funcall 3110def __PYTHON_EL_native_completion_setup():
3072 'mapconcat 3111 try:
3073 #'identity 3112 import readline
3074 (list 3113 try:
3075 "try:" 3114 import __builtin__
3076 " import readline, rlcompleter" 3115 except ImportError:
3077 ;; Remove parens on callables as it breaks completion on 3116 # Python 3
3078 ;; arguments (e.g. str(Ari<tab>)). 3117 import builtins as __builtin__
3079 " class Completer(rlcompleter.Completer):" 3118 builtins = dir(__builtin__)
3080 " def _callable_postfix(self, val, word):" 3119 is_ipython = ('__IPYTHON__' in builtins or
3081 " return word" 3120 '__IPYTHON__active' in builtins)
3082 " readline.set_completer(Completer().complete)" 3121 class __PYTHON_EL_Completer:
3083 " if readline.__doc__ and 'libedit' in readline.__doc__:" 3122 PYTHON_EL_WRAPPED = True
3084 " readline.parse_and_bind('bind ^I rl_complete')" 3123 def __init__(self, completer):
3085 " else:" 3124 self.completer = completer
3086 " readline.parse_and_bind('tab: complete')" 3125 self.last_completion = None
3087 " print ('python.el: readline is available')" 3126 def __call__(self, text, state):
3088 "except:" 3127 if state == 0:
3089 " print ('python.el: readline not available')") 3128 # The first completion is always a dummy completion. This
3090 "\n") 3129 # ensures proper output for sole completions and a current
3130 # input safeguard when no completions are available.
3131 self.last_completion = None
3132 completion = '0__dummy_completion__'
3133 else:
3134 completion = self.completer(text, state - 1)
3135 if not completion:
3136 if state == 1:
3137 # When no completions are available, two non-sharing
3138 # prefix strings are returned just to ensure output
3139 # while preventing changes to current input.
3140 completion = '1__dummy_completion__'
3141 elif self.last_completion != '~~~~__dummy_completion__':
3142 # This marks the end of output.
3143 completion = '~~~~__dummy_completion__'
3144 elif completion.endswith('('):
3145 # Remove parens on callables as it breaks completion on
3146 # arguments (e.g. str(Ari<tab>)).
3147 completion = completion[:-1]
3148 self.last_completion = completion
3149 return completion
3150 completer = readline.get_completer()
3151 if not completer:
3152 # Used as last resort to avoid breaking customizations.
3153 import rlcompleter
3154 completer = readline.get_completer()
3155 if completer and not getattr(completer, 'PYTHON_EL_WRAPPED', False):
3156 # Wrap the existing completer function only once.
3157 new_completer = __PYTHON_EL_Completer(completer)
3158 if not is_ipython:
3159 readline.set_completer(new_completer)
3160 else:
3161 # IPython hacks readline such that `readline.set_completer`
3162 # won't work. This workaround injects the new completer
3163 # function into the existing instance directly.
3164 instance = getattr(completer, 'im_self', completer.__self__)
3165 instance.rlcomplete = new_completer
3166 if readline.__doc__ and 'libedit' in readline.__doc__:
3167 readline.parse_and_bind('bind ^I rl_complete')
3168 else:
3169 readline.parse_and_bind('tab: complete')
3170 # Require just one tab to send output.
3171 readline.parse_and_bind('set show-all-if-ambiguous on')
3172 print ('python.el: readline is available')
3173 except IOError:
3174 print ('python.el: readline not available')
3175__PYTHON_EL_native_completion_setup()"
3091 process) 3176 process)
3092 (python-shell-accept-process-output process) 3177 (python-shell-accept-process-output process)
3093 (when (save-excursion 3178 (when (save-excursion
@@ -3165,9 +3250,8 @@ completion."
3165 (original-filter-fn (process-filter process)) 3250 (original-filter-fn (process-filter process))
3166 (redirect-buffer (get-buffer-create 3251 (redirect-buffer (get-buffer-create
3167 python-shell-completion-native-redirect-buffer)) 3252 python-shell-completion-native-redirect-buffer))
3168 (separators (python-rx 3253 (separators (python-rx (or whitespace open-paren close-paren)))
3169 (or whitespace open-paren close-paren))) 3254 (trigger "\t")
3170 (trigger "\t\t\t")
3171 (new-input (concat input trigger)) 3255 (new-input (concat input trigger))
3172 (input-length 3256 (input-length
3173 (save-excursion 3257 (save-excursion
@@ -3189,7 +3273,8 @@ completion."
3189 (comint-redirect-insert-matching-regexp nil) 3273 (comint-redirect-insert-matching-regexp nil)
3190 ;; Feed it some regex that will never match. 3274 ;; Feed it some regex that will never match.
3191 (comint-redirect-finished-regexp "^\\'$") 3275 (comint-redirect-finished-regexp "^\\'$")
3192 (comint-redirect-output-buffer redirect-buffer)) 3276 (comint-redirect-output-buffer redirect-buffer)
3277 (current-time (float-time)))
3193 ;; Compatibility with Emacs 24.x. Comint changed and 3278 ;; Compatibility with Emacs 24.x. Comint changed and
3194 ;; now `comint-redirect-filter' gets 3 args. This 3279 ;; now `comint-redirect-filter' gets 3 args. This
3195 ;; checks which version of `comint-redirect-filter' is 3280 ;; checks which version of `comint-redirect-filter' is
@@ -3202,22 +3287,22 @@ completion."
3202 #'comint-redirect-filter original-filter-fn)) 3287 #'comint-redirect-filter original-filter-fn))
3203 (set-process-filter process #'comint-redirect-filter)) 3288 (set-process-filter process #'comint-redirect-filter))
3204 (process-send-string process input-to-send) 3289 (process-send-string process input-to-send)
3205 (accept-process-output 3290 ;; Grab output until our dummy completion used as
3206 process 3291 ;; output end marker is found. Output is accepted
3207 python-shell-completion-native-output-timeout) 3292 ;; *very* quickly to keep the shell super-responsive.
3208 ;; XXX: can't use `python-shell-accept-process-output' 3293 (while (and (not (re-search-backward "~~~~__dummy_completion__" nil t))
3209 ;; here because there are no guarantees on how output 3294 (< (- current-time (float-time))
3210 ;; ends. The workaround here is to call 3295 python-shell-completion-native-output-timeout))
3211 ;; `accept-process-output' until we don't find anything 3296 (accept-process-output process 0.01))
3212 ;; else to accept.
3213 (while (accept-process-output
3214 process
3215 python-shell-completion-native-output-timeout))
3216 (cl-remove-duplicates 3297 (cl-remove-duplicates
3217 (split-string 3298 (cl-remove-if
3218 (buffer-substring-no-properties 3299 (lambda (c)
3219 (point-min) (point-max)) 3300 (string-match "__dummy_completion__" c))
3220 separators t)))) 3301 (split-string
3302 (buffer-substring-no-properties
3303 (point-min) (point-max))
3304 separators t))
3305 :test #'string=)))
3221 (set-process-filter process original-filter-fn)))))) 3306 (set-process-filter process original-filter-fn))))))
3222 3307
3223(defun python-shell-completion-get-completions (process import input) 3308(defun python-shell-completion-get-completions (process import input)
@@ -3587,17 +3672,12 @@ JUSTIFY should be used (if applicable) as in `fill-paragraph'."
3587 (`pep-257 (and multi-line-p (cons nil 2))) 3672 (`pep-257 (and multi-line-p (cons nil 2)))
3588 (`pep-257-nn (and multi-line-p (cons nil 1))) 3673 (`pep-257-nn (and multi-line-p (cons nil 1)))
3589 (`symmetric (and multi-line-p (cons 1 1))))) 3674 (`symmetric (and multi-line-p (cons 1 1)))))
3590 (docstring-p (save-excursion
3591 ;; Consider docstrings those strings which
3592 ;; start on a line by themselves.
3593 (python-nav-beginning-of-statement)
3594 (and (= (point) str-start-pos))))
3595 (fill-paragraph-function)) 3675 (fill-paragraph-function))
3596 (save-restriction 3676 (save-restriction
3597 (narrow-to-region str-start-pos str-end-pos) 3677 (narrow-to-region str-start-pos str-end-pos)
3598 (fill-paragraph justify)) 3678 (fill-paragraph justify))
3599 (save-excursion 3679 (save-excursion
3600 (when (and docstring-p python-fill-docstring-style) 3680 (when (and (python-info-docstring-p) python-fill-docstring-style)
3601 ;; Add the number of newlines indicated by the selected style 3681 ;; Add the number of newlines indicated by the selected style
3602 ;; at the start of the docstring. 3682 ;; at the start of the docstring.
3603 (goto-char (+ str-start-pos num-quotes)) 3683 (goto-char (+ str-start-pos num-quotes))
@@ -4423,23 +4503,40 @@ where the continued line ends."
4423 (when (looking-at (python-rx block-start)) 4503 (when (looking-at (python-rx block-start))
4424 (point-marker))))) 4504 (point-marker)))))
4425 4505
4506(defun python-info-assignment-statement-p (&optional current-line-only)
4507 "Check if current line is an assignment.
4508With argument CURRENT-LINE-ONLY is non-nil, don't follow any
4509continuations, just check the if current line is an assignment."
4510 (save-excursion
4511 (let ((found nil))
4512 (if current-line-only
4513 (back-to-indentation)
4514 (python-nav-beginning-of-statement))
4515 (while (and
4516 (re-search-forward (python-rx not-simple-operator
4517 assignment-operator
4518 (group not-simple-operator))
4519 (line-end-position) t)
4520 (not found))
4521 (save-excursion
4522 ;; The assignment operator should not be inside a string.
4523 (backward-char (length (match-string-no-properties 1)))
4524 (setq found (not (python-syntax-context-type)))))
4525 (when found
4526 (skip-syntax-forward " ")
4527 (point-marker)))))
4528
4529;; TODO: rename to clarify this is only for the first continuation
4530;; line or remove it and move its body to `python-indent-context'.
4426(defun python-info-assignment-continuation-line-p () 4531(defun python-info-assignment-continuation-line-p ()
4427 "Check if current line is a continuation of an assignment. 4532 "Check if current line is the first continuation of an assignment.
4428When current line is continuation of another with an assignment 4533When current line is continuation of another with an assignment
4429return the point of the first non-blank character after the 4534return the point of the first non-blank character after the
4430operator." 4535operator."
4431 (save-excursion 4536 (save-excursion
4432 (when (python-info-continuation-line-p) 4537 (when (python-info-continuation-line-p)
4433 (forward-line -1) 4538 (forward-line -1)
4434 (back-to-indentation) 4539 (python-info-assignment-statement-p t))))
4435 (when (and (not (looking-at (python-rx block-start)))
4436 (and (re-search-forward (python-rx not-simple-operator
4437 assignment-operator
4438 not-simple-operator)
4439 (line-end-position) t)
4440 (not (python-syntax-context-type))))
4441 (skip-syntax-forward "\s")
4442 (point-marker)))))
4443 4540
4444(defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss) 4541(defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss)
4445 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS." 4542 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS."
@@ -4464,6 +4561,46 @@ operator."
4464 (* whitespace) line-end)) 4561 (* whitespace) line-end))
4465 (string-equal "" (match-string-no-properties 1)))) 4562 (string-equal "" (match-string-no-properties 1))))
4466 4563
4564(defun python-info-docstring-p (&optional syntax-ppss)
4565 "Return non-nil if point is in a docstring.
4566When optional argument SYNTAX-PPSS is given, use that instead of
4567point's current `syntax-ppss'."
4568 ;;; https://www.python.org/dev/peps/pep-0257/#what-is-a-docstring
4569 (save-excursion
4570 (when (and syntax-ppss (python-syntax-context 'string syntax-ppss))
4571 (goto-char (nth 8 syntax-ppss)))
4572 (python-nav-beginning-of-statement)
4573 (let ((counter 1)
4574 (indentation (current-indentation))
4575 (backward-sexp-point)
4576 (re (concat "[uU]?[rR]?"
4577 (python-rx string-delimiter))))
4578 (when (and
4579 (not (python-info-assignment-statement-p))
4580 (looking-at-p re)
4581 ;; Allow up to two consecutive docstrings only.
4582 (>=
4583 2
4584 (progn
4585 (while (save-excursion
4586 (python-nav-backward-sexp)
4587 (setq backward-sexp-point (point))
4588 (and (= indentation (current-indentation))
4589 (not (bobp)) ; Prevent infloop.
4590 (looking-at-p
4591 (concat "[uU]?[rR]?"
4592 (python-rx string-delimiter)))))
4593 ;; Previous sexp was a string, restore point.
4594 (goto-char backward-sexp-point)
4595 (cl-incf counter))
4596 counter)))
4597 (python-util-forward-comment -1)
4598 (python-nav-beginning-of-statement)
4599 (cond ((bobp))
4600 ((python-info-assignment-statement-p) t)
4601 ((python-info-looking-at-beginning-of-defun))
4602 (t nil))))))
4603
4467(defun python-info-encoding-from-cookie () 4604(defun python-info-encoding-from-cookie ()
4468 "Detect current buffer's encoding from its coding cookie. 4605 "Detect current buffer's encoding from its coding cookie.
4469Returns the encoding as a symbol." 4606Returns the encoding as a symbol."