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.
53 (defconst elpher-version "1.0.0"
54 "Current version of elpher.")
56 (defconst elpher-margin-width 6
57 "Width of left-hand margin used when rendering indicies.")
59 (defconst elpher-start-index
62 (list "i\tfake\tfake\t1"
63 "i--------------------------------------------\tfake\tfake\t1"
64 "i Elpher Gopher Client \tfake\tfake\t1"
65 (format "i version %s\tfake\tfake\t1" elpher-version)
66 "i--------------------------------------------\tfake\tfake\t1"
68 "iBasic usage:\tfake\tfake\t1"
70 "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
71 "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
72 "i - u: return to parent directory entry\tfake\tfake\t1"
73 "i - g: go to a particular page\tfake\tfake\t1"
74 "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
75 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
76 "i - d: download directory entry under cursor\tfake\tfake\t1"
77 "i - w: display the raw server response for the current page\tfake\tfake\t1"
79 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
81 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
83 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
84 "isearch terms:\tfake\tfake\t1"
86 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
89 "Source for elpher start page.")
91 (defconst elpher-type-map
92 '((?0 elpher-get-text-node "T" elpher-text)
93 (?1 elpher-get-index-node "/" elpher-index)
94 (?g elpher-get-image-node "im" elpher-image)
95 (?p elpher-get-image-node "im" elpher-image)
96 (?I elpher-get-image-node "im" elpher-image)
97 (?4 elpher-get-node-download "B" elpher-binary)
98 (?5 elpher-get-node-download "B" elpher-binary)
99 (?9 elpher-get-node-download "B" elpher-binary)
100 (?7 elpher-get-search-node "?" elpher-search))
101 "Association list from types to getters, margin codes and index faces.")
104 ;;; Customization group
109 :group 'applications)
111 (defface elpher-index
112 '((((background dark)) :foreground "deep sky blue")
113 (((background light)) :foreground "blue"))
114 "Face used for index records.")
117 '((((background dark)) :foreground "white")
118 (((background light)) :weight bold))
119 "Face used for text records.")
121 (defface elpher-info '()
122 "Face used for info records.")
124 (defface elpher-image
125 '((((background dark)) :foreground "green")
126 (t :foreground "dark green"))
127 "Face used for image records.")
129 (defface elpher-search
130 '((((background light)) :foreground "orange")
131 (((background dark)) :foreground "dark orange"))
132 "Face used for search records.")
135 '((((background dark)) :foreground "yellow")
136 (((background light)) :foreground "dark red"))
137 "Face used for url records.")
139 (defface elpher-binary
140 '((t :foreground "magenta"))
141 "Face used for binary records.")
143 (defface elpher-unknown
144 '((t :foreground "red"))
145 "Face used for unknown record types.")
147 (defface elpher-margin-key
148 '((((background dark)) :foreground "white"))
149 "Face used for margin key.")
151 (defface elpher-margin-brackets
152 '((t :foreground "blue"))
153 "Face used for brackets around margin key.")
155 (defcustom elpher-open-urls-with-eww nil
156 "If non-nil, open URL selectors using eww.
157 Otherwise, use the system browser via the BROWSE-URL function."
160 (defcustom elpher-cache-images nil
161 "If non-nil, cache images in memory in the same way as other content."
169 (defun elpher-make-address (selector host port)
170 "Create an address of a gopher object with SELECTOR, HOST and PORT."
171 (list selector host port))
173 (defun elpher-address-selector (address)
174 "Retrieve selector from ADDRESS."
177 (defun elpher-address-host (address)
178 "Retrieve host from ADDRESS."
181 (defun elpher-address-port (address)
182 "Retrieve port from ADDRESS."
187 (defun elpher-make-node (parent address getter &optional content pos)
188 "Create a node in the gopher page hierarchy.
190 PARENT specifies the parent of the node, ADDRESS specifies the address of
191 the gopher page, GETTER provides the getter function used to obtain this
194 The optional arguments CONTENT and POS can be used to fill the cached
195 content and cursor position fields of the node."
196 (list parent address getter content pos))
198 (defun elpher-node-parent (node)
199 "Retrieve the parent node of NODE."
202 (defun elpher-node-address (node)
203 "Retrieve the address of NODE."
206 (defun elpher-node-getter (node)
207 "Retrieve the preferred getter function of NODE."
210 (defun elpher-node-content (node)
211 "Retrieve the cached content of NODE, or nil if none exists."
214 (defun elpher-node-pos (node)
215 "Retrieve the cached cursor position for NODE, or nil if none exists."
218 (defun elpher-set-node-content (node content)
219 "Set the content cache of NODE to CONTENT."
220 (setcar (nthcdr 3 node) content))
222 (defun elpher-set-node-pos (node pos)
223 "Set the cursor position cache of NODE to POS."
224 (setcar (nthcdr 4 node) pos))
226 ;; Node graph traversal
228 (defvar elpher-current-node)
230 (defun elpher-visit-node (node &optional getter)
231 "Visit NODE using its own getter or GETTER, if non-nil."
233 (elpher-process-cleanup)
234 (setq elpher-current-node node)
237 (funcall (elpher-node-getter node))))
239 (defun elpher-visit-parent-node ()
240 "Visit the parent of the current node."
241 (let ((parent-node (elpher-node-parent elpher-current-node)))
243 (elpher-visit-node parent-node))))
245 (defun elpher-reload-current-node ()
246 "Reload the current node, discarding any existing cached content."
247 (elpher-set-node-content elpher-current-node nil)
248 (elpher-visit-node elpher-current-node))
250 (defun elpher-save-pos ()
251 "Save the current position of point to the current node."
252 (when elpher-current-node
253 (elpher-set-node-pos elpher-current-node (point))))
255 (defun elpher-restore-pos ()
256 "Restore the position of point to that cached in the current node."
257 (let ((pos (elpher-node-pos elpher-current-node)))
260 (goto-char (point-min)))))
262 ;;; Buffer preparation
265 (defmacro elpher-with-clean-buffer (&rest args)
266 "Evaluate ARGS with a clean *elpher* buffer as current."
268 '(switch-to-buffer "*elpher*")
270 (append (list 'let '((inhibit-read-only t))
277 (defun elpher-insert-index (string)
278 "Insert the index corresponding to STRING into the current buffer."
279 (dolist (line (split-string string "\r\n"))
280 (unless (= (length line) 0)
281 (elpher-insert-index-record line))))
283 (defun elpher-insert-margin (&optional type-name)
284 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
287 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
289 (propertize "[" 'face 'elpher-margin-brackets)
290 (propertize type-name 'face 'elpher-margin-key)
291 (propertize "]" 'face 'elpher-margin-brackets))))
293 (insert (make-string elpher-margin-width ?\s))))
295 (defun elpher-insert-index-record (line)
296 "Insert the index record corresponding to LINE into the current buffer."
297 (let* ((type (elt line 0))
298 (fields (split-string (substring line 1) "\t"))
299 (display-string (elt fields 0))
300 (selector (elt fields 1))
301 (host (elt fields 2))
302 (port (elt fields 3))
303 (address (elpher-make-address selector host port))
304 (type-map-entry (alist-get type elpher-type-map)))
306 (let ((getter (car type-map-entry))
307 (margin-code (cadr type-map-entry))
308 (face (caddr type-map-entry)))
309 (elpher-insert-margin margin-code)
310 (insert-text-button display-string
312 'elpher-node (elpher-make-node elpher-current-node
315 'action #'elpher-click-link
317 'help-echo (format "mouse-1, RET: open %s on %s port %s"
318 selector host port)))
320 (?i (elpher-insert-margin) ; Information
321 (insert (propertize display-string
322 'face 'elpher-info)))
323 (?h (elpher-insert-margin "W") ; Web link
324 (let ((url (elt (split-string selector "URL:") 1)))
325 (insert-text-button display-string
328 'action #'elpher-click-url
330 'help-echo (format "mouse-1, RET: open url %s" url))))
331 (?.) ; Occurs at end of index, can safely ignore.
332 (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
333 (insert (propertize display-string
334 'face 'elpher-unknown-face)))))
338 ;;; Selector retrieval (all kinds)
341 (defun elpher-process-cleanup ()
342 "Immediately shut down any extant elpher process."
343 (let ((p (get-process "elpher-process")))
344 (if p (delete-process p))))
346 (defvar elpher-selector-string)
348 (defun elpher-get-selector (address after)
349 "Retrieve selector specified by ADDRESS, then execute AFTER.
350 The result is stored as a string in the variable ‘elpher-selector-string’."
351 (setq elpher-selector-string "")
352 (make-network-process
353 :name "elpher-process"
354 :host (elpher-address-host address)
355 :service (elpher-address-port address)
356 :filter (lambda (proc string)
357 (setq elpher-selector-string (concat elpher-selector-string string)))
359 (process-send-string "elpher-process"
360 (concat (elpher-address-selector address) "\n")))
364 (defun elpher-get-index-node ()
365 "Getter which retrieves the current node contents as an index."
366 (let ((content (elpher-node-content elpher-current-node))
367 (address (elpher-node-address elpher-current-node)))
370 (elpher-with-clean-buffer
372 (elpher-restore-pos))
375 (elpher-with-clean-buffer
376 (insert "LOADING DIRECTORY..."))
377 (elpher-get-selector address
379 (unless (string-prefix-p "deleted" event)
380 (elpher-with-clean-buffer
381 (elpher-insert-index elpher-selector-string))
383 (elpher-set-node-content elpher-current-node
386 (elpher-with-clean-buffer
387 (elpher-insert-index elpher-start-index))
389 (elpher-set-node-content elpher-current-node
390 (buffer-string)))))))
394 (defconst elpher-url-regex
395 "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
396 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
398 (defun elpher-buttonify-urls (string)
399 "Turn substrings which look like urls in STRING into clickable buttons."
402 (goto-char (point-min))
403 (while (re-search-forward elpher-url-regex nil t)
404 (let ((url (match-string 0))
405 (protocol (downcase (match-string 1))))
406 (if (string= protocol "gopher")
407 (let* ((host (match-string 2))
409 (type-and-selector (match-string 4))
410 (type (if (> (length type-and-selector) 1)
411 (elt type-and-selector 1)
413 (selector (if (> (length type-and-selector) 1)
414 (substring type-and-selector 2)
416 (address (elpher-make-address selector host port))
417 (getter (car (alist-get type elpher-type-map))))
418 (make-text-button (match-beginning 0)
420 'elpher-node (elpher-make-node elpher-current-node
423 'action #'elpher-click-link
425 'help-echo (format "mouse-1, RET: open %s on %s port %s"
426 selector host port)))
427 (make-text-button (match-beginning 0)
430 'action #'elpher-click-url
432 'help-echo (format "mouse-1, RET: open url %s" url)))))
435 (defun elpher-process-text (string)
436 "Remove CRs and trailing period from the gopher text document STRING."
437 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
438 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
439 (elpher-buttonify-urls cleaned-str)))
441 (defun elpher-get-text-node ()
442 "Getter which retrieves the current node contents as a text document."
443 (let ((content (elpher-node-content elpher-current-node))
444 (address (elpher-node-address elpher-current-node)))
447 (elpher-with-clean-buffer
449 (elpher-restore-pos))
451 (elpher-with-clean-buffer
452 (insert "LOADING TEXT..."))
453 (elpher-get-selector address
455 (unless (string-prefix-p "deleted" event)
456 (elpher-with-clean-buffer
457 (insert (elpher-process-text elpher-selector-string)))
459 (elpher-set-node-content elpher-current-node
460 (buffer-string)))))))))
464 (defun elpher-get-image-node ()
465 "Getter which retrieves the current node contents as an image to view."
466 (let ((content (elpher-node-content elpher-current-node))
467 (address (elpher-node-address elpher-current-node)))
470 (elpher-with-clean-buffer
471 (insert-image content))
472 (setq cursor-type nil)
473 (elpher-restore-pos))
474 (if (display-images-p)
476 (elpher-with-clean-buffer
477 (insert "LOADING IMAGE..."))
478 (elpher-get-selector address
480 (unless (string-prefix-p "deleted" event)
481 (let ((image (create-image
482 (encode-coding-string elpher-selector-string
485 (elpher-with-clean-buffer
486 (insert-image image))
487 (setq cursor-type nil)
489 (if elpher-cache-images
490 (elpher-set-node-content elpher-current-node
492 (elpher-get-node-download)))))
496 (defun elpher-get-search-node ()
497 "Getter which submits a search query to the address of the current node."
498 (let ((content (elpher-node-content elpher-current-node))
499 (address (elpher-node-address elpher-current-node))
503 (elpher-with-clean-buffer
506 (message "Displaying cached search results. Reload to perform a new search."))
508 (let* ((query-string (read-string "Query: "))
509 (query-selector (concat (elpher-address-selector address) "\t" query-string))
510 (search-address (elpher-make-address query-selector
511 (elpher-address-host address)
512 (elpher-address-port address))))
514 (elpher-with-clean-buffer
515 (insert "LOADING RESULTS..."))
516 (elpher-get-selector search-address
518 (unless (string-prefix-p "deleted" event)
519 (elpher-with-clean-buffer
520 (elpher-insert-index elpher-selector-string))
521 (goto-char (point-min))
522 (elpher-set-node-content elpher-current-node
525 (elpher-visit-parent-node))))))
527 ;; Raw server response retrieval
529 (defun elpher-get-node-raw ()
530 "Getter which retrieves the raw server response for the current node."
531 (let* ((content (elpher-node-content elpher-current-node))
532 (address (elpher-node-address elpher-current-node)))
533 (elpher-with-clean-buffer
534 (insert "LOADING RAW SERVER RESPONSE..."))
536 (elpher-get-selector address
538 (unless (string-prefix-p "deleted" event)
539 (elpher-with-clean-buffer
540 (insert elpher-selector-string))
541 (goto-char (point-min)))))
543 (elpher-with-clean-buffer
544 (insert elpher-start-index))
545 (goto-char (point-min)))))
546 (message "Displaying raw server response. Reload or redraw to return to standard view."))
548 ;; File export retrieval
550 (defvar elpher-download-filename)
552 (defun elpher-get-node-download ()
553 "Getter which retrieves the current node and writes the result to a file."
554 (let* ((address (elpher-node-address elpher-current-node))
555 (selector (elpher-address-selector address)))
556 (elpher-visit-parent-node) ; Do first in case of non-local exits.
557 (let* ((filename-proposal (file-name-nondirectory selector))
558 (filename (read-file-name "Save file as: "
560 (if (> (length filename-proposal) 0)
563 (message "Downloading...")
564 (setq elpher-download-filename filename)
565 (elpher-get-selector address
567 (let ((coding-system-for-write 'binary))
568 (with-temp-file elpher-download-filename
569 (insert elpher-selector-string)
570 (message (format "Download complate, saved to file %s."
571 elpher-download-filename)))))))))
574 ;;; Navigation procedures
577 (defun elpher-next-link ()
578 "Move point to the next link on the current page."
582 (defun elpher-prev-link ()
583 "Move point to the previous link on the current page."
587 (defun elpher-click-link (button)
588 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
589 (let ((node (button-get button 'elpher-node)))
590 (elpher-visit-node node)))
592 (defun elpher-click-url (button)
593 "Function called when the url link BUTTON is activated (via mouse or keypress)."
594 (let ((url (button-get button 'elpher-url)))
595 (if elpher-open-urls-with-eww
599 (defun elpher-follow-current-link ()
600 "Open the link or url at point."
605 "Go to a particular gopher site."
608 (hostname (read-string "Gopher host: "))
609 (selector (read-string "Selector (default none): " nil nil ""))
610 (port (read-string "Port (default 70): " nil nil 70))
611 (address (list selector hostname port)))
613 (elpher-make-node elpher-current-node
615 #'elpher-get-index-node))))
617 (defun elpher-redraw ()
618 "Redraw current page."
620 (elpher-visit-node elpher-current-node))
622 (defun elpher-reload ()
623 "Reload current page."
625 (elpher-reload-current-node))
627 (defun elpher-view-raw ()
628 "View current page as plain text."
630 (elpher-visit-node elpher-current-node
631 #'elpher-get-node-raw))
633 (defun elpher-back ()
634 "Go to previous site."
636 (if (elpher-node-parent elpher-current-node)
637 (elpher-visit-parent-node)
638 (message "No previous site.")))
640 (defun elpher-download ()
641 "Download the link at point."
643 (let ((button (button-at (point))))
645 (let ((node (button-get button 'elpher-node)))
647 (elpher-visit-node (button-get button 'elpher-node)
648 #'elpher-get-node-download)
649 (message "Can only download gopher links, not general URLs.")))
650 (message "No link selected."))))
655 (defvar elpher-mode-map
656 (let ((map (make-sparse-keymap)))
657 (define-key map (kbd "TAB") 'elpher-next-link)
658 (define-key map (kbd "<backtab>") 'elpher-prev-link)
659 (define-key map (kbd "u") 'elpher-back)
660 (define-key map (kbd "g") 'elpher-go)
661 (define-key map (kbd "r") 'elpher-redraw)
662 (define-key map (kbd "R") 'elpher-reload)
663 (define-key map (kbd "w") 'elpher-view-raw)
664 (define-key map (kbd "d") 'elpher-download)
665 (when (fboundp 'evil-define-key)
666 (evil-define-key 'normal map
667 (kbd "TAB") 'elpher-next-link
668 (kbd "C-]") 'elpher-follow-current-link
669 (kbd "C-t") 'elpher-back
670 (kbd "u") 'elpher-back
672 (kbd "r") 'elpher-redraw
673 (kbd "R") 'elpher-reload
674 (kbd "w") 'elpher-view-raw
675 (kbd "d") 'elpher-download))
677 "Keymap for gopher client.")
679 (define-derived-mode elpher-mode special-mode "elpher"
680 "Major mode for elpher, an elisp gopher client.")
683 ;;; Main start procedure
688 "Start elpher with default landing page."
690 (setq elpher-current-node nil)
691 (let ((start-node (elpher-make-node nil nil #'elpher-get-index-node)))
692 (elpher-visit-node start-node))
693 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
695 ;;; elpher.el ends here