Enabled major mode, added basic keymap.
authorTim Vaughan <tgvaughan@gmail.com>
Tue, 16 Apr 2019 22:41:58 +0000 (00:41 +0200)
committerTim Vaughan <tgvaughan@gmail.com>
Tue, 16 Apr 2019 22:41:58 +0000 (00:41 +0200)
elopher.el

index 609876d..2907e95 100644 (file)
@@ -6,10 +6,21 @@
 
 ;;; Code:
 
-;; (defvar elopher-mode-map nil "Keymap for gopher client.")
-
-;; (define-derived-mode elopher-mode special-mode "elopher"
-;;   "Major mode for elopher, an elisp gopher client.")
+(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 "u") 'elopher-history-back)
+    (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))
+    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)
 
                'face '(foreground-color . "yellow")))
     (insert (make-string elopher-margin-width ?\s))))
 
-(defun elopher-follow-index-link (button)
-  (apply #'elopher-get-index (button-get button 'link-address)))
-
-(defun elopher-follow-text-link (button)
-  (apply #'elopher-get-text (button-get button 'link-address)))
-
-(defun elopher-follow-image-link (button)
-  (apply #'elopher-get-image (button-get button 'link-address)))
-
 (defun elopher-process-record (line)
   (let* ((type (elt line 0))
          (fields (split-string (substring line 1) "\t"))
     (pcase type
       (?i (elopher-insert-margin)
           (insert (propertize display-string
-                              'face '(foreground-color. "white"))))
+                              'face '(foreground-color. "gray"))))
       (?0 (elopher-insert-margin "T")
           (insert-text-button display-string
-                              'face '(foreground-color . "gray")
+                              'face '(foreground-color . "white")
+                              'link-getter #'elopher-get-text
                               'link-address address
-                              'action #'elopher-follow-text-link
+                              'action #'elopher-click-link
                               'follow-link t))
       (?1 (elopher-insert-margin "/")
           (insert-text-button display-string
-                              'face '(foreground-color . "cyan")
+                              'face '(foreground-color . "yellow")
+                              'link-getter #'elopher-get-index
                               'link-address address
-                              'action #'elopher-follow-index-link
+                              'action #'elopher-click-link
                               'follow-link t))
       (?p (elopher-insert-margin "img")
           (insert-text-button display-string
-                             'face '(foreground-color . "gray")
+                             'face '(foreground-color . "cyan")
+                             'link-getter #'elopher-get-image
                              'link-address address
-                             'action #'elopher-follow-image-link
+                             'action #'elopher-click-link
                              'follow-link t))
       (?.) ; Occurs at end of index, can safely ignore.
       (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
               (elopher-process-record line)))
         (setq elopher-incomplete-record (elt lines idx))))))
 
+(defun elopher-get-selector (selector host port filter)
+  (switch-to-buffer "*elopher*")
+  (elopher-mode)
+  (let ((inhibit-read-only t))
+    (erase-buffer))
+  (setq elopher-incomplete-record "")
+  (make-network-process
+   :name "elopher-process"
+   :host host
+   :service (if port port 70)
+   :filter filter)
+  (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
         (set-marker marker (point))))))
     
 (defun elopher-get-index (selector host port)
-  (switch-to-buffer "*elopher*")
-  (erase-buffer)
-  (setq elopher-incomplete-record "")
-  (make-network-process
-   :name "elopher-process"
-   :host host
-   :service (if port port 70)
-   :filter #'elopher-index-filter)
-  (process-send-string "elopher-process" (concat selector "\n")))
+  (elopher-get-selector selector host port #'elopher-index-filter))
 
 (defun elopher-text-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
         (set-marker marker (point))))))
 
 (defun elopher-get-text (selector host port)
-  (switch-to-buffer "*elopher*")
-  (erase-buffer)
-  (make-network-process
-   :name "elopher-process"
-   :host host
-   :service port
-   :filter #'elopher-text-filter)
-  (process-send-string "elopher-process" (concat selector "\n")))
+  (elopher-get-selector selector host port #'elopher-text-filter))
 
 (defvar elopher-image-buffer "")
 
   (setq elopher-image-buffer (concat elopher-image-buffer string)))
 
 (defun elopher-get-image (selector host port)
-  (switch-to-buffer "*elopher*")
-  (erase-buffer)
   (setq elopher-image-buffer "")
-  (make-network-process
-   :name "elopher-process"
-   :host host
-   :service port
-   :filter #'elopher-image-filter)
-  (process-send-string "elopher-process" (concat selector "\n"))
+  (elopher-get-selector selector host port #'elopher-image-filter)
   (insert-image (create-image elopher-image-buffer)))
 
+(defun elopher-history-back ()
+  (interactive)
+  (let ((inhibit-read-only t))
+    (undo)))
+
+(defun elopher-next-link ()
+  (interactive)
+  (forward-button 1))
+
+(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-follow-closest-link ()
+  (interactive)
+  (push-button))
+
 (defun elopher ()
   "Start gopher client."
   (interactive)
   (elopher-get-index "" (read-from-minibuffer "Gopher host: ") 70))
 
-(elopher-get-index "" "cosmic.voyage" 70)
+(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