A few explanatory comments.
[elpher.git] / elpher.el
1 ;;; elpher.el --- Full-featured 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.0.0
8 ;; Keywords: comm gopher
9 ;; Homepage: https://github.com/tgvaughan/elpher
10 ;; Package-Requires: ((emacs "25"))
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 full-featured gopher client for GNU Emacs.
30 ;; It supports:
31
32 ;; - intuitive keyboard and mouse-driven browsing,
33 ;; - caching of visited sites (both content and cursor position),
34 ;; - pleasant and configurable colouring of Gopher directories,
35 ;; - direct visualisation of image files,
36 ;; - (m)enu key support, similar to Emacs' info browser,
37 ;; - clickable web and gopher links in plain text.
38
39 ;; The caching mechanism works by maintaining a hierarchy of visited
40 ;; pages rather than a linear history, meaning that it is quick and
41 ;; easy to navigate this history.
42
43 ;; To launch Elpher, simply use 'M-x elpher'.  This will open a start
44 ;; page containing information on key bindings and suggested starting
45 ;; points for your gopher exploration.
46
47 ;; Faces, caching options and start page can be configured via
48 ;; the Elpher customization group in Applications.
49
50 ;;; Code:
51
52 (provide 'elpher)
53
54 ;;; Global constants
55 ;;
56
57 (defconst elpher-version "1.0.0"
58   "Current version of elpher.")
59
60 (defconst elpher-margin-width 6
61   "Width of left-hand margin used when rendering indicies.")
62
63 (defconst elpher-start-index
64   (mapconcat
65    'identity
66    (list "i\tfake\tfake\t1"
67          "i--------------------------------------------\tfake\tfake\t1"
68          "i           Elpher Gopher Client             \tfake\tfake\t1"
69          (format "i              version %s\tfake\tfake\t1" elpher-version)
70          "i--------------------------------------------\tfake\tfake\t1"
71          "i\tfake\tfake\t1"
72          "iBasic usage:\tfake\tfake\t1"
73          "i\tfake\tfake\t1"
74          "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
75          "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
76          "i - m: select a directory entry by name (autocompletes)\tfake\tfake\t1"
77          "i - u: return to parent directory entry\tfake\tfake\t1"
78          "i - g: go to a particular page\tfake\tfake\t1"
79          "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
80          "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
81          "i - d: download directory entry under cursor\tfake\tfake\t1"
82          "i - w: display the raw server response for the current page\tfake\tfake\t1"
83          "i\tfake\tfake\t1"
84          "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
85          "i\tfake\tfake\t1"
86          "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
87          "i\tfake\tfake\t1"
88          "iAlternatively, select the following item and enter some\tfake\tfake\t1"
89          "isearch terms:\tfake\tfake\t1"
90          "i\tfake\tfake\t1"
91          "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
92          ".")
93    "\r\n")
94   "Source for elpher start page.")
95
96 (defconst elpher-type-map
97   '((?0 elpher-get-text-node "T" elpher-text)
98     (?1 elpher-get-index-node "/" elpher-index)
99     (?g elpher-get-image-node "im" elpher-image)
100     (?p elpher-get-image-node "im" elpher-image)
101     (?I elpher-get-image-node "im" elpher-image)
102     (?4 elpher-get-node-download "B" elpher-binary)
103     (?5 elpher-get-node-download "B" elpher-binary)
104     (?9 elpher-get-node-download "B" elpher-binary)
105     (?7 elpher-get-search-node "?" elpher-search))
106   "Association list from types to getters, margin codes and index faces.")
107
108
109 ;;; Customization group
110 ;;
111
112 (defgroup elpher nil
113   "A gopher client."
114   :group 'applications)
115
116 ;; Face customizations
117
118 (defface elpher-index
119   '((((background dark)) :foreground "deep sky blue")
120     (((background light)) :foreground "blue"))
121   "Face used for index records.")
122
123 (defface elpher-text
124   '((((background dark)) :foreground "white")
125     (((background light)) :weight bold))
126   "Face used for text records.")
127
128 (defface elpher-info '()
129   "Face used for info records.")
130
131 (defface elpher-image
132   '((((background dark)) :foreground "green")
133     (t :foreground "dark green"))
134   "Face used for image records.")
135
136 (defface elpher-search
137   '((((background light)) :foreground "orange")
138     (((background dark)) :foreground "dark orange"))
139   "Face used for search records.")
140
141 (defface elpher-url
142   '((((background dark)) :foreground "yellow")
143     (((background light)) :foreground "dark red"))
144   "Face used for url records.")
145
146 (defface elpher-binary
147   '((t :foreground "magenta"))
148   "Face used for binary records.")
149
150 (defface elpher-unknown
151   '((t :foreground "red"))
152   "Face used for unknown record types.")
153
154 (defface elpher-margin-key
155   '((((background dark)) :foreground "white"))
156   "Face used for margin key.")
157
158 (defface elpher-margin-brackets
159   '((t :foreground "blue"))
160   "Face used for brackets around margin key.")
161
162 ;; Other customizations
163
164 (defcustom elpher-open-urls-with-eww nil
165   "If non-nil, open URL selectors using eww.
166 Otherwise, use the system browser via the BROWSE-URL function."
167   :type '(boolean))
168
169 (defcustom elpher-cache-images nil
170   "If non-nil, cache images in memory in the same way as other content."
171   :type '(boolean))
172
173 (defcustom elpher-start-address nil
174   "If nil, the default start directory is shown when Elpher is started.
175 Otherwise, a list containing the selector, host and port of a directory to
176 use as the start page."
177   :type '(list string string integer))
178
179
180 ;;; Model
181 ;;
182
183 ;; Address
184
185 (defun elpher-make-address (selector host port)
186   "Create an address of a gopher object with SELECTOR, HOST and PORT."
187   (list selector host port))
188
189 (defun elpher-address-selector (address)
190   "Retrieve selector from ADDRESS."
191   (car address))
192
193 (defun elpher-address-host (address)
194   "Retrieve host from ADDRESS."
195   (cadr address))
196
197 (defun elpher-address-port (address)
198   "Retrieve port from ADDRESS."
199   (caddr address))
200
201 ;; Node
202
203 (defun elpher-make-node (parent address getter &optional content pos)
204   "Create a node in the gopher page hierarchy.
205
206 PARENT specifies the parent of the node, ADDRESS specifies the address of
207 the gopher page, GETTER provides the getter function used to obtain this
208 page.
209
210 The optional arguments CONTENT and POS can be used to fill the cached
211 content and cursor position fields of the node."
212   (list parent address getter content pos))
213
214 (defun elpher-node-parent (node)
215   "Retrieve the parent node of NODE."
216   (elt node 0))
217
218 (defun elpher-node-address (node)
219   "Retrieve the address of NODE."
220   (elt node 1))
221
222 (defun elpher-node-getter (node)
223   "Retrieve the preferred getter function of NODE."
224   (elt node 2))
225
226 (defun elpher-node-content (node)
227   "Retrieve the cached content of NODE, or nil if none exists."
228   (elt node 3))
229
230 (defun elpher-node-pos (node)
231   "Retrieve the cached cursor position for NODE, or nil if none exists."
232   (elt node 4))
233
234 (defun elpher-set-node-content (node content)
235   "Set the content cache of NODE to CONTENT."
236   (setcar (nthcdr 3 node) content))
237
238 (defun elpher-set-node-pos (node pos)
239   "Set the cursor position cache of NODE to POS."
240   (setcar (nthcdr 4 node) pos))
241
242 ;; Node graph traversal
243
244 (defvar elpher-current-node)
245
246 (defun elpher-visit-node (node &optional getter)
247   "Visit NODE using its own getter or GETTER, if non-nil."
248   (elpher-save-pos)
249   (elpher-process-cleanup)
250   (setq elpher-current-node node)
251   (if getter
252       (funcall getter)
253     (funcall (elpher-node-getter node))))
254
255 (defun elpher-visit-parent-node ()
256   "Visit the parent of the current node."
257   (let ((parent-node (elpher-node-parent elpher-current-node)))
258     (when parent-node
259       (elpher-visit-node parent-node))))
260       
261 (defun elpher-reload-current-node ()
262   "Reload the current node, discarding any existing cached content."
263   (elpher-set-node-content elpher-current-node nil)
264   (elpher-visit-node elpher-current-node))
265
266 (defun elpher-save-pos ()
267   "Save the current position of point to the current node."
268   (when elpher-current-node
269     (elpher-set-node-pos elpher-current-node (point))))
270
271 (defun elpher-restore-pos ()
272   "Restore the position of point to that cached in the current node."
273   (let ((pos (elpher-node-pos elpher-current-node)))
274     (if pos
275         (goto-char pos)
276       (goto-char (point-min)))))
277
278 ;;; Buffer preparation
279 ;;
280
281 (defmacro elpher-with-clean-buffer (&rest args)
282   "Evaluate ARGS with a clean *elpher* buffer as current."
283   (list 'progn
284         '(switch-to-buffer "*elpher*")
285         '(elpher-mode)
286         (append (list 'let '((inhibit-read-only t))
287                       '(erase-buffer))
288                 args)))
289
290 ;;; Index rendering
291 ;;
292
293 (defun elpher-insert-index (string)
294   "Insert the index corresponding to STRING into the current buffer."
295   ;; Should be able to split directly on CRLF, but some non-conformant
296   ;; LF-only servers sadly exist, hence the following.
297   (dolist (line (split-string (replace-regexp-in-string "\r" "" string) "\n"))
298     (unless (= (length line) 0)
299       (elpher-insert-index-record line))))
300
301 (defun elpher-insert-margin (&optional type-name)
302   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
303   (if type-name
304       (progn
305         (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
306                         (concat
307                          (propertize "[" 'face 'elpher-margin-brackets)
308                          (propertize type-name 'face 'elpher-margin-key)
309                          (propertize "]" 'face 'elpher-margin-brackets))))
310         (insert " "))
311     (insert (make-string elpher-margin-width ?\s))))
312
313 (defun elpher-insert-index-record (line)
314   "Insert the index record corresponding to LINE into the current buffer."
315   (let* ((type (elt line 0))
316          (fields (split-string (substring line 1) "\t"))
317          (display-string (elt fields 0))
318          (selector (elt fields 1))
319          (host (elt fields 2))
320          (port (elt fields 3))
321          (address (elpher-make-address selector host port))
322          (type-map-entry (alist-get type elpher-type-map)))
323     (if type-map-entry
324         (let ((getter (car type-map-entry))
325               (margin-code (cadr type-map-entry))
326               (face (caddr type-map-entry)))
327           (elpher-insert-margin margin-code)
328           (insert-text-button display-string
329                               'face face
330                               'elpher-node (elpher-make-node elpher-current-node
331                                                                address
332                                                                getter)
333                               'action #'elpher-click-link
334                               'follow-link t
335                               'help-echo (format "mouse-1, RET: open %s on %s port %s"
336                                                  selector host port)))
337       (pcase type
338         (?i (elpher-insert-margin) ;; Information
339             (insert (propertize display-string
340                                 'face 'elpher-info)))
341         (?h (elpher-insert-margin "W") ;; Web link
342             (let ((url (elt (split-string selector "URL:") 1)))
343               (insert-text-button display-string
344                                   'face 'elpher-url
345                                   'elpher-url url
346                                   'action #'elpher-click-url
347                                   'follow-link t
348                                   'help-echo (format "mouse-1, RET: open url %s" url))))
349         (?.) ;; Occurs at end of index, can safely ignore.
350         (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
351             (insert (propertize display-string
352                                 'face 'elpher-unknown-face)))))
353     (insert "\n")))
354
355
356 ;;; Selector retrieval (all kinds)
357 ;;
358
359 (defun elpher-process-cleanup ()
360   "Immediately shut down any extant elpher process."
361   (let ((p (get-process "elpher-process")))
362     (if p (delete-process p))))
363
364 (defvar elpher-selector-string)
365
366 (defun elpher-get-selector (address after)
367   "Retrieve selector specified by ADDRESS, then execute AFTER.
368 The result is stored as a string in the variable â€˜elpher-selector-string’."
369   (setq elpher-selector-string "")
370   (make-network-process
371    :name "elpher-process"
372    :host (elpher-address-host address)
373    :service (elpher-address-port address)
374    :filter (lambda (proc string)
375              (setq elpher-selector-string (concat elpher-selector-string string)))
376    :sentinel after)
377   (process-send-string "elpher-process"
378                        (concat (elpher-address-selector address) "\n")))
379
380 ;; Index retrieval
381
382 (defun elpher-get-index-node ()
383   "Getter which retrieves the current node contents as an index."
384   (let ((content (elpher-node-content elpher-current-node))
385         (address (elpher-node-address elpher-current-node)))
386     (if content
387         (progn
388           (elpher-with-clean-buffer
389            (insert content))
390           (elpher-restore-pos))
391       (if address
392           (progn
393             (elpher-with-clean-buffer
394              (insert "LOADING DIRECTORY..."))
395             (elpher-get-selector address
396                                   (lambda (proc event)
397                                     (unless (string-prefix-p "deleted" event)
398                                       (elpher-with-clean-buffer
399                                        (elpher-insert-index elpher-selector-string))
400                                       (elpher-restore-pos)
401                                       (elpher-set-node-content elpher-current-node
402                                                                 (buffer-string))))))
403         (progn
404           (elpher-with-clean-buffer
405            (elpher-insert-index elpher-start-index))
406           (elpher-restore-pos)
407           (elpher-set-node-content elpher-current-node
408                                     (buffer-string)))))))
409
410 ;; Text retrieval
411
412 (defconst elpher-url-regex
413   "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
414   "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
415
416 (defun elpher-buttonify-urls (string)
417   "Turn substrings which look like urls in STRING into clickable buttons."
418   (with-temp-buffer
419     (insert string)
420     (goto-char (point-min))
421     (while (re-search-forward elpher-url-regex nil t)
422       (let ((url (match-string 0))
423             (protocol (downcase (match-string 1))))
424         (if (string= protocol "gopher")
425             (let* ((host (match-string 2))
426                    (port 70)
427                    (type-and-selector (match-string 4))
428                    (type (if (> (length type-and-selector) 1)
429                              (elt type-and-selector 1)
430                            ?1))
431                    (selector (if (> (length type-and-selector) 1)
432                                  (substring type-and-selector 2)
433                                ""))
434                    (address (elpher-make-address selector host port))
435                    (getter (car (alist-get type elpher-type-map))))
436               (make-text-button (match-beginning 0)
437                                 (match-end 0)
438                                 'elpher-node (elpher-make-node elpher-current-node
439                                                                  address
440                                                                  getter)
441                                 'action #'elpher-click-link
442                                 'follow-link t
443                                 'help-echo (format "mouse-1, RET: open %s on %s port %s"
444                                                    selector host port)))
445           (make-text-button (match-beginning 0)
446                             (match-end 0)
447                             'elpher-url url
448                             'action #'elpher-click-url
449                             'follow-link t
450                             'help-echo (format "mouse-1, RET: open url %s" url)))))
451     (buffer-string)))
452
453 (defun elpher-process-text (string)
454   "Remove CRs and trailing period from the gopher text document STRING."
455   (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
456          (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
457     (elpher-buttonify-urls cleaned-str)))
458
459 (defun elpher-get-text-node ()
460   "Getter which retrieves the current node contents as a text document."
461   (let ((content (elpher-node-content elpher-current-node))
462         (address (elpher-node-address elpher-current-node)))
463     (if content
464         (progn
465           (elpher-with-clean-buffer
466            (insert content))
467           (elpher-restore-pos))
468       (progn
469         (elpher-with-clean-buffer
470          (insert "LOADING TEXT..."))
471         (elpher-get-selector address
472                               (lambda (proc event)
473                                 (unless (string-prefix-p "deleted" event)
474                                   (elpher-with-clean-buffer
475                                    (insert (elpher-process-text elpher-selector-string)))
476                                   (elpher-restore-pos)
477                                   (elpher-set-node-content elpher-current-node
478                                                             (buffer-string)))))))))
479
480 ;; Image retrieval
481
482 (defun elpher-get-image-node ()
483   "Getter which retrieves the current node contents as an image to view."
484   (let ((content (elpher-node-content elpher-current-node))
485         (address (elpher-node-address elpher-current-node)))
486     (if content
487         (progn
488           (elpher-with-clean-buffer
489            (insert-image content))
490           (setq cursor-type nil)
491           (elpher-restore-pos))
492       (if (display-images-p)
493           (progn
494             (elpher-with-clean-buffer
495              (insert "LOADING IMAGE..."))
496             (elpher-get-selector address
497                                  (lambda (proc event)
498                                    (unless (string-prefix-p "deleted" event)
499                                      (let ((image (create-image
500                                                    (encode-coding-string elpher-selector-string
501                                                                          'no-conversion)
502                                                    nil t)))
503                                        (elpher-with-clean-buffer
504                                         (insert-image image))
505                                        (setq cursor-type nil)
506                                        (elpher-restore-pos)
507                                        (if elpher-cache-images
508                                            (elpher-set-node-content elpher-current-node
509                                                                     image)))))))
510         (elpher-get-node-download)))))
511
512 ;; Search retrieval
513
514 (defun elpher-get-search-node ()
515   "Getter which submits a search query to the address of the current node."
516   (let ((content (elpher-node-content elpher-current-node))
517         (address (elpher-node-address elpher-current-node))
518         (aborted t))
519     (if content
520         (progn
521           (elpher-with-clean-buffer
522            (insert content))
523           (elpher-restore-pos)
524           (message "Displaying cached search results.  Reload to perform a new search."))
525       (unwind-protect
526           (let* ((query-string (read-string "Query: "))
527                  (query-selector (concat (elpher-address-selector address) "\t" query-string))
528                  (search-address (elpher-make-address query-selector
529                                                        (elpher-address-host address)
530                                                        (elpher-address-port address))))
531             (setq aborted nil)
532             (elpher-with-clean-buffer
533              (insert "LOADING RESULTS..."))
534             (elpher-get-selector search-address
535                                   (lambda (proc event)
536                                     (unless (string-prefix-p "deleted" event)
537                                       (elpher-with-clean-buffer
538                                        (elpher-insert-index elpher-selector-string))
539                                       (goto-char (point-min))
540                                       (elpher-set-node-content elpher-current-node
541                                                                 (buffer-string))))))
542         (if aborted
543             (elpher-visit-parent-node))))))
544
545 ;; Raw server response retrieval
546
547 (defun elpher-get-node-raw ()
548   "Getter which retrieves the raw server response for the current node."
549   (let* ((content (elpher-node-content elpher-current-node))
550          (address (elpher-node-address elpher-current-node)))
551     (elpher-with-clean-buffer
552      (insert "LOADING RAW SERVER RESPONSE..."))
553     (if address
554         (elpher-get-selector address
555                               (lambda (proc event)
556                                 (unless (string-prefix-p "deleted" event)
557                                   (elpher-with-clean-buffer
558                                    (insert elpher-selector-string))
559                                   (goto-char (point-min)))))
560       (progn
561         (elpher-with-clean-buffer
562          (insert elpher-start-index))
563         (goto-char (point-min)))))
564   (message "Displaying raw server response.  Reload or redraw to return to standard view."))
565  
566 ;; File export retrieval
567
568 (defvar elpher-download-filename)
569
570 (defun elpher-get-node-download ()
571   "Getter which retrieves the current node and writes the result to a file."
572   (let* ((address (elpher-node-address elpher-current-node))
573          (selector (elpher-address-selector address)))
574     (elpher-visit-parent-node) ; Do first in case of non-local exits.
575     (let* ((filename-proposal (file-name-nondirectory selector))
576            (filename (read-file-name "Save file as: "
577                                      nil nil nil
578                                      (if (> (length filename-proposal) 0)
579                                          filename-proposal
580                                        "gopher.file"))))
581       (message "Downloading...")
582       (setq elpher-download-filename filename)
583       (elpher-get-selector address
584                             (lambda (proc event)
585                               (let ((coding-system-for-write 'binary))
586                                 (with-temp-file elpher-download-filename
587                                   (insert elpher-selector-string)
588                                   (message (format "Download complate, saved to file %s."
589                                                    elpher-download-filename)))))))))
590
591
592 ;;; Navigation procedures
593 ;;
594
595 (defun elpher-next-link ()
596   "Move point to the next link on the current page."
597   (interactive)
598   (forward-button 1))
599
600 (defun elpher-prev-link ()
601   "Move point to the previous link on the current page."
602   (interactive)
603   (backward-button 1))
604
605 (defun elpher-click-link (button)
606   "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
607   (let ((node (button-get button 'elpher-node)))
608     (elpher-visit-node node)))
609
610 (defun elpher-click-url (button)
611   "Function called when the url link BUTTON is activated (via mouse or keypress)."
612   (let ((url (button-get button 'elpher-url)))
613     (if elpher-open-urls-with-eww
614         (browse-web url)
615       (browse-url url))))
616
617 (defun elpher-follow-current-link ()
618   "Open the link or url at point."
619   (interactive)
620   (push-button))
621
622 (defun elpher-go ()
623   "Go to a particular gopher site."
624   (interactive)
625   (let* (
626          (hostname (read-string "Gopher host: "))
627          (selector (read-string "Selector (default none): " nil nil ""))
628          (port (read-string "Port (default 70): " nil nil 70))
629          (address (list selector hostname port)))
630     (elpher-visit-node
631      (elpher-make-node elpher-current-node
632                         address
633                         #'elpher-get-index-node))))
634
635 (defun  elpher-redraw ()
636   "Redraw current page."
637   (interactive)
638   (elpher-visit-node elpher-current-node))
639
640 (defun  elpher-reload ()
641   "Reload current page."
642   (interactive)
643   (elpher-reload-current-node))
644
645 (defun elpher-view-raw ()
646   "View current page as plain text."
647   (interactive)
648   (elpher-visit-node elpher-current-node
649                       #'elpher-get-node-raw))
650
651 (defun elpher-back ()
652   "Go to previous site."
653   (interactive)
654   (if (elpher-node-parent elpher-current-node)
655       (elpher-visit-parent-node)
656     (message "No previous site.")))
657
658 (defun elpher-download ()
659   "Download the link at point."
660   (interactive)
661   (let ((button (button-at (point))))
662     (if button
663         (let ((node (button-get button 'elpher-node)))
664           (if node
665               (elpher-visit-node (button-get button 'elpher-node)
666                                  #'elpher-get-node-download)
667             (message "Can only download gopher links, not general URLs.")))
668       (message "No link selected."))))
669
670 (defun elpher-build-link-map ()
671   "Build alist mapping link names to destination nodes in current buffer."
672   (let ((link-map nil)
673         (b (next-button (point-min) t)))
674     (while b
675       (add-to-list 'link-map (cons (button-label b) b))
676       (setq b (next-button (button-start b))))
677     link-map))
678
679 (defun elpher-menu ()
680   "Select a directory entry by name.  Similar to the info browser (m)enu command."
681   (interactive)
682   (let* ((link-map (elpher-build-link-map)))
683     (if link-map
684         (let ((key (let ((completion-ignore-case t))
685                      (completing-read "Directory entry/link (tab to autocomplete): " link-map nil t))))
686           (if (and key (> (length key) 0))
687               (let ((b (cdr (assoc key link-map))))
688                 (goto-char (button-start b))
689                 (button-activate b)))))))
690
691 ;;; Mode and keymap
692 ;;
693
694 (defvar elpher-mode-map
695   (let ((map (make-sparse-keymap)))
696     (define-key map (kbd "TAB") 'elpher-next-link)
697     (define-key map (kbd "<backtab>") 'elpher-prev-link)
698     (define-key map (kbd "u") 'elpher-back)
699     (define-key map (kbd "g") 'elpher-go)
700     (define-key map (kbd "r") 'elpher-redraw)
701     (define-key map (kbd "R") 'elpher-reload)
702     (define-key map (kbd "w") 'elpher-view-raw)
703     (define-key map (kbd "d") 'elpher-download)
704     (define-key map (kbd "m") 'elpher-menu)
705     (when (fboundp 'evil-define-key)
706       (evil-define-key 'normal map
707         (kbd "TAB") 'elpher-next-link
708         (kbd "C-]") 'elpher-follow-current-link
709         (kbd "C-t") 'elpher-back
710         (kbd "u") 'elpher-back
711         (kbd "g") 'elpher-go
712         (kbd "r") 'elpher-redraw
713         (kbd "R") 'elpher-reload
714         (kbd "w") 'elpher-view-raw
715         (kbd "d") 'elpher-download
716         (kbd "m") 'elpher-menu))
717     map)
718   "Keymap for gopher client.")
719
720 (define-derived-mode elpher-mode special-mode "elpher"
721   "Major mode for elpher, an elisp gopher client.")
722
723
724 ;;; Main start procedure
725 ;;
726
727 ;;;###autoload
728 (defun elpher ()
729   "Start elpher with default landing page."
730   (interactive)
731   (setq elpher-current-node nil)
732   (let ((start-node (elpher-make-node nil
733                                       elpher-start-address
734                                       #'elpher-get-index-node)))
735     (elpher-visit-node start-node))
736   "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
737
738 ;;; elpher.el ends here