From b15e880041f5aacb12de0959c79d60e6065188fd Mon Sep 17 00:00:00 2001 From: =?utf8?q?Fabi=C3=A1n=20Ezequiel=20Gallina?= Date: Thu, 17 May 2012 00:03:43 -0300 Subject: [PATCH] `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' --- lisp/progmodes/python.el | 55 ++++++++++++++++++++++++---------------- 1 file 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." (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))) -- 2.39.2