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 ;; - (m)enu key support, similar to Emacs' info browser,
37 ;; - clickable web and gopher links in plain text.
39 ;; The caching mechanism works by maintaining a hierarchy of visited
40 ;; pages rather than a linear history, meaning that it is quick and
41 ;; easy to navigate this history.
43 ;; To launch Elpher, simply use 'M-x elpher'. This will open a start
44 ;; page containing information on key bindings and suggested starting
45 ;; points for your gopher exploration.
47 ;; Faces, caching options and start page can be configured via
48 ;; the Elpher customization group in Applications.
57 (defconst elpher-version "1.0.0"
58 "Current version of elpher.")
60 (defconst elpher-margin-width 6
61 "Width of left-hand margin used when rendering indicies.")
63 (defconst elpher-start-index
66 (list "i\tfake\tfake\t1"
67 "i--------------------------------------------\tfake\tfake\t1"
68 "i Elpher Gopher Client \tfake\tfake\t1"
69 (format "i version %s\tfake\tfake\t1" elpher-version)
70 "i--------------------------------------------\tfake\tfake\t1"
72 "iBasic usage:\tfake\tfake\t1"
74 "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
75 "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
76 "i - m: select a directory entry by name (autocompletes)\tfake\tfake\t1"
77 "i - u: return to parent directory entry\tfake\tfake\t1"
78 "i - g: go to a particular page\tfake\tfake\t1"
79 "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
80 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
81 "i - d: download directory entry under cursor\tfake\tfake\t1"
82 "i - w: display the raw server response for the current page\tfake\tfake\t1"
84 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
86 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
88 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
89 "isearch terms:\tfake\tfake\t1"
91 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
94 "Source for elpher start page.")
96 (defconst elpher-type-map
97 '((?0 elpher-get-text-node "T" elpher-text)
98 (?1 elpher-get-index-node "/" elpher-index)
99 (?g elpher-get-image-node "im" elpher-image)
100 (?p elpher-get-image-node "im" elpher-image)
101 (?I elpher-get-image-node "im" elpher-image)
102 (?4 elpher-get-node-download "B" elpher-binary)
103 (?5 elpher-get-node-download "B" elpher-binary)
104 (?9 elpher-get-node-download "B" elpher-binary)
105 (?7 elpher-get-search-node "?" elpher-search))
106 "Association list from types to getters, margin codes and index faces.")
109 ;;; Customization group
114 :group 'applications)
116 ;; Face customizations
118 (defface elpher-index
119 '((((background dark)) :foreground "deep sky blue")
120 (((background light)) :foreground "blue"))
121 "Face used for index records.")
124 '((((background dark)) :foreground "white")
125 (((background light)) :weight bold))
126 "Face used for text records.")
128 (defface elpher-info '()
129 "Face used for info records.")
131 (defface elpher-image
132 '((((background dark)) :foreground "green")
133 (t :foreground "dark green"))
134 "Face used for image records.")
136 (defface elpher-search
137 '((((background light)) :foreground "orange")
138 (((background dark)) :foreground "dark orange"))
139 "Face used for search records.")
142 '((((background dark)) :foreground "yellow")
143 (((background light)) :foreground "dark red"))
144 "Face used for url records.")
146 (defface elpher-binary
147 '((t :foreground "magenta"))
148 "Face used for binary records.")
150 (defface elpher-unknown
151 '((t :foreground "red"))
152 "Face used for unknown record types.")
154 (defface elpher-margin-key
155 '((((background dark)) :foreground "white"))
156 "Face used for margin key.")
158 (defface elpher-margin-brackets
159 '((t :foreground "blue"))
160 "Face used for brackets around margin key.")
162 ;; Other customizations
164 (defcustom elpher-open-urls-with-eww nil
165 "If non-nil, open URL selectors using eww.
166 Otherwise, use the system browser via the BROWSE-URL function."
169 (defcustom elpher-cache-images nil
170 "If non-nil, cache images in memory in the same way as other content."
173 (defcustom elpher-start-address nil
174 "If nil, the default start directory is shown when Elpher is started.
175 Otherwise, a list containing the selector, host and port of a directory to
176 use as the start page."
177 :type '(list string string integer))
185 (defun elpher-make-address (selector host port)
186 "Create an address of a gopher object with SELECTOR, HOST and PORT."
187 (list selector host port))
189 (defun elpher-address-selector (address)
190 "Retrieve selector from ADDRESS."
193 (defun elpher-address-host (address)
194 "Retrieve host from ADDRESS."
197 (defun elpher-address-port (address)
198 "Retrieve port from ADDRESS."
203 (defun elpher-make-node (parent address getter &optional content pos)
204 "Create a node in the gopher page hierarchy.
206 PARENT specifies the parent of the node, ADDRESS specifies the address of
207 the gopher page, GETTER provides the getter function used to obtain this
210 The optional arguments CONTENT and POS can be used to fill the cached
211 content and cursor position fields of the node."
212 (list parent address getter content pos))
214 (defun elpher-node-parent (node)
215 "Retrieve the parent node of NODE."
218 (defun elpher-node-address (node)
219 "Retrieve the address of NODE."
222 (defun elpher-node-getter (node)
223 "Retrieve the preferred getter function of NODE."
226 (defun elpher-node-content (node)
227 "Retrieve the cached content of NODE, or nil if none exists."
230 (defun elpher-node-pos (node)
231 "Retrieve the cached cursor position for NODE, or nil if none exists."
234 (defun elpher-set-node-content (node content)
235 "Set the content cache of NODE to CONTENT."
236 (setcar (nthcdr 3 node) content))
238 (defun elpher-set-node-pos (node pos)
239 "Set the cursor position cache of NODE to POS."
240 (setcar (nthcdr 4 node) pos))
242 ;; Node graph traversal
244 (defvar elpher-current-node)
246 (defun elpher-visit-node (node &optional getter)
247 "Visit NODE using its own getter or GETTER, if non-nil."
249 (elpher-process-cleanup)
250 (setq elpher-current-node node)
253 (funcall (elpher-node-getter node))))
255 (defun elpher-visit-parent-node ()
256 "Visit the parent of the current node."
257 (let ((parent-node (elpher-node-parent elpher-current-node)))
259 (elpher-visit-node parent-node))))
261 (defun elpher-reload-current-node ()
262 "Reload the current node, discarding any existing cached content."
263 (elpher-set-node-content elpher-current-node nil)
264 (elpher-visit-node elpher-current-node))
266 (defun elpher-save-pos ()
267 "Save the current position of point to the current node."
268 (when elpher-current-node
269 (elpher-set-node-pos elpher-current-node (point))))
271 (defun elpher-restore-pos ()
272 "Restore the position of point to that cached in the current node."
273 (let ((pos (elpher-node-pos elpher-current-node)))
276 (goto-char (point-min)))))
278 ;;; Buffer preparation
281 (defmacro elpher-with-clean-buffer (&rest args)
282 "Evaluate ARGS with a clean *elpher* buffer as current."
284 '(switch-to-buffer "*elpher*")
286 (append (list 'let '((inhibit-read-only t))
293 (defun elpher-insert-index (string)
294 "Insert the index corresponding to STRING into the current buffer."
295 (dolist (line (split-string string "\r\n"))
296 (unless (= (length line) 0)
297 (elpher-insert-index-record line))))
299 (defun elpher-insert-margin (&optional type-name)
300 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
303 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
305 (propertize "[" 'face 'elpher-margin-brackets)
306 (propertize type-name 'face 'elpher-margin-key)
307 (propertize "]" 'face 'elpher-margin-brackets))))
309 (insert (make-string elpher-margin-width ?\s))))
311 (defun elpher-insert-index-record (line)
312 "Insert the index record corresponding to LINE into the current buffer."
313 (let* ((type (elt line 0))
314 (fields (split-string (substring line 1) "\t"))
315 (display-string (elt fields 0))
316 (selector (elt fields 1))
317 (host (elt fields 2))
318 (port (elt fields 3))
319 (address (elpher-make-address selector host port))
320 (type-map-entry (alist-get type elpher-type-map)))
322 (let ((getter (car type-map-entry))
323 (margin-code (cadr type-map-entry))
324 (face (caddr type-map-entry)))
325 (elpher-insert-margin margin-code)
326 (insert-text-button display-string
328 'elpher-node (elpher-make-node elpher-current-node
331 'action #'elpher-click-link
333 'help-echo (format "mouse-1, RET: open %s on %s port %s"
334 selector host port)))
336 (?i (elpher-insert-margin) ; Information
337 (insert (propertize display-string
338 'face 'elpher-info)))
339 (?h (elpher-insert-margin "W") ; Web link
340 (let ((url (elt (split-string selector "URL:") 1)))
341 (insert-text-button display-string
344 'action #'elpher-click-url
346 'help-echo (format "mouse-1, RET: open url %s" url))))
347 (?.) ; Occurs at end of index, can safely ignore.
348 (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
349 (insert (propertize display-string
350 'face 'elpher-unknown-face)))))
354 ;;; Selector retrieval (all kinds)
357 (defun elpher-process-cleanup ()
358 "Immediately shut down any extant elpher process."
359 (let ((p (get-process "elpher-process")))
360 (if p (delete-process p))))
362 (defvar elpher-selector-string)
364 (defun elpher-get-selector (address after)
365 "Retrieve selector specified by ADDRESS, then execute AFTER.
366 The result is stored as a string in the variable ‘elpher-selector-string’."
367 (setq elpher-selector-string "")
368 (make-network-process
369 :name "elpher-process"
370 :host (elpher-address-host address)
371 :service (elpher-address-port address)
372 :filter (lambda (proc string)
373 (setq elpher-selector-string (concat elpher-selector-string string)))
375 (process-send-string "elpher-process"
376 (concat (elpher-address-selector address) "\n")))
380 (defun elpher-get-index-node ()
381 "Getter which retrieves the current node contents as an index."
382 (let ((content (elpher-node-content elpher-current-node))
383 (address (elpher-node-address elpher-current-node)))
386 (elpher-with-clean-buffer
388 (elpher-restore-pos))
391 (elpher-with-clean-buffer
392 (insert "LOADING DIRECTORY..."))
393 (elpher-get-selector address
395 (unless (string-prefix-p "deleted" event)
396 (elpher-with-clean-buffer
397 (elpher-insert-index elpher-selector-string))
399 (elpher-set-node-content elpher-current-node
402 (elpher-with-clean-buffer
403 (elpher-insert-index elpher-start-index))
405 (elpher-set-node-content elpher-current-node
406 (buffer-string)))))))
410 (defconst elpher-url-regex
411 "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
412 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
414 (defun elpher-buttonify-urls (string)
415 "Turn substrings which look like urls in STRING into clickable buttons."
418 (goto-char (point-min))
419 (while (re-search-forward elpher-url-regex nil t)
420 (let ((url (match-string 0))
421 (protocol (downcase (match-string 1))))
422 (if (string= protocol "gopher")
423 (let* ((host (match-string 2))
425 (type-and-selector (match-string 4))
426 (type (if (> (length type-and-selector) 1)
427 (elt type-and-selector 1)
429 (selector (if (> (length type-and-selector) 1)
430 (substring type-and-selector 2)
432 (address (elpher-make-address selector host port))
433 (getter (car (alist-get type elpher-type-map))))
434 (make-text-button (match-beginning 0)
436 'elpher-node (elpher-make-node elpher-current-node
439 'action #'elpher-click-link
441 'help-echo (format "mouse-1, RET: open %s on %s port %s"
442 selector host port)))
443 (make-text-button (match-beginning 0)
446 'action #'elpher-click-url
448 'help-echo (format "mouse-1, RET: open url %s" url)))))
451 (defun elpher-process-text (string)
452 "Remove CRs and trailing period from the gopher text document STRING."
453 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
454 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
455 (elpher-buttonify-urls cleaned-str)))
457 (defun elpher-get-text-node ()
458 "Getter which retrieves the current node contents as a text document."
459 (let ((content (elpher-node-content elpher-current-node))
460 (address (elpher-node-address elpher-current-node)))
463 (elpher-with-clean-buffer
465 (elpher-restore-pos))
467 (elpher-with-clean-buffer
468 (insert "LOADING TEXT..."))
469 (elpher-get-selector address
471 (unless (string-prefix-p "deleted" event)
472 (elpher-with-clean-buffer
473 (insert (elpher-process-text elpher-selector-string)))
475 (elpher-set-node-content elpher-current-node
476 (buffer-string)))))))))
480 (defun elpher-get-image-node ()
481 "Getter which retrieves the current node contents as an image to view."
482 (let ((content (elpher-node-content elpher-current-node))
483 (address (elpher-node-address elpher-current-node)))
486 (elpher-with-clean-buffer
487 (insert-image content))
488 (setq cursor-type nil)
489 (elpher-restore-pos))
490 (if (display-images-p)
492 (elpher-with-clean-buffer
493 (insert "LOADING IMAGE..."))
494 (elpher-get-selector address
496 (unless (string-prefix-p "deleted" event)
497 (let ((image (create-image
498 (encode-coding-string elpher-selector-string
501 (elpher-with-clean-buffer
502 (insert-image image))
503 (setq cursor-type nil)
505 (if elpher-cache-images
506 (elpher-set-node-content elpher-current-node
508 (elpher-get-node-download)))))
512 (defun elpher-get-search-node ()
513 "Getter which submits a search query to the address of the current node."
514 (let ((content (elpher-node-content elpher-current-node))
515 (address (elpher-node-address elpher-current-node))
519 (elpher-with-clean-buffer
522 (message "Displaying cached search results. Reload to perform a new search."))
524 (let* ((query-string (read-string "Query: "))
525 (query-selector (concat (elpher-address-selector address) "\t" query-string))
526 (search-address (elpher-make-address query-selector
527 (elpher-address-host address)
528 (elpher-address-port address))))
530 (elpher-with-clean-buffer
531 (insert "LOADING RESULTS..."))
532 (elpher-get-selector search-address
534 (unless (string-prefix-p "deleted" event)
535 (elpher-with-clean-buffer
536 (elpher-insert-index elpher-selector-string))
537 (goto-char (point-min))
538 (elpher-set-node-content elpher-current-node
541 (elpher-visit-parent-node))))))
543 ;; Raw server response retrieval
545 (defun elpher-get-node-raw ()
546 "Getter which retrieves the raw server response for the current node."
547 (let* ((content (elpher-node-content elpher-current-node))
548 (address (elpher-node-address elpher-current-node)))
549 (elpher-with-clean-buffer
550 (insert "LOADING RAW SERVER RESPONSE..."))
552 (elpher-get-selector address
554 (unless (string-prefix-p "deleted" event)
555 (elpher-with-clean-buffer
556 (insert elpher-selector-string))
557 (goto-char (point-min)))))
559 (elpher-with-clean-buffer
560 (insert elpher-start-index))
561 (goto-char (point-min)))))
562 (message "Displaying raw server response. Reload or redraw to return to standard view."))
564 ;; File export retrieval
566 (defvar elpher-download-filename)
568 (defun elpher-get-node-download ()
569 "Getter which retrieves the current node and writes the result to a file."
570 (let* ((address (elpher-node-address elpher-current-node))
571 (selector (elpher-address-selector address)))
572 (elpher-visit-parent-node) ; Do first in case of non-local exits.
573 (let* ((filename-proposal (file-name-nondirectory selector))
574 (filename (read-file-name "Save file as: "
576 (if (> (length filename-proposal) 0)
579 (message "Downloading...")
580 (setq elpher-download-filename filename)
581 (elpher-get-selector address
583 (let ((coding-system-for-write 'binary))
584 (with-temp-file elpher-download-filename
585 (insert elpher-selector-string)
586 (message (format "Download complate, saved to file %s."
587 elpher-download-filename)))))))))
590 ;;; Navigation procedures
593 (defun elpher-next-link ()
594 "Move point to the next link on the current page."
598 (defun elpher-prev-link ()
599 "Move point to the previous link on the current page."
603 (defun elpher-click-link (button)
604 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
605 (let ((node (button-get button 'elpher-node)))
606 (elpher-visit-node node)))
608 (defun elpher-click-url (button)
609 "Function called when the url link BUTTON is activated (via mouse or keypress)."
610 (let ((url (button-get button 'elpher-url)))
611 (if elpher-open-urls-with-eww
615 (defun elpher-follow-current-link ()
616 "Open the link or url at point."
621 "Go to a particular gopher site."
624 (hostname (read-string "Gopher host: "))
625 (selector (read-string "Selector (default none): " nil nil ""))
626 (port (read-string "Port (default 70): " nil nil 70))
627 (address (list selector hostname port)))
629 (elpher-make-node elpher-current-node
631 #'elpher-get-index-node))))
633 (defun elpher-redraw ()
634 "Redraw current page."
636 (elpher-visit-node elpher-current-node))
638 (defun elpher-reload ()
639 "Reload current page."
641 (elpher-reload-current-node))
643 (defun elpher-view-raw ()
644 "View current page as plain text."
646 (elpher-visit-node elpher-current-node
647 #'elpher-get-node-raw))
649 (defun elpher-back ()
650 "Go to previous site."
652 (if (elpher-node-parent elpher-current-node)
653 (elpher-visit-parent-node)
654 (message "No previous site.")))
656 (defun elpher-download ()
657 "Download the link at point."
659 (let ((button (button-at (point))))
661 (let ((node (button-get button 'elpher-node)))
663 (elpher-visit-node (button-get button 'elpher-node)
664 #'elpher-get-node-download)
665 (message "Can only download gopher links, not general URLs.")))
666 (message "No link selected."))))
668 (defun elpher-build-link-map ()
669 "Build alist mapping link names to destination nodes in current buffer."
671 (b (next-button (point-min) t)))
673 (add-to-list 'link-map (cons (button-label b) b))
674 (setq b (next-button (button-start b))))
677 (defun elpher-menu ()
678 "Select a directory entry by name. Similar to the info browser (m)enu command."
680 (let* ((link-map (elpher-build-link-map)))
682 (let ((key (let ((completion-ignore-case t))
683 (completing-read "Directory entry/link (tab to autocomplete): " link-map nil t))))
684 (if (and key (> (length key) 0))
685 (let ((b (cdr (assoc key link-map))))
686 (goto-char (button-start b))
687 (button-activate b)))))))
692 (defvar elpher-mode-map
693 (let ((map (make-sparse-keymap)))
694 (define-key map (kbd "TAB") 'elpher-next-link)
695 (define-key map (kbd "<backtab>") 'elpher-prev-link)
696 (define-key map (kbd "u") 'elpher-back)
697 (define-key map (kbd "g") 'elpher-go)
698 (define-key map (kbd "r") 'elpher-redraw)
699 (define-key map (kbd "R") 'elpher-reload)
700 (define-key map (kbd "w") 'elpher-view-raw)
701 (define-key map (kbd "d") 'elpher-download)
702 (define-key map (kbd "m") 'elpher-menu)
703 (when (fboundp 'evil-define-key)
704 (evil-define-key 'normal map
705 (kbd "TAB") 'elpher-next-link
706 (kbd "C-]") 'elpher-follow-current-link
707 (kbd "C-t") 'elpher-back
708 (kbd "u") 'elpher-back
710 (kbd "r") 'elpher-redraw
711 (kbd "R") 'elpher-reload
712 (kbd "w") 'elpher-view-raw
713 (kbd "d") 'elpher-download
714 (kbd "m") 'elpher-menu))
716 "Keymap for gopher client.")
718 (define-derived-mode elpher-mode special-mode "elpher"
719 "Major mode for elpher, an elisp gopher client.")
722 ;;; Main start procedure
727 "Start elpher with default landing page."
729 (setq elpher-current-node nil)
730 (let ((start-node (elpher-make-node nil
732 #'elpher-get-index-node)))
733 (elpher-visit-node start-node))
734 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
736 ;;; elpher.el ends here