Skip to content
Browse files

Include a web cache ahead-of-time.

  • Loading branch information...
1 parent e48fcc4 commit dd1e9e16e4de3d62470c9c6cb815dbc385517b3c @skeeto committed Nov 18, 2011
View
75 java-docs.el
@@ -74,18 +74,36 @@
(if ido-mode 'ido-completing-read 'completing-read)
"Function used when performing a minibuffer read.")
-(defvar java-docs-current-root nil
- "Current root being indexed. Used to determine full class name.")
-
(defvar java-docs-cache-version "-v3"
"Cache version, so we don't load the wrong cache.")
+(defvar java-docs-home (file-name-directory load-file-name)
+ "The location of the java-docs source file.")
+
+(defvar java-docs-current-root nil
+ "Current root being indexed. Used to determine full class name.")
+
(defun java-docs (&rest dirs)
"Set the Javadoc search path to DIRS and index them."
(let ((list (mapcar (lambda (dir) (expand-file-name (concat dir "/"))) dirs)))
(dolist (java-docs-current-root (remove-if 'java-docs-loadedp list))
(message java-docs-current-root)
(java-docs-add java-docs-current-root)))
+ (java-docs-sort))
+
+(defun java-docs-web (&rest urls)
+ "Load pre-cached web indexes for URLS."
+ (dolist (url (remove-if 'java-docs-loadedp urls))
+ (let ((cache-file (concat java-docs-home "webcache/"
+ (java-docs-hash-name url))))
+ (if (file-exists-p cache-file)
+ (progn
+ (java-docs-load-cache cache-file)
+ (java-docs-sort))
+ (error "No cache for %s" url)))))
+
+(defun java-docs-sort ()
+ "Sort the loaded indexes."
(setq java-docs-class-list
(sort* java-docs-class-list '< :key 'length))
(setq java-docs-short-class-list
@@ -103,17 +121,22 @@
(setq java-docs-loaded nil)
(setq java-docs-index (make-hash-table :test 'equal)))
+(defun java-docs-hash-name (dir)
+ "Get the cache hashed name for DIR. This is basically an MD5
+hash plus version info."
+ (concat (md5 dir) java-docs-cache-version
+ (if java-docs-compress-cache ".gz" "")))
+
(defun java-docs-add (dir)
"Add directory to directory list and either index or fetch the cache."
(add-to-list 'java-docs-loaded dir)
- (let ((cache-name (concat (md5 dir) java-docs-cache-version
- (if java-docs-compress-cache ".gz" "")))
+ (let ((cache-file (concat java-docs-cache-dir "/" (java-docs-hash-name dir)))
(hash (make-hash-table :test 'equal)))
(if (and java-docs-enable-cache
- (file-exists-p (concat java-docs-cache-dir "/" cache-name)))
- (java-docs-load-cache cache-name)
+ (file-exists-p cache-file))
+ (java-docs-load-cache cache-file)
(java-docs-index dir hash)
- (java-docs-save-cache cache-name dir hash)
+ (java-docs-save-cache cache-file dir hash)
(java-docs-add-hash hash))))
(defun java-docs-short-name (fullclass)
@@ -127,26 +150,25 @@
(maphash (lambda (k v) (setq keys (cons k keys))) hash)
keys))
-(defun java-docs-load-cache (cache-name)
+(defun java-docs-load-cache (cache-file)
"Load a cache from disk."
- (let ((file (concat java-docs-cache-dir "/" cache-name)))
- (with-current-buffer (find-file-noselect file)
- (goto-char (point-min))
- (let ((hash (read (current-buffer))))
- (java-docs-add-hash hash)
- (setq java-docs-class-list
- (nconc java-docs-class-list (hash-table-keys hash))))
- (kill-buffer))))
-
-(defun java-docs-save-cache (cache-name dir hash)
+ (with-current-buffer (find-file-noselect cache-file)
+ (goto-char (point-min))
+ (let ((hash (read (current-buffer))))
+ (java-docs-add-hash hash)
+ (setq java-docs-class-list
+ (nconc java-docs-class-list (hash-table-keys hash))))
+ (kill-buffer)))
+
+(defun java-docs-save-cache (cache-file dir hash)
"Save a cache to the disk."
(when java-docs-enable-cache
(if (not (file-exists-p java-docs-cache-dir))
(make-directory java-docs-cache-dir))
(with-temp-buffer
(insert ";; " dir "\n\n")
(insert (prin1-to-string hash))
- (write-file (concat java-docs-cache-dir "/" cache-name)))))
+ (write-file cache-file))))
(defun java-docs-add-hash (hash)
"Combine HASH into the main index hash."
@@ -196,11 +218,22 @@
(defun java-docs-lookup (name)
"Lookup based on class name."
- (interactive (list (java-docs-completing-read)))
+ (interactive
+ (progn
+ (unless (java-docs-core-indexed-p)
+ (ignore-errors ; Provide *something* useful, if needed
+ (java-docs-web "http://download.oracle.com/javase/6/docs/api/")))
+ (list (java-docs-completing-read))))
(let ((file (gethash name java-docs-index)))
(if file
(browse-url file))))
+(defun java-docs-core-indexed-p ()
+ "Return t if the JRE Javadoc has been indexed. The class
+java.net.URL is used for this test, since it's simple and should
+always be there."
+ (member "java.net.URL" java-docs-class-list))
+
;; Insert import functions
(defvar java-docs-import-regexp "^import "
View
3 webcache/b5e09576051076d6d1797b698e5d924f-v3
3 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
View
BIN webcache/b5e09576051076d6d1797b698e5d924f-v3.gz
Binary file not shown.

0 comments on commit dd1e9e1

Please sign in to comment.
Something went wrong with that request. Please try again.