1 ;;; elopher.el --- gopher client
5 ;; Simple gopher client in elisp.
12 (defconst elopher-version "1.0.0"
13 "Current version of elopher.")
15 (defconst elopher-margin-width 6
16 "Width of left-hand margin used when rendering indicies.")
18 (defconst elopher-start-page
20 (list "i\tfake\tfake\t1"
21 "i--------------------------------------------\tfake\tfake\t1"
22 "i Elopher Gopher Client \tfake\tfake\t1"
23 (format "i version %s\tfake\tfake\t1" elopher-version)
24 "i--------------------------------------------\tfake\tfake\t1"
26 "iBasic usage:\tfake\tfake\t1"
27 "i - tab/shift-tab: next/prev directory entry\tfake\tfake\t1"
28 "i - RET/mouse-1: open directory entry\tfake\tfake\t1"
29 "i - u: return to parent directory entry\tfake\tfake\t1"
30 "i - g: go to a particular site\tfake\tfake\t1"
32 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
33 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
34 "1Super-Dimensional Fortress\t\tsdf.org\t70"
36 "iTest entries:\tfake\tfake\t1"
37 "pXKCD comic image\t/fun/xkcd/comics/2130/2137/text_entry.png\tgopher.floodgap.com\t70"
38 "1Test server\t\tlocalhost\t70"
43 ;;; Customization group
47 "A simple gopher client."
50 (defcustom elopher-index-face '(foreground-color . "cyan")
51 "Face used for index records.")
52 (defcustom elopher-text-face '(foreground-color . "white")
53 "Face used for text records.")
54 (defcustom elopher-info-face '(foreground-color . "gray")
55 "Face used for info records.")
56 (defcustom elopher-image-face '(foreground-color . "green")
57 "Face used for image records.")
58 (defcustom elopher-unknown-face '(foreground-color . "red")
59 "Face used for unknown record types.")
66 (defun elopher-make-address (selector host port)
67 (list selector host port))
69 (defun elopher-address-selector (address)
72 (defun elopher-address-host (address)
75 (defun elopher-address-port (address)
80 (defun elopher-make-node (parent address getter &optional content)
81 (list parent address getter content))
83 (defun elopher-node-parent (node)
86 (defun elopher-node-address (node)
89 (defun elopher-node-getter (node)
92 (defun elopher-node-content (node)
95 (defvar elopher-start-node (elopher-make-node nil nil #'elopher-get-index-node))
96 (defvar elopher-current-node)
98 (defun elopher-set-node-content (node content)
99 (setcar (elopher-node-content node)
102 (defun elopher-visit-node (node)
103 (elopher-prepare-buffer)
104 (setq elopher-current-node node)
105 (funcall (elopher-node-getter node)))
107 (defun elopher-reload-current-node ()
108 (elopher-prepare-buffer)
109 (elopher-set-node-content elopher-current-node nil)
110 (funcall (elopher-node-getter elopher-current-node)))
113 ;;; Buffer preparation
116 (defun elopher-prepare-buffer ()
117 (switch-to-buffer "*elopher*")
119 (let ((inhibit-read-only t))
126 (defun elopher-insert-index (string)
127 "Inserts the index corresponding to STRING into the current buffer."
128 (dolist (line (split-string string "\r\n"))
129 (unless (string-empty-p line)
130 (elopher-insert-index-record line))))
132 (defun elopher-insert-margin (&optional type-name)
135 (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
137 (propertize "[" 'face '(foreground-color . "blue"))
138 (propertize type-name 'face '(foreground-color . "white"))
139 (propertize "]" 'face '(foreground-color . "blue")))))
141 (insert (make-string elopher-margin-width ?\s))))
143 (defun elopher-insert-index-record (line)
144 "Inserts the index record corresponding to LINE into the current buffer."
145 (let* ((type (elt line 0))
146 (fields (split-string (substring line 1) "\t"))
147 (display-string (elt fields 0))
148 (address (elopher-make-address (elt fields 1) (elt fields 2) (elt fields 3))))
150 (?i (elopher-insert-margin)
151 (insert (propertize display-string
152 'face elopher-info-face)))
153 (?0 (elopher-insert-margin "T")
154 (insert-text-button display-string
155 'face elopher-text-face
156 'elopher-node (elopher-make-node elopher-current-node
158 #'elopher-get-text-node)
159 'action #'elopher-click-link
161 (?1 (elopher-insert-margin "/")
162 (insert-text-button display-string
163 'face elopher-index-face
164 'elopher-node (elopher-make-node elopher-current-node
166 #'elopher-get-index-node)
167 'action #'elopher-click-link
169 (?.) ; Occurs at end of index, can safely ignore.
170 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
171 (insert (propertize display-string
172 'face elopher-unknown-face))))
176 ;;; Selector retrieval (all kinds)
179 (defvar elopher-selector-string)
181 (defun elopher-get-selector (address after)
182 (setq elopher-selector-string "")
183 (let ((p (get-process "elopher-process")))
184 (if p (delete-process p)))
185 (make-network-process
186 :name "elopher-process"
187 :host (elopher-address-host address)
188 :service (elopher-address-port address)
189 :filter (lambda (proc string)
190 (setq elopher-selector-string (concat elopher-selector-string string)))
192 (process-send-string "elopher-process"
193 (concat (elopher-address-selector address) "\n")))
197 (defun elopher-get-index-node ()
198 (let ((content (elopher-node-content elopher-current-node))
199 (address (elopher-node-address elopher-current-node)))
203 (elopher-get-selector address
205 (let ((inhibit-read-only t))
207 (elopher-insert-index elopher-selector-string))
208 (elopher-set-node-content elopher-current-node
210 (let ((inhibit-read-only t))
212 (elopher-insert-index elopher-start-page))))))
216 (defun elopher-get-text-node ()
217 (let ((content (elopher-node-content elopher-current-node))
218 (address (elopher-node-address elopher-current-node)))
220 (let ((inhibit-read-only t))
222 (elopher-get-selector address
224 (let ((inhibit-read-only t))
226 (insert elopher-selector-string))
227 (elopher-set-node-content elopher-current-node
228 elopher-selector-string))))))
230 ;;; Navigation methods
233 (defun elopher-next-link ()
237 (defun elopher-prev-link ()
241 (defun elopher-click-link (button)
242 (let ((node (button-get button 'elopher-node)))
243 (elopher-visit-node node)))
245 (defun elopher-follow-closest-link ()
250 "Go to a particular gopher site."
253 (hostname (read-from-minibuffer "Gopher host: "))
255 (address (list selector hostname port)))
257 (elopher-make-node elopher-current-node
259 #'elopher-get-index-node))))
261 (defun elopher-reload ()
262 "Reload current site."
264 (elopher-reload-current-node))
267 ;;; Main start procedure
270 "Start elopher with default landing page."
272 (elopher-visit-node elopher-start-node))
278 (setq elopher-mode-map
279 (let ((map (make-sparse-keymap)))
280 (define-key map (kbd "<tab>") 'elopher-next-link)
281 (define-key map (kbd "<S-tab>") 'elopher-prev-link)
282 (define-key map (kbd "u") 'elopher-pop-history)
283 (define-key map (kbd "g") 'elopher-go)
284 (define-key map (kbd "r") 'elopher-reload)
285 (when (require 'evil nil t)
286 (evil-define-key 'normal map
287 (kbd "C-]") 'elopher-follow-closest-link
288 (kbd "C-t") 'elopher-pop-history
289 (kbd "u") 'elopher-pop-history
290 (kbd "g") 'elopher-go
291 (kbd "r") 'elopher-reload))
293 ;; "Keymap for gopher client.")
295 (define-derived-mode elopher-mode special-mode "elopher"
296 "Major mode for elopher, an elisp gopher client.")
299 ;;; elopher.el ends here