Separated caches from nodes.
[elpher.git] / elpher.el
index fc18741..63a1239 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -29,7 +29,7 @@
 ;; Elpher aims to provide a full-featured gopher client for GNU Emacs.
 ;; It supports:
 
-;; - intuitive keyboard and mouse-driven browsing,
+;; - intuitive keyboard and mouse-driven interface,
 ;; - caching of visited sites (both content and cursor position),
 ;; - pleasant and configurable colouring of Gopher directories,
 ;; - direct visualisation of image files,
@@ -87,7 +87,7 @@
          "i\tfake\tfake\t1"
          "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
          "i\tfake\tfake\t1"
-         "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
+         "1Floodgap Systems Gopher Server\t/\tgopher.floodgap.com\t70"
          "i\tfake\tfake\t1"
          "iAlternatively, select the following item and enter some\tfake\tfake\t1"
          "isearch terms:\tfake\tfake\t1"
     (?g elpher-get-image-node "im" elpher-image)
     (?p elpher-get-image-node "im" elpher-image)
     (?I elpher-get-image-node "im" elpher-image)
-    (?h elpher-get-url-node "W" elpher-url))
+    (?h elpher-get-url-node "W" elpher-url)
+    (bookmarks elpher-get-bookmarks-node "#" elpher-index)
+    (start elpher-get-start-node "#" elpher-index))
   "Association list from types to getters, margin codes and index faces.")
 
 
@@ -195,8 +197,10 @@ use as the start page."
 
 ;; Address
 
