Everything seems to be working again.
[elpher.git] / elpher.el
1 ;;; elpher.el --- A friendly gopher client.
2
3 ;; Copyright (C) 2019 Tim Vaughan
4
5 ;; Author: Tim Vaughan <tgvaughan@gmail.com>
6 ;; Created: 11 April 2019
7 ;; Version: 1.4.7
8 ;; Keywords: comm gopher
9 ;; Homepage: https://github.com/tgvaughan/elpher
10 ;; Package-Requires: ((emacs "26"))
11
12 ;; This file is not part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
29 ;; Elpher aims to provide a practical and friendly gopher client
30 ;; for GNU Emacs.  It supports:
31
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
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.
44
45 ;; Full instructions can be found in the Elpher info manual.
46
47 ;; Elpher is under active development.  Any suggestions for
48 ;; improvements are welcome!
49
50 ;;; Code:
51
52 (provide 'elpher)
53
54 ;;; Dependencies
55 ;;
56
57 (require 'seq)
58 (require 'pp)
59 (require 'shr)
60 (require 'url-util)
61
62
63 ;;; Global constants
64 ;;
65
66 (defconst elpher-version "2.0.0"
67   "Current version of elpher.")
68
69 (defconst elpher-margin-width 6
70   "Width of left-hand margin used when rendering indicies.")
71
72 (defconst elpher-type-map
73   '(((gopher ?0) elpher-get-text-node "txt" elpher-text)
74     ((gopher ?1) elpher-get-index-node "/" elpher-index)
75     ((gopher ?4) elpher-get-node-download "bin" elpher-binary)
76     ((gopher ?5) elpher-get-node-download "bin" elpher-binary)
77     ((gopher ?7) elpher-get-search-node "?" elpher-search)
78     ((gopher ?8) elpher-get-telnet-node "tel" elpher-telnet)
79     ((gopher ?9) elpher-get-node-download "bin" elpher-binary)
80     ((gopher ?g) elpher-get-image-node "img" elpher-image)
81     ((gopher ?p) elpher-get-image-node "img" elpher-image)
82     ((gopher ?I) elpher-get-image-node "img" elpher-image)
83     ((gopher ?d) elpher-get-node-download "doc" elpher-binary)
84     ((gopher ?P) elpher-get-node-download "doc" elpher-binary)
85     ((gopher ?s) elpher-get-node-download "snd" elpher-binary)
86     ((gopher ?h) elpher-get-url-node "htm" elpher-html)
87     ((special bookmarks) elpher-get-bookmarks-node)
88     ((special start) elpher-get-start-node))
89   "Association list from types to getters, margin codes and index faces.")
90
91
92 ;;; Customization group
93 ;;
94
95 (defgroup elpher nil
96   "A gopher client."
97   :group 'applications)
98
99 ;; Face customizations
100
101 (defface elpher-index
102   '((t :inherit font-lock-keyword-face))
103   "Face used for directory type directory records.")
104
105 (defface elpher-text
106   '((t :inherit bold))
107   "Face used for text type directory records.")
108
109 (defface elpher-info
110   '((t :inherit default))
111   "Face used for info type directory records.")
112
113 (defface elpher-image
114   '((t :inherit font-lock-string-face))
115   "Face used for image type directory records.")
116
117 (defface elpher-search
118   '((t :inherit warning))
119   "Face used for search type directory records.")
120
121 (defface elpher-url
122   '((t :inherit font-lock-comment-face))
123   "Face used for url type directory records.")
124
125 (defface elpher-telnet
126   '((t :inherit font-lock-function-name-face))
127   "Face used for telnet type directory records.")
128
129 (defface elpher-binary
130   '((t :inherit font-lock-doc-face))
131   "Face used for binary type directory records.")
132
133 (defface elpher-unknown
134   '((t :inherit error))
135   "Face used for directory records with unknown/unsupported types.")
136
137 (defface elpher-margin-key
138   '((t :inherit bold))
139   "Face used for directory margin key.")
140
141 (defface elpher-margin-brackets
142   '((t :inherit shadow))
143   "Face used for brackets around directory margin key.")
144
145 ;; Other customizations
146
147 (defcustom elpher-open-urls-with-eww nil
148   "If non-nil, open URL selectors using eww.
149 Otherwise, use the system browser via the BROWSE-URL function."
150   :type '(boolean))
151
152 (defcustom elpher-buttonify-urls-in-directories nil
153   "If non-nil, turns URLs matched in directories into clickable buttons."
154   :type '(boolean))
155
156 (defcustom elpher-use-header t
157   "If non-nil, display current node information in buffer header."
158   :type '(boolean))
159
160 (defcustom elpher-auto-disengage-TLS nil
161   "If non-nil, automatically disengage TLS following an unsuccessful connection.
162 While enabling this may seem convenient, it is also potentially dangerous as it
163 allows switching from an encrypted channel back to plain text without user input."
164   :type '(boolean))
165
166
167 ;;; Model
168 ;;
169
170 ;; Address
171
172 ;; An elpher "address" object is either a url object or a symbol.
173 ;; Symbol addresses are "special", corresponding to pages generated
174 ;; dynamically for and by elpher.  All others represent pages which
175 ;; rely on content retrieved over the network.
176
177 (defun elpher-address-from-url (url-string)
178   "Create a ADDRESS object corresponding to the given URL-STRING."
179   (let ((data (match-data))) ; Prevent parsing clobbering match data
180     (unwind-protect
181         (let ((url (url-generic-parse-url url-string)))
182           (if (and (url-type url)
183                    (url-host url))
184               (let ((is-gopher (or (equal "gopher" (url-type url))
185                                    (equal "gophers" (url-type url)))))
186                 (setf (url-filename url)
187                       (url-unhex-string (url-filename url)))
188                 (when (string-empty-p (url-filename url))
189                   (if is-gopher
190                       (setf (url-filename url) "1")))
191                 (unless (> (url-port url) 0)
192                   (if is-gopher
193                       (setf (url-port url) 70)))
194                 url)
195             (error "Malformed URL" url)))
196       (set-match-data data))))
197
198 (defun elpher-make-gopher-address (type selector host port &optional tls)
199   "Create an ADDRESS object corresponding to the given gopher directory record
200 attributes: TYPE, SELECTOR, HOST and PORT."
201   (elpher-address-from-url
202    (concat "gopher" (if tls "s" "")
203            "://" host
204            ":" (number-to-string port)
205            "/" (string type)
206            selector)))
207
208 (defun elpher-make-special-address (type)
209   "Create an ADDRESS object corresponding to the given special page symbol TYPE."
210   type)
211
212 (defun elpher-address-to-url (address)
213   "Get string representation of ADDRESS, or nil if ADDRESS is special."
214   (if (not (elpher-address-special-p address))
215       (url-encode-url (url-recreate-url address))
216     nil))
217
218 (defun elpher-address-type (address)
219   "Retrieve selector type from ADDRESS object."
220   (if (symbolp address)
221       (list 'special address)
222     (let ((protocol (url-type address)))
223       (cond ((or (equal protocol "gopher")
224                  (equal protocol "gophers"))
225              (list 'gopher (string-to-char (substring (url-filename address) 1))))
226             ((string-equal protocol "gemini")
227              'gemini)))))
228
229 (defun elpher-address-protocol (address)
230   (if (symbolp address)
231       nil
232     (url-type address)))
233
234 (defun elpher-address-filename (address)
235   (if (symbolp address)
236       nil
237     (url-filename address)))
238
239 (defun elpher-address-host (address)
240   "Retrieve host from ADDRESS object."
241   (url-host address))
242
243 (defun elpher-address-port (address)
244   "Retrieve port from ADDRESS object."
245   (url-port address))
246
247 (defun elpher-address-special-p (address)
248   "Return non-nil if ADDRESS object is special (e.g. start page, bookmarks page)."
249   (symbolp address))
250
251 (defun elpher-address-gopher-p (address)
252   "Return non-nill if ADDRESS object is a gopher address."
253   (memq (elpher-address-protocol address) '("gopher gophers")))
254
255 (defun elpher-gopher-address-selector (address)
256   "Retrieve gopher selector from ADDRESS object."
257   (substring (url-filename address) 2))
258
259 ;; Node
260
261 (defun elpher-make-node (display-string address &optional parent)
262   "Create a node in the page hierarchy.
263
264 DISPLAY-STRING records the display string used for the page.
265
266 ADDRESS specifies the address object of the page.
267
268 The optional PARENT specifies the parent node in the hierarchy.
269 This is set every time the node is visited, so while it forms
270 an important part of the node data there is no need to set it
271 initially."
272   (list display-string address parent))
273
274 (defun elpher-node-display-string (node)
275   "Retrieve the display string of NODE."
276   (elt node 0))
277
278 (defun elpher-node-address (node)
279   "Retrieve the ADDRESS object of NODE."
280   (elt node 1))
281
282 (defun elpher-node-parent (node)
283   "Retrieve the parent node of NODE."
284   (elt node 2))
285
286 (defun elpher-set-node-parent (node parent)
287   "Set the parent node of NODE to be PARENT."
288   (setcar (cdr (cdr node)) parent))
289
290 ;; Cache
291
292 (defvar elpher-content-cache (make-hash-table :test 'equal))
293 (defvar elpher-pos-cache (make-hash-table :test 'equal))
294
295 (defun elpher-get-cached-content (address)
296   "Retrieve the cached content for ADDRESS, or nil if none exists."
297   (gethash address elpher-content-cache))
298
299 (defun elpher-cache-content (address content)
300   "Set the content cache for ADDRESS to CONTENT."
301   (puthash address content elpher-content-cache))
302
303 (defun elpher-get-cached-pos (address)
304   "Retrieve the cached cursor position for ADDRESS, or nil if none exists."
305   (gethash address elpher-pos-cache))
306
307 (defun elpher-cache-pos (address pos)
308   "Set the cursor position cache for ADDRESS to POS."
309   (puthash address pos elpher-pos-cache))
310
311 ;; Node graph traversal
312
313 (defvar elpher-current-node nil)
314
315 (defun elpher-visit-node (node &optional getter preserve-parent)
316   "Visit NODE using its own getter or GETTER, if non-nil.
317 Additionally, set the parent of NODE to `elpher-current-node',
318 unless PRESERVE-PARENT is non-nil."
319   (elpher-save-pos)
320   (elpher-process-cleanup)
321   (unless preserve-parent
322     (if (and (elpher-node-parent elpher-current-node)
323              (equal (elpher-node-address elpher-current-node)
324                     (elpher-node-address node)))
325         (elpher-set-node-parent node (elpher-node-parent elpher-current-node))
326       (elpher-set-node-parent node elpher-current-node)))
327   (setq elpher-current-node node)
328   (if getter
329       (funcall getter)
330     (let* ((address (elpher-node-address node))
331            (type (elpher-address-type address))
332            (type-record (cdr (assoc type elpher-type-map))))
333       (if type-record
334           (funcall (car type-record))
335         (elpher-visit-parent-node)
336         (pcase type
337           (`(gopher ,type-char)
338            (error "Unsupported gopher selector type '%c'" type-char))
339           (else
340            (error "Unsupported address type '%S'" type)))))))
341
342 (defun elpher-visit-parent-node ()
343   "Visit the parent of the current node."
344   (let ((parent-node (elpher-node-parent elpher-current-node)))
345     (when parent-node
346       (elpher-visit-node parent-node nil t))))
347       
348 (defun elpher-reload-current-node ()
349   "Reload the current node, discarding any existing cached content."
350   (elpher-cache-content (elpher-node-address elpher-current-node) nil)
351   (elpher-visit-node elpher-current-node))
352
353 (defun elpher-save-pos ()
354   "Save the current position of point to the current node."
355   (when elpher-current-node
356     (elpher-cache-pos (elpher-node-address elpher-current-node) (point))))
357
358 (defun elpher-restore-pos ()
359   "Restore the position of point to that cached in the current node."
360   (let ((pos (elpher-get-cached-pos (elpher-node-address elpher-current-node))))
361     (if pos
362         (goto-char pos)
363       (goto-char (point-min)))))
364
365
366 ;;; Buffer preparation
367 ;;
368
369 (defun elpher-update-header ()
370   "If `elpher-use-header' is true, display current node info in window header."
371   (if elpher-use-header
372       (setq header-line-format (elpher-node-display-string elpher-current-node))))
373
374 (defmacro elpher-with-clean-buffer (&rest args)
375   "Evaluate ARGS with a clean *elpher* buffer as current."
376   (list 'with-current-buffer "*elpher*"
377         '(elpher-mode)
378         (append (list 'let '((inhibit-read-only t))
379                       '(erase-buffer)
380                       '(elpher-update-header))
381                 args)))
382
383
384 ;;; Text Processing
385 ;;
386
387 (defvar elpher-user-coding-system nil
388   "User-specified coding system to use for decoding text responses.")
389
390 (defun elpher-decode (string)
391   "Decode STRING using autodetected or user-specified coding system."
392   (decode-coding-string string
393                         (if elpher-user-coding-system
394                             elpher-user-coding-system
395                           (detect-coding-string string t))))
396
397 (defun elpher-preprocess-text-response (string)
398   "Preprocess text selector response contained in STRING.
399 This involes decoding the character representation, and clearing
400 away CRs and any terminating period."
401   (elpher-decode (replace-regexp-in-string "\n\.\n$" "\n"
402                                            (replace-regexp-in-string "\r" "" string))))
403
404
405 ;;; Index rendering
406 ;;
407
408 (defun elpher-insert-index (string)
409   "Insert the index corresponding to STRING into the current buffer."
410   ;; Should be able to split directly on CRLF, but some non-conformant
411   ;; LF-only servers sadly exist, hence the following.
412   (let ((str-processed (elpher-preprocess-text-response string)))
413     (dolist (line (split-string str-processed "\n"))
414       (unless (= (length line) 0)
415         (let* ((type (elt line 0))
416                (fields (split-string (substring line 1) "\t"))
417                (display-string (elt fields 0))
418                (selector (elt fields 1))
419                (host (elt fields 2))
420                (port (if (elt fields 3)
421                          (string-to-number (elt fields 3))
422                        nil))
423                (address (elpher-make-gopher-address type selector host port)))
424           (elpher-insert-index-record display-string address))))))
425
426 (defun elpher-insert-margin (&optional type-name)
427   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
428   (if type-name
429       (progn
430         (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
431                         (concat
432                          (propertize "[" 'face 'elpher-margin-brackets)
433                          (propertize type-name 'face 'elpher-margin-key)
434                          (propertize "]" 'face 'elpher-margin-brackets))))
435         (insert " "))
436     (insert (make-string elpher-margin-width ?\s))))
437
438 (defun elpher-node-button-help (node)
439   "Return a string containing the help text for a button corresponding to NODE."
440   (let ((address (elpher-node-address node)))
441     (if (equal (elpher-address-type address) '(gopher ?h))
442         (let ((url (cadr (split-string (elpher-gopher-address-selector address) "URL:"))))
443           (format "mouse-1, RET: open url '%s'" url))
444       (format "mouse-1, RET: open '%s' on %s port %s"
445               (elpher-gopher-address-selector address)
446               (elpher-address-host address)
447               (elpher-address-port address)))))
448
449 (defun elpher-insert-index-record (display-string address)
450   "Function to insert an index record into the current buffer.
451 The contents of the record are dictated by DISPLAY-STRING and ADDRESS."
452   (let* ((type (elpher-address-type address))
453          (type-map-entry (cdr (assoc type elpher-type-map))))
454     (if type-map-entry
455         (let* ((margin-code (elt type-map-entry 1))
456                (face (elt type-map-entry 2))
457                (node (elpher-make-node display-string address)))
458           (elpher-insert-margin margin-code)
459           (insert-text-button display-string
460                               'face face
461                               'elpher-node node
462                               'action #'elpher-click-link
463                               'follow-link t
464                               'help-echo (elpher-node-button-help node)))
465       (pcase type
466         ('(gopher ?i) ;; Information
467          (elpher-insert-margin)
468          (insert (propertize
469                   (if elpher-buttonify-urls-in-directories
470                       (elpher-buttonify-urls display-string)
471                     display-string)
472                   'face 'elpher-info)))
473         (`(gopher ,selector-type) ;; Unknown
474          (elpher-insert-margin (concat (char-to-string selector-type) "?"))
475          (insert (propertize display-string
476                              'face 'elpher-unknown)))))
477     (insert "\n")))
478
479 (defun elpher-click-link (button)
480   "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
481   (let ((node (button-get button 'elpher-node)))
482     (elpher-visit-node node)))
483
484
485 ;;; Gopher selector retrieval (all kinds)
486 ;;
487
488 (defun elpher-process-cleanup ()
489   "Immediately shut down any extant elpher process."
490   (let ((p (get-process "elpher-process")))
491     (if p (delete-process p))))
492
493 (defvar elpher-use-tls nil
494   "If non-nil, use TLS to communicate with gopher servers.")
495
496 (defvar elpher-selector-string)
497
498 (defun elpher-get-selector (address after &optional propagate-error)
499   "Retrieve selector specified by ADDRESS, then execute AFTER.
500 The result is stored as a string in the variable â€˜elpher-selector-string’.
501
502 Usually errors result in an error page being displayed.  This is only
503 appropriate if the selector is to be directly viewed.  If PROPAGATE-ERROR
504 is non-nil, this message is not displayed.  Instead, the error propagates
505 up to the calling function."
506   (setq elpher-selector-string "")
507   (when (equal (elpher-address-protocol address) "gophers")
508       (if (gnutls-available-p)
509           (when (not elpher-use-tls)
510             (setq elpher-use-tls t)
511             (message "Engaging TLS mode."))
512         (error "Cannot retrieve TLS selector: GnuTLS not available")))
513   (condition-case the-error
514       (let* ((kill-buffer-query-functions nil)
515              (proc (open-network-stream "elpher-process"
516                                        nil
517                                        (elpher-address-host address)
518                                        (elpher-address-port address)
519                                        :type (if elpher-use-tls 'tls 'plain))))
520         (set-process-coding-system proc 'binary)
521         (set-process-filter proc
522                             (lambda (proc string)
523                               (setq elpher-selector-string
524                                     (concat elpher-selector-string string))))
525         (set-process-sentinel proc after)
526         (process-send-string proc
527                              (concat (elpher-gopher-address-selector address) "\n")))
528     (error
529      (if (and (consp the-error)
530               (eq (car the-error) 'gnutls-error)
531               (not (equal (elpher-address-protocol address) "gophers"))
532               (or elpher-auto-disengage-TLS
533                   (yes-or-no-p "Could not establish encrypted connection.  Disable TLS mode? ")))
534          (progn
535            (message "Disengaging TLS mode.")
536            (setq elpher-use-tls nil)
537            (elpher-get-selector address after))
538        (elpher-process-cleanup)
539        (if propagate-error
540            (error the-error)
541          (elpher-with-clean-buffer
542           (insert (propertize "\n---- ERROR -----\n\n" 'face 'error)
543                   "Failed to connect to " (elpher-address-to-url address) ".\n"
544                   (propertize "\n----------------\n\n" 'face 'error)
545                   "Press 'u' to return to the previous page.")))))))
546
547 ;; Index retrieval
548
549 (defun elpher-get-index-node ()
550   "Getter which retrieves the current node contents as an index."
551   (let* ((address (elpher-node-address elpher-current-node))
552          (content (elpher-get-cached-content address)))
553     (if content
554         (progn
555           (elpher-with-clean-buffer
556            (insert content)
557            (elpher-restore-pos)))
558       (elpher-with-clean-buffer
559        (insert "LOADING DIRECTORY... (use 'u' to cancel)"))
560       (elpher-get-selector address
561                            (lambda (proc event)
562                              (unless (string-prefix-p "deleted" event)
563                                (elpher-with-clean-buffer
564                                 (elpher-insert-index elpher-selector-string)
565                                 (elpher-restore-pos)
566                                 (elpher-cache-content
567                                  (elpher-node-address elpher-current-node)
568                                  (buffer-string)))))))))
569
570 ;; Text retrieval
571
572 (defconst elpher-url-regex
573   "\\([a-zA-Z]+\\)://\\([a-zA-Z0-9.\-]+\\|\[[a-zA-Z0-9:]+\]\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
574   "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
575
576 (defun elpher-make-node-from-matched-url (&optional string)
577   "Convert most recent `elpher-url-regex' match to a node.
578
579 If STRING is non-nil, this is given as an argument to all `match-string'
580 calls, as is necessary if the match is performed by `string-match'."
581   (let ((url (match-string 0 string))
582         (protocol (downcase (match-string 1 string))))
583     (if (or (string= protocol "gopher")
584             (string= protocol "gophers"))
585         (let* ((bare-host (match-string 2 string))
586                (host (if (string-prefix-p "[" bare-host)
587                          (substring bare-host 1 (- (length bare-host) 1))
588                        bare-host))
589                (port (if (> (length (match-string 3 string))  1)
590                          (string-to-number (substring (match-string 3 string) 1))
591                        70))
592                (type-and-selector (match-string 4 string))
593                (type (if (> (length type-and-selector) 1)
594                          (elt type-and-selector 1)
595                        ?1))
596                (selector (decode-coding-string
597                           (url-unhex-string
598                            (if (> (length type-and-selector) 1)
599                                (substring type-and-selector 2)
600                              "")) 'utf-8))
601                (use-tls (string= protocol "gophers"))
602                (address (elpher-make-gopher-address type selector host port use-tls)))
603           (elpher-make-node url address))
604       (let* ((host (match-string 2 string))
605              (port (if (> (length (match-string 3 string)) 1)
606                        (string-to-number (substring (match-string 3 string) 1))
607                      70))
608              (selector (concat "URL:" url))
609              (address (elpher-make-gopher-address ?h selector host port)))
610         (elpher-make-node url address)))))
611
612
613 (defun elpher-buttonify-urls (string)
614   "Turn substrings which look like urls in STRING into clickable buttons."
615   (with-temp-buffer
616     (insert string)
617     (goto-char (point-min))
618     (while (re-search-forward elpher-url-regex nil t)
619         (let ((node (elpher-make-node-from-matched-url)))
620           (make-text-button (match-beginning 0)
621                             (match-end 0)
622                             'elpher-node  node
623                             'action #'elpher-click-link
624                             'follow-link t
625                             'help-echo (elpher-node-button-help node))))
626     (buffer-string)))
627
628 (defun elpher-get-text-node ()
629   "Getter which retrieves the current node contents as a text document."
630   (let* ((address (elpher-node-address elpher-current-node))
631          (content (elpher-get-cached-content address)))
632     (if content
633         (progn
634           (elpher-with-clean-buffer
635            (insert content)
636            (elpher-restore-pos)))
637       (progn
638         (elpher-with-clean-buffer
639          (insert "LOADING TEXT... (use 'u' to cancel)"))
640         (elpher-get-selector address
641                               (lambda (proc event)
642                                 (unless (string-prefix-p "deleted" event)
643                                   (elpher-with-clean-buffer
644                                    (insert (elpher-buttonify-urls
645                                             (elpher-preprocess-text-response
646                                              elpher-selector-string)))
647                                    (elpher-restore-pos)
648                                    (elpher-cache-content
649                                     (elpher-node-address elpher-current-node)
650                                     (buffer-string))))))))))
651
652 ;; Image retrieval
653
654 (defun elpher-get-image-node ()
655   "Getter which retrieves the current node contents as an image to view."
656   (let* ((address (elpher-node-address elpher-current-node)))
657     (if (display-images-p)
658         (progn
659           (elpher-with-clean-buffer
660            (insert "LOADING IMAGE... (use 'u' to cancel)"))
661           (elpher-get-selector address
662                                (lambda (proc event)
663                                  (unless (string-prefix-p "deleted" event)
664                                    (let ((image (create-image
665                                                  elpher-selector-string
666                                                  nil t)))
667                                      (elpher-with-clean-buffer
668                                       (insert-image image)
669                                       (elpher-restore-pos)))))))
670       (elpher-get-node-download))))
671
672 ;; Search retrieval
673
674 (defun elpher-get-search-node ()
675   "Getter which submits a search query to the address of the current node."
676   (let* ((address (elpher-node-address elpher-current-node))
677          (content (elpher-get-cached-content address))
678          (aborted t))
679     (if content
680         (progn
681           (elpher-with-clean-buffer
682            (insert content)
683            (elpher-restore-pos))
684           (message "Displaying cached search results.  Reload to perform a new search."))
685       (unwind-protect
686           (let* ((query-string (read-string "Query: "))
687                  (query-selector (concat (elpher-gopher-address-selector address) "\t" query-string))
688                  (search-address (elpher-make-gopher-address ?1
689                                                       query-selector
690                                                       (elpher-address-host address)
691                                                       (elpher-address-port address))))
692             (setq aborted nil)
693             (elpher-with-clean-buffer
694              (insert "LOADING RESULTS... (use 'u' to cancel)"))
695             (elpher-get-selector search-address
696                                   (lambda (proc event)
697                                     (unless (string-prefix-p "deleted" event)
698                                       (elpher-with-clean-buffer
699                                        (elpher-insert-index elpher-selector-string))
700                                       (goto-char (point-min))
701                                       (elpher-cache-content
702                                        (elpher-node-address elpher-current-node)
703                                        (buffer-string))))))
704         (if aborted
705             (elpher-visit-parent-node))))))
706
707 ;; Raw server response retrieval
708
709 (defun elpher-get-node-raw ()
710   "Getter which retrieves the raw server response for the current node."
711   (let ((address (elpher-node-address elpher-current-node)))
712     (elpher-with-clean-buffer
713      (insert "LOADING RAW SERVER RESPONSE... (use 'u' to cancel)"))
714     (elpher-get-selector address
715                          (lambda (proc event)
716                            (unless (string-prefix-p "deleted" event)
717                              (elpher-with-clean-buffer
718                               (insert elpher-selector-string)
719                               (goto-char (point-min)))))))
720   (message "Displaying raw server response.  Reload or redraw to return to standard view."))
721  
722 ;; File export retrieval
723
724 (defvar elpher-download-filename)
725
726 (defun elpher-get-node-download ()
727   "Getter which retrieves the current node and writes the result to a file."
728   (let* ((address (elpher-node-address elpher-current-node))
729          (selector (elpher-gopher-address-selector address)))
730     (elpher-visit-parent-node) ; Do first in case of non-local exits.
731     (let* ((filename-proposal (file-name-nondirectory selector))
732            (filename (read-file-name "Save file as: "
733                                      nil nil nil
734                                      (if (> (length filename-proposal) 0)
735                                          filename-proposal
736                                        "gopher.file"))))
737       (message "Downloading...")
738       (setq elpher-download-filename filename)
739       (condition-case the-error
740           (elpher-get-selector address
741                                (lambda (proc event)
742                                  (let ((coding-system-for-write 'binary))
743                                    (with-temp-file elpher-download-filename
744                                      (insert elpher-selector-string)
745                                      (message (format "Download complate, saved to file %s."
746                                                       elpher-download-filename)))))
747                                t)
748         (error
749          (error "Error downloading %s" elpher-download-filename))))))
750
751 ;; URL retrieval
752
753 (defun elpher-insert-rendered-html (string)
754   "Use shr to insert rendered view of html STRING into current buffer."
755   (let ((dom (with-temp-buffer
756                (insert string)
757                (libxml-parse-html-region (point-min) (point-max)))))
758     (shr-insert-document dom)))
759
760 (defun elpher-get-url-node ()
761   "Getter which attempts to open the URL specified by the current node."
762   (let* ((address (elpher-node-address elpher-current-node))
763          (selector (elpher-gopher-address-selector address)))
764     (let ((url (elt (split-string selector "URL:") 1)))
765       (if url
766           (progn
767             (elpher-visit-parent-node) ; Do first in case of non-local exits.
768             (message "Opening URL...")
769             (if elpher-open-urls-with-eww
770                 (browse-web url)
771               (browse-url url)))
772         (let ((content (elpher-get-cached-content address)))
773           (if content
774               (progn
775                 (elpher-with-clean-buffer
776                  (insert content)
777                  (elpher-restore-pos)))
778             (elpher-with-clean-buffer
779              (insert "LOADING HTML... (use 'u' to cancel)"))
780             (elpher-get-selector address
781                                  (lambda (proc event)
782                                    (unless (string-prefix-p "deleted" event)
783                                      (elpher-with-clean-buffer
784                                       (elpher-insert-rendered-html elpher-selector-string)
785                                       (goto-char (point-min))
786                                       (elpher-cache-content
787                                        (elpher-node-address elpher-current-node)
788                                        (buffer-string))))))))))))
789
790 ;; Telnet node connection
791
792 (defun elpher-get-telnet-node ()
793   "Getter which opens a telnet connection to the server specified by the current node."
794   (let* ((address (elpher-node-address elpher-current-node))
795          (host (elpher-address-host address))
796          (port (elpher-address-port address)))
797     (elpher-visit-parent-node)
798     (telnet host port)))
799
800 ;; Start page node retrieval
801
802 (defun elpher-get-start-node ()
803   "Getter which displays the start page."
804   (elpher-with-clean-buffer
805    (insert "     --------------------------------------------\n"
806            "                Elpher Gopher Client             \n"
807            "                   version " elpher-version "\n"
808            "     --------------------------------------------\n"
809            "\n"
810            "Default bindings:\n"
811            "\n"
812            " - TAB/Shift-TAB: next/prev item on current page\n"
813            " - RET/mouse-1: open item under cursor\n"
814            " - m: select an item on current page by name (autocompletes)\n"
815            " - u: return to previous page\n"
816            " - o/O: visit different selector or the root menu of the current server\n"
817            " - g: go to a particular gopher address\n"
818            " - i/I: info on item under cursor or current page\n"
819            " - c/C: copy URL representation of item under cursor or current page\n"
820            " - a/A: bookmark the item under cursor or current page\n"
821            " - x/X: remove bookmark for item under cursor or current page\n"
822            " - B: visit the bookmarks page\n"
823            " - r: redraw current page (using cached contents if available)\n"
824            " - R: reload current page (regenerates cache)\n"
825            " - T: toggle TLS mode\n"
826            " - d/D: download item under cursor or current page\n"
827            " - .: display the raw server response for the current page\n"
828            " - S: set an explicit character coding system (default is to autodetect)\n"
829            "\n"
830            "Start your exploration of gopher space:\n")
831    (elpher-insert-index-record "Floodgap Systems Gopher Server"
832                                (elpher-make-gopher-address ?1 "" "gopher.floodgap.com" 70))
833    (insert "\n"
834            "Alternatively, select the following item and enter some search terms:\n")
835    (elpher-insert-index-record "Veronica-2 Gopher Search Engine"
836                                (elpher-make-gopher-address ?7 "/v2/vs" "gopher.floodgap.com" 70))
837    (insert "\n"
838            "** Refer to the ")
839    (let ((help-string "RET,mouse-1: Open Elpher info manual (if available)"))
840      (insert-text-button "Elpher info manual"
841                          'face 'link
842                          'action (lambda (button)
843                                    (interactive)
844                                    (info "(elpher)"))
845                          'follow-link t
846                          'help-echo help-string))
847    (insert " for the full documentation. **\n")
848    (insert (propertize
849             (concat "  (This should be available if you have installed Elpher using\n"
850                     "   MELPA. Otherwise you will have to install the manual yourself.)")
851             'face 'shadow))
852    (elpher-restore-pos)))
853
854 ;; Bookmarks page node retrieval
855
856 (defun elpher-get-bookmarks-node ()
857   "Getter to load and display the current bookmark list."
858   (elpher-with-clean-buffer
859    (insert "---- Bookmark list ----\n\n")
860    (let ((bookmarks (elpher-load-bookmarks)))
861      (if bookmarks
862          (dolist (bookmark bookmarks)
863            (let ((display-string (elpher-bookmark-display-string bookmark))
864                  (address (elpher-bookmark-address bookmark)))
865              (elpher-insert-index-record display-string address)))
866        (insert "No bookmarks found.\n")))
867    (insert "\n-----------------------\n\n"
868            "- u: return to previous page\n"
869            "- x: delete selected bookmark\n"
870            "- a: rename selected bookmark\n\n"
871            "Bookmarks are stored in the file "
872            (locate-user-emacs-file "elpher-bookmarks"))
873    (elpher-restore-pos)))
874   
875
876 ;;; Bookmarks
877 ;;
878
879 (defun elpher-make-bookmark (display-string url)
880   "Make an elpher bookmark.
881 DISPLAY-STRING determines how the bookmark will appear in the
882 bookmark list, while ADDRESS is the address of the entry."
883   (list display-string (elpher-address-to-url address)))
884   
885 (defun elpher-bookmark-display-string (bookmark)
886   "Get the display string of BOOKMARK."
887   (elt bookmark 0))
888
889 (defun elpher-set-bookmark-display-string (bookmark display-string)
890   "Set the display string of BOOKMARK to DISPLAY-STRING."
891   (setcar bookmark display-string))
892
893 (defun elpher-bookmark-url (bookmark)
894   "Get the address for BOOKMARK."
895   (elt bookmark 1))
896
897
898 (defun elpher-save-bookmarks (bookmarks)
899   "Record the bookmark list BOOKMARKS to the user's bookmark file.
900 Beware that this completely replaces the existing contents of the file."
901   (with-temp-file (locate-user-emacs-file "elpher2-bookmarks")
902     (erase-buffer)
903     (insert "; Elpher bookmarks file\n\n"
904             "; Bookmarks are stored as a list of (label URL) items.\n"
905             "; Feel free to edit by hand, but take care to ensure\n"
906             "; the list structure remains intact.\n\n")
907     (pp bookmarks (current-buffer))))
908
909 (defun elpher-load-bookmarks ()
910   "Get the list of bookmarks from the users's bookmark file."
911   (with-temp-buffer
912     (ignore-errors
913       (insert-file-contents (locate-user-emacs-file "elpher2-bookmarks"))
914       (goto-char (point-min))
915       (read (current-buffer)))))
916
917 (defun elpher-add-address-bookmark (address display-string)
918   "Save a bookmark for ADDRESS with label DISPLAY-STRING.)))
919 If ADDRESS is already bookmarked, update the label only."
920   (let ((bookmarks (elpher-load-bookmarks))
921         (url (elpher-address-to-url address)))
922     (let ((existing-bookmark (rassoc (list url) bookmarks)))
923       (if existing-bookmark
924           (elpher-set-bookmark-display-string existing-bookmark display-string)
925         (add-to-list 'bookmarks (elpher-make-bookmark display-string url))))
926     (elpher-save-bookmarks bookmarks)))
927
928 (defun elpher-remove-address-bookmark (address)
929   "Remove any bookmark to ADDRESS."
930   (let ((url (elpher-address-to-url address)))
931     (elpher-save-bookmarks
932      (seq-filter (lambda (bookmark)
933                    (not (equal (elpher-bookmark-url bookmark) url)))
934                  (elpher-load-bookmarks)))))
935
936 ;;; Interactive procedures
937 ;;
938
939 (defun elpher-next-link ()
940   "Move point to the next link on the current page."
941   (interactive)
942   (forward-button 1))
943
944 (defun elpher-prev-link ()
945   "Move point to the previous link on the current page."
946   (interactive)
947   (backward-button 1))
948
949 (defun elpher-follow-current-link ()
950   "Open the link or url at point."
951   (interactive)
952   (push-button))
953
954 (defun elpher-go ()
955   "Go to a particular gopher site read from the minibuffer.
956 The site may be specified via a URL or explicitly in terms of
957 host, selector and port."
958   (interactive)
959   (let ((node
960          (let ((host-or-url (read-string "Gopher host or URL: ")))
961            (if (string-match elpher-url-regex host-or-url)
962                (elpher-make-node-from-matched-url host-or-url)
963              (let ((selector (read-string "Selector (default none): " nil nil ""))
964                    (port-string (read-string "Port (default 70): " nil nil "70")))
965                (elpher-make-node (concat "gopher://" host-or-url
966                                          ":" port-string
967                                          "/1" selector)
968                                  (elpher-make-gopher-address ?1 selector host-or-url
969                                                              (string-to-number port-string))))))))
970     (switch-to-buffer "*elpher*")
971     (elpher-visit-node node)))
972
973 (defun elpher-go-current ()
974   "Go to a particular site read from the minibuffer, initialized with the current URL."
975   (interactive)
976   (let ((address (elpher-node-address elpher-current-node)))
977     (if (elpher-address-special-p address)
978         (error "Command not valid for this page")
979       (let ((url (read-string "URL: " (elpher-address-to-url address))))
980         (if (string-match elpher-url-regex url)
981             (let ((new-node (elpher-make-node-from-matched-url url)))
982               (unless (equal (elpher-node-address new-node) address)
983                 (elpher-visit-node new-node)))
984           (error "Could not parse URL %s" url))))))
985
986 (defun elpher-redraw ()
987   "Redraw current page."
988   (interactive)
989   (if elpher-current-node
990       (elpher-visit-node elpher-current-node)
991     (message "No current site.")))
992
993 (defun elpher-reload ()
994   "Reload current page."
995   (interactive)
996   (if elpher-current-node
997       (elpher-reload-current-node)
998     (message "No current site.")))
999
1000 (defun elpher-toggle-tls ()
1001   "Toggle TLS encryption mode."
1002   (interactive)
1003   (setq elpher-use-tls (not elpher-use-tls))
1004   (if elpher-use-tls
1005       (if (gnutls-available-p)
1006           (message "TLS mode enabled.  (Will not affect current page until reload.)")
1007         (setq elpher-use-tls nil)
1008         (error "Cannot enable TLS mode: GnuTLS not available"))
1009     (message "TLS mode disabled.  (Will not affect current page until reload.)")))
1010
1011 (defun elpher-view-raw ()
1012   "View raw server response for current page."
1013   (interactive)
1014   (if elpher-current-node
1015       (if (elpher-address-special-p (elpher-node-address elpher-current-node))
1016           (error "This page was not generated by a server")
1017         (elpher-visit-node elpher-current-node
1018                            #'elpher-get-node-raw))
1019     (message "No current site.")))
1020
1021 (defun elpher-back ()
1022   "Go to previous site."
1023   (interactive)
1024   (if (elpher-node-parent elpher-current-node)
1025       (elpher-visit-parent-node)
1026     (error "No previous site")))
1027
1028 (defun elpher-download ()
1029   "Download the link at point."
1030   (interactive)
1031   (let ((button (button-at (point))))
1032     (if button
1033         (let ((node (button-get button 'elpher-node)))
1034           (if (elpher-address-special-p (elpher-node-address node))
1035               (error "Cannot download this link")
1036             (elpher-visit-node (button-get button 'elpher-node)
1037                                #'elpher-get-node-download)))
1038       (error "No link selected"))))
1039
1040 (defun elpher-download-current ()
1041   "Download the current page."
1042   (interactive)
1043   (if (elpher-address-special-p (elpher-node-address elpher-current-node))
1044       (error "Cannot download this page")
1045     (elpher-visit-node (elpher-make-node
1046                         (elpher-node-display-string elpher-current-node)
1047                         (elpher-node-address elpher-current-node)
1048                         elpher-current-node)
1049                        #'elpher-get-node-download
1050                        t)))
1051
1052 (defun elpher-build-link-map ()
1053   "Build alist mapping link names to destination nodes in current buffer."
1054   (let ((link-map nil)
1055         (b (next-button (point-min) t)))
1056     (while b
1057       (add-to-list 'link-map (cons (button-label b) b))
1058       (setq b (next-button (button-start b))))
1059     link-map))
1060
1061 (defun elpher-jump ()
1062   "Select a directory entry by name.  Similar to the info browser (m)enu command."
1063   (interactive)
1064   (let* ((link-map (elpher-build-link-map)))
1065     (if link-map
1066         (let ((key (let ((completion-ignore-case t))
1067                      (completing-read "Directory item/link: "
1068                                       link-map nil t))))
1069           (if (and key (> (length key) 0))
1070               (let ((b (cdr (assoc key link-map))))
1071                 (goto-char (button-start b))
1072                 (button-activate b)))))))
1073
1074 (defun elpher-root-dir ()
1075   "Visit root of current server."
1076   (interactive)
1077   (let* ((address (elpher-node-address elpher-current-node))
1078          (host (elpher-address-host address)))
1079     (if host
1080         (let ((host (elpher-address-host address))
1081               (selector (elpher-gopher-address-selector address))
1082               (port (elpher-address-port address)))
1083           (if (> (length selector) 0)
1084               (let ((root-address (elpher-make-gopher-address ?1 "" host port)))
1085                 (elpher-visit-node
1086                  (elpher-make-node (concat "gopher://" host
1087                                            ":" (number-to-string port)
1088                                            "/1/")
1089                                    root-address)))
1090             (error "Already at root directory of current server")))
1091       (error "Command invalid for this page"))))
1092
1093 (defun elpher-bookmarks-current-p ()
1094   "Return non-nil if current node is a bookmarks page."
1095   (equal (elpher-address-type (elpher-node-address elpher-current-node))
1096          '(special bookmarks)))
1097
1098 (defun elpher-reload-bookmarks ()
1099   "Reload bookmarks if current node is a bookmarks page."
1100   (if (elpher-bookmarks-current-p)
1101       (elpher-reload-current-node)))
1102
1103 (defun elpher-bookmark-current ()
1104   "Bookmark the current node."
1105   (interactive)
1106   (let ((address (elpher-node-address elpher-current-node))
1107         (display-string (elpher-node-display-string elpher-current-node)))
1108     (if (not (elpher-address-special-p address))
1109         (let ((bookmark-display-string (read-string "Bookmark display string: "
1110                                                     display-string)))
1111           (elpher-add-address-bookmark address bookmark-display-string)
1112           (message "Bookmark added."))
1113       (error "Cannot bookmark %s" display-string))))
1114
1115 (defun elpher-bookmark-link ()
1116   "Bookmark the link at point."
1117   (interactive)
1118   (let ((button (button-at (point))))
1119     (if button
1120         (let* ((node (button-get button 'elpher-node))
1121                (address (elpher-node-address node))
1122                (display-string (elpher-node-display-string node)))
1123           (if (not (elpher-address-special-p address))
1124               (let ((bookmark-display-string (read-string "Bookmark display string: "
1125                                                           display-string)))
1126                 (elpher-add-address-bookmark address bookmark-display-string)
1127                 (elpher-reload-bookmarks)
1128                 (message "Bookmark added."))
1129             (error "Cannot bookmark %s" display-string)))
1130       (error "No link selected"))))
1131
1132 (defun elpher-unbookmark-current ()
1133   "Remove bookmark for the current node."
1134   (interactive)
1135   (let ((address (elpher-node-address elpher-current-node)))
1136     (unless (elpher-address-special-p address)
1137       (elpher-remove-address-bookmark address)
1138       (message "Bookmark removed."))))
1139
1140 (defun elpher-unbookmark-link ()
1141   "Remove bookmark for the link at point."
1142   (interactive)
1143   (let ((button (button-at (point))))
1144     (if button
1145         (let ((node (button-get button 'elpher-node)))
1146           (elpher-remove-address-bookmark (elpher-node-address node))
1147           (elpher-reload-bookmarks)
1148           (message "Bookmark removed."))
1149       (error "No link selected"))))
1150
1151 (defun elpher-bookmarks ()
1152   "Visit bookmarks page."
1153   (interactive)
1154   (switch-to-buffer "*elpher*")
1155   (elpher-visit-node
1156    (elpher-make-node "Bookmarks Page" (elpher-make-special-address 'bookmarks))))
1157
1158 (defun elpher-info-node (node)
1159   "Display information on NODE."
1160   (let ((display-string (elpher-node-display-string node))
1161         (address (elpher-node-address node)))
1162     (if (not (elpher-address-special-p address))
1163         (message "`%s' on %s port %s"
1164                 (elpher-gopher-address-selector address)
1165                 (elpher-address-host address)
1166                 (elpher-address-port address))
1167       (message "%s" display-string))))
1168
1169 (defun elpher-info-link ()
1170   "Display information on node corresponding to link at point."
1171   (interactive)
1172   (let ((button (button-at (point))))
1173     (if button
1174         (elpher-info-node (button-get button 'elpher-node))
1175       (error "No item selected"))))
1176   
1177 (defun elpher-info-current ()
1178   "Display information on current node."
1179   (interactive)
1180   (elpher-info-node elpher-current-node))
1181
1182 (defun elpher-copy-node-url (node)
1183   "Copy URL representation of address of NODE to `kill-ring'."
1184   (let ((address (elpher-node-address node)))
1185     (if (elpher-address-special-p address)
1186         (error (format "Cannot represent %s as URL" (elpher-node-display-string node)))
1187       (let ((url (elpher-address-to-url address)))
1188         (message "Copied \"%s\" to kill-ring/clipboard." url)
1189         (kill-new url)))))
1190
1191 (defun elpher-copy-link-url ()
1192   "Copy URL of item at point to `kill-ring'."
1193   (interactive)
1194   (let ((button (button-at (point))))
1195     (if button
1196         (elpher-copy-node-url (button-get button 'elpher-node))
1197       (error "No item selected"))))
1198
1199 (defun elpher-copy-current-url ()
1200   "Copy URL of current node to `kill-ring'."
1201   (interactive)
1202   (elpher-copy-node-url elpher-current-node))
1203
1204 (defun elpher-set-coding-system ()
1205   "Specify an explicit character coding system."
1206   (interactive)
1207   (let ((system (read-coding-system "Set coding system to use (default is to autodetect): " nil)))
1208     (setq elpher-user-coding-system system)
1209     (if system
1210         (message "Coding system fixed to %s. (Reload to see effect)." system)
1211       (message "Coding system set to autodetect. (Reload to see effect)."))))
1212
1213
1214 ;;; Mode and keymap
1215 ;;
1216
1217 (defvar elpher-mode-map
1218   (let ((map (make-sparse-keymap)))
1219     (define-key map (kbd "TAB") 'elpher-next-link)
1220     (define-key map (kbd "<backtab>") 'elpher-prev-link)
1221     (define-key map (kbd "u") 'elpher-back)
1222     (define-key map (kbd "O") 'elpher-root-dir)
1223     (define-key map (kbd "g") 'elpher-go)
1224     (define-key map (kbd "o") 'elpher-go-current)
1225     (define-key map (kbd "r") 'elpher-redraw)
1226     (define-key map (kbd "R") 'elpher-reload)
1227     (define-key map (kbd "T") 'elpher-toggle-tls)
1228     (define-key map (kbd ".") 'elpher-view-raw)
1229     (define-key map (kbd "d") 'elpher-download)
1230     (define-key map (kbd "D") 'elpher-download-current)
1231     (define-key map (kbd "m") 'elpher-jump)
1232     (define-key map (kbd "i") 'elpher-info-link)
1233     (define-key map (kbd "I") 'elpher-info-current)
1234     (define-key map (kbd "c") 'elpher-copy-link-url)
1235     (define-key map (kbd "C") 'elpher-copy-current-url)
1236     (define-key map (kbd "a") 'elpher-bookmark-link)
1237     (define-key map (kbd "A") 'elpher-bookmark-current)
1238     (define-key map (kbd "x") 'elpher-unbookmark-link)
1239     (define-key map (kbd "X") 'elpher-unbookmark-current)
1240     (define-key map (kbd "B") 'elpher-bookmarks)
1241     (define-key map (kbd "S") 'elpher-set-coding-system)
1242     (when (fboundp 'evil-define-key)
1243       (evil-define-key 'motion map
1244         (kbd "TAB") 'elpher-next-link
1245         (kbd "C-") 'elpher-follow-current-link
1246         (kbd "C-t") 'elpher-back
1247         (kbd "u") 'elpher-back
1248         (kbd "O") 'elpher-root-dir
1249         (kbd "g") 'elpher-go
1250         (kbd "o") 'elpher-go-current
1251         (kbd "r") 'elpher-redraw
1252         (kbd "R") 'elpher-reload
1253         (kbd "T") 'elpher-toggle-tls
1254         (kbd ".") 'elpher-view-raw
1255         (kbd "d") 'elpher-download
1256         (kbd "D") 'elpher-download-current
1257         (kbd "m") 'elpher-jump
1258         (kbd "i") 'elpher-info-link
1259         (kbd "I") 'elpher-info-current
1260         (kbd "c") 'elpher-copy-link-url
1261         (kbd "C") 'elpher-copy-current-url
1262         (kbd "a") 'elpher-bookmark-link
1263         (kbd "A") 'elpher-bookmark-current
1264         (kbd "x") 'elpher-unbookmark-link
1265         (kbd "X") 'elpher-unbookmark-current
1266         (kbd "B") 'elpher-bookmarks
1267         (kbd "S") 'elpher-set-coding-system))
1268     map)
1269   "Keymap for gopher client.")
1270
1271 (define-derived-mode elpher-mode special-mode "elpher"
1272   "Major mode for elpher, an elisp gopher client.
1273
1274 This mode is automatically enabled by the interactive
1275 functions which initialize the gopher client, namely
1276 `elpher', `elpher-go' and `elpher-bookmarks'.")
1277
1278 (when (fboundp 'evil-set-initial-state)
1279   (evil-set-initial-state 'elpher-mode 'motion))
1280
1281
1282 ;;; Main start procedure
1283 ;;
1284
1285 ;;;###autoload
1286 (defun elpher ()
1287   "Start elpher with default landing page."
1288   (interactive)
1289   (if (get-buffer "*elpher*")
1290       (switch-to-buffer "*elpher*")
1291     (switch-to-buffer "*elpher*")
1292     (setq elpher-current-node nil)
1293     (let ((start-node (elpher-make-node "Elpher Start Page"
1294                                         (elpher-make-special-address 'start))))
1295       (elpher-visit-node start-node)))
1296   "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
1297
1298 ;;; elpher.el ends here