summaryrefslogtreecommitdiff
path: root/lisp/progmodes
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina <fgallina@gnu.org>2012-10-08 23:07:26 -0300
committerFabián Ezequiel Gallina <fgallina@gnu.org>2012-10-08 23:07:26 -0300
commita4ff7fe1452e56d2c11ca31652bd145868e87e17 (patch)
treea5147d1d065e92aa928dab125bf3134002bf9ebc /lisp/progmodes
parent24517d82a98eaeb33f44854e72bfbf342bf24ea4 (diff)
downloademacs-a4ff7fe1452e56d2c11ca31652bd145868e87e17.tar.gz
Implemented `backward-up-list'-like navigation.
* progmodes/python.el (python-nav-up-list) (python-nav-backward-up-list): New functions. (python-mode-map): Define substitute key for backward-up-list to python-nav-backward-up-list.
Diffstat (limited to 'lisp/progmodes')
-rw-r--r--lisp/progmodes/python.el64
1 files changed, 64 insertions, 0 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 0bde8ce3334..5bf64c18f99 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -235,6 +235,9 @@
(substitute-key-definition 'forward-sentence
'python-nav-forward-block
map global-map)
+ (substitute-key-definition 'backward-up-list
+ 'python-nav-backward-up-list
+ map global-map)
(define-key map "\C-c\C-j" 'imenu)
;; Indent specific
(define-key map "\177" 'python-indent-dedent-line-backspace)
@@ -1409,6 +1412,67 @@ move backward N times."
(python-nav--backward-sexp)
(setq arg (1+ arg))))
+(defun python-nav--up-list (&optional dir)
+ "Internal implementation of `python-nav-up-list'.
+DIR is always 1 or -1 and comes sanitized from
+`python-nav-up-list' calls."
+ (let ((context (python-syntax-context-type))
+ (forward-p (> dir 0)))
+ (cond
+ ((memq context '(string comment)))
+ ((eq context 'paren)
+ (let ((forward-sexp-function))
+ (up-list dir)))
+ ((and forward-p (python-info-end-of-block-p))
+ (let ((parent-end-pos
+ (save-excursion
+ (let ((indentation (and
+ (python-nav-beginning-of-block)
+ (current-indentation))))
+ (while (and indentation
+ (> indentation 0)
+ (>= (current-indentation) indentation)
+ (python-nav-backward-block)))
+ (python-nav-end-of-block)))))
+ (and (> (or parent-end-pos (point)) (point))
+ (goto-char parent-end-pos))))
+ (forward-p (python-nav-end-of-block))
+ ((and (not forward-p)
+ (> (current-indentation) 0)
+ (python-info-beginning-of-block-p))
+ (let ((prev-block-pos
+ (save-excursion
+ (let ((indentation (current-indentation)))
+ (while (and (python-nav-backward-block)
+ (> (current-indentation) indentation))))
+ (point))))
+ (and (> (point) prev-block-pos)
+ (goto-char prev-block-pos))))
+ ((not forward-p) (python-nav-beginning-of-block)))))
+
+(defun python-nav-up-list (&optional arg)
+ "Move forward out of one level of parentheses (or blocks).
+With ARG, do this that many times.
+A negative argument means move backward but still to a less deep spot.
+This command assumes point is not in a string or comment."
+ (interactive "^p")
+ (or arg (setq arg 1))
+ (while (> arg 0)
+ (python-nav--up-list 1)
+ (setq arg (1- arg)))
+ (while (< arg 0)
+ (python-nav--up-list -1)
+ (setq arg (1+ arg))))
+
+(defun python-nav-backward-up-list (&optional arg)
+ "Move backward out of one level of parentheses (or blocks).
+With ARG, do this that many times.
+A negative argument means move backward but still to a less deep spot.
+This command assumes point is not in a string or comment."
+ (interactive "^p")
+ (or arg (setq arg 1))
+ (python-nav-up-list (- arg)))
+
;;; Shell integration