-(defun elpher-make-address (type selector host port)
-  "Create an address of a gopher object with TYPE, SELECTOR, HOST and PORT."
+(defun elpher-make-address (type &optional selector host port)
+  "Create an address of a gopher object with TYPE, SELECTOR, HOST and PORT.
+Although selector host and port are optional, they are only omitted for
+special address types, such as 'start for the start page."
   (list type selector host port))
 
 (defun elpher-address-type (address)
@@ -217,17 +221,14 @@ use as the start page."
 
 ;; Node
 
-(defun elpher-make-node (display-string parent address &optional content pos)
+(defun elpher-make-node (display-string parent address)
   "Create a node in the gopher page hierarchy.
 
 DISPLAY-STRING records the display string used for the page.
 
 PARENT specifies the parent of the node, and ADDRESS specifies the
-address of the gopher page.
-
-The optional arguments CONTENT and POS can be used to fill the cached
-content and cursor position fields of the node."
-  (list display-string parent address content pos))
+address of the gopher page."
+  (list display-string parent address))
 
 (defun elpher-node-display-string (node)
   "Retrieve the display string of NODE."
@@ -241,21 +242,26 @@ content and cursor position fields of the node."
   "Retrieve the address of NODE."
   (elt node 2))
 
-(defun elpher-node-content (node)
-  "Retrieve the cached content of NODE, or nil if none exists."
-  (elt node 3))
+;; Cache
+
+(defvar elpher-content-cache (make-hash-table :test 'equal))
+(defvar elpher-pos-cache (make-hash-table :test 'equal))
+
+(defun elpher-get-cached-content (address)
+  "Retrieve the cached content for ADDRESS, or nil if none exists."
+  (gethash address elpher-content-cache))
 
-(defun elpher-node-pos (node)
-  "Retrieve the cached cursor position for NODE, or nil if none exists."
-  (elt node 4))
+(defun elpher-cache-content (address content)
+  "Set the content cache for ADDRESS to CONTENT."
+  (puthash address content elpher-content-cache))
 
-(defun elpher-set-node-content (node content)
-  "Set the content cache of NODE to CONTENT."
-  (setcar (nthcdr 3 node) content))
+(defun elpher-get-cached-pos (address)
+  "Retrieve the cached cursor position for ADDRESS, or nil if none exists."
+  (gethash address elpher-pos-cache))
 
-(defun elpher-set-node-pos (node pos)
-  "Set the cursor position cache of NODE to POS."
-  (setcar (nthcdr 4 node) pos))
+(defun elpher-cache-pos (address pos)
+  "Set the cursor position cache for ADDRESS to POS."
+  (puthash address pos elpher-pos-cache))
 
 ;; Node graph traversal
 
@@ -266,17 +272,10 @@ content and cursor position fields of the node."
   (elpher-save-pos)
   (elpher-process-cleanup)
   (setq elpher-current-node node)
-  (with-current-buffer "*elpher*"
-    (setq header-line-format "hello"))
-    ;; (let ((inhibit-read-only t))
-
-    ;; (force-mode-line-update))
   (if getter
       (funcall getter)
     (let* ((address (elpher-node-address node))
-           (type (if address
-                     (elpher-address-type address)
-                   ?1)))
+           (type (elpher-address-type address)))
       (funcall (car (alist-get type elpher-type-map))))))
 
 (defun elpher-visit-parent-node ()
@@ -287,17 +286,17 @@ content and cursor position fields of the node."
       
 (defun elpher-reload-current-node ()
   "Reload the current node, discarding any existing cached content."
-  (elpher-set-node-content elpher-current-node nil)
+  (elpher-cache-content (elpher-node-address elpher-current-node) nil)
   (elpher-visit-node elpher-current-node))
 
 (defun elpher-save-pos ()
   "Save the current position of point to the current node."
   (when elpher-current-node
-    (elpher-set-node-pos elpher-current-node (point))))
+    (elpher-cache-pos (elpher-node-address elpher-current-node) (point))))
 
 (defun elpher-restore-pos ()
   "Restore the position of point to that cached in the current node."
-  (let ((pos (elpher-node-pos elpher-current-node)))
+  (let ((pos (elpher-get-cached-pos (elpher-node-address elpher-current-node))))
     (if pos
         (goto-char pos)
       (goto-char (point-min)))))
@@ -334,8 +333,7 @@ content and cursor position fields of the node."
   "Insert the index corresponding to STRING into the current buffer."
   ;; Should be able to split directly on CRLF, but some non-conformant
   ;; LF-only servers sadly exist, hence the following.
-  (let ((str-processed (elpher-preprocess-text-response string))
-        formatting-error)
+  (let ((str-processed (elpher-preprocess-text-response string)))
     (dolist (line (split-string str-processed "\n"))
       (unless (= (length line) 0)
         (let* ((type (elt line 0))
@@ -346,11 +344,7 @@ content and cursor position fields of the node."
                (port (if (elt fields 3)
                          (string-to-number (elt fields 3))
                        nil)))
-          (if (< (length fields) 4)
-              (setq formatting-error t))
-          (elpher-insert-index-record display-string type selector host port))))
-    (if formatting-error
-        (display-warning :warning "One or more badly formatted index records detected."))))
+          (elpher-insert-index-record display-string type selector host port))))))
 
 (defun elpher-insert-margin (&optional type-name)
   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
@@ -383,7 +377,7 @@ and PORT."
         (type-map-entry (alist-get type elpher-type-map)))
     (if type-map-entry
         (let* ((margin-code (cadr type-map-entry))
-               (face (caddr type-map-entry))
+               (face (elt type-map-entry 2))
                (node (elpher-make-node display-string elpher-current-node address)))
           (elpher-insert-margin margin-code)
           (insert-text-button display-string
@@ -440,31 +434,24 @@ The result is stored as a string in the variable ‘elpher-selector-string’."
 
 (defun elpher-get-index-node ()
   "Getter which retrieves the current node contents as an index."
-  (let ((content (elpher-node-content elpher-current-node))
-        (address (elpher-node-address elpher-current-node)))
+  (let* ((address (elpher-node-address elpher-current-node))
+         (content (elpher-get-cached-content address)))
     (if content
         (progn
           (elpher-with-clean-buffer
            (insert content)
            (elpher-restore-pos)))
-      (if address
-          (progn
-            (elpher-with-clean-buffer
-             (insert "LOADING DIRECTORY..."))
-            (elpher-get-selector address
-                                  (lambda (proc event)
-                                    (unless (string-prefix-p "deleted" event)
-                                      (elpher-with-clean-buffer
-                                       (elpher-insert-index elpher-selector-string)
-                                       (elpher-restore-pos)
-                                       (elpher-set-node-content elpher-current-node
-                                                                (buffer-string)))))))
-        (progn
-          (elpher-with-clean-buffer
-           (elpher-insert-index elpher-start-index)
-           (elpher-restore-pos)
-           (elpher-set-node-content elpher-current-node
-                                    (buffer-string))))))))
+      (elpher-with-clean-buffer
+       (insert "LOADING DIRECTORY..."))
+      (elpher-get-selector address
+                           (lambda (proc event)
+                             (unless (string-prefix-p "deleted" event)
+                               (elpher-with-clean-buffer
+                                (elpher-insert-index elpher-selector-string)
+                                (elpher-restore-pos)
+                                (elpher-cache-content
+                                 (elpher-node-address elpher-current-node)
+                                 (buffer-string)))))))))
 
 ;; Text retrieval
 
@@ -521,8 +508,8 @@ calls, as is necessary if the match is performed by `string-match'."
 
 (defun elpher-get-text-node ()
   "Getter which retrieves the current node contents as a text document."
-  (let ((content (elpher-node-content elpher-current-node))
-        (address (elpher-node-address elpher-current-node)))
+  (let* ((address (elpher-node-address elpher-current-node))
+         (content (elpher-get-cached-content address)))
     (if content
         (progn
           (elpher-with-clean-buffer
@@ -539,15 +526,16 @@ calls, as is necessary if the match is performed by `string-match'."
                                             (elpher-preprocess-text-response
                                              elpher-selector-string)))
                                    (elpher-restore-pos)
