aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2012-05-17 00:03:11 -0300
committerFabián Ezequiel Gallina2012-05-17 00:03:11 -0300
commitc2cb97aec2d940c21629b7872fafc9998f673dae (patch)
tree8b25b39b005adbc39f4c205aaac5c6bcf1f6c3c9 /lisp/progmodes/python.el
parent0567effbae17635607455d23be90da9d02d744c7 (diff)
downloademacs-c2cb97aec2d940c21629b7872fafc9998f673dae.tar.gz
emacs-c2cb97aec2d940c21629b7872fafc9998f673dae.zip
User customizable fill-paragraph behavior.
For this, four new variables which contain the symbol name of the function that specifies the behavior of fill-paragraph on certain conditions were added: * python-fill-comment-function: For comments * python-fill-string-function: For strings * python-fill-decorator-function: For decorators * python-fill-paren-function: For parens All of these variables are safe local variables in the case the value provided is a symbol. Out of the box, they default to these four new functions respectively: * python-fill-comment * python-fill-string * python-fill-decorator * python-fill-paren
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el175
1 files changed, 112 insertions, 63 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index bbf66f2033f..be5221cee28 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1501,6 +1501,38 @@ inferior python process is updated properly."
1501 1501
1502;;; Fill paragraph 1502;;; Fill paragraph
1503 1503
1504(defcustom python-fill-comment-function 'python-fill-comment
1505 "Function to fill comments.
1506This is the function used by `python-fill-paragraph-function' to
1507fill comments."
1508 :type 'symbol
1509 :group 'python
1510 :safe 'symbolp)
1511
1512(defcustom python-fill-string-function 'python-fill-string
1513 "Function to fill strings.
1514This is the function used by `python-fill-paragraph-function' to
1515fill strings."
1516 :type 'symbol
1517 :group 'python
1518 :safe 'symbolp)
1519
1520(defcustom python-fill-decorator-function 'python-fill-decorator
1521 "Function to fill decorators.
1522This is the function used by `python-fill-paragraph-function' to
1523fill decorators."
1524 :type 'symbol
1525 :group 'python
1526 :safe 'symbolp)
1527
1528(defcustom python-fill-paren-function 'python-fill-paren
1529 "Function to fill parens.
1530This is the function used by `python-fill-paragraph-function' to
1531fill parens."
1532 :type 'symbol
1533 :group 'python
1534 :safe 'symbolp)
1535
1504(defun python-fill-paragraph-function (&optional justify) 1536(defun python-fill-paragraph-function (&optional justify)
1505 "`fill-paragraph-function' handling multi-line strings and possibly comments. 1537 "`fill-paragraph-function' handling multi-line strings and possibly comments.
1506If any of the current line is in or at the end of a multi-line string, 1538If any of the current line is in or at the end of a multi-line string,
@@ -1512,82 +1544,99 @@ Optional argument JUSTIFY defines if the paragraph should be justified."
1512 (back-to-indentation) 1544 (back-to-indentation)
1513 (cond 1545 (cond
1514 ;; Comments 1546 ;; Comments
1515 ((fill-comment-paragraph justify)) 1547 ((funcall python-fill-comment-function justify))
1516 ;; Docstrings 1548 ;; Strings/Docstrings
1517 ((save-excursion (skip-chars-forward "\"'uUrR") 1549 ((save-excursion (skip-chars-forward "\"'uUrR")
1518 (python-info-ppss-context 'string)) 1550 (python-info-ppss-context 'string))
1519 (let ((marker (point-marker)) 1551 (funcall python-fill-string-function justify))
1520 (string-start-marker
1521 (progn
1522 (skip-chars-forward "\"'uUrR")
1523 (goto-char (python-info-ppss-context 'string))
1524 (skip-chars-forward "\"'uUrR")
1525 (point-marker)))
1526 (reg-start (line-beginning-position))
1527 (string-end-marker
1528 (progn
1529 (while (python-info-ppss-context 'string)
1530 (goto-char (1+ (point-marker))))
1531 (skip-chars-backward "\"'")
1532 (point-marker)))
1533 (reg-end (line-end-position))
1534 (fill-paragraph-function))
1535 (save-restriction
1536 (narrow-to-region reg-start reg-end)
1537 (save-excursion
1538 (goto-char string-start-marker)
1539 (delete-region (point-marker) (progn
1540 (skip-syntax-forward "> ")
1541 (point-marker)))
1542 (goto-char string-end-marker)
1543 (delete-region (point-marker) (progn
1544 (skip-syntax-backward "> ")
1545 (point-marker)))
1546 (save-excursion
1547 (goto-char marker)
1548 (fill-paragraph justify))
1549 ;; If there is a newline in the docstring lets put triple
1550 ;; quote in it's own line to follow pep 8
1551 (when (save-excursion
1552 (re-search-backward "\n" string-start-marker t))
1553 (newline)
1554 (newline-and-indent))
1555 (fill-paragraph justify)))) t)
1556 ;; Decorators 1552 ;; Decorators
1557 ((equal (char-after (save-excursion 1553 ((equal (char-after (save-excursion
1558 (back-to-indentation) 1554 (back-to-indentation)
1559 (point-marker))) ?@) t) 1555 (point-marker))) ?@)
1556 (funcall python-fill-decorator-function justify))
1560 ;; Parens 1557 ;; Parens
1561 ((or (python-info-ppss-context 'paren) 1558 ((or (python-info-ppss-context 'paren)
1562 (looking-at (python-rx open-paren)) 1559 (looking-at (python-rx open-paren))
1563 (save-excursion 1560 (save-excursion
1564 (skip-syntax-forward "^(" (line-end-position)) 1561 (skip-syntax-forward "^(" (line-end-position))
1565 (looking-at (python-rx open-paren)))) 1562 (looking-at (python-rx open-paren))))
1566 (save-restriction 1563 (funcall python-fill-paren-function justify))
1567 (narrow-to-region (progn
1568 (while (python-info-ppss-context 'paren)
1569 (goto-char (1- (point-marker))))
1570 (point-marker)
1571 (line-beginning-position))
1572 (progn
1573 (when (not (python-info-ppss-context 'paren))
1574 (end-of-line)
1575 (when (not (python-info-ppss-context 'paren))
1576 (skip-syntax-backward "^)")))
1577 (while (python-info-ppss-context 'paren)
1578 (goto-char (1+ (point-marker))))
1579 (point-marker)))
1580 (let ((paragraph-start "\f\\|[ \t]*$")
1581 (paragraph-separate ",")
1582 (fill-paragraph-function))
1583 (goto-char (point-min))
1584 (fill-paragraph justify))
1585 (while (not (eobp))
1586 (forward-line 1)
1587 (python-indent-line)
1588 (goto-char (line-end-position)))) t)
1589 (t t)))) 1564 (t t))))
1590 1565
1566(defun python-fill-comment (&optional justify)
1567 "Comment fill function for `python-fill-paragraph-function'."
1568 (fill-comment-paragraph justify))
1569
1570(defun python-fill-string (&optional justify)
1571 "String fill function for `python-fill-paragraph-function'."
1572 (let ((marker (point-marker))
1573 (string-start-marker
1574 (progn
1575 (skip-chars-forward "\"'uUrR")
1576 (goto-char (python-info-ppss-context 'string))
1577 (skip-chars-forward "\"'uUrR")
1578 (point-marker)))
1579 (reg-start (line-beginning-position))
1580 (string-end-marker
1581 (progn
1582 (while (python-info-ppss-context 'string)
1583 (goto-char (1+ (point-marker))))
1584 (skip-chars-backward "\"'")
1585 (point-marker)))
1586 (reg-end (line-end-position))
1587 (fill-paragraph-function))
1588 (save-restriction
1589 (narrow-to-region reg-start reg-end)
1590 (save-excursion
1591 (goto-char string-start-marker)
1592 (delete-region (point-marker) (progn
1593 (skip-syntax-forward "> ")
1594 (point-marker)))
1595 (goto-char string-end-marker)
1596 (delete-region (point-marker) (progn
1597 (skip-syntax-backward "> ")
1598 (point-marker)))
1599 (save-excursion
1600 (goto-char marker)
1601 (fill-paragraph justify))
1602 ;; If there is a newline in the docstring lets put triple
1603 ;; quote in it's own line to follow pep 8
1604 (when (save-excursion
1605 (re-search-backward "\n" string-start-marker t))
1606 (newline)
1607 (newline-and-indent))
1608 (fill-paragraph justify)))) t)
1609
1610(defun python-fill-decorator (&optional justify)
1611 "Decorator fill function for `python-fill-paragraph-function'."
1612 t)
1613
1614(defun python-fill-paren (&optional justify)
1615 "Paren fill function for `python-fill-paragraph-function'."
1616 (save-restriction
1617 (narrow-to-region (progn
1618 (while (python-info-ppss-context 'paren)
1619 (goto-char (1- (point-marker))))
1620 (point-marker)
1621 (line-beginning-position))
1622 (progn
1623 (when (not (python-info-ppss-context 'paren))
1624 (end-of-line)
1625 (when (not (python-info-ppss-context 'paren))
1626 (skip-syntax-backward "^)")))
1627 (while (python-info-ppss-context 'paren)
1628 (goto-char (1+ (point-marker))))
1629 (point-marker)))
1630 (let ((paragraph-start "\f\\|[ \t]*$")
1631 (paragraph-separate ",")
1632 (fill-paragraph-function))
1633 (goto-char (point-min))
1634 (fill-paragraph justify))
1635 (while (not (eobp))
1636 (forward-line 1)
1637 (python-indent-line)
1638 (goto-char (line-end-position)))) t)
1639
1591 1640
1592;;; Skeletons 1641;;; Skeletons
1593 1642