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 (defface elpher-index
115 '((((background dark)) :foreground "deep sky blue")
116 (((background light)) :foreground "blue"))
117 "Face used for index records.")
120 '((((background dark)) :foreground "white")
121 (((background light)) :weight bold))
122 "Face used for text records.")
124 (defface elpher-info '()
125 "Face used for info records.")
127 (defface elpher-image
128 '((((background dark)) :foreground "green")
129 (t :foreground "dark green"))
130 "Face used for image records.")
132 (defface elpher-search
133 '((((background light)) :foreground "orange")
134 (((background dark)) :foreground "dark orange"))
135 "Face used for search records.")
138 '((((background dark)) :foreground "yellow")
139 (((background light)) :foreground "dark red"))
140 "Face used for url records.")
142 (defface elpher-binary
143 '((t :foreground "magenta"))
144 "Face used for binary records.")
146 (defface elpher-unknown
147 '((t :foreground "red"))
148 "Face used for unknown record types.")
150 (defface elpher-margin-key
151 '((((background dark)) :foreground "white"))
152 "Face used for margin key.")
154 (defface elpher-margin-brackets
155 '((t :foreground "blue"))
156 "Face used for brackets around margin key.")
158 (defcustom elpher-open-urls-with-eww nil
159 "If non-nil, open URL selectors using eww.
160 Otherwise, use the system browser via the BROWSE-URL function."
163 (defcustom elpher-cache-images nil
164 "If non-nil, cache images in memory in the same way as other content."
167 (defcustom elpher-start-address nil
168 "If nil, the default start directory is shown when Elpher is started.
169 Otherwise, a list containing the selector, host and port of a directory to
170 use as the start page."
171 :type '(list string string integer))
179 (defun elpher-make-address (selector host port)
180 "Create an address of a gopher object with SELECTOR, HOST and PORT."
181 (list selector host port))
183 (defun elpher-address-selector (address)
184 "Retrieve selector from ADDRESS."
187 (defun elpher-address-host (address)
188 "Retrieve host from ADDRESS."
191 (defun elpher-address-port (address)
192 "Retrieve port from ADDRESS."
197 (defun elpher-make-node (parent address getter &optional content pos)
198 "Create a node in the gopher page hierarchy.
200 PARENT specifies the parent of the node, ADDRESS specifies the address of
201 the gopher page, GETTER provides the getter function used to obtain this
204 The optional arguments CONTENT and POS can be used to fill the cached
205 content and cursor position fields of the node."
206 (list parent address getter content pos))
208 (defun elpher-node-parent (node)
209 "Retrieve the parent node of NODE."
212 (defun elpher-node-address (node)
213 "Retrieve the address of NODE."
216 (defun elpher-node-getter (node)
217 "Retrieve the preferred getter function of NODE."
220 (defun elpher-node-content (node)
221 "Retrieve the cached content of NODE, or nil if none exists."
224 (defun elpher-node-pos (node)
225 "Retrieve the cached cursor position for NODE, or nil if none exists."
228 (defun elpher-set-node-content (node content)
229 "Set the content cache of NODE to CONTENT."
230 (setcar (nthcdr 3 node) content))
232 (defun elpher-set-node-pos (node pos)
233 "Set the cursor position cache of NODE to POS."
234 (setcar (nthcdr 4 node) pos))
236 ;; Node graph traversal
238 (defvar elpher-current-node)
240 (defun elpher-visit-node (node &optional getter)
241 "Visit NODE using its own getter or GETTER, if non-nil."
243 (elpher-process-cleanup)
244 (setq elpher-current-node node)
247 (funcall (elpher-node-getter node))))
249 (defun elpher-visit-parent-node ()
250 "Visit the parent of the current node."
251 (let ((parent-node (elpher-node-parent elpher-current-node)))
253 (elpher-visit-node parent-node))))
255 (defun elpher-reload-current-node ()
256 "Reload the current node, discarding any existing cached content."
257 (elpher-set-node-content elpher-current-node nil)
258 (elpher-visit-node elpher-current-node))
260 (defun elpher-save-pos ()
261 "Save the current position of point to the current node."
262 (when elpher-current-node
263 (elpher-set-node-pos elpher-current-node (point))))
265 (defun elpher-restore-pos ()
266 "Restore the position of point to that cached in the current node."
267 (let ((pos (elpher-node-pos elpher-current-node)))
270 (goto-char (point-min)))))
272 ;;; Buffer preparation
275 (defmacro elpher-with-clean-buffer (&rest args)
276 "Evaluate ARGS with a clean *elpher* buffer as current."
278 '(switch-to-buffer "*elpher*")
280 (append (list 'let '((inhibit-read-only t))
287 (defun elpher-insert-index (string)
288 "Insert the index corresponding to STRING into the current buffer."
289 (dolist (line (split-string string "\r\n"))
290 (unless (= (length line) 0)
291 (elpher-insert-index-record line))))
293 (defun elpher-insert-margin (&optional type-name)
294 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
297 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
299 (propertize "[" 'face 'elpher-margin-brackets)
300 (propertize type-name 'face 'elpher-margin-key)
301 (propertize "]" 'face 'elpher-margin-brackets))))
303 (insert (make-string elpher-margin-width ?\s))))
305 (defun elpher-insert-index-record (line)
306 "Insert the index record corresponding to LINE into the current buffer."
307 (let* ((type (elt line 0))
308 (fields (split-string (substring line 1) "\t"))
309 (display-string (elt fields 0))
310 (selector (elt fields 1))
311 (host (elt fields 2))
312 (port (elt fields 3))
313 (address (elpher-make-address selector host port))
314 (type-map-entry (alist-get type elpher-type-map)))
316 (let ((getter (car type-map-entry))
317 (margin-code (cadr type-map-entry))
318 (face (caddr type-map-entry)))
319 (elpher-insert-margin margin-code)
320 (insert-text-button display-string
322 'elpher-node (elpher-make-node elpher-current-node
325 'action #'elpher-click-link
327 'help-echo (format "mouse-1, RET: open %s on %s port %s"
328 selector host port)))
330 (?i (elpher-insert-margin) ; Information
331 (insert (propertize display-string
332 'face 'elpher-info)))
333 (?h (elpher-insert-margin "W") ; Web link
334 (let ((url (elt (split-string selector "URL:") 1)))
335 (insert-text-button display-string
338 'action #'elpher-click-url
340 'help-echo (format "mouse-1, RET: open url %s" url))))
341 (?.) ; Occurs at end of index, can safely ignore.
342 (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
343 (insert (propertize display-string
344 'face 'elpher-unknown-face)))))
348 ;;; Selector retrieval (all kinds)
351 (defun elpher-process-cleanup ()
352 "Immediately shut down any extant elpher process."
353 (let ((p (get-process "elpher-process")))
354 (if p (delete-process p))))
356 (defvar elpher-selector-string)
358 (defun elpher-get-selector (address after)
359 "Retrieve selector specified by ADDRESS, then execute AFTER.
360 The result is stored as a string in the variable ‘elpher-selector-string’."
361 (setq elpher-selector-string "")
362 (make-network-process
363 :name "elpher-process"
364 :host (elpher-address-host address)
365 :service (elpher-address-port address)
366 :filter (lambda (proc string)
367 (setq elpher-selector-string (concat elpher-selector-string string)))
369 (process-send-string "elpher-process"
370 (concat (elpher-address-selector address) "\n")))
374 (defun elpher-get-index-node ()
375 "Getter which retrieves the current node contents as an index."
376 (let ((content (elpher-node-content elpher-current-node))
377 (address (elpher-node-address elpher-current-node)))
380 (elpher-with-clean-buffer
382 (elpher-restore-pos))
385 (elpher-with-clean-buffer
386 (insert "LOADING DIRECTORY..."))
387 (elpher-get-selector address
389 (unless (string-prefix-p "deleted" event)
390 (elpher-with-clean-buffer
391 (elpher-insert-index elpher-selector-string))
393 (elpher-set-node-content elpher-current-node
396 (elpher-with-clean-buffer
397 (elpher-insert-index elpher-start-index))
399 (elpher-set-node-content elpher-current-node
400 (buffer-string)))))))
404 (defconst elpher-url-regex
405 "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
406 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
408 (defun elpher-buttonify-urls (string)
409 "Turn substrings which look like urls in STRING into clickable buttons."
412 (goto-char (point-min))
413 (while (re-search-forward elpher-url-regex nil t)
414 (let ((url (match-string 0))
415 (protocol (downcase (match-string 1))))
416 (if (string= protocol "gopher")
417 (let* ((host (match-string 2))
419 (type-and-selector (match-string 4))
420 (type (if (> (length type-and-selector) 1)
421 (elt type-and-selector 1)
423 (selector (if (> (length type-and-selector) 1)
424 (substring type-and-selector 2)
426 (address (elpher-make-address selector host port))
427 (getter (car (alist-get type elpher-type-map))))
428 (make-text-button (match-beginning 0)
430 'elpher-node (elpher-make-node elpher-current-node
433 'action #'elpher-click-link
435 'help-echo (format "mouse-1, RET: open %s on %s port %s"
436 selector host port)))
437 (make-text-button (match-beginning 0)
440 'action #'elpher-click-url
442 'help-echo (format "mouse-1, RET: open url %s" url)))))
445 (defun elpher-process-text (string)
446 "Remove CRs and trailing period from the gopher text document STRING."
447 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
448 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
449 (elpher-buttonify-urls cleaned-str)))
451 (defun elpher-get-text-node ()
452 "Getter which retrieves the current node contents as a text document."
453 (let ((content (elpher-node-content elpher-current-node))
454 (address (elpher-node-address elpher-current-node)))
457 (elpher-with-clean-buffer
459 (elpher-restore-pos))
461 (elpher-with-clean-buffer
462 (insert "LOADING TEXT..."))
463 (elpher-get-selector address
465 (unless (string-prefix-p "deleted" event)
466 (elpher-with-clean-buffer
467 (insert (elpher-process-text elpher-selector-string)))
469 (elpher-set-node-content elpher-current-node
470 (buffer-string)))))))))
474 (defun elpher-get-image-node ()
475 "Getter which retrieves the current node contents as an image to view."
476 (let ((content (elpher-node-content elpher-current-node))
477 (address (elpher-node-address elpher-current-node)))
480 (elpher-with-clean-buffer
481 (insert-image content))
482 (setq cursor-type nil)
483 (elpher-restore-pos))
484 (if (display-images-p)
486 (elpher-with-clean-buffer
487 (insert "LOADING IMAGE..."))
488 (elpher-get-selector address
490 (unless (string-prefix-p "deleted" event)
491 (let ((image (create-image
492 (encode-coding-string elpher-selector-string
495 (elpher-with-clean-buffer
496 (insert-image image))
497 (setq cursor-type nil)
499 (if elpher-cache-images
500 (elpher-set-node-content elpher-current-node
502 (elpher-get-node-download)))))
506 (defun elpher-get-search-node ()
507 "Getter which submits a search query to the address of the current node."
508 (let ((content (elpher-node-content elpher-current-node))
509 (address (elpher-node-address elpher-current-node))
513 (elpher-with-clean-buffer
516 (message "Displaying cached search results. Reload to perform a new search."))
518 (let* ((query-string (read-string "Query: "))
519 (query-selector (concat (elpher-address-selector address) "\t" query-string))
520 (search-address (elpher-make-address query-selector
521 (elpher-address-host address)
522 (elpher-address-port address))))
524 (elpher-with-clean-buffer
525 (insert "LOADING RESULTS..."))
526 (elpher-get-selector search-address
528 (unless (string-prefix-p "deleted" event)
529 (elpher-with-clean-buffer
530 (elpher-insert-index elpher-selector-string))
531 (goto-char (point-min))
532 (elpher-set-node-content elpher-current-node
535 (elpher-visit-parent-node))))))
537 ;; Raw server response retrieval
539 (defun elpher-get-node-raw ()
540 "Getter which retrieves the raw server response for the current node."
541 (let* ((content (elpher-node-content elpher-current-node))
542 (address (elpher-node-address elpher-current-node)))
543 (elpher-with-clean-buffer
544 (insert "LOADING RAW SERVER RESPONSE..."))
546 (elpher-get-selector address
548 (unless (string-prefix-p "deleted" event)
549 (elpher-with-clean-buffer
550 (insert elpher-selector-string))
551 (goto-char (point-min)))))
553 (elpher-with-clean-buffer
554 (insert elpher-start-index))
555 (goto-char (point-min)))))
556 (message "Displaying raw server response. Reload or redraw to return to standard view."))
558 ;; File export retrieval
560 (defvar elpher-download-filename)
562 (defun elpher-get-node-download ()
563 "Getter which retrieves the current node and writes the result to a file."
564 (let* ((address (elpher-node-address elpher-current-node))
565 (selector (elpher-address-selector address)))
566 (elpher-visit-parent-node) ; Do first in case of non-local exits.
567 (let* ((filename-proposal (file-name-nondirectory selector))
568 (filename (read-file-name "Save file as: "
570 (if (> (length filename-proposal) 0)
573 (message "Downloading...")
574 (setq elpher-download-filename filename)
575 (elpher-get-selector address
577 (let ((coding-system-for-write 'binary))
578 (with-temp-file elpher-download-filename
579 (insert elpher-selector-string)
580 (message (format "Download complate, saved to file %s."
581 elpher-download-filename)))))))))
584 ;;; Navigation procedures
587 (defun elpher-next-link ()
588 "Move point to the next link on the current page."
592 (defun elpher-prev-link ()
593 "Move point to the previous link on the current page."
597 (defun elpher-click-link (button)
598 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
599 (let ((node (button-get button 'elpher-node)))
600 (elpher-visit-node node)))
602 (defun elpher-click-url (button)
603 "Function called when the url link BUTTON is activated (via mouse or keypress)."
604 (let ((url (button-get button 'elpher-url)))
605 (if elpher-open-urls-with-eww
609 (defun elpher-follow-current-link ()
610 "Open the link or url at point."
615 "Go to a particular gopher site."
618 (hostname (read-string "Gopher host: "))
619 (selector (read-string "Selector (default none): " nil nil ""))
620 (port (read-string "Port (default 70): " nil nil 70))
621 (address (list selector hostname port)))
623 (elpher-make-node elpher-current-node
625 #'elpher-get-index-node))))
627 (defun elpher-redraw ()
628 "Redraw current page."
630 (elpher-visit-node elpher-current-node))
632 (defun elpher-reload ()
633 "Reload current page."
635 (elpher-reload-current-node))
637 (defun elpher-view-raw ()
638 "View current page as plain text."
640 (elpher-visit-node elpher-current-node
641 #'elpher-get-node-raw))
643 (defun elpher-back ()
644 "Go to previous site."
646 (if (elpher-node-parent elpher-current-node)
647 (elpher-visit-parent-node)
648 (message "No previous site.")))
650 (defun elpher-download ()
651 "Download the link at point."
653 (let ((button (button-at (point))))
655 (let ((node (button-get button 'elpher-node)))
657 (elpher-visit-node (button-get button 'elpher-node)
658 #'elpher-get-node-download)
659 (message "Can only download gopher links, not general URLs.")))
660 (message "No link selected."))))
665 (defvar elpher-mode-map
666 (let ((map (make-sparse-keymap)))
667 (define-key map (kbd "TAB") 'elpher-next-link)
668 (define-key map (kbd "<backtab>") 'elpher-prev-link)
669 (define-key map (kbd "u") 'elpher-back)
670 (define-key map (kbd "g") 'elpher-go)
671 (define-key map (kbd "r") 'elpher-redraw)
672 (define-key map (kbd "R") 'elpher-reload)
673 (define-key map (kbd "w") 'elpher-view-raw)
674 (define-key map (kbd "d") 'elpher-download)
675 (when (fboundp 'evil-define-key)
676 (evil-define-key 'normal map
677 (kbd "TAB") 'elpher-next-link
678 (kbd "C-]") 'elpher-follow-current-link
679 (kbd "C-t") 'elpher-back
680 (kbd "u") 'elpher-back
682 (kbd "r") 'elpher-redraw
683 (kbd "R") 'elpher-reload
684 (kbd "w") 'elpher-view-raw
685 (kbd "d") 'elpher-download))
687 "Keymap for gopher client.")
689 (define-derived-mode elpher-mode special-mode "elpher"
690 "Major mode for elpher, an elisp gopher client.")
693 ;;; Main start procedure
698 "Start elpher with default landing page."
700 (setq elpher-current-node nil)
701 (let ((start-node (elpher-make-node nil
703 #'elpher-get-index-node)))
704 (elpher-visit-node start-node))
705 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
707 ;;; elpher.el ends here