]> git.eshelyaron.com Git - emacs.git/commitdiff
`python-nav-list-defun-positions' now caches defuns positions (#75)
authorFabián Ezequiel Gallina <fgallina@cuca>
Thu, 17 May 2012 03:03:43 +0000 (00:03 -0300)
committerFabián Ezequiel Gallina <fgallina@gnu.org>
Thu, 17 May 2012 03:03:43 +0000 (00:03 -0300)
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'

lisp/progmodes/python.el

index 71c5453ee2d491548bd8d060b95dfde1f01f1aa0..4654143a160e730259d0d97f5442868c1d68154e 100644 (file)
@@ -1134,30 +1134,39 @@ With negative argument, move backward repeatedly to start of sentence."
     (forward-line -1)
     (setq arg (1+ arg))))
 
-(defun python-nav-list-defun-positions (&optional include-type)
+(defvar python-nav-list-defun-positions-cache nil)
+(make-variable-buffer-local 'python-nav-list-defun-positions-cache)
+
+(defun python-nav-list-defun-positions (&optional include-type rescan)
   "Make an Alist of defun names and point markers for current buffer.
 When optional argument INCLUDE-TYPE is non-nil the type is
-included the defun name."
-  (let ((defs))
-    (save-restriction
-      (widen)
-      (save-excursion
-        (goto-char (point-max))
-        (while (re-search-backward python-nav-beginning-of-defun-regexp nil t)
-          (when (and (not (python-info-ppss-context 'string))
-                     (not (python-info-ppss-context 'comment))
-                     (not (python-info-ppss-context 'parent)))
-            (add-to-list
-             'defs (cons
-                    (python-info-current-defun include-type)
-                    (point-marker)))))
-        defs))))
-
-(defun python-nav-read-defun ()
+included the defun name.  With optional argument RESCAN the
+`python-nav-list-defun-positions-cache' is invalidated and the
+list of defun is regenerated again."
+  (if (and python-nav-list-defun-positions-cache (not rescan))
+      python-nav-list-defun-positions-cache
+    (let ((defs))
+      (save-restriction
+        (widen)
+        (save-excursion
+          (goto-char (point-max))
+          (while (re-search-backward python-nav-beginning-of-defun-regexp nil t)
+            (when (and (not (python-info-ppss-context 'string))
+                       (not (python-info-ppss-context 'comment))
+                       (not (python-info-ppss-context 'parent)))
+              (add-to-list
+               'defs (cons
+                      (python-info-current-defun include-type)
+                      (point-marker)))))
+          (setq python-nav-list-defun-positions-cache defs))))))
+
+(defun python-nav-read-defun (&optional rescan)
   "Read a defun name of current buffer and return its point marker.
 A cons cell with the form (DEFUN-NAME . POINT-MARKER) is returned
-when defun is completed, else nil."
-  (let ((defs (python-nav-list-defun-positions)))
+when defun is completed, else nil. With optional argument RESCAN
+forces `python-nav-list-defun-positions' to invalidate its
+cache."
+  (let ((defs (python-nav-list-defun-positions nil rescan)))
     (minibuffer-with-setup-hook
         (lambda ()
           (setq minibuffer-completion-table (mapcar 'car defs)))
@@ -1169,9 +1178,11 @@ when defun is completed, else nil."
           (assoc-string stringdef defs))))))
 
 (defun python-nav-jump-to-defun (def)
-  "Jump to the definition of DEF in current file."
+  "Jump to the definition of DEF in current file.
+Locations are cached; use a C-u prefix argument to force a
+rescan."
   (interactive
-   (list (python-nav-read-defun)))
+   (list (python-nav-read-defun current-prefix-arg)))
   (when (not (called-interactively-p 'interactive))
     (setq def (assoc-string def (python-nav-list-defun-positions))))
   (let ((def-marker (cdr def)))