Subject: bug#12456: 24.2.50; Completion in `Info-goto-node' (cross-manual jump)

From: Juri Linkov <[email-masked]>
Date: Sat, 15 Dec 2012 17:30:30 +0200

>>> * "(elisp)<TAB>" completes to all nodes in the elisp manual.
>>
>> Yes, that's the part that's not yet implemented.  Patch welcome,
>
> This can be implemented by this patch.  The remaining problem is
> that it's too slow because it doesn't cache completions of non-current
> manuals.  Perhaps `Info-build-node-completions' should use `Info-toc-nodes'
> that caches node names from all visited manuals.

Actually I realized that `Info-toc-nodes' can't be used because it
doesn't contain Info anchors whereas `Info-build-node-completions'
adds anchors along with references.

So to be able to cache completions in non-current manuals
requires adding a global variable `Info-file-completions'
like in the patch below:

=== modified file 'lisp/info.el'
--- lisp/info.el	2012-12-08 23:12:08 +0000
+++ lisp/info.el	2012-12-15 15:28:17 +0000
@@ -397,6 +395,10 @@ (defvar Info-tag-table-buffer nil
 (defvar Info-current-file-completions nil
   "Cached completion list for current Info file.")
 
+(defvar Info-file-completions nil
+  "Cached completion alist of visited Info files.
+Each element of the alist is (FILE . COMPLETIONS)")
+
 (defvar Info-file-supports-index-cookies nil
   "Non-nil if current Info file supports index cookies.")
 
@@ -1771,12 +1783,20 @@ (defun Info-read-node-name-1 (string pre
      (substring string 1)
      predicate
      code))
-   ;; If a file name was given, then any node is fair game.
-   ((string-match "\\`(" string)
-    (cond
-     ((eq code nil) string)
-     ((eq code t) nil)
-     (t t)))
+   ;; If a file name was given, complete nodes in the file.
+   ((string-match "\\`(\\([^)]+\\))" string)
+    (let ((file0 (match-string 0 string))
+	  (file1 (match-string 1 string))
+	  (node (substring string (match-end 0))))
+      (completion-table-with-context
+       file0
+       (apply-partially
+	(lambda (string pred action)
+	  (complete-with-action
+	   action
+	   (Info-build-node-completions (Info-find-file file1))
+	   string pred)))
+       node predicate code)))
    ;; Otherwise use Info-read-node-completion-table.
    (t (complete-with-action
        code Info-read-node-completion-table string predicate))))
@@ -1793,8 +1813,19 @@ (defun Info-read-node-name (prompt)
 	(Info-read-node-name prompt)
       nodename)))
 
-(defun Info-build-node-completions ()
+(defun Info-build-node-completions (&optional file)
+  (if file
+      (or (cdr (assoc file Info-file-completions))
+	  (with-temp-buffer
+	    (Info-mode)
+	    (Info-goto-node (format "(%s)Top" file))
+	    (Info-build-node-completions-1)
+	    (push (cons file Info-current-file-completions) Info-file-completions)
+	    Info-file-completions))
   (or Info-current-file-completions
+	(Info-build-node-completions-1))))
+
+(defun Info-build-node-completions-1 ()
       (let ((compl nil)
 	    ;; Bind this in case the user sets it to nil.
 	    (case-fold-search t)
@@ -1826,8 +1857,10 @@ (defun Info-build-node-completions ()
 		      (setq compl
 			    (cons (list (match-string-no-properties 1))
 				  compl))))))))
-	(setq compl (cons '("*") compl))
-	(set (make-local-variable 'Info-current-file-completions) compl))))
+    (setq compl (cons '("*") (nreverse compl)))
+    (set (make-local-variable 'Info-current-file-completions) compl)
+    compl))
+
 
 (defun Info-restore-point (hl)
   "If this node has been visited, restore the point value when we left."