-                                   (elpher-set-node-content elpher-current-node
-                                                            (buffer-string))))))))))
+                                   (elpher-cache-content
+                                    (elpher-node-address elpher-current-node)
+                                    (buffer-string))))))))))
 
 ;; Image retrieval
 
 (defun elpher-get-image-node ()
   "Getter which retrieves the current node contents as an image to view."
-  (let ((content (elpher-node-content elpher-current-node))
-        (address (elpher-node-address elpher-current-node)))
+  (let* ((address (elpher-node-address elpher-current-node))
+         (content (elpher-get-cached-content address)))
     (if content
         (progn
           (elpher-with-clean-buffer
@@ -569,17 +557,18 @@ calls, as is necessary if the match is performed by `string-match'."
                                         (insert-image image)
                                         (elpher-restore-pos))
                                        (if elpher-cache-images
-                                           (elpher-set-node-content elpher-current-node
-                                                                    image)))))))
+                                           (elpher-cache-content
+                                            (elpher-node-address elpher-current-node)
+                                            image)))))))
         (elpher-get-node-download)))))
 
 ;; Search retrieval
 
 (defun elpher-get-search-node ()
   "Getter which submits a search query to the address of the current node."
-  (let ((content (elpher-node-content elpher-current-node))
-        (address (elpher-node-address elpher-current-node))
-        (aborted t))
+  (let* ((address (elpher-node-address elpher-current-node))
+         (content (elpher-get-cached-content address))
+         (aborted t))
     (if content
         (progn
           (elpher-with-clean-buffer
@@ -602,8 +591,9 @@ calls, as is necessary if the match is performed by `string-match'."
                                       (elpher-with-clean-buffer
                                        (elpher-insert-index elpher-selector-string))
                                       (goto-char (point-min))
-                                      (elpher-set-node-content elpher-current-node
-                                                                (buffer-string))))))
+                                      (elpher-cache-content
+                                       (elpher-node-address elpher-current-node)
+                                       (buffer-string))))))
         (if aborted
             (elpher-visit-parent-node))))))
 
@@ -611,8 +601,7 @@ calls, as is necessary if the match is performed by `string-match'."
 
 (defun elpher-get-node-raw ()
   "Getter which retrieves the raw server response for the current node."
-  (let* ((content (elpher-node-content elpher-current-node))
-         (address (elpher-node-address elpher-current-node)))
+  (let ((address (elpher-node-address elpher-current-node)))
     (elpher-with-clean-buffer
      (insert "LOADING RAW SERVER RESPONSE..."))
     (if address
@@ -675,6 +664,14 @@ calls, as is necessary if the match is performed by `string-match'."
     (elpher-visit-parent-node)
     (telnet host port)))
 
