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