1 ;;; elopher.el --- elisp gopher client
5 ;; An elisp gopher client.
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: redraw current page (using cached contents if available)\tfake\tfake\t1"
34 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
35 "i - d: download directory entry under cursor\tfake\tfake\t1"
36 "i - w: display the raw server response for the current page\tfake\tfake\t1"
38 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
40 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
42 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
43 "isearch terms:\tfake\tfake\t1"
45 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
48 "Source for elopher start page.")
51 ;;; Customization group
58 (defface elopher-index
59 '((((background dark)) :foreground "deep sky blue")
60 (((background light)) :foreground "blue"))
61 "Face used for index records.")
64 '((((background dark)) :foreground "white")
65 (((background light)) :weight bold))
66 "Face used for text records.")
68 (defface elopher-info '()
69 "Face used for info records.")
71 (defface elopher-image
72 '((((background dark)) :foreground "green")
73 (t :foreground "dark green"))
74 "Face used for image records.")
76 (defface elopher-search
77 '((((background light)) :foreground "orange")
78 (((background dark)) :foreground "dark orange"))
79 "Face used for search records.")
82 '((((background dark)) :foreground "yellow")
83 (((background light)) :foreground "dark red"))
84 "Face used for url records.")
86 (defface elopher-binary
87 '((t :foreground "magenta"))
88 "Face used for binary records.")
90 (defface elopher-unknown
91 '((t :foreground "red"))
92 "Face used for unknown record types.")
94 (defface elopher-margin-key
95 '((((background dark)) :foreground "white"))
96 "Face used for margin key.")
98 (defface elopher-margin-brackets
99 '((t :foreground "blue"))
100 "Face used for brackets around margin key.")
102 (defcustom elopher-open-urls-with-eww nil
103 "If non-nil, open URL selectors using eww.
104 Otherwise, use the system browser via the BROWSE-URL function."
112 (defun elopher-make-address (selector host port)
113 "Create an address of a gopher object with SELECTOR, HOST and PORT."
114 (list selector host port))
116 (defun elopher-address-selector (address)
117 "Retrieve selector from ADDRESS."
120 (defun elopher-address-host (address)
121 "Retrieve host from ADDRESS."
124 (defun elopher-address-port (address)
125 "Retrieve port from ADDRESS."
130 (defun elopher-make-node (parent address getter &optional content pos)
131 "Create a node in the gopher page hierarchy.
133 PARENT specifies the parent of the node, ADDRESS specifies the address of
134 the gopher page, GETTER provides the getter function used to obtain this
137 The optional arguments CONTENT and POS can be used to fill the cached
138 content and cursor position fields of the node."
139 (list parent address getter content pos))
141 (defun elopher-node-parent (node)
142 "Retrieve the parent node of NODE."
145 (defun elopher-node-address (node)
146 "Retrieve the address of NODE."
149 (defun elopher-node-getter (node)
150 "Retrieve the preferred getter function of NODE."
153 (defun elopher-node-content (node)
154 "Retrieve the cached content of NODE, or nil if none exists."
157 (defun elopher-node-pos (node)
158 "Retrieve the cached cursor position for NODE, or nil if none exists."
161 (defun elopher-set-node-content (node content)
162 "Set the content cache of NODE to CONTENT."
163 (setcar (nthcdr 3 node) content))
165 (defun elopher-set-node-pos (node pos)
166 "Set the cursor position cache of NODE to POS."
167 (setcar (nthcdr 4 node) pos))
169 (defun elopher-save-pos ()
170 "Save the current position of point to the current node."
171 (when elopher-current-node
172 (elopher-set-node-pos elopher-current-node (point))))
174 (defun elopher-restore-pos ()
175 "Restore the position of point to that cached in the current node."
176 (let ((pos (elopher-node-pos elopher-current-node)))
179 (goto-char (point-min)))))
181 ;; Node graph traversal
183 (defvar elopher-current-node)
185 (defun elopher-visit-node (node &optional getter)
186 "Visit NODE using its own getter or GETTER, if non-nil."
188 (elopher-process-cleanup)
189 (setq elopher-current-node node)
192 (funcall (elopher-node-getter node))))
194 (defun elopher-visit-parent-node ()
195 "Visit the parent of the current node."
196 (let ((parent-node (elopher-node-parent elopher-current-node)))
198 (elopher-visit-node parent-node))))
200 (defun elopher-reload-current-node ()
201 "Reload the current node, discarding any existing cached content."
202 (elopher-set-node-content elopher-current-node nil)
203 (elopher-visit-node elopher-current-node))
205 ;;; Buffer preparation
208 (defmacro elopher-with-clean-buffer (&rest args)
209 "Evaluate ARGS with a clean *elopher* buffer as current."
211 '(switch-to-buffer "*elopher*")
213 (append (list 'let '((inhibit-read-only t))
220 (defun elopher-insert-index (string)
221 "Insert the index corresponding to STRING into the current buffer."
222 (dolist (line (split-string string "\r\n"))
223 (unless (= (length line) 0)
224 (elopher-insert-index-record line))))
226 (defun elopher-insert-margin (&optional type-name)
227 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
230 (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
232 (propertize "[" 'face 'elopher-margin-brackets)
233 (propertize type-name 'face 'elopher-margin-key)
234 (propertize "]" 'face 'elopher-margin-brackets))))
236 (insert (make-string elopher-margin-width ?\s))))
238 (defvar elopher-type-map
239 '((?0 elopher-get-text-node "T" elopher-text)
240 (?1 elopher-get-index-node "/" elopher-index)
241 (?g elopher-get-image-node "im" elopher-image)
242 (?p elopher-get-image-node "im" elopher-image)
243 (?I elopher-get-image-node "im" elopher-image)
244 (?4 elopher-get-node-download "B" elopher-binary)
245 (?5 elopher-get-node-download "B" elopher-binary)
246 (?9 elopher-get-node-download "B" elopher-binary)
247 (?7 elopher-get-search-node "?" elopher-search))
248 "Association list from types to getters, margin codes and index faces.")
250 (defun elopher-insert-index-record (line)
251 "Insert the index record corresponding to LINE into the current buffer."
252 (let* ((type (elt line 0))
253 (fields (split-string (substring line 1) "\t"))
254 (display-string (elt fields 0))
255 (selector (elt fields 1))
256 (host (elt fields 2))
257 (port (elt fields 3))
258 (address (elopher-make-address selector host port))
259 (type-map-entry (alist-get type elopher-type-map)))
261 (let ((getter (car type-map-entry))
262 (margin-code (cadr type-map-entry))
263 (face (caddr type-map-entry)))
264 (elopher-insert-margin margin-code)
265 (insert-text-button display-string
267 'elopher-node (elopher-make-node elopher-current-node
270 'action #'elopher-click-link
272 'help-echo (format "mouse-1, RET: open %s on %s port %s"
273 selector host port)))
275 (?i (elopher-insert-margin) ; Information
276 (insert (propertize display-string
277 'face 'elopher-info)))
278 (?h (elopher-insert-margin "W") ; Web link
279 (let ((url (elt (split-string selector "URL:") 1)))
280 (insert-text-button display-string
283 'action #'elopher-click-url
285 'help-echo (format "mouse-1, RET: open url %s" url))))
286 (?.) ; Occurs at end of index, can safely ignore.
287 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
288 (insert (propertize display-string
289 'face 'elopher-unknown-face)))))
293 ;;; Selector retrieval (all kinds)
296 (defun elopher-process-cleanup ()
297 "Immediately shut down any extant elopher process."
298 (let ((p (get-process "elopher-process")))
299 (if p (delete-process p))))
301 (defvar elopher-selector-string)
303 (defun elopher-get-selector (address after)
304 "Retrieve selector specified by ADDRESS, then execute AFTER.
305 The result is stored as a string in the variable elopher-selector-string."
306 (setq elopher-selector-string "")
307 (make-network-process
308 :name "elopher-process"
309 :host (elopher-address-host address)
310 :service (elopher-address-port address)
311 :filter (lambda (proc string)
312 (setq elopher-selector-string (concat elopher-selector-string string)))
314 (process-send-string "elopher-process"
315 (concat (elopher-address-selector address) "\n")))
319 (defun elopher-get-index-node ()
320 (let ((content (elopher-node-content elopher-current-node))
321 (address (elopher-node-address elopher-current-node)))
324 (elopher-with-clean-buffer
326 (elopher-restore-pos))
329 (elopher-with-clean-buffer
330 (insert "LOADING DIRECTORY..."))
331 (elopher-get-selector address
333 (unless (string-prefix-p "deleted" event)
334 (elopher-with-clean-buffer
335 (elopher-insert-index elopher-selector-string))
336 (elopher-restore-pos)
337 (elopher-set-node-content elopher-current-node
340 (elopher-with-clean-buffer
341 (elopher-insert-index elopher-start-index))
342 (elopher-restore-pos)
343 (elopher-set-node-content elopher-current-node
344 (buffer-string)))))))
348 (defconst elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
349 "Regexp used to locate and buttinofy URLs in text files loaded by elopher.")
351 (defun elopher-buttonify-urls (string)
352 "Turn substrings which look like urls in STRING into clickable buttons."
355 (goto-char (point-min))
356 (while (re-search-forward elopher-url-regex nil t)
357 (let ((url (match-string 0))
358 (protocol (downcase (match-string 1))))
359 (if (string= protocol "gopher")
360 (let* ((host (match-string 2))
362 (type-and-selector (match-string 4))
363 (type (if (> (length type-and-selector) 1)
364 (elt type-and-selector 1)
366 (selector (if (> (length type-and-selector) 1)
367 (substring type-and-selector 2)
369 (address (elopher-make-address selector host port))
370 (getter (car (alist-get type elopher-type-map))))
371 (make-text-button (match-beginning 0)
373 'elopher-node (elopher-make-node elopher-current-node
376 'action #'elopher-click-link
378 'help-echo (format "mouse-1, RET: open %s on %s port %s"
379 selector host port)))
380 (make-text-button (match-beginning 0)
383 'action #'elopher-click-url
385 'help-echo (format "mouse-1, RET: open url %s" url)))))
388 (defun elopher-process-text (string)
389 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
390 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
391 (elopher-buttonify-urls cleaned-str)))
393 (defun elopher-get-text-node ()
394 (let ((content (elopher-node-content elopher-current-node))
395 (address (elopher-node-address elopher-current-node)))
398 (elopher-with-clean-buffer
400 (elopher-restore-pos))
402 (elopher-with-clean-buffer
403 (insert "LOADING TEXT..."))
404 (elopher-get-selector address
406 (unless (string-prefix-p "deleted" event)
407 (elopher-with-clean-buffer
408 (insert (elopher-process-text elopher-selector-string)))
409 (elopher-restore-pos)
410 (elopher-set-node-content elopher-current-node
411 (buffer-string)))))))))
415 (defun elopher-get-image-node ()
416 (let ((content (elopher-node-content elopher-current-node))
417 (address (elopher-node-address elopher-current-node)))
420 (elopher-with-clean-buffer
421 (insert-image content))
422 (setq cursor-type nil)
423 (elopher-restore-pos))
425 (elopher-with-clean-buffer
426 (insert "LOADING IMAGE..."))
427 (elopher-get-selector address
429 (unless (string-prefix-p "deleted" event)
430 (let ((image (create-image
431 (string-as-unibyte elopher-selector-string)
433 (elopher-with-clean-buffer
434 (insert-image image))
435 (setq cursor-type nil)
436 (elopher-restore-pos)
437 (elopher-set-node-content elopher-current-node
442 (defun elopher-get-search-node ()
443 (let ((content (elopher-node-content elopher-current-node))
444 (address (elopher-node-address elopher-current-node))
448 (elopher-with-clean-buffer
450 (elopher-restore-pos)
451 (message "Displaying cached search results. Reload to perform a new search."))
453 (let* ((query-string (read-string "Query: "))
454 (query-selector (concat (elopher-address-selector address) "\t" query-string))
455 (search-address (elopher-make-address query-selector
456 (elopher-address-host address)
457 (elopher-address-port address))))
459 (elopher-with-clean-buffer
460 (insert "LOADING RESULTS..."))
461 (elopher-get-selector search-address
463 (unless (string-prefix-p "deleted" event)
464 (elopher-with-clean-buffer
465 (elopher-insert-index elopher-selector-string))
466 (goto-char (point-min))
467 (elopher-set-node-content elopher-current-node
470 (elopher-visit-parent-node))))))
472 ;; Raw server response retrieval
474 (defun elopher-get-node-raw ()
475 (let* ((content (elopher-node-content elopher-current-node))
476 (address (elopher-node-address elopher-current-node)))
477 (elopher-with-clean-buffer
478 (insert "LOADING RAW SERVER RESPONSE..."))
480 (elopher-get-selector address
482 (unless (string-prefix-p "deleted" event)
483 (elopher-with-clean-buffer
484 (insert elopher-selector-string))
485 (goto-char (point-min)))))
487 (elopher-with-clean-buffer
488 (insert elopher-start-index))
489 (goto-char (point-min)))))
490 (message "Displaying raw server response. Reload or redraw to return to standard view."))
492 ;; File export retrieval
494 (defvar elopher-download-filename)
496 (defun elopher-get-node-download ()
497 (let* ((address (elopher-node-address elopher-current-node))
498 (selector (elopher-address-selector address)))
499 (elopher-visit-parent-node) ; Do first in case of non-local exits.
500 (let* ((filename-proposal (file-name-nondirectory selector))
501 (filename (read-file-name "Save file as: "
503 (if (> (length filename-proposal) 0)
506 (message "Downloading...")
507 (setq elopher-download-filename filename)
508 (elopher-get-selector address
510 (let ((coding-system-for-write 'binary))
511 (with-temp-file elopher-download-filename
512 (insert elopher-selector-string)
513 (message (format "Download complate, saved to file %s."
514 elopher-download-filename)))))))))
517 ;;; Navigation procedures
520 (defun elopher-next-link ()
524 (defun elopher-prev-link ()
528 (defun elopher-click-link (button)
529 (let ((node (button-get button 'elopher-node)))
530 (elopher-visit-node node)))
532 (defun elopher-click-url (button)
533 (let ((url (button-get button 'elopher-url)))
534 (if elopher-open-urls-with-eww
538 (defun elopher-follow-current-link ()
539 "Open the link or url at point."
544 "Go to a particular gopher site."
547 (hostname (read-string "Gopher host: "))
548 (selector (read-string "Selector (default none): " nil nil ""))
549 (port (read-string "Port (default 70): " nil nil 70))
550 (address (list selector hostname port)))
552 (elopher-make-node elopher-current-node
554 #'elopher-get-index-node))))
556 (defun elopher-redraw ()
557 "Redraw current page."
559 (elopher-visit-node elopher-current-node))
561 (defun elopher-reload ()
562 "Reload current page."
564 (elopher-reload-current-node))
566 (defun elopher-view-raw ()
567 "View current page as plain text."
569 (elopher-visit-node elopher-current-node
570 #'elopher-get-node-raw))
572 (defun elopher-back ()
573 "Go to previous site."
575 (if (elopher-node-parent elopher-current-node)
576 (elopher-visit-parent-node)
577 (message "No previous site.")))
579 (defun elopher-download ()
580 "Download the link at point."
582 (let ((button (button-at (point))))
584 (let ((node (button-get button 'elopher-node)))
586 (elopher-visit-node (button-get button 'elopher-node)
587 #'elopher-get-node-download)
588 (message "Can only download gopher links, not general URLs.")))
589 (message "No link selected."))))
594 (defvar elopher-mode-map
595 (let ((map (make-sparse-keymap)))
596 (define-key map (kbd "TAB") 'elopher-next-link)
597 (define-key map (kbd "<backtab>") 'elopher-prev-link)
598 (define-key map (kbd "u") 'elopher-back)
599 (define-key map (kbd "g") 'elopher-go)
600 (define-key map (kbd "r") 'elopher-redraw)
601 (define-key map (kbd "R") 'elopher-reload)
602 (define-key map (kbd "w") 'elopher-view-raw)
603 (define-key map (kbd "d") 'elopher-download)
604 (when (fboundp 'evil-define-key)
605 (evil-define-key 'normal map
606 (kbd "TAB") 'elopher-next-link
607 (kbd "C-]") 'elopher-follow-current-link
608 (kbd "C-t") 'elopher-back
609 (kbd "u") 'elopher-back
610 (kbd "g") 'elopher-go
611 (kbd "r") 'elopher-redraw
612 (kbd "R") 'elopher-reload
613 (kbd "w") 'elopher-view-raw
614 (kbd "d") 'elopher-download))
616 "Keymap for gopher client.")
618 (define-derived-mode elopher-mode special-mode "elopher"
619 "Major mode for elopher, an elisp gopher client.")
622 ;;; Main start procedure
626 "Start elopher with default landing page."
628 (setq elopher-current-node nil)
629 (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
630 (elopher-visit-node start-node))
631 "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
633 ;;; elopher.el ends here