aboutsummaryrefslogtreecommitdiffstats
path: root/lisp/progmodes/python.el
diff options
context:
space:
mode:
authorFabián Ezequiel Gallina2012-05-17 00:03:43 -0300
committerFabián Ezequiel Gallina2012-05-17 00:03:43 -0300
commitb15e880041f5aacb12de0959c79d60e6065188fd (patch)
treecd79d28ac324b272f13464c009745739f9ce9825 /lisp/progmodes/python.el
parent0d85f46527ddbe40510181c025a67bd819d208b3 (diff)
downloademacs-b15e880041f5aacb12de0959c79d60e6065188fd.tar.gz
emacs-b15e880041f5aacb12de0959c79d60e6065188fd.zip
`python-nav-list-defun-positions' now caches defuns positions (#75)
This is a simplified version of @dandavison pull request (thanks dan!) `python-nav-list-defun-positions' now uses `python-nav-list-defun-positions-cache' buffer local variable to store cached values of defun positions. `python-nav-jump-to-defun' now benefits from this new cache and if called with prefix argument it will invalidate it so new defuns are scanned. New Vars: + `python-nav-list-defun-positions-cache'
Diffstat (limited to 'lisp/progmodes/python.el')
-rw-r--r--lisp/progmodes/python.el55
1 files changed, 33 insertions, 22 deletions
diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el
index 71c5453ee2d..4654143a160 100644
--- a/lisp/progmodes/python.el
+++ b/lisp/progmodes/python.el
@@ -1134,30 +1134,39 @@ With negative argument, move backward repeatedly to start of sentence."
1134 (forward-line -1) 1134 (forward-line -1)
1135 (setq arg (1+ arg)))) 1135 (setq arg (1+ arg))))
1136 1136
1137(defun python-nav-list-defun-positions (&optional include-type) 1137(defvar python-nav-list-defun-positions-cache nil)
1138(make-variable-buffer-local 'python-nav-list-defun-positions-cache)
1139
1140(defun python-nav-list-defun-positions (&optional include-type rescan)
1138 "Make an Alist of defun names and point markers for current buffer. 1141 "Make an Alist of defun names and point markers for current buffer.
1139When optional argument INCLUDE-TYPE is non-nil the type is 1142When optional argument INCLUDE-TYPE is non-nil the type is
1140included the defun name." 1143included the defun name. With optional argument RESCAN the
1141 (let ((defs)) 1144`python-nav-list-defun-positions-cache' is invalidated and the
1142 (save-restriction 1145list of defun is regenerated again."
1143 (widen) 1146 (if (and python-nav-list-defun-positions-cache (not rescan))
1144 (save-excursion 1147 python-nav-list-defun-positions-cache
1145 (goto-char (point-max)) 1148 (let ((defs))
1146 (while (re-search-backward python-nav-beginning-of-defun-regexp nil t) 1149 (save-restriction
1147 (when (and (not (python-info-ppss-context 'string)) 1150 (widen)
1148 (not (python-info-ppss-context 'comment)) 1151 (save-excursion
1149 (not (python-info-ppss-context 'parent))) 1152 (goto-char (point-max))
1150 (add-to-list 1153 (while (re-search-backward python-nav-beginning-of-defun-regexp nil t)
1151 'defs (cons 1154 (when (and (not (python-info-ppss-context 'string))
1152 (python-info-current-defun include-type) 1155 (not (python-info-ppss-context 'comment))
1153 (point-marker))))) 1156 (not (python-info-ppss-context 'parent)))
1154 defs)))) 1157 (add-to-list
1155 1158 'defs (cons
1156(defun python-nav-read-defun () 1159 (python-info-current-defun include-type)
1160 (point-marker)))))
1161 (setq python-nav-list-defun-positions-cache defs))))))
1162
1163(defun python-nav-read-defun (&optional rescan)
1157 "Read a defun name of current buffer and return its point marker. 1164 "Read a defun name of current buffer and return its point marker.
1158A cons cell with the form (DEFUN-NAME . POINT-MARKER) is returned 1165A cons cell with the form (DEFUN-NAME . POINT-MARKER) is returned
1159when defun is completed, else nil." 1166when defun is completed, else nil. With optional argument RESCAN
1160 (let ((defs (python-nav-list-defun-positions))) 1167forces `python-nav-list-defun-positions' to invalidate its
1168cache."
1169 (let ((defs (python-nav-list-defun-positions nil rescan)))
1161 (minibuffer-with-setup-hook 1170 (minibuffer-with-setup-hook
1162 (lambda () 1171 (lambda ()
1163 (setq minibuffer-completion-table (mapcar 'car defs))) 1172 (setq minibuffer-completion-table (mapcar 'car defs)))
@@ -1169,9 +1178,11 @@ when defun is completed, else nil."
1169 (assoc-string stringdef defs)))))) 1178 (assoc-string stringdef defs))))))
1170 1179
1171(defun python-nav-jump-to-defun (def) 1180(defun python-nav-jump-to-defun (def)
1172 "Jump to the definition of DEF in current file." 1181 "Jump to the definition of DEF in current file.
1182Locations are cached; use a C-u prefix argument to force a
1183rescan."
1173 (interactive 1184 (interactive
1174 (list (python-nav-read-defun))) 1185 (list (python-nav-read-defun current-prefix-arg)))
1175 (when (not (called-interactively-p 'interactive)) 1186 (when (not (called-interactively-p 'interactive))
1176 (setq def (assoc-string def (python-nav-list-defun-positions)))) 1187 (setq def (assoc-string def (python-nav-list-defun-positions))))
1177 (let ((def-marker (cdr def))) 1188 (let ((def-marker (cdr def)))