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."
138 (defun elpher-make-address (selector host port)
139 "Create an address of a gopher object with SELECTOR, HOST and PORT."
140 (list selector host port))
142 (defun elpher-address-selector (address)
143 "Retrieve selector from ADDRESS."
146 (defun elpher-address-host (address)
147 "Retrieve host from ADDRESS."
150 (defun elpher-address-port (address)
151 "Retrieve port from ADDRESS."
156 (defun elpher-make-node (parent address getter &optional content pos)
157 "Create a node in the gopher page hierarchy.
159 PARENT specifies the parent of the node, ADDRESS specifies the address of
160 the gopher page, GETTER provides the getter function used to obtain this
163 The optional arguments CONTENT and POS can be used to fill the cached
164 content and cursor position fields of the node."
165 (list parent address getter content pos))
167 (defun elpher-node-parent (node)
168 "Retrieve the parent node of NODE."
171 (defun elpher-node-address (node)
172 "Retrieve the address of NODE."
175 (defun elpher-node-getter (node)
176 "Retrieve the preferred getter function of NODE."
179 (defun elpher-node-content (node)
180 "Retrieve the cached content of NODE, or nil if none exists."
183 (defun elpher-node-pos (node)
184 "Retrieve the cached cursor position for NODE, or nil if none exists."
187 (defun elpher-set-node-content (node content)
188 "Set the content cache of NODE to CONTENT."
189 (setcar (nthcdr 3 node) content))
191 (defun elpher-set-node-pos (node pos)
192 "Set the cursor position cache of NODE to POS."
193 (setcar (nthcdr 4 node) pos))
195 ;; Node graph traversal
197 (defvar elpher-current-node)
199 (defun elpher-visit-node (node &optional getter)
200 "Visit NODE using its own getter or GETTER, if non-nil."
202 (elpher-process-cleanup)
203 (setq elpher-current-node node)
206 (funcall (elpher-node-getter node))))
208 (defun elpher-visit-parent-node ()
209 "Visit the parent of the current node."
210 (let ((parent-node (elpher-node-parent elpher-current-node)))
212 (elpher-visit-node parent-node))))
214 (defun elpher-reload-current-node ()
215 "Reload the current node, discarding any existing cached content."
216 (elpher-set-node-content elpher-current-node nil)
217 (elpher-visit-node elpher-current-node))
219 (defun elpher-save-pos ()
220 "Save the current position of point to the current node."
221 (when elpher-current-node
222 (elpher-set-node-pos elpher-current-node (point))))
224 (defun elpher-restore-pos ()
225 "Restore the position of point to that cached in the current node."
226 (let ((pos (elpher-node-pos elpher-current-node)))
229 (goto-char (point-min)))))
231 ;;; Buffer preparation
234 (defmacro elpher-with-clean-buffer (&rest args)
235 "Evaluate ARGS with a clean *elpher* buffer as current."
237 '(switch-to-buffer "*elpher*")
239 (append (list 'let '((inhibit-read-only t))
246 (defun elpher-insert-index (string)
247 "Insert the index corresponding to STRING into the current buffer."
248 (dolist (line (split-string string "\r\n"))
249 (unless (= (length line) 0)
250 (elpher-insert-index-record line))))
252 (defun elpher-insert-margin (&optional type-name)
253 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
256 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
258 (propertize "[" 'face 'elpher-margin-brackets)
259 (propertize type-name 'face 'elpher-margin-key)
260 (propertize "]" 'face 'elpher-margin-brackets))))
262 (insert (make-string elpher-margin-width ?\s))))
264 (defvar elpher-type-map
265 '((?0 elpher-get-text-node "T" elpher-text)
266 (?1 elpher-get-index-node "/" elpher-index)
267 (?g elpher-get-image-node "im" elpher-image)
268 (?p elpher-get-image-node "im" elpher-image)
269 (?I elpher-get-image-node "im" elpher-image)
270 (?4 elpher-get-node-download "B" elpher-binary)
271 (?5 elpher-get-node-download "B" elpher-binary)
272 (?9 elpher-get-node-download "B" elpher-binary)
273 (?7 elpher-get-search-node "?" elpher-search))
274 "Association list from types to getters, margin codes and index faces.")
276 (defun elpher-insert-index-record (line)
277 "Insert the index record corresponding to LINE into the current buffer."
278 (let* ((type (elt line 0))
279 (fields (split-string (substring line 1) "\t"))
280 (display-string (elt fields 0))
281 (selector (elt fields 1))
282 (host (elt fields 2))
283 (port (elt fields 3))
284 (address (elpher-make-address selector host port))
285 (type-map-entry (alist-get type elpher-type-map)))
287 (let ((getter (car type-map-entry))
288 (margin-code (cadr type-map-entry))
289 (face (caddr type-map-entry)))
290 (elpher-insert-margin margin-code)
291 (insert-text-button display-string
293 'elpher-node (elpher-make-node elpher-current-node
296 'action #'elpher-click-link
298 'help-echo (format "mouse-1, RET: open %s on %s port %s"
299 selector host port)))
301 (?i (elpher-insert-margin) ; Information
302 (insert (propertize display-string
303 'face 'elpher-info)))
304 (?h (elpher-insert-margin "W") ; Web link
305 (let ((url (elt (split-string selector "URL:") 1)))
306 (insert-text-button display-string
309 'action #'elpher-click-url
311 'help-echo (format "mouse-1, RET: open url %s" url))))
312 (?.) ; Occurs at end of index, can safely ignore.
313 (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
314 (insert (propertize display-string
315 'face 'elpher-unknown-face)))))
319 ;;; Selector retrieval (all kinds)
322 (defun elpher-process-cleanup ()
323 "Immediately shut down any extant elpher process."
324 (let ((p (get-process "elpher-process")))
325 (if p (delete-process p))))
327 (defvar elpher-selector-string)
329 (defun elpher-get-selector (address after)
330 "Retrieve selector specified by ADDRESS, then execute AFTER.
331 The result is stored as a string in the variable elpher-selector-string."
332 (setq elpher-selector-string "")
333 (make-network-process
334 :name "elpher-process"
335 :host (elpher-address-host address)
336 :service (elpher-address-port address)
337 :filter (lambda (proc string)
338 (setq elpher-selector-string (concat elpher-selector-string string)))
340 (process-send-string "elpher-process"
341 (concat (elpher-address-selector address) "\n")))
345 (defun elpher-get-index-node ()
346 "Getter which retrieves the current node contents as an index."
347 (let ((content (elpher-node-content elpher-current-node))
348 (address (elpher-node-address elpher-current-node)))
351 (elpher-with-clean-buffer
353 (elpher-restore-pos))
356 (elpher-with-clean-buffer
357 (insert "LOADING DIRECTORY..."))
358 (elpher-get-selector address
360 (unless (string-prefix-p "deleted" event)
361 (elpher-with-clean-buffer
362 (elpher-insert-index elpher-selector-string))
364 (elpher-set-node-content elpher-current-node
367 (elpher-with-clean-buffer
368 (elpher-insert-index elpher-start-index))
370 (elpher-set-node-content elpher-current-node
371 (buffer-string)))))))
375 (defconst elpher-url-regex
376 "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
377 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
379 (defun elpher-buttonify-urls (string)
380 "Turn substrings which look like urls in STRING into clickable buttons."
383 (goto-char (point-min))
384 (while (re-search-forward elpher-url-regex nil t)
385 (let ((url (match-string 0))
386 (protocol (downcase (match-string 1))))
387 (if (string= protocol "gopher")
388 (let* ((host (match-string 2))
390 (type-and-selector (match-string 4))
391 (type (if (> (length type-and-selector) 1)
392 (elt type-and-selector 1)
394 (selector (if (> (length type-and-selector) 1)
395 (substring type-and-selector 2)
397 (address (elpher-make-address selector host port))
398 (getter (car (alist-get type elpher-type-map))))
399 (make-text-button (match-beginning 0)
401 'elpher-node (elpher-make-node elpher-current-node
404 'action #'elpher-click-link
406 'help-echo (format "mouse-1, RET: open %s on %s port %s"
407 selector host port)))
408 (make-text-button (match-beginning 0)
411 'action #'elpher-click-url
413 'help-echo (format "mouse-1, RET: open url %s" url)))))
416 (defun elpher-process-text (string)
417 "Remove CRs and trailing period from the gopher text document STRING."
418 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
419 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
420 (elpher-buttonify-urls cleaned-str)))
422 (defun elpher-get-text-node ()
423 "Getter which retrieves the current node contents as a text document."
424 (let ((content (elpher-node-content elpher-current-node))
425 (address (elpher-node-address elpher-current-node)))
428 (elpher-with-clean-buffer
430 (elpher-restore-pos))
432 (elpher-with-clean-buffer
433 (insert "LOADING TEXT..."))
434 (elpher-get-selector address
436 (unless (string-prefix-p "deleted" event)
437 (elpher-with-clean-buffer
438 (insert (elpher-process-text elpher-selector-string)))
440 (elpher-set-node-content elpher-current-node
441 (buffer-string)))))))))
445 (defun elpher-get-image-node ()
446 "Getter which retrieves the current node contents as an image to view."
447 (let ((content (elpher-node-content elpher-current-node))
448 (address (elpher-node-address elpher-current-node)))
451 (elpher-with-clean-buffer
452 (insert-image content))
453 (setq cursor-type nil)
454 (elpher-restore-pos))
456 (elpher-with-clean-buffer
457 (insert "LOADING IMAGE..."))
458 (elpher-get-selector address
460 (unless (string-prefix-p "deleted" event)
461 (let ((image (create-image
462 (encode-coding-string elpher-selector-string
465 (elpher-with-clean-buffer
466 (insert-image image))
467 (setq cursor-type nil)
469 (elpher-set-node-content elpher-current-node
474 (defun elpher-get-search-node ()
475 "Getter which submits a search query to the address of the current node."
476 (let ((content (elpher-node-content elpher-current-node))
477 (address (elpher-node-address elpher-current-node))
481 (elpher-with-clean-buffer
484 (message "Displaying cached search results. Reload to perform a new search."))
486 (let* ((query-string (read-string "Query: "))
487 (query-selector (concat (elpher-address-selector address) "\t" query-string))
488 (search-address (elpher-make-address query-selector
489 (elpher-address-host address)
490 (elpher-address-port address))))
492 (elpher-with-clean-buffer
493 (insert "LOADING RESULTS..."))
494 (elpher-get-selector search-address
496 (unless (string-prefix-p "deleted" event)
497 (elpher-with-clean-buffer
498 (elpher-insert-index elpher-selector-string))
499 (goto-char (point-min))
500 (elpher-set-node-content elpher-current-node
503 (elpher-visit-parent-node))))))
505 ;; Raw server response retrieval
507 (defun elpher-get-node-raw ()
508 "Getter which retrieves the raw server response for the current node."
509 (let* ((content (elpher-node-content elpher-current-node))
510 (address (elpher-node-address elpher-current-node)))
511 (elpher-with-clean-buffer
512 (insert "LOADING RAW SERVER RESPONSE..."))
514 (elpher-get-selector address
516 (unless (string-prefix-p "deleted" event)
517 (elpher-with-clean-buffer
518 (insert elpher-selector-string))
519 (goto-char (point-min)))))
521 (elpher-with-clean-buffer
522 (insert elpher-start-index))
523 (goto-char (point-min)))))
524 (message "Displaying raw server response. Reload or redraw to return to standard view."))
526 ;; File export retrieval
528 (defvar elpher-download-filename)
530 (defun elpher-get-node-download ()
531 "Getter which retrieves the current node and writes the result to a file."
532 (let* ((address (elpher-node-address elpher-current-node))
533 (selector (elpher-address-selector address)))
534 (elpher-visit-parent-node) ; Do first in case of non-local exits.
535 (let* ((filename-proposal (file-name-nondirectory selector))
536 (filename (read-file-name "Save file as: "
538 (if (> (length filename-proposal) 0)
541 (message "Downloading...")
542 (setq elpher-download-filename filename)
543 (elpher-get-selector address
545 (let ((coding-system-for-write 'binary))
546 (with-temp-file elpher-download-filename
547 (insert elpher-selector-string)
548 (message (format "Download complate, saved to file %s."
549 elpher-download-filename)))))))))
552 ;;; Navigation procedures
555 (defun elpher-next-link ()
556 "Move point to the next link on the current page."
560 (defun elpher-prev-link ()
561 "Move point to the previous link on the current page."
565 (defun elpher-click-link (button)
566 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
567 (let ((node (button-get button 'elpher-node)))
568 (elpher-visit-node node)))
570 (defun elpher-click-url (button)
571 "Function called when the url link BUTTON is activated (via mouse or keypress)."
572 (let ((url (button-get button 'elpher-url)))
573 (if elpher-open-urls-with-eww
577 (defun elpher-follow-current-link ()
578 "Open the link or url at point."
583 "Go to a particular gopher site."
586 (hostname (read-string "Gopher host: "))
587 (selector (read-string "Selector (default none): " nil nil ""))
588 (port (read-string "Port (default 70): " nil nil 70))
589 (address (list selector hostname port)))
591 (elpher-make-node elpher-current-node
593 #'elpher-get-index-node))))
595 (defun elpher-redraw ()
596 "Redraw current page."
598 (elpher-visit-node elpher-current-node))
600 (defun elpher-reload ()
601 "Reload current page."
603 (elpher-reload-current-node))
605 (defun elpher-view-raw ()
606 "View current page as plain text."
608 (elpher-visit-node elpher-current-node
609 #'elpher-get-node-raw))
611 (defun elpher-back ()
612 "Go to previous site."
614 (if (elpher-node-parent elpher-current-node)
615 (elpher-visit-parent-node)
616 (message "No previous site.")))
618 (defun elpher-download ()
619 "Download the link at point."
621 (let ((button (button-at (point))))
623 (let ((node (button-get button 'elpher-node)))
625 (elpher-visit-node (button-get button 'elpher-node)
626 #'elpher-get-node-download)
627 (message "Can only download gopher links, not general URLs.")))
628 (message "No link selected."))))
633 (defvar elpher-mode-map
634 (let ((map (make-sparse-keymap)))
635 (define-key map (kbd "TAB") 'elpher-next-link)
636 (define-key map (kbd "<backtab>") 'elpher-prev-link)
637 (define-key map (kbd "u") 'elpher-back)
638 (define-key map (kbd "g") 'elpher-go)
639 (define-key map (kbd "r") 'elpher-redraw)
640 (define-key map (kbd "R") 'elpher-reload)
641 (define-key map (kbd "w") 'elpher-view-raw)
642 (define-key map (kbd "d") 'elpher-download)
643 (when (fboundp 'evil-define-key)
644 (evil-define-key 'normal map
645 (kbd "TAB") 'elpher-next-link
646 (kbd "C-]") 'elpher-follow-current-link
647 (kbd "C-t") 'elpher-back
648 (kbd "u") 'elpher-back
650 (kbd "r") 'elpher-redraw
651 (kbd "R") 'elpher-reload
652 (kbd "w") 'elpher-view-raw
653 (kbd "d") 'elpher-download))
655 "Keymap for gopher client.")
657 (define-derived-mode elpher-mode special-mode "elpher"
658 "Major mode for elpher, an elisp gopher client.")
661 ;;; Main start procedure
666 "Start elpher with default landing page."
668 (setq elpher-current-node nil)
669 (let ((start-node (elpher-make-node nil nil #'elpher-get-index-node)))
670 (elpher-visit-node start-node))
671 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
673 ;;; elpher.el ends here