Documented some functions.
[elpher.git] / elopher.el
index 4617d08..2d97313 100644 (file)
@@ -1,8 +1,8 @@
-;;; elopher.el --- gopher client
+;;; elopher.el --- elisp gopher client
 
 ;;; Commentary:
 
-;; Simple gopher client in elisp.
+;; An elisp gopher client.
 
 ;;; Code:
 
@@ -30,7 +30,8 @@
          "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
          "i - u: return to parent directory entry\tfake\tfake\t1"
          "i - g: go to a particular page\tfake\tfake\t1"
-         "i - r: reload current page\tfake\tfake\t1"
+         "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
+         "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
          "i - d: download directory entry under cursor\tfake\tfake\t1"
          "i - w: display the raw server response for the current page\tfake\tfake\t1"
          "i\tfake\tfake\t1"
          "i\tfake\tfake\t1"
          "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
          ".")
-   "\r\n"))
+   "\r\n")
+  "Source for elopher start page.")
 
 
 ;;; Customization group
 ;;
 
 (defgroup elopher nil
-  "A simple gopher client."
+  "A gopher client."
   :group 'applications)
 
-(defcustom elopher-index-face '(foreground-color . "cyan")
-  "Face used for index records."
-  :type '(face))
+(defface elopher-index
+  '((((background dark)) :foreground "deep sky blue")
+    (((background light)) :foreground "blue"))
+  "Face used for index records.")
 
-(defcustom elopher-text-face '(foreground-color . "white")
-  "Face used for text records."
-  :type '(face))
+(defface elopher-text
+  '((((background dark)) :foreground "white")
+    (((background light)) :weight bold))
+  "Face used for text records.")
 
-(defcustom elopher-info-face '(foreground-color . "gray")
-  "Face used for info records."
-  :type '(face))
+(defface elopher-info '()
+  "Face used for info records.")
 
-(defcustom elopher-image-face '(foreground-color . "green")
-  "Face used for image records."
-  :type '(face))
+(defface elopher-image
+  '((((background dark)) :foreground "green")
+    (t :foreground "dark green"))
+  "Face used for image records.")
 
-(defcustom elopher-search-face '(foreground-color . "orange")
-  "Face used for image records."
-  :type '(face))
+(defface elopher-search
+  '((((background light)) :foreground "orange")
+    (((background dark)) :foreground "dark orange"))
+  "Face used for search records.")
 
-(defcustom elopher-http-face '(foreground-color . "yellow")
-  "Face used for image records."
-  :type '(face))
+(defface elopher-url
+  '((((background dark)) :foreground "yellow")
+    (((background light)) :foreground "dark red"))
+  "Face used for url records.")
 
-(defcustom elopher-binary-face '(foreground-color . "magenta")
-  "Face used for image records."
-  :type '(face))
+(defface elopher-binary
+  '((t :foreground "magenta"))
+  "Face used for binary records.")
 
-(defcustom elopher-unknown-face '(foreground-color . "red")
-  "Face used for unknown record types."
-  :type '(face))
+(defface elopher-unknown
+  '((t :foreground "red"))
+  "Face used for unknown record types.")
+
+(defface elopher-margin-key
+  '((((background dark)) :foreground "white"))
+  "Face used for margin key.")
+
+(defface elopher-margin-brackets
+  '((t :foreground "blue"))
+  "Face used for brackets around margin key.")
 
 (defcustom elopher-open-urls-with-eww nil
   "If non-nil, open URL selectors using eww.
@@ -96,48 +110,69 @@ Otherwise, use the system browser via the BROWSE-URL function."
 ;; Address
 
 (defun elopher-make-address (selector host port)
+  "Create an address of a gopher object with SELECTOR, HOST and PORT."
   (list selector host port))
 
 (defun elopher-address-selector (address)
+  "Retrieve selector from ADDRESS."
   (car address))
 
 (defun elopher-address-host (address)
+  "Retrieve host from ADDRESS."
   (cadr address))
 
 (defun elopher-address-port (address)
+  "Retrieve port from ADDRESS."
   (caddr address))
 
 ;; Node
 
 (defun elopher-make-node (parent address getter &optional content pos)
+  "Create a node in the gopher page hierarchy.
+
+PARENT specifies the parent of the node, ADDRESS specifies the address of
+the gopher page, GETTER provides the getter function used to obtain this
+page.
+
+The optional arguments CONTENT and POS can be used to fill the cached
+content and cursor position fields of the node."
   (list parent address getter content pos))
 
 (defun elopher-node-parent (node)
+  "Retrieve the parent node of NODE."
   (elt node 0))
 
 (defun elopher-node-address (node)
+  "Retrieve the address of NODE."
   (elt node 1))
 
 (defun elopher-node-getter (node)
+  "Retrieve the preferred getter function of NODE."
   (elt node 2))
 
 (defun elopher-node-content (node)
+  "Retrieve the cached content of NODE, or nil if none exists."
   (elt node 3))
 
 (defun elopher-node-pos (node)
+  "Retrieve the cached cursor position for NODE, or nil if none exists."
   (elt node 4))
 
 (defun elopher-set-node-content (node content)
+  "Set the content cache of NODE to CONTENT."
   (setcar (nthcdr 3 node) content))
 
 (defun elopher-set-node-pos (node pos)
+  "Set the cursor position cache of NODE to POS."
   (setcar (nthcdr 4 node) pos))
 
 (defun elopher-save-pos ()
+  "Save the current position of point to the current node."
   (when elopher-current-node
     (elopher-set-node-pos elopher-current-node (point))))
 
 (defun elopher-restore-pos ()
+  "Restore the position of point to that cached in the current node."
   (let ((pos (elopher-node-pos elopher-current-node)))
     (if pos
         (goto-char pos)
@@ -148,6 +183,7 @@ Otherwise, use the system browser via the BROWSE-URL function."
 (defvar elopher-current-node)
 
 (defun elopher-visit-node (node &optional getter)
+  "Visit NODE using its own getter or GETTER, if non-nil."
   (elopher-save-pos)
   (elopher-process-cleanup)
   (setq elopher-current-node node)
@@ -156,11 +192,13 @@ Otherwise, use the system browser via the BROWSE-URL function."
     (funcall (elopher-node-getter node))))
 
 (defun elopher-visit-parent-node ()
+  "Visit the parent of the current node."
   (let ((parent-node (elopher-node-parent elopher-current-node)))
     (when parent-node
       (elopher-visit-node parent-node))))
       
 (defun elopher-reload-current-node ()
+  "Reload the current node, discarding any existing cached content."
   (elopher-set-node-content elopher-current-node nil)
   (elopher-visit-node elopher-current-node))
 
@@ -191,22 +229,23 @@ Otherwise, use the system browser via the BROWSE-URL function."
       (progn
         (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
                         (concat
-                         (propertize "[" 'face '(foreground-color . "blue"))
-                         (propertize type-name 'face '(foreground-color . "white"))
-                         (propertize "]" 'face '(foreground-color . "blue")))))
+                         (propertize "[" 'face 'elopher-margin-brackets)
+                         (propertize type-name 'face 'elopher-margin-key)
+                         (propertize "]" 'face 'elopher-margin-brackets))))
         (insert " "))
     (insert (make-string elopher-margin-width ?\s))))
 
 (defvar elopher-type-map
-  `((?0 elopher-get-text-node "T" ,elopher-text-face)
-    (?1 elopher-get-index-node "/" ,elopher-index-face)
-    (?g elopher-get-image-node "im" ,elopher-image-face)
-    (?p elopher-get-image-node "im" ,elopher-image-face)
-    (?I elopher-get-image-node "im" ,elopher-image-face)
-    (?4 elopher-get-node-download "B" ,elopher-binary-face)
-    (?5 elopher-get-node-download "B" ,elopher-binary-face)
-    (?9 elopher-get-node-download "B" ,elopher-binary-face)
-    (?7 elopher-get-search-node "?" ,elopher-search-face)))
+  '((?0 elopher-get-text-node "T" elopher-text)
+    (?1 elopher-get-index-node "/" elopher-index)
+    (?g elopher-get-image-node "im" elopher-image)
+    (?p elopher-get-image-node "im" elopher-image)
+    (?I elopher-get-image-node "im" elopher-image)
+    (?4 elopher-get-node-download "B" elopher-binary)
+    (?5 elopher-get-node-download "B" elopher-binary)
+    (?9 elopher-get-node-download "B" elopher-binary)
+    (?7 elopher-get-search-node "?" elopher-search))
+  "Association list from types to getters, margin codes and index faces.")
 
 (defun elopher-insert-index-record (line)
   "Insert the index record corresponding to LINE into the current buffer."
@@ -233,13 +272,13 @@ Otherwise, use the system browser via the BROWSE-URL function."
                               'help-echo (format "mouse-1, RET: open %s on %s port %s"
                                                  selector host port)))
       (pcase type
-        (?i (elopher-insert-margin) ; Information 
+        (?i (elopher-insert-margin) ; Information
             (insert (propertize display-string
-                                'face elopher-info-face)))
+                                'face 'elopher-info)))
         (?h (elopher-insert-margin "W") ; Web link
             (let ((url (elt (split-string selector "URL:") 1)))
               (insert-text-button display-string
-                                  'face elopher-http-face
+                                  'face 'elopher-url
                                   'elopher-url url
                                   'action #'elopher-click-url
                                   'follow-link t
@@ -247,7 +286,7 @@ Otherwise, use the system browser via the BROWSE-URL function."
         (?.) ; Occurs at end of index, can safely ignore.
         (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
             (insert (propertize display-string
-                                'face elopher-unknown-face)))))
+                                'face 'elopher-unknown-face)))))
     (insert "\n")))
 
 
@@ -306,7 +345,8 @@ The result is stored as a string in the variable elopher-selector-string."
 
 ;; Text retrieval
 
-(defvar elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)")
+(defconst elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
+  "Regexp used to locate and buttinofy URLs in text files loaded by elopher.")
 
 (defun elopher-buttonify-urls (string)
   "Turn substrings which look like urls in STRING into clickable buttons."
@@ -400,8 +440,9 @@ The result is stored as a string in the variable elopher-selector-string."
 ;; Search retrieval
 
 (defun elopher-get-search-node ()
-  (let* ((content (elopher-node-content elopher-current-node))
-         (address (elopher-node-address elopher-current-node)))
+  (let ((content (elopher-node-content elopher-current-node))
+        (address (elopher-node-address elopher-current-node))
+        (aborted t))
     (if content
         (progn
           (elopher-with-clean-buffer
@@ -414,6 +455,7 @@ The result is stored as a string in the variable elopher-selector-string."
                  (search-address (elopher-make-address query-selector
                                                        (elopher-address-host address)
                                                        (elopher-address-port address))))
+            (setq aborted nil)
             (elopher-with-clean-buffer
              (insert "LOADING RESULTS..."))
             (elopher-get-selector search-address
@@ -424,7 +466,8 @@ The result is stored as a string in the variable elopher-selector-string."
                                       (goto-char (point-min))
                                       (elopher-set-node-content elopher-current-node
                                                                 (buffer-string))))))
-        (elopher-visit-parent-node)))))
+        (if aborted
+            (elopher-visit-parent-node))))))
 
 ;; Raw server response retrieval
 
@@ -444,7 +487,7 @@ The result is stored as a string in the variable elopher-selector-string."
         (elopher-with-clean-buffer
          (insert elopher-start-index))
         (goto-char (point-min)))))
-  (message "Displaying raw server response.  Reload to return to standard view."))
+  (message "Displaying raw server response.  Reload or redraw to return to standard view."))
  
 ;; File export retrieval
 
@@ -492,7 +535,8 @@ The result is stored as a string in the variable elopher-selector-string."
         (browse-web url)
       (browse-url url))))
 
-(defun elopher-follow-closest-link ()
+(defun elopher-follow-current-link ()
+  "Open the link or url at point."
   (interactive)
   (push-button))
 
@@ -509,6 +553,11 @@ The result is stored as a string in the variable elopher-selector-string."
                         address
                         #'elopher-get-index-node))))
 
+(defun  elopher-redraw ()
+  "Redraw current page."
+  (interactive)
+  (elopher-visit-node elopher-current-node))
+
 (defun  elopher-reload ()
   "Reload current page."
   (interactive)
@@ -544,20 +593,23 @@ The result is stored as a string in the variable elopher-selector-string."
 
 (defvar elopher-mode-map
   (let ((map (make-sparse-keymap)))
-    (define-key map (kbd "<tab>") 'elopher-next-link)
-    (define-key map (kbd "<S-tab>") 'elopher-prev-link)
+    (define-key map (kbd "TAB") 'elopher-next-link)
+    (define-key map (kbd "<backtab>") 'elopher-prev-link)
     (define-key map (kbd "u") 'elopher-back)
     (define-key map (kbd "g") 'elopher-go)
-    (define-key map (kbd "r") 'elopher-reload)
+    (define-key map (kbd "r") 'elopher-redraw)
+    (define-key map (kbd "R") 'elopher-reload)
     (define-key map (kbd "w") 'elopher-view-raw)
     (define-key map (kbd "d") 'elopher-download)
     (when (fboundp 'evil-define-key)
       (evil-define-key 'normal map
-        (kbd "C-]") 'elopher-follow-closest-link
+        (kbd "TAB") 'elopher-next-link
+        (kbd "C-]") 'elopher-follow-current-link
         (kbd "C-t") 'elopher-back
         (kbd "u") 'elopher-back
         (kbd "g") 'elopher-go
-        (kbd "r") 'elopher-reload
+        (kbd "r") 'elopher-redraw
+        (kbd "R") 'elopher-reload
         (kbd "w") 'elopher-view-raw
         (kbd "d") 'elopher-download))
     map)
@@ -579,4 +631,3 @@ The result is stored as a string in the variable elopher-selector-string."
   "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
 
 ;;; elopher.el ends here
-