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-index
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"
28 "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
29 "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
30 "i - u: return to parent directory entry\tfake\tfake\t1"
31 "i - g: go to a particular page\tfake\tfake\t1"
32 "i - r: reload current page\tfake\tfake\t1"
33 "i - w: display the raw server response for the current page\tfake\tfake\t1"
35 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
37 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
39 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
40 "isearch terms:\tfake\tfake\t1"
42 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
47 ;;; Customization group
51 "A simple gopher client."
54 (defcustom elopher-index-face '(foreground-color . "cyan")
55 "Face used for index records."
58 (defcustom elopher-text-face '(foreground-color . "white")
59 "Face used for text records."
62 (defcustom elopher-info-face '(foreground-color . "gray")
63 "Face used for info records."
66 (defcustom elopher-image-face '(foreground-color . "green")
67 "Face used for image records."
70 (defcustom elopher-search-face '(foreground-color . "orange")
71 "Face used for image records."
74 (defcustom elopher-http-face '(foreground-color . "yellow")
75 "Face used for image records."
78 (defcustom elopher-unknown-face '(foreground-color . "red")
79 "Face used for unknown record types."
82 (defcustom elopher-open-urls-with-eww nil
83 "If non-nil, open URL selectors using eww.
84 Otherwise, use the system browser via the BROWSE-URL function."
92 (defun elopher-make-address (selector host port)
93 (list selector host port))
95 (defun elopher-address-selector (address)
98 (defun elopher-address-host (address)
101 (defun elopher-address-port (address)
106 (defun elopher-make-node (parent address getter &optional content pos)
107 (list parent address getter content pos))
109 (defun elopher-node-parent (node)
112 (defun elopher-node-address (node)
115 (defun elopher-node-getter (node)
118 (defun elopher-node-content (node)
121 (defun elopher-node-pos (node)
124 (defun elopher-set-node-content (node content)
125 (setcar (nthcdr 3 node) content))
127 (defun elopher-set-node-pos (node pos)
128 (setcar (nthcdr 4 node) pos))
130 (defun elopher-save-pos ()
131 (when elopher-current-node
132 (elopher-set-node-pos elopher-current-node (point))))
134 (defun elopher-restore-pos ()
135 (let ((pos (elopher-node-pos elopher-current-node)))
138 (goto-char (point-min)))))
140 ;; Node graph traversal
142 (defvar elopher-current-node)
144 (defun elopher-visit-node (node &optional raw)
146 (setq elopher-current-node node)
148 (elopher-get-node-raw)
149 (funcall (elopher-node-getter node))))
151 (defun elopher-visit-parent-node ()
152 (let ((parent-node (elopher-node-parent elopher-current-node)))
154 (elopher-visit-node parent-node))))
156 (defun elopher-reload-current-node ()
157 (elopher-set-node-content elopher-current-node nil)
158 (elopher-visit-node elopher-current-node))
160 ;;; Buffer preparation
163 (defmacro elopher-with-clean-buffer (&rest args)
165 '(switch-to-buffer "*elopher*")
167 (append (list 'let '((inhibit-read-only t))
174 (defun elopher-insert-index (string)
175 "Insert the index corresponding to STRING into the current buffer."
176 (dolist (line (split-string string "\r\n"))
177 (unless (= (length line) 0)
178 (elopher-insert-index-record line))))
180 (defun elopher-insert-margin (&optional type-name)
181 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
184 (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
186 (propertize "[" 'face '(foreground-color . "blue"))
187 (propertize type-name 'face '(foreground-color . "white"))
188 (propertize "]" 'face '(foreground-color . "blue")))))
190 (insert (make-string elopher-margin-width ?\s))))
192 (defun elopher-insert-index-record (line)
193 "Insert the index record corresponding to LINE into the current buffer."
194 (let* ((type (elt line 0))
195 (fields (split-string (substring line 1) "\t"))
196 (display-string (elt fields 0))
197 (selector (elt fields 1))
198 (host (elt fields 2))
199 (port (elt fields 3))
200 (address (elopher-make-address selector host port))
201 (help-string (format "mouse-1, RET: open %s on %s port %s"
202 selector host port)))
204 (?i (elopher-insert-margin)
205 (insert (propertize display-string
206 'face elopher-info-face)))
207 (?0 (elopher-insert-margin "T")
208 (insert-text-button display-string
209 'face elopher-text-face
210 'elopher-node (elopher-make-node elopher-current-node
212 #'elopher-get-text-node)
213 'action #'elopher-click-link
215 'help-echo help-string))
216 (?1 (elopher-insert-margin "/")
217 (insert-text-button display-string
218 'face elopher-index-face
219 'elopher-node (elopher-make-node elopher-current-node
221 #'elopher-get-index-node)
222 'action #'elopher-click-link
224 'help-echo help-string))
225 ((or ?g ?p ?I) (elopher-insert-margin "im")
226 (insert-text-button display-string
227 'face elopher-image-face
228 'elopher-node (elopher-make-node elopher-current-node
230 #'elopher-get-image-node)
231 'action #'elopher-click-link
233 'help-echo help-string))
234 (?7 (elopher-insert-margin "S")
235 (insert-text-button display-string
236 'face elopher-search-face
237 'elopher-node (elopher-make-node elopher-current-node
239 #'elopher-get-search-node)
240 'action #'elopher-click-link
242 'help-echo help-string))
243 (?h (elopher-insert-margin "W")
244 (let ((url (elt (split-string selector "URL:") 1)))
245 (insert-text-button display-string
246 'face elopher-http-face
248 'action #'elopher-click-url
250 'help-echo (format "mouse-1, RET: open url %s" url))))
251 (?.) ; Occurs at end of index, can safely ignore.
252 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
253 (insert (propertize display-string
254 'face elopher-unknown-face))))
258 ;;; Selector retrieval (all kinds)
261 (defvar elopher-selector-string)
263 (defun elopher-get-selector (address after)
264 "Retrieve selector specified by ADDRESS, then execute AFTER.
265 The result is stored as a string in the variable elopher-selector-string."
266 (setq elopher-selector-string "")
267 (let ((p (get-process "elopher-process")))
268 (if p (delete-process p)))
269 (make-network-process
270 :name "elopher-process"
271 :host (elopher-address-host address)
272 :service (elopher-address-port address)
273 :filter (lambda (proc string)
274 (setq elopher-selector-string (concat elopher-selector-string string)))
276 (process-send-string "elopher-process"
277 (concat (elopher-address-selector address) "\n")))
281 (defun elopher-get-index-node ()
282 (let ((content (elopher-node-content elopher-current-node))
283 (address (elopher-node-address elopher-current-node)))
286 (elopher-with-clean-buffer
288 (elopher-restore-pos))
291 (elopher-with-clean-buffer
292 (insert "LOADING DIRECTORY..."))
293 (elopher-get-selector address
295 (elopher-with-clean-buffer
296 (elopher-insert-index elopher-selector-string))
297 (elopher-restore-pos)
298 (elopher-set-node-content elopher-current-node
301 (elopher-with-clean-buffer
302 (elopher-insert-index elopher-start-index))
303 (elopher-restore-pos)
304 (elopher-set-node-content elopher-current-node
305 (buffer-string)))))))
309 (defun elopher-process-text (string)
310 (let ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)))
311 (replace-regexp-in-string "\r" "" chopped-str)))
313 (defun elopher-get-text-node ()
314 (let ((content (elopher-node-content elopher-current-node))
315 (address (elopher-node-address elopher-current-node)))
318 (elopher-with-clean-buffer
320 (elopher-restore-pos))
322 (elopher-with-clean-buffer
323 (insert "LOADING TEXT..."))
324 (elopher-get-selector address
326 (elopher-with-clean-buffer
327 (insert (elopher-process-text elopher-selector-string)))
328 (elopher-restore-pos)
329 (elopher-set-node-content elopher-current-node
330 (buffer-string))))))))
334 (defun elopher-get-image-node ()
335 (let ((content (elopher-node-content elopher-current-node))
336 (address (elopher-node-address elopher-current-node)))
339 (elopher-with-clean-buffer
340 (insert-image content))
341 (setq cursor-type nil)
342 (elopher-restore-pos))
344 (elopher-with-clean-buffer
345 (insert "LOADING IMAGE..."))
346 (elopher-get-selector address
348 (let ((image (create-image
349 (string-as-unibyte elopher-selector-string)
351 (elopher-with-clean-buffer
352 (insert-image image))
353 (setq cursor-type nil)
354 (elopher-restore-pos)
355 (elopher-set-node-content elopher-current-node image))))))))
359 (defun elopher-get-search-node ()
360 (let* ((content (elopher-node-content elopher-current-node))
361 (address (elopher-node-address elopher-current-node)))
364 (elopher-with-clean-buffer
366 (elopher-restore-pos)
367 (message "Displaying cached search results. Reload to perform a new search."))
368 (let* ((query-string (read-string "Query: "))
369 (query-selector (concat (elopher-address-selector address) "\t" query-string))
370 (search-address (elopher-make-address query-selector
371 (elopher-address-host address)
372 (elopher-address-port address))))
373 (elopher-with-clean-buffer
374 (insert "LOADING RESULTS..."))
375 (elopher-get-selector search-address
377 (elopher-with-clean-buffer
378 (elopher-insert-index elopher-selector-string))
379 (goto-char (point-min))
380 (elopher-set-node-content elopher-current-node
381 (buffer-string))))))))
383 (defun elopher-get-node-raw ()
384 (let* ((content (elopher-node-content elopher-current-node))
385 (address (elopher-node-address elopher-current-node)))
386 (elopher-with-clean-buffer
387 (insert "LOADING RAW SERVER RESPONSE..."))
389 (elopher-get-selector address
391 (elopher-with-clean-buffer
392 (insert elopher-selector-string))
393 (goto-char (point-min))))
395 (elopher-with-clean-buffer
396 (insert elopher-start-index))
397 (goto-char (point-min)))))
398 (message "Displaying raw server response. Reload to return to standard view."))
401 ;;; Navigation procedures
404 (defun elopher-next-link ()
408 (defun elopher-prev-link ()
412 (defun elopher-click-link (button)
413 (let ((node (button-get button 'elopher-node)))
414 (elopher-visit-node node)))
416 (defun elopher-click-url (button)
417 (let ((url (button-get button 'elopher-url)))
418 (if elopher-open-urls-with-eww
422 (defun elopher-follow-closest-link ()
427 "Go to a particular gopher site."
430 (hostname (read-string "Gopher host: "))
431 (selector (read-string "Selector (default none): " nil nil ""))
432 (port (read-string "Port (default 70): " nil nil 70))
433 (address (list selector hostname port)))
435 (elopher-make-node elopher-current-node
437 #'elopher-get-index-node))))
439 (defun elopher-reload ()
440 "Reload current page."
442 (elopher-reload-current-node))
444 (defun elopher-view-raw ()
445 "View current page as plain text."
447 (elopher-visit-node elopher-current-node t))
449 (defun elopher-back ()
450 "Go to previous site."
452 (if (elopher-node-parent elopher-current-node)
453 (elopher-visit-parent-node)
454 (message "No previous site.")))
460 (defvar elopher-mode-map
461 (let ((map (make-sparse-keymap)))
462 (define-key map (kbd "<tab>") 'elopher-next-link)
463 (define-key map (kbd "<S-tab>") 'elopher-prev-link)
464 (define-key map (kbd "u") 'elopher-back)
465 (define-key map (kbd "g") 'elopher-go)
466 (define-key map (kbd "r") 'elopher-reload)
467 (define-key map (kbd "w") 'elopher-view-raw)
468 (when (fboundp 'evil-define-key)
469 (evil-define-key 'normal map
470 (kbd "C-]") 'elopher-follow-closest-link
471 (kbd "C-t") 'elopher-back
472 (kbd "u") 'elopher-back
473 (kbd "g") 'elopher-go
474 (kbd "r") 'elopher-reload
475 (kbd "w") 'elopher-view-raw))
477 "Keymap for gopher client.")
479 (define-derived-mode elopher-mode special-mode "elopher"
480 "Major mode for elopher, an elisp gopher client.")
483 ;;; Main start procedure
486 "Start elopher with default landing page."
488 (setq elopher-current-node nil)
489 (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
490 (elopher-visit-node start-node)))
492 ;;; elopher.el ends here