1 ;;; elpher.el --- A friendly gopher client. -*- lexical-binding:t -*-
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,
40 ;; - basic support for the fledgling Gemini protocol.
42 ;; To launch Elpher, simply use 'M-x elpher'. This will open a start
43 ;; page containing information on key bindings and suggested starting
44 ;; points for your gopher exploration.
46 ;; Full instructions can be found in the Elpher info manual.
48 ;; Elpher is under active development. Any suggestions for
49 ;; improvements are welcome!
68 (defconst elpher-version "2.3.1"
69 "Current version of elpher.")
71 (defconst elpher-margin-width 6
72 "Width of left-hand margin used when rendering indicies.")
74 (defconst elpher-type-map
75 '(((gopher ?0) elpher-get-gopher-node elpher-render-text "txt" elpher-text)
76 ((gopher ?1) elpher-get-gopher-node elpher-render-index "/" elpher-index)
77 ((gopher ?4) elpher-get-gopher-node elpher-render-download "bin" elpher-binary)
78 ((gopher ?5) elpher-get-gopher-node elpher-render-download "bin" elpher-binary)
79 ((gopher ?7) elpher-get-gopher-query-node elpher-render-index "?" elpher-search)
80 ((gopher ?9) elpher-get-gopher-node elpher-render-download "bin" elpher-binary)
81 ((gopher ?g) elpher-get-gopher-node elpher-render-image "img" elpher-image)
82 ((gopher ?p) elpher-get-gopher-node elpher-render-image "img" elpher-image)
83 ((gopher ?I) elpher-get-gopher-node elpher-render-image "img" elpher-image)
84 ((gopher ?d) elpher-get-gopher-node elpher-render-download "doc" elpher-binary)
85 ((gopher ?P) elpher-get-gopher-node elpher-render-download "doc" elpher-binary)
86 ((gopher ?s) elpher-get-gopher-node elpher-render-download "snd" elpher-binary)
87 ((gopher ?h) elpher-get-gopher-node elpher-render-html "htm" elpher-html)
88 (gemini elpher-get-gemini-node elpher-render-gemini "gem" elpher-gemini)
89 (telnet elpher-get-telnet-node nil "tel" elpher-telnet)
90 (other-url elpher-get-other-url-node nil "url" elpher-other-url)
91 ((special bookmarks) elpher-get-bookmarks-node nil)
92 ((special start) elpher-get-start-node nil))
93 "Association list from types to getters, renderers, margin codes and index faces.")
96 ;;; Customization group
101 :group 'applications)
103 ;; Face customizations
105 (defface elpher-index
106 '((t :inherit font-lock-keyword-face))
107 "Face used for directory type directory records.")
111 "Face used for text type directory records.")
114 '((t :inherit default))
115 "Face used for info type directory records.")
117 (defface elpher-image
118 '((t :inherit font-lock-string-face))
119 "Face used for image type directory records.")
121 (defface elpher-search
122 '((t :inherit warning))
123 "Face used for search type directory records.")
126 '((t :inherit font-lock-comment-face))
127 "Face used for html type directory records.")
129 (defface elpher-gemini
130 '((t :inherit font-lock-regexp-grouping-backslash))
131 "Face used for html type directory records.")
133 (defface elpher-other-url
134 '((t :inherit font-lock-comment-face))
135 "Face used for other URL type links records.")
137 (defface elpher-telnet
138 '((t :inherit font-lock-function-name-face))
139 "Face used for telnet type directory records.")
141 (defface elpher-binary
142 '((t :inherit font-lock-doc-face))
143 "Face used for binary type directory records.")
145 (defface elpher-unknown
146 '((t :inherit error))
147 "Face used for directory records with unknown/unsupported types.")
149 (defface elpher-margin-key
151 "Face used for directory margin key.")
153 (defface elpher-margin-brackets
154 '((t :inherit shadow))
155 "Face used for brackets around directory margin key.")
157 ;; Other customizations
159 (defcustom elpher-open-urls-with-eww nil
160 "If non-nil, open URL selectors using eww.
161 Otherwise, use the system browser via the BROWSE-URL function."
164 (defcustom elpher-buttonify-urls-in-directories t
165 "If non-nil, turns URLs matched in directories into clickable buttons."
168 (defcustom elpher-use-header t
169 "If non-nil, display current node information in buffer header."
172 (defcustom elpher-auto-disengage-TLS nil
173 "If non-nil, automatically disengage TLS following an unsuccessful connection.
174 While enabling this may seem convenient, it is also potentially dangerous as it
175 allows switching from an encrypted channel back to plain text without user input."
184 ;; An elpher "address" object is either a url object or a symbol.
185 ;; Symbol addresses are "special", corresponding to pages generated
186 ;; dynamically for and by elpher. All others represent pages which
187 ;; rely on content retrieved over the network.
189 (defun elpher-address-from-url (url-string)
190 "Create a ADDRESS object corresponding to the given URL-STRING."
191 (let ((data (match-data))) ; Prevent parsing clobbering match data
193 (let ((url (url-generic-parse-url url-string)))
194 (unless (and (not (url-fullness url)) (url-type url))
195 (setf (url-fullness url) t)
196 (setf (url-filename url)
197 (url-unhex-string (url-filename url)))
198 (unless (url-type url)
199 (setf (url-type url) "gopher"))
200 (when (or (equal "gopher" (url-type url))
201 (equal "gophers" (url-type url)))
203 (unless (url-host url)
204 (setf (url-host url) (url-filename url))
205 (setf (url-filename url) ""))
206 (when (or (equal (url-filename url) "")
207 (equal (url-filename url) "/"))
208 (setf (url-filename url) "/1")))
209 (when (equal "gemini" (url-type url))
211 (if (equal (url-filename url) "")
212 (setf (url-filename url) "/"))))
214 (set-match-data data))))
216 (defun elpher-make-gopher-address (type selector host port &optional tls)
217 "Create an ADDRESS object using gopher directory record attributes.
218 The basic attributes include: TYPE, SELECTOR, HOST and PORT.
219 If the optional attribute TLS is non-nil, the address will be marked as
220 requiring gopher-over-TLS."
222 ((and (equal type ?h)
223 (string-prefix-p "URL:" selector))
224 (elpher-address-from-url (elt (split-string selector "URL:") 1)))
226 (elpher-address-from-url
229 ":" (number-to-string port))))
231 (elpher-address-from-url
232 (concat "gopher" (if tls "s" "")
234 ":" (number-to-string port)
238 (defun elpher-make-special-address (type)
239 "Create an ADDRESS object corresponding to the given special page symbol TYPE."
242 (defun elpher-address-to-url (address)
243 "Get string representation of ADDRESS, or nil if ADDRESS is special."
244 (if (not (elpher-address-special-p address))
245 (url-encode-url (url-recreate-url address))
248 (defun elpher-address-type (address)
249 "Retrieve type of ADDRESS object.
250 This is used to determine how to retrieve and render the document the
251 address refers to, via the table `elpher-type-map'."
252 (if (symbolp address)
253 (list 'special address)
254 (let ((protocol (url-type address)))
255 (cond ((or (equal protocol "gopher")
256 (equal protocol "gophers"))
258 (if (member (url-filename address) '("" "/"))
260 (string-to-char (substring (url-filename address) 1)))))
261 ((equal protocol "gemini")
263 ((equal protocol "telnet")
267 (defun elpher-address-protocol (address)
268 "Retrieve the transport protocol for ADDRESS. This is nil for special addresses."
269 (if (symbolp address)
273 (defun elpher-address-filename (address)
274 "Retrieve the filename component of ADDRESS.
275 For gopher addresses this is a combination of the selector type and selector."
276 (if (symbolp address)
278 (url-filename address)))
280 (defun elpher-address-host (address)
281 "Retrieve host from ADDRESS object."
284 (defun elpher-address-port (address)
285 "Retrieve port from ADDRESS object."
286 (if (symbolp address)
288 (if (> (url-port address) 0)
290 (or (and (or (equal (url-type address) "gopher")
291 (equal (url-type address) "gophers"))
293 (and (equal (url-type address) "gemini")
296 (defun elpher-address-special-p (address)
297 "Return non-nil if ADDRESS object is special (e.g. start page, bookmarks page)."
300 (defun elpher-address-gopher-p (address)
301 "Return non-nill if ADDRESS object is a gopher address."
302 (and (not (elpher-address-special-p address))
303 (member (elpher-address-protocol address) '("gopher gophers"))))
305 (defun elpher-gopher-address-selector (address)
306 "Retrieve gopher selector from ADDRESS object."
307 (if (member (url-filename address) '("" "/"))
309 (substring (url-filename address) 2)))
313 (defun elpher-make-node (display-string address &optional parent)
314 "Create a node in the page hierarchy.
316 DISPLAY-STRING records the display string used for the page.
318 ADDRESS specifies the address object of the page.
320 The optional PARENT specifies the parent node in the hierarchy.
321 This is set every time the node is visited, so while it forms
322 an important part of the node data there is no need to set it
324 (list display-string address parent))
326 (defun elpher-node-display-string (node)
327 "Retrieve the display string of NODE."
330 (defun elpher-node-address (node)
331 "Retrieve the ADDRESS object of NODE."
334 (defun elpher-node-parent (node)
335 "Retrieve the parent node of NODE."
338 (defun elpher-set-node-parent (node parent)
339 "Set the parent node of NODE to be PARENT."
340 (setcar (cdr (cdr node)) parent))
344 (defvar elpher-content-cache (make-hash-table :test 'equal))
345 (defvar elpher-pos-cache (make-hash-table :test 'equal))
347 (defun elpher-get-cached-content (address)
348 "Retrieve the cached content for ADDRESS, or nil if none exists."
349 (gethash address elpher-content-cache))
351 (defun elpher-cache-content (address content)
352 "Set the content cache for ADDRESS to CONTENT."
353 (puthash address content elpher-content-cache))
355 (defun elpher-get-cached-pos (address)
356 "Retrieve the cached cursor position for ADDRESS, or nil if none exists."
357 (gethash address elpher-pos-cache))
359 (defun elpher-cache-pos (address pos)
360 "Set the cursor position cache for ADDRESS to POS."
361 (puthash address pos elpher-pos-cache))
363 ;; Node graph traversal
365 (defvar elpher-current-node nil)
367 (defun elpher-visit-node (node &optional renderer preserve-parent)
368 "Visit NODE using its own renderer or RENDERER, if non-nil.
369 Additionally, set the parent of NODE to `elpher-current-node',
370 unless PRESERVE-PARENT is non-nil."
372 (elpher-process-cleanup)
373 (unless preserve-parent
374 (if (and (elpher-node-parent elpher-current-node)
375 (equal (elpher-node-address elpher-current-node)
376 (elpher-node-address node)))
377 (elpher-set-node-parent node (elpher-node-parent elpher-current-node))
378 (elpher-set-node-parent node elpher-current-node)))
379 (setq elpher-current-node node)
380 (let* ((address (elpher-node-address node))
381 (type (elpher-address-type address))
382 (type-record (cdr (assoc type elpher-type-map))))
384 (funcall (car type-record)
388 (elpher-visit-parent-node)
390 (`(gopher ,type-char)
391 (error "Unsupported gopher selector type '%c' for '%s'"
392 type-char (elpher-address-to-url address)))
394 (error "Unsupported address type '%S' for '%s'"
395 other (elpher-address-to-url address)))))))
397 (defun elpher-visit-parent-node ()
398 "Visit the parent of the current node."
399 (let ((parent-node (elpher-node-parent elpher-current-node)))
401 (elpher-visit-node parent-node nil t))))
403 (defun elpher-reload-current-node ()
404 "Reload the current node, discarding any existing cached content."
405 (elpher-cache-content (elpher-node-address elpher-current-node) nil)
406 (elpher-visit-node elpher-current-node))
408 (defun elpher-save-pos ()
409 "Save the current position of point to the current node."
410 (when elpher-current-node
411 (elpher-cache-pos (elpher-node-address elpher-current-node) (point))))
413 (defun elpher-restore-pos ()
414 "Restore the position of point to that cached in the current node."
415 (let ((pos (elpher-get-cached-pos (elpher-node-address elpher-current-node))))
418 (goto-char (point-min)))))
421 ;;; Buffer preparation
424 (defun elpher-update-header ()
425 "If `elpher-use-header' is true, display current node info in window header."
426 (if elpher-use-header
427 (let* ((display-string (elpher-node-display-string elpher-current-node))
428 (address (elpher-node-address elpher-current-node))
429 (url-string (if (elpher-address-special-p address)
431 (concat " - " (elpher-address-to-url address) "")))
432 (header (replace-regexp-in-string "%" "%%" (concat display-string
434 (setq header-line-format header))))
436 (defmacro elpher-with-clean-buffer (&rest args)
437 "Evaluate ARGS with a clean *elpher* buffer as current."
438 (list 'with-current-buffer "*elpher*"
440 (append (list 'let '((inhibit-read-only t))
442 '(elpher-update-header))
449 (defvar elpher-user-coding-system nil
450 "User-specified coding system to use for decoding text responses.")
452 (defun elpher-decode (string)
453 "Decode STRING using autodetected or user-specified coding system."
454 (decode-coding-string string
455 (if elpher-user-coding-system
456 elpher-user-coding-system
457 (detect-coding-string string t))))
459 (defun elpher-preprocess-text-response (string)
460 "Preprocess text selector response contained in STRING.
461 This involes decoding the character representation, and clearing
462 away CRs and any terminating period."
463 (elpher-decode (replace-regexp-in-string "\n\.\n$" "\n"
464 (replace-regexp-in-string "\r" "" string))))
467 ;;; Network error reporting
470 (defun elpher-network-error (address error)
471 "Display ERROR message following unsuccessful negotiation with ADDRESS."
472 (elpher-with-clean-buffer
473 (insert (propertize "\n---- ERROR -----\n\n" 'face 'error)
474 "When attempting to retrieve " (elpher-address-to-url address) ":\n"
475 (error-message-string error) ".\n"
476 (propertize "\n----------------\n\n" 'face 'error)
477 "Press 'u' to return to the previous page.")))
480 ;;; Gopher selector retrieval
483 (defun elpher-process-cleanup ()
484 "Immediately shut down any extant elpher process."
485 (let ((p (get-process "elpher-process")))
486 (if p (delete-process p))))
488 (defvar elpher-use-tls nil
489 "If non-nil, use TLS to communicate with gopher servers.")
491 (defvar elpher-selector-string)
493 (defun elpher-get-selector (address after &optional propagate-error)
494 "Retrieve selector specified by ADDRESS, then execute AFTER.
495 The result is stored as a string in the variable ‘elpher-selector-string’.
497 Usually errors result in an error page being displayed. This is only
498 appropriate if the selector is to be directly viewed. If PROPAGATE-ERROR
499 is non-nil, this message is not displayed. Instead, the error propagates
500 up to the calling function."
501 (setq elpher-selector-string "")
502 (when (equal (elpher-address-protocol address) "gophers")
503 (if (gnutls-available-p)
504 (when (not elpher-use-tls)
505 (setq elpher-use-tls t)
506 (message "Engaging TLS gopher mode."))
507 (error "Cannot retrieve TLS gopher selector: GnuTLS not available")))
508 (condition-case the-error
509 (let* ((kill-buffer-query-functions nil)
510 (proc (open-network-stream "elpher-process"
512 (elpher-address-host address)
513 (elpher-address-port address)
514 :type (if elpher-use-tls 'tls 'plain))))
515 (set-process-coding-system proc 'binary)
516 (set-process-filter proc
517 (lambda (_proc string)
518 (setq elpher-selector-string
519 (concat elpher-selector-string string))))
520 (set-process-sentinel proc after)
521 (process-send-string proc
522 (concat (elpher-gopher-address-selector address) "\n")))
524 (if (and (consp the-error)
525 (eq (car the-error) 'gnutls-error)
526 (not (equal (elpher-address-protocol address) "gophers"))
527 (or elpher-auto-disengage-TLS
528 (yes-or-no-p "Could not establish encrypted connection. Disable TLS mode? ")))
530 (message "Disengaging TLS gopher mode.")
531 (setq elpher-use-tls nil)
532 (elpher-get-selector address after))
533 (elpher-process-cleanup)
536 (elpher-with-clean-buffer
537 (insert (propertize "\n---- ERROR -----\n\n" 'face 'error)
538 "Failed to connect to " (elpher-address-to-url address) ".\n"
539 (propertize "\n----------------\n\n" 'face 'error)
540 "Press 'u' to return to the previous page.")))))))
542 (defun elpher-get-gopher-node (renderer)
543 "Getter function for gopher nodes.
544 The RENDERER procedure is used to display the contents of the node
545 once they are retrieved from the gopher server."
546 (let* ((address (elpher-node-address elpher-current-node))
547 (content (elpher-get-cached-content address)))
548 (if (and content (funcall renderer nil))
549 (elpher-with-clean-buffer
551 (elpher-restore-pos))
552 (elpher-with-clean-buffer
553 (insert "LOADING... (use 'u' to cancel)"))
554 (elpher-get-selector address
555 (lambda (_proc event)
556 (unless (string-prefix-p "deleted" event)
557 (funcall renderer elpher-selector-string)
558 (elpher-restore-pos)))))))
562 (defun elpher-insert-index (string)
563 "Insert the index corresponding to STRING into the current buffer."
564 ;; Should be able to split directly on CRLF, but some non-conformant
565 ;; LF-only servers sadly exist, hence the following.
566 (let ((str-processed (elpher-preprocess-text-response string)))
567 (dolist (line (split-string str-processed "\n"))
569 (unless (= (length line) 0)
570 (let* ((type (elt line 0))
571 (fields (split-string (substring line 1) "\t"))
572 (display-string (elt fields 0))
573 (selector (elt fields 1))
574 (host (elt fields 2))
575 (port (if (elt fields 3)
576 (string-to-number (elt fields 3))
578 (address (elpher-make-gopher-address type selector host port)))
579 (elpher-insert-index-record display-string address)))))))
581 (defun elpher-insert-margin (&optional type-name)
582 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
585 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
587 (propertize "[" 'face 'elpher-margin-brackets)
588 (propertize type-name 'face 'elpher-margin-key)
589 (propertize "]" 'face 'elpher-margin-brackets))))
591 (insert (make-string elpher-margin-width ?\s))))
593 (defun elpher-node-button-help (node)
594 "Return a string containing the help text for a button corresponding to NODE."
595 (let ((address (elpher-node-address node)))
596 (format "mouse-1, RET: open '%s'" (elpher-address-to-url address))))
598 (defun elpher-insert-index-record (display-string &optional address)
599 "Function to insert an index record into the current buffer.
600 The contents of the record are dictated by DISPLAY-STRING and ADDRESS.
601 If ADDRESS is not supplied or nil the record is rendered as an
603 (let* ((type (if address (elpher-address-type address) nil))
604 (type-map-entry (cdr (assoc type elpher-type-map))))
606 (let* ((margin-code (elt type-map-entry 2))
607 (face (elt type-map-entry 3))
608 (node (elpher-make-node display-string address)))
609 (elpher-insert-margin margin-code)
610 (insert-text-button display-string
613 'action #'elpher-click-link
615 'help-echo (elpher-node-button-help node)))
617 ((or '(gopher ?i) 'nil) ;; Information
618 (elpher-insert-margin)
619 (let ((propertized-display-string
620 (propertize display-string 'face 'elpher-info)))
621 (insert (if elpher-buttonify-urls-in-directories
622 (elpher-buttonify-urls propertized-display-string)
623 propertized-display-string))))
624 (`(gopher ,selector-type) ;; Unknown
625 (elpher-insert-margin (concat (char-to-string selector-type) "?"))
626 (insert (propertize display-string
627 'face 'elpher-unknown)))))
630 (defun elpher-click-link (button)
631 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
632 (let ((node (button-get button 'elpher-node)))
633 (elpher-visit-node node)))
635 (defun elpher-render-index (data &optional _mime-type-string)
636 "Render DATA as an index. MIME-TYPE-STRING is unused."
637 (elpher-with-clean-buffer
640 (elpher-insert-index data)
641 (elpher-cache-content (elpher-node-address elpher-current-node)
646 (defconst elpher-url-regex
647 "\\([a-zA-Z]+\\)://\\([a-zA-Z0-9.\-]*[a-zA-Z0-9\-]\\|\[[a-zA-Z0-9:]+\]\\)\\(:[0-9]+\\)?\\(/\\([0-9a-zA-Z\-_~?/@|:.]*[0-9a-zA-Z\-_~?/@|]\\)?\\)?"
648 "Regexp used to locate and buttniofy URLs in text files loaded by elpher.")
650 (defun elpher-buttonify-urls (string)
651 "Turn substrings which look like urls in STRING into clickable buttons."
654 (goto-char (point-min))
655 (while (re-search-forward elpher-url-regex nil t)
656 (let ((node (elpher-make-node (match-string 0)
657 (elpher-address-from-url (match-string 0)))))
658 (make-text-button (match-beginning 0)
661 'action #'elpher-click-link
663 'help-echo (elpher-node-button-help node)
667 (defun elpher-render-text (data &optional _mime-type-string)
668 "Render DATA as text. MIME-TYPE-STRING is unused."
669 (elpher-with-clean-buffer
672 (insert (elpher-buttonify-urls (elpher-preprocess-text-response data)))
673 (elpher-cache-content
674 (elpher-node-address elpher-current-node)
679 (defun elpher-render-image (data &optional _mime-type-string)
680 "Display DATA as image. MIME-TYPE-STRING is unused."
683 (if (display-images-p)
685 (let ((image (create-image
688 (elpher-with-clean-buffer
690 (elpher-restore-pos))))
691 (elpher-render-download data))))
693 ;; Search retrieval and rendering
695 (defun elpher-get-gopher-query-node (renderer)
696 "Getter for gopher addresses requiring input.
697 The response is rendered using the rendering function RENDERER."
698 (let* ((address (elpher-node-address elpher-current-node))
699 (content (elpher-get-cached-content address))
701 (if (and content (funcall renderer nil))
702 (elpher-with-clean-buffer
705 (message "Displaying cached search results. Reload to perform a new search."))
707 (let* ((query-string (read-string "Query: "))
708 (query-selector (concat (elpher-gopher-address-selector address) "\t" query-string))
709 (search-address (elpher-make-gopher-address ?1
711 (elpher-address-host address)
712 (elpher-address-port address)
713 (equal (elpher-address-type address) "gophers"))))
716 (elpher-with-clean-buffer
717 (insert "LOADING RESULTS... (use 'u' to cancel)"))
718 (elpher-get-selector search-address
719 (lambda (_proc event)
720 (unless (string-prefix-p "deleted" event)
721 (funcall renderer elpher-selector-string)
722 (elpher-restore-pos)))))
724 (elpher-visit-parent-node))))))
726 ;; Raw server response rendering
728 (defun elpher-render-raw (data &optional _mime-type-string)
729 "Display raw DATA in buffer. MIME-TYPE-STRING is unused."
732 (elpher-with-clean-buffer
734 (goto-char (point-min)))
735 (message "Displaying raw server response. Reload or redraw to return to standard view.")))
737 ;; File save "rendering"
739 (defun elpher-render-download (data &optional _mime-type-string)
740 "Save DATA to file. MIME-TYPE-STRING is unused."
743 (let* ((address (elpher-node-address elpher-current-node))
744 (selector (elpher-gopher-address-selector address)))
745 (elpher-visit-parent-node) ; Do first in case of non-local exits.
746 (let* ((filename-proposal (file-name-nondirectory selector))
747 (filename (read-file-name "Download complete. Save file as: "
749 (if (> (length filename-proposal) 0)
752 (let ((coding-system-for-write 'binary))
753 (with-temp-file filename
755 (message (format "Saved to file %s." filename))))))
759 (defun elpher-render-html (data &optional _mime-type-string)
760 "Render DATA as HTML using shr. MIME-TYPE-STRING is unused."
761 (elpher-with-clean-buffer
764 (let ((dom (with-temp-buffer
766 (libxml-parse-html-region (point-min) (point-max)))))
767 (shr-insert-document dom)))))
769 ;; Gemini node retrieval
771 (defvar elpher-gemini-response)
773 (defun elpher-get-gemini-response (address after)
774 "Retrieve gemini ADDRESS, then execute AFTER.
775 The response is stored in the variable ‘elpher-gemini-response’."
776 (setq elpher-gemini-response "")
777 (if (not (gnutls-available-p))
778 (error "Cannot retrieve TLS selector: GnuTLS not available")
779 (condition-case the-error
780 (let* ((kill-buffer-query-functions nil)
781 (proc (open-network-stream "elpher-process"
783 (elpher-address-host address)
784 (elpher-address-port address)
786 (set-process-coding-system proc 'binary)
787 (set-process-filter proc
788 (lambda (_proc string)
789 (setq elpher-gemini-response
790 (concat elpher-gemini-response string))))
791 (set-process-sentinel proc after)
792 (process-send-string proc
793 (concat (elpher-address-to-url address) "\r\n")))
795 (error "Error initiating connection to server")))))
797 (defun elpher-process-gemini-response (renderer)
798 "Process the gemini response and pass the result to RENDERER.
799 The response is assumed to be in the variable `elpher-gemini-response'."
800 (condition-case the-error
801 (let* ((response-header (car (split-string elpher-gemini-response "\r\n")))
802 (response-body (substring elpher-gemini-response
803 (+ (string-match "\r\n" elpher-gemini-response) 2)))
804 (response-code (car (split-string response-header)))
805 (response-meta (string-trim
806 (substring response-header
807 (string-match "[ \t]+" response-header)))))
808 (pcase (elt response-code 0)
810 (elpher-with-clean-buffer
811 (insert "Gemini server is requesting input."))
812 (let* ((query-string (read-string (concat response-meta ": ")))
813 (url (elpher-address-to-url (elpher-node-address elpher-current-node)))
814 (query-address (elpher-address-from-url (concat url "?" query-string))))
815 (elpher-get-gemini-response query-address
816 (lambda (_proc event)
817 (unless (string-prefix-p "deleted" event)
818 (funcall #'elpher-process-gemini-response
820 (elpher-restore-pos))))))
821 (?2 ; Normal response
822 ;; (message response-header)
823 (funcall renderer response-body response-meta))
825 (message "Following redirect to %s" response-meta)
826 (let ((redirect-address (elpher-address-from-gemini-url response-meta)))
827 (elpher-get-gemini-response redirect-address
828 (lambda (_proc event)
829 (unless (string-prefix-p "deleted" event)
830 (funcall #'elpher-process-gemini-response
832 (elpher-restore-pos))))))
833 (?4 ; Temporary failure
834 (error "Gemini server reports TEMPORARY FAILURE for this request: %S"
836 (?5 ; Permanent failure
837 (error "Gemini server reports PERMANENT FAILURE for this request: %S"
839 (?6 ; Client certificate required
840 (error "Gemini server requires client certificate (unsupported at this time)"))
842 (error "Gemini server response unknown: %S"
845 (elpher-network-error (elpher-node-address elpher-current-node) the-error))))
847 (defun elpher-get-gemini-node (renderer)
848 "Getter which retrieves and renders a Gemini node and renders it using RENDERER."
849 (let* ((address (elpher-node-address elpher-current-node))
850 (content (elpher-get-cached-content address)))
851 (condition-case the-error
852 (if (and content (funcall renderer nil))
853 (elpher-with-clean-buffer
855 (elpher-restore-pos))
856 (elpher-with-clean-buffer
857 (insert "LOADING GEMINI... (use 'u' to cancel)"))
858 (elpher-get-gemini-response address
859 (lambda (_proc event)
860 (unless (string-prefix-p "deleted" event)
861 (funcall #'elpher-process-gemini-response
863 (elpher-restore-pos)))))
865 (elpher-network-error address the-error)))))
868 (defun elpher-render-gemini (body &optional mime-type-string)
869 "Render gemini response BODY with rendering MIME-TYPE-STRING."
872 (let* ((mime-type-string* (if (or (not mime-type-string)
873 (string-empty-p mime-type-string))
874 "text/gemini; charset=utf-8"
876 (mime-type-split (split-string mime-type-string* ";" t))
877 (mime-type (string-trim (car mime-type-split)))
878 (parameters (mapcar (lambda (s)
879 (let ((key-val (split-string s "=")))
880 (list (downcase (string-trim (car key-val)))
881 (downcase (string-trim (cadr key-val))))))
882 (cdr mime-type-split))))
883 (if (and (equal "text/gemini" mime-type)
884 (not (assoc "charset" parameters)))
885 (setq parameters (cons (list "charset" "utf-8") parameters)))
886 (when (string-prefix-p "text/" mime-type)
887 (if (assoc "charset" parameters)
888 (setq body (decode-coding-string body
889 (intern (cadr (assoc "charset" parameters))))))
890 (setq body (replace-regexp-in-string "\r" "" body)))
892 ((or "text/gemini" "")
893 (elpher-render-gemini-map body parameters))
894 ((pred (string-prefix-p "text/"))
895 (elpher-render-gemini-plain-text body parameters))
896 ((pred (string-prefix-p "image/"))
897 (elpher-render-image body))
899 (error "Unsupported MIME type %S" mime-type))))))
901 (defun elpher-gemini-get-link-url (line)
902 "Extract the url portion of LINE, a gemini map file link line."
903 (string-trim (elt (split-string (substring line 2)) 0)))
905 (defun elpher-gemini-get-link-display-string (line)
906 "Extract the display string portion of LINE, a gemini map file link line."
907 (let* ((rest (string-trim (elt (split-string line "=>") 1)))
908 (idx (string-match "[ \t]" rest)))
910 (string-trim (substring rest (+ idx 1)))
913 (defun elpher-address-from-gemini-url (url)
914 "Extract address from URL with defaults as per gemini map files."
915 (let ((address (url-generic-parse-url url)))
916 (unless (and (url-type address) (not (url-fullness address))) ;avoid mangling mailto: urls
917 (setf (url-fullness address) t)
918 (unless (url-host address) ;if there is an explicit host, filenames are absolute
919 (setf (url-host address) (url-host (elpher-node-address elpher-current-node)))
920 (unless (string-prefix-p "/" (url-filename address)) ;deal with relative links
921 (setf (url-filename address)
922 (concat (file-name-directory
923 (url-filename (elpher-node-address elpher-current-node)))
924 (url-filename address)))))
925 (unless (url-type address)
926 (setf (url-type address) "gemini")))
929 (defun elpher-render-gemini-map (data _parameters)
930 "Render DATA as a gemini map file, PARAMETERS is currently unused."
931 (elpher-with-clean-buffer
932 (dolist (line (split-string data "\n"))
933 (if (string-prefix-p "=>" line)
934 (let* ((url (elpher-gemini-get-link-url line))
935 (display-string (elpher-gemini-get-link-display-string line))
936 (address (elpher-address-from-gemini-url url)))
937 (if (> (length display-string) 0)
938 (elpher-insert-index-record display-string address)
939 (elpher-insert-index-record url address)))
940 (elpher-insert-index-record line)))
941 (elpher-cache-content
942 (elpher-node-address elpher-current-node)
945 (defun elpher-render-gemini-plain-text (data _parameters)
946 "Render DATA as plain text file. PARAMETERS is currently unused."
947 (elpher-with-clean-buffer
948 (insert (elpher-buttonify-urls data))
949 (elpher-cache-content
950 (elpher-node-address elpher-current-node)
953 ;; Other URL node opening
955 (defun elpher-get-other-url-node (renderer)
956 "Getter which attempts to open the URL specified by the current node (RENDERER must be nil)."
958 (elpher-visit-parent-node)
959 (error "Command not supported for general URLs"))
960 (let* ((address (elpher-node-address elpher-current-node))
961 (url (elpher-address-to-url address)))
963 (elpher-visit-parent-node) ; Do first in case of non-local exits.
964 (message "Opening URL...")
965 (if elpher-open-urls-with-eww
969 ;; Telnet node connection
971 (defun elpher-get-telnet-node (renderer)
972 "Opens a telnet connection to the current node address (RENDERER must be nil)."
974 (elpher-visit-parent-node)
975 (error "Command not supported for telnet URLs"))
976 (let* ((address (elpher-node-address elpher-current-node))
977 (host (elpher-address-host address))
978 (port (elpher-address-port address)))
979 (elpher-visit-parent-node)
982 ;; Start page node retrieval
984 (defun elpher-get-start-node (renderer)
985 "Getter which displays the start page (RENDERER must be nil)."
987 (elpher-visit-parent-node)
988 (error "Command not supported for start page"))
989 (elpher-with-clean-buffer
990 (insert " --------------------------------------------\n"
991 " Elpher Gopher Client \n"
992 " version " elpher-version "\n"
993 " --------------------------------------------\n"
995 "Default bindings:\n"
997 " - TAB/Shift-TAB: next/prev item on current page\n"
998 " - RET/mouse-1: open item under cursor\n"
999 " - m: select an item on current page by name (autocompletes)\n"
1000 " - u: return to previous page\n"
1001 " - o/O: visit different selector or the root menu of the current server\n"
1002 " - g: go to a particular gopher address\n"
1003 " - d/D: download item under cursor or current page\n"
1004 " - i/I: info on item under cursor or current page\n"
1005 " - c/C: copy URL representation of item under cursor or current page\n"
1006 " - a/A: bookmark the item under cursor or current page\n"
1007 " - x/X: remove bookmark for item under cursor or current page\n"
1008 " - B: visit the bookmarks page\n"
1009 " - r: redraw current page (using cached contents if available)\n"
1010 " - R: reload current page (regenerates cache)\n"
1011 " - S: set character coding system for gopher (default is to autodetect)\n"
1012 " - T: toggle TLS gopher mode\n"
1013 " - .: display the raw server response for the current page\n"
1015 "Start your exploration of gopher space:\n")
1016 (elpher-insert-index-record "Floodgap Systems Gopher Server"
1017 (elpher-make-gopher-address ?1 "" "gopher.floodgap.com" 70))
1019 "Alternatively, select the following item and enter some search terms:\n")
1020 (elpher-insert-index-record "Veronica-2 Gopher Search Engine"
1021 (elpher-make-gopher-address ?7 "/v2/vs" "gopher.floodgap.com" 70))
1024 (let ((help-string "RET,mouse-1: Open Elpher info manual (if available)"))
1025 (insert-text-button "Elpher info manual"
1031 'help-echo help-string))
1032 (insert " for the full documentation. **\n")
1034 (concat " (This should be available if you have installed Elpher using\n"
1035 " MELPA. Otherwise you will have to install the manual yourself.)")
1037 (elpher-restore-pos)))
1039 ;; Bookmarks page node retrieval
1041 (defun elpher-get-bookmarks-node (renderer)
1042 "Getter to load and display the current bookmark list (RENDERER must be nil)."
1044 (elpher-visit-parent-node)
1045 (error "Command not supported for bookmarks page"))
1046 (elpher-with-clean-buffer
1047 (insert "---- Bookmark list ----\n\n")
1048 (let ((bookmarks (elpher-load-bookmarks)))
1050 (dolist (bookmark bookmarks)
1051 (let ((display-string (elpher-bookmark-display-string bookmark))
1052 (address (elpher-address-from-url (elpher-bookmark-url bookmark))))
1053 (elpher-insert-index-record display-string address)))
1054 (insert "No bookmarks found.\n")))
1055 (insert "\n-----------------------\n\n"
1056 "- u: return to previous page\n"
1057 "- x: delete selected bookmark\n"
1058 "- a: rename selected bookmark\n\n"
1059 "Bookmarks are stored in the file "
1060 (locate-user-emacs-file "elpher-bookmarks"))
1061 (elpher-restore-pos)))
1067 (defun elpher-make-bookmark (display-string url)
1068 "Make an elpher bookmark.
1069 DISPLAY-STRING determines how the bookmark will appear in the
1070 bookmark list, while URL is the url of the entry."
1071 (list display-string url))
1073 (defun elpher-bookmark-display-string (bookmark)
1074 "Get the display string of BOOKMARK."
1077 (defun elpher-set-bookmark-display-string (bookmark display-string)
1078 "Set the display string of BOOKMARK to DISPLAY-STRING."
1079 (setcar bookmark display-string))
1081 (defun elpher-bookmark-url (bookmark)
1082 "Get the address for BOOKMARK."
1085 (defun elpher-save-bookmarks (bookmarks)
1086 "Record the bookmark list BOOKMARKS to the user's bookmark file.
1087 Beware that this completely replaces the existing contents of the file."
1088 (with-temp-file (locate-user-emacs-file "elpher-bookmarks")
1090 (insert "; Elpher bookmarks file\n\n"
1091 "; Bookmarks are stored as a list of (label URL) items.\n"
1092 "; Feel free to edit by hand, but take care to ensure\n"
1093 "; the list structure remains intact.\n\n")
1094 (pp bookmarks (current-buffer))))
1096 (defun elpher-load-bookmarks ()
1097 "Get the list of bookmarks from the users's bookmark file."
1101 (insert-file-contents (locate-user-emacs-file "elpher-bookmarks"))
1102 (goto-char (point-min))
1103 (read (current-buffer))))))
1104 (if (and bookmarks (listp (cadar bookmarks)))
1106 (message "Reading old bookmark file. (Will be updated on write.)")
1107 (mapcar (lambda (old-bm)
1109 (elpher-address-to-url (apply #'elpher-make-gopher-address
1114 (defun elpher-add-address-bookmark (address display-string)
1115 "Save a bookmark for ADDRESS with label DISPLAY-STRING.)))
1116 If ADDRESS is already bookmarked, update the label only."
1117 (let ((bookmarks (elpher-load-bookmarks))
1118 (url (elpher-address-to-url address)))
1119 (let ((existing-bookmark (rassoc (list url) bookmarks)))
1120 (if existing-bookmark
1121 (elpher-set-bookmark-display-string existing-bookmark display-string)
1122 (push (elpher-make-bookmark display-string url) bookmarks)))
1123 (elpher-save-bookmarks bookmarks)))
1125 (defun elpher-remove-address-bookmark (address)
1126 "Remove any bookmark to ADDRESS."
1127 (let ((url (elpher-address-to-url address)))
1128 (elpher-save-bookmarks
1129 (seq-filter (lambda (bookmark)
1130 (not (equal (elpher-bookmark-url bookmark) url)))
1131 (elpher-load-bookmarks)))))
1133 ;;; Interactive procedures
1136 (defun elpher-next-link ()
1137 "Move point to the next link on the current page."
1141 (defun elpher-prev-link ()
1142 "Move point to the previous link on the current page."
1144 (backward-button 1))
1146 (defun elpher-follow-current-link ()
1147 "Open the link or url at point."
1152 "Go to a particular gopher site read from the minibuffer."
1155 (let ((host-or-url (read-string "Gopher or Gemini URL: ")))
1156 (elpher-make-node host-or-url
1157 (elpher-address-from-url host-or-url)))))
1158 (switch-to-buffer "*elpher*")
1159 (elpher-visit-node node)))
1161 (defun elpher-go-current ()
1162 "Go to a particular site read from the minibuffer, initialized with the current URL."
1164 (let ((address (elpher-node-address elpher-current-node)))
1165 (if (elpher-address-special-p address)
1166 (error "Command invalid for this page")
1167 (let ((url (read-string "Gopher or Gemini URL: " (elpher-address-to-url address))))
1168 (elpher-visit-node (elpher-make-node url (elpher-address-from-url url)))))))
1170 (defun elpher-redraw ()
1171 "Redraw current page."
1173 (if elpher-current-node
1174 (elpher-visit-node elpher-current-node)
1175 (message "No current site.")))
1177 (defun elpher-reload ()
1178 "Reload current page."
1180 (if elpher-current-node
1181 (elpher-reload-current-node)
1182 (message "No current site.")))
1184 (defun elpher-toggle-tls ()
1185 "Toggle TLS encryption mode."
1187 (setq elpher-use-tls (not elpher-use-tls))
1189 (if (gnutls-available-p)
1190 (message "TLS gopher mode enabled. (Will not affect current page until reload.)")
1191 (setq elpher-use-tls nil)
1192 (error "Cannot enable TLS gopher mode: GnuTLS not available"))
1193 (message "TLS gopher mode disabled. (Will not affect current page until reload.)")))
1195 (defun elpher-view-raw ()
1196 "View raw server response for current page."
1198 (if elpher-current-node
1199 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
1200 (error "This page was not generated by a server")
1201 (elpher-visit-node elpher-current-node
1202 #'elpher-render-raw))
1203 (message "No current site.")))
1205 (defun elpher-back ()
1206 "Go to previous site."
1208 (if (elpher-node-parent elpher-current-node)
1209 (elpher-visit-parent-node)
1210 (error "No previous site")))
1212 (defun elpher-download ()
1213 "Download the link at point."
1215 (let ((button (button-at (point))))
1217 (let ((node (button-get button 'elpher-node)))
1218 (if (elpher-address-special-p (elpher-node-address node))
1219 (error "Cannot download %s"
1220 (elpher-node-display-string node))
1221 (elpher-visit-node (button-get button 'elpher-node)
1222 #'elpher-render-download)))
1223 (error "No link selected"))))
1225 (defun elpher-download-current ()
1226 "Download the current page."
1228 (if (elpher-address-special-p (elpher-node-address elpher-current-node))
1229 (error "Cannot download %s"
1230 (elpher-node-display-string elpher-current-node))
1231 (elpher-visit-node (elpher-make-node
1232 (elpher-node-display-string elpher-current-node)
1233 (elpher-node-address elpher-current-node)
1234 elpher-current-node)
1235 #'elpher-render-download
1238 (defun elpher-build-link-map ()
1239 "Build alist mapping link names to destination nodes in current buffer."
1240 (let ((link-map nil)
1241 (b (next-button (point-min) t)))
1243 (push (cons (button-label b) b) link-map)
1244 (setq b (next-button (button-start b))))
1247 (defun elpher-jump ()
1248 "Select a directory entry by name. Similar to the info browser (m)enu command."
1250 (let* ((link-map (elpher-build-link-map)))
1252 (let ((key (let ((completion-ignore-case t))
1253 (completing-read "Directory item/link: "
1255 (if (and key (> (length key) 0))
1256 (let ((b (cdr (assoc key link-map))))
1257 (goto-char (button-start b))
1258 (button-activate b)))))))
1260 (defun elpher-root-dir ()
1261 "Visit root of current server."
1263 (let ((address (elpher-node-address elpher-current-node)))
1264 (if (not (elpher-address-special-p address))
1265 (if (or (member (url-filename address) '("/" ""))
1266 (and (elpher-address-gopher-p address)
1267 (= (length (elpher-gopher-address-selector address)) 0)))
1268 (error "Already at root directory of current server")
1269 (let ((address-copy (elpher-address-from-url
1270 (elpher-address-to-url address))))
1271 (setf (url-filename address-copy) "")
1273 (elpher-make-node (elpher-address-to-url address-copy)
1275 (error "Command invalid for %s" (elpher-node-display-string elpher-current-node)))))
1277 (defun elpher-bookmarks-current-p ()
1278 "Return non-nil if current node is a bookmarks page."
1279 (equal (elpher-address-type (elpher-node-address elpher-current-node))
1280 '(special bookmarks)))
1282 (defun elpher-reload-bookmarks ()
1283 "Reload bookmarks if current node is a bookmarks page."
1284 (if (elpher-bookmarks-current-p)
1285 (elpher-reload-current-node)))
1287 (defun elpher-bookmark-current ()
1288 "Bookmark the current node."
1290 (let ((address (elpher-node-address elpher-current-node))
1291 (display-string (elpher-node-display-string elpher-current-node)))
1292 (if (not (elpher-address-special-p address))
1293 (let ((bookmark-display-string (read-string "Bookmark display string: "
1295 (elpher-add-address-bookmark address bookmark-display-string)
1296 (message "Bookmark added."))
1297 (error "Cannot bookmark %s" display-string))))
1299 (defun elpher-bookmark-link ()
1300 "Bookmark the link at point."
1302 (let ((button (button-at (point))))
1304 (let* ((node (button-get button 'elpher-node))
1305 (address (elpher-node-address node))
1306 (display-string (elpher-node-display-string node)))
1307 (if (not (elpher-address-special-p address))
1308 (let ((bookmark-display-string (read-string "Bookmark display string: "
1310 (elpher-add-address-bookmark address bookmark-display-string)
1311 (elpher-reload-bookmarks)
1312 (message "Bookmark added."))
1313 (error "Cannot bookmark %s" display-string)))
1314 (error "No link selected"))))
1316 (defun elpher-unbookmark-current ()
1317 "Remove bookmark for the current node."
1319 (let ((address (elpher-node-address elpher-current-node)))
1320 (unless (elpher-address-special-p address)
1321 (elpher-remove-address-bookmark address)
1322 (message "Bookmark removed."))))
1324 (defun elpher-unbookmark-link ()
1325 "Remove bookmark for the link at point."
1327 (let ((button (button-at (point))))
1329 (let ((node (button-get button 'elpher-node)))
1330 (elpher-remove-address-bookmark (elpher-node-address node))
1331 (elpher-reload-bookmarks)
1332 (message "Bookmark removed."))
1333 (error "No link selected"))))
1335 (defun elpher-bookmarks ()
1336 "Visit bookmarks page."
1338 (switch-to-buffer "*elpher*")
1340 (elpher-make-node "Bookmarks Page" (elpher-make-special-address 'bookmarks))))
1342 (defun elpher-info-node (node)
1343 "Display information on NODE."
1344 (let ((display-string (elpher-node-display-string node))
1345 (address (elpher-node-address node)))
1346 (if (elpher-address-special-p address)
1347 (message "Special page: %s" display-string)
1348 (message (elpher-address-to-url address)))))
1350 (defun elpher-info-link ()
1351 "Display information on node corresponding to link at point."
1353 (let ((button (button-at (point))))
1355 (elpher-info-node (button-get button 'elpher-node))
1356 (error "No item selected"))))
1358 (defun elpher-info-current ()
1359 "Display information on current node."
1361 (elpher-info-node elpher-current-node))
1363 (defun elpher-copy-node-url (node)
1364 "Copy URL representation of address of NODE to `kill-ring'."
1365 (let ((address (elpher-node-address node)))
1366 (if (elpher-address-special-p address)
1367 (error (format "Cannot represent %s as URL" (elpher-node-display-string node)))
1368 (let ((url (elpher-address-to-url address)))
1369 (message "Copied \"%s\" to kill-ring/clipboard." url)
1372 (defun elpher-copy-link-url ()
1373 "Copy URL of item at point to `kill-ring'."
1375 (let ((button (button-at (point))))
1377 (elpher-copy-node-url (button-get button 'elpher-node))
1378 (error "No item selected"))))
1380 (defun elpher-copy-current-url ()
1381 "Copy URL of current node to `kill-ring'."
1383 (elpher-copy-node-url elpher-current-node))
1385 (defun elpher-set-gopher-coding-system ()
1386 "Specify an explicit character coding system for gopher selectors."
1388 (let ((system (read-coding-system "Set coding system to use for gopher (default is to autodetect): " nil)))
1389 (setq elpher-user-coding-system system)
1391 (message "Gopher coding system fixed to %s. (Reload to see effect)." system)
1392 (message "Gopher coding system set to autodetect. (Reload to see effect)."))))
1398 (defvar elpher-mode-map
1399 (let ((map (make-sparse-keymap)))
1400 (define-key map (kbd "TAB") 'elpher-next-link)
1401 (define-key map (kbd "<backtab>") 'elpher-prev-link)
1402 (define-key map (kbd "u") 'elpher-back)
1403 (define-key map [mouse-3] 'elpher-back)
1404 (define-key map (kbd "O") 'elpher-root-dir)
1405 (define-key map (kbd "g") 'elpher-go)
1406 (define-key map (kbd "o") 'elpher-go-current)
1407 (define-key map (kbd "r") 'elpher-redraw)
1408 (define-key map (kbd "R") 'elpher-reload)
1409 (define-key map (kbd "T") 'elpher-toggle-tls)
1410 (define-key map (kbd ".") 'elpher-view-raw)
1411 (define-key map (kbd "d") 'elpher-download)
1412 (define-key map (kbd "D") 'elpher-download-current)
1413 (define-key map (kbd "m") 'elpher-jump)
1414 (define-key map (kbd "i") 'elpher-info-link)
1415 (define-key map (kbd "I") 'elpher-info-current)
1416 (define-key map (kbd "c") 'elpher-copy-link-url)
1417 (define-key map (kbd "C") 'elpher-copy-current-url)
1418 (define-key map (kbd "a") 'elpher-bookmark-link)
1419 (define-key map (kbd "A") 'elpher-bookmark-current)
1420 (define-key map (kbd "x") 'elpher-unbookmark-link)
1421 (define-key map (kbd "X") 'elpher-unbookmark-current)
1422 (define-key map (kbd "B") 'elpher-bookmarks)
1423 (define-key map (kbd "S") 'elpher-set-gopher-coding-system)
1424 (when (fboundp 'evil-define-key*)
1425 (evil-define-key* 'motion map
1426 (kbd "TAB") 'elpher-next-link
1427 (kbd "C-") 'elpher-follow-current-link
1428 (kbd "C-t") 'elpher-back
1429 (kbd "u") 'elpher-back
1430 [mouse-3] 'elpher-back
1431 (kbd "g") 'elpher-go
1432 (kbd "o") 'elpher-go-current
1433 (kbd "r") 'elpher-redraw
1434 (kbd "R") 'elpher-reload
1435 (kbd "T") 'elpher-toggle-tls
1436 (kbd ".") 'elpher-view-raw
1437 (kbd "d") 'elpher-download
1438 (kbd "D") 'elpher-download-current
1439 (kbd "m") 'elpher-jump
1440 (kbd "i") 'elpher-info-link
1441 (kbd "I") 'elpher-info-current
1442 (kbd "c") 'elpher-copy-link-url
1443 (kbd "C") 'elpher-copy-current-url
1444 (kbd "a") 'elpher-bookmark-link
1445 (kbd "A") 'elpher-bookmark-current
1446 (kbd "x") 'elpher-unbookmark-link
1447 (kbd "X") 'elpher-unbookmark-current
1448 (kbd "B") 'elpher-bookmarks
1449 (kbd "S") 'elpher-set-gopher-coding-system))
1451 "Keymap for gopher client.")
1453 (define-derived-mode elpher-mode special-mode "elpher"
1454 "Major mode for elpher, an elisp gopher client.
1456 This mode is automatically enabled by the interactive
1457 functions which initialize the gopher client, namely
1458 `elpher', `elpher-go' and `elpher-bookmarks'.")
1460 (when (fboundp 'evil-set-initial-state)
1461 (evil-set-initial-state 'elpher-mode 'motion))
1464 ;;; Main start procedure
1469 "Start elpher with default landing page."
1471 (if (get-buffer "*elpher*")
1472 (switch-to-buffer "*elpher*")
1473 (switch-to-buffer "*elpher*")
1474 (setq elpher-current-node nil)
1475 (let ((start-node (elpher-make-node "Elpher Start Page"
1476 (elpher-make-special-address 'start))))
1477 (elpher-visit-node start-node)))
1478 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
1480 ;;; elpher.el ends here