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 "26"))
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 and friendly gopher client
30 ;; for GNU Emacs. It supports:
32 ;; - intuitive keyboard and mouse-driven browsing,
33 ;; - out-of-the-box compatibility with evil-mode,
34 ;; - clickable web and gopher links *in plain text*,
35 ;; - caching of visited sites,
36 ;; - pleasant and configurable colouring of Gopher directories,
37 ;; - direct visualisation of image files,
38 ;; - a simple bookmark management system,
39 ;; - connections using TLS encryption.
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 ;; Full instructions can be found in the Elpher info manual.
47 ;; Elpher is under active development. Any suggestions for
48 ;; improvements are welcome!
66 (defconst elpher-version "2.0.0"
67 "Current version of elpher.")
69 (defconst elpher-margin-width 6
70 "Width of left-hand margin used when rendering indicies.")
72 (defconst elpher-type-map
73 '(((gopher ?0) elpher-get-text-node "txt" elpher-text)
74 ((gopher ?1) elpher-get-index-node "/" elpher-index)
75 ((gopher ?4) elpher-get-node-download "bin" elpher-binary)
76 ((gopher ?5) elpher-get-node-download "bin" elpher-binary)
77 ((gopher ?7) elpher-get-search-node "?" elpher-search)
78 ((gopher ?8) elpher-get-telnet-node "tel" elpher-telnet)
79 ((gopher ?9) elpher-get-node-download "bin" elpher-binary)
80 ((gopher ?g) elpher-get-image-node "img" elpher-image)
81 ((gopher ?p) elpher-get-image-node "img" elpher-image)
82 ((gopher ?I) elpher-get-image-node "img" elpher-image)
83 ((gopher ?d) elpher-get-node-download "doc" elpher-binary)
84 ((gopher ?P) elpher-get-node-download "doc" elpher-binary)
85 ((gopher ?s) elpher-get-node-download "snd" elpher-binary)
86 ((gopher ?h) elpher-get-url-node "htm" elpher-html)
87 ((special bookmarks) elpher-get-bookmarks-node)
88 ((special start) elpher-get-start-node))
89 "Association list from types to getters, margin codes and index faces.")
92 ;;; Customization group
99 ;; Face customizations
101 (defface elpher-index
102 '((t :inherit font-lock-keyword-face))
103 "Face used for directory type directory records.")
107 "Face used for text type directory records.")
110 '((t :inherit default))
111 "Face used for info type directory records.")
113 (defface elpher-image
114 '((t :inherit font-lock-string-face))
115 "Face used for image type directory records.")
117 (defface elpher-search
118 '((t :inherit warning))
119 "Face used for search type directory records.")
122 '((t :inherit font-lock-comment-face))
123 "Face used for url type directory records.")
125 (defface elpher-telnet
126 '((t :inherit font-lock-function-name-face))
127 "Face used for telnet type directory records.")
129 (defface elpher-binary
130 '((t :inherit font-lock-doc-face))
131 "Face used for binary type directory records.")
133 (defface elpher-unknown
134 '((t :inherit error))
135 "Face used for directory records with unknown/unsupported types.")
137 (defface elpher-margin-key
139 "Face used for directory margin key.")
141 (defface elpher-margin-brackets
142 '((t :inherit shadow))
143 "Face used for brackets around directory margin key.")
145 ;; Other customizations
147 (defcustom elpher-open-urls-with-eww nil
148 "If non-nil, open URL selectors using eww.
149 Otherwise, use the system browser via the BROWSE-URL function."
152 (defcustom elpher-buttonify-urls-in-directories nil
153 "If non-nil, turns URLs matched in directories into clickable buttons."
156 (defcustom elpher-use-header t
157 "If non-nil, display current node information in buffer header."
160 (defcustom elpher-auto-disengage-TLS nil
161 "If non-nil, automatically disengage TLS following an unsuccessful connection.
162 While enabling this may seem convenient, it is also potentially dangerous as it
163 allows switching from an encrypted channel back to plain text without user input."
172 ;; An elpher "address" object is either a url object or a symbol.
173 ;; Symbol addresses are "special", corresponding to pages generated
174 ;; dynamically for and by elpher. All others represent pages which
175 ;; rely on content retrieved over the network.
177 (defun elpher-address-from-url (url-string)
178 "Create a ADDRESS object corresponding to the given URL-STRING."
179 (let ((data (match-data))) ; Prevent parsing clobbering match data
181 (let ((url (url-generic-parse-url url-string)))
182 (unless (url-host url)
183 (setf (url-host url) (url-filename url))
184 (setf (url-filename "")))
185 (unless (url-type url)
186 (setf (url-type url) "gopher"))
187 (if (and (url-type url)
189 (let ((is-gopher (or (equal "gopher" (url-type url))
190 (equal "gophers" (url-type url)))))
191 (setf (url-filename url)
192 (url-unhex-string (url-filename url)))
193 (when (string-empty-p (url-filename url))
195 (setf (url-filename url) "1")))
196 (unless (> (url-port url) 0)
198 (setf (url-port url) 70)))
200 (error "Malformed URL" url)))
201 (set-match-data data))))
203 (defun elpher-make-gopher-address (type selector host port &optional tls)
204 "Create an ADDRESS object corresponding to the given gopher directory record
205 attributes: TYPE, SELECTOR, HOST and PORT."
206 (elpher-address-from-url
207 (concat "gopher" (if tls "s" "")
209 ":" (number-to-string port)
213 (defun elpher-make-special-address (type)
214 "Create an ADDRESS object corresponding to the given special page symbol TYPE."
217 (defun elpher-address-to-url (address)
218 "Get string representation of ADDRESS, or nil if ADDRESS is special."
219 (if (not (elpher-address-special-p address))
220 (url-encode-url (url-recreate-url address))
223 (defun elpher-address-type (address)
224 "Retrieve selector type from ADDRESS object."
225 (if (symbolp address)
226 (list 'special address)
227 (let ((protocol (url-type address)))
228 (cond ((or (equal protocol "gopher")
229 (equal protocol "gophers"))
230 (list 'gopher (string-to-char (substring (url-filename address) 1))))
231 ((string-equal protocol "gemini")
234 (defun elpher-address-protocol (address)
235 (if (symbolp address)
239 (defun elpher-address-filename (address)
240 (if (symbolp address)
242 (url-filename address)))
244 (defun elpher-address-host (address)
245 "Retrieve host from ADDRESS object."
248 (defun elpher-address-port (address)
249 "Retrieve port from ADDRESS object."
252 (defun elpher-address-special-p (address)
253 "Return non-nil if ADDRESS object is special (e.g. start page, bookmarks page)."
256 (defun elpher-address-gopher-p (address)
257 "Return non-nill if ADDRESS object is a gopher address."
258 (memq (elpher-address-protocol address) '("gopher gophers")))
260 (defun elpher-gopher-address-selector (address)
261 "Retrieve gopher selector from ADDRESS object."
262 (substring (url-filename address) 2))
266 (defun elpher-make-node (display-string address &optional parent)
267 "Create a node in the page hierarchy.
269 DISPLAY-STRING records the display string used for the page.
271 ADDRESS specifies the address object of the page.
273 The optional PARENT specifies the parent node in the hierarchy.
274 This is set every time the node is visited, so while it forms
275 an important part of the node data there is no need to set it
277 (list display-string address parent))
279 (defun elpher-node-display-string (node)
280 "Retrieve the display string of NODE."
283 (defun elpher-node-address (node)
284 "Retrieve the ADDRESS object of NODE."
287 (defun elpher-node-parent (node)
288 "Retrieve the parent node of NODE."
291 (defun elpher-set-node-parent (node parent)
292 "Set the parent node of NODE to be PARENT."
293 (setcar (cdr (cdr node)) parent))
297 (defvar elpher-content-cache (make-hash-table :test 'equal))
298 (defvar elpher-pos-cache (make-hash-table :test 'equal))
300 (defun elpher-get-cached-content (address)
301 "Retrieve the cached content for ADDRESS, or nil if none exists."
302 (gethash address elpher-content-cache))
304 (defun elpher-cache-content (address content)
305 "Set the content cache for ADDRESS to CONTENT."
306 (puthash address content elpher-content-cache))
308 (defun elpher-get-cached-pos (address)
309 "Retrieve the cached cursor position for ADDRESS, or nil if none exists."
310 (gethash address elpher-pos-cache))
312 (defun elpher-cache-pos (address pos)
313 "Set the cursor position cache for ADDRESS to POS."
314 (puthash address pos elpher-pos-cache))
316 ;; Node graph traversal
318 (defvar elpher-current-node nil)
320 (defun elpher-visit-node (node &optional getter preserve-parent)
321 "Visit NODE using its own getter or GETTER, if non-nil.
322 Additionally, set the parent of NODE to `elpher-current-node',
323 unless PRESERVE-PARENT is non-nil."
325 (elpher-process-cleanup)
326 (unless preserve-parent
327 (if (and (elpher-node-parent elpher-current-node)
328 (equal (elpher-node-address elpher-current-node)
329 (elpher-node-address node)))
330 (elpher-set-node-parent node (elpher-node-parent elpher-current-node))
331 (elpher-set-node-parent node elpher-current-node)))
332 (setq elpher-current-node node)
335 (let* ((address (elpher-node-address node))
336 (type (elpher-address-type address))
337 (type-record (cdr (assoc type elpher-type-map))))
339 (funcall (car type-record))
340 (elpher-visit-parent-node)
342 (`(gopher ,type-char)
343 (error "Unsupported gopher selector type '%c'" type-char))
345 (error "Unsupported address type '%S'" type)))))))
347 (defun elpher-visit-parent-node ()
348 "Visit the parent of the current node."
349 (let ((parent-node (elpher-node-parent elpher-current-node)))
351 (elpher-visit-node parent-node nil t))))
353 (defun elpher-reload-current-node ()
354 "Reload the current node, discarding any existing cached content."
355 (elpher-cache-content (elpher-node-address elpher-current-node) nil)
356 (elpher-visit-node elpher-current-node))
358 (defun elpher-save-pos ()
359 "Save the current position of point to the current node."
360 (when elpher-current-node
361 (elpher-cache-pos (elpher-node-address elpher-current-node) (point))))
363 (defun elpher-restore-pos ()
364 "Restore the position of point to that cached in the current node."
365 (let ((pos (elpher-get-cached-pos (elpher-node-address elpher-current-node))))
368 (goto-char (point-min)))))
371 ;;; Buffer preparation
374 (defun elpher-update-header ()
375 "If `elpher-use-header' is true, display current node info in window header."
376 (if elpher-use-header
377 (setq header-line-format (elpher-node-display-string elpher-current-node))))
379 (defmacro elpher-with-clean-buffer (&rest args)
380 "Evaluate ARGS with a clean *elpher* buffer as current."
381 (list 'with-current-buffer "*elpher*"
383 (append (list 'let '((inhibit-read-only t))
385 '(elpher-update-header))
392 (defvar elpher-user-coding-system nil
393 "User-specified coding system to use for decoding text responses.")
395 (defun elpher-decode (string)
396 "Decode STRING using autodetected or user-specified coding system."
397 (decode-coding-string string
398 (if elpher-user-coding-system
399 elpher-user-coding-system
400 (detect-coding-string string t))))
402 (defun elpher-preprocess-text-response (string)
403 "Preprocess text selector response contained in STRING.
404 This involes decoding the character representation, and clearing
405 away CRs and any terminating period."
406 (elpher-decode (replace-regexp-in-string "\n\.\n$" "\n"
407 (replace-regexp-in-string "\r" "" string))))
413 (defun elpher-insert-index (string)
414 "Insert the index corresponding to STRING into the current buffer."
415 ;; Should be able to split directly on CRLF, but some non-conformant
416 ;; LF-only servers sadly exist, hence the following.
417 (let ((str-processed (elpher-preprocess-text-response string)))
418 (dolist (line (split-string str-processed "\n"))
419 (unless (= (length line) 0)
420 (let* ((type (elt line 0))
421 (fields (split-string (substring line 1) "\t"))
422 (display-string (elt fields 0))
423 (selector (elt fields 1))
424 (host (elt fields 2))
425 (port (if (elt fields 3)
426 (string-to-number (elt fields 3))
428 (address (elpher-make-gopher-address type selector host port)))
429 (elpher-insert-index-record display-string address))))))
431 (defun elpher-insert-margin (&optional type-name)
432 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
435 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
437 (propertize "[" 'face 'elpher-margin-brackets)
438 (propertize type-name 'face 'elpher-margin-key)
439 (propertize "]" 'face 'elpher-margin-brackets))))
441 (insert (make-string elpher-margin-width ?\s))))
443 (defun elpher-node-button-help (node)
444 "Return a string containing the help text for a button corresponding to NODE."
445 (let ((address (elpher-node-address node)))
446 (if (equal (elpher-address-type address) '(gopher ?h))
447 (let ((url (cadr (split-string (elpher-gopher-address-selector address) "URL:"))))
448 (format "mouse-1, RET: open url '%s'" url))
449 (format "mouse-1, RET: open '%s' on %s port %s"
450 (elpher-gopher-address-selector address)
451 (elpher-address-host address)
452 (elpher-address-port address)))))
454 (defun elpher-insert-index-record (display-string address)
455 "Function to insert an index record into the current buffer.
456 The contents of the record are dictated by DISPLAY-STRING and ADDRESS."
457 (let* ((type (elpher-address-type address))
458 (type-map-entry (cdr (assoc type elpher-type-map))))
460 (let* ((margin-code (elt type-map-entry 1))
461 (face (elt type-map-entry 2))
462 (node (elpher-make-node display-string address)))
463 (elpher-insert-margin margin-code)
464 (insert-text-button display-string
467 'action #'elpher-click-link
469 'help-echo (elpher-node-button-help node)))
471 ('(gopher ?i) ;; Information
472 (elpher-insert-margin)
474 (if elpher-buttonify-urls-in-directories
475 (elpher-buttonify-urls display-string)
477 'face 'elpher-info)))
478 (`(gopher ,selector-type) ;; Unknown
479 (elpher-insert-margin (concat (char-to-string selector-type) "?"))
480 (insert (propertize display-string
481 'face 'elpher-unknown)))))
484 (defun elpher-click-link (button)
485 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
486 (let ((node (button-get button 'elpher-node)))
487 (elpher-visit-node node)))
490 ;;; Gopher selector retrieval (all kinds)
493 (defun elpher-process-cleanup ()
494 "Immediately shut down any extant elpher process."
495 (let ((p (get-process "elpher-process")))
496 (if p (delete-process p))))
498 (defvar elpher-use-tls nil
499 "If non-nil, use TLS to communicate with gopher servers.")
501 (defvar elpher-selector-string)
503 (defun elpher-get-selector (address after &optional propagate-error)
504 "Retrieve selector specified by ADDRESS, then execute AFTER.
505 The result is stored as a string in the variable ‘elpher-selector-string’.
507 Usually errors result in an error page being displayed. This is only
508 appropriate if the selector is to be directly viewed. If PROPAGATE-ERROR
509 is non-nil, this message is not displayed. Instead, the error propagates
510 up to the calling function."
511 (setq elpher-selector-string "")
512 (when (equal (elpher-address-protocol address) "gophers")
513 (if (gnutls-available-p)
514 (when (not elpher-use-tls)
515 (setq elpher-use-tls t)
516 (message "Engaging TLS mode."))
517 (error "Cannot retrieve TLS selector: GnuTLS not available")))
518 (condition-case the-error
519 (let* ((kill-buffer-query-functions nil)
520 (proc (open-network-stream "elpher-process"
522 (elpher-address-host address)
523 (elpher-address-port address)
524 :type (if elpher-use-tls 'tls 'plain))))
525 (set-process-coding-system proc 'binary)
526 (set-process-filter proc
527 (lambda (proc string)
528 (setq elpher-selector-string
529 (concat elpher-selector-string string))))
530 (set-process-sentinel proc after)
531 (process-send-string proc
532 (concat (elpher-gopher-address-selector address) "\n")))
534 (if (and (consp the-error)
535 (eq (car the-error) 'gnutls-error)
536 (not (equal (elpher-address-protocol address) "gophers"))
537 (or elpher-auto-disengage-TLS
538 (yes-or-no-p "Could not establish encrypted connection. Disable TLS mode? ")))
540 (message "Disengaging TLS mode.")
541 (setq elpher-use-tls nil)
542 (elpher-get-selector address after))
543 (elpher-process-cleanup)
546 (elpher-with-clean-buffer
547 (insert (propertize "\n---- ERROR -----\n\n" 'face 'error)
548 "Failed to connect to " (elpher-address-to-url address) ".\n"
549 (propertize "\n----------------\n\n" 'face 'error)
550 "Press 'u' to return to the previous page.")))))))
554 (defun elpher-get-index-node ()
555 "Getter which retrieves the current node contents as an index."
556 (let* ((address (elpher-node-address elpher-current-node))
557 (content (elpher-get-cached-content address)))
560 (elpher-with-clean-buffer
562 (elpher-restore-pos)))
563 (elpher-with-clean-buffer
564 (insert "LOADING DIRECTORY... (use 'u' to cancel)"))
565 (elpher-get-selector address
567 (unless (string-prefix-p "deleted" event)
568 (elpher-with-clean-buffer
569 (elpher-insert-index elpher-selector-string)
571 (elpher-cache-content
572 (elpher-node-address elpher-current-node)
573 (buffer-string)))))))))
577 (defconst elpher-url-regex
578 "\\([a-zA-Z]+\\)://\\([a-zA-Z0-9.\-]+\\|\[[a-zA-Z0-9:]+\]\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
579 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
581 (defun elpher-make-node-from-matched-url (&optional string)
582 "Convert most recent `elpher-url-regex' match to a node.
584 If STRING is non-nil, this is given as an argument to all `match-string'
585 calls, as is necessary if the match is performed by `string-match'."
586 (let ((url (match-string 0 string))
587 (protocol (downcase (match-string 1 string))))
588 (if (or (string= protocol "gopher")
589 (string= protocol "gophers"))
590 (let* ((bare-host (match-string 2 string))
591 (host (if (string-prefix-p "[" bare-host)
592 (substring bare-host 1 (- (length bare-host) 1))
594 (port (if (> (length (match-string 3 string)) 1)
595 (string-to-number (substring (match-string 3 string) 1))
597 (type-and-selector (match-string 4 string))
598 (type (if (> (length type-and-selector) 1)
599 (elt type-and-selector 1)
601 (selector (decode-coding-string
603 (if (> (length type-and-selector) 1)
604 (substring type-and-selector 2)
606 (use-tls (string= protocol "gophers"))
607 (address (elpher-make-gopher-address type selector host port use-tls)))
608 (elpher-make-node url address))
609 (let* ((host (match-string 2 string))
610 (port (if (> (length (match-string 3 string)) 1)
611 (string-to-number (substring (match-string 3 string) 1))
613 (selector (concat "URL:" url))
614 (address (elpher-make-gopher-address ?h selector host port)))
615 (elpher-make-node url address)))))
618 (defun elpher-buttonify-urls (string)
619 "Turn substrings which look like urls in STRING into clickable buttons."
622 (goto-char (point-min))
623 (while (re-search-forward elpher-url-regex nil t)
624 (let ((node (elpher-make-node-from-matched-url)))
625 (make-text-button (match-beginning 0)
628 'action #'elpher-click-link
630 'help-echo (elpher-node-button-help node))))
633 (defun elpher-get-text-node ()
634 "Getter which retrieves the current node contents as a text document."
635 (let* ((address (elpher-node-address elpher-current-node))
636 (content (elpher-get-cached-content address)))
639 (elpher-with-clean-buffer
641 (elpher-restore-pos)))
643 (elpher-with-clean-buffer
644 (insert "LOADING TEXT... (use 'u' to cancel)"))
645 (elpher-get-selector address
647 (unless (string-prefix-p "deleted" event)
648 (elpher-with-clean-buffer
649 (insert (elpher-buttonify-urls
650 (elpher-preprocess-text-response
651 elpher-selector-string)))
653 (elpher-cache-content
654 (elpher-node-address elpher-current-node)
655 (buffer-string))))))))))
659 (defun elpher-get-image-node ()
660 "Getter which retrieves the current node contents as an image to view."
661 (let* ((address (elpher-node-address elpher-current-node)))
662 (if (display-images-p)
664 (elpher-with-clean-buffer
665 (insert "LOADING IMAGE... (use 'u' to cancel)"))
666 (elpher-get-selector address
668 (unless (string-prefix-p "deleted" event)
669 (let ((image (create-image
670 elpher-selector-string
672 (elpher-with-clean-buffer
674 (elpher-restore-pos)))))))
675 (elpher-get-node-download))))
679 (defun elpher-get-search-node ()
680 "Getter which submits a search query to the address of the current node."
681 (let* ((address (elpher-node-address elpher-current-node))
682 (content (elpher-get-cached-content address))
686 (elpher-with-clean-buffer
688 (elpher-restore-pos))
689 (message "Displaying cached search results. Reload to perform a new search."))
691 (let* ((query-string (read-string "Query: "))
692 (query-selector (concat (elpher-gopher-address-selector address) "\t" query-string))
693 (search-address (elpher-make-gopher-address ?1
695 (elpher-address-host address)
696 (elpher-address-port address))))
698 (elpher-with-clean-buffer
699 (insert "LOADING RESULTS... (use 'u' to cancel)"))
700 (elpher-get-selector search-address
702 (unless (string-prefix-p "deleted" event)
703 (elpher-with-clean-buffer
704 (elpher-insert-index elpher-selector-string))
705 (goto-char (point-min))
706 (elpher-cache-content
707 (elpher-node-address elpher-current-node)
710 (elpher-visit-parent-node))))))
712 ;; Raw server response retrieval
714 (defun elpher-get-node-raw ()
715 "Getter which retrieves the raw server response for the current node."
716 (let ((address (elpher-node-address elpher-current-node)))
717 (elpher-with-clean-buffer
718 (insert "LOADING RAW SERVER RESPONSE... (use 'u' to cancel)"))
719 (elpher-get-selector address
721 (unless (string-prefix-p "deleted" event)
722 (elpher-with-clean-buffer
723 (insert elpher-selector-string)
724 (goto-char (point-min)))))))
725 (message "Displaying raw server response. Reload or redraw to return to standard view."))
727 ;; File export retrieval
729 (defvar elpher-download-filename)
731 (defun elpher-get-node-download ()
732 "Getter which retrieves the current node and writes the result to a file."
733 (let* ((address (elpher-node-address elpher-current-node))
734 (selector (elpher-gopher-address-selector address)))
735 (elpher-visit-parent-node) ; Do first in case of non-local exits.
736 (let* ((filename-proposal (file-name-nondirectory selector))
737 (filename (read-file-name "Save file as: "
739 (if (> (length filename-proposal) 0)
742 (message "Downloading...")
743 (setq elpher-download-filename filename)
744 (condition-case the-error
745 (elpher-get-selector address
747 (let ((coding-system-for-write 'binary))
748 (with-temp-file elpher-download-filename
749 (insert elpher-selector-string)
750 (message (format "Download complate, saved to file %s."
751 elpher-download-filename)))))
754 (error "Error downloading %s" elpher-download-filename))))))
758 (defun elpher-insert-rendered-html (string)
759 "Use shr to insert rendered view of html STRING into current buffer."
760 (let ((dom (with-temp-buffer
762 (libxml-parse-html-region (point-min) (point-max)))))
763 (shr-insert-document dom)))
765 (defun elpher-get-url-node ()
766 "Getter which attempts to open the URL specified by the current node."
767 (let* ((address (elpher-node-address elpher-current-node))
768 (selector (elpher-gopher-address-selector address)))
769 (let ((url (elt (split-string selector "URL:") 1)))
772 (elpher-visit-parent-node) ; Do first in case of non-local exits.
773 (message "Opening URL...")
774 (if elpher-open-urls-with-eww
777 (let ((content (elpher-get-cached-content address)))
780 (elpher-with-clean-buffer
782 (elpher-restore-pos)))
783 (elpher-with-clean-buffer
784 (insert "LOADING HTML... (use 'u' to cancel)"))
785 (elpher-get-selector address
787 (unless (string-prefix-p "deleted" event)
788 (elpher-with-clean-buffer
789 (elpher-insert-rendered-html elpher-selector-string)
790 (goto-char (point-min))
791 (elpher-cache-content
792 (elpher-node-address elpher-current-node)
793 (buffer-string))))))))))))
795 ;; Telnet node connection
797 (defun elpher-get-telnet-node ()
798 "Getter which opens a telnet connection to the server specified by the current node."
799 (let* ((address (elpher-node-address elpher-current-node))
800 (host (elpher-address-host address))
801 (port (elpher-address-port address)))
802 (elpher-visit-parent-node)
805 ;; Start page node retrieval
807 (defun elpher-get-start-node ()
808 "Getter which displays the start page."
809 (elpher-with-clean-buffer
810 (insert " --------------------------------------------\n"
811 " Elpher Gopher Client \n"
812 " version " elpher-version "\n"
813 " --------------------------------------------\n"
815 "Default bindings:\n"
817 " - TAB/Shift-TAB: next/prev item on current page\n"
818 " - RET/mouse-1: open item under cursor\n"
819 " - m: select an item on current page by name (autocompletes)\n"
820 " - u: return to previous page\n"
821 " - o/O: visit different selector or the root menu of the current server\n"
822 " - g: go to a particular gopher address\n"
823 " - i/I: info on item under cursor or current page\n"
824 " - c/C: copy URL representation of item under cursor or current page\n"
825 " - a/A: bookmark the item under cursor or current page\n"
826 " - x/X: remove bookmark for item under cursor or current page\n"
827 " - B: visit the bookmarks page\n"
828 " - r: redraw current page (using cached contents if available)\n"
829 " - R: reload current page (regenerates cache)\n"
830 " - T: toggle TLS mode\n"
831 " - d/D: download item under cursor or current page\n"
832 " - .: display the raw server response for the current page\n"
833 " - S: set an explicit character coding system (default is to autodetect)\n"
835 "Start your exploration of gopher space:\n")
836 (elpher-insert-index-record "Floodgap Systems Gopher Server"
837 (elpher-make-gopher-address ?1 "" "gopher.floodgap.com" 70))
839 "Alternatively, select the following item and enter some search terms:\n")
840 (elpher-insert-index-record "Veronica-2 Gopher Search Engine"
841 (elpher-make-gopher-address ?7 "/v2/vs" "gopher.floodgap.com" 70))
844 (let ((help-string "RET,mouse-1: Open Elpher info manual (if available)"))
845 (insert-text-button "Elpher info manual"
847 'action (lambda (button)
851 'help-echo help-string))
852 (insert " for the full documentation. **\n")
854 (concat " (This should be available if you have installed Elpher using\n"
855 " MELPA. Otherwise you will have to install the manual yourself.)")
857 (elpher-restore-pos)))
859 ;; Bookmarks page node retrieval
861 (defun elpher-get-bookmarks-node ()
862 "Getter to load and display the current bookmark list."
863 (elpher-with-clean-buffer
864 (insert "---- Bookmark list ----\n\n")
865 (let ((bookmarks (elpher-load-bookmarks)))
867 (dolist (bookmark bookmarks)
868 (let ((display-string (elpher-bookmark-display-string bookmark))
869 (address (elpher-bookmark-address bookmark)))
870 (elpher-insert-index-record display-string address)))
871 (insert "No bookmarks found.\n")))
872 (insert "\n-----------------------\n\n"
873 "- u: return to previous page\n"
874 "- x: delete selected bookmark\n"
875 "- a: rename selected bookmark\n\n"
876 "Bookmarks are stored in the file "
877 (locate-user-emacs-file "elpher-bookmarks"))
878 (elpher-restore-pos)))
884 (defun elpher-make-bookmark (display-string url)
885 "Make an elpher bookmark.
886 DISPLAY-STRING determines how the bookmark will appear in the
887 bookmark list, while ADDRESS is the address of the entry."
888 (list display-string (elpher-address-to-url address)))
890 (defun elpher-bookmark-display-string (bookmark)
891 "Get the display string of BOOKMARK."
894 (defun elpher-set-bookmark-display-string (bookmark display-string)
895 "Set the display string of BOOKMARK to DISPLAY-STRING."
896 (setcar bookmark display-string))
898 (defun elpher-bookmark-url (bookmark)
899 "Get the address for BOOKMARK."
903 (defun elpher-save-bookmarks (bookmarks)
904 "Record the bookmark list BOOKMARKS to the user's bookmark file.
905 Beware that this completely replaces the existing contents of the file."
906 (with-temp-file (locate-user-emacs-file "elpher2-bookmarks")
908 (insert "; Elpher bookmarks file\n\n"
909 "; Bookmarks are stored as a list of (label URL) items.\n"
910 "; Feel free to edit by hand, but take care to ensure\n"
911 "; the list structure remains intact.\n\n")
912 (pp bookmarks (current-buffer))))
914 (defun elpher-load-bookmarks ()
915 "Get the list of bookmarks from the users's bookmark file."
918 (insert-file-contents (locate-user-emacs-file "elpher2-bookmarks"))
919 (goto-char (point-min))
920 (read (current-buffer)))))
922 (defun elpher-add-address-bookmark (address display-string)
923 "Save a bookmark for ADDRESS with label DISPLAY-STRING.)))
924 If ADDRESS is already bookmarked, update the label only."
925 (let ((bookmarks (elpher-load-bookmarks))
926 (url (elpher-address-to-url address)))
927 (let ((existing-bookmark (rassoc (list url) bookmarks)))
928 (if existing-bookmark
929 (elpher-set-bookmark-display-string existing-bookmark display-string)
930 (add-to-list 'bookmarks (elpher-make-bookmark display-string url))))
931 (elpher-save-bookmarks bookmarks)))
933 (defun elpher-remove-address-bookmark (address)
934 "Remove any bookmark to ADDRESS."
935 (let ((url (elpher-address-to-url address)))
936 (elpher-save-bookmarks
937 (seq-filter (lambda (bookmark)
938 (not (equal (elpher-bookmark-url bookmark) url)))
939 (elpher-load-bookmarks)))))
941 ;;; Interactive procedures
944 (defun elpher-next-link ()
945 "Move point to the next link on the current page."
949 (defun elpher-prev-link ()
950 "Move point to the previous link on the current page."
954 (defun elpher-follow-current-link ()
955 "Open the link or url at point."
960 "Go to a particular gopher site read from the minibuffer.
961 The site may be specified via a URL or explicitly in terms of
962 host, selector and port."
965 (let ((host-or-url (read-string "Gopher host or URL: ")))
966 (if (string-match elpher-url-regex host-or-url)
967 (elpher-make-node-from-matched-url host-or-url)
968 (let ((selector (read-string "Selector (default none): " nil nil ""))
969 (port-string (read-string "Port (default 70): " nil nil "70")))
970 (elpher-make-node (concat "gopher://" host-or-url
973 (elpher-make-gopher-address ?1 selector host-or-url
974 (string-to-number port-string))))))))
975 (switch-to-buffer "*elpher*")
976 (elpher-visit-node node)))
978 (defun elpher-go-current ()
979 "Go to a particular site read from the minibuffer, initialized with the current URL."
981 (let ((address (elpher-node-address elpher-current-node)))
982 (if (elpher-address-special-p address)
983 (error "Command not valid for this page")
984 (let ((url (read-string "URL: " (elpher-address-to-url address))))
985 (if (string-match elpher-url-regex url)
986 (let ((new-node (elpher-make-node-from-matched-url url)))
987 (unless (equal (elpher-node-address new-node) address)
988 (elpher-visit-node new-node)))
989 (error "Could not parse URL %s" url))))))
991 (defun elpher-redraw ()
992 "Redraw current page."
994 (if elpher-current-node
995 (elpher-visit-node elpher-current-node)
996 (message "No current site.")))
998 (defun elpher-reload ()
999 "Reload current page."
1001 (if elpher-current-node
1002 (elpher-reload-current-node)
1003 (message "No current site.")))
1005 (defun elpher-toggle-tls ()
1006 "Toggle TLS encryption mode."
1008 (setq elpher-use-tls (not elpher-use-tls))
1010 (if (gnutls-available-p)
1011 (message "TLS mode enabled. (Will not affect current page until reload.)")
1012 (setq elpher-use-tls nil)
1013 (error "Cannot enable TLS mode: GnuTLS not available"))
1014 (message "TLS mode disabled. (Will not affect current page until reload.)")))
1016 (defun elpher-view-raw ()
1017 "View raw server response for current page."
1019 (if elpher-current-node
1020 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
1021 (error "This page was not generated by a server")
1022 (elpher-visit-node elpher-current-node
1023 #'elpher-get-node-raw))
1024 (message "No current site.")))
1026 (defun elpher-back ()
1027 "Go to previous site."
1029 (if (elpher-node-parent elpher-current-node)
1030 (elpher-visit-parent-node)
1031 (error "No previous site")))
1033 (defun elpher-download ()
1034 "Download the link at point."
1036 (let ((button (button-at (point))))
1038 (let ((node (button-get button 'elpher-node)))
1039 (if (elpher-address-special-p (elpher-node-address node))
1040 (error "Cannot download this link")
1041 (elpher-visit-node (button-get button 'elpher-node)
1042 #'elpher-get-node-download)))
1043 (error "No link selected"))))
1045 (defun elpher-download-current ()
1046 "Download the current page."
1048 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
1049 (error "Cannot download this page")
1050 (elpher-visit-node (elpher-make-node
1051 (elpher-node-display-string elpher-current-node)
1052 (elpher-node-address elpher-current-node)
1053 elpher-current-node)
1054 #'elpher-get-node-download
1057 (defun elpher-build-link-map ()
1058 "Build alist mapping link names to destination nodes in current buffer."
1059 (let ((link-map nil)
1060 (b (next-button (point-min) t)))
1062 (add-to-list 'link-map (cons (button-label b) b))
1063 (setq b (next-button (button-start b))))
1066 (defun elpher-jump ()
1067 "Select a directory entry by name. Similar to the info browser (m)enu command."
1069 (let* ((link-map (elpher-build-link-map)))
1071 (let ((key (let ((completion-ignore-case t))
1072 (completing-read "Directory item/link: "
1074 (if (and key (> (length key) 0))
1075 (let ((b (cdr (assoc key link-map))))
1076 (goto-char (button-start b))
1077 (button-activate b)))))))
1079 (defun elpher-root-dir ()
1080 "Visit root of current server."
1082 (let* ((address (elpher-node-address elpher-current-node))
1083 (host (elpher-address-host address)))
1085 (let ((host (elpher-address-host address))
1086 (selector (elpher-gopher-address-selector address))
1087 (port (elpher-address-port address)))
1088 (if (> (length selector) 0)
1089 (let ((root-address (elpher-make-gopher-address ?1 "" host port)))
1091 (elpher-make-node (concat "gopher://" host
1092 ":" (number-to-string port)
1095 (error "Already at root directory of current server")))
1096 (error "Command invalid for this page"))))
1098 (defun elpher-bookmarks-current-p ()
1099 "Return non-nil if current node is a bookmarks page."
1100 (equal (elpher-address-type (elpher-node-address elpher-current-node))
1101 '(special bookmarks)))
1103 (defun elpher-reload-bookmarks ()
1104 "Reload bookmarks if current node is a bookmarks page."
1105 (if (elpher-bookmarks-current-p)
1106 (elpher-reload-current-node)))
1108 (defun elpher-bookmark-current ()
1109 "Bookmark the current node."
1111 (let ((address (elpher-node-address elpher-current-node))
1112 (display-string (elpher-node-display-string elpher-current-node)))
1113 (if (not (elpher-address-special-p address))
1114 (let ((bookmark-display-string (read-string "Bookmark display string: "
1116 (elpher-add-address-bookmark address bookmark-display-string)
1117 (message "Bookmark added."))
1118 (error "Cannot bookmark %s" display-string))))
1120 (defun elpher-bookmark-link ()
1121 "Bookmark the link at point."
1123 (let ((button (button-at (point))))
1125 (let* ((node (button-get button 'elpher-node))
1126 (address (elpher-node-address node))
1127 (display-string (elpher-node-display-string node)))
1128 (if (not (elpher-address-special-p address))
1129 (let ((bookmark-display-string (read-string "Bookmark display string: "
1131 (elpher-add-address-bookmark address bookmark-display-string)
1132 (elpher-reload-bookmarks)
1133 (message "Bookmark added."))
1134 (error "Cannot bookmark %s" display-string)))
1135 (error "No link selected"))))
1137 (defun elpher-unbookmark-current ()
1138 "Remove bookmark for the current node."
1140 (let ((address (elpher-node-address elpher-current-node)))
1141 (unless (elpher-address-special-p address)
1142 (elpher-remove-address-bookmark address)
1143 (message "Bookmark removed."))))
1145 (defun elpher-unbookmark-link ()
1146 "Remove bookmark for the link at point."
1148 (let ((button (button-at (point))))
1150 (let ((node (button-get button 'elpher-node)))
1151 (elpher-remove-address-bookmark (elpher-node-address node))
1152 (elpher-reload-bookmarks)
1153 (message "Bookmark removed."))
1154 (error "No link selected"))))
1156 (defun elpher-bookmarks ()
1157 "Visit bookmarks page."
1159 (switch-to-buffer "*elpher*")
1161 (elpher-make-node "Bookmarks Page" (elpher-make-special-address 'bookmarks))))
1163 (defun elpher-info-node (node)
1164 "Display information on NODE."
1165 (let ((display-string (elpher-node-display-string node))
1166 (address (elpher-node-address node)))
1167 (if (not (elpher-address-special-p address))
1168 (message "`%s' on %s port %s"
1169 (elpher-gopher-address-selector address)
1170 (elpher-address-host address)
1171 (elpher-address-port address))
1172 (message "%s" display-string))))
1174 (defun elpher-info-link ()
1175 "Display information on node corresponding to link at point."
1177 (let ((button (button-at (point))))
1179 (elpher-info-node (button-get button 'elpher-node))
1180 (error "No item selected"))))
1182 (defun elpher-info-current ()
1183 "Display information on current node."
1185 (elpher-info-node elpher-current-node))
1187 (defun elpher-copy-node-url (node)
1188 "Copy URL representation of address of NODE to `kill-ring'."
1189 (let ((address (elpher-node-address node)))
1190 (if (elpher-address-special-p address)
1191 (error (format "Cannot represent %s as URL" (elpher-node-display-string node)))
1192 (let ((url (elpher-address-to-url address)))
1193 (message "Copied \"%s\" to kill-ring/clipboard." url)
1196 (defun elpher-copy-link-url ()
1197 "Copy URL of item at point to `kill-ring'."
1199 (let ((button (button-at (point))))
1201 (elpher-copy-node-url (button-get button 'elpher-node))
1202 (error "No item selected"))))
1204 (defun elpher-copy-current-url ()
1205 "Copy URL of current node to `kill-ring'."
1207 (elpher-copy-node-url elpher-current-node))
1209 (defun elpher-set-coding-system ()
1210 "Specify an explicit character coding system."
1212 (let ((system (read-coding-system "Set coding system to use (default is to autodetect): " nil)))
1213 (setq elpher-user-coding-system system)
1215 (message "Coding system fixed to %s. (Reload to see effect)." system)
1216 (message "Coding system set to autodetect. (Reload to see effect)."))))
1222 (defvar elpher-mode-map
1223 (let ((map (make-sparse-keymap)))
1224 (define-key map (kbd "TAB") 'elpher-next-link)
1225 (define-key map (kbd "<backtab>") 'elpher-prev-link)
1226 (define-key map (kbd "u") 'elpher-back)
1227 (define-key map (kbd "O") 'elpher-root-dir)
1228 (define-key map (kbd "g") 'elpher-go)
1229 (define-key map (kbd "o") 'elpher-go-current)
1230 (define-key map (kbd "r") 'elpher-redraw)
1231 (define-key map (kbd "R") 'elpher-reload)
1232 (define-key map (kbd "T") 'elpher-toggle-tls)
1233 (define-key map (kbd ".") 'elpher-view-raw)
1234 (define-key map (kbd "d") 'elpher-download)
1235 (define-key map (kbd "D") 'elpher-download-current)
1236 (define-key map (kbd "m") 'elpher-jump)
1237 (define-key map (kbd "i") 'elpher-info-link)
1238 (define-key map (kbd "I") 'elpher-info-current)
1239 (define-key map (kbd "c") 'elpher-copy-link-url)
1240 (define-key map (kbd "C") 'elpher-copy-current-url)
1241 (define-key map (kbd "a") 'elpher-bookmark-link)
1242 (define-key map (kbd "A") 'elpher-bookmark-current)
1243 (define-key map (kbd "x") 'elpher-unbookmark-link)
1244 (define-key map (kbd "X") 'elpher-unbookmark-current)
1245 (define-key map (kbd "B") 'elpher-bookmarks)
1246 (define-key map (kbd "S") 'elpher-set-coding-system)
1247 (when (fboundp 'evil-define-key)
1248 (evil-define-key 'motion map
1249 (kbd "TAB") 'elpher-next-link
1250 (kbd "C-") 'elpher-follow-current-link
1251 (kbd "C-t") 'elpher-back
1252 (kbd "u") 'elpher-back
1253 (kbd "O") 'elpher-root-dir
1254 (kbd "g") 'elpher-go
1255 (kbd "o") 'elpher-go-current
1256 (kbd "r") 'elpher-redraw
1257 (kbd "R") 'elpher-reload
1258 (kbd "T") 'elpher-toggle-tls
1259 (kbd ".") 'elpher-view-raw
1260 (kbd "d") 'elpher-download
1261 (kbd "D") 'elpher-download-current
1262 (kbd "m") 'elpher-jump
1263 (kbd "i") 'elpher-info-link
1264 (kbd "I") 'elpher-info-current
1265 (kbd "c") 'elpher-copy-link-url
1266 (kbd "C") 'elpher-copy-current-url
1267 (kbd "a") 'elpher-bookmark-link
1268 (kbd "A") 'elpher-bookmark-current
1269 (kbd "x") 'elpher-unbookmark-link
1270 (kbd "X") 'elpher-unbookmark-current
1271 (kbd "B") 'elpher-bookmarks
1272 (kbd "S") 'elpher-set-coding-system))
1274 "Keymap for gopher client.")
1276 (define-derived-mode elpher-mode special-mode "elpher"
1277 "Major mode for elpher, an elisp gopher client.
1279 This mode is automatically enabled by the interactive
1280 functions which initialize the gopher client, namely
1281 `elpher', `elpher-go' and `elpher-bookmarks'.")
1283 (when (fboundp 'evil-set-initial-state)
1284 (evil-set-initial-state 'elpher-mode 'motion))
1287 ;;; Main start procedure
1292 "Start elpher with default landing page."
1294 (if (get-buffer "*elpher*")
1295 (switch-to-buffer "*elpher*")
1296 (switch-to-buffer "*elpher*")
1297 (setq elpher-current-node nil)
1298 (let ((start-node (elpher-make-node "Elpher Start Page"
1299 (elpher-make-special-address 'start))))
1300 (elpher-visit-node start-node)))
1301 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
1303 ;;; elpher.el ends here