1 ;;; elpher.el --- A friendly 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 practical gopher client for GNU Emacs.
32 ;; - intuitive keyboard and mouse-driven interface,
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,
38 ;; - a simple bookmark management system,
39 ;; - support for TLS gopherholes.
41 ;; To launch Elpher, simply use 'M-x elpher'. This will open a start
42 ;; page containing information on key bindings and suggested starting
43 ;; points for your gopher exploration.
45 ;; Faces, caching and other options can be configured via
46 ;; the Elpher customization group in Applications.
58 (defconst elpher-version "1.4.3"
59 "Current version of elpher.")
61 (defconst elpher-margin-width 6
62 "Width of left-hand margin used when rendering indicies.")
64 (defconst elpher-start-index
67 (list "i\tfake\tfake\t1"
68 "i --------------------------------------------\tfake\tfake\t1"
69 "i Elpher Gopher Client \tfake\tfake\t1"
70 (format "i version %s\tfake\tfake\t1" elpher-version)
71 "i --------------------------------------------\tfake\tfake\t1"
73 "iUsage:\tfake\tfake\t1"
75 "i - tab/shift-tab: next/prev item on current page\tfake\tfake\t1"
76 "i - RET/mouse-1: open item under cursor\tfake\tfake\t1"
77 "i - m: select an item on current page by name (autocompletes)\tfake\tfake\t1"
78 "i - u: return to parent\tfake\tfake\t1"
79 "i - O: visit the root menu of the current server\tfake\tfake\t1"
80 "i - g: go to a particular gopher address\tfake\tfake\t1"
81 "i - i/I: info on item under cursor or current page\tfake\tfake\t1"
82 "i - c/C: copy URL representation of item under cursor or current page\tfake\tfake\t1"
83 "i - a/A: bookmark the item under cursor or current page\tfake\tfake\t1"
84 "i - x/X: remove bookmark for item under cursor or current page\tfake\tfake\t1"
85 "i - B: visit the bookmarks page\tfake\tfake\t1"
86 "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
87 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
88 "i - T: toggle TLS mode\tfake\tfake\t1"
89 "i - d/D: download item under cursor or current page\tfake\tfake\t1"
90 "i - w: display the raw server response for the current page\tfake\tfake\t1"
91 "i - S: set an explicit character coding system (default is to autodetect)\tfake\tfake\t1"
93 "iWhere to start exploring Gopherspace:\tfake\tfake\t1"
95 "1Floodgap Systems Gopher Server\t/\tgopher.floodgap.com\t70"
97 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
98 "isearch terms:\tfake\tfake\t1"
100 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
103 "Source for elpher start page.")
105 (defconst elpher-type-map
106 '((?0 elpher-get-text-node "txt" elpher-text)
107 (?1 elpher-get-index-node "/" elpher-index)
108 (?4 elpher-get-node-download "bin" elpher-binary)
109 (?5 elpher-get-node-download "bin" elpher-binary)
110 (?7 elpher-get-search-node "?" elpher-search)
111 (?8 elpher-get-telnet-node "tel" elpher-telnet)
112 (?9 elpher-get-node-download "bin" elpher-binary)
113 (?g elpher-get-image-node "img" elpher-image)
114 (?p elpher-get-image-node "img" elpher-image)
115 (?I elpher-get-image-node "img" elpher-image)
116 (?d elpher-get-node-download "doc" elpher-binary)
117 (?h elpher-get-url-node "web" elpher-url)
118 (bookmarks elpher-get-bookmarks-node "#" elpher-index)
119 (start elpher-get-start-node "#" elpher-index))
120 "Association list from types to getters, margin codes and index faces.")
123 ;;; Customization group
128 :group 'applications)
130 ;; Face customizations
132 (defface elpher-index
133 '((t :inherit font-lock-keyword-face))
134 "Face used for directory type directory records.")
138 "Face used for text type directory records.")
141 '((t :inherit default))
142 "Face used for info type directory records.")
144 (defface elpher-image
145 '((t :inherit font-lock-string-face))
146 "Face used for image type directory records.")
148 (defface elpher-search
149 '((t :inherit warning))
150 "Face used for search type directory records.")
153 '((t :inherit font-lock-comment-face))
154 "Face used for url type directory records.")
156 (defface elpher-telnet
157 '((t :inherit font-lock-function-name-face))
158 "Face used for telnet type directory records.")
160 (defface elpher-binary
161 '((t :inherit font-lock-doc-face))
162 "Face used for binary type directory records.")
164 (defface elpher-unknown
165 '((t :inherit error))
166 "Face used for directory records with unknown/unsupported types.")
168 (defface elpher-margin-key
170 "Face used for directory margin key.")
172 (defface elpher-margin-brackets
173 '((t :inherit shadow))
174 "Face used for brackets around directory margin key.")
176 ;; Other customizations
178 (defcustom elpher-open-urls-with-eww nil
179 "If non-nil, open URL selectors using eww.
180 Otherwise, use the system browser via the BROWSE-URL function."
183 (defcustom elpher-buttonify-urls-in-directories nil
184 "If non-nil, turns URLs matched in directories into clickable buttons."
187 (defcustom elpher-cache-images nil
188 "If non-nil, cache images in memory in the same way as other content."
191 (defcustom elpher-use-header t
192 "If non-nil, display current node information in buffer header."
200 (defun elpher-make-address (type &optional selector host port use-tls)
201 "Create an address of a gopher object with TYPE, SELECTOR, HOST and PORT.
202 Although selector host and port are optional, they are only omitted for
203 special address types, such as 'start for the start page.
205 Setting the USE-TLS parameter to non-nil causes Elpher to engage TLS mode
206 before attempting to connect to the server."
208 (list type selector host port 'tls)
209 (list type selector host port)))
211 (defun elpher-address-type (address)
212 "Retrieve type from ADDRESS."
215 (defun elpher-address-selector (address)
216 "Retrieve selector from ADDRESS."
219 (defun elpher-address-host (address)
220 "Retrieve host from ADDRESS."
223 (defun elpher-address-port (address)
224 "Retrieve port from ADDRESS."
227 (defun elpher-address-use-tls-p (address)
228 "Return non-nil if ADDRESS is marked as needing TLS."
231 (defun elpher-address-special-p (address)
232 "Return non-nil if ADDRESS is special (e.g. start page, bookmarks page)."
233 (not (elpher-address-host address)))
237 (defun elpher-make-node (display-string address &optional parent)
238 "Create a node in the gopher page hierarchy.
240 DISPLAY-STRING records the display string used for the page.
242 ADDRESS specifies the address of the gopher page.
244 The optional PARENT specifies the parent node in the hierarchy.
245 This is set every time the node is visited, so while it forms
246 an important part of the node data there is no need to set it
248 (list display-string address parent))
250 (defun elpher-node-display-string (node)
251 "Retrieve the display string of NODE."
254 (defun elpher-node-address (node)
255 "Retrieve the address of NODE."
258 (defun elpher-node-parent (node)
259 "Retrieve the parent node of NODE."
262 (defun elpher-set-node-parent (node parent)
263 "Set the parent node of NODE to be PARENT."
264 (setcar (cdr (cdr node)) parent))
268 (defvar elpher-content-cache (make-hash-table :test 'equal))
269 (defvar elpher-pos-cache (make-hash-table :test 'equal))
271 (defun elpher-get-cached-content (address)
272 "Retrieve the cached content for ADDRESS, or nil if none exists."
273 (gethash address elpher-content-cache))
275 (defun elpher-cache-content (address content)
276 "Set the content cache for ADDRESS to CONTENT."
277 (puthash address content elpher-content-cache))
279 (defun elpher-get-cached-pos (address)
280 "Retrieve the cached cursor position for ADDRESS, or nil if none exists."
281 (gethash address elpher-pos-cache))
283 (defun elpher-cache-pos (address pos)
284 "Set the cursor position cache for ADDRESS to POS."
285 (puthash address pos elpher-pos-cache))
287 ;; Node graph traversal
289 (defvar elpher-current-node nil)
291 (defun elpher-visit-node (node &optional getter preserve-parent)
292 "Visit NODE using its own getter or GETTER, if non-nil.
293 Additionally, set the parent of NODE to `elpher-current-node',
294 unless PRESERVE-PARENT is non-nil."
296 (elpher-process-cleanup)
297 (unless preserve-parent
298 (if (and (elpher-node-parent elpher-current-node)
299 (equal (elpher-node-address elpher-current-node)
300 (elpher-node-address node)))
301 (elpher-set-node-parent node (elpher-node-parent elpher-current-node))
302 (elpher-set-node-parent node elpher-current-node)))
303 (setq elpher-current-node node)
306 (let* ((address (elpher-node-address node))
307 (type (elpher-address-type address)))
308 (funcall (car (alist-get type elpher-type-map))))))
310 (defun elpher-visit-parent-node ()
311 "Visit the parent of the current node."
312 (let ((parent-node (elpher-node-parent elpher-current-node)))
314 (elpher-visit-node parent-node nil t))))
316 (defun elpher-reload-current-node ()
317 "Reload the current node, discarding any existing cached content."
318 (elpher-cache-content (elpher-node-address elpher-current-node) nil)
319 (elpher-visit-node elpher-current-node))
321 (defun elpher-save-pos ()
322 "Save the current position of point to the current node."
323 (when elpher-current-node
324 (elpher-cache-pos (elpher-node-address elpher-current-node) (point))))
326 (defun elpher-restore-pos ()
327 "Restore the position of point to that cached in the current node."
328 (let ((pos (elpher-get-cached-pos (elpher-node-address elpher-current-node))))
331 (goto-char (point-min)))))
334 ;;; Buffer preparation
337 (defun elpher-update-header ()
338 "If `elpher-use-header' is true, display current node info in window header."
339 (if elpher-use-header
340 (setq header-line-format (elpher-node-display-string elpher-current-node))))
342 (defmacro elpher-with-clean-buffer (&rest args)
343 "Evaluate ARGS with a clean *elpher* buffer as current."
344 (list 'with-current-buffer "*elpher*"
346 (append (list 'let '((inhibit-read-only t))
348 '(elpher-update-header))
355 (defvar elpher-user-coding-system nil
356 "User-specified coding system to use for decoding text responses.")
358 (defun elpher-decode (string)
359 "Decode STRING using autodetected or user-specified coding system."
360 (decode-coding-string string
361 (if elpher-user-coding-system
362 elpher-user-coding-system
363 (detect-coding-string string t))))
365 (defun elpher-preprocess-text-response (string)
366 "Preprocess text selector response contained in STRING.
367 This involes decoding the character representation, and clearing
368 away CRs and any terminating period."
369 (elpher-decode (replace-regexp-in-string "\n\.\n$" "\n"
370 (replace-regexp-in-string "\r" "" string))))
375 (defun elpher-insert-index (string)
376 "Insert the index corresponding to STRING into the current buffer."
377 ;; Should be able to split directly on CRLF, but some non-conformant
378 ;; LF-only servers sadly exist, hence the following.
379 (let ((str-processed (elpher-preprocess-text-response string)))
380 (dolist (line (split-string str-processed "\n"))
381 (unless (= (length line) 0)
382 (let* ((type (elt line 0))
383 (fields (split-string (substring line 1) "\t"))
384 (display-string (elt fields 0))
385 (selector (elt fields 1))
386 (host (elt fields 2))
387 (port (if (elt fields 3)
388 (string-to-number (elt fields 3))
390 (address (elpher-make-address type selector host port)))
391 (elpher-insert-index-record display-string address))))))
393 (defun elpher-insert-margin (&optional type-name)
394 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
397 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
399 (propertize "[" 'face 'elpher-margin-brackets)
400 (propertize type-name 'face 'elpher-margin-key)
401 (propertize "]" 'face 'elpher-margin-brackets))))
403 (insert (make-string elpher-margin-width ?\s))))
405 (defun elpher-node-button-help (node)
406 "Return a string containing the help text for a button corresponding to NODE."
407 (let ((address (elpher-node-address node)))
408 (if (eq (elpher-address-type address) ?h)
409 (let ((url (cadr (split-string (elpher-address-selector address) "URL:"))))
410 (format "mouse-1, RET: open url '%s'" url))
411 (format "mouse-1, RET: open '%s' on %s port %s"
412 (elpher-address-selector address)
413 (elpher-address-host address)
414 (elpher-address-port address)))))
417 (defun elpher-insert-index-record (display-string address)
418 "Function to insert an index record into the current buffer.
419 The contents of the record are dictated by DISPLAY-STRING and ADDRESS."
420 (let* ((type (elpher-address-type address))
421 (type-map-entry (alist-get type elpher-type-map)))
423 (let* ((margin-code (elt type-map-entry 1))
424 (face (elt type-map-entry 2))
425 (node (elpher-make-node display-string address)))
426 (elpher-insert-margin margin-code)
427 (insert-text-button display-string
430 'action #'elpher-click-link
432 'help-echo (elpher-node-button-help node)))
435 (elpher-insert-margin)
437 (if elpher-buttonify-urls-in-directories
438 (elpher-buttonify-urls display-string)
440 'face 'elpher-info)))
442 (elpher-insert-margin (concat (char-to-string type) "?"))
443 (insert (propertize display-string
444 'face 'elpher-unknown)))))
447 (defun elpher-click-link (button)
448 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
449 (let ((node (button-get button 'elpher-node)))
450 (elpher-visit-node node)))
453 ;;; Selector retrieval (all kinds)
456 (defun elpher-process-cleanup ()
457 "Immediately shut down any extant elpher process."
458 (let ((p (get-process "elpher-process")))
459 (if p (delete-process p))))
461 (defvar elpher-use-tls nil
462 "If non-nil, use TLS to communicate with gopher servers.")
464 (defvar elpher-selector-string)
466 (defun elpher-get-selector (address after &optional propagate-error)
467 "Retrieve selector specified by ADDRESS, then execute AFTER.
468 The result is stored as a string in the variable ‘elpher-selector-string’.
470 Usually errors result in an error page being displayed. This is only
471 appropriate if the selector is to be directly viewed. If PROPAGATE-ERROR
472 is non-nil, this message is not displayed. Instead, the error propagates
473 up to the calling function."
474 (setq elpher-selector-string "")
475 (when (elpher-address-use-tls-p address)
476 (if (gnutls-available-p)
477 (when (not elpher-use-tls)
478 (setq elpher-use-tls t)
479 (message "Engaging TLS mode."))
480 (error "Cannot retrieve TLS selector: GnuTLS not available")))
481 (condition-case the-error
482 (let* ((kill-buffer-query-functions nil)
483 (proc (open-network-stream "elpher-process"
485 (elpher-address-host address)
486 (elpher-address-port address)
487 :type (if elpher-use-tls 'tls 'plain))))
488 (set-process-coding-system proc 'binary)
489 (set-process-filter proc
490 (lambda (proc string)
491 (setq elpher-selector-string
492 (concat elpher-selector-string string))))
493 (set-process-sentinel proc after)
494 (process-send-string proc
495 (concat (elpher-address-selector address) "\n")))
497 (if (and (consp the-error)
498 (eq (car the-error) 'gnutls-error)
499 (not (elpher-address-use-tls-p address)))
501 (message "Could not establish TLS connection. Disengaging TLS mode.")
502 (setq elpher-use-tls nil)
503 (elpher-get-selector address after))
504 (elpher-process-cleanup)
507 (elpher-with-clean-buffer
508 (insert (propertize "\n---- ERROR -----\n\n" 'face 'error)
509 "Failed to connect to " (elpher-get-address-url address) ".\n"
510 (propertize "\n----------------\n\n" 'face 'error)
511 "Press 'u' to return to the previous page.")))))))
515 (defun elpher-get-index-node ()
516 "Getter which retrieves the current node contents as an index."
517 (let* ((address (elpher-node-address elpher-current-node))
518 (content (elpher-get-cached-content address)))
521 (elpher-with-clean-buffer
523 (elpher-restore-pos)))
524 (elpher-with-clean-buffer
525 (insert "LOADING DIRECTORY... (use 'u' to cancel)"))
526 (elpher-get-selector address
528 (unless (string-prefix-p "deleted" event)
529 (elpher-with-clean-buffer
530 (elpher-insert-index elpher-selector-string)
532 (elpher-cache-content
533 (elpher-node-address elpher-current-node)
534 (buffer-string)))))))))
538 (defconst elpher-url-regex
539 "\\([a-zA-Z]+\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
540 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
542 (defun elpher-make-node-from-matched-url (&optional string)
543 "Convert most recent `elpher-url-regex' match to a node.
545 If STRING is non-nil, this is given as an argument to all `match-string'
546 calls, as is necessary if the match is performed by `string-match'."
547 (let ((url (match-string 0 string))
548 (protocol (downcase (match-string 1 string))))
549 (if (or (string= protocol "gopher")
550 (string= protocol "gophers"))
551 (let* ((host (match-string 2 string))
552 (port (if (> (length (match-string 3 string)) 1)
553 (string-to-number (substring (match-string 3 string) 1))
555 (type-and-selector (match-string 4 string))
556 (type (if (> (length type-and-selector) 1)
557 (elt type-and-selector 1)
559 (selector (if (> (length type-and-selector) 1)
560 (substring type-and-selector 2)
562 (use-tls (string= protocol "gophers"))
563 (address (elpher-make-address type selector host port use-tls)))
564 (elpher-make-node url address))
565 (let* ((host (match-string 2 string))
566 (port (if (> (length (match-string 3 string)) 1)
567 (string-to-number (substring (match-string 3 string) 1))
569 (selector (concat "URL:" url))
570 (address (elpher-make-address ?h selector host port)))
571 (elpher-make-node url address)))))
574 (defun elpher-buttonify-urls (string)
575 "Turn substrings which look like urls in STRING into clickable buttons."
578 (goto-char (point-min))
579 (while (re-search-forward elpher-url-regex nil t)
580 (let ((node (elpher-make-node-from-matched-url)))
581 (make-text-button (match-beginning 0)
584 'action #'elpher-click-link
586 'help-echo (elpher-node-button-help node))))
589 (defun elpher-get-text-node ()
590 "Getter which retrieves the current node contents as a text document."
591 (let* ((address (elpher-node-address elpher-current-node))
592 (content (elpher-get-cached-content address)))
595 (elpher-with-clean-buffer
597 (elpher-restore-pos)))
599 (elpher-with-clean-buffer
600 (insert "LOADING TEXT... (use 'u' to cancel)"))
601 (elpher-get-selector address
603 (unless (string-prefix-p "deleted" event)
604 (elpher-with-clean-buffer
605 (insert (elpher-buttonify-urls
606 (elpher-preprocess-text-response
607 elpher-selector-string)))
609 (elpher-cache-content
610 (elpher-node-address elpher-current-node)
611 (buffer-string))))))))))
615 (defun elpher-get-image-node ()
616 "Getter which retrieves the current node contents as an image to view."
617 (let* ((address (elpher-node-address elpher-current-node))
618 (content (elpher-get-cached-content address)))
621 (elpher-with-clean-buffer
622 (insert-image content)
623 (elpher-restore-pos)))
624 (if (display-images-p)
626 (elpher-with-clean-buffer
627 (insert "LOADING IMAGE... (use 'u' to cancel)"))
628 (elpher-get-selector address
630 (unless (string-prefix-p "deleted" event)
631 (let ((image (create-image
632 elpher-selector-string
634 (elpher-with-clean-buffer
636 (elpher-restore-pos))
637 (if elpher-cache-images
638 (elpher-cache-content
639 (elpher-node-address elpher-current-node)
641 (elpher-get-node-download)))))
645 (defun elpher-get-search-node ()
646 "Getter which submits a search query to the address of the current node."
647 (let* ((address (elpher-node-address elpher-current-node))
648 (content (elpher-get-cached-content address))
652 (elpher-with-clean-buffer
654 (elpher-restore-pos))
655 (message "Displaying cached search results. Reload to perform a new search."))
657 (let* ((query-string (read-string "Query: "))
658 (query-selector (concat (elpher-address-selector address) "\t" query-string))
659 (search-address (elpher-make-address ?1
661 (elpher-address-host address)
662 (elpher-address-port address))))
664 (elpher-with-clean-buffer
665 (insert "LOADING RESULTS... (use 'u' to cancel)"))
666 (elpher-get-selector search-address
668 (unless (string-prefix-p "deleted" event)
669 (elpher-with-clean-buffer
670 (elpher-insert-index elpher-selector-string))
671 (goto-char (point-min))
672 (elpher-cache-content
673 (elpher-node-address elpher-current-node)
676 (elpher-visit-parent-node))))))
678 ;; Raw server response retrieval
680 (defun elpher-get-node-raw ()
681 "Getter which retrieves the raw server response for the current node."
682 (let ((address (elpher-node-address elpher-current-node)))
683 (elpher-with-clean-buffer
684 (insert "LOADING RAW SERVER RESPONSE... (use 'u' to cancel)"))
686 (elpher-get-selector address
688 (unless (string-prefix-p "deleted" event)
689 (elpher-with-clean-buffer
690 (insert elpher-selector-string)
691 (goto-char (point-min))))))
693 (elpher-with-clean-buffer
694 (insert elpher-start-index))
695 (goto-char (point-min)))))
696 (message "Displaying raw server response. Reload or redraw to return to standard view."))
698 ;; File export retrieval
700 (defvar elpher-download-filename)
702 (defun elpher-get-node-download ()
703 "Getter which retrieves the current node and writes the result to a file."
704 (let* ((address (elpher-node-address elpher-current-node))
705 (selector (elpher-address-selector address)))
706 (elpher-visit-parent-node) ; Do first in case of non-local exits.
707 (let* ((filename-proposal (file-name-nondirectory selector))
708 (filename (read-file-name "Save file as: "
710 (if (> (length filename-proposal) 0)
713 (message "Downloading...")
714 (setq elpher-download-filename filename)
715 (condition-case the-error
716 (elpher-get-selector address
718 (let ((coding-system-for-write 'binary))
719 (with-temp-file elpher-download-filename
720 (insert elpher-selector-string)
721 (message (format "Download complate, saved to file %s."
722 elpher-download-filename)))))
725 (error "Error downloading %s" elpher-download-filename))))))
729 (defun elpher-insert-rendered-html (string)
730 "Use shr to insert rendered view of html STRING into current buffer."
731 (let ((dom (with-temp-buffer
733 (libxml-parse-html-region (point-min) (point-max)))))
734 (shr-insert-document dom)))
736 (defun elpher-get-url-node ()
737 "Getter which attempts to open the URL specified by the current node."
738 (let* ((address (elpher-node-address elpher-current-node))
739 (selector (elpher-address-selector address)))
740 (let ((url (elt (split-string selector "URL:") 1)))
743 (elpher-visit-parent-node) ; Do first in case of non-local exits.
744 (message "Opening URL...")
745 (if elpher-open-urls-with-eww
748 (let ((content (elpher-get-cached-content address)))
751 (elpher-with-clean-buffer
753 (elpher-restore-pos)))
754 (elpher-with-clean-buffer
755 (insert "LOADING HTML... (use 'u' to cancel)"))
756 (elpher-get-selector address
758 (unless (string-prefix-p "deleted" event)
759 (elpher-with-clean-buffer
760 (elpher-insert-rendered-html elpher-selector-string)
761 (goto-char (point-min))
762 (elpher-cache-content
763 (elpher-node-address elpher-current-node)
764 (buffer-string))))))))))))
766 ;; Telnet node connection
768 (defun elpher-get-telnet-node ()
769 "Getter which opens a telnet connection to the server specified by the current node."
770 (let* ((address (elpher-node-address elpher-current-node))
771 (host (elpher-address-host address))
772 (port (elpher-address-port address)))
773 (elpher-visit-parent-node)
776 ;; Start page node retrieval
778 (defun elpher-get-start-node ()
779 "Getter which displays the start page."
780 (elpher-with-clean-buffer
781 (elpher-insert-index elpher-start-index)
782 (elpher-restore-pos)))
784 ;; Bookmarks page node retrieval
786 (defun elpher-get-bookmarks-node ()
787 "Getter to load and display the current bookmark list."
788 (elpher-with-clean-buffer
789 (insert "---- Bookmark list ----\n\n")
790 (let ((bookmarks (elpher-load-bookmarks)))
792 (dolist (bookmark bookmarks)
793 (let ((display-string (elpher-bookmark-display-string bookmark))
794 (address (elpher-bookmark-address bookmark)))
795 (elpher-insert-index-record display-string address)))
796 (insert "No bookmarks found.\n")))
797 (insert "\n-----------------------\n\n"
798 "- u: return to previous page\n"
799 "- x: delete selected bookmark\n"
800 "- a: rename selected bookmark\n\n"
801 "Bookmarks are stored in the file "
802 (locate-user-emacs-file "elpher-bookmarks"))
803 (elpher-restore-pos)))
809 (defun elpher-make-bookmark (display-string address)
810 "Make an elpher bookmark.
811 DISPLAY-STRING determines how the bookmark will appear in the
812 bookmark list, while ADDRESS is the address of the entry."
813 (list display-string address))
815 (defun elpher-bookmark-display-string (bookmark)
816 "Get the display string of BOOKMARK."
819 (defun elpher-set-bookmark-display-string (bookmark display-string)
820 "Set the display string of BOOKMARK to DISPLAY-STRING."
821 (setcar bookmark display-string))
823 (defun elpher-bookmark-address (bookmark)
824 "Get the address for BOOKMARK."
827 (defun elpher-save-bookmarks (bookmarks)
828 "Record the bookmark list BOOKMARKS to the user's bookmark file.
829 Beware that this completely replaces the existing contents of the file."
830 (with-temp-file (locate-user-emacs-file "elpher-bookmarks")
832 (insert "; Elpher gopher bookmarks file\n\n"
833 "; Bookmarks are stored as a list of (label (type selector host port))\n"
834 "; s-expressions, where type is stored as a character (i.e. 49 = ?1).\n"
835 "; Feel free to edit by hand, but ensure this structure remains intact.\n\n")
836 (pp bookmarks (current-buffer))))
838 (defun elpher-load-bookmarks ()
839 "Get the list of bookmarks from the users's bookmark file."
842 (insert-file-contents (locate-user-emacs-file "elpher-bookmarks"))
843 (goto-char (point-min))
844 (read (current-buffer)))))
846 (defun elpher-add-address-bookmark (address display-string)
847 "Save a bookmark for ADDRESS with label DISPLAY-STRING.
848 If ADDRESS is already bookmarked, update the label only."
849 (let ((bookmarks (elpher-load-bookmarks)))
850 (let ((existing-bookmark (rassoc (list address) bookmarks)))
851 (if existing-bookmark
852 (elpher-set-bookmark-display-string existing-bookmark display-string)
853 (add-to-list 'bookmarks (elpher-make-bookmark display-string address))))
854 (elpher-save-bookmarks bookmarks)))
856 (defun elpher-remove-address-bookmark (address)
857 "Remove any bookmark to ADDRESS."
858 (elpher-save-bookmarks
859 (seq-filter (lambda (bookmark)
860 (not (equal (elpher-bookmark-address bookmark) address)))
861 (elpher-load-bookmarks))))
863 ;;; Interactive procedures
866 (defun elpher-next-link ()
867 "Move point to the next link on the current page."
871 (defun elpher-prev-link ()
872 "Move point to the previous link on the current page."
876 (defun elpher-follow-current-link ()
877 "Open the link or url at point."
882 "Go to a particular gopher site read from the minibuffer.
883 The site may be specified via a URL or explicitly in terms of
884 host, selector and port."
887 (let ((host-or-url (read-string "Gopher host or URL: ")))
888 (if (string-match elpher-url-regex host-or-url)
889 (elpher-make-node-from-matched-url host-or-url)
890 (let ((selector (read-string "Selector (default none): " nil nil ""))
891 (port-string (read-string "Port (default 70): " nil nil "70")))
892 (elpher-make-node (concat "gopher://" host-or-url
895 (elpher-make-address ?1 selector host-or-url
896 (string-to-number port-string))))))))
897 (switch-to-buffer "*elpher*")
898 (elpher-visit-node node)))
900 (defun elpher-redraw ()
901 "Redraw current page."
903 (if elpher-current-node
904 (elpher-visit-node elpher-current-node)
905 (message "No current site.")))
907 (defun elpher-reload ()
908 "Reload current page."
910 (if elpher-current-node
911 (elpher-reload-current-node)
912 (message "No current site.")))
914 (defun elpher-toggle-tls ()
915 "Toggle TLS encryption mode."
917 (setq elpher-use-tls (not elpher-use-tls))
919 (if (gnutls-available-p)
920 (message "TLS mode enabled. (Will not affect current page until reload.)")
921 (setq elpher-use-tls nil)
922 (error "Cannot enable TLS mode: GnuTLS not available"))
923 (message "TLS mode disabled. (Will not affect current page until reload.)")))
925 (defun elpher-view-raw ()
926 "View raw server response for current page."
928 (if elpher-current-node
929 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
930 (error "This page was not generated by a server")
931 (elpher-visit-node elpher-current-node
932 #'elpher-get-node-raw))
933 (message "No current site.")))
935 (defun elpher-back ()
936 "Go to previous site."
938 (if (elpher-node-parent elpher-current-node)
939 (elpher-visit-parent-node)
940 (error "No previous site")))
942 (defun elpher-download ()
943 "Download the link at point."
945 (let ((button (button-at (point))))
947 (let ((node (button-get button 'elpher-node)))
948 (if (elpher-address-special-p (elpher-node-address node))
949 (error "Cannot download this link")
950 (elpher-visit-node (button-get button 'elpher-node)
951 #'elpher-get-node-download)))
952 (error "No link selected"))))
954 (defun elpher-download-current ()
955 "Download the current page."
957 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
958 (error "Cannot download this page")
959 (elpher-visit-node (elpher-make-node
960 (elpher-node-display-string elpher-current-node)
961 (elpher-node-address elpher-current-node)
963 #'elpher-get-node-download
966 (defun elpher-build-link-map ()
967 "Build alist mapping link names to destination nodes in current buffer."
969 (b (next-button (point-min) t)))
971 (add-to-list 'link-map (cons (button-label b) b))
972 (setq b (next-button (button-start b))))
975 (defun elpher-jump ()
976 "Select a directory entry by name. Similar to the info browser (m)enu command."
978 (let* ((link-map (elpher-build-link-map)))
980 (let ((key (let ((completion-ignore-case t))
981 (completing-read "Directory item/link: "
983 (if (and key (> (length key) 0))
984 (let ((b (cdr (assoc key link-map))))
985 (goto-char (button-start b))
986 (button-activate b)))))))
988 (defun elpher-root-dir ()
989 "Visit root of current server."
991 (let* ((address (elpher-node-address elpher-current-node))
992 (host (elpher-address-host address)))
994 (let ((host (elpher-address-host address))
995 (selector (elpher-address-selector address))
996 (port (elpher-address-port address)))
997 (if (> (length selector) 0)
998 (let ((root-address (elpher-make-address ?1 "" host port)))
1000 (elpher-make-node (concat "gopher://" host
1001 ":" (number-to-string port)
1004 (error "Already at root directory of current server")))
1005 (error "Command invalid for this page"))))
1007 (defun elpher-bookmarks-current-p ()
1008 "Return non-nil if current node is a bookmarks page."
1009 (eq (elpher-address-type (elpher-node-address elpher-current-node)) 'bookmarks))
1011 (defun elpher-reload-bookmarks ()
1012 "Reload bookmarks if current node is a bookmarks page."
1013 (if (elpher-bookmarks-current-p)
1014 (elpher-reload-current-node)))
1016 (defun elpher-bookmark-current ()
1017 "Bookmark the current node."
1019 (let ((address (elpher-node-address elpher-current-node))
1020 (display-string (elpher-node-display-string elpher-current-node)))
1021 (if (not (elpher-address-special-p address))
1022 (let ((bookmark-display-string (read-string "Bookmark display string: "
1024 (elpher-add-address-bookmark address bookmark-display-string)
1025 (message "Bookmark added."))
1026 (error "Cannot bookmark %s" display-string))))
1028 (defun elpher-bookmark-link ()
1029 "Bookmark the link at point."
1031 (let ((button (button-at (point))))
1033 (let* ((node (button-get button 'elpher-node))
1034 (address (elpher-node-address node))
1035 (display-string (elpher-node-display-string node)))
1036 (if (not (elpher-address-special-p address))
1037 (let ((bookmark-display-string (read-string "Bookmark display string: "
1039 (elpher-add-address-bookmark address bookmark-display-string)
1040 (elpher-reload-bookmarks)
1041 (message "Bookmark added."))
1042 (error "Cannot bookmark %s" display-string)))
1043 (error "No link selected"))))
1045 (defun elpher-unbookmark-current ()
1046 "Remove bookmark for the current node."
1048 (let ((address (elpher-node-address elpher-current-node)))
1049 (unless (elpher-address-special-p address)
1050 (elpher-remove-address-bookmark address)
1051 (message "Bookmark removed."))))
1053 (defun elpher-unbookmark-link ()
1054 "Remove bookmark for the link at point."
1056 (let ((button (button-at (point))))
1058 (let ((node (button-get button 'elpher-node)))
1059 (elpher-remove-address-bookmark (elpher-node-address node))
1060 (elpher-reload-bookmarks)
1061 (message "Bookmark removed."))
1062 (error "No link selected"))))
1064 (defun elpher-bookmarks ()
1067 (switch-to-buffer "*elpher*")
1069 (elpher-make-node "Bookmarks Page" (elpher-make-address 'bookmarks))))
1071 (defun elpher-info-node (node)
1072 "Display information on NODE."
1073 (let ((display-string (elpher-node-display-string node))
1074 (address (elpher-node-address node)))
1075 (if (not (elpher-address-special-p address))
1076 (message "`%s' on %s port %s"
1077 (elpher-address-selector address)
1078 (elpher-address-host address)
1079 (elpher-address-port address))
1080 (message "%s" display-string))))
1082 (defun elpher-info-link ()
1083 "Display information on node corresponding to link at point."
1085 (let ((button (button-at (point))))
1087 (elpher-info-node (button-get button 'elpher-node))
1088 (error "No item selected"))))
1090 (defun elpher-info-current ()
1091 "Display information on current node."
1093 (elpher-info-node elpher-current-node))
1095 (defun elpher-get-address-url (address)
1096 "Get URL representation of ADDRESS."
1097 (let ((type (elpher-address-type address))
1098 (selector (elpher-address-selector address))
1099 (host (elpher-address-host address))
1100 (port (elpher-address-port address)))
1101 (if (and (equal type ?h)
1102 (string-prefix-p "URL:" selector))
1103 (elt (split-string selector "URL:") 1)
1105 (if (elpher-address-use-tls-p address) "s" "")
1110 (format ":%d" port))
1114 (defun elpher-copy-node-url (node)
1115 "Copy URL representation of address of NODE to `kill-ring'."
1116 (let ((address (elpher-node-address node)))
1117 (if (elpher-address-special-p address)
1118 (error (format "Cannot represent %s as URL" (elpher-node-display-string node)))
1119 (let ((url (elpher-get-address-url address)))
1120 (message "Copied \"%s\" to kill-ring/clipboard." url)
1123 (defun elpher-copy-link-url ()
1124 "Copy URL of item at point to `kill-ring'."
1126 (let ((button (button-at (point))))
1128 (elpher-copy-node-url (button-get button 'elpher-node))
1129 (error "No item selected"))))
1131 (defun elpher-copy-current-url ()
1132 "Copy URL of current node to `kill-ring'."
1134 (elpher-copy-node-url elpher-current-node))
1136 (defun elpher-set-coding-system ()
1137 "Specify an explicit character coding system."
1139 (let ((system (read-coding-system "Set coding system to use (default is to autodetect): " nil)))
1140 (setq elpher-user-coding-system system)
1142 (message "Coding system fixed to %s. (Reload to see effect)." system)
1143 (message "Coding system set to autodetect. (Reload to see effect)."))))
1148 (defvar elpher-mode-map
1149 (let ((map (make-sparse-keymap)))
1150 (define-key map (kbd "TAB") 'elpher-next-link)
1151 (define-key map (kbd "<backtab>") 'elpher-prev-link)
1152 (define-key map (kbd "u") 'elpher-back)
1153 (define-key map (kbd "O") 'elpher-root-dir)
1154 (define-key map (kbd "g") 'elpher-go)
1155 (define-key map (kbd "r") 'elpher-redraw)
1156 (define-key map (kbd "R") 'elpher-reload)
1157 (define-key map (kbd "T") 'elpher-toggle-tls)
1158 (define-key map (kbd "w") 'elpher-view-raw)
1159 (define-key map (kbd "d") 'elpher-download)
1160 (define-key map (kbd "D") 'elpher-download-current)
1161 (define-key map (kbd "m") 'elpher-jump)
1162 (define-key map (kbd "i") 'elpher-info-link)
1163 (define-key map (kbd "I") 'elpher-info-current)
1164 (define-key map (kbd "c") 'elpher-copy-link-url)
1165 (define-key map (kbd "C") 'elpher-copy-current-url)
1166 (define-key map (kbd "a") 'elpher-bookmark-link)
1167 (define-key map (kbd "A") 'elpher-bookmark-current)
1168 (define-key map (kbd "x") 'elpher-unbookmark-link)
1169 (define-key map (kbd "X") 'elpher-unbookmark-current)
1170 (define-key map (kbd "B") 'elpher-bookmarks)
1171 (define-key map (kbd "S") 'elpher-set-coding-system)
1172 (when (fboundp 'evil-define-key)
1173 (evil-define-key 'motion map
1174 (kbd "TAB") 'elpher-next-link
1175 (kbd "C-]") 'elpher-follow-current-link
1176 (kbd "C-t") 'elpher-back
1177 (kbd "u") 'elpher-back
1178 (kbd "O") 'elpher-root-dir
1179 (kbd "g") 'elpher-go
1180 (kbd "r") 'elpher-redraw
1181 (kbd "R") 'elpher-reload
1182 (kbd "T") 'elpher-toggle-tls
1183 (kbd "w") 'elpher-view-raw
1184 (kbd "d") 'elpher-download
1185 (kbd "D") 'elpher-download-current
1186 (kbd "m") 'elpher-jump
1187 (kbd "i") 'elpher-info-link
1188 (kbd "I") 'elpher-info-current
1189 (kbd "c") 'elpher-copy-link-url
1190 (kbd "C") 'elpher-copy-current-url
1191 (kbd "a") 'elpher-bookmark-link
1192 (kbd "A") 'elpher-bookmark-current
1193 (kbd "x") 'elpher-unbookmark-link
1194 (kbd "X") 'elpher-unbookmark-current
1195 (kbd "B") 'elpher-bookmarks
1196 (kbd "S") 'elpher-set-coding-system))
1198 "Keymap for gopher client.")
1200 (define-derived-mode elpher-mode special-mode "elpher"
1201 "Major mode for elpher, an elisp gopher client.")
1203 (when (fboundp 'evil-set-initial-state)
1204 (evil-set-initial-state 'elpher-mode 'motion))
1206 ;;; Main start procedure
1211 "Start elpher with default landing page."
1213 (if (get-buffer "*elpher*")
1214 (switch-to-buffer "*elpher*")
1215 (switch-to-buffer "*elpher*")
1216 (setq elpher-current-node nil)
1217 (let ((start-node (elpher-make-node "Elpher Start Page"
1218 (elpher-make-address 'start))))
1219 (elpher-visit-node start-node)))
1220 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
1222 ;;; elpher.el ends here