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
21 (list "i\tfake\tfake\t1"
22 "i--------------------------------------------\tfake\tfake\t1"
23 "i Elopher Gopher Client \tfake\tfake\t1"
24 (format "i version %s\tfake\tfake\t1" elopher-version)
25 "i--------------------------------------------\tfake\tfake\t1"
27 "iBasic usage:\tfake\tfake\t1"
29 "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
30 "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
31 "i - u: return to parent directory entry\tfake\tfake\t1"
32 "i - g: go to a particular page\tfake\tfake\t1"
33 "i - r: reload current page\tfake\tfake\t1"
34 "i - d: download directory entry under cursor\tfake\tfake\t1"
35 "i - w: display the raw server response for the current page\tfake\tfake\t1"
37 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
39 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
41 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
42 "isearch terms:\tfake\tfake\t1"
44 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
49 ;;; Customization group
53 "A simple gopher client."
56 (defcustom elopher-index-face '(foreground-color . "cyan")
57 "Face used for index records."
60 (defcustom elopher-text-face '(foreground-color . "white")
61 "Face used for text records."
64 (defcustom elopher-info-face '(foreground-color . "gray")
65 "Face used for info records."
68 (defcustom elopher-image-face '(foreground-color . "green")
69 "Face used for image records."
72 (defcustom elopher-search-face '(foreground-color . "orange")
73 "Face used for image records."
76 (defcustom elopher-http-face '(foreground-color . "yellow")
77 "Face used for image records."
80 (defcustom elopher-binary-face '(foreground-color . "magenta")
81 "Face used for image records."
84 (defcustom elopher-unknown-face '(foreground-color . "red")
85 "Face used for unknown record types."
88 (defcustom elopher-open-urls-with-eww nil
89 "If non-nil, open URL selectors using eww.
90 Otherwise, use the system browser via the BROWSE-URL function."
98 (defun elopher-make-address (selector host port)
99 (list selector host port))
101 (defun elopher-address-selector (address)
104 (defun elopher-address-host (address)
107 (defun elopher-address-port (address)
112 (defun elopher-make-node (parent address getter &optional content pos)
113 (list parent address getter content pos))
115 (defun elopher-node-parent (node)
118 (defun elopher-node-address (node)
121 (defun elopher-node-getter (node)
124 (defun elopher-node-content (node)
127 (defun elopher-node-pos (node)
130 (defun elopher-set-node-content (node content)
131 (setcar (nthcdr 3 node) content))
133 (defun elopher-set-node-pos (node pos)
134 (setcar (nthcdr 4 node) pos))
136 (defun elopher-save-pos ()
137 (when elopher-current-node
138 (elopher-set-node-pos elopher-current-node (point))))
140 (defun elopher-restore-pos ()
141 (let ((pos (elopher-node-pos elopher-current-node)))
144 (goto-char (point-min)))))
146 ;; Node graph traversal
148 (defvar elopher-current-node)
150 (defun elopher-visit-node (node &optional getter)
152 (elopher-process-cleanup)
153 (setq elopher-current-node node)
156 (funcall (elopher-node-getter node))))
158 (defun elopher-visit-parent-node ()
159 (let ((parent-node (elopher-node-parent elopher-current-node)))
161 (elopher-visit-node parent-node))))
163 (defun elopher-reload-current-node ()
164 (elopher-set-node-content elopher-current-node nil)
165 (elopher-visit-node elopher-current-node))
167 ;;; Buffer preparation
170 (defmacro elopher-with-clean-buffer (&rest args)
171 "Evaluate ARGS with a clean *elopher* buffer as current."
173 '(switch-to-buffer "*elopher*")
175 (append (list 'let '((inhibit-read-only t))
182 (defun elopher-insert-index (string)
183 "Insert the index corresponding to STRING into the current buffer."
184 (dolist (line (split-string string "\r\n"))
185 (unless (= (length line) 0)
186 (elopher-insert-index-record line))))
188 (defun elopher-insert-margin (&optional type-name)
189 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
192 (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
194 (propertize "[" 'face '(foreground-color . "blue"))
195 (propertize type-name 'face '(foreground-color . "white"))
196 (propertize "]" 'face '(foreground-color . "blue")))))
198 (insert (make-string elopher-margin-width ?\s))))
200 (defun elopher-insert-index-record (line)
201 "Insert the index record corresponding to LINE into the current buffer."
202 (let* ((type (elt line 0))
203 (fields (split-string (substring line 1) "\t"))
204 (display-string (elt fields 0))
205 (selector (elt fields 1))
206 (host (elt fields 2))
207 (port (elt fields 3))
208 (address (elopher-make-address selector host port))
209 (help-string (format "mouse-1, RET: open %s on %s port %s"
210 selector host port)))
212 (?i (elopher-insert-margin) ; Information
213 (insert (propertize display-string
214 'face elopher-info-face)))
215 (?0 (elopher-insert-margin "T") ; Text
216 (insert-text-button display-string
217 'face elopher-text-face
218 'elopher-node (elopher-make-node elopher-current-node
220 #'elopher-get-text-node)
221 'action #'elopher-click-link
223 'help-echo help-string))
224 (?1 (elopher-insert-margin "/") ; Index
225 (insert-text-button display-string
226 'face elopher-index-face
227 'elopher-node (elopher-make-node elopher-current-node
229 #'elopher-get-index-node)
230 'action #'elopher-click-link
232 'help-echo help-string))
233 ((or ?g ?p ?I) (elopher-insert-margin "im") ; Image
234 (insert-text-button display-string
235 'face elopher-image-face
236 'elopher-node (elopher-make-node elopher-current-node
238 #'elopher-get-image-node)
239 'action #'elopher-click-link
241 'help-echo help-string))
242 ((or ?4 ?9) (elopher-insert-margin "B") ; Binary
243 (insert-text-button display-string
244 'face elopher-binary-face
245 'elopher-node (elopher-make-node elopher-current-node
247 #'elopher-get-node-download)
248 'action #'elopher-click-link
250 'help-echo help-string))
251 (?7 (elopher-insert-margin "S")
252 (insert-text-button display-string
253 'face elopher-search-face
254 'elopher-node (elopher-make-node elopher-current-node
256 #'elopher-get-search-node)
257 'action #'elopher-click-link
259 'help-echo help-string))
260 (?h (elopher-insert-margin "W")
261 (let ((url (elt (split-string selector "URL:") 1)))
262 (insert-text-button display-string
263 'face elopher-http-face
265 'action #'elopher-click-url
267 'help-echo (format "mouse-1, RET: open url %s" url))))
268 (?.) ; Occurs at end of index, can safely ignore.
269 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
270 (insert (propertize display-string
271 'face elopher-unknown-face))))
275 ;;; Selector retrieval (all kinds)
278 (defun elopher-process-cleanup ()
279 "Immediately shut down any extant elopher process."
280 (let ((p (get-process "elopher-process")))
281 (if p (delete-process p))))
283 (defvar elopher-selector-string)
285 (defun elopher-get-selector (address after)
286 "Retrieve selector specified by ADDRESS, then execute AFTER.
287 The result is stored as a string in the variable elopher-selector-string."
288 (setq elopher-selector-string "")
289 (make-network-process
290 :name "elopher-process"
291 :host (elopher-address-host address)
292 :service (elopher-address-port address)
293 :filter (lambda (proc string)
294 (setq elopher-selector-string (concat elopher-selector-string string)))
296 (process-send-string "elopher-process"
297 (concat (elopher-address-selector address) "\n")))
301 (defun elopher-get-index-node ()
302 (let ((content (elopher-node-content elopher-current-node))
303 (address (elopher-node-address elopher-current-node)))
306 (elopher-with-clean-buffer
308 (elopher-restore-pos))
311 (elopher-with-clean-buffer
312 (insert "LOADING DIRECTORY..."))
313 (elopher-get-selector address
315 (unless (string-prefix-p "deleted" event)
316 (elopher-with-clean-buffer
317 (elopher-insert-index elopher-selector-string))
318 (elopher-restore-pos)
319 (elopher-set-node-content elopher-current-node
322 (elopher-with-clean-buffer
323 (elopher-insert-index elopher-start-index))
324 (elopher-restore-pos)
325 (elopher-set-node-content elopher-current-node
326 (buffer-string)))))))
330 (defun elopher-process-text (string)
331 (let ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)))
332 (replace-regexp-in-string "\r" "" chopped-str)))
334 (defun elopher-get-text-node ()
335 (let ((content (elopher-node-content elopher-current-node))
336 (address (elopher-node-address elopher-current-node)))
339 (elopher-with-clean-buffer
341 (elopher-restore-pos))
343 (elopher-with-clean-buffer
344 (insert "LOADING TEXT..."))
345 (elopher-get-selector address
347 (unless (string-prefix-p "deleted" event)
348 (elopher-with-clean-buffer
349 (insert (elopher-process-text elopher-selector-string)))
350 (elopher-restore-pos)
351 (elopher-set-node-content elopher-current-node
352 (buffer-string)))))))))
356 (defun elopher-get-image-node ()
357 (let ((content (elopher-node-content elopher-current-node))
358 (address (elopher-node-address elopher-current-node)))
361 (elopher-with-clean-buffer
362 (insert-image content))
363 (setq cursor-type nil)
364 (elopher-restore-pos))
366 (elopher-with-clean-buffer
367 (insert "LOADING IMAGE..."))
368 (elopher-get-selector address
370 (unless (string-prefix-p "deleted" event)
371 (let ((image (create-image
372 (string-as-unibyte elopher-selector-string)
374 (elopher-with-clean-buffer
375 (insert-image image))
376 (setq cursor-type nil)
377 (elopher-restore-pos)
378 (elopher-set-node-content elopher-current-node
383 (defun elopher-get-search-node ()
384 (let* ((content (elopher-node-content elopher-current-node))
385 (address (elopher-node-address elopher-current-node)))
388 (elopher-with-clean-buffer
390 (elopher-restore-pos)
391 (message "Displaying cached search results. Reload to perform a new search."))
392 (let* ((query-string (read-string "Query: "))
393 (query-selector (concat (elopher-address-selector address) "\t" query-string))
394 (search-address (elopher-make-address query-selector
395 (elopher-address-host address)
396 (elopher-address-port address))))
397 (elopher-with-clean-buffer
398 (insert "LOADING RESULTS..."))
399 (elopher-get-selector search-address
401 (unless (string-prefix-p "deleted" event)
402 (elopher-with-clean-buffer
403 (elopher-insert-index elopher-selector-string))
404 (goto-char (point-min))
405 (elopher-set-node-content elopher-current-node
406 (buffer-string)))))))))
408 ;; Raw server response retrieval
410 (defun elopher-get-node-raw ()
411 (let* ((content (elopher-node-content elopher-current-node))
412 (address (elopher-node-address elopher-current-node)))
413 (elopher-with-clean-buffer
414 (insert "LOADING RAW SERVER RESPONSE..."))
416 (elopher-get-selector address
418 (unless (string-prefix-p "deleted" event)
419 (elopher-with-clean-buffer
420 (insert elopher-selector-string))
421 (goto-char (point-min)))))
423 (elopher-with-clean-buffer
424 (insert elopher-start-index))
425 (goto-char (point-min)))))
426 (message "Displaying raw server response. Reload to return to standard view."))
429 ;; File export retrieval
431 (defvar elopher-download-filename)
433 (defun elopher-get-node-download ()
434 (let* ((address (elopher-node-address elopher-current-node))
435 (selector (elopher-address-selector address)))
437 (let* ((filename-proposal (file-name-nondirectory selector))
438 (filename (read-file-name "Save file as: "
440 (if (> (length filename-proposal) 0)
443 (message "Downloading...")
444 (setq elopher-download-filename filename)
445 (elopher-get-selector address
447 (let ((coding-system-for-write 'binary))
448 (with-temp-file elopher-download-filename
449 (insert elopher-selector-string)))
450 (message (format "Download complate, saved to file %s."
451 elopher-download-filename)))))
452 (elopher-visit-parent-node))))
455 ;;; Navigation procedures
458 (defun elopher-next-link ()
462 (defun elopher-prev-link ()
466 (defun elopher-click-link (button)
467 (let ((node (button-get button 'elopher-node)))
468 (elopher-visit-node node)))
470 (defun elopher-click-url (button)
471 (let ((url (button-get button 'elopher-url)))
472 (if elopher-open-urls-with-eww
476 (defun elopher-follow-closest-link ()
481 "Go to a particular gopher site."
484 (hostname (read-string "Gopher host: "))
485 (selector (read-string "Selector (default none): " nil nil ""))
486 (port (read-string "Port (default 70): " nil nil 70))
487 (address (list selector hostname port)))
489 (elopher-make-node elopher-current-node
491 #'elopher-get-index-node))))
493 (defun elopher-reload ()
494 "Reload current page."
496 (elopher-reload-current-node))
498 (defun elopher-view-raw ()
499 "View current page as plain text."
501 (elopher-visit-node elopher-current-node
502 #'elopher-get-node-raw))
504 (defun elopher-back ()
505 "Go to previous site."
507 (if (elopher-node-parent elopher-current-node)
508 (elopher-visit-parent-node)
509 (message "No previous site.")))
511 (defun elopher-download ()
512 "Download the link at point."
514 (let ((button (button-at (point))))
516 (elopher-visit-node (button-get button 'elopher-node)
517 #'elopher-get-node-download)
518 (message "No link selected."))))
524 (defvar elopher-mode-map
525 (let ((map (make-sparse-keymap)))
526 (define-key map (kbd "<tab>") 'elopher-next-link)
527 (define-key map (kbd "<S-tab>") 'elopher-prev-link)
528 (define-key map (kbd "u") 'elopher-back)
529 (define-key map (kbd "g") 'elopher-go)
530 (define-key map (kbd "r") 'elopher-reload)
531 (define-key map (kbd "w") 'elopher-view-raw)
532 (define-key map (kbd "d") 'elopher-download)
533 (when (fboundp 'evil-define-key)
534 (evil-define-key 'normal map
535 (kbd "C-]") 'elopher-follow-closest-link
536 (kbd "C-t") 'elopher-back
537 (kbd "u") 'elopher-back
538 (kbd "g") 'elopher-go
539 (kbd "r") 'elopher-reload
540 (kbd "w") 'elopher-view-raw
541 (kbd "d") 'elopher-download))
543 "Keymap for gopher client.")
545 (define-derived-mode elopher-mode special-mode "elopher"
546 "Major mode for elopher, an elisp gopher client.")
549 ;;; Main start procedure
553 "Start elopher with default landing page."
555 (setq elopher-current-node nil)
556 (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
557 (elopher-visit-node start-node))
558 "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
560 ;;; elopher.el ends here