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.
40 ;; Visited pages are stored as a hierarchy rather than a linear history,
41 ;; meaning that navigation between these pages is quick and easy.
43 ;; To launch Elpher, simply use 'M-x elpher'. This will open a start
44 ;; page containing information on key bindings and suggested starting
45 ;; points for your gopher exploration.
47 ;; Faces, caching and other options can be configured via
48 ;; the Elpher customization group in Applications.
60 (defconst elpher-version "1.3.0"
61 "Current version of elpher.")
63 (defconst elpher-margin-width 6
64 "Width of left-hand margin used when rendering indicies.")
66 (defconst elpher-start-index
69 (list "i\tfake\tfake\t1"
70 "i --------------------------------------------\tfake\tfake\t1"
71 "i Elpher Gopher Client \tfake\tfake\t1"
72 (format "i version %s\tfake\tfake\t1" elpher-version)
73 "i --------------------------------------------\tfake\tfake\t1"
75 "iUsage:\tfake\tfake\t1"
77 "i - tab/shift-tab: next/prev item on current page\tfake\tfake\t1"
78 "i - RET/mouse-1: open item under cursor\tfake\tfake\t1"
79 "i - m: select an item on current page by name (autocompletes)\tfake\tfake\t1"
80 "i - u: return to parent\tfake\tfake\t1"
81 "i - O: visit the root menu of the current server\tfake\tfake\t1"
82 "i - g: go to a particular gopher address\tfake\tfake\t1"
83 "i - i/I: info on item under cursor or current page\tfake\tfake\t1"
84 "i - c/C: copy URL representation of item under cursor or current page\tfake\tfake\t1"
85 "i - a/A: bookmark the item under cursor or current page\tfake\tfake\t1"
86 "i - x/X: remove bookmark for item under cursor or current page\tfake\tfake\t1"
87 "i - B: visit the bookmarks page\tfake\tfake\t1"
88 "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
89 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
90 "i - d: download directory entry under cursor\tfake\tfake\t1"
91 "i - w: display the raw server response for the current page\tfake\tfake\t1"
92 "i - S: set an explicit character coding system (default is to autodetect)\tfake\tfake\t1"
94 "iWhere to start exploring Gopherspace:\tfake\tfake\t1"
96 "1Floodgap Systems Gopher Server\t/\tgopher.floodgap.com\t70"
98 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
99 "isearch terms:\tfake\tfake\t1"
101 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
104 "Source for elpher start page.")
106 (defconst elpher-type-map
107 '((?0 elpher-get-text-node "txt" elpher-text)
108 (?1 elpher-get-index-node "/" elpher-index)
109 (?4 elpher-get-node-download "bin" elpher-binary)
110 (?5 elpher-get-node-download "bin" elpher-binary)
111 (?7 elpher-get-search-node "?" elpher-search)
112 (?8 elpher-get-telnet-node "tel" elpher-telnet)
113 (?9 elpher-get-node-download "bin" elpher-binary)
114 (?g elpher-get-image-node "img" elpher-image)
115 (?p elpher-get-image-node "img" elpher-image)
116 (?I elpher-get-image-node "img" elpher-image)
117 (?d elpher-get-node-download "doc" elpher-binary)
118 (?h elpher-get-url-node "web" elpher-url)
119 (bookmarks elpher-get-bookmarks-node "#" elpher-index)
120 (start elpher-get-start-node "#" elpher-index))
121 "Association list from types to getters, margin codes and index faces.")
124 ;;; Customization group
129 :group 'applications)
131 ;; Face customizations
133 (defface elpher-index
134 '((t :inherit font-lock-keyword-face))
135 "Face used for directory type directory records.")
139 "Face used for text type directory records.")
142 '((t :inherit default))
143 "Face used for info type directory records.")
145 (defface elpher-image
146 '((t :inherit font-lock-string-face))
147 "Face used for image type directory records.")
149 (defface elpher-search
150 '((t :inherit warning))
151 "Face used for search type directory records.")
154 '((t :inherit font-lock-comment-face))
155 "Face used for url type directory records.")
157 (defface elpher-telnet
158 '((t :inherit font-lock-function-name-face))
159 "Face used for telnet type directory records.")
161 (defface elpher-binary
162 '((t :inherit font-lock-doc-face))
163 "Face used for binary type directory records.")
165 (defface elpher-unknown
166 '((t :inherit error))
167 "Face used for directory records with unknown/unsupported types.")
169 (defface elpher-margin-key
171 "Face used for directory margin key.")
173 (defface elpher-margin-brackets
174 '((t :inherit shadow))
175 "Face used for brackets around directory margin key.")
177 ;; Other customizations
179 (defcustom elpher-open-urls-with-eww nil
180 "If non-nil, open URL selectors using eww.
181 Otherwise, use the system browser via the BROWSE-URL function."
184 (defcustom elpher-buttonify-urls-in-directories nil
185 "If non-nil, turns URLs matched in directories into clickable buttons."
188 (defcustom elpher-cache-images nil
189 "If non-nil, cache images in memory in the same way as other content."
192 (defcustom elpher-use-header t
193 "If non-nil, display current node information in buffer header."
201 (defun elpher-make-address (type &optional selector host port use-tls)
202 "Create an address of a gopher object with TYPE, SELECTOR, HOST and PORT.
203 Although selector host and port are optional, they are only omitted for
204 special address types, such as 'start for the start page.
206 Setting the USE-TLS parameter to non-nil causes Elpher to engage TLS mode
207 before attempting to connect to the server."
209 (list type selector host port 'tls)
210 (list type selector host port)))
212 (defun elpher-address-type (address)
213 "Retrieve type from ADDRESS."
216 (defun elpher-address-selector (address)
217 "Retrieve selector from ADDRESS."
220 (defun elpher-address-host (address)
221 "Retrieve host from ADDRESS."
224 (defun elpher-address-port (address)
225 "Retrieve port from ADDRESS."
228 (defun elpher-address-use-tls-p (address)
229 "Returns non-nil if ADDRESS is marked as needing TLS."
232 (defun elpher-address-special-p (address)
233 "Return non-nil if ADDRESS is special (e.g. start page, bookmarks page)."
234 (not (elpher-address-host address)))
238 (defun elpher-make-node (display-string address &optional parent)
239 "Create a node in the gopher page hierarchy.
241 DISPLAY-STRING records the display string used for the page.
243 ADDRESS specifies the address of the gopher page.
245 The optional PARENT specifies the parent node in the hierarchy.
246 This is set every time the node is visited, so while it forms
247 an important part of the node data there is no need to set it
249 (list display-string address parent))
251 (defun elpher-node-display-string (node)
252 "Retrieve the display string of NODE."
255 (defun elpher-node-address (node)
256 "Retrieve the address of NODE."
259 (defun elpher-node-parent (node)
260 "Retrieve the parent node of NODE."
263 (defun elpher-set-node-parent (node parent)
264 "Set the parent node of NODE to be PARENT."
265 (setcar (cdr (cdr node)) parent))
269 (defvar elpher-content-cache (make-hash-table :test 'equal))
270 (defvar elpher-pos-cache (make-hash-table :test 'equal))
272 (defun elpher-get-cached-content (address)
273 "Retrieve the cached content for ADDRESS, or nil if none exists."
274 (gethash address elpher-content-cache))
276 (defun elpher-cache-content (address content)
277 "Set the content cache for ADDRESS to CONTENT."
278 (puthash address content elpher-content-cache))
280 (defun elpher-get-cached-pos (address)
281 "Retrieve the cached cursor position for ADDRESS, or nil if none exists."
282 (gethash address elpher-pos-cache))
284 (defun elpher-cache-pos (address pos)
285 "Set the cursor position cache for ADDRESS to POS."
286 (puthash address pos elpher-pos-cache))
288 ;; Node graph traversal
290 (defvar elpher-current-node nil)
292 (defun elpher-visit-node (node &optional getter preserve-parent)
293 "Visit NODE using its own getter or GETTER, if non-nil.
294 Additionally, set the parent of NODE to `elpher-current-node',
295 unless PRESERVE-PARENT is non-nil."
297 (elpher-process-cleanup)
298 (unless preserve-parent
299 (if (and (elpher-node-parent elpher-current-node)
300 (equal (elpher-node-address elpher-current-node)
301 (elpher-node-address node)))
302 (elpher-set-node-parent node (elpher-node-parent elpher-current-node))
303 (elpher-set-node-parent node elpher-current-node)))
304 (setq elpher-current-node node)
307 (let* ((address (elpher-node-address node))
308 (type (elpher-address-type address)))
309 (funcall (car (alist-get type elpher-type-map))))))
311 (defun elpher-visit-parent-node ()
312 "Visit the parent of the current node."
313 (let ((parent-node (elpher-node-parent elpher-current-node)))
315 (elpher-visit-node parent-node nil t))))
317 (defun elpher-reload-current-node ()
318 "Reload the current node, discarding any existing cached content."
319 (elpher-cache-content (elpher-node-address elpher-current-node) nil)
320 (elpher-visit-node elpher-current-node))
322 (defun elpher-save-pos ()
323 "Save the current position of point to the current node."
324 (when elpher-current-node
325 (elpher-cache-pos (elpher-node-address elpher-current-node) (point))))
327 (defun elpher-restore-pos ()
328 "Restore the position of point to that cached in the current node."
329 (let ((pos (elpher-get-cached-pos (elpher-node-address elpher-current-node))))
332 (goto-char (point-min)))))
335 ;;; Buffer preparation
338 (defun elpher-update-header ()
339 "If `elpher-use-header' is true, display current node info in window header."
340 (if elpher-use-header
341 (setq header-line-format (elpher-node-display-string elpher-current-node))))
343 (defmacro elpher-with-clean-buffer (&rest args)
344 "Evaluate ARGS with a clean *elpher* buffer as current."
345 (list 'with-current-buffer "*elpher*"
347 (append (list 'let '((inhibit-read-only t))
349 '(elpher-update-header))
356 (defvar elpher-user-coding-system nil
357 "User-specified coding system to use for decoding text responses.")
359 (defun elpher-decode (string)
360 "Decode STRING using autodetected or user-specified coding system."
361 (decode-coding-string string
362 (if elpher-user-coding-system
363 elpher-user-coding-system
364 (detect-coding-string string t))))
366 (defun elpher-preprocess-text-response (string)
367 "Preprocess text selector response contained in STRING.
368 This involes decoding the character representation, and clearing
369 away CRs and any terminating period."
370 (elpher-decode (replace-regexp-in-string "\n\.\n$" "\n"
371 (replace-regexp-in-string "\r" "" string))))
376 (defun elpher-insert-index (string)
377 "Insert the index corresponding to STRING into the current buffer."
378 ;; Should be able to split directly on CRLF, but some non-conformant
379 ;; LF-only servers sadly exist, hence the following.
380 (let ((str-processed (elpher-preprocess-text-response string)))
381 (dolist (line (split-string str-processed "\n"))
382 (unless (= (length line) 0)
383 (let* ((type (elt line 0))
384 (fields (split-string (substring line 1) "\t"))
385 (display-string (elt fields 0))
386 (selector (elt fields 1))
387 (host (elt fields 2))
388 (port (if (elt fields 3)
389 (string-to-number (elt fields 3))
391 (address (elpher-make-address type selector host port)))
392 (elpher-insert-index-record display-string address))))))
394 (defun elpher-insert-margin (&optional type-name)
395 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
398 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
400 (propertize "[" 'face 'elpher-margin-brackets)
401 (propertize type-name 'face 'elpher-margin-key)
402 (propertize "]" 'face 'elpher-margin-brackets))))
404 (insert (make-string elpher-margin-width ?\s))))
406 (defun elpher-node-button-help (node)
407 "Return a string containing the help text for a button corresponding to NODE."
408 (let ((address (elpher-node-address node)))
409 (if (eq (elpher-address-type address) ?h)
410 (let ((url (cadr (split-string (elpher-address-selector address) "URL:"))))
411 (format "mouse-1, RET: open url '%s'" url))
412 (format "mouse-1, RET: open '%s' on %s port %s"
413 (elpher-address-selector address)
414 (elpher-address-host address)
415 (elpher-address-port address)))))
418 (defun elpher-insert-index-record (display-string address)
419 "Function to insert an index record into the current buffer.
420 The contents of the record are dictated by DISPLAY-STRING and ADDRESS."
421 (let ((type-map-entry (alist-get (elpher-address-type address)
424 (let* ((margin-code (elt type-map-entry 1))
425 (face (elt type-map-entry 2))
426 (node (elpher-make-node display-string address)))
427 (elpher-insert-margin margin-code)
428 (insert-text-button display-string
431 'action #'elpher-click-link
433 'help-echo (elpher-node-button-help node)))
436 (elpher-insert-margin)
438 (if elpher-buttonify-urls-in-directories
439 (elpher-buttonify-urls display-string)
441 'face 'elpher-info)))
443 (elpher-insert-margin (concat (char-to-string type) "?"))
444 (insert (propertize display-string
445 'face 'elpher-unknown)))))
448 (defun elpher-click-link (button)
449 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
450 (let ((node (button-get button 'elpher-node)))
451 (elpher-visit-node node)))
454 ;;; Selector retrieval (all kinds)
457 (defun elpher-process-cleanup ()
458 "Immediately shut down any extant elpher process."
459 (let ((p (get-process "elpher-process")))
460 (if p (delete-process p))))
462 (defvar elpher-use-tls nil
463 "If non-nil, use TLS to communicate with gopher servers.")
465 (defvar elpher-selector-string)
467 (defun elpher-get-selector (address after)
468 "Retrieve selector specified by ADDRESS, then execute AFTER.
469 The result is stored as a string in the variable ‘elpher-selector-string’."
470 (setq elpher-selector-string "")
471 (when (and (elpher-address-use-tls-p address)
473 (gnutls-available-p))
474 (setq elpher-use-tls t)
475 (message "Engaging TLS mode."))
476 (condition-case the-error
477 (let* ((kill-buffer-query-functions nil)
478 (proc (open-network-stream "elpher-process"
480 (elpher-address-host address)
481 (elpher-address-port address)
482 :type (if elpher-use-tls 'tls 'plain))))
483 (set-process-filter proc
484 (lambda (proc string)
485 (setq elpher-selector-string
486 (concat elpher-selector-string string))))
487 (set-process-sentinel proc after)
488 (process-send-string proc
489 (concat (elpher-address-selector address) "\n")))
491 (if (and (consp the-error)
492 (eq (car the-error) 'gnutls-error)
493 (not (elpher-address-use-tls-p address)))
495 (message "Disengaging TLS mode.")
496 (setq elpher-use-tls nil)
497 (elpher-get-selector address after))
498 (elpher-process-cleanup)
499 (elpher-with-clean-buffer
500 (insert (propertize "\n---- ERROR -----\n\n" 'face 'error)
501 "Failed to connect to " (elpher-get-address-url address) ".\n"
502 (propertize "\n----------------\n\n" 'face 'error)
503 "Press 'u' to return to the previous page."))))))
507 (defun elpher-get-index-node ()
508 "Getter which retrieves the current node contents as an index."
509 (let* ((address (elpher-node-address elpher-current-node))
510 (content (elpher-get-cached-content address)))
513 (elpher-with-clean-buffer
515 (elpher-restore-pos)))
516 (elpher-with-clean-buffer
517 (insert "LOADING DIRECTORY... (use 'u' to cancel)"))
518 (elpher-get-selector address
520 (unless (string-prefix-p "deleted" event)
521 (elpher-with-clean-buffer
522 (elpher-insert-index elpher-selector-string)
524 (elpher-cache-content
525 (elpher-node-address elpher-current-node)
526 (buffer-string)))))))))
530 (defconst elpher-url-regex
531 "\\([a-zA-Z]+\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
532 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
534 (defun elpher-make-node-from-matched-url (&optional string)
535 "Convert most recent `elpher-url-regex' match to a node.
537 If STRING is non-nil, this is given as an argument to all `match-string'
538 calls, as is necessary if the match is performed by `string-match'."
539 (let ((url (match-string 0 string))
540 (protocol (downcase (match-string 1 string))))
541 (if (or (string= protocol "gopher")
542 (string= protocol "gophers"))
543 (let* ((host (match-string 2 string))
544 (port (if (> (length (match-string 3 string)) 1)
545 (string-to-number (substring (match-string 3 string) 1))
547 (type-and-selector (match-string 4 string))
548 (type (if (> (length type-and-selector) 1)
549 (elt type-and-selector 1)
551 (selector (if (> (length type-and-selector) 1)
552 (substring type-and-selector 2)
554 (use-tls (string= protocol "gophers"))
555 (address (elpher-make-address type selector host port use-tls)))
556 (elpher-make-node url address))
557 (let* ((host (match-string 2 string))
558 (port (if (> (length (match-string 3 string)) 1)
559 (string-to-number (substring (match-string 3 string) 1))
561 (selector (concat "URL:" url))
562 (address (elpher-make-address ?h selector host port)))
563 (elpher-make-node url address)))))
566 (defun elpher-buttonify-urls (string)
567 "Turn substrings which look like urls in STRING into clickable buttons."
570 (goto-char (point-min))
571 (while (re-search-forward elpher-url-regex nil t)
572 (let ((node (elpher-make-node-from-matched-url)))
573 (make-text-button (match-beginning 0)
576 'action #'elpher-click-link
578 'help-echo (elpher-node-button-help node))))
581 (defun elpher-get-text-node ()
582 "Getter which retrieves the current node contents as a text document."
583 (let* ((address (elpher-node-address elpher-current-node))
584 (content (elpher-get-cached-content address)))
587 (elpher-with-clean-buffer
589 (elpher-restore-pos)))
591 (elpher-with-clean-buffer
592 (insert "LOADING TEXT... (use 'u' to cancel)"))
593 (elpher-get-selector address
595 (unless (string-prefix-p "deleted" event)
596 (elpher-with-clean-buffer
597 (insert (elpher-buttonify-urls
598 (elpher-preprocess-text-response
599 elpher-selector-string)))
601 (elpher-cache-content
602 (elpher-node-address elpher-current-node)
603 (buffer-string))))))))))
607 (defun elpher-get-image-node ()
608 "Getter which retrieves the current node contents as an image to view."
609 (let* ((address (elpher-node-address elpher-current-node))
610 (content (elpher-get-cached-content address)))
613 (elpher-with-clean-buffer
614 (insert-image content)
615 (elpher-restore-pos)))
616 (if (display-images-p)
618 (elpher-with-clean-buffer
619 (insert "LOADING IMAGE... (use 'u' to cancel)"))
620 (elpher-get-selector address
622 (unless (string-prefix-p "deleted" event)
623 (let ((image (create-image
624 elpher-selector-string
626 (elpher-with-clean-buffer
628 (elpher-restore-pos))
629 (if elpher-cache-images
630 (elpher-cache-content
631 (elpher-node-address elpher-current-node)
633 (elpher-get-node-download)))))
637 (defun elpher-get-search-node ()
638 "Getter which submits a search query to the address of the current node."
639 (let* ((address (elpher-node-address elpher-current-node))
640 (content (elpher-get-cached-content address))
644 (elpher-with-clean-buffer
646 (elpher-restore-pos))
647 (message "Displaying cached search results. Reload to perform a new search."))
649 (let* ((query-string (read-string "Query: "))
650 (query-selector (concat (elpher-address-selector address) "\t" query-string))
651 (search-address (elpher-make-address ?1
653 (elpher-address-host address)
654 (elpher-address-port address))))
656 (elpher-with-clean-buffer
657 (insert "LOADING RESULTS... (use 'u' to cancel)"))
658 (elpher-get-selector search-address
660 (unless (string-prefix-p "deleted" event)
661 (elpher-with-clean-buffer
662 (elpher-insert-index elpher-selector-string))
663 (goto-char (point-min))
664 (elpher-cache-content
665 (elpher-node-address elpher-current-node)
668 (elpher-visit-parent-node))))))
670 ;; Raw server response retrieval
672 (defun elpher-get-node-raw ()
673 "Getter which retrieves the raw server response for the current node."
674 (let ((address (elpher-node-address elpher-current-node)))
675 (elpher-with-clean-buffer
676 (insert "LOADING RAW SERVER RESPONSE... (use 'u' to cancel)"))
678 (elpher-get-selector address
680 (unless (string-prefix-p "deleted" event)
681 (elpher-with-clean-buffer
682 (insert elpher-selector-string)
683 (goto-char (point-min))))))
685 (elpher-with-clean-buffer
686 (insert elpher-start-index))
687 (goto-char (point-min)))))
688 (message "Displaying raw server response. Reload or redraw to return to standard view."))
690 ;; File export retrieval
692 (defvar elpher-download-filename)
694 (defun elpher-get-node-download ()
695 "Getter which retrieves the current node and writes the result to a file."
696 (let* ((address (elpher-node-address elpher-current-node))
697 (selector (elpher-address-selector address)))
698 (elpher-visit-parent-node) ; Do first in case of non-local exits.
699 (let* ((filename-proposal (file-name-nondirectory selector))
700 (filename (read-file-name "Save file as: "
702 (if (> (length filename-proposal) 0)
705 (message "Downloading...")
706 (setq elpher-download-filename filename)
707 (elpher-get-selector address
709 (let ((coding-system-for-write 'binary))
710 (with-temp-file elpher-download-filename
711 (insert elpher-selector-string)
712 (message (format "Download complate, saved to file %s."
713 elpher-download-filename)))))))))
717 (defun elpher-insert-rendered-html (string)
718 "Use shr to insert rendered view of html STRING into current buffer."
719 (let ((dom (with-temp-buffer
721 (libxml-parse-html-region (point-min) (point-max)))))
722 (shr-insert-document dom)))
724 (defun elpher-get-url-node ()
725 "Getter which attempts to open the URL specified by the current node."
726 (let* ((address (elpher-node-address elpher-current-node))
727 (selector (elpher-address-selector address)))
728 (let ((url (elt (split-string selector "URL:") 1)))
731 (elpher-visit-parent-node) ; Do first in case of non-local exits.
732 (message "Opening URL...")
733 (if elpher-open-urls-with-eww
736 (let ((content (elpher-get-cached-content address)))
739 (elpher-with-clean-buffer
741 (elpher-restore-pos)))
742 (elpher-with-clean-buffer
743 (insert "LOADING HTML... (use 'u' to cancel)"))
744 (elpher-get-selector address
746 (unless (string-prefix-p "deleted" event)
747 (elpher-with-clean-buffer
748 (elpher-insert-rendered-html elpher-selector-string)
749 (goto-char (point-min))
750 (elpher-cache-content
751 (elpher-node-address elpher-current-node)
752 (buffer-string))))))))))))
754 ;; Telnet node connection
756 (defun elpher-get-telnet-node ()
757 "Getter which opens a telnet connection to the server specified by the current node."
758 (let* ((address (elpher-node-address elpher-current-node))
759 (host (elpher-address-host address))
760 (port (elpher-address-port address)))
761 (elpher-visit-parent-node)
764 ;; Start page node retrieval
766 (defun elpher-get-start-node ()
767 "Getter which displays the start page."
768 (elpher-with-clean-buffer
769 (elpher-insert-index elpher-start-index)
770 (elpher-restore-pos)))
772 ;; Bookmarks page node retrieval
774 (defun elpher-get-bookmarks-node ()
775 "Getter to load and display the current bookmark list."
776 (elpher-with-clean-buffer
777 (insert "---- Bookmark list ----\n\n")
778 (let ((bookmarks (elpher-load-bookmarks)))
780 (dolist (bookmark bookmarks)
781 (let ((display-string (elpher-bookmark-display-string bookmark))
782 (address (elpher-bookmark-address bookmark)))
783 (elpher-insert-index-record display-string address)))
784 (insert "No bookmarks found.\n")))
785 (insert "\n-----------------------\n\n"
786 "- u: return to previous page\n"
787 "- x: delete selected bookmark\n"
788 "- a: rename selected bookmark\n\n"
789 "Bookmarks are stored in the file "
790 (locate-user-emacs-file "elpher-bookmarks"))
791 (elpher-restore-pos)))
797 (defun elpher-make-bookmark (display-string address)
798 "Make an elpher bookmark.
799 DISPLAY-STRING determines how the bookmark will appear in the
800 bookmark list, while ADDRESS is the address of the entry."
801 (list display-string address))
803 (defun elpher-bookmark-display-string (bookmark)
804 "Get the display string of BOOKMARK."
807 (defun elpher-set-bookmark-display-string (bookmark display-string)
808 "Set the display string of BOOKMARK to DISPLAY-STRING."
809 (setcar bookmark display-string))
811 (defun elpher-bookmark-address (bookmark)
812 "Get the address for BOOKMARK."
815 (defun elpher-save-bookmarks (bookmarks)
816 "Record the bookmark list BOOKMARKS to the user's bookmark file.
817 Beware that this completely replaces the existing contents of the file."
818 (with-temp-file (locate-user-emacs-file "elpher-bookmarks")
820 (insert "; Elpher gopher bookmarks file\n\n"
821 "; Bookmarks are stored as a list of (label (type selector host port))\n"
822 "; s-expressions, where type is stored as a character (i.e. 49 = ?1).\n"
823 "; Feel free to edit by hand, but ensure this structure remains intact.\n\n")
824 (pp bookmarks (current-buffer))))
826 (defun elpher-load-bookmarks ()
827 "Get the list of bookmarks from the users's bookmark file."
830 (insert-file-contents (locate-user-emacs-file "elpher-bookmarks"))
831 (goto-char (point-min))
832 (read (current-buffer)))))
834 (defun elpher-add-address-bookmark (address display-string)
835 "Save a bookmark for ADDRESS with label DISPLAY-STRING.
836 If ADDRESS is already bookmarked, update the label only."
837 (let ((bookmarks (elpher-load-bookmarks)))
838 (let ((existing-bookmark (rassoc (list address) bookmarks)))
839 (if existing-bookmark
840 (elpher-set-bookmark-display-string existing-bookmark display-string)
841 (add-to-list 'bookmarks (elpher-make-bookmark display-string address))))
842 (elpher-save-bookmarks bookmarks)))
844 (defun elpher-remove-address-bookmark (address)
845 "Remove any bookmark to ADDRESS."
846 (elpher-save-bookmarks
847 (seq-filter (lambda (bookmark)
848 (not (equal (elpher-bookmark-address bookmark) address)))
849 (elpher-load-bookmarks))))
851 ;;; Interactive procedures
854 (defun elpher-next-link ()
855 "Move point to the next link on the current page."
859 (defun elpher-prev-link ()
860 "Move point to the previous link on the current page."
864 (defun elpher-follow-current-link ()
865 "Open the link or url at point."
870 "Go to a particular gopher site read from the minibuffer.
871 The site may be specified via a URL or explicitly in terms of
872 host, selector and port."
875 (let ((host-or-url (read-string "Gopher host or URL: ")))
876 (if (string-match elpher-url-regex host-or-url)
877 (elpher-make-node-from-matched-url host-or-url)
878 (let ((selector (read-string "Selector (default none): " nil nil ""))
879 (port-string (read-string "Port (default 70): " nil nil "70")))
880 (elpher-make-node (concat "gopher://" host-or-url
883 (elpher-make-address ?1 selector host-or-url
884 (string-to-number port-string))))))))
885 (switch-to-buffer "*elpher*")
886 (elpher-visit-node node)))
888 (defun elpher-redraw ()
889 "Redraw current page."
891 (if elpher-current-node
892 (elpher-visit-node elpher-current-node)
893 (message "No current site.")))
895 (defun elpher-reload ()
896 "Reload current page."
898 (if elpher-current-node
899 (elpher-reload-current-node)
900 (message "No current site.")))
902 (defun elpher-view-raw ()
903 "View raw server response for current page."
905 (if elpher-current-node
906 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
907 (error "This page was not generated by a server")
908 (elpher-visit-node elpher-current-node
909 #'elpher-get-node-raw))
910 (message "No current site.")))
912 (defun elpher-back ()
913 "Go to previous site."
915 (if (elpher-node-parent elpher-current-node)
916 (elpher-visit-parent-node)
917 (error "No previous site")))
919 (defun elpher-download ()
920 "Download the link at point."
922 (let ((button (button-at (point))))
924 (let ((node (button-get button 'elpher-node)))
925 (if (elpher-address-special-p (elpher-node-address node))
926 (error "Cannot download this link")
927 (elpher-visit-node (button-get button 'elpher-node)
928 #'elpher-get-node-download)))
929 (error "No link selected"))))
931 (defun elpher-build-link-map ()
932 "Build alist mapping link names to destination nodes in current buffer."
934 (b (next-button (point-min) t)))
936 (add-to-list 'link-map (cons (button-label b) b))
937 (setq b (next-button (button-start b))))
940 (defun elpher-jump ()
941 "Select a directory entry by name. Similar to the info browser (m)enu command."
943 (let* ((link-map (elpher-build-link-map)))
945 (let ((key (let ((completion-ignore-case t))
946 (completing-read "Directory item/link: "
948 (if (and key (> (length key) 0))
949 (let ((b (cdr (assoc key link-map))))
950 (goto-char (button-start b))
951 (button-activate b)))))))
953 (defun elpher-root-dir ()
954 "Visit root of current server."
956 (let* ((address (elpher-node-address elpher-current-node))
957 (host (elpher-address-host address)))
959 (let ((host (elpher-address-host address))
960 (selector (elpher-address-selector address))
961 (port (elpher-address-port address)))
962 (if (> (length selector) 0)
963 (let ((root-address (elpher-make-address ?1 "" host port)))
965 (elpher-make-node (concat "gopher://" host
966 ":" (number-to-string port)
969 (error "Already at root directory of current server")))
970 (error "Command invalid for this page"))))
972 (defun elpher-bookmarks-current-p ()
973 "Return non-nil if current node is a bookmarks page."
974 (eq (elpher-address-type (elpher-node-address elpher-current-node)) 'bookmarks))
976 (defun elpher-reload-bookmarks ()
977 "Reload bookmarks if current node is a bookmarks page."
978 (if (elpher-bookmarks-current-p)
979 (elpher-reload-current-node)))
981 (defun elpher-bookmark-current ()
982 "Bookmark the current node."
984 (let ((address (elpher-node-address elpher-current-node))
985 (display-string (elpher-node-display-string elpher-current-node)))
986 (if (not (elpher-address-special-p address))
987 (let ((bookmark-display-string (read-string "Bookmark display string: "
989 (elpher-add-address-bookmark address bookmark-display-string)
990 (message "Bookmark added."))
991 (error "Cannot bookmark %s" display-string))))
993 (defun elpher-bookmark-link ()
994 "Bookmark the link at point."
996 (let ((button (button-at (point))))
998 (let* ((node (button-get button 'elpher-node))
999 (address (elpher-node-address node))
1000 (display-string (elpher-node-display-string node)))
1001 (if (not (elpher-address-special-p address))
1002 (let ((bookmark-display-string (read-string "Bookmark display string: "
1004 (elpher-add-address-bookmark address bookmark-display-string)
1005 (elpher-reload-bookmarks)
1006 (message "Bookmark added."))
1007 (error "Cannot bookmark %s" display-string)))
1008 (error "No link selected"))))
1010 (defun elpher-unbookmark-current ()
1011 "Remove bookmark for the current node."
1013 (let ((address (elpher-node-address elpher-current-node)))
1014 (unless (elpher-address-special-p address)
1015 (elpher-remove-address-bookmark address)
1016 (message "Bookmark removed."))))
1018 (defun elpher-unbookmark-link ()
1019 "Remove bookmark for the link at point."
1021 (let ((button (button-at (point))))
1023 (let ((node (button-get button 'elpher-node)))
1024 (elpher-remove-address-bookmark (elpher-node-address node))
1025 (elpher-reload-bookmarks)
1026 (message "Bookmark removed."))
1027 (error "No link selected"))))
1029 (defun elpher-bookmarks ()
1032 (switch-to-buffer "*elpher*")
1034 (elpher-make-node "Bookmarks Page" (elpher-make-address 'bookmarks))))
1036 (defun elpher-info-node (node)
1037 "Display information on NODE."
1038 (let ((display-string (elpher-node-display-string node))
1039 (address (elpher-node-address node)))
1040 (if (not (elpher-address-special-p address))
1041 (message "`%s' on %s port %s"
1042 (elpher-address-selector address)
1043 (elpher-address-host address)
1044 (elpher-address-port address))
1045 (message "%s" display-string))))
1047 (defun elpher-info-link ()
1048 "Display information on node corresponding to link at point."
1050 (let ((button (button-at (point))))
1052 (elpher-info-node (button-get button 'elpher-node))
1053 (error "No item selected"))))
1055 (defun elpher-info-current ()
1056 "Display information on current node."
1058 (elpher-info-node elpher-current-node))
1060 (defun elpher-get-address-url (address)
1061 "Get URL representation of ADDRESS."
1062 (let ((type (elpher-address-type address))
1063 (selector (elpher-address-selector address))
1064 (host (elpher-address-host address))
1065 (port (elpher-address-port address)))
1066 (if (and (equal type ?h)
1067 (string-prefix-p "URL:" selector))
1068 (elt (split-string selector "URL:") 1)
1073 (format ":%d" port))
1077 (defun elpher-copy-node-url (node)
1078 "Copy URL representation of address of NODE to `kill-ring'."
1079 (let ((address (elpher-node-address node)))
1080 (if (elpher-address-special-p address)
1081 (error (format "Cannot represent %s as URL" (elpher-node-display-string node)))
1082 (let ((url (elpher-get-address-url address)))
1083 (message "Copied \"%s\" to kill-ring/clipboard." url)
1086 (defun elpher-copy-link-url ()
1087 "Copy URL of item at point to `kill-ring'."
1089 (let ((button (button-at (point))))
1091 (elpher-copy-node-url (button-get button 'elpher-node))
1092 (error "No item selected"))))
1094 (defun elpher-copy-current-url ()
1095 "Copy URL of current node to `kill-ring'."
1097 (elpher-copy-node-url elpher-current-node))
1099 (defun elpher-set-coding-system ()
1100 "Specify an explicit character coding system."
1102 (let ((system (read-coding-system "Set coding system to use (default is to autodetect): " nil)))
1103 (setq elpher-user-coding-system system)
1105 (message "Coding system fixed to %s. (Reload to see effect)." system)
1106 (message "Coding system set to autodetect. (Reload to see effect)."))))
1111 (defvar elpher-mode-map
1112 (let ((map (make-sparse-keymap)))
1113 (define-key map (kbd "TAB") 'elpher-next-link)
1114 (define-key map (kbd "<backtab>") 'elpher-prev-link)
1115 (define-key map (kbd "u") 'elpher-back)
1116 (define-key map (kbd "O") 'elpher-root-dir)
1117 (define-key map (kbd "g") 'elpher-go)
1118 (define-key map (kbd "r") 'elpher-redraw)
1119 (define-key map (kbd "R") 'elpher-reload)
1120 (define-key map (kbd "w") 'elpher-view-raw)
1121 (define-key map (kbd "d") 'elpher-download)
1122 (define-key map (kbd "m") 'elpher-jump)
1123 (define-key map (kbd "i") 'elpher-info-link)
1124 (define-key map (kbd "I") 'elpher-info-current)
1125 (define-key map (kbd "c") 'elpher-copy-link-url)
1126 (define-key map (kbd "C") 'elpher-copy-current-url)
1127 (define-key map (kbd "a") 'elpher-bookmark-link)
1128 (define-key map (kbd "A") 'elpher-bookmark-current)
1129 (define-key map (kbd "x") 'elpher-unbookmark-link)
1130 (define-key map (kbd "X") 'elpher-unbookmark-current)
1131 (define-key map (kbd "B") 'elpher-bookmarks)
1132 (define-key map (kbd "S") 'elpher-set-coding-system)
1133 (when (fboundp 'evil-define-key)
1134 (evil-define-key 'motion map
1135 (kbd "TAB") 'elpher-next-link
1136 (kbd "C-]") 'elpher-follow-current-link
1137 (kbd "C-t") 'elpher-back
1138 (kbd "u") 'elpher-back
1139 (kbd "O") 'elpher-root-dir
1140 (kbd "g") 'elpher-go
1141 (kbd "r") 'elpher-redraw
1142 (kbd "R") 'elpher-reload
1143 (kbd "w") 'elpher-view-raw
1144 (kbd "d") 'elpher-download
1145 (kbd "m") 'elpher-jump
1146 (kbd "i") 'elpher-info-link
1147 (kbd "I") 'elpher-info-current
1148 (kbd "c") 'elpher-copy-link-url
1149 (kbd "C") 'elpher-copy-current-url
1150 (kbd "a") 'elpher-bookmark-link
1151 (kbd "A") 'elpher-bookmark-current
1152 (kbd "x") 'elpher-unbookmark-link
1153 (kbd "X") 'elpher-unbookmark-current
1154 (kbd "B") 'elpher-bookmarks
1155 (kbd "S") 'elpher-set-coding-system))
1157 "Keymap for gopher client.")
1159 (define-derived-mode elpher-mode special-mode "elpher"
1160 "Major mode for elpher, an elisp gopher client.")
1162 (when (fboundp 'evil-set-initial-state)
1163 (evil-set-initial-state 'elpher-mode 'motion))
1165 ;;; Main start procedure
1170 "Start elpher with default landing page."
1172 (if (get-buffer "*elpher*")
1173 (switch-to-buffer "*elpher*")
1174 (switch-to-buffer "*elpher*")
1175 (setq elpher-current-node nil)
1176 (let ((start-node (elpher-make-node "Elpher Start Page"
1177 (elpher-make-address 'start))))
1178 (elpher-visit-node start-node)))
1179 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
1181 ;;; elpher.el ends here