diff options
| author | Fabián Ezequiel Gallina | 2012-05-17 00:03:07 -0300 |
|---|---|---|
| committer | Fabián Ezequiel Gallina | 2012-05-17 00:03:07 -0300 |
| commit | 14a7849583a5d42aeb196226723cb9386aac2bba (patch) | |
| tree | 276963d763579dcd65c27cb7e4e62a954fd15090 /lisp/progmodes/python.el | |
| parent | 099bf0104e2c6118189b64e79651f3bdfe9e09b2 (diff) | |
| download | emacs-14a7849583a5d42aeb196226723cb9386aac2bba.tar.gz emacs-14a7849583a5d42aeb196226723cb9386aac2bba.zip | |
New function python-info-ppss-context
Use this function for most syntax-ppss related tasks. While in some
parts code could be longer it makes everything more readable.
This is the first step for a cleaner indentation machinery.
Diffstat (limited to 'lisp/progmodes/python.el')
| -rw-r--r-- | lisp/progmodes/python.el | 75 |
1 files changed, 48 insertions, 27 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index 623614598d3..ab711724ae7 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el | |||
| @@ -331,9 +331,9 @@ | |||
| 331 | (? ?\[ (+ (not (any ?\]))) ?\]) (* space) | 331 | (? ?\[ (+ (not (any ?\]))) ?\]) (* space) |
| 332 | assignment-operator))) | 332 | assignment-operator))) |
| 333 | (when (re-search-forward re limit t) | 333 | (when (re-search-forward re limit t) |
| 334 | (while (and (not (equal (nth 0 (syntax-ppss)) 0)) | 334 | (while (and (python-info-ppss-context 'paren) |
| 335 | (re-search-forward re limit t))) | 335 | (re-search-forward re limit t))) |
| 336 | (if (and (equal (nth 0 (syntax-ppss)) 0) | 336 | (if (and (not (python-info-ppss-context 'paren)) |
| 337 | (not (equal (char-after (point-marker)) ?=))) | 337 | (not (equal (char-after (point-marker)) ?=))) |
| 338 | t | 338 | t |
| 339 | (set-match-data nil))))) | 339 | (set-match-data nil))))) |
| @@ -346,10 +346,10 @@ | |||
| 346 | assignment-operator))) | 346 | assignment-operator))) |
| 347 | (when (and (re-search-forward re limit t) | 347 | (when (and (re-search-forward re limit t) |
| 348 | (goto-char (nth 3 (match-data)))) | 348 | (goto-char (nth 3 (match-data)))) |
| 349 | (while (and (not (equal (nth 0 (syntax-ppss)) 0)) | 349 | (while (and (python-info-ppss-context 'paren) |
| 350 | (re-search-forward re limit t)) | 350 | (re-search-forward re limit t)) |
| 351 | (goto-char (nth 3 (match-data)))) | 351 | (goto-char (nth 3 (match-data)))) |
| 352 | (if (equal (nth 0 (syntax-ppss)) 0) | 352 | (if (not (python-info-ppss-context 'paren)) |
| 353 | t | 353 | t |
| 354 | (set-match-data nil))))) | 354 | (set-match-data nil))))) |
| 355 | (1 font-lock-variable-name-face nil nil)))) | 355 | (1 font-lock-variable-name-face nil nil)))) |
| @@ -477,7 +477,8 @@ These make `python-indent-calculate-indentation' subtract the value of | |||
| 477 | (while (and (not found-block) | 477 | (while (and (not found-block) |
| 478 | (re-search-forward | 478 | (re-search-forward |
| 479 | (python-rx line-start block-start) nil t)) | 479 | (python-rx line-start block-start) nil t)) |
| 480 | (when (and (not (syntax-ppss-context (syntax-ppss))) | 480 | (when (and (not (python-info-ppss-context 'string)) |
| 481 | (not (python-info-ppss-context 'comment)) | ||
| 481 | (progn | 482 | (progn |
| 482 | (goto-char (line-end-position)) | 483 | (goto-char (line-end-position)) |
| 483 | (forward-comment -1) | 484 | (forward-comment -1) |
| @@ -527,14 +528,14 @@ START is the buffer position where the sexp starts." | |||
| 527 | (bobp)) | 528 | (bobp)) |
| 528 | 'no-indent) | 529 | 'no-indent) |
| 529 | ;; Inside a paren | 530 | ;; Inside a paren |
| 530 | ((setq start (nth 1 ppss)) | 531 | ((setq start (python-info-ppss-context 'paren ppss)) |
| 531 | 'inside-paren) | 532 | 'inside-paren) |
| 532 | ;; Inside string | 533 | ;; Inside string |
| 533 | ((setq start (when (and (nth 3 ppss)) | 534 | ((setq start (python-info-ppss-context 'string ppss)) |
| 534 | (nth 8 ppss))) | ||
| 535 | 'inside-string) | 535 | 'inside-string) |
| 536 | ;; After backslash | 536 | ;; After backslash |
| 537 | ((setq start (when (not (syntax-ppss-context ppss)) | 537 | ((setq start (when (not (or (python-info-ppss-context 'string ppss) |
| 538 | (python-info-ppss-context 'comment ppss))) | ||
| 538 | (let ((line-beg-pos (line-beginning-position))) | 539 | (let ((line-beg-pos (line-beginning-position))) |
| 539 | (when (eq ?\\ (char-before (1- line-beg-pos))) | 540 | (when (eq ?\\ (char-before (1- line-beg-pos))) |
| 540 | (- line-beg-pos 2))))) | 541 | (- line-beg-pos 2))))) |
| @@ -570,7 +571,7 @@ START is the buffer position where the sexp starts." | |||
| 570 | (while (and (forward-comment -1) (not (bobp)))) | 571 | (while (and (forward-comment -1) (not (bobp)))) |
| 571 | (while (and (not (back-to-indentation)) | 572 | (while (and (not (back-to-indentation)) |
| 572 | (not (bobp)) | 573 | (not (bobp)) |
| 573 | (if (> (nth 0 (syntax-ppss)) 0) | 574 | (if (python-info-ppss-context 'paren) |
| 574 | (forward-line -1) | 575 | (forward-line -1) |
| 575 | (if (save-excursion | 576 | (if (save-excursion |
| 576 | (forward-line -1) | 577 | (forward-line -1) |
| @@ -642,7 +643,7 @@ START is the buffer position where the sexp starts." | |||
| 642 | (forward-comment 1) | 643 | (forward-comment 1) |
| 643 | (looking-at (regexp-opt '(")" "]" "}"))) | 644 | (looking-at (regexp-opt '(")" "]" "}"))) |
| 644 | (forward-char 1) | 645 | (forward-char 1) |
| 645 | (when (not (nth 1 (syntax-ppss))) | 646 | (when (not (python-info-ppss-context 'paren)) |
| 646 | (goto-char context-start) | 647 | (goto-char context-start) |
| 647 | (back-to-indentation) | 648 | (back-to-indentation) |
| 648 | (current-column))) | 649 | (current-column))) |
| @@ -737,7 +738,8 @@ Internally just calls `python-indent-line'." | |||
| 737 | (defun python-indent-dedent-line () | 738 | (defun python-indent-dedent-line () |
| 738 | "Dedent current line." | 739 | "Dedent current line." |
| 739 | (interactive "*") | 740 | (interactive "*") |
| 740 | (when (and (not (syntax-ppss-context (syntax-ppss))) | 741 | (when (and (not (or (python-info-ppss-context 'string) |
| 742 | (python-info-ppss-context 'comment))) | ||
| 741 | (<= (point-marker) (save-excursion | 743 | (<= (point-marker) (save-excursion |
| 742 | (back-to-indentation) | 744 | (back-to-indentation) |
| 743 | (point-marker))) | 745 | (point-marker))) |
| @@ -837,7 +839,8 @@ just insert a single colon." | |||
| 837 | (self-insert-command (if (not (integerp arg)) 1 arg)) | 839 | (self-insert-command (if (not (integerp arg)) 1 arg)) |
| 838 | (and (not arg) | 840 | (and (not arg) |
| 839 | (eolp) | 841 | (eolp) |
| 840 | (not (nth 8 (syntax-ppss))) | 842 | (not (or (python-info-ppss-context 'string) |
| 843 | (python-info-ppss-context 'comment))) | ||
| 841 | (> (current-indentation) (python-indent-calculate-indentation)) | 844 | (> (current-indentation) (python-indent-calculate-indentation)) |
| 842 | (save-excursion (python-indent-line)))) | 845 | (save-excursion (python-indent-line)))) |
| 843 | (put 'python-indent-electric-colon 'delete-selection t) | 846 | (put 'python-indent-electric-colon 'delete-selection t) |
| @@ -1454,18 +1457,19 @@ Optional argument JUSTIFY defines if the paragraph should be justified." | |||
| 1454 | ((fill-comment-paragraph justify)) | 1457 | ((fill-comment-paragraph justify)) |
| 1455 | ;; Docstrings | 1458 | ;; Docstrings |
| 1456 | ((save-excursion (skip-chars-forward "\"'uUrR") | 1459 | ((save-excursion (skip-chars-forward "\"'uUrR") |
| 1457 | (nth 3 (syntax-ppss))) | 1460 | (python-info-ppss-context 'string)) |
| 1458 | (let ((marker (point-marker)) | 1461 | (let ((marker (point-marker)) |
| 1459 | (string-start-marker | 1462 | (string-start-marker |
| 1460 | (progn | 1463 | (progn |
| 1461 | (skip-chars-forward "\"'uUrR") | 1464 | (skip-chars-forward "\"'uUrR") |
| 1462 | (goto-char (nth 8 (syntax-ppss))) | 1465 | (goto-char (python-info-ppss-context 'string)) |
| 1463 | (skip-chars-forward "\"'uUrR") | 1466 | (skip-chars-forward "\"'uUrR") |
| 1464 | (point-marker))) | 1467 | (point-marker))) |
| 1465 | (reg-start (line-beginning-position)) | 1468 | (reg-start (line-beginning-position)) |
| 1466 | (string-end-marker | 1469 | (string-end-marker |
| 1467 | (progn | 1470 | (progn |
| 1468 | (while (nth 3 (syntax-ppss)) (goto-char (1+ (point-marker)))) | 1471 | (while (python-info-ppss-context 'string) |
| 1472 | (goto-char (1+ (point-marker)))) | ||
| 1469 | (skip-chars-backward "\"'") | 1473 | (skip-chars-backward "\"'") |
| 1470 | (point-marker))) | 1474 | (point-marker))) |
| 1471 | (reg-end (line-end-position)) | 1475 | (reg-end (line-end-position)) |
| @@ -1496,23 +1500,23 @@ Optional argument JUSTIFY defines if the paragraph should be justified." | |||
| 1496 | (back-to-indentation) | 1500 | (back-to-indentation) |
| 1497 | (point-marker))) ?@) t) | 1501 | (point-marker))) ?@) t) |
| 1498 | ;; Parens | 1502 | ;; Parens |
| 1499 | ((or (> (nth 0 (syntax-ppss)) 0) | 1503 | ((or (python-info-ppss-context 'paren) |
| 1500 | (looking-at (python-rx open-paren)) | 1504 | (looking-at (python-rx open-paren)) |
| 1501 | (save-excursion | 1505 | (save-excursion |
| 1502 | (skip-syntax-forward "^(" (line-end-position)) | 1506 | (skip-syntax-forward "^(" (line-end-position)) |
| 1503 | (looking-at (python-rx open-paren)))) | 1507 | (looking-at (python-rx open-paren)))) |
| 1504 | (save-restriction | 1508 | (save-restriction |
| 1505 | (narrow-to-region (progn | 1509 | (narrow-to-region (progn |
| 1506 | (while (> (nth 0 (syntax-ppss)) 0) | 1510 | (while (python-info-ppss-context 'paren) |
| 1507 | (goto-char (1- (point-marker)))) | 1511 | (goto-char (1- (point-marker)))) |
| 1508 | (point-marker) | 1512 | (point-marker) |
| 1509 | (line-beginning-position)) | 1513 | (line-beginning-position)) |
| 1510 | (progn | 1514 | (progn |
| 1511 | (when (not (> (nth 0 (syntax-ppss)) 0)) | 1515 | (when (not (python-info-ppss-context 'paren)) |
| 1512 | (end-of-line) | 1516 | (end-of-line) |
| 1513 | (when (not (> (nth 0 (syntax-ppss)) 0)) | 1517 | (when (not (python-info-ppss-context 'paren)) |
| 1514 | (skip-syntax-backward "^)"))) | 1518 | (skip-syntax-backward "^)"))) |
| 1515 | (while (> (nth 0 (syntax-ppss)) 0) | 1519 | (while (python-info-ppss-context 'paren) |
| 1516 | (goto-char (1+ (point-marker)))) | 1520 | (goto-char (1+ (point-marker)))) |
| 1517 | (point-marker))) | 1521 | (point-marker))) |
| 1518 | (let ((paragraph-start "\f\\|[ \t]*$") | 1522 | (let ((paragraph-start "\f\\|[ \t]*$") |
| @@ -1548,7 +1552,8 @@ the if condition." | |||
| 1548 | ;; Only expand in code. | 1552 | ;; Only expand in code. |
| 1549 | :enable-function (lambda () | 1553 | :enable-function (lambda () |
| 1550 | (and | 1554 | (and |
| 1551 | (not (nth 8 (syntax-ppss))) | 1555 | (not (or (python-info-ppss-context 'string) |
| 1556 | (python-info-ppss-context 'comment))) | ||
| 1552 | python-skeleton-autoinsert))) | 1557 | python-skeleton-autoinsert))) |
| 1553 | 1558 | ||
| 1554 | (defmacro python-skeleton-define (name doc &rest skel) | 1559 | (defmacro python-skeleton-define (name doc &rest skel) |
| @@ -1785,8 +1790,8 @@ will be used. If not FORCE-PROCESS is passed what | |||
| 1785 | (ppss (syntax-ppss)) | 1790 | (ppss (syntax-ppss)) |
| 1786 | (help (when (and input | 1791 | (help (when (and input |
| 1787 | (not (string= input (concat current-defun "."))) | 1792 | (not (string= input (concat current-defun "."))) |
| 1788 | (eq nil (nth 3 ppss)) | 1793 | (not (or (python-info-ppss-context 'string ppss) |
| 1789 | (eq nil (nth 4 ppss))) | 1794 | (python-info-ppss-context 'comment ppss)))) |
| 1790 | (when (string-match (concat | 1795 | (when (string-match (concat |
| 1791 | (regexp-quote (concat current-defun ".")) | 1796 | (regexp-quote (concat current-defun ".")) |
| 1792 | "self\\.") input) | 1797 | "self\\.") input) |
| @@ -1910,7 +1915,7 @@ not inside a defun." | |||
| 1910 | (save-excursion | 1915 | (save-excursion |
| 1911 | (let ((innermost-paren (progn | 1916 | (let ((innermost-paren (progn |
| 1912 | (goto-char (line-end-position)) | 1917 | (goto-char (line-end-position)) |
| 1913 | (nth 1 (syntax-ppss))))) | 1918 | (python-info-ppss-context 'paren)))) |
| 1914 | (when (and innermost-paren | 1919 | (when (and innermost-paren |
| 1915 | (and (<= (line-beginning-position) innermost-paren) | 1920 | (and (<= (line-beginning-position) innermost-paren) |
| 1916 | (>= (line-end-position) innermost-paren))) | 1921 | (>= (line-end-position) innermost-paren))) |
| @@ -1918,7 +1923,7 @@ not inside a defun." | |||
| 1918 | (looking-at (python-rx open-paren (* space) line-end))))) | 1923 | (looking-at (python-rx open-paren (* space) line-end))))) |
| 1919 | (save-excursion | 1924 | (save-excursion |
| 1920 | (back-to-indentation) | 1925 | (back-to-indentation) |
| 1921 | (nth 1 (syntax-ppss))))) | 1926 | (python-info-ppss-context 'paren)))) |
| 1922 | 1927 | ||
| 1923 | (defun python-info-block-continuation-line-p () | 1928 | (defun python-info-block-continuation-line-p () |
| 1924 | "Return non-nil if current line is a continuation of a block." | 1929 | "Return non-nil if current line is a continuation of a block." |
| @@ -1945,9 +1950,25 @@ not inside a defun." | |||
| 1945 | assignment-operator | 1950 | assignment-operator |
| 1946 | not-simple-operator) | 1951 | not-simple-operator) |
| 1947 | (line-end-position) t) | 1952 | (line-end-position) t) |
| 1948 | (not (syntax-ppss-context (syntax-ppss)))))) | 1953 | (not (or (python-info-ppss-context 'string) |
| 1954 | (python-info-ppss-context 'comment)))))) | ||
| 1949 | (point-marker)))) | 1955 | (point-marker)))) |
| 1950 | 1956 | ||
| 1957 | (defun python-info-ppss-context (type &optional syntax-ppss) | ||
| 1958 | "Return non-nil if point is on TYPE using SYNTAX-PPSS. | ||
| 1959 | TYPE can be 'comment, 'string or 'parent. It returns the start | ||
| 1960 | character address of the specified TYPE." | ||
| 1961 | (let ((ppss (or syntax-ppss (syntax-ppss)))) | ||
| 1962 | (case type | ||
| 1963 | ('comment | ||
| 1964 | (and (nth 4 ppss) | ||
| 1965 | (nth 8 ppss))) | ||
| 1966 | ('string | ||
| 1967 | (nth 8 ppss)) | ||
| 1968 | ('paren | ||
| 1969 | (nth 1 ppss)) | ||
| 1970 | (t nil)))) | ||
| 1971 | |||
| 1951 | 1972 | ||
| 1952 | ;;;###autoload | 1973 | ;;;###autoload |
| 1953 | (define-derived-mode python-mode fundamental-mode "Python" | 1974 | (define-derived-mode python-mode fundamental-mode "Python" |