aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2012-07-18 21:55:55 -0300
committerFabián Ezequiel Gallina2012-07-18 21:55:55 -0300
commit1d29cc7da73dde538c97d029723b8a5a1be6cea1 (patch)
tree9b2d9a1376246d508b5a150091a5da21db82bf92 /lisp/progmodes/python.el
parent60cfd2785740850bbc46c954e22e51b1d26fc446 (diff)
downloademacs-1d29cc7da73dde538c97d029723b8a5a1be6cea1.tar.gz
emacs-1d29cc7da73dde538c97d029723b8a5a1be6cea1.zip
Enhancements to ppss related code (thanks Stefan).
* progmodes/python.el (python-indent-context) (python-indent-calculate-indentation, python-indent-dedent-line) (python-indent-electric-colon, python-nav-forward-block) (python-mode-abbrev-table) (python-info-assignment-continuation-line-p): Simplified checks for ppss context. (python-info-continuation-line-p): Cleanup. (python-info-ppss-context): Do not catch 'quote. (python-info-ppss-context-type) (python-info-ppss-comment-or-string-p): Simplify.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el73
1 files changed, 24 insertions, 49 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 23e040366dc..132951aedc8 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -656,9 +656,7 @@ START is the buffer position where the sexp starts."
656 (while (and (re-search-backward 656 (while (and (re-search-backward
657 (python-rx block-start) nil t) 657 (python-rx block-start) nil t)
658 (or 658 (or
659 (python-info-ppss-context 'string) 659 (python-info-ppss-context-type)
660 (python-info-ppss-context 'comment)
661 (python-info-ppss-context 'paren)
662 (python-info-continuation-line-p)))) 660 (python-info-continuation-line-p))))
663 (when (looking-at (python-rx block-start)) 661 (when (looking-at (python-rx block-start))
664 (point-marker))))) 662 (point-marker)))))
@@ -726,13 +724,9 @@ START is the buffer position where the sexp starts."
726 (goto-char (line-end-position)) 724 (goto-char (line-end-position))
727 (while (and (re-search-backward 725 (while (and (re-search-backward
728 "\\." (line-beginning-position) t) 726 "\\." (line-beginning-position) t)
729 (or (python-info-ppss-context 'comment) 727 (python-info-ppss-context-type)))
730 (python-info-ppss-context 'string)
731 (python-info-ppss-context 'paren))))
732 (if (and (looking-at "\\.") 728 (if (and (looking-at "\\.")
733 (not (or (python-info-ppss-context 'comment) 729 (not (python-info-ppss-context-type)))
734 (python-info-ppss-context 'string)
735 (python-info-ppss-context 'paren))))
736 ;; The indentation is the same column of the 730 ;; The indentation is the same column of the
737 ;; first matching dot that's not inside a 731 ;; first matching dot that's not inside a
738 ;; comment, a string or a paren 732 ;; comment, a string or a paren
@@ -888,8 +882,7 @@ See `python-indent-line' for details."
888(defun python-indent-dedent-line () 882(defun python-indent-dedent-line ()
889 "De-indent current line." 883 "De-indent current line."
890 (interactive "*") 884 (interactive "*")
891 (when (and (not (or (python-info-ppss-context 'string) 885 (when (and (not (python-info-ppss-comment-or-string-p))
892 (python-info-ppss-context 'comment)))
893 (<= (point-marker) (save-excursion 886 (<= (point-marker) (save-excursion
894 (back-to-indentation) 887 (back-to-indentation)
895 (point-marker))) 888 (point-marker)))
@@ -980,8 +973,7 @@ With numeric ARG, just insert that many colons. With
980 (when (and (not arg) 973 (when (and (not arg)
981 (eolp) 974 (eolp)
982 (not (equal ?: (char-after (- (point-marker) 2)))) 975 (not (equal ?: (char-after (- (point-marker) 2))))
983 (not (or (python-info-ppss-context 'string) 976 (not (python-info-ppss-comment-or-string-p)))
984 (python-info-ppss-context 'comment))))
985 (let ((indentation (current-indentation)) 977 (let ((indentation (current-indentation))
986 (calculated-indentation (python-indent-calculate-indentation))) 978 (calculated-indentation (python-indent-calculate-indentation)))
987 (python-info-closing-block-message) 979 (python-info-closing-block-message)
@@ -1209,17 +1201,13 @@ backward to previous block."
1209 (python-nav-end-of-statement) 1201 (python-nav-end-of-statement)
1210 (while (and 1202 (while (and
1211 (re-search-forward block-start-regexp nil t) 1203 (re-search-forward block-start-regexp nil t)
1212 (or (python-info-ppss-context 'string) 1204 (python-info-ppss-context-type)))
1213 (python-info-ppss-context 'comment)
1214 (python-info-ppss-context 'paren))))
1215 (setq arg (1- arg))) 1205 (setq arg (1- arg)))
1216 (while (< arg 0) 1206 (while (< arg 0)
1217 (python-nav-beginning-of-statement) 1207 (python-nav-beginning-of-statement)
1218 (while (and 1208 (while (and
1219 (re-search-backward block-start-regexp nil t) 1209 (re-search-backward block-start-regexp nil t)
1220 (or (python-info-ppss-context 'string) 1210 (python-info-ppss-context-type)))
1221 (python-info-ppss-context 'comment)
1222 (python-info-ppss-context 'paren))))
1223 (setq arg (1+ arg))) 1211 (setq arg (1+ arg)))
1224 (python-nav-beginning-of-statement) 1212 (python-nav-beginning-of-statement)
1225 (if (not (looking-at (python-rx block-start))) 1213 (if (not (looking-at (python-rx block-start)))
@@ -2249,8 +2237,7 @@ the if condition."
2249 ;; Only expand in code. 2237 ;; Only expand in code.
2250 :enable-function (lambda () 2238 :enable-function (lambda ()
2251 (and 2239 (and
2252 (not (or (python-info-ppss-context 'string) 2240 (not (python-info-ppss-comment-or-string-p))
2253 (python-info-ppss-context 'comment)))
2254 python-skeleton-autoinsert))) 2241 python-skeleton-autoinsert)))
2255 2242
2256(defmacro python-skeleton-define (name doc &rest skel) 2243(defmacro python-skeleton-define (name doc &rest skel)
@@ -2686,24 +2673,20 @@ where the continued line ends."
2686 (cond ((equal context-type 'paren) 2673 (cond ((equal context-type 'paren)
2687 ;; Lines inside a paren are always a continuation line 2674 ;; Lines inside a paren are always a continuation line
2688 ;; (except the first one). 2675 ;; (except the first one).
2689 (when (equal (python-info-ppss-context-type) 'paren) 2676 (python-util-forward-comment -1)
2690 (python-util-forward-comment -1) 2677 (point-marker))
2691 (python-util-forward-comment -1) 2678 ((member context-type '(string comment))
2692 (point-marker)))
2693 ((or (equal context-type 'comment)
2694 (equal context-type 'string))
2695 ;; move forward an roll again 2679 ;; move forward an roll again
2696 (goto-char context-start) 2680 (goto-char context-start)
2697 (python-util-forward-comment) 2681 (python-util-forward-comment)
2698 (python-info-continuation-line-p)) 2682 (python-info-continuation-line-p))
2699 (t 2683 (t
2700 ;; Not within a paren, string or comment, the only way we are 2684 ;; Not within a paren, string or comment, the only way
2701 ;; dealing with a continuation line is that previous line 2685 ;; we are dealing with a continuation line is that
2702 ;; contains a backslash, and this can only be the previous line 2686 ;; previous line contains a backslash, and this can
2703 ;; from current 2687 ;; only be the previous line from current
2704 (back-to-indentation) 2688 (back-to-indentation)
2705 (python-util-forward-comment -1) 2689 (python-util-forward-comment -1)
2706 (python-util-forward-comment -1)
2707 (when (and (equal (1- line-start) (line-number-at-pos)) 2690 (when (and (equal (1- line-start) (line-number-at-pos))
2708 (python-info-line-ends-backslash-p)) 2691 (python-info-line-ends-backslash-p))
2709 (point-marker)))))))) 2692 (point-marker))))))))
@@ -2731,45 +2714,37 @@ operator."
2731 assignment-operator 2714 assignment-operator
2732 not-simple-operator) 2715 not-simple-operator)
2733 (line-end-position) t) 2716 (line-end-position) t)
2734 (not (or (python-info-ppss-context 'string) 2717 (not (python-info-ppss-context-type))))
2735 (python-info-ppss-context 'paren)
2736 (python-info-ppss-context 'comment)))))
2737 (skip-syntax-forward "\s") 2718 (skip-syntax-forward "\s")
2738 (point-marker))))) 2719 (point-marker)))))
2739 2720
2740(defun python-info-ppss-context (type &optional syntax-ppss) 2721(defun python-info-ppss-context (type &optional syntax-ppss)
2741 "Return non-nil if point is on TYPE using SYNTAX-PPSS. 2722 "Return non-nil if point is on TYPE using SYNTAX-PPSS.
2742TYPE can be 'comment, 'string or 'paren. It returns the start 2723TYPE can be `comment', `string' or `paren'. It returns the start
2743character address of the specified TYPE." 2724character address of the specified TYPE."
2744 (let ((ppss (or syntax-ppss (syntax-ppss)))) 2725 (let ((ppss (or syntax-ppss (syntax-ppss))))
2745 (case type 2726 (case type
2746 ('comment 2727 (comment
2747 (and (nth 4 ppss) 2728 (and (nth 4 ppss)
2748 (nth 8 ppss))) 2729 (nth 8 ppss)))
2749 ('string 2730 (string
2750 (and (not (nth 4 ppss)) 2731 (and (not (nth 4 ppss))
2751 (nth 8 ppss))) 2732 (nth 8 ppss)))
2752 ('paren 2733 (paren
2753 (nth 1 ppss)) 2734 (nth 1 ppss))
2754 (t nil)))) 2735 (t nil))))
2755 2736
2756(defun python-info-ppss-context-type (&optional syntax-ppss) 2737(defun python-info-ppss-context-type (&optional syntax-ppss)
2757 "Return the context type using SYNTAX-PPSS. 2738 "Return the context type using SYNTAX-PPSS.
2758The type returned can be 'comment, 'string or 'paren." 2739The type returned can be `comment', `string' or `paren'."
2759 (let ((ppss (or syntax-ppss (syntax-ppss)))) 2740 (let ((ppss (or syntax-ppss (syntax-ppss))))
2760 (cond 2741 (cond
2761 ((and (nth 4 ppss) 2742 ((nth 8 ppss) (if (nth 4 ppss) 'comment 'string))
2762 (nth 8 ppss)) 2743 ((nth 1 ppss) 'paren))))
2763 'comment)
2764 ((nth 8 ppss)
2765 'string)
2766 ((nth 1 ppss)
2767 'paren)
2768 (t nil))))
2769 2744
2770(defsubst python-info-ppss-comment-or-string-p () 2745(defsubst python-info-ppss-comment-or-string-p ()
2771 "Return non-nil if point is inside 'comment or 'string." 2746 "Return non-nil if point is inside 'comment or 'string."
2772 (car (member (python-info-ppss-context-type) '(string comment)))) 2747 (nth 8 (syntax-ppss)))
2773 2748
2774(defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss) 2749(defun python-info-looking-at-beginning-of-defun (&optional syntax-ppss)
2775 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS." 2750 "Check if point is at `beginning-of-defun' using SYNTAX-PPSS."