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 aims to provide a full-featured gopher client for GNU Emacs.
32 ;; - intuitive keyboard and mouse-driven browsing,
33 ;; - caching of visited sites (both content and cursor position),
34 ;; - pleasant and configurable colouring of Gopher directories,
35 ;; - direct visualisation of image files,
36 ;; - clickable web and gopher links in plain text.
38 ;; The caching mechanism works by maintaining a hierarchy of visited
39 ;; pages rather than a linear history, meaning that it is quick and
40 ;; easy to navigate this history.
42 ;; To launch Elpher, simply use 'M-x elpher'. This will open a start
43 ;; page containing information on key bindings and suggested starting
44 ;; points for your gopher exploration.
46 ;; Faces, caching options and start page can be configured via
47 ;; the Elpher customization group in Applications.
56 (defconst elpher-version "1.0.0"
57 "Current version of elpher.")
59 (defconst elpher-margin-width 6
60 "Width of left-hand margin used when rendering indicies.")
62 (defconst elpher-start-index
65 (list "i\tfake\tfake\t1"
66 "i--------------------------------------------\tfake\tfake\t1"
67 "i Elpher Gopher Client \tfake\tfake\t1"
68 (format "i version %s\tfake\tfake\t1" elpher-version)
69 "i--------------------------------------------\tfake\tfake\t1"
71 "iBasic usage:\tfake\tfake\t1"
73 "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
74 "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
75 "i - u: return to parent directory entry\tfake\tfake\t1"
76 "i - g: go to a particular page\tfake\tfake\t1"
77 "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
78 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
79 "i - d: download directory entry under cursor\tfake\tfake\t1"
80 "i - w: display the raw server response for the current page\tfake\tfake\t1"
82 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
84 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
86 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
87 "isearch terms:\tfake\tfake\t1"
89 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
92 "Source for elpher start page.")
94 (defconst elpher-type-map
95 '((?0 elpher-get-text-node "T" elpher-text)
96 (?1 elpher-get-index-node "/" elpher-index)
97 (?g elpher-get-image-node "im" elpher-image)
98 (?p elpher-get-image-node "im" elpher-image)
99 (?I elpher-get-image-node "im" elpher-image)
100 (?4 elpher-get-node-download "B" elpher-binary)
101 (?5 elpher-get-node-download "B" elpher-binary)
102 (?9 elpher-get-node-download "B" elpher-binary)
103 (?7 elpher-get-search-node "?" elpher-search))
104 "Association list from types to getters, margin codes and index faces.")
107 ;;; Customization group
112 :group 'applications)
114 ;; Face customizations
116 (defface elpher-index
117 '((((background dark)) :foreground "deep sky blue")
118 (((background light)) :foreground "blue"))
119 "Face used for index records.")
122 '((((background dark)) :foreground "white")
123 (((background light)) :weight bold))
124 "Face used for text records.")
126 (defface elpher-info '()
127 "Face used for info records.")
129 (defface elpher-image
130 '((((background dark)) :foreground "green")
131 (t :foreground "dark green"))
132 "Face used for image records.")
134 (defface elpher-search
135 '((((background light)) :foreground "orange")
136 (((background dark)) :foreground "dark orange"))
137 "Face used for search records.")
140 '((((background dark)) :foreground "yellow")
141 (((background light)) :foreground "dark red"))
142 "Face used for url records.")
144 (defface elpher-binary
145 '((t :foreground "magenta"))
146 "Face used for binary records.")
148 (defface elpher-unknown
149 '((t :foreground "red"))
150 "Face used for unknown record types.")
152 (defface elpher-margin-key
153 '((((background dark)) :foreground "white"))
154 "Face used for margin key.")
156 (defface elpher-margin-brackets
157 '((t :foreground "blue"))
158 "Face used for brackets around margin key.")
160 ;; Other customizations
162 (defcustom elpher-open-urls-with-eww nil
163 "If non-nil, open URL selectors using eww.
164 Otherwise, use the system browser via the BROWSE-URL function."
167 (defcustom elpher-cache-images nil
168 "If non-nil, cache images in memory in the same way as other content."
171 (defcustom elpher-start-address nil
172 "If nil, the default start directory is shown when Elpher is started.
173 Otherwise, a list containing the selector, host and port of a directory to
174 use as the start page."
175 :type '(list string string integer))
183 (defun elpher-make-address (selector host port)
184 "Create an address of a gopher object with SELECTOR, HOST and PORT."
185 (list selector host port))
187 (defun elpher-address-selector (address)
188 "Retrieve selector from ADDRESS."
191 (defun elpher-address-host (address)
192 "Retrieve host from ADDRESS."
195 (defun elpher-address-port (address)
196 "Retrieve port from ADDRESS."
201 (defun elpher-make-node (parent address getter &optional content pos)
202 "Create a node in the gopher page hierarchy.
204 PARENT specifies the parent of the node, ADDRESS specifies the address of
205 the gopher page, GETTER provides the getter function used to obtain this
208 The optional arguments CONTENT and POS can be used to fill the cached
209 content and cursor position fields of the node."
210 (list parent address getter content pos))
212 (defun elpher-node-parent (node)
213 "Retrieve the parent node of NODE."
216 (defun elpher-node-address (node)
217 "Retrieve the address of NODE."
220 (defun elpher-node-getter (node)
221 "Retrieve the preferred getter function of NODE."
224 (defun elpher-node-content (node)
225 "Retrieve the cached content of NODE, or nil if none exists."
228 (defun elpher-node-pos (node)
229 "Retrieve the cached cursor position for NODE, or nil if none exists."
232 (defun elpher-set-node-content (node content)
233 "Set the content cache of NODE to CONTENT."
234 (setcar (nthcdr 3 node) content))
236 (defun elpher-set-node-pos (node pos)
237 "Set the cursor position cache of NODE to POS."
238 (setcar (nthcdr 4 node) pos))
240 ;; Node graph traversal
242 (defvar elpher-current-node)
244 (defun elpher-visit-node (node &optional getter)
245 "Visit NODE using its own getter or GETTER, if non-nil."
247 (elpher-process-cleanup)
248 (setq elpher-current-node node)
251 (funcall (elpher-node-getter node))))
253 (defun elpher-visit-parent-node ()
254 "Visit the parent of the current node."
255 (let ((parent-node (elpher-node-parent elpher-current-node)))
257 (elpher-visit-node parent-node))))
259 (defun elpher-reload-current-node ()
260 "Reload the current node, discarding any existing cached content."
261 (elpher-set-node-content elpher-current-node nil)
262 (elpher-visit-node elpher-current-node))
264 (defun elpher-save-pos ()
265 "Save the current position of point to the current node."
266 (when elpher-current-node
267 (elpher-set-node-pos elpher-current-node (point))))
269 (defun elpher-restore-pos ()
270 "Restore the position of point to that cached in the current node."
271 (let ((pos (elpher-node-pos elpher-current-node)))
274 (goto-char (point-min)))))
276 ;;; Buffer preparation
279 (defmacro elpher-with-clean-buffer (&rest args)
280 "Evaluate ARGS with a clean *elpher* buffer as current."
282 '(switch-to-buffer "*elpher*")
284 (append (list 'let '((inhibit-read-only t))
291 (defun elpher-insert-index (string)
292 "Insert the index corresponding to STRING into the current buffer."
293 (dolist (line (split-string string "\r\n"))
294 (unless (= (length line) 0)
295 (elpher-insert-index-record line))))
297 (defun elpher-insert-margin (&optional type-name)
298 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
301 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
303 (propertize "[" 'face 'elpher-margin-brackets)
304 (propertize type-name 'face 'elpher-margin-key)
305 (propertize "]" 'face 'elpher-margin-brackets))))
307 (insert (make-string elpher-margin-width ?\s))))
309 (defun elpher-insert-index-record (line)
310 "Insert the index record corresponding to LINE into the current buffer."
311 (let* ((type (elt line 0))
312 (fields (split-string (substring line 1) "\t"))
313 (display-string (elt fields 0))
314 (selector (elt fields 1))
315 (host (elt fields 2))
316 (port (elt fields 3))
317 (address (elpher-make-address selector host port))
318 (type-map-entry (alist-get type elpher-type-map)))
320 (let ((getter (car type-map-entry))
321 (margin-code (cadr type-map-entry))
322 (face (caddr type-map-entry)))
323 (elpher-insert-margin margin-code)
324 (insert-text-button display-string
326 'elpher-node (elpher-make-node elpher-current-node
329 'action #'elpher-click-link
331 'help-echo (format "mouse-1, RET: open %s on %s port %s"
332 selector host port)))
334 (?i (elpher-insert-margin) ; Information
335 (insert (propertize display-string
336 'face 'elpher-info)))
337 (?h (elpher-insert-margin "W") ; Web link
338 (let ((url (elt (split-string selector "URL:") 1)))
339 (insert-text-button display-string
342 'action #'elpher-click-url
344 'help-echo (format "mouse-1, RET: open url %s" url))))
345 (?.) ; Occurs at end of index, can safely ignore.
346 (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
347 (insert (propertize display-string
348 'face 'elpher-unknown-face)))))
352 ;;; Selector retrieval (all kinds)
355 (defun elpher-process-cleanup ()
356 "Immediately shut down any extant elpher process."
357 (let ((p (get-process "elpher-process")))
358 (if p (delete-process p))))
360 (defvar elpher-selector-string)
362 (defun elpher-get-selector (address after)
363 "Retrieve selector specified by ADDRESS, then execute AFTER.
364 The result is stored as a string in the variable ‘elpher-selector-string’."
365 (setq elpher-selector-string "")
366 (make-network-process
367 :name "elpher-process"
368 :host (elpher-address-host address)
369 :service (elpher-address-port address)
370 :filter (lambda (proc string)
371 (setq elpher-selector-string (concat elpher-selector-string string)))
373 (process-send-string "elpher-process"
374 (concat (elpher-address-selector address) "\n")))
378 (defun elpher-get-index-node ()
379 "Getter which retrieves the current node contents as an index."
380 (let ((content (elpher-node-content elpher-current-node))
381 (address (elpher-node-address elpher-current-node)))
384 (elpher-with-clean-buffer
386 (elpher-restore-pos))
389 (elpher-with-clean-buffer
390 (insert "LOADING DIRECTORY..."))
391 (elpher-get-selector address
393 (unless (string-prefix-p "deleted" event)
394 (elpher-with-clean-buffer
395 (elpher-insert-index elpher-selector-string))
397 (elpher-set-node-content elpher-current-node
400 (elpher-with-clean-buffer
401 (elpher-insert-index elpher-start-index))
403 (elpher-set-node-content elpher-current-node
404 (buffer-string)))))))
408 (defconst elpher-url-regex
409 "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
410 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
412 (defun elpher-buttonify-urls (string)
413 "Turn substrings which look like urls in STRING into clickable buttons."
416 (goto-char (point-min))
417 (while (re-search-forward elpher-url-regex nil t)
418 (let ((url (match-string 0))
419 (protocol (downcase (match-string 1))))
420 (if (string= protocol "gopher")
421 (let* ((host (match-string 2))
423 (type-and-selector (match-string 4))
424 (type (if (> (length type-and-selector) 1)
425 (elt type-and-selector 1)
427 (selector (if (> (length type-and-selector) 1)
428 (substring type-and-selector 2)
430 (address (elpher-make-address selector host port))
431 (getter (car (alist-get type elpher-type-map))))
432 (make-text-button (match-beginning 0)
434 'elpher-node (elpher-make-node elpher-current-node
437 'action #'elpher-click-link
439 'help-echo (format "mouse-1, RET: open %s on %s port %s"
440 selector host port)))
441 (make-text-button (match-beginning 0)
444 'action #'elpher-click-url
446 'help-echo (format "mouse-1, RET: open url %s" url)))))
449 (defun elpher-process-text (string)
450 "Remove CRs and trailing period from the gopher text document STRING."
451 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
452 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
453 (elpher-buttonify-urls cleaned-str)))
455 (defun elpher-get-text-node ()
456 "Getter which retrieves the current node contents as a text document."
457 (let ((content (elpher-node-content elpher-current-node))
458 (address (elpher-node-address elpher-current-node)))
461 (elpher-with-clean-buffer
463 (elpher-restore-pos))
465 (elpher-with-clean-buffer
466 (insert "LOADING TEXT..."))
467 (elpher-get-selector address
469 (unless (string-prefix-p "deleted" event)
470 (elpher-with-clean-buffer
471 (insert (elpher-process-text elpher-selector-string)))
473 (elpher-set-node-content elpher-current-node
474 (buffer-string)))))))))
478 (defun elpher-get-image-node ()
479 "Getter which retrieves the current node contents as an image to view."
480 (let ((content (elpher-node-content elpher-current-node))
481 (address (elpher-node-address elpher-current-node)))
484 (elpher-with-clean-buffer
485 (insert-image content))
486 (setq cursor-type nil)
487 (elpher-restore-pos))
488 (if (display-images-p)
490 (elpher-with-clean-buffer
491 (insert "LOADING IMAGE..."))
492 (elpher-get-selector address
494 (unless (string-prefix-p "deleted" event)
495 (let ((image (create-image
496 (encode-coding-string elpher-selector-string
499 (elpher-with-clean-buffer
500 (insert-image image))
501 (setq cursor-type nil)
503 (if elpher-cache-images
504 (elpher-set-node-content elpher-current-node
506 (elpher-get-node-download)))))
510 (defun elpher-get-search-node ()
511 "Getter which submits a search query to the address of the current node."
512 (let ((content (elpher-node-content elpher-current-node))
513 (address (elpher-node-address elpher-current-node))
517 (elpher-with-clean-buffer
520 (message "Displaying cached search results. Reload to perform a new search."))
522 (let* ((query-string (read-string "Query: "))
523 (query-selector (concat (elpher-address-selector address) "\t" query-string))
524 (search-address (elpher-make-address query-selector
525 (elpher-address-host address)
526 (elpher-address-port address))))
528 (elpher-with-clean-buffer
529 (insert "LOADING RESULTS..."))
530 (elpher-get-selector search-address
532 (unless (string-prefix-p "deleted" event)
533 (elpher-with-clean-buffer
534 (elpher-insert-index elpher-selector-string))
535 (goto-char (point-min))
536 (elpher-set-node-content elpher-current-node
539 (elpher-visit-parent-node))))))
541 ;; Raw server response retrieval
543 (defun elpher-get-node-raw ()
544 "Getter which retrieves the raw server response for the current node."
545 (let* ((content (elpher-node-content elpher-current-node))
546 (address (elpher-node-address elpher-current-node)))
547 (elpher-with-clean-buffer
548 (insert "LOADING RAW SERVER RESPONSE..."))
550 (elpher-get-selector address
552 (unless (string-prefix-p "deleted" event)
553 (elpher-with-clean-buffer
554 (insert elpher-selector-string))
555 (goto-char (point-min)))))
557 (elpher-with-clean-buffer
558 (insert elpher-start-index))
559 (goto-char (point-min)))))
560 (message "Displaying raw server response. Reload or redraw to return to standard view."))
562 ;; File export retrieval
564 (defvar elpher-download-filename)
566 (defun elpher-get-node-download ()
567 "Getter which retrieves the current node and writes the result to a file."
568 (let* ((address (elpher-node-address elpher-current-node))
569 (selector (elpher-address-selector address)))
570 (elpher-visit-parent-node) ; Do first in case of non-local exits.
571 (let* ((filename-proposal (file-name-nondirectory selector))
572 (filename (read-file-name "Save file as: "
574 (if (> (length filename-proposal) 0)
577 (message "Downloading...")
578 (setq elpher-download-filename filename)
579 (elpher-get-selector address
581 (let ((coding-system-for-write 'binary))
582 (with-temp-file elpher-download-filename
583 (insert elpher-selector-string)
584 (message (format "Download complate, saved to file %s."
585 elpher-download-filename)))))))))
588 ;;; Navigation procedures
591 (defun elpher-next-link ()
592 "Move point to the next link on the current page."
596 (defun elpher-prev-link ()
597 "Move point to the previous link on the current page."
601 (defun elpher-click-link (button)
602 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
603 (let ((node (button-get button 'elpher-node)))
604 (elpher-visit-node node)))
606 (defun elpher-click-url (button)
607 "Function called when the url link BUTTON is activated (via mouse or keypress)."
608 (let ((url (button-get button 'elpher-url)))
609 (if elpher-open-urls-with-eww
613 (defun elpher-follow-current-link ()
614 "Open the link or url at point."
619 "Go to a particular gopher site."
622 (hostname (read-string "Gopher host: "))
623 (selector (read-string "Selector (default none): " nil nil ""))
624 (port (read-string "Port (default 70): " nil nil 70))
625 (address (list selector hostname port)))
627 (elpher-make-node elpher-current-node
629 #'elpher-get-index-node))))
631 (defun elpher-redraw ()
632 "Redraw current page."
634 (elpher-visit-node elpher-current-node))
636 (defun elpher-reload ()
637 "Reload current page."
639 (elpher-reload-current-node))
641 (defun elpher-view-raw ()
642 "View current page as plain text."
644 (elpher-visit-node elpher-current-node
645 #'elpher-get-node-raw))
647 (defun elpher-back ()
648 "Go to previous site."
650 (if (elpher-node-parent elpher-current-node)
651 (elpher-visit-parent-node)
652 (message "No previous site.")))
654 (defun elpher-download ()
655 "Download the link at point."
657 (let ((button (button-at (point))))
659 (let ((node (button-get button 'elpher-node)))
661 (elpher-visit-node (button-get button 'elpher-node)
662 #'elpher-get-node-download)
663 (message "Can only download gopher links, not general URLs.")))
664 (message "No link selected."))))
669 (defvar elpher-mode-map
670 (let ((map (make-sparse-keymap)))
671 (define-key map (kbd "TAB") 'elpher-next-link)
672 (define-key map (kbd "<backtab>") 'elpher-prev-link)
673 (define-key map (kbd "u") 'elpher-back)
674 (define-key map (kbd "g") 'elpher-go)
675 (define-key map (kbd "r") 'elpher-redraw)
676 (define-key map (kbd "R") 'elpher-reload)
677 (define-key map (kbd "w") 'elpher-view-raw)
678 (define-key map (kbd "d") 'elpher-download)
679 (when (fboundp 'evil-define-key)
680 (evil-define-key 'normal map
681 (kbd "TAB") 'elpher-next-link
682 (kbd "C-]") 'elpher-follow-current-link
683 (kbd "C-t") 'elpher-back
684 (kbd "u") 'elpher-back
686 (kbd "r") 'elpher-redraw
687 (kbd "R") 'elpher-reload
688 (kbd "w") 'elpher-view-raw
689 (kbd "d") 'elpher-download))
691 "Keymap for gopher client.")
693 (define-derived-mode elpher-mode special-mode "elpher"
694 "Major mode for elpher, an elisp gopher client.")
697 ;;; Main start procedure
702 "Start elpher with default landing page."
704 (setq elpher-current-node nil)
705 (let ((start-node (elpher-make-node nil
707 #'elpher-get-index-node)))
708 (elpher-visit-node start-node))
709 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
711 ;;; elpher.el ends here