(? ?\[ (+ (not (any ?\]))) ?\]) (* space)
assignment-operator)))
(when (re-search-forward re limit t)
- (while (and (not (equal (nth 0 (syntax-ppss)) 0))
+ (while (and (python-info-ppss-context 'paren)
(re-search-forward re limit t)))
- (if (and (equal (nth 0 (syntax-ppss)) 0)
+ (if (and (not (python-info-ppss-context 'paren))
(not (equal (char-after (point-marker)) ?=)))
t
(set-match-data nil)))))
assignment-operator)))
(when (and (re-search-forward re limit t)
(goto-char (nth 3 (match-data))))
- (while (and (not (equal (nth 0 (syntax-ppss)) 0))
+ (while (and (python-info-ppss-context 'paren)
(re-search-forward re limit t))
(goto-char (nth 3 (match-data))))
- (if (equal (nth 0 (syntax-ppss)) 0)
+ (if (not (python-info-ppss-context 'paren))
t
(set-match-data nil)))))
(1 font-lock-variable-name-face nil nil))))
(while (and (not found-block)
(re-search-forward
(python-rx line-start block-start) nil t))
- (when (and (not (syntax-ppss-context (syntax-ppss)))
+ (when (and (not (python-info-ppss-context 'string))
+ (not (python-info-ppss-context 'comment))
(progn
(goto-char (line-end-position))
(forward-comment -1)
(bobp))
'no-indent)
;; Inside a paren
- ((setq start (nth 1 ppss))
+ ((setq start (python-info-ppss-context 'paren ppss))
'inside-paren)
;; Inside string
- ((setq start (when (and (nth 3 ppss))
- (nth 8 ppss)))
+ ((setq start (python-info-ppss-context 'string ppss))
'inside-string)
;; After backslash
- ((setq start (when (not (syntax-ppss-context ppss))
+ ((setq start (when (not (or (python-info-ppss-context 'string ppss)
+ (python-info-ppss-context 'comment ppss)))
(let ((line-beg-pos (line-beginning-position)))
(when (eq ?\\ (char-before (1- line-beg-pos)))
(- line-beg-pos 2)))))
(while (and (forward-comment -1) (not (bobp))))
(while (and (not (back-to-indentation))
(not (bobp))
- (if (> (nth 0 (syntax-ppss)) 0)
+ (if (python-info-ppss-context 'paren)
(forward-line -1)
(if (save-excursion
(forward-line -1)
(forward-comment 1)
(looking-at (regexp-opt '(")" "]" "}")))
(forward-char 1)
- (when (not (nth 1 (syntax-ppss)))
+ (when (not (python-info-ppss-context 'paren))
(goto-char context-start)
(back-to-indentation)
(current-column)))
(defun python-indent-dedent-line ()
"Dedent current line."
(interactive "*")
- (when (and (not (syntax-ppss-context (syntax-ppss)))
+ (when (and (not (or (python-info-ppss-context 'string)
+ (python-info-ppss-context 'comment)))
(<= (point-marker) (save-excursion
(back-to-indentation)
(point-marker)))
(self-insert-command (if (not (integerp arg)) 1 arg))
(and (not arg)
(eolp)
- (not (nth 8 (syntax-ppss)))
+ (not (or (python-info-ppss-context 'string)
+ (python-info-ppss-context 'comment)))
(> (current-indentation) (python-indent-calculate-indentation))
(save-excursion (python-indent-line))))
(put 'python-indent-electric-colon 'delete-selection t)
((fill-comment-paragraph justify))
;; Docstrings
((save-excursion (skip-chars-forward "\"'uUrR")
- (nth 3 (syntax-ppss)))
+ (python-info-ppss-context 'string))
(let ((marker (point-marker))
(string-start-marker
(progn
(skip-chars-forward "\"'uUrR")
- (goto-char (nth 8 (syntax-ppss)))
+ (goto-char (python-info-ppss-context 'string))
(skip-chars-forward "\"'uUrR")
(point-marker)))
(reg-start (line-beginning-position))
(string-end-marker
(progn
- (while (nth 3 (syntax-ppss)) (goto-char (1+ (point-marker))))
+ (while (python-info-ppss-context 'string)
+ (goto-char (1+ (point-marker))))
(skip-chars-backward "\"'")
(point-marker)))
(reg-end (line-end-position))
(back-to-indentation)
(point-marker))) ?@) t)
;; Parens
- ((or (> (nth 0 (syntax-ppss)) 0)
+ ((or (python-info-ppss-context 'paren)
(looking-at (python-rx open-paren))
(save-excursion
(skip-syntax-forward "^(" (line-end-position))
(looking-at (python-rx open-paren))))
(save-restriction
(narrow-to-region (progn
- (while (> (nth 0 (syntax-ppss)) 0)
+ (while (python-info-ppss-context 'paren)
(goto-char (1- (point-marker))))
(point-marker)
(line-beginning-position))
(progn
- (when (not (> (nth 0 (syntax-ppss)) 0))
+ (when (not (python-info-ppss-context 'paren))
(end-of-line)
- (when (not (> (nth 0 (syntax-ppss)) 0))
+ (when (not (python-info-ppss-context 'paren))
(skip-syntax-backward "^)")))
- (while (> (nth 0 (syntax-ppss)) 0)
+ (while (python-info-ppss-context 'paren)
(goto-char (1+ (point-marker))))
(point-marker)))
(let ((paragraph-start "\f\\|[ \t]*$")
;; Only expand in code.
:enable-function (lambda ()
(and
- (not (nth 8 (syntax-ppss)))
+ (not (or (python-info-ppss-context 'string)
+ (python-info-ppss-context 'comment)))
python-skeleton-autoinsert)))
(defmacro python-skeleton-define (name doc &rest skel)
(ppss (syntax-ppss))
(help (when (and input
(not (string= input (concat current-defun ".")))
- (eq nil (nth 3 ppss))
- (eq nil (nth 4 ppss)))
+ (not (or (python-info-ppss-context 'string ppss)
+ (python-info-ppss-context 'comment ppss))))
(when (string-match (concat
(regexp-quote (concat current-defun "."))
"self\\.") input)
(save-excursion
(let ((innermost-paren (progn
(goto-char (line-end-position))
- (nth 1 (syntax-ppss)))))
+ (python-info-ppss-context 'paren))))
(when (and innermost-paren
(and (<= (line-beginning-position) innermost-paren)
(>= (line-end-position) innermost-paren)))
(looking-at (python-rx open-paren (* space) line-end)))))
(save-excursion
(back-to-indentation)
- (nth 1 (syntax-ppss)))))
+ (python-info-ppss-context 'paren))))
(defun python-info-block-continuation-line-p ()
"Return non-nil if current line is a continuation of a block."
assignment-operator
not-simple-operator)
(line-end-position) t)
- (not (syntax-ppss-context (syntax-ppss))))))
+ (not (or (python-info-ppss-context 'string)
+ (python-info-ppss-context 'comment))))))
(point-marker))))
+(defun python-info-ppss-context (type &optional syntax-ppss)
+ "Return non-nil if point is on TYPE using SYNTAX-PPSS.
+TYPE can be 'comment, 'string or 'parent. It returns the start
+character address of the specified TYPE."
+ (let ((ppss (or syntax-ppss (syntax-ppss))))
+ (case type
+ ('comment
+ (and (nth 4 ppss)
+ (nth 8 ppss)))
+ ('string
+ (nth 8 ppss))
+ ('paren
+ (nth 1 ppss))
+ (t nil))))
+
\f
;;;###autoload
(define-derived-mode python-mode fundamental-mode "Python"