Modified menu function prompt.
[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   (dolist (line (split-string string "\r\n"))
296     (unless (= (length line) 0)
297       (elpher-insert-index-record line))))
298
299 (defun elpher-insert-margin (&optional type-name)
300   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
301   (if type-name
302       (progn
303         (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
304                         (concat
305                          (propertize "[" 'face 'elpher-margin-brackets)
306                          (propertize type-name 'face 'elpher-margin-key)
307                          (propertize "]" 'face 'elpher-margin-brackets))))
308         (insert " "))
309     (insert (make-string elpher-margin-width ?\s))))
310
311 (defun elpher-insert-index-record (line)
312   "Insert the index record corresponding to LINE into the current buffer."
313   (let* ((type (elt line 0))
314          (fields (split-string (substring line 1) "\t"))
315          (display-string (elt fields 0))
316          (selector (elt fields 1))
317          (host (elt fields 2))
318          (port (elt fields 3))
319          (address (elpher-make-address selector host port))
320          (type-map-entry (alist-get type elpher-type-map)))
321     (if type-map-entry
322         (let ((getter (car type-map-entry))
323               (margin-code (cadr type-map-entry))
324               (face (caddr type-map-entry)))
325           (elpher-insert-margin margin-code)
326           (insert-text-button display-string
327                               'face face
328                               'elpher-node (elpher-make-node elpher-current-node
329                                                                address
330                                                                getter)
331                               'action #'elpher-click-link
332                               'follow-link t
333                               'help-echo (format "mouse-1, RET: open %s on %s port %s"
334                                                  selector host port)))
335       (pcase type
336         (?i (elpher-insert-margin) ; Information
337             (insert (propertize display-string
338                                 'face 'elpher-info)))
339         (?h (elpher-insert-margin "W") ; Web link
340             (let ((url (elt (split-string selector "URL:") 1)))
341               (insert-text-button display-string
342                                   'face 'elpher-url
343                                   'elpher-url url
344                                   'action #'elpher-click-url
345                                   'follow-link t
346                                   'help-echo (format "mouse-1, RET: open url %s" url))))
347         (?.) ; Occurs at end of index, can safely ignore.
348         (tp (elpher-insert-margin (concat (char-to-string tp) "?"))
349             (insert (propertize display-string
350                                 'face 'elpher-unknown-face)))))
351     (insert "\n")))
352
353
354 ;;; Selector retrieval (all kinds)
355 ;;
356
357 (defun elpher-process-cleanup ()
358   "Immediately shut down any extant elpher process."
359   (let ((p (get-process "elpher-process")))
360     (if p (delete-process p))))
361
362 (defvar elpher-selector-string)
363
364 (defun elpher-get-selector (address after)
365   "Retrieve selector specified by ADDRESS, then execute AFTER.
366 The result is stored as a string in the variable â€˜elpher-selector-string’."
367   (setq elpher-selector-string "")
368   (make-network-process
369    :name "elpher-process"
370    :host (elpher-address-host address)
371    :service (elpher-address-port address)
372    :filter (lambda (proc string)
373              (setq elpher-selector-string (concat elpher-selector-string string)))
374    :sentinel after)
375   (process-send-string "elpher-process"
376                        (concat (elpher-address-selector address) "\n")))
377
378 ;; Index retrieval
379
380 (defun elpher-get-index-node ()
381   "Getter which retrieves the current node contents as an index."
382   (let ((content (elpher-node-content elpher-current-node))
383         (address (elpher-node-address elpher-current-node)))
384     (if content
385         (progn
386           (elpher-with-clean-buffer
387            (insert content))
388           (elpher-restore-pos))
389       (if address
390           (progn
391             (elpher-with-clean-buffer
392              (insert "LOADING DIRECTORY..."))
393             (elpher-get-selector address
394                                   (lambda (proc event)
395                                     (unless (string-prefix-p "deleted" event)
396                                       (elpher-with-clean-buffer
397                                        (elpher-insert-index elpher-selector-string))
398                                       (elpher-restore-pos)
399                                       (elpher-set-node-content elpher-current-node
400                                                                 (buffer-string))))))
401         (progn
402           (elpher-with-clean-buffer
403            (elpher-insert-index elpher-start-index))
404           (elpher-restore-pos)
405           (elpher-set-node-content elpher-current-node
406                                     (buffer-string)))))))
407
408 ;; Text retrieval
409
410 (defconst elpher-url-regex
411   "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
412   "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
413
414 (defun elpher-buttonify-urls (string)
415   "Turn substrings which look like urls in STRING into clickable buttons."
416   (with-temp-buffer
417     (insert string)
418     (goto-char (point-min))
419     (while (re-search-forward elpher-url-regex nil t)
420       (let ((url (match-string 0))
421             (protocol (downcase (match-string 1))))
422         (if (string= protocol "gopher")
423             (let* ((host (match-string 2))
424                    (port 70)
425                    (type-and-selector (match-string 4))
426                    (type (if (> (length type-and-selector) 1)
427                              (elt type-and-selector 1)
428                            ?1))
429                    (selector (if (> (length type-and-selector) 1)
430                                  (substring type-and-selector 2)
431                                ""))
432                    (address (elpher-make-address selector host port))
433                    (getter (car (alist-get type elpher-type-map))))
434               (make-text-button (match-beginning 0)
435                                 (match-end 0)
436                                 'elpher-node (elpher-make-node elpher-current-node
437                                                                  address
438                                                                  getter)
439                                 'action #'elpher-click-link
440                                 'follow-link t
441                                 'help-echo (format "mouse-1, RET: open %s on %s port %s"
442                                                    selector host port)))
443           (make-text-button (match-beginning 0)
444                             (match-end 0)
445                             'elpher-url url
446                             'action #'elpher-click-url
447                             'follow-link t
448                             'help-echo (format "mouse-1, RET: open url %s" url)))))
449     (buffer-string)))
450
451 (defun elpher-process-text (string)
452   "Remove CRs and trailing period from the gopher text document STRING."
453   (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
454          (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
455     (elpher-buttonify-urls cleaned-str)))
456
457 (defun elpher-get-text-node ()
458   "Getter which retrieves the current node contents as a text document."
459   (let ((content (elpher-node-content elpher-current-node))
460         (address (elpher-node-address elpher-current-node)))
461     (if content
462         (progn
463           (elpher-with-clean-buffer
464            (insert content))
465           (elpher-restore-pos))
466       (progn
467         (elpher-with-clean-buffer
468          (insert "LOADING TEXT..."))
469         (elpher-get-selector address
470                               (lambda (proc event)
471                                 (unless (string-prefix-p "deleted" event)
472                                   (elpher-with-clean-buffer
473                                    (insert (elpher-process-text elpher-selector-string)))
474                                   (elpher-restore-pos)
475                                   (elpher-set-node-content elpher-current-node
476                                                             (buffer-string)))))))))
477
478 ;; Image retrieval
479
480 (defun elpher-get-image-node ()
481   "Getter which retrieves the current node contents as an image to view."
482   (let ((content (elpher-node-content elpher-current-node))
483         (address (elpher-node-address elpher-current-node)))
484     (if content
485         (progn
486           (elpher-with-clean-buffer
487            (insert-image content))
488           (setq cursor-type nil)
489           (elpher-restore-pos))
490       (if (display-images-p)
491           (progn
492             (elpher-with-clean-buffer
493              (insert "LOADING IMAGE..."))
494             (elpher-get-selector address
495                                  (lambda (proc event)
496                                    (unless (string-prefix-p "deleted" event)
497                                      (let ((image (create-image
498                                                    (encode-coding-string elpher-selector-string
499                                                                          'no-conversion)
500                                                    nil t)))
501                                        (elpher-with-clean-buffer
502                                         (insert-image image))
503                                        (setq cursor-type nil)
504                                        (elpher-restore-pos)
505                                        (if elpher-cache-images
506                                            (elpher-set-node-content elpher-current-node
507                                                                     image)))))))
508         (elpher-get-node-download)))))
509
510 ;; Search retrieval
511
512 (defun elpher-get-search-node ()
513   "Getter which submits a search query to the address of the current node."
514   (let ((content (elpher-node-content elpher-current-node))
515         (address (elpher-node-address elpher-current-node))
516         (aborted t))
517     (if content
518         (progn
519           (elpher-with-clean-buffer
520            (insert content))
521           (elpher-restore-pos)
522           (message "Displaying cached search results.  Reload to perform a new search."))
523       (unwind-protect
524           (let* ((query-string (read-string "Query: "))
525                  (query-selector (concat (elpher-address-selector address) "\t" query-string))
526                  (search-address (elpher-make-address query-selector
527                                                        (elpher-address-host address)
528                                                        (elpher-address-port address))))
529             (setq aborted nil)
530             (elpher-with-clean-buffer
531              (insert "LOADING RESULTS..."))
532             (elpher-get-selector search-address
533                                   (lambda (proc event)
534                                     (unless (string-prefix-p "deleted" event)
535                                       (elpher-with-clean-buffer
536                                        (elpher-insert-index elpher-selector-string))
537                                       (goto-char (point-min))
538                                       (elpher-set-node-content elpher-current-node
539                                                                 (buffer-string))))))
540         (if aborted
541             (elpher-visit-parent-node))))))
542
543 ;; Raw server response retrieval
544
545 (defun elpher-get-node-raw ()
546   "Getter which retrieves the raw server response for the current node."
547   (let* ((content (elpher-node-content elpher-current-node))
548          (address (elpher-node-address elpher-current-node)))
549     (elpher-with-clean-buffer
550      (insert "LOADING RAW SERVER RESPONSE..."))
551     (if address
552         (elpher-get-selector address
553                               (lambda (proc event)
554                                 (unless (string-prefix-p "deleted" event)
555                                   (elpher-with-clean-buffer
556                                    (insert elpher-selector-string))
557                                   (goto-char (point-min)))))
558       (progn
559         (elpher-with-clean-buffer
560          (insert elpher-start-index))
561         (goto-char (point-min)))))
562   (message "Displaying raw server response.  Reload or redraw to return to standard view."))
563  
564 ;; File export retrieval
565
566 (defvar elpher-download-filename)
567
568 (defun elpher-get-node-download ()
569   "Getter which retrieves the current node and writes the result to a file."
570   (let* ((address (elpher-node-address elpher-current-node))
571          (selector (elpher-address-selector address)))
572     (elpher-visit-parent-node) ; Do first in case of non-local exits.
573     (let* ((filename-proposal (file-name-nondirectory selector))
574            (filename (read-file-name "Save file as: "
575                                      nil nil nil
576                                      (if (> (length filename-proposal) 0)
577                                          filename-proposal
578                                        "gopher.file"))))
579       (message "Downloading...")
580       (setq elpher-download-filename filename)
581       (elpher-get-selector address
582                             (lambda (proc event)
583                               (let ((coding-system-for-write 'binary))
584                                 (with-temp-file elpher-download-filename
585                                   (insert elpher-selector-string)
586                                   (message (format "Download complate, saved to file %s."
587                                                    elpher-download-filename)))))))))
588
589
590 ;;; Navigation procedures
591 ;;
592
593 (defun elpher-next-link ()
594   "Move point to the next link on the current page."
595   (interactive)
596   (forward-button 1))
597
598 (defun elpher-prev-link ()
599   "Move point to the previous link on the current page."
600   (interactive)
601   (backward-button 1))
602
603 (defun elpher-click-link (button)
604   "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
605   (let ((node (button-get button 'elpher-node)))
606     (elpher-visit-node node)))
607
608 (defun elpher-click-url (button)
609   "Function called when the url link BUTTON is activated (via mouse or keypress)."
610   (let ((url (button-get button 'elpher-url)))
611     (if elpher-open-urls-with-eww
612         (browse-web url)
613       (browse-url url))))
614
615 (defun elpher-follow-current-link ()
616   "Open the link or url at point."
617   (interactive)
618   (push-button))
619
620 (defun elpher-go ()
621   "Go to a particular gopher site."
622   (interactive)
623   (let* (
624          (hostname (read-string "Gopher host: "))
625          (selector (read-string "Selector (default none): " nil nil ""))
626          (port (read-string "Port (default 70): " nil nil 70))
627          (address (list selector hostname port)))
628     (elpher-visit-node
629      (elpher-make-node elpher-current-node
630                         address
631                         #'elpher-get-index-node))))
632
633 (defun  elpher-redraw ()
634   "Redraw current page."
635   (interactive)
636   (elpher-visit-node elpher-current-node))
637
638 (defun  elpher-reload ()
639   "Reload current page."
640   (interactive)
641   (elpher-reload-current-node))
642
643 (defun elpher-view-raw ()
644   "View current page as plain text."
645   (interactive)
646   (elpher-visit-node elpher-current-node
647                       #'elpher-get-node-raw))
648
649 (defun elpher-back ()
650   "Go to previous site."
651   (interactive)
652   (if (elpher-node-parent elpher-current-node)
653       (elpher-visit-parent-node)
654     (message "No previous site.")))
655
656 (defun elpher-download ()
657   "Download the link at point."
658   (interactive)
659   (let ((button (button-at (point))))
660     (if button
661         (let ((node (button-get button 'elpher-node)))
662           (if node
663               (elpher-visit-node (button-get button 'elpher-node)
664                                  #'elpher-get-node-download)
665             (message "Can only download gopher links, not general URLs.")))
666       (message "No link selected."))))
667
668 (defun elpher-build-link-map ()
669   "Build alist mapping link names to destination nodes in current buffer."
670   (let ((link-map nil)
671         (b (next-button (point-min) t)))
672     (while b
673       (add-to-list 'link-map (cons (button-label b) b))
674       (setq b (next-button (button-start b))))
675     link-map))
676
677 (defun elpher-menu ()
678   "Select a directory entry by name.  Similar to the info browser (m)enu command."
679   (interactive)
680   (let* ((link-map (elpher-build-link-map)))
681     (if link-map
682         (let ((key (let ((completion-ignore-case t))
683                      (completing-read "Directory entry/link (tab to autocomplete): " link-map nil t))))
684           (if (and key (> (length key) 0))
685               (let ((b (cdr (assoc key link-map))))
686                 (goto-char (button-start b))
687                 (button-activate b)))))))
688
689 ;;; Mode and keymap
690 ;;
691
692 (defvar elpher-mode-map
693   (let ((map (make-sparse-keymap)))
694     (define-key map (kbd "TAB") 'elpher-next-link)
695     (define-key map (kbd "<backtab>") 'elpher-prev-link)
696     (define-key map (kbd "u") 'elpher-back)
697     (define-key map (kbd "g") 'elpher-go)
698     (define-key map (kbd "r") 'elpher-redraw)
699     (define-key map (kbd "R") 'elpher-reload)
700     (define-key map (kbd "w") 'elpher-view-raw)
701     (define-key map (kbd "d") 'elpher-download)
702     (define-key map (kbd "m") 'elpher-menu)
703     (when (fboundp 'evil-define-key)
704       (evil-define-key 'normal map
705         (kbd "TAB") 'elpher-next-link
706         (kbd "C-]") 'elpher-follow-current-link
707         (kbd "C-t") 'elpher-back
708         (kbd "u") 'elpher-back
709         (kbd "g") 'elpher-go
710         (kbd "r") 'elpher-redraw
711         (kbd "R") 'elpher-reload
712         (kbd "w") 'elpher-view-raw
713         (kbd "d") 'elpher-download
714         (kbd "m") 'elpher-menu))
715     map)
716   "Keymap for gopher client.")
717
718 (define-derived-mode elpher-mode special-mode "elpher"
719   "Major mode for elpher, an elisp gopher client.")
720
721
722 ;;; Main start procedure
723 ;;
724
725 ;;;###autoload
726 (defun elpher ()
727   "Start elpher with default landing page."
728   (interactive)
729   (setq elpher-current-node nil)
730   (let ((start-node (elpher-make-node nil
731                                       elpher-start-address
732                                       #'elpher-get-index-node)))
733     (elpher-visit-node start-node))
734   "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
735
736 ;;; elpher.el ends here