aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2013-04-17 02:08:20 -0300
committerFabián Ezequiel Gallina2013-04-17 02:08:20 -0300
commit083850a6a195c5d536bd4cd344b5917b225597cc (patch)
tree8015b635f7d05add5ce355d04a06569e7a63798b /lisp/progmodes/python.el
parent619ed6e18a4231c9d9c8e50b21eb1e870e7e6853 (diff)
downloademacs-083850a6a195c5d536bd4cd344b5917b225597cc.tar.gz
emacs-083850a6a195c5d536bd4cd344b5917b225597cc.zip
New defun movement commands.
* lisp/progmodes/python.el (python-nav--syntactically) (python-nav--forward-defun, python-nav-backward-defun) (python-nav-forward-defun): New functions. * test/automated/python-tests.el (python-nav-backward-defun-1) (python-nav-forward-defun-1): New tests.
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el60
1 files changed, 60 insertions, 0 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index b0c00a309eb..1d7cf02ca5a 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1192,6 +1192,66 @@ Returns nil if point is not in a def or class."
1192 ;; Ensure point moves forward. 1192 ;; Ensure point moves forward.
1193 (and (> beg-pos (point)) (goto-char beg-pos))))) 1193 (and (> beg-pos (point)) (goto-char beg-pos)))))
1194 1194
1195(defun python-nav--syntactically (fn poscompfn &optional pos)
1196 "Move to point using FN ignoring non-code or paren context.
1197FN must take no arguments and could be used to set match-data.
1198POSCOMPFN is a two arguments function used to compare current and
1199previous point after it is moved using FN, this is normally a
1200less-than or greater-than comparison. Optional argument POS is
1201internally used in recursive calls and should not be explicitly
1202passed."
1203 (let* ((newpos
1204 (and (funcall fn)
1205 (save-match-data
1206 (and
1207 (not (python-syntax-context-type))
1208 (point-marker)))))
1209 (current-match-data (match-data)))
1210 (cond ((or (and (not pos) newpos)
1211 (and pos newpos (funcall poscompfn newpos pos)))
1212 (set-match-data current-match-data)
1213 (point-marker))
1214 ((and (not pos) (not newpos)) nil)
1215 (t (python-nav--syntactically
1216 fn poscompfn (point-marker))))))
1217
1218(defun python-nav--forward-defun (arg)
1219 "Internal implementation of python-nav-{backward,forward}-defun.
1220Uses ARG to define which function to call, and how many times
1221repeat it."
1222 (let ((found))
1223 (while (and (> arg 0)
1224 (setq found
1225 (python-nav--syntactically
1226 (lambda ()
1227 (re-search-forward
1228 python-nav-beginning-of-defun-regexp nil t))
1229 '>)))
1230 (setq arg (1- arg)))
1231 (while (and (< arg 0)
1232 (setq found
1233 (python-nav--syntactically
1234 (lambda ()
1235 (re-search-backward
1236 python-nav-beginning-of-defun-regexp nil t))
1237 '<)))
1238 (setq arg (1+ arg)))
1239 found))
1240
1241(defun python-nav-backward-defun (&optional arg)
1242 "Navigate to closer defun backward ARG times.
1243Unlikely `python-nav-beginning-of-defun' this doesn't care about
1244nested definitions."
1245 (interactive "^p")
1246 (python-nav--forward-defun (- (or arg 1))))
1247
1248(defun python-nav-forward-defun (&optional arg)
1249 "Navigate to closer defun forward ARG times.
1250Unlikely `python-nav-beginning-of-defun' this doesn't care about
1251nested definitions."
1252 (interactive "^p")
1253 (python-nav--forward-defun (or arg 1)))
1254
1195(defun python-nav-beginning-of-statement () 1255(defun python-nav-beginning-of-statement ()
1196 "Move to start of current statement." 1256 "Move to start of current statement."
1197 (interactive "^") 1257 (interactive "^")