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 (defvar elopher-type-map
201 `((?0 elopher-get-text-node "T" ,elopher-text-face)
202 (?1 elopher-get-index-node "/" ,elopher-index-face)
203 (?g elopher-get-image-node "im" ,elopher-image-face)
204 (?p elopher-get-image-node "im" ,elopher-image-face)
205 (?I elopher-get-image-node "im" ,elopher-image-face)
206 (?4 elopher-get-binary-node "B" ,elopher-binary-face)
207 (?9 elopher-get-binary-node "B" ,elopher-binary-face)
208 (?7 elopher-get-search-node "?" ,elopher-search-face)))
210 (defun elopher-insert-index-record (line)
211 "Insert the index record corresponding to LINE into the current buffer."
212 (let* ((type (elt line 0))
213 (fields (split-string (substring line 1) "\t"))
214 (display-string (elt fields 0))
215 (selector (elt fields 1))
216 (host (elt fields 2))
217 (port (elt fields 3))
218 (address (elopher-make-address selector host port))
219 (type-map-entry (alist-get type elopher-type-map)))
221 (let ((getter (car type-map-entry))
222 (margin-code (cadr type-map-entry))
223 (face (caddr type-map-entry)))
224 (elopher-insert-margin margin-code)
225 (insert-text-button display-string
227 'elopher-node (elopher-make-node elopher-current-node
230 'action #'elopher-click-link
232 'help-echo (format "mouse-1, RET: open %s on %s port %s"
233 selector host port)))
235 (?i (elopher-insert-margin) ; Information
236 (insert (propertize display-string
237 'face elopher-info-face)))
238 (?h (elopher-insert-margin "W") ; Web link
239 (let ((url (elt (split-string selector "URL:") 1)))
240 (insert-text-button display-string
241 'face elopher-http-face
243 'action #'elopher-click-url
245 'help-echo (format "mouse-1, RET: open url %s" url))))
246 (?.) ; Occurs at end of index, can safely ignore.
247 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
248 (insert (propertize display-string
249 'face elopher-unknown-face)))))
253 ;;; Selector retrieval (all kinds)
256 (defun elopher-process-cleanup ()
257 "Immediately shut down any extant elopher process."
258 (let ((p (get-process "elopher-process")))
259 (if p (delete-process p))))
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 (make-network-process
268 :name "elopher-process"
269 :host (elopher-address-host address)
270 :service (elopher-address-port address)
271 :filter (lambda (proc string)
272 (setq elopher-selector-string (concat elopher-selector-string string)))
274 (process-send-string "elopher-process"
275 (concat (elopher-address-selector address) "\n")))
279 (defun elopher-get-index-node ()
280 (let ((content (elopher-node-content elopher-current-node))
281 (address (elopher-node-address elopher-current-node)))
284 (elopher-with-clean-buffer
286 (elopher-restore-pos))
289 (elopher-with-clean-buffer
290 (insert "LOADING DIRECTORY..."))
291 (elopher-get-selector address
293 (unless (string-prefix-p "deleted" event)
294 (elopher-with-clean-buffer
295 (elopher-insert-index elopher-selector-string))
296 (elopher-restore-pos)
297 (elopher-set-node-content elopher-current-node
300 (elopher-with-clean-buffer
301 (elopher-insert-index elopher-start-index))
302 (elopher-restore-pos)
303 (elopher-set-node-content elopher-current-node
304 (buffer-string)))))))
308 (defvar elopher-url-regex "\\(https?\\|gopher\\)://\\([a-z.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)")
310 (defun elopher-buttonify-urls (string)
311 "Turn substrings which look like urls in STRING into clickable buttons."
314 (goto-char (point-min))
315 (while (re-search-forward elopher-url-regex nil t)
316 (let ((url (match-string 0))
317 (protocol (downcase (match-string 1))))
318 (if (string= protocol "gopher")
319 (let* ((host (match-string 2))
321 (type-and-selector (match-string 4))
322 (type (if (> (length type-and-selector) 1)
323 (elt type-and-selector 1)
325 (selector (if (> (length type-and-selector) 1)
326 (substring type-and-selector 2)
328 (address (elopher-make-address selector host port))
329 (getter (car (alist-get type elopher-type-map))))
330 (make-text-button (match-beginning 0)
332 'elopher-node (elopher-make-node elopher-current-node
335 'action #'elopher-click-link
337 'help-echo (format "mouse-1, RET: open %s on %s port %s"
338 selector host port)))
339 (make-text-button (match-beginning 0)
342 'action #'elopher-click-url
344 'help-echo (format "mouse-1, RET: open url %s" url)))))
347 (defun elopher-process-text (string)
348 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
349 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
350 (elopher-buttonify-urls cleaned-str)))
352 (defun elopher-get-text-node ()
353 (let ((content (elopher-node-content elopher-current-node))
354 (address (elopher-node-address elopher-current-node)))
357 (elopher-with-clean-buffer
359 (elopher-restore-pos))
361 (elopher-with-clean-buffer
362 (insert "LOADING TEXT..."))
363 (elopher-get-selector address
365 (unless (string-prefix-p "deleted" event)
366 (elopher-with-clean-buffer
367 (insert (elopher-process-text elopher-selector-string)))
368 (elopher-restore-pos)
369 (elopher-set-node-content elopher-current-node
370 (buffer-string)))))))))
374 (defun elopher-get-image-node ()
375 (let ((content (elopher-node-content elopher-current-node))
376 (address (elopher-node-address elopher-current-node)))
379 (elopher-with-clean-buffer
380 (insert-image content))
381 (setq cursor-type nil)
382 (elopher-restore-pos))
384 (elopher-with-clean-buffer
385 (insert "LOADING IMAGE..."))
386 (elopher-get-selector address
388 (unless (string-prefix-p "deleted" event)
389 (let ((image (create-image
390 (string-as-unibyte elopher-selector-string)
392 (elopher-with-clean-buffer
393 (insert-image image))
394 (setq cursor-type nil)
395 (elopher-restore-pos)
396 (elopher-set-node-content elopher-current-node
401 (defun elopher-get-search-node ()
402 (let* ((content (elopher-node-content elopher-current-node))
403 (address (elopher-node-address elopher-current-node)))
406 (elopher-with-clean-buffer
408 (elopher-restore-pos)
409 (message "Displaying cached search results. Reload to perform a new search."))
410 (let* ((query-string (read-string "Query: "))
411 (query-selector (concat (elopher-address-selector address) "\t" query-string))
412 (search-address (elopher-make-address query-selector
413 (elopher-address-host address)
414 (elopher-address-port address))))
415 (elopher-with-clean-buffer
416 (insert "LOADING RESULTS..."))
417 (elopher-get-selector search-address
419 (unless (string-prefix-p "deleted" event)
420 (elopher-with-clean-buffer
421 (elopher-insert-index elopher-selector-string))
422 (goto-char (point-min))
423 (elopher-set-node-content elopher-current-node
424 (buffer-string)))))))))
426 ;; Raw server response retrieval
428 (defun elopher-get-node-raw ()
429 (let* ((content (elopher-node-content elopher-current-node))
430 (address (elopher-node-address elopher-current-node)))
431 (elopher-with-clean-buffer
432 (insert "LOADING RAW SERVER RESPONSE..."))
434 (elopher-get-selector address
436 (unless (string-prefix-p "deleted" event)
437 (elopher-with-clean-buffer
438 (insert elopher-selector-string))
439 (goto-char (point-min)))))
441 (elopher-with-clean-buffer
442 (insert elopher-start-index))
443 (goto-char (point-min)))))
444 (message "Displaying raw server response. Reload to return to standard view."))
447 ;; File export retrieval
449 (defvar elopher-download-filename)
451 (defun elopher-get-node-download ()
452 (let* ((address (elopher-node-address elopher-current-node))
453 (selector (elopher-address-selector address)))
454 (elopher-visit-parent-node) ; Do first in case of non-local exits.
455 (let* ((filename-proposal (file-name-nondirectory selector))
456 (filename (read-file-name "Save file as: "
458 (if (> (length filename-proposal) 0)
461 (message "Downloading...")
462 (setq elopher-download-filename filename)
463 (elopher-get-selector address
465 (let ((coding-system-for-write 'binary))
466 (with-temp-file elopher-download-filename
467 (insert elopher-selector-string)
468 (message (format "Download complate, saved to file %s."
469 elopher-download-filename)))))))))
472 ;;; Navigation procedures
475 (defun elopher-next-link ()
479 (defun elopher-prev-link ()
483 (defun elopher-click-link (button)
484 (let ((node (button-get button 'elopher-node)))
485 (elopher-visit-node node)))
487 (defun elopher-click-url (button)
488 (let ((url (button-get button 'elopher-url)))
489 (if elopher-open-urls-with-eww
493 (defun elopher-follow-closest-link ()
498 "Go to a particular gopher site."
501 (hostname (read-string "Gopher host: "))
502 (selector (read-string "Selector (default none): " nil nil ""))
503 (port (read-string "Port (default 70): " nil nil 70))
504 (address (list selector hostname port)))
506 (elopher-make-node elopher-current-node
508 #'elopher-get-index-node))))
510 (defun elopher-reload ()
511 "Reload current page."
513 (elopher-reload-current-node))
515 (defun elopher-view-raw ()
516 "View current page as plain text."
518 (elopher-visit-node elopher-current-node
519 #'elopher-get-node-raw))
521 (defun elopher-back ()
522 "Go to previous site."
524 (if (elopher-node-parent elopher-current-node)
525 (elopher-visit-parent-node)
526 (message "No previous site.")))
528 (defun elopher-download ()
529 "Download the link at point."
531 (let ((button (button-at (point))))
533 (elopher-visit-node (button-get button 'elopher-node)
534 #'elopher-get-node-download)
535 (message "No link selected."))))
541 (defvar elopher-mode-map
542 (let ((map (make-sparse-keymap)))
543 (define-key map (kbd "<tab>") 'elopher-next-link)
544 (define-key map (kbd "<S-tab>") 'elopher-prev-link)
545 (define-key map (kbd "u") 'elopher-back)
546 (define-key map (kbd "g") 'elopher-go)
547 (define-key map (kbd "r") 'elopher-reload)
548 (define-key map (kbd "w") 'elopher-view-raw)
549 (define-key map (kbd "d") 'elopher-download)
550 (when (fboundp 'evil-define-key)
551 (evil-define-key 'normal map
552 (kbd "C-]") 'elopher-follow-closest-link
553 (kbd "C-t") 'elopher-back
554 (kbd "u") 'elopher-back
555 (kbd "g") 'elopher-go
556 (kbd "r") 'elopher-reload
557 (kbd "w") 'elopher-view-raw
558 (kbd "d") 'elopher-download))
560 "Keymap for gopher client.")
562 (define-derived-mode elopher-mode special-mode "elopher"
563 "Major mode for elopher, an elisp gopher client.")
566 ;;; Main start procedure
570 "Start elopher with default landing page."
572 (setq elopher-current-node nil)
573 (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
574 (elopher-visit-node start-node))
575 "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
577 ;;; elopher.el ends here