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-html-node "htm" elpher-html)
87 (other-url elpher-get-other-url-node "url" elpher-other-url)
88 ((special bookmarks) elpher-get-bookmarks-node)
89 ((special start) elpher-get-start-node))
90 "Association list from types to getters, margin codes and index faces.")
93 ;;; Customization group
100 ;; Face customizations
102 (defface elpher-index
103 '((t :inherit font-lock-keyword-face))
104 "Face used for directory type directory records.")
108 "Face used for text type directory records.")
111 '((t :inherit default))
112 "Face used for info type directory records.")
114 (defface elpher-image
115 '((t :inherit font-lock-string-face))
116 "Face used for image type directory records.")
118 (defface elpher-search
119 '((t :inherit warning))
120 "Face used for search type directory records.")
123 '((t :inherit font-lock-comment-face))
124 "Face used for url type directory records.")
126 (defface elpher-other-url
127 '((t :inherit font-lock-comment-face))
128 "Face used for url type directory records.")
130 (defface elpher-telnet
131 '((t :inherit font-lock-function-name-face))
132 "Face used for telnet type directory records.")
134 (defface elpher-binary
135 '((t :inherit font-lock-doc-face))
136 "Face used for binary type directory records.")
138 (defface elpher-unknown
139 '((t :inherit error))
140 "Face used for directory records with unknown/unsupported types.")
142 (defface elpher-margin-key
144 "Face used for directory margin key.")
146 (defface elpher-margin-brackets
147 '((t :inherit shadow))
148 "Face used for brackets around directory margin key.")
150 ;; Other customizations
152 (defcustom elpher-open-urls-with-eww nil
153 "If non-nil, open URL selectors using eww.
154 Otherwise, use the system browser via the BROWSE-URL function."
157 (defcustom elpher-buttonify-urls-in-directories nil
158 "If non-nil, turns URLs matched in directories into clickable buttons."
161 (defcustom elpher-use-header t
162 "If non-nil, display current node information in buffer header."
165 (defcustom elpher-auto-disengage-TLS nil
166 "If non-nil, automatically disengage TLS following an unsuccessful connection.
167 While enabling this may seem convenient, it is also potentially dangerous as it
168 allows switching from an encrypted channel back to plain text without user input."
177 ;; An elpher "address" object is either a url object or a symbol.
178 ;; Symbol addresses are "special", corresponding to pages generated
179 ;; dynamically for and by elpher. All others represent pages which
180 ;; rely on content retrieved over the network.
182 (defun elpher-address-from-url (url-string)
183 "Create a ADDRESS object corresponding to the given URL-STRING."
184 (let ((data (match-data))) ; Prevent parsing clobbering match data
186 (let ((url (url-generic-parse-url url-string)))
187 (setf (url-fullness url) t)
188 (unless (url-host url)
189 (setf (url-host url) (url-filename url))
190 (setf (url-filename url) ""))
191 (unless (url-type url)
192 (setf (url-type url) "gopher"))
193 (if (and (url-type url)
195 (let ((is-gopher (or (equal "gopher" (url-type url))
196 (equal "gophers" (url-type url)))))
197 (setf (url-filename url)
198 (url-unhex-string (url-filename url)))
199 (when (or (equal (url-filename url) "")
200 (equal (url-filename url) "/"))
202 (setf (url-filename url) "/1")))
203 (unless (> (url-port url) 0)
205 (setf (url-port url) 70)))
207 (error "Malformed URL" url)))
208 (set-match-data data))))
210 (defun elpher-make-gopher-address (type selector host port &optional tls)
211 "Create an ADDRESS object corresponding to the given gopher directory record
212 attributes: TYPE, SELECTOR, HOST and PORT."
213 (if (and (equal type ?h)
214 (string-prefix-p "URL:" selector))
215 (elpher-address-from-url (elt (split-string selector "URL:") 1))
216 (elpher-address-from-url
217 (concat "gopher" (if tls "s" "")
219 ":" (number-to-string port)
223 (defun elpher-make-special-address (type)
224 "Create an ADDRESS object corresponding to the given special page symbol TYPE."
227 (defun elpher-address-to-url (address)
228 "Get string representation of ADDRESS, or nil if ADDRESS is special."
229 (if (not (elpher-address-special-p address))
230 (url-encode-url (url-recreate-url address))
233 (defun elpher-address-type (address)
234 "Retrieve selector type from ADDRESS object."
235 (if (symbolp address)
236 (list 'special address)
237 (let ((protocol (url-type address)))
238 (cond ((or (equal protocol "gopher")
239 (equal protocol "gophers"))
240 (list 'gopher (string-to-char (substring (url-filename address) 1))))
241 ((equal protocol "gemini")
245 (defun elpher-address-protocol (address)
246 (if (symbolp address)
250 (defun elpher-address-filename (address)
251 (if (symbolp address)
253 (url-filename address)))
255 (defun elpher-address-host (address)
256 "Retrieve host from ADDRESS object."
259 (defun elpher-address-port (address)
260 "Retrieve port from ADDRESS object."
263 (defun elpher-address-special-p (address)
264 "Return non-nil if ADDRESS object is special (e.g. start page, bookmarks page)."
267 (defun elpher-address-gopher-p (address)
268 "Return non-nill if ADDRESS object is a gopher address."
269 (memq (elpher-address-protocol address) '("gopher gophers")))
271 (defun elpher-gopher-address-selector (address)
272 "Retrieve gopher selector from ADDRESS object."
273 (substring (url-filename address) 2))
277 (defun elpher-make-node (display-string address &optional parent)
278 "Create a node in the page hierarchy.
280 DISPLAY-STRING records the display string used for the page.
282 ADDRESS specifies the address object of the page.
284 The optional PARENT specifies the parent node in the hierarchy.
285 This is set every time the node is visited, so while it forms
286 an important part of the node data there is no need to set it
288 (list display-string address parent))
290 (defun elpher-node-display-string (node)
291 "Retrieve the display string of NODE."
294 (defun elpher-node-address (node)
295 "Retrieve the ADDRESS object of NODE."
298 (defun elpher-node-parent (node)
299 "Retrieve the parent node of NODE."
302 (defun elpher-set-node-parent (node parent)
303 "Set the parent node of NODE to be PARENT."
304 (setcar (cdr (cdr node)) parent))
308 (defvar elpher-content-cache (make-hash-table :test 'equal))
309 (defvar elpher-pos-cache (make-hash-table :test 'equal))
311 (defun elpher-get-cached-content (address)
312 "Retrieve the cached content for ADDRESS, or nil if none exists."
313 (gethash address elpher-content-cache))
315 (defun elpher-cache-content (address content)
316 "Set the content cache for ADDRESS to CONTENT."
317 (puthash address content elpher-content-cache))
319 (defun elpher-get-cached-pos (address)
320 "Retrieve the cached cursor position for ADDRESS, or nil if none exists."
321 (gethash address elpher-pos-cache))
323 (defun elpher-cache-pos (address pos)
324 "Set the cursor position cache for ADDRESS to POS."
325 (puthash address pos elpher-pos-cache))
327 ;; Node graph traversal
329 (defvar elpher-current-node nil)
331 (defun elpher-visit-node (node &optional getter preserve-parent)
332 "Visit NODE using its own getter or GETTER, if non-nil.
333 Additionally, set the parent of NODE to `elpher-current-node',
334 unless PRESERVE-PARENT is non-nil."
336 (elpher-process-cleanup)
337 (unless preserve-parent
338 (if (and (elpher-node-parent elpher-current-node)
339 (equal (elpher-node-address elpher-current-node)
340 (elpher-node-address node)))
341 (elpher-set-node-parent node (elpher-node-parent elpher-current-node))
342 (elpher-set-node-parent node elpher-current-node)))
343 (setq elpher-current-node node)
346 (let* ((address (elpher-node-address node))
347 (type (elpher-address-type address))
348 (type-record (cdr (assoc type elpher-type-map))))
350 (funcall (car type-record))
351 (elpher-visit-parent-node)
353 (`(gopher ,type-char)
354 (error "Unsupported gopher selector type '%c' for '%s'"
355 type-char (elpher-address-to-url address)))
357 (error "Unsupported address type '%S' for '%s'"
358 type (elpher-address-to-url address))))))))
360 (defun elpher-visit-parent-node ()
361 "Visit the parent of the current node."
362 (let ((parent-node (elpher-node-parent elpher-current-node)))
364 (elpher-visit-node parent-node nil t))))
366 (defun elpher-reload-current-node ()
367 "Reload the current node, discarding any existing cached content."
368 (elpher-cache-content (elpher-node-address elpher-current-node) nil)
369 (elpher-visit-node elpher-current-node))
371 (defun elpher-save-pos ()
372 "Save the current position of point to the current node."
373 (when elpher-current-node
374 (elpher-cache-pos (elpher-node-address elpher-current-node) (point))))
376 (defun elpher-restore-pos ()
377 "Restore the position of point to that cached in the current node."
378 (let ((pos (elpher-get-cached-pos (elpher-node-address elpher-current-node))))
381 (goto-char (point-min)))))
384 ;;; Buffer preparation
387 (defun elpher-update-header ()
388 "If `elpher-use-header' is true, display current node info in window header."
389 (if elpher-use-header
390 (setq header-line-format (elpher-node-display-string elpher-current-node))))
392 (defmacro elpher-with-clean-buffer (&rest args)
393 "Evaluate ARGS with a clean *elpher* buffer as current."
394 (list 'with-current-buffer "*elpher*"
396 (append (list 'let '((inhibit-read-only t))
398 '(elpher-update-header))
405 (defvar elpher-user-coding-system nil
406 "User-specified coding system to use for decoding text responses.")
408 (defun elpher-decode (string)
409 "Decode STRING using autodetected or user-specified coding system."
410 (decode-coding-string string
411 (if elpher-user-coding-system
412 elpher-user-coding-system
413 (detect-coding-string string t))))
415 (defun elpher-preprocess-text-response (string)
416 "Preprocess text selector response contained in STRING.
417 This involes decoding the character representation, and clearing
418 away CRs and any terminating period."
419 (elpher-decode (replace-regexp-in-string "\n\.\n$" "\n"
420 (replace-regexp-in-string "\r" "" string))))
426 (defun elpher-insert-index (string)
427 "Insert the index corresponding to STRING into the current buffer."
428 ;; Should be able to split directly on CRLF, but some non-conformant
429 ;; LF-only servers sadly exist, hence the following.
430 (let ((str-processed (elpher-preprocess-text-response string)))
431 (dolist (line (split-string str-processed "\n"))
432 (unless (= (length line) 0)
433 (let* ((type (elt line 0))
434 (fields (split-string (substring line 1) "\t"))
435 (display-string (elt fields 0))
436 (selector (elt fields 1))
437 (host (elt fields 2))
438 (port (if (elt fields 3)
439 (string-to-number (elt fields 3))
441 (address (elpher-make-gopher-address type selector host port)))
442 (elpher-insert-index-record display-string address))))))
444 (defun elpher-insert-margin (&optional type-name)
445 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
448 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
450 (propertize "[" 'face 'elpher-margin-brackets)
451 (propertize type-name 'face 'elpher-margin-key)
452 (propertize "]" 'face 'elpher-margin-brackets))))
454 (insert (make-string elpher-margin-width ?\s))))
456 (defun elpher-node-button-help (node)
457 "Return a string containing the help text for a button corresponding to NODE."
458 (let ((address (elpher-node-address node)))
459 (format "mouse-1, RET: open '%s'" (elpher-address-to-url address))))
461 (defun elpher-insert-index-record (display-string address)
462 "Function to insert an index record into the current buffer.
463 The contents of the record are dictated by DISPLAY-STRING and ADDRESS."
464 (let* ((type (elpher-address-type address))
465 (type-map-entry (cdr (assoc type elpher-type-map))))
467 (let* ((margin-code (elt type-map-entry 1))
468 (face (elt type-map-entry 2))
469 (node (elpher-make-node display-string address)))
470 (elpher-insert-margin margin-code)
471 (insert-text-button display-string
474 'action #'elpher-click-link
476 'help-echo (elpher-node-button-help node)))
478 ('(gopher ?i) ;; Information
479 (elpher-insert-margin)
481 (if elpher-buttonify-urls-in-directories
482 (elpher-buttonify-urls display-string)
484 'face 'elpher-info)))
485 (`(gopher ,selector-type) ;; Unknown
486 (elpher-insert-margin (concat (char-to-string selector-type) "?"))
487 (insert (propertize display-string
488 'face 'elpher-unknown)))))
491 (defun elpher-click-link (button)
492 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
493 (let ((node (button-get button 'elpher-node)))
494 (elpher-visit-node node)))
497 ;;; Gopher selector retrieval (all kinds)
500 (defun elpher-process-cleanup ()
501 "Immediately shut down any extant elpher process."
502 (let ((p (get-process "elpher-process")))
503 (if p (delete-process p))))
505 (defvar elpher-use-tls nil
506 "If non-nil, use TLS to communicate with gopher servers.")
508 (defvar elpher-selector-string)
510 (defun elpher-get-selector (address after &optional propagate-error)
511 "Retrieve selector specified by ADDRESS, then execute AFTER.
512 The result is stored as a string in the variable ‘elpher-selector-string’.
514 Usually errors result in an error page being displayed. This is only
515 appropriate if the selector is to be directly viewed. If PROPAGATE-ERROR
516 is non-nil, this message is not displayed. Instead, the error propagates
517 up to the calling function."
518 (setq elpher-selector-string "")
519 (when (equal (elpher-address-protocol address) "gophers")
520 (if (gnutls-available-p)
521 (when (not elpher-use-tls)
522 (setq elpher-use-tls t)
523 (message "Engaging TLS mode."))
524 (error "Cannot retrieve TLS selector: GnuTLS not available")))
525 (condition-case the-error
526 (let* ((kill-buffer-query-functions nil)
527 (proc (open-network-stream "elpher-process"
529 (elpher-address-host address)
530 (elpher-address-port address)
531 :type (if elpher-use-tls 'tls 'plain))))
532 (set-process-coding-system proc 'binary)
533 (set-process-filter proc
534 (lambda (proc string)
535 (setq elpher-selector-string
536 (concat elpher-selector-string string))))
537 (set-process-sentinel proc after)
538 (process-send-string proc
539 (concat (elpher-gopher-address-selector address) "\n")))
541 (if (and (consp the-error)
542 (eq (car the-error) 'gnutls-error)
543 (not (equal (elpher-address-protocol address) "gophers"))
544 (or elpher-auto-disengage-TLS
545 (yes-or-no-p "Could not establish encrypted connection. Disable TLS mode? ")))
547 (message "Disengaging TLS mode.")
548 (setq elpher-use-tls nil)
549 (elpher-get-selector address after))
550 (elpher-process-cleanup)
553 (elpher-with-clean-buffer
554 (insert (propertize "\n---- ERROR -----\n\n" 'face 'error)
555 "Failed to connect to " (elpher-address-to-url address) ".\n"
556 (propertize "\n----------------\n\n" 'face 'error)
557 "Press 'u' to return to the previous page.")))))))
561 (defun elpher-get-index-node ()
562 "Getter which retrieves the current node contents as an index."
563 (let* ((address (elpher-node-address elpher-current-node))
564 (content (elpher-get-cached-content address)))
567 (elpher-with-clean-buffer
569 (elpher-restore-pos)))
570 (elpher-with-clean-buffer
571 (insert "LOADING DIRECTORY... (use 'u' to cancel)"))
572 (elpher-get-selector address
574 (unless (string-prefix-p "deleted" event)
575 (elpher-with-clean-buffer
576 (elpher-insert-index elpher-selector-string)
578 (elpher-cache-content
579 (elpher-node-address elpher-current-node)
580 (buffer-string)))))))))
584 (defconst elpher-url-regex
585 "\\([a-zA-Z]+\\)://\\([a-zA-Z0-9.\-]+\\|\[[a-zA-Z0-9:]+\]\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
586 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
588 (defun elpher-buttonify-urls (string)
589 "Turn substrings which look like urls in STRING into clickable buttons."
592 (goto-char (point-min))
593 (while (re-search-forward elpher-url-regex nil t)
594 (let ((node (elpher-make-node (match-string 0)
595 (elpher-address-from-url (match-string 0)))))
596 (make-text-button (match-beginning 0)
599 'action #'elpher-click-link
601 'help-echo (elpher-node-button-help node))))
604 (defun elpher-get-text-node ()
605 "Getter which retrieves the current node contents as a text document."
606 (let* ((address (elpher-node-address elpher-current-node))
607 (content (elpher-get-cached-content address)))
610 (elpher-with-clean-buffer
612 (elpher-restore-pos)))
614 (elpher-with-clean-buffer
615 (insert "LOADING TEXT... (use 'u' to cancel)"))
616 (elpher-get-selector address
618 (unless (string-prefix-p "deleted" event)
619 (elpher-with-clean-buffer
620 (insert (elpher-buttonify-urls
621 (elpher-preprocess-text-response
622 elpher-selector-string)))
624 (elpher-cache-content
625 (elpher-node-address elpher-current-node)
626 (buffer-string))))))))))
630 (defun elpher-get-image-node ()
631 "Getter which retrieves the current node contents as an image to view."
632 (let* ((address (elpher-node-address elpher-current-node)))
633 (if (display-images-p)
635 (elpher-with-clean-buffer
636 (insert "LOADING IMAGE... (use 'u' to cancel)"))
637 (elpher-get-selector address
639 (unless (string-prefix-p "deleted" event)
640 (let ((image (create-image
641 elpher-selector-string
643 (elpher-with-clean-buffer
645 (elpher-restore-pos)))))))
646 (elpher-get-node-download))))
650 (defun elpher-get-search-node ()
651 "Getter which submits a search query to the address of the current node."
652 (let* ((address (elpher-node-address elpher-current-node))
653 (content (elpher-get-cached-content address))
657 (elpher-with-clean-buffer
659 (elpher-restore-pos))
660 (message "Displaying cached search results. Reload to perform a new search."))
662 (let* ((query-string (read-string "Query: "))
663 (query-selector (concat (elpher-gopher-address-selector address) "\t" query-string))
664 (search-address (elpher-make-gopher-address ?1
666 (elpher-address-host address)
667 (elpher-address-port address))))
669 (elpher-with-clean-buffer
670 (insert "LOADING RESULTS... (use 'u' to cancel)"))
671 (elpher-get-selector search-address
673 (unless (string-prefix-p "deleted" event)
674 (elpher-with-clean-buffer
675 (elpher-insert-index elpher-selector-string))
676 (goto-char (point-min))
677 (elpher-cache-content
678 (elpher-node-address elpher-current-node)
681 (elpher-visit-parent-node))))))
683 ;; Raw server response retrieval
685 (defun elpher-get-node-raw ()
686 "Getter which retrieves the raw server response for the current node."
687 (let ((address (elpher-node-address elpher-current-node)))
688 (elpher-with-clean-buffer
689 (insert "LOADING RAW SERVER RESPONSE... (use 'u' to cancel)"))
690 (elpher-get-selector address
692 (unless (string-prefix-p "deleted" event)
693 (elpher-with-clean-buffer
694 (insert elpher-selector-string)
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-gopher-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))))))
727 ;; HTML node retrieval
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-html-node ()
737 "Getter which retrieves and renders an HTML node."
738 (let* ((address (elpher-node-address elpher-current-node))
739 (selector (elpher-gopher-address-selector address)))
740 (let ((content (elpher-get-cached-content address)))
743 (elpher-with-clean-buffer
745 (elpher-restore-pos)))
746 (elpher-with-clean-buffer
747 (insert "LOADING HTML... (use 'u' to cancel)"))
748 (elpher-get-selector address
750 (unless (string-prefix-p "deleted" event)
751 (elpher-with-clean-buffer
752 (elpher-insert-rendered-html elpher-selector-string)
753 (goto-char (point-min))
754 (elpher-cache-content
755 (elpher-node-address elpher-current-node)
756 (buffer-string))))))))))
759 ;; Other URL node opening
761 (defun elpher-get-other-url-node ()
762 "Getter which attempts to open the URL specified by the current node."
763 (let* ((address (elpher-node-address elpher-current-node))
764 (url (elpher-address-to-url address)))
766 (elpher-visit-parent-node) ; Do first in case of non-local exits.
767 (message "Opening URL...")
768 (if elpher-open-urls-with-eww
772 ;; Telnet node connection
774 (defun elpher-get-telnet-node ()
775 "Getter which opens a telnet connection to the server specified by the current node."
776 (let* ((address (elpher-node-address elpher-current-node))
777 (host (elpher-address-host address))
778 (port (elpher-address-port address)))
779 (elpher-visit-parent-node)
782 ;; Start page node retrieval
784 (defun elpher-get-start-node ()
785 "Getter which displays the start page."
786 (elpher-with-clean-buffer
787 (insert " --------------------------------------------\n"
788 " Elpher Gopher Client \n"
789 " version " elpher-version "\n"
790 " --------------------------------------------\n"
792 "Default bindings:\n"
794 " - TAB/Shift-TAB: next/prev item on current page\n"
795 " - RET/mouse-1: open item under cursor\n"
796 " - m: select an item on current page by name (autocompletes)\n"
797 " - u: return to previous page\n"
798 " - o/O: visit different selector or the root menu of the current server\n"
799 " - g: go to a particular gopher address\n"
800 " - i/I: info on item under cursor or current page\n"
801 " - c/C: copy URL representation of item under cursor or current page\n"
802 " - a/A: bookmark the item under cursor or current page\n"
803 " - x/X: remove bookmark for item under cursor or current page\n"
804 " - B: visit the bookmarks page\n"
805 " - r: redraw current page (using cached contents if available)\n"
806 " - R: reload current page (regenerates cache)\n"
807 " - T: toggle TLS mode\n"
808 " - d/D: download item under cursor or current page\n"
809 " - .: display the raw server response for the current page\n"
810 " - S: set an explicit character coding system (default is to autodetect)\n"
812 "Start your exploration of gopher space:\n")
813 (elpher-insert-index-record "Floodgap Systems Gopher Server"
814 (elpher-make-gopher-address ?1 "" "gopher.floodgap.com" 70))
816 "Alternatively, select the following item and enter some search terms:\n")
817 (elpher-insert-index-record "Veronica-2 Gopher Search Engine"
818 (elpher-make-gopher-address ?7 "/v2/vs" "gopher.floodgap.com" 70))
821 (let ((help-string "RET,mouse-1: Open Elpher info manual (if available)"))
822 (insert-text-button "Elpher info manual"
824 'action (lambda (button)
828 'help-echo help-string))
829 (insert " for the full documentation. **\n")
831 (concat " (This should be available if you have installed Elpher using\n"
832 " MELPA. Otherwise you will have to install the manual yourself.)")
834 (elpher-restore-pos)))
836 ;; Bookmarks page node retrieval
838 (defun elpher-get-bookmarks-node ()
839 "Getter to load and display the current bookmark list."
840 (elpher-with-clean-buffer
841 (insert "---- Bookmark list ----\n\n")
842 (let ((bookmarks (elpher-load-bookmarks)))
844 (dolist (bookmark bookmarks)
845 (let ((display-string (elpher-bookmark-display-string bookmark))
846 (address (elpher-address-from-url (elpher-bookmark-url bookmark))))
847 (elpher-insert-index-record display-string address)))
848 (insert "No bookmarks found.\n")))
849 (insert "\n-----------------------\n\n"
850 "- u: return to previous page\n"
851 "- x: delete selected bookmark\n"
852 "- a: rename selected bookmark\n\n"
853 "Bookmarks are stored in the file "
854 (locate-user-emacs-file "elpher-bookmarks"))
855 (elpher-restore-pos)))
861 (defun elpher-make-bookmark (display-string url)
862 "Make an elpher bookmark.
863 DISPLAY-STRING determines how the bookmark will appear in the
864 bookmark list, while ADDRESS is the address of the entry."
865 (list display-string (elpher-address-to-url address)))
867 (defun elpher-bookmark-display-string (bookmark)
868 "Get the display string of BOOKMARK."
871 (defun elpher-set-bookmark-display-string (bookmark display-string)
872 "Set the display string of BOOKMARK to DISPLAY-STRING."
873 (setcar bookmark display-string))
875 (defun elpher-bookmark-url (bookmark)
876 "Get the address for BOOKMARK."
880 (defun elpher-save-bookmarks (bookmarks)
881 "Record the bookmark list BOOKMARKS to the user's bookmark file.
882 Beware that this completely replaces the existing contents of the file."
883 (with-temp-file (locate-user-emacs-file "elpher2-bookmarks")
885 (insert "; Elpher bookmarks file\n\n"
886 "; Bookmarks are stored as a list of (label URL) items.\n"
887 "; Feel free to edit by hand, but take care to ensure\n"
888 "; the list structure remains intact.\n\n")
889 (pp bookmarks (current-buffer))))
891 (defun elpher-load-bookmarks ()
892 "Get the list of bookmarks from the users's bookmark file."
895 (insert-file-contents (locate-user-emacs-file "elpher2-bookmarks"))
896 (goto-char (point-min))
897 (read (current-buffer)))))
899 (defun elpher-add-address-bookmark (address display-string)
900 "Save a bookmark for ADDRESS with label DISPLAY-STRING.)))
901 If ADDRESS is already bookmarked, update the label only."
902 (let ((bookmarks (elpher-load-bookmarks))
903 (url (elpher-address-to-url address)))
904 (let ((existing-bookmark (rassoc (list url) bookmarks)))
905 (if existing-bookmark
906 (elpher-set-bookmark-display-string existing-bookmark display-string)
907 (add-to-list 'bookmarks (elpher-make-bookmark display-string url))))
908 (elpher-save-bookmarks bookmarks)))
910 (defun elpher-remove-address-bookmark (address)
911 "Remove any bookmark to ADDRESS."
912 (let ((url (elpher-address-to-url address)))
913 (elpher-save-bookmarks
914 (seq-filter (lambda (bookmark)
915 (not (equal (elpher-bookmark-url bookmark) url)))
916 (elpher-load-bookmarks)))))
918 ;;; Interactive procedures
921 (defun elpher-next-link ()
922 "Move point to the next link on the current page."
926 (defun elpher-prev-link ()
927 "Move point to the previous link on the current page."
931 (defun elpher-follow-current-link ()
932 "Open the link or url at point."
937 "Go to a particular gopher site read from the minibuffer.
938 The site may be specified via a URL or explicitly in terms of
939 host, selector and port."
942 (let ((host-or-url (read-string "Gopher host or URL: ")))
943 (elpher-make-node host-or-url
944 (elpher-address-from-url host-or-url)))))
945 (switch-to-buffer "*elpher*")
946 (elpher-visit-node node)))
948 (defun elpher-go-current ()
949 "Go to a particular site read from the minibuffer, initialized with the current URL."
951 (let ((address (elpher-node-address elpher-current-node)))
952 (if (elpher-address-special-p address)
953 (error "Command not valid for this page")
954 (let ((url (read-string "URL: " (elpher-address-to-url address))))
955 (elpher-visit-node (elpher-make-node url (elpher-address-from-url url)))))))
957 (defun elpher-redraw ()
958 "Redraw current page."
960 (if elpher-current-node
961 (elpher-visit-node elpher-current-node)
962 (message "No current site.")))
964 (defun elpher-reload ()
965 "Reload current page."
967 (if elpher-current-node
968 (elpher-reload-current-node)
969 (message "No current site.")))
971 (defun elpher-toggle-tls ()
972 "Toggle TLS encryption mode."
974 (setq elpher-use-tls (not elpher-use-tls))
976 (if (gnutls-available-p)
977 (message "TLS mode enabled. (Will not affect current page until reload.)")
978 (setq elpher-use-tls nil)
979 (error "Cannot enable TLS mode: GnuTLS not available"))
980 (message "TLS mode disabled. (Will not affect current page until reload.)")))
982 (defun elpher-view-raw ()
983 "View raw server response for current page."
985 (if elpher-current-node
986 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
987 (error "This page was not generated by a server")
988 (elpher-visit-node elpher-current-node
989 #'elpher-get-node-raw))
990 (message "No current site.")))
992 (defun elpher-back ()
993 "Go to previous site."
995 (if (elpher-node-parent elpher-current-node)
996 (elpher-visit-parent-node)
997 (error "No previous site")))
999 (defun elpher-download ()
1000 "Download the link at point."
1002 (let ((button (button-at (point))))
1004 (let ((node (button-get button 'elpher-node)))
1005 (if (elpher-address-special-p (elpher-node-address node))
1006 (error "Cannot download this link")
1007 (elpher-visit-node (button-get button 'elpher-node)
1008 #'elpher-get-node-download)))
1009 (error "No link selected"))))
1011 (defun elpher-download-current ()
1012 "Download the current page."
1014 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
1015 (error "Cannot download this page")
1016 (elpher-visit-node (elpher-make-node
1017 (elpher-node-display-string elpher-current-node)
1018 (elpher-node-address elpher-current-node)
1019 elpher-current-node)
1020 #'elpher-get-node-download
1023 (defun elpher-build-link-map ()
1024 "Build alist mapping link names to destination nodes in current buffer."
1025 (let ((link-map nil)
1026 (b (next-button (point-min) t)))
1028 (add-to-list 'link-map (cons (button-label b) b))
1029 (setq b (next-button (button-start b))))
1032 (defun elpher-jump ()
1033 "Select a directory entry by name. Similar to the info browser (m)enu command."
1035 (let* ((link-map (elpher-build-link-map)))
1037 (let ((key (let ((completion-ignore-case t))
1038 (completing-read "Directory item/link: "
1040 (if (and key (> (length key) 0))
1041 (let ((b (cdr (assoc key link-map))))
1042 (goto-char (button-start b))
1043 (button-activate b)))))))
1045 (defun elpher-root-dir ()
1046 "Visit root of current server."
1048 (let* ((address (elpher-node-address elpher-current-node))
1049 (host (elpher-address-host address)))
1051 (let ((host (elpher-address-host address))
1052 (selector (elpher-gopher-address-selector address))
1053 (port (elpher-address-port address)))
1054 (if (> (length selector) 0)
1055 (let ((root-address (elpher-make-gopher-address ?1 "" host port)))
1057 (elpher-make-node (elpher-address-to-url root-address))))
1058 (error "Already at root directory of current server")))
1059 (error "Command invalid for this page"))))
1061 (defun elpher-bookmarks-current-p ()
1062 "Return non-nil if current node is a bookmarks page."
1063 (equal (elpher-address-type (elpher-node-address elpher-current-node))
1064 '(special bookmarks)))
1066 (defun elpher-reload-bookmarks ()
1067 "Reload bookmarks if current node is a bookmarks page."
1068 (if (elpher-bookmarks-current-p)
1069 (elpher-reload-current-node)))
1071 (defun elpher-bookmark-current ()
1072 "Bookmark the current node."
1074 (let ((address (elpher-node-address elpher-current-node))
1075 (display-string (elpher-node-display-string elpher-current-node)))
1076 (if (not (elpher-address-special-p address))
1077 (let ((bookmark-display-string (read-string "Bookmark display string: "
1079 (elpher-add-address-bookmark address bookmark-display-string)
1080 (message "Bookmark added."))
1081 (error "Cannot bookmark %s" display-string))))
1083 (defun elpher-bookmark-link ()
1084 "Bookmark the link at point."
1086 (let ((button (button-at (point))))
1088 (let* ((node (button-get button 'elpher-node))
1089 (address (elpher-node-address node))
1090 (display-string (elpher-node-display-string node)))
1091 (if (not (elpher-address-special-p address))
1092 (let ((bookmark-display-string (read-string "Bookmark display string: "
1094 (elpher-add-address-bookmark address bookmark-display-string)
1095 (elpher-reload-bookmarks)
1096 (message "Bookmark added."))
1097 (error "Cannot bookmark %s" display-string)))
1098 (error "No link selected"))))
1100 (defun elpher-unbookmark-current ()
1101 "Remove bookmark for the current node."
1103 (let ((address (elpher-node-address elpher-current-node)))
1104 (unless (elpher-address-special-p address)
1105 (elpher-remove-address-bookmark address)
1106 (message "Bookmark removed."))))
1108 (defun elpher-unbookmark-link ()
1109 "Remove bookmark for the link at point."
1111 (let ((button (button-at (point))))
1113 (let ((node (button-get button 'elpher-node)))
1114 (elpher-remove-address-bookmark (elpher-node-address node))
1115 (elpher-reload-bookmarks)
1116 (message "Bookmark removed."))
1117 (error "No link selected"))))
1119 (defun elpher-bookmarks ()
1120 "Visit bookmarks page."
1122 (switch-to-buffer "*elpher*")
1124 (elpher-make-node "Bookmarks Page" (elpher-make-special-address 'bookmarks))))
1126 (defun elpher-info-node (node)
1127 "Display information on NODE."
1128 (let ((display-string (elpher-node-display-string node))
1129 (address (elpher-node-address node)))
1130 (if (elpher-address-special-p address)
1131 (message "Special page: %s" display-string)
1132 (message (elpher-address-to-url address)))))
1134 (defun elpher-info-link ()
1135 "Display information on node corresponding to link at point."
1137 (let ((button (button-at (point))))
1139 (elpher-info-node (button-get button 'elpher-node))
1140 (error "No item selected"))))
1142 (defun elpher-info-current ()
1143 "Display information on current node."
1145 (elpher-info-node elpher-current-node))
1147 (defun elpher-copy-node-url (node)
1148 "Copy URL representation of address of NODE to `kill-ring'."
1149 (let ((address (elpher-node-address node)))
1150 (if (elpher-address-special-p address)
1151 (error (format "Cannot represent %s as URL" (elpher-node-display-string node)))
1152 (let ((url (elpher-address-to-url address)))
1153 (message "Copied \"%s\" to kill-ring/clipboard." url)
1156 (defun elpher-copy-link-url ()
1157 "Copy URL of item at point to `kill-ring'."
1159 (let ((button (button-at (point))))
1161 (elpher-copy-node-url (button-get button 'elpher-node))
1162 (error "No item selected"))))
1164 (defun elpher-copy-current-url ()
1165 "Copy URL of current node to `kill-ring'."
1167 (elpher-copy-node-url elpher-current-node))
1169 (defun elpher-set-coding-system ()
1170 "Specify an explicit character coding system."
1172 (let ((system (read-coding-system "Set coding system to use (default is to autodetect): " nil)))
1173 (setq elpher-user-coding-system system)
1175 (message "Coding system fixed to %s. (Reload to see effect)." system)
1176 (message "Coding system set to autodetect. (Reload to see effect)."))))
1182 (defvar elpher-mode-map
1183 (let ((map (make-sparse-keymap)))
1184 (define-key map (kbd "TAB") 'elpher-next-link)
1185 (define-key map (kbd "<backtab>") 'elpher-prev-link)
1186 (define-key map (kbd "u") 'elpher-back)
1187 (define-key map (kbd "O") 'elpher-root-dir)
1188 (define-key map (kbd "g") 'elpher-go)
1189 (define-key map (kbd "o") 'elpher-go-current)
1190 (define-key map (kbd "r") 'elpher-redraw)
1191 (define-key map (kbd "R") 'elpher-reload)
1192 (define-key map (kbd "T") 'elpher-toggle-tls)
1193 (define-key map (kbd ".") 'elpher-view-raw)
1194 (define-key map (kbd "d") 'elpher-download)
1195 (define-key map (kbd "D") 'elpher-download-current)
1196 (define-key map (kbd "m") 'elpher-jump)
1197 (define-key map (kbd "i") 'elpher-info-link)
1198 (define-key map (kbd "I") 'elpher-info-current)
1199 (define-key map (kbd "c") 'elpher-copy-link-url)
1200 (define-key map (kbd "C") 'elpher-copy-current-url)
1201 (define-key map (kbd "a") 'elpher-bookmark-link)
1202 (define-key map (kbd "A") 'elpher-bookmark-current)
1203 (define-key map (kbd "x") 'elpher-unbookmark-link)
1204 (define-key map (kbd "X") 'elpher-unbookmark-current)
1205 (define-key map (kbd "B") 'elpher-bookmarks)
1206 (define-key map (kbd "S") 'elpher-set-coding-system)
1207 (when (fboundp 'evil-define-key)
1208 (evil-define-key 'motion map
1209 (kbd "TAB") 'elpher-next-link
1210 (kbd "C-") 'elpher-follow-current-link
1211 (kbd "C-t") 'elpher-back
1212 (kbd "u") 'elpher-back
1213 (kbd "O") 'elpher-root-dir
1214 (kbd "g") 'elpher-go
1215 (kbd "o") 'elpher-go-current
1216 (kbd "r") 'elpher-redraw
1217 (kbd "R") 'elpher-reload
1218 (kbd "T") 'elpher-toggle-tls
1219 (kbd ".") 'elpher-view-raw
1220 (kbd "d") 'elpher-download
1221 (kbd "D") 'elpher-download-current
1222 (kbd "m") 'elpher-jump
1223 (kbd "i") 'elpher-info-link
1224 (kbd "I") 'elpher-info-current
1225 (kbd "c") 'elpher-copy-link-url
1226 (kbd "C") 'elpher-copy-current-url
1227 (kbd "a") 'elpher-bookmark-link
1228 (kbd "A") 'elpher-bookmark-current
1229 (kbd "x") 'elpher-unbookmark-link
1230 (kbd "X") 'elpher-unbookmark-current
1231 (kbd "B") 'elpher-bookmarks
1232 (kbd "S") 'elpher-set-coding-system))
1234 "Keymap for gopher client.")
1236 (define-derived-mode elpher-mode special-mode "elpher"
1237 "Major mode for elpher, an elisp gopher client.
1239 This mode is automatically enabled by the interactive
1240 functions which initialize the gopher client, namely
1241 `elpher', `elpher-go' and `elpher-bookmarks'.")
1243 (when (fboundp 'evil-set-initial-state)
1244 (evil-set-initial-state 'elpher-mode 'motion))
1247 ;;; Main start procedure
1252 "Start elpher with default landing page."
1254 (if (get-buffer "*elpher*")
1255 (switch-to-buffer "*elpher*")
1256 (switch-to-buffer "*elpher*")
1257 (setq elpher-current-node nil)
1258 (let ((start-node (elpher-make-node "Elpher Start Page"
1259 (elpher-make-special-address 'start))))
1260 (elpher-visit-node start-node)))
1261 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
1263 ;;; elpher.el ends here