Added start page.
[elpher.git] / elopher.el
index cd1f7eb..2be607a 100644 (file)
 
 ;;; Code:
 
-;; (defvar elopher-mode-map nil "Keymap for gopher client.")
-;; (define-key elopher-mode-map (kbd "p") 'elopher-quit)
-
-;; (define-derived-mode elopher-mode special-mode "elopher"
-;;   "Major mode for elopher, an elisp gopher client.")
-
-;; (global-set-key (kbd "C-c C-b") 'eval-buffer)
-
-(defvar elopher-type-margin-width 5)
-
-(defun elopher-type-margin (&optional type-name)
+(setq 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 "u") 'elopher-history-back)
+    (define-key map (kbd "g") 'elopher-go)
+    (when (require 'evil nil t)
+      (evil-define-key 'normal map
+        (kbd "C-]") 'elopher-follow-closest-link
+        (kbd "C-t") 'elopher-history-back
+        (kbd "u") 'elopher-history-back
+        (kbd "g") 'elopher-go))
+    map))
+  ;; "Keymap for gopher client.")
+
+(define-derived-mode elopher-mode special-mode "elopher"
+  "Major mode for elopher, an elisp gopher client.")
+
+(defvar elopher-margin-width 5)
+
+(defun elopher-insert-margin (&optional type-name)
   (if type-name
       (insert (propertize
-               (format (concat "%" (number-to-string elopher-type-margin-width) "s")
+               (format (concat "%" (number-to-string elopher-margin-width) "s")
                        (concat "[" type-name "] "))
-               'face '(foreground-color . "yellow")))
-    (insert (make-string elopher-type-margin-width ?\s))))
-
-(defun elopher-format-i (display-string)
-  (elopher-type-margin)
-  (insert (propertize display-string 'face '(foreground-color . "white")))
-  (insert "\n"))
+               'face '(foreground-color . "orange")))
+    (insert (make-string elopher-margin-width ?\s))))
 
-(defun elopher-format-0 (display-string selector hostname port)
-  (elopher-type-margin "T")
-  (insert (propertize display-string 'face '(foreground-color . "gray")))
-  (insert "\n"))
-
-(defun elopher-format-1 (display-string selector hostname port)
-  (elopher-type-margin "/")
-  (let ((map (make-sparse-keymap)))
-    (define-key map [mouse-1]
-      (eval `(lambda () (interactive) (elopher-get-index ,hostname ,port ,selector))))
-    (insert (propertize display-string
-                        'face '(foreground-color . "cyan")
-                        'mouse-face 'highlight
-                        'help-echo (format "mouse-1: follow link to \"%s\" on %s port %s"
-                                           selector hostname port)
-                        'keymap map))
-    (insert "\n")))
-
-(defun elopher-process-record (line)
+(defun elopher-render-record (line)
   (let* ((type (elt line 0))
          (fields (split-string (substring line 1) "\t"))
-         (g-display-string (elt fields 0))
-         (g-selector (elt fields 1))
-         (g-hostname (elt fields 2))
-         (g-port (elt fields 3)))
+         (display-string (elt fields 0))
+         (selector (elt fields 1))
+         (hostname (elt fields 2))
+         (port (elt fields 3))
+         (address (list selector hostname port)))
     (pcase type
-      (?i (elopher-format-i g-display-string))
-      (?0 (elopher-format-0 g-display-string g-selector g-hostname g-port))
-      (?1 (elopher-format-1 g-display-string g-selector g-hostname g-port)))))
+      (?i (elopher-insert-margin)
+          (insert (propertize display-string
+                              'face '(foreground-color . "gray"))))
+      (?0 (elopher-insert-margin "T")
+          (insert-text-button display-string
+                              'face '(foreground-color . "white")
+                              'link-getter #'elopher-get-text
+                              'link-address address
+                              'action #'elopher-click-link
+                              'follow-link t))
+      (?1 (elopher-insert-margin "/")
+          (insert-text-button display-string
+                              'face '(foreground-color . "cyan")
+                              'link-getter #'elopher-get-index
+                              'link-address address
+                              'action #'elopher-click-link
+                              'follow-link t))
+      (?p (elopher-insert-margin "img")
+          (insert-text-button display-string
+                             'face '(foreground-color . "cyan")
+                             'link-getter #'elopher-get-image
+                             'link-address address
+                             'action #'elopher-click-link
+                             'follow-link t))
+      (?.) ; Occurs at end of index, can safely ignore.
+      (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
+          (insert (propertize display-string
+                              'face '(foreground-color . "red")))))
+    (insert "\n")))
 
 (defvar elopher-incomplete-record "")
 
-(defun elopher-process-complete-records (string)
+(defun elopher-render-complete-records (string)
   (let* ((til-now (string-join (list elopher-incomplete-record string)))
          (lines (split-string til-now "\r\n")))
     (dotimes (idx (length lines))
       (if (< idx (- (length lines) 1))
-          (elopher-process-record (elt lines idx))
+          (let ((line (elt lines idx)))
+            (unless (string-empty-p line)
+              (elopher-render-record line)))
         (setq elopher-incomplete-record (elt lines idx))))))
 
-(defun elopher-filter (proc string)
+(defun elopher-get-selector (selector host port filter &optional sentinel)
+  (switch-to-buffer "*elopher*")
+  (elopher-mode)
+  (let ((inhibit-read-only t))
+    (erase-buffer))
+  (make-network-process
+   :name "elopher-process"
+   :host host
+   :service (if port port 70)
+   :filter filter
+   :sentinel sentinel)
+  (process-send-string "elopher-process" (concat selector "\n")))
+
+(defun elopher-index-filter (proc string)
   (with-current-buffer (get-buffer "*elopher*")
-    (let ((marker (process-mark proc)))
+    (let ((marker (process-mark proc))
+          (inhibit-read-only t))
       (if (not (marker-position marker))
           (set-marker marker 0 (current-buffer)))
       (save-excursion
         (goto-char marker)
-        (elopher-process-complete-records string)
+        (elopher-render-complete-records string)
         (set-marker marker (point))))))
     
-(defun elopher-get-index (host &optional port path)
-  (switch-to-buffer "*elopher*")
-  (erase-buffer)
-  (make-network-process
-   :name "elopher-process"
-   :host host
-   :service (if port port 70)
-   :filter #'elopher-filter)
-  (process-send-string "elopher-process" (concat path "\n")))
+(defun elopher-get-index (selector host port)
+  (setq elopher-incomplete-record "")
+  (elopher-get-selector selector host port #'elopher-index-filter))
 
-(defun elopher ()
-  "Start gopher client."
+(defun elopher-text-filter (proc string)
+  (with-current-buffer (get-buffer "*elopher*")
+    (let ((marker (process-mark proc))
+          (inhibit-read-only t))
+      (if (not (marker-position marker))
+          (set-marker marker 0 (current-buffer)))
+      (save-excursion
+        (goto-char marker)
+        (dolist (line (split-string string "\r"))
+          (insert line))
+        (set-marker marker (point))))))
+
+(defun elopher-get-text (selector host port)
+  (elopher-get-selector selector host port #'elopher-text-filter))
+
+(defvar elopher-image-buffer "")
+
+(defun elopher-image-filter (proc string)
+  (setq elopher-image-buffer (concat elopher-image-buffer string)))
+
+(defun elopher-image-sentinel (proc event)
+  (let ((inhibit-read-only t))
+    (insert-image (create-image elopher-image-buffer))))
+
+(defun elopher-get-image (selector host port)
+  (setq elopher-image-buffer "")
+  (elopher-get-selector selector host port #'elopher-image-filter #'elopher-image-sentinel))
+
+(defun elopher-history-back ()
+  (interactive)
+  (let ((inhibit-read-only t))
+    (undo)))
+
+(defun elopher-next-link ()
   (interactive)
-  (elopher-get-index (read-from-minibuffer "Gopher host: ") 70))
+  (forward-button 1))
 
-;; (elopher-get-index "cosmic.voyage")
-(elopher-get-index "gopher.floodgap.com")
-;; (elopher-get-index "maurits.id.au")
+(defun elopher-prev-link ()
+  (interactive)
+  (backward-button 1))
+
+(defun elopher-click-link (button)
+  (apply (button-get button 'link-getter) (button-get button 'link-address)))
 
-(defun elopher-quit ()
+(defun elopher-follow-closest-link ()
   (interactive)
-  (kill-buffer "*elopher*"))
+  (push-button))
+
+(defun elopher-go ()
+  "Go to a particular gopher site."
+  (interactive)
+  (elopher-get-index "" (read-from-minibuffer "Gopher host: ") 70))
+
+(defun elopher ()
+  "Start elopher with default landing page."
+  (interactive)
+  (switch-to-buffer "*elopher*")
+  (elopher-mode)
+  (let ((inhibit-read-only t))
+    (erase-buffer)
+    (save-excursion
+      (elopher-render-complete-records
+       (concat "i\tfake\tfake\t1\r\n"
+               "i--------------------------------------------\tfake\tfake\t1\r\n"
+               "i          Elopher Gopher Client             \tfake\tfake\t1\r\n"
+               "i              version 1.0                   \tfake\tfake\t1\r\n"
+               "i--------------------------------------------\tfake\tfake\t1\r\n"
+               "i\tfake\tfake\t1\r\n"
+               "iBasic usage:\tfake\tfake\t1\r\n"
+               "i - tab/shift-tab: next/prev directory entry\tfake\tfake\t1\r\n"
+               "i - RET/mouse-1: open directory entry\tfake\tfake\t1\r\n"
+               "i - u: return to parent directory entry\tfake\tfake\t1\r\n"
+               "i - g: go to a particular site\tfake\tfake\t1\r\n"
+               "i\tfake\tfake\t1\r\n"
+               "iPlaces to start exploring Gopherspace:\tfake\tfake\t1\r\n"
+               "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70\r\n"
+               "1Super-Dimensional Fortress\t\tgopher.floodgap.com\t70\r\n"
+               "i\tfake\tfake\t1\r\n"
+               "iTest entries:\tfake\tfake\t1\r\n"
+               "pXKCD comic image\t/fun/xkcd/comics/2130/2137/text_entry.png\tgopher.floodgap.com\t70\r\n")))))
+
+(elopher)
+;; (elopher-get-index "" "gopher.floodgap.com" 70)
+;; (elopher-get-image "/fun/xkcd/comics/2130/2137/text_entry.png" "gopher.floodgap.com" 70)
 
 ;;; elopher.el ends here