+;; Start node retrieval
+
+(defun elpher-get-start-node ()
+  "Getter which displays the start page."
+  (elpher-with-clean-buffer
+   (elpher-insert-index elpher-start-index)
+   (elpher-restore-pos)))
+  
 
 ;;; Bookmarks
 ;;
@@ -799,13 +796,13 @@ Beware that this completely replaces the existing contents of the file."
                (elpher-make-node-from-matched-url elpher-current-node
                                                   host-or-url)
              (let ((selector (read-string "Selector (default none): " nil nil ""))
-                   (port (string-to-number (read-string "Port (default 70): "
-                                                        nil nil 70))))
+                   (port-string (read-string "Port (default 70): " nil nil "70")))
                (elpher-make-node (concat "gopher://" host-or-url
-                                         ":" port
+                                         ":" port-string
                                          "/1" selector)
                                  elpher-current-node
-                                 (elpher-make-address ?1 selector host-or-url port)))))))
+                                 (elpher-make-address ?1 selector host-or-url
+                                                      (string-to-number port-string))))))))
     (switch-to-buffer "*elpher*")
     (elpher-visit-node node)))
 
@@ -859,7 +856,7 @@ Beware that this completely replaces the existing contents of the file."
       (setq b (next-button (button-start b))))
     link-map))
 
-(defun elpher-menu ()
+(defun elpher-jump ()
   "Select a directory entry by name.  Similar to the info browser (m)enu command."
   (interactive)
   (let* ((link-map (elpher-build-link-map)))
@@ -875,8 +872,9 @@ Beware that this completely replaces the existing contents of the file."
 (defun elpher-root-dir ()
   "Visit root of current server."
   (interactive)
-  (let ((address (elpher-node-address elpher-current-node)))
-    (if address
+  (let* ((address (elpher-node-address elpher-current-node))
+         (host (elpher-address-host address)))
+    (if host
         (let ((host (elpher-address-host address))
               (selector (elpher-address-selector address))
               (port (elpher-address-port address)))
@@ -889,7 +887,7 @@ Beware that this completely replaces the existing contents of the file."
                                    elpher-current-node
                                    root-address)))
             (error "Already at root directory of current server")))
-      (error "Command invalid for Elpher start page"))))
+      (error "Command invalid for this page"))))
 
 (defun elpher-info-node (node)
   "Display information on NODE."
@@ -962,7 +960,7 @@ Beware that this completely replaces the existing contents of the file."
     (define-key map (kbd "R") 'elpher-reload)
     (define-key map (kbd "w") 'elpher-view-raw)
     (define-key map (kbd "d") 'elpher-download)
-    (define-key map (kbd "m") 'elpher-menu)
+    (define-key map (kbd "m") 'elpher-jump)
     (define-key map (kbd "i") 'elpher-info-link)
     (define-key map (kbd "I") 'elpher-info-current)
     (define-key map (kbd "c") 'elpher-copy-link-url)
@@ -979,7 +977,7 @@ Beware that this completely replaces the existing contents of the file."
         (kbd "R") 'elpher-reload
         (kbd "w") 'elpher-view-raw
         (kbd "d") 'elpher-download
-        (kbd "m") 'elpher-menu
+        (kbd "m") 'elpher-jump
         (kbd "i") 'elpher-info-link
         (kbd "I") 'elpher-info-current
         (kbd "c") 'elpher-copy-link-url
@@ -1009,8 +1007,7 @@ Beware that this completely replaces the existing contents of the file."
       (switch-to-buffer "*elpher*")
     (switch-to-buffer "*elpher*")
     (setq elpher-current-node nil)
-    (let ((start-node (elpher-make-node "Elpher Start Page"
-                                        nil elpher-start-address)))
+    (let ((start-node (elpher-make-node "Elpher Start Page" nil (elpher-make-address 'start))))
       (elpher-visit-node start-node)))
   "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.