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.el176
1 files changed, 128 insertions, 48 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index f5e4bffd598..b917d3f6429 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -2267,32 +2267,82 @@ inferior python process is updated properly."
2267This is the function used by `python-fill-paragraph-function' to 2267This is the function used by `python-fill-paragraph-function' to
2268fill comments." 2268fill comments."
2269 :type 'symbol 2269 :type 'symbol
2270 :group 'python 2270 :group 'python)
2271 :safe 'symbolp)
2272 2271
2273(defcustom python-fill-string-function 'python-fill-string 2272(defcustom python-fill-string-function 'python-fill-string
2274 "Function to fill strings. 2273 "Function to fill strings.
2275This is the function used by `python-fill-paragraph-function' to 2274This is the function used by `python-fill-paragraph-function' to
2276fill strings." 2275fill strings."
2277 :type 'symbol 2276 :type 'symbol
2278 :group 'python 2277 :group 'python)
2279 :safe 'symbolp)
2280 2278
2281(defcustom python-fill-decorator-function 'python-fill-decorator 2279(defcustom python-fill-decorator-function 'python-fill-decorator
2282 "Function to fill decorators. 2280 "Function to fill decorators.
2283This is the function used by `python-fill-paragraph-function' to 2281This is the function used by `python-fill-paragraph-function' to
2284fill decorators." 2282fill decorators."
2285 :type 'symbol 2283 :type 'symbol
2286 :group 'python 2284 :group 'python)
2287 :safe 'symbolp)
2288 2285
2289(defcustom python-fill-paren-function 'python-fill-paren 2286(defcustom python-fill-paren-function 'python-fill-paren
2290 "Function to fill parens. 2287 "Function to fill parens.
2291This is the function used by `python-fill-paragraph-function' to 2288This is the function used by `python-fill-paragraph-function' to
2292fill parens." 2289fill parens."
2293 :type 'symbol 2290 :type 'symbol
2291 :group 'python)
2292
2293(defcustom python-fill-string-style 'pep-257
2294 "Style used to fill docstrings.
2295This affects `python-fill-string' behavior with regards to
2296triple quotes positioning.
2297
2298Possible values are DJANGO, PEP-257, PEP-257-NN, SYMMETRIC and
2299NIL. A value of NIL won't care about quotes position, will do
2300what `fill-paragraph' does, any other value may result in one of
2301the following docstring styles:
2302
2303DJANGO:
2304
2305 \"\"\"
2306 Process foo, return bar.
2307 \"\"\"
2308
2309 \"\"\"
2310 Process foo, return bar.
2311
2312 If processing fails throw ProcessingError.
2313 \"\"\"
2314
2315PEP-257:
2316
2317 \"\"\"Process foo, return bar.\"\"\"
2318
2319 \"\"\"Process foo, return bar.
2320
2321 If processing fails throw ProcessingError.
2322
2323 \"\"\"
2324
2325PEP-257-NN:
2326
2327 \"\"\"Process foo, return bar.\"\"\"
2328
2329 \"\"\"Process foo, return bar.
2330
2331 If processing fails throw ProcessingError.
2332 \"\"\"
2333
2334SYMMETRIC:
2335
2336 \"\"\"Process foo, return bar.\"\"\"
2337
2338 \"\"\"
2339 Process foo, return bar.
2340
2341 If processing fails throw ProcessingError.
2342 \"\"\""
2343 :type 'symbol
2294 :group 'python 2344 :group 'python
2295 :safe 'symbolp) 2345 :safe (lambda (val) (memq val '(django pep-257 pep-257-nn symmetric nil))))
2296 2346
2297(defun python-fill-paragraph-function (&optional justify) 2347(defun python-fill-paragraph-function (&optional justify)
2298 "`fill-paragraph-function' handling multi-line strings and possibly comments. 2348 "`fill-paragraph-function' handling multi-line strings and possibly comments.
@@ -2302,18 +2352,19 @@ the string's indentation.
2302Optional argument JUSTIFY defines if the paragraph should be justified." 2352Optional argument JUSTIFY defines if the paragraph should be justified."
2303 (interactive "P") 2353 (interactive "P")
2304 (save-excursion 2354 (save-excursion
2305 (back-to-indentation)
2306 (cond 2355 (cond
2307 ;; Comments 2356 ;; Comments
2308 ((funcall python-fill-comment-function justify)) 2357 ((python-syntax-context 'comment)
2358 (funcall python-fill-comment-function justify))
2309 ;; Strings/Docstrings 2359 ;; Strings/Docstrings
2310 ((save-excursion (skip-chars-forward "\"'uUrR") 2360 ((save-excursion (or (python-syntax-context 'string)
2311 (python-syntax-context 'string)) 2361 (equal (string-to-syntax "|")
2362 (syntax-after (point)))))
2312 (funcall python-fill-string-function justify)) 2363 (funcall python-fill-string-function justify))
2313 ;; Decorators 2364 ;; Decorators
2314 ((equal (char-after (save-excursion 2365 ((equal (char-after (save-excursion
2315 (back-to-indentation) 2366 (back-to-indentation)
2316 (point-marker))) ?@) 2367 (point))) ?@)
2317 (funcall python-fill-decorator-function justify)) 2368 (funcall python-fill-decorator-function justify))
2318 ;; Parens 2369 ;; Parens
2319 ((or (python-syntax-context 'paren) 2370 ((or (python-syntax-context 'paren)
@@ -2332,43 +2383,72 @@ JUSTIFY should be used (if applicable) as in `fill-paragraph'."
2332(defun python-fill-string (&optional justify) 2383(defun python-fill-string (&optional justify)
2333 "String fill function for `python-fill-paragraph-function'. 2384 "String fill function for `python-fill-paragraph-function'.
2334JUSTIFY should be used (if applicable) as in `fill-paragraph'." 2385JUSTIFY should be used (if applicable) as in `fill-paragraph'."
2335 (let ((marker (point-marker)) 2386 (let* ((marker (point-marker))
2336 (string-start-marker 2387 (str-start-pos
2337 (progn 2388 (let ((m (make-marker)))
2338 (skip-chars-forward "\"'uUrR") 2389 (setf (marker-position m)
2339 (goto-char (python-syntax-context 'string)) 2390 (or (python-syntax-context 'string)
2340 (skip-chars-forward "\"'uUrR") 2391 (and (equal (string-to-syntax "|")
2341 (point-marker))) 2392 (syntax-after (point)))
2342 (reg-start (line-beginning-position)) 2393 (point)))) m))
2343 (string-end-marker 2394 (num-quotes (python-syntax-count-quotes
2344 (progn 2395 (char-after str-start-pos) str-start-pos))
2345 (while (python-syntax-context 'string) 2396 (str-end-pos
2346 (goto-char (1+ (point-marker)))) 2397 (save-excursion
2347 (skip-chars-backward "\"'") 2398 (goto-char (+ str-start-pos num-quotes))
2348 (point-marker))) 2399 (or (re-search-forward (rx (syntax string-delimiter)) nil t)
2349 (reg-end (line-end-position)) 2400 (goto-char (point-max)))
2350 (fill-paragraph-function)) 2401 (point-marker)))
2402 (multi-line-p
2403 ;; Docstring styles may vary for oneliners and multi-liners.
2404 (> (count-matches "\n" str-start-pos str-end-pos) 0))
2405 (delimiters-style
2406 (case python-fill-string-style
2407 ;; delimiters-style is a cons cell with the form
2408 ;; (START-NEWLINES . END-NEWLINES). When any of the sexps
2409 ;; is NIL means to not add any newlines for start or end
2410 ;; of docstring. See `python-fill-string-style' for a
2411 ;; graphic idea of each style.
2412 (pep-257 (and multi-line-p (cons nil 2)))
2413 (pep-257-nn (and multi-line-p (cons nil 1)))
2414 (django (cons 1 1))
2415 (symmetric (and multi-line-p (cons 1 1)))))
2416 (docstring-p (save-excursion
2417 ;; Consider docstrings those strings which
2418 ;; start on a line by themselves.
2419 (goto-char str-start-pos)
2420 (skip-chars-backward (rx whitespace))
2421 (= (point) (line-beginning-position))))
2422 (fill-paragraph-function))
2351 (save-restriction 2423 (save-restriction
2352 (narrow-to-region reg-start reg-end) 2424 (narrow-to-region str-start-pos str-end-pos)
2353 (save-excursion 2425 (fill-paragraph justify))
2354 (goto-char string-start-marker) 2426 (save-excursion
2355 (delete-region (point-marker) (progn 2427 (when (and docstring-p python-fill-string-style)
2356 (skip-syntax-forward "> ") 2428 ;; Add the number of newlines indicated by the selected style
2357 (point-marker))) 2429 ;; at the start of the docstring.
2358 (goto-char string-end-marker) 2430 (goto-char (+ str-start-pos num-quotes))
2359 (delete-region (point-marker) (progn 2431 (delete-region (point) (progn
2360 (skip-syntax-backward "> ") 2432 (skip-syntax-forward "> ")
2361 (point-marker))) 2433 (point)))
2362 (save-excursion 2434 (and (car delimiters-style)
2363 (goto-char marker) 2435 (or (newline (car delimiters-style)) t)
2364 (fill-paragraph justify)) 2436 ;; Indent only if a newline is added.
2365 ;; If there is a newline in the docstring lets put triple 2437 (indent-according-to-mode))
2366 ;; quote in it's own line to follow pep 8 2438 ;; Add the number of newlines indicated by the selected style
2367 (when (save-excursion 2439 ;; at the end of the docstring.
2368 (re-search-backward "\n" string-start-marker t)) 2440 (goto-char (if (not (= str-end-pos (point-max)))
2369 (newline) 2441 (- str-end-pos num-quotes)
2370 (newline-and-indent)) 2442 str-end-pos))
2371 (fill-paragraph justify)))) t) 2443 (delete-region (point) (progn
2444 (skip-syntax-backward "> ")
2445 (point)))
2446 (and (cdr delimiters-style)
2447 ;; Add newlines only if string ends.
2448 (not (= str-end-pos (point-max)))
2449 (or (newline (cdr delimiters-style)) t)
2450 ;; Again indent only if a newline is added.
2451 (indent-according-to-mode))))) t)
2372 2452
2373(defun python-fill-decorator (&optional justify) 2453(defun python-fill-decorator (&optional justify)
2374 "Decorator fill function for `python-fill-paragraph-function'. 2454 "Decorator fill function for `python-fill-paragraph-function'.