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-node-download "B" ,elopher-binary-face)
207 (?5 elopher-get-node-download "B" ,elopher-binary-face)
208 (?9 elopher-get-node-download "B" ,elopher-binary-face)
209 (?7 elopher-get-search-node "?" ,elopher-search-face)))
211 (defun elopher-insert-index-record (line)
212 "Insert the index record corresponding to LINE into the current buffer."
213 (let* ((type (elt line 0))
214 (fields (split-string (substring line 1) "\t"))
215 (display-string (elt fields 0))
216 (selector (elt fields 1))
217 (host (elt fields 2))
218 (port (elt fields 3))
219 (address (elopher-make-address selector host port))
220 (type-map-entry (alist-get type elopher-type-map)))
222 (let ((getter (car type-map-entry))
223 (margin-code (cadr type-map-entry))
224 (face (caddr type-map-entry)))
225 (elopher-insert-margin margin-code)
226 (insert-text-button display-string
228 'elopher-node (elopher-make-node elopher-current-node
231 'action #'elopher-click-link
233 'help-echo (format "mouse-1, RET: open %s on %s port %s"
234 selector host port)))
236 (?i (elopher-insert-margin) ; Information
237 (insert (propertize display-string
238 'face elopher-info-face)))
239 (?h (elopher-insert-margin "W") ; Web link
240 (let ((url (elt (split-string selector "URL:") 1)))
241 (insert-text-button display-string
242 'face elopher-http-face
244 'action #'elopher-click-url
246 'help-echo (format "mouse-1, RET: open url %s" url))))
247 (?.) ; Occurs at end of index, can safely ignore.
248 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
249 (insert (propertize display-string
250 'face elopher-unknown-face)))))
254 ;;; Selector retrieval (all kinds)
257 (defun elopher-process-cleanup ()
258 "Immediately shut down any extant elopher process."
259 (let ((p (get-process "elopher-process")))
260 (if p (delete-process p))))
262 (defvar elopher-selector-string)
264 (defun elopher-get-selector (address after)
265 "Retrieve selector specified by ADDRESS, then execute AFTER.
266 The result is stored as a string in the variable elopher-selector-string."
267 (setq elopher-selector-string "")
268 (make-network-process
269 :name "elopher-process"
270 :host (elopher-address-host address)
271 :service (elopher-address-port address)
272 :filter (lambda (proc string)
273 (setq elopher-selector-string (concat elopher-selector-string string)))
275 (process-send-string "elopher-process"
276 (concat (elopher-address-selector address) "\n")))
280 (defun elopher-get-index-node ()
281 (let ((content (elopher-node-content elopher-current-node))
282 (address (elopher-node-address elopher-current-node)))
285 (elopher-with-clean-buffer
287 (elopher-restore-pos))
290 (elopher-with-clean-buffer
291 (insert "LOADING DIRECTORY..."))
292 (elopher-get-selector address
294 (unless (string-prefix-p "deleted" event)
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 (defvar elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)")
311 (defun elopher-buttonify-urls (string)
312 "Turn substrings which look like urls in STRING into clickable buttons."
315 (goto-char (point-min))
316 (while (re-search-forward elopher-url-regex nil t)
317 (let ((url (match-string 0))
318 (protocol (downcase (match-string 1))))
319 (if (string= protocol "gopher")
320 (let* ((host (match-string 2))
322 (type-and-selector (match-string 4))
323 (type (if (> (length type-and-selector) 1)
324 (elt type-and-selector 1)
326 (selector (if (> (length type-and-selector) 1)
327 (substring type-and-selector 2)
329 (address (elopher-make-address selector host port))
330 (getter (car (alist-get type elopher-type-map))))
331 (make-text-button (match-beginning 0)
333 'elopher-node (elopher-make-node elopher-current-node
336 'action #'elopher-click-link
338 'help-echo (format "mouse-1, RET: open %s on %s port %s"
339 selector host port)))
340 (make-text-button (match-beginning 0)
343 'action #'elopher-click-url
345 'help-echo (format "mouse-1, RET: open url %s" url)))))
348 (defun elopher-process-text (string)
349 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
350 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
351 (elopher-buttonify-urls cleaned-str)))
353 (defun elopher-get-text-node ()
354 (let ((content (elopher-node-content elopher-current-node))
355 (address (elopher-node-address elopher-current-node)))
358 (elopher-with-clean-buffer
360 (elopher-restore-pos))
362 (elopher-with-clean-buffer
363 (insert "LOADING TEXT..."))
364 (elopher-get-selector address
366 (unless (string-prefix-p "deleted" event)
367 (elopher-with-clean-buffer
368 (insert (elopher-process-text elopher-selector-string)))
369 (elopher-restore-pos)
370 (elopher-set-node-content elopher-current-node
371 (buffer-string)))))))))
375 (defun elopher-get-image-node ()
376 (let ((content (elopher-node-content elopher-current-node))
377 (address (elopher-node-address elopher-current-node)))
380 (elopher-with-clean-buffer
381 (insert-image content))
382 (setq cursor-type nil)
383 (elopher-restore-pos))
385 (elopher-with-clean-buffer
386 (insert "LOADING IMAGE..."))
387 (elopher-get-selector address
389 (unless (string-prefix-p "deleted" event)
390 (let ((image (create-image
391 (string-as-unibyte elopher-selector-string)
393 (elopher-with-clean-buffer
394 (insert-image image))
395 (setq cursor-type nil)
396 (elopher-restore-pos)
397 (elopher-set-node-content elopher-current-node
402 (defun elopher-get-search-node ()
403 (let* ((content (elopher-node-content elopher-current-node))
404 (address (elopher-node-address elopher-current-node)))
407 (elopher-with-clean-buffer
409 (elopher-restore-pos)
410 (message "Displaying cached search results. Reload to perform a new search."))
412 (let* ((query-string (read-string "Query: "))
413 (query-selector (concat (elopher-address-selector address) "\t" query-string))
414 (search-address (elopher-make-address query-selector
415 (elopher-address-host address)
416 (elopher-address-port address))))
417 (elopher-with-clean-buffer
418 (insert "LOADING RESULTS..."))
419 (elopher-get-selector search-address
421 (unless (string-prefix-p "deleted" event)
422 (elopher-with-clean-buffer
423 (elopher-insert-index elopher-selector-string))
424 (goto-char (point-min))
425 (elopher-set-node-content elopher-current-node
427 (elopher-visit-parent-node)))))
429 ;; Raw server response retrieval
431 (defun elopher-get-node-raw ()
432 (let* ((content (elopher-node-content elopher-current-node))
433 (address (elopher-node-address elopher-current-node)))
434 (elopher-with-clean-buffer
435 (insert "LOADING RAW SERVER RESPONSE..."))
437 (elopher-get-selector address
439 (unless (string-prefix-p "deleted" event)
440 (elopher-with-clean-buffer
441 (insert elopher-selector-string))
442 (goto-char (point-min)))))
444 (elopher-with-clean-buffer
445 (insert elopher-start-index))
446 (goto-char (point-min)))))
447 (message "Displaying raw server response. Reload to return to standard view."))
449 ;; File export retrieval
451 (defvar elopher-download-filename)
453 (defun elopher-get-node-download ()
454 (let* ((address (elopher-node-address elopher-current-node))
455 (selector (elopher-address-selector address)))
456 (elopher-visit-parent-node) ; Do first in case of non-local exits.
457 (let* ((filename-proposal (file-name-nondirectory selector))
458 (filename (read-file-name "Save file as: "
460 (if (> (length filename-proposal) 0)
463 (message "Downloading...")
464 (setq elopher-download-filename filename)
465 (elopher-get-selector address
467 (let ((coding-system-for-write 'binary))
468 (with-temp-file elopher-download-filename
469 (insert elopher-selector-string)
470 (message (format "Download complate, saved to file %s."
471 elopher-download-filename)))))))))
474 ;;; Navigation procedures
477 (defun elopher-next-link ()
481 (defun elopher-prev-link ()
485 (defun elopher-click-link (button)
486 (let ((node (button-get button 'elopher-node)))
487 (elopher-visit-node node)))
489 (defun elopher-click-url (button)
490 (let ((url (button-get button 'elopher-url)))
491 (if elopher-open-urls-with-eww
495 (defun elopher-follow-closest-link ()
500 "Go to a particular gopher site."
503 (hostname (read-string "Gopher host: "))
504 (selector (read-string "Selector (default none): " nil nil ""))
505 (port (read-string "Port (default 70): " nil nil 70))
506 (address (list selector hostname port)))
508 (elopher-make-node elopher-current-node
510 #'elopher-get-index-node))))
512 (defun elopher-reload ()
513 "Reload current page."
515 (elopher-reload-current-node))
517 (defun elopher-view-raw ()
518 "View current page as plain text."
520 (elopher-visit-node elopher-current-node
521 #'elopher-get-node-raw))
523 (defun elopher-back ()
524 "Go to previous site."
526 (if (elopher-node-parent elopher-current-node)
527 (elopher-visit-parent-node)
528 (message "No previous site.")))
530 (defun elopher-download ()
531 "Download the link at point."
533 (let ((button (button-at (point))))
535 (let ((node (button-get button 'elopher-node)))
537 (elopher-visit-node (button-get button 'elopher-node)
538 #'elopher-get-node-download)
539 (message "Can only download gopher links, not general URLs.")))
540 (message "No link selected."))))
545 (defvar elopher-mode-map
546 (let ((map (make-sparse-keymap)))
547 (define-key map (kbd "<tab>") 'elopher-next-link)
548 (define-key map (kbd "<S-tab>") 'elopher-prev-link)
549 (define-key map (kbd "u") 'elopher-back)
550 (define-key map (kbd "g") 'elopher-go)
551 (define-key map (kbd "r") 'elopher-reload)
552 (define-key map (kbd "w") 'elopher-view-raw)
553 (define-key map (kbd "d") 'elopher-download)
554 (when (fboundp 'evil-define-key)
555 (evil-define-key 'normal map
556 (kbd "C-]") 'elopher-follow-closest-link
557 (kbd "C-t") 'elopher-back
558 (kbd "u") 'elopher-back
559 (kbd "g") 'elopher-go
560 (kbd "r") 'elopher-reload
561 (kbd "w") 'elopher-view-raw
562 (kbd "d") 'elopher-download))
564 "Keymap for gopher client.")
566 (define-derived-mode elopher-mode special-mode "elopher"
567 "Major mode for elopher, an elisp gopher client.")
570 ;;; Main start procedure
574 "Start elopher with default landing page."
576 (setq elopher-current-node nil)
577 (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
578 (elopher-visit-node start-node))
579 "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
581 ;;; elopher.el ends here