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