1 ;;; elpher.el --- Full-featured gopher client.
3 ;; Copyright (C) 2019 Tim Vaughan
5 ;; Author: Tim Vaughan <tgvaughan@gmail.com>
6 ;; Created: 11 April 2019
8 ;; Keywords: comm gopher
9 ;; Homepage: https://github.com/tgvaughan/elpher
10 ;; Package-Requires: ((emacs "25"))
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with this file. If not, see <http://www.gnu.org/licenses/>.
29 ;; Elpher is a tool for exploring "gopherspace" using GNU Emacs.
38 (defconst elpher-version "1.0.0"
39 "Current version of elpher.")
41 (defconst elpher-margin-width 6
42 "Width of left-hand margin used when rendering indicies.")
44 (defconst elpher-start-index
47 (list "i\tfake\tfake\t1"
48 "i--------------------------------------------\tfake\tfake\t1"
49 "i Elpher Gopher Client \tfake\tfake\t1"
50 (format "i version %s\tfake\tfake\t1" elpher-version)
51 "i--------------------------------------------\tfake\tfake\t1"
53 "iBasic usage:\tfake\tfake\t1"
55 "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
56 "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
57 "i - u: return to parent directory entry\tfake\tfake\t1"
58 "i - g: go to a particular page\tfake\tfake\t1"
59 "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
60 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
61 "i - d: download directory entry under cursor\tfake\tfake\t1"
62 "i - w: display the raw server response for the current page\tfake\tfake\t1"
64 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
66 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
68 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
69 "isearch terms:\tfake\tfake\t1"
71 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
74 "Source for elpher start page.")
77 ;;; Customization group
85 '((((background dark)) :foreground "deep sky blue")
86 (((background light)) :foreground "blue"))
87 "Face used for index records.")
90 '((((background dark)) :foreground "white")
91 (((background light)) :weight bold))
92 "Face used for text records.")
94 (defface elpher-info '()
95 "Face used for info records.")
98 '((((background dark)) :foreground "green")
99 (t :foreground "dark green"))
100 "Face used for image records.")
102 (defface elpher-search
103 '((((background light)) :foreground "orange")
104 (((background dark)) :foreground "dark orange"))
105 "Face used for search records.")
108 '((((background dark)) :foreground "yellow")
109 (((background light)) :foreground "dark red"))
110 "Face used for url records.")
112 (defface elpher-binary
113 '((t :foreground "magenta"))
114 "Face used for binary records.")
116 (defface elpher-unknown
117 '((t :foreground "red"))
118 "Face used for unknown record types.")
120 (defface elpher-margin-key
121 '((((background dark)) :foreground "white"))
122 "Face used for margin key.")
124 (defface elpher-margin-brackets
125 '((t :foreground "blue"))
126 "Face used for brackets around margin key.")
128 (defcustom elpher-open-urls-with-eww nil
129 "If non-nil, open URL selectors using eww.
130 Otherwise, use the system browser via the BROWSE-URL function."
133 (defcustom elpher-cache-images nil
134 "If non-nil, cache images in memory in the same way as other content."
142 (defun elpher-make-address (selector host port)
143 "Create an address of a gopher object with SELECTOR, HOST and PORT."
144 (list selector host port))
146 (defun elpher-address-selector (address)
147 "Retrieve selector from ADDRESS."
150 (defun elpher-address-host (address)
151 "Retrieve host from ADDRESS."
154 (defun elpher-address-port (address)
155 "Retrieve port from ADDRESS."
160 (defun elpher-make-node (parent address getter &optional content pos)
161 "Create a node in the gopher page hierarchy.
163 PARENT specifies the parent of the node, ADDRESS specifies the address of
164 the gopher page, GETTER provides the getter function used to obtain this
167 The optional arguments CONTENT and POS can be used to fill the cached
168 content and cursor position fields of the node."
169 (list parent address getter content pos))
171 (defun elpher-node-parent (node)
172 "Retrieve the parent node of NODE."
175 (defun elpher-node-address (node)
176 "Retrieve the address of NODE."
179 (defun elpher-node-getter (node)
180 "Retrieve the preferred getter function of NODE."
183 (defun elpher-node-content (node)
184 "Retrieve the cached content of NODE, or nil if none exists."
187 (defun elpher-node-pos (node)
188 "Retrieve the cached cursor position for NODE, or nil if none exists."
191 (defun elpher-set-node-content (node content)
192 "Set the content cache of NODE to CONTENT."
193 (setcar (nthcdr 3 node) content))
195 (defun elpher-set-node-pos (node pos)
196 "Set the cursor position cache of NODE to POS."
197 (setcar (nthcdr 4 node) pos))
199 ;; Node graph traversal
201 (defvar elpher-current-node)
203 (defun elpher-visit-node (node &optional getter)
204 "Visit NODE using its own getter or GETTER, if non-nil."
206 (elpher-process-cleanup)
207 (setq elpher-current-node node)
210 (funcall (elpher-node-getter node))))
212 (defun elpher-visit-parent-node ()
213 "Visit the parent of the current node."
214 (let ((parent-node (elpher-node-parent elpher-current-node)))
216 (elpher-visit-node parent-node))))
218 (defun elpher-reload-current-node ()
219 "Reload the current node, discarding any existing cached content."
220 (elpher-set-node-content elpher-current-node nil)
221 (elpher-visit-node elpher-current-node))
223 (defun elpher-save-pos ()
224 "Save the current position of point to the current node."
225 (when elpher-current-node
226 (elpher-set-node-pos elpher-current-node (point))))
228 (defun elpher-restore-pos ()
229 "Restore the position of point to that cached in the current node."
230 (let ((pos (elpher-node-pos elpher-current-node)))
233 (goto-char (point-min)))))
235 ;;; Buffer preparation
238 (defmacro elpher-with-clean-buffer (&rest args)
239 "Evaluate ARGS with a clean *elpher* buffer as current."
241 '(switch-to-buffer "*elpher*")
243 (append (list 'let '((inhibit-read-only t))
250 (defun elpher-insert-index (string)
251 "Insert the index corresponding to STRING into the current buffer."
252 (dolist (line (split-string string "\r\n"))
253 (unless (= (length line) 0)
254 (elpher-insert-index-record line))))
256 (defun elpher-insert-margin (&optional type-name)
257 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
260 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
262 (propertize "[" 'face 'elpher-margin-brackets)
263 (propertize type-name 'face 'elpher-margin-key)
264 (propertize "]" 'face 'elpher-margin-brackets))))
266 (insert (make-string elpher-margin-width ?\s))))
268 (defvar elpher-type-map
269 '((?0 elpher-get-text-node "T" elpher-text)
270 (?1 elpher-get-index-node "/" elpher-index)
271 (?g elpher-get-image-node "im" elpher-image)
272 (?p elpher-get-image-node "im" elpher-image)
273 (?I elpher-get-image-node "im" elpher-image)
274 (?4 elpher-get-node-download "B" elpher-binary)
275 (?5 elpher-get-node-download "B" elpher-binary)
276 (?9 elpher-get-node-download "B" elpher-binary)
277 (?7 elpher-get-search-node "?" elpher-search))
278 "Association list from types to getters, margin codes and index faces.")
280 (defun elpher-insert-index-record (line)
281 "Insert the index record corresponding to LINE into the current buffer."
282 (let* ((type (elt line 0))
283 (fields (split-string (substring line 1) "\t"))
284 (display-string (elt fields 0))
285 (selector (elt fields 1))
286 (host (elt fields 2))
287 (port (elt fields 3))
288 (address (elpher-make-address selector host port))
289 (type-map-entry (alist-get type elpher-type-map)))
291 (let ((getter (car type-map-entry))
292 (margin-code (cadr type-map-entry))
293 (face (caddr type-map-entry)))
294 (elpher-insert-margin margin-code)
295 (insert-text-button display-string
297 'elpher-node (elpher-make-node elpher-current-node
300 'action #'elpher-click-link
302 'help-echo (format "mouse-1, RET: open %s on %s port %s"
303 selector host port)))
305 (?i (elpher-insert-margin) ; Information
306 (insert (propertize display-string
307 'face 'elpher-info)))
308 (?h (elpher-insert-margin "W") ; Web link
309 (let ((url (elt (split-string selector "URL:") 1)))
310 (insert-text-button display-string
313 'action #'elpher-click-url
315 'help-echo (format "mouse-1, RET: open url %s" url))))
316 (?.) ; Occurs at end of index, can safely ignore.
317 (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
318 (insert (propertize display-string
319 'face 'elpher-unknown-face)))))
323 ;;; Selector retrieval (all kinds)
326 (defun elpher-process-cleanup ()
327 "Immediately shut down any extant elpher process."
328 (let ((p (get-process "elpher-process")))
329 (if p (delete-process p))))
331 (defvar elpher-selector-string)
333 (defun elpher-get-selector (address after)
334 "Retrieve selector specified by ADDRESS, then execute AFTER.
335 The result is stored as a string in the variable ‘elpher-selector-string’."
336 (setq elpher-selector-string "")
337 (make-network-process
338 :name "elpher-process"
339 :host (elpher-address-host address)
340 :service (elpher-address-port address)
341 :filter (lambda (proc string)
342 (setq elpher-selector-string (concat elpher-selector-string string)))
344 (process-send-string "elpher-process"
345 (concat (elpher-address-selector address) "\n")))
349 (defun elpher-get-index-node ()
350 "Getter which retrieves the current node contents as an index."
351 (let ((content (elpher-node-content elpher-current-node))
352 (address (elpher-node-address elpher-current-node)))
355 (elpher-with-clean-buffer
357 (elpher-restore-pos))
360 (elpher-with-clean-buffer
361 (insert "LOADING DIRECTORY..."))
362 (elpher-get-selector address
364 (unless (string-prefix-p "deleted" event)
365 (elpher-with-clean-buffer
366 (elpher-insert-index elpher-selector-string))
368 (elpher-set-node-content elpher-current-node
371 (elpher-with-clean-buffer
372 (elpher-insert-index elpher-start-index))
374 (elpher-set-node-content elpher-current-node
375 (buffer-string)))))))
379 (defconst elpher-url-regex
380 "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
381 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
383 (defun elpher-buttonify-urls (string)
384 "Turn substrings which look like urls in STRING into clickable buttons."
387 (goto-char (point-min))
388 (while (re-search-forward elpher-url-regex nil t)
389 (let ((url (match-string 0))
390 (protocol (downcase (match-string 1))))
391 (if (string= protocol "gopher")
392 (let* ((host (match-string 2))
394 (type-and-selector (match-string 4))
395 (type (if (> (length type-and-selector) 1)
396 (elt type-and-selector 1)
398 (selector (if (> (length type-and-selector) 1)
399 (substring type-and-selector 2)
401 (address (elpher-make-address selector host port))
402 (getter (car (alist-get type elpher-type-map))))
403 (make-text-button (match-beginning 0)
405 'elpher-node (elpher-make-node elpher-current-node
408 'action #'elpher-click-link
410 'help-echo (format "mouse-1, RET: open %s on %s port %s"
411 selector host port)))
412 (make-text-button (match-beginning 0)
415 'action #'elpher-click-url
417 'help-echo (format "mouse-1, RET: open url %s" url)))))
420 (defun elpher-process-text (string)
421 "Remove CRs and trailing period from the gopher text document STRING."
422 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
423 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
424 (elpher-buttonify-urls cleaned-str)))
426 (defun elpher-get-text-node ()
427 "Getter which retrieves the current node contents as a text document."
428 (let ((content (elpher-node-content elpher-current-node))
429 (address (elpher-node-address elpher-current-node)))
432 (elpher-with-clean-buffer
434 (elpher-restore-pos))
436 (elpher-with-clean-buffer
437 (insert "LOADING TEXT..."))
438 (elpher-get-selector address
440 (unless (string-prefix-p "deleted" event)
441 (elpher-with-clean-buffer
442 (insert (elpher-process-text elpher-selector-string)))
444 (elpher-set-node-content elpher-current-node
445 (buffer-string)))))))))
449 (defun elpher-get-image-node ()
450 "Getter which retrieves the current node contents as an image to view."
451 (let ((content (elpher-node-content elpher-current-node))
452 (address (elpher-node-address elpher-current-node)))
455 (elpher-with-clean-buffer
456 (insert-image content))
457 (setq cursor-type nil)
458 (elpher-restore-pos))
459 (if (display-images-p)
461 (elpher-with-clean-buffer
462 (insert "LOADING IMAGE..."))
463 (elpher-get-selector address
465 (unless (string-prefix-p "deleted" event)
466 (let ((image (create-image
467 (encode-coding-string elpher-selector-string
470 (elpher-with-clean-buffer
471 (insert-image image))
472 (setq cursor-type nil)
474 (if elpher-cache-images
475 (elpher-set-node-content elpher-current-node
477 (elpher-get-node-download)))))
481 (defun elpher-get-search-node ()
482 "Getter which submits a search query to the address of the current node."
483 (let ((content (elpher-node-content elpher-current-node))
484 (address (elpher-node-address elpher-current-node))
488 (elpher-with-clean-buffer
491 (message "Displaying cached search results. Reload to perform a new search."))
493 (let* ((query-string (read-string "Query: "))
494 (query-selector (concat (elpher-address-selector address) "\t" query-string))
495 (search-address (elpher-make-address query-selector
496 (elpher-address-host address)
497 (elpher-address-port address))))
499 (elpher-with-clean-buffer
500 (insert "LOADING RESULTS..."))
501 (elpher-get-selector search-address
503 (unless (string-prefix-p "deleted" event)
504 (elpher-with-clean-buffer
505 (elpher-insert-index elpher-selector-string))
506 (goto-char (point-min))
507 (elpher-set-node-content elpher-current-node
510 (elpher-visit-parent-node))))))
512 ;; Raw server response retrieval
514 (defun elpher-get-node-raw ()
515 "Getter which retrieves the raw server response for the current node."
516 (let* ((content (elpher-node-content elpher-current-node))
517 (address (elpher-node-address elpher-current-node)))
518 (elpher-with-clean-buffer
519 (insert "LOADING RAW SERVER RESPONSE..."))
521 (elpher-get-selector address
523 (unless (string-prefix-p "deleted" event)
524 (elpher-with-clean-buffer
525 (insert elpher-selector-string))
526 (goto-char (point-min)))))
528 (elpher-with-clean-buffer
529 (insert elpher-start-index))
530 (goto-char (point-min)))))
531 (message "Displaying raw server response. Reload or redraw to return to standard view."))
533 ;; File export retrieval
535 (defvar elpher-download-filename)
537 (defun elpher-get-node-download ()
538 "Getter which retrieves the current node and writes the result to a file."
539 (let* ((address (elpher-node-address elpher-current-node))
540 (selector (elpher-address-selector address)))
541 (elpher-visit-parent-node) ; Do first in case of non-local exits.
542 (let* ((filename-proposal (file-name-nondirectory selector))
543 (filename (read-file-name "Save file as: "
545 (if (> (length filename-proposal) 0)
548 (message "Downloading...")
549 (setq elpher-download-filename filename)
550 (elpher-get-selector address
552 (let ((coding-system-for-write 'binary))
553 (with-temp-file elpher-download-filename
554 (insert elpher-selector-string)
555 (message (format "Download complate, saved to file %s."
556 elpher-download-filename)))))))))
559 ;;; Navigation procedures
562 (defun elpher-next-link ()
563 "Move point to the next link on the current page."
567 (defun elpher-prev-link ()
568 "Move point to the previous link on the current page."
572 (defun elpher-click-link (button)
573 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
574 (let ((node (button-get button 'elpher-node)))
575 (elpher-visit-node node)))
577 (defun elpher-click-url (button)
578 "Function called when the url link BUTTON is activated (via mouse or keypress)."
579 (let ((url (button-get button 'elpher-url)))
580 (if elpher-open-urls-with-eww
584 (defun elpher-follow-current-link ()
585 "Open the link or url at point."
590 "Go to a particular gopher site."
593 (hostname (read-string "Gopher host: "))
594 (selector (read-string "Selector (default none): " nil nil ""))
595 (port (read-string "Port (default 70): " nil nil 70))
596 (address (list selector hostname port)))
598 (elpher-make-node elpher-current-node
600 #'elpher-get-index-node))))
602 (defun elpher-redraw ()
603 "Redraw current page."
605 (elpher-visit-node elpher-current-node))
607 (defun elpher-reload ()
608 "Reload current page."
610 (elpher-reload-current-node))
612 (defun elpher-view-raw ()
613 "View current page as plain text."
615 (elpher-visit-node elpher-current-node
616 #'elpher-get-node-raw))
618 (defun elpher-back ()
619 "Go to previous site."
621 (if (elpher-node-parent elpher-current-node)
622 (elpher-visit-parent-node)
623 (message "No previous site.")))
625 (defun elpher-download ()
626 "Download the link at point."
628 (let ((button (button-at (point))))
630 (let ((node (button-get button 'elpher-node)))
632 (elpher-visit-node (button-get button 'elpher-node
633 #'elpher-get-node-download))
634 (message "Can only download gopher links, not general URLs.")))
635 (message "No link selected."))))
640 (defvar elpher-mode-map
641 (let ((map (make-sparse-keymap)))
642 (define-key map (kbd "TAB") 'elpher-next-link)
643 (define-key map (kbd "<backtab>") 'elpher-prev-link)
644 (define-key map (kbd "u") 'elpher-back)
645 (define-key map (kbd "g") 'elpher-go)
646 (define-key map (kbd "r") 'elpher-redraw)
647 (define-key map (kbd "R") 'elpher-reload)
648 (define-key map (kbd "w") 'elpher-view-raw)
649 (define-key map (kbd "d") 'elpher-download)
650 (when (fboundp 'evil-define-key)
651 (evil-define-key 'normal map
652 (kbd "TAB") 'elpher-next-link
653 (kbd "C-]") 'elpher-follow-current-link
654 (kbd "C-t") 'elpher-back
655 (kbd "u") 'elpher-back
657 (kbd "r") 'elpher-redraw
658 (kbd "R") 'elpher-reload
659 (kbd "w") 'elpher-view-raw
660 (kbd "d") 'elpher-download))
662 "Keymap for gopher client.")
664 (define-derived-mode elpher-mode special-mode "elpher"
665 "Major mode for elpher, an elisp gopher client.")
668 ;;; Main start procedure
673 "Start elpher with default landing page."
675 (setq elpher-current-node nil)
676 (let ((start-node (elpher-make-node nil nil #'elpher-get-index-node)))
677 (elpher-visit-node start-node))
678 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
680 ;;; elpher.el ends here