Function: python-nav--forward-sexp
python-nav--forward-sexp is a byte-compiled function defined in
python.el.gz.
Signature
(python-nav--forward-sexp &optional DIR SAFE SKIP-PARENS-P)
Documentation
Move to forward sexp.
With positive optional argument DIR direction move forward, else backwards. When optional argument SAFE is non-nil do not throw errors when at end of sexp, skip it instead. With optional argument SKIP-PARENS-P force sexp motion to ignore parenthesized expressions when looking at them in either direction.
Source Code
;; Defined in /usr/src/emacs/lisp/progmodes/python.el.gz
(defun python-nav--forward-sexp (&optional dir safe skip-parens-p)
"Move to forward sexp.
With positive optional argument DIR direction move forward, else
backwards. When optional argument SAFE is non-nil do not throw
errors when at end of sexp, skip it instead. With optional
argument SKIP-PARENS-P force sexp motion to ignore parenthesized
expressions when looking at them in either direction."
(setq dir (or dir 1))
(unless (= dir 0)
(let* ((forward-p (if (> dir 0)
(and (setq dir 1) t)
(and (setq dir -1) nil)))
(context-type (python-syntax-context-type)))
(cond
((memq context-type '(string comment))
;; Inside of a string, get out of it.
(let ((forward-sexp-function))
(forward-sexp dir)))
((and (not skip-parens-p)
(or (eq context-type 'paren)
(if forward-p
(eq (syntax-class (syntax-after (point)))
(car (string-to-syntax "(")))
(eq (syntax-class (syntax-after (1- (point))))
(car (string-to-syntax ")"))))))
;; Inside a paren or looking at it, lisp knows what to do.
(if safe
(python-nav--lisp-forward-sexp-safe dir)
(python-nav--lisp-forward-sexp dir)))
(t
;; This part handles the lispy feel of
;; `python-nav-forward-sexp'. Knowing everything about the
;; current context and the context of the next sexp tries to
;; follow the lisp sexp motion commands in a symmetric manner.
(let* ((context
(cond
((python-info-beginning-of-block-p) 'block-start)
((python-info-end-of-block-p) 'block-end)
((python-info-beginning-of-statement-p) 'statement-start)
((python-info-end-of-statement-p) 'statement-end)))
(next-sexp-pos
(save-excursion
(if safe
(python-nav--lisp-forward-sexp-safe dir)
(python-nav--lisp-forward-sexp dir))
(point)))
(next-sexp-context
(save-excursion
(goto-char next-sexp-pos)
(cond
((python-info-beginning-of-block-p) 'block-start)
((python-info-end-of-block-p) 'block-end)
((python-info-beginning-of-statement-p) 'statement-start)
((python-info-end-of-statement-p) 'statement-end)
((python-info-statement-starts-block-p) 'starts-block)
((python-info-statement-ends-block-p) 'ends-block)))))
(if forward-p
(cond ((and (not (eobp))
(python-info-current-line-empty-p))
(python-util-forward-comment dir)
(python-nav--forward-sexp dir safe skip-parens-p))
((eq context 'block-start)
(python-nav-end-of-block))
((eq context 'statement-start)
(python-nav-end-of-statement))
((and (memq context '(statement-end block-end))
(eq next-sexp-context 'ends-block))
(goto-char next-sexp-pos)
(python-nav-end-of-block))
((and (memq context '(statement-end block-end))
(eq next-sexp-context 'starts-block))
(goto-char next-sexp-pos)
(python-nav-end-of-block))
((memq context '(statement-end block-end))
(goto-char next-sexp-pos)
(python-nav-end-of-statement))
(t (goto-char next-sexp-pos)))
(cond ((and (not (bobp))
(python-info-current-line-empty-p))
(python-util-forward-comment dir)
(python-nav--forward-sexp dir safe skip-parens-p))
((eq context 'block-end)
(python-nav-beginning-of-block))
((eq context 'statement-end)
(python-nav-beginning-of-statement))
((and (memq context '(statement-start block-start))
(eq next-sexp-context 'starts-block))
(goto-char next-sexp-pos)
(python-nav-beginning-of-block))
((and (memq context '(statement-start block-start))
(eq next-sexp-context 'ends-block))
(goto-char next-sexp-pos)
(python-nav-beginning-of-block))
((memq context '(statement-start block-start))
(goto-char next-sexp-pos)
(python-nav-beginning-of-statement))
(t (goto-char next-sexp-pos))))))))))