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