1 ;;; elpher.el --- Full-featured gopher client.
3 ;; Copyright (C) 2019 Tim Vaughan
5 ;; Author: Tim Vaughan <tgvaughan@gmail.com>
6 ;; Created: 11 April 2019
8 ;; Keywords: comm gopher
9 ;; Homepage: https://github.com/tgvaughan/elpher
10 ;; Package-Requires: ((emacs "25"))
12 ;; This file is not part of GNU Emacs.
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.
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.
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/>.
29 ;; Elpher aims to provide a full-featured gopher client for GNU Emacs.
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.
39 ;; Visited pages are stored as a hierarchy rather than a linear history,
40 ;; meaning that navigation between these pages is quick and easy.
42 ;; To launch Elpher, simply use 'M-x elpher'. This will open a start
43 ;; page containing information on key bindings and suggested starting
44 ;; points for your gopher exploration.
46 ;; Faces, caching options and start page can be configured via
47 ;; the Elpher customization group in Applications.
58 (defconst elpher-version "1.1.0"
59 "Current version of elpher.")
61 (defconst elpher-margin-width 6
62 "Width of left-hand margin used when rendering indicies.")
64 (defconst elpher-start-index
67 (list "i\tfake\tfake\t1"
68 "i --------------------------------------------\tfake\tfake\t1"
69 "i Elpher Gopher Client \tfake\tfake\t1"
70 (format "i version %s\tfake\tfake\t1" elpher-version)
71 "i --------------------------------------------\tfake\tfake\t1"
73 "iUsage:\tfake\tfake\t1"
75 "i - tab/shift-tab: next/prev item on current page\tfake\tfake\t1"
76 "i - RET/mouse-1: open item under cursor\tfake\tfake\t1"
77 "i - m: select an item on current page by name (autocompletes)\tfake\tfake\t1"
78 "i - u: return to parent\tfake\tfake\t1"
79 "i - O: visit the root menu of the current server\tfake\tfake\t1"
80 "i - g: go to a particular menu or item\tfake\tfake\t1"
81 "i - i/I: info on item under cursor or current page\tfake\tfake\t1"
82 "i - r: redraw current page (using cached contents if available)\tfake\tfake\t1"
83 "i - R: reload current page (regenerates cache)\tfake\tfake\t1"
84 "i - d: download directory entry under cursor\tfake\tfake\t1"
85 "i - w: display the raw server response for the current page\tfake\tfake\t1"
87 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
89 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
91 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
92 "isearch terms:\tfake\tfake\t1"
94 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
97 "Source for elpher start page.")
99 (defconst elpher-type-map
100 '((?0 elpher-get-text-node "T" elpher-text)
101 (?1 elpher-get-index-node "/" elpher-index)
102 (?4 elpher-get-node-download "B" elpher-binary)
103 (?5 elpher-get-node-download "B" elpher-binary)
104 (?7 elpher-get-search-node "?" elpher-search)
105 (?8 elpher-get-telnet-node "?" elpher-telnet)
106 (?9 elpher-get-node-download "B" elpher-binary)
107 (?g elpher-get-image-node "im" elpher-image)
108 (?p elpher-get-image-node "im" elpher-image)
109 (?I elpher-get-image-node "im" elpher-image)
110 (?h elpher-get-url-node "W" elpher-url))
111 "Association list from types to getters, margin codes and index faces.")
114 ;;; Customization group
119 :group 'applications)
121 ;; Face customizations
123 (defface elpher-index
124 '((t :inherit org-drawer))
125 "Face used for directory type directory records.")
128 '((t :inherit org-tag))
129 "Face used for text type directory records.")
132 '((t :inherit org-default))
133 "Face used for info type directory records.")
135 (defface elpher-image
136 '((t :inherit org-level-4))
137 "Face used for image type directory records.")
139 (defface elpher-search
140 '((t :inherit org-level-5))
141 "Face used for search type directory records.")
144 '((t :inherit org-level-6))
145 "Face used for url type directory records.")
147 (defface elpher-telnet
148 '((t :inherit org-level-6))
149 "Face used for telnet type directory records.")
151 (defface elpher-binary
152 '((t :inherit org-level-7))
153 "Face used for binary type directory records.")
155 (defface elpher-unknown
156 '((t :inherit org-warning))
157 "Face used for directory records with unknown/unsupported types.")
159 (defface elpher-margin-key
160 '((t :inherit org-tag))
161 "Face used for directory margin key.")
163 (defface elpher-margin-brackets
164 '((t :inherit org-special-keyword))
165 "Face used for brackets around directory margin key.")
167 ;; Other customizations
169 (defcustom elpher-open-urls-with-eww nil
170 "If non-nil, open URL selectors using eww.
171 Otherwise, use the system browser via the BROWSE-URL function."
174 (defcustom elpher-buttonify-urls-in-directories nil
175 "If non-nil, turns URLs matched in directories into clickable buttons."
178 (defcustom elpher-cache-images nil
179 "If non-nil, cache images in memory in the same way as other content."
182 (defcustom elpher-start-address nil
183 "If nil, the default start directory is shown when Elpher is started.
184 Otherwise, a list containing the selector, host and port of a directory to
185 use as the start page."
186 :type '(list string string integer))
188 (defcustom elpher-use-header t
189 "If non-nil, display current node information in buffer header."
197 (defun elpher-make-address (type selector host port)
198 "Create an address of a gopher object with TYPE, SELECTOR, HOST and PORT."
199 (list type selector host port))
201 (defun elpher-address-type (address)
202 "Retrieve type from ADDRESS."
205 (defun elpher-address-selector (address)
206 "Retrieve selector from ADDRESS."
209 (defun elpher-address-host (address)
210 "Retrieve host from ADDRESS."
213 (defun elpher-address-port (address)
214 "Retrieve port from ADDRESS."
219 (defun elpher-make-node (display-string parent address &optional content pos)
220 "Create a node in the gopher page hierarchy.
222 DISPLAY-STRING records the display string used for the page.
224 PARENT specifies the parent of the node, and ADDRESS specifies the
225 address of the gopher page.
227 The optional arguments CONTENT and POS can be used to fill the cached
228 content and cursor position fields of the node."
229 (list display-string parent address content pos))
231 (defun elpher-node-display-string (node)
232 "Retrieve the display string of NODE."
235 (defun elpher-node-parent (node)
236 "Retrieve the parent node of NODE."
239 (defun elpher-node-address (node)
240 "Retrieve the address of NODE."
243 (defun elpher-node-content (node)
244 "Retrieve the cached content of NODE, or nil if none exists."
247 (defun elpher-node-pos (node)
248 "Retrieve the cached cursor position for NODE, or nil if none exists."
251 (defun elpher-set-node-content (node content)
252 "Set the content cache of NODE to CONTENT."
253 (setcar (nthcdr 3 node) content))
255 (defun elpher-set-node-pos (node pos)
256 "Set the cursor position cache of NODE to POS."
257 (setcar (nthcdr 4 node) pos))
259 ;; Node graph traversal
261 (defvar elpher-current-node nil)
263 (defun elpher-visit-node (node &optional getter)
264 "Visit NODE using its own getter or GETTER, if non-nil."
266 (elpher-process-cleanup)
267 (setq elpher-current-node node)
268 (with-current-buffer "*elpher*"
269 (setq header-line-format "hello"))
270 ;; (let ((inhibit-read-only t))
272 ;; (force-mode-line-update))
275 (let* ((address (elpher-node-address node))
277 (elpher-address-type address)
279 (funcall (car (alist-get type elpher-type-map))))))
281 (defun elpher-visit-parent-node ()
282 "Visit the parent of the current node."
283 (let ((parent-node (elpher-node-parent elpher-current-node)))
285 (elpher-visit-node parent-node))))
287 (defun elpher-reload-current-node ()
288 "Reload the current node, discarding any existing cached content."
289 (elpher-set-node-content elpher-current-node nil)
290 (elpher-visit-node elpher-current-node))
292 (defun elpher-save-pos ()
293 "Save the current position of point to the current node."
294 (when elpher-current-node
295 (elpher-set-node-pos elpher-current-node (point))))
297 (defun elpher-restore-pos ()
298 "Restore the position of point to that cached in the current node."
299 (let ((pos (elpher-node-pos elpher-current-node)))
302 (goto-char (point-min)))))
305 ;;; Buffer preparation
308 (defun elpher-update-header ()
309 "If `elpher-use-header' is true, display current node info in window header."
310 (if elpher-use-header
311 (setq header-line-format (elpher-node-display-string elpher-current-node))))
313 (defmacro elpher-with-clean-buffer (&rest args)
314 "Evaluate ARGS with a clean *elpher* buffer as current."
315 (list 'with-current-buffer "*elpher*"
317 (append (list 'let '((inhibit-read-only t))
319 '(elpher-update-header))
326 (defun elpher-insert-index (string)
327 "Insert the index corresponding to STRING into the current buffer."
328 ;; Should be able to split directly on CRLF, but some non-conformant
329 ;; LF-only servers sadly exist, hence the following.
330 (let* ((str-no-period (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
331 (str-no-cr (replace-regexp-in-string "\r" "" str-no-period)))
332 (dolist (line (split-string str-no-cr "\n"))
333 (unless (= (length line) 0)
334 (elpher-insert-index-record line)))))
336 (defun elpher-insert-margin (&optional type-name)
337 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
340 (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s")
342 (propertize "[" 'face 'elpher-margin-brackets)
343 (propertize type-name 'face 'elpher-margin-key)
344 (propertize "]" 'face 'elpher-margin-brackets))))
346 (insert (make-string elpher-margin-width ?\s))))
348 (defun elpher-node-button-help (node)
349 "Return a string containing the help text for a button corresponding to NODE."
350 (let ((address (elpher-node-address node)))
351 (if (eq (elpher-address-type address) ?h)
352 (let ((url (cadr (split-string (elpher-address-selector address) "URL:"))))
353 (format "mouse-1, RET: open url '%s'" url))
354 (format "mouse-1, RET: open '%s' on %s port %s"
355 (elpher-address-selector address)
356 (elpher-address-host address)
357 (elpher-address-port address)))))
359 (defun elpher-insert-index-record (line)
360 "Insert the index record corresponding to LINE into the current buffer."
361 (let* ((type (elt line 0))
362 (fields (split-string (substring line 1) "\t"))
363 (display-string (elt fields 0))
364 (selector (elt fields 1))
365 (host (elt fields 2))
366 (port (string-to-number (elt fields 3))))
367 (elpher-insert-index-record-helper display-string type selector host port)))
369 (defun elpher-insert-index-record-helper (display-string type selector host port)
370 "Helper function to insert an index record into the current buffer.
371 The contents of the record are dictated by TYPE, DISPLAY-STRING, SELECTOR, HOST
374 This function is essentially the second half of `elpher-insert-index-record',
375 but broken out so that it can be used elsewhere."
376 (let ((address (elpher-make-address type selector host port))
377 (type-map-entry (alist-get type elpher-type-map)))
379 (let* ((margin-code (cadr type-map-entry))
380 (face (caddr type-map-entry))
381 (node (elpher-make-node display-string elpher-current-node address)))
382 (elpher-insert-margin margin-code)
383 (insert-text-button display-string
386 'action #'elpher-click-link
388 'help-echo (elpher-node-button-help node)))
391 (elpher-insert-margin)
393 (if elpher-buttonify-urls-in-directories
394 (elpher-buttonify-urls display-string)
396 'face 'elpher-info)))
398 (elpher-insert-margin (concat (char-to-string type) "?"))
399 (insert (propertize display-string
400 'face 'elpher-unknown-face)))))
403 (defun elpher-click-link (button)
404 "Function called when the gopher link BUTTON is activated (via mouse or keypress)."
405 (let ((node (button-get button 'elpher-node)))
406 (elpher-visit-node node)))
409 ;;; Selector retrieval (all kinds)
412 (defun elpher-process-cleanup ()
413 "Immediately shut down any extant elpher process."
414 (let ((p (get-process "elpher-process")))
415 (if p (delete-process p))))
417 (defvar elpher-selector-string)
419 (defun elpher-get-selector (address after)
420 "Retrieve selector specified by ADDRESS, then execute AFTER.
421 The result is stored as a string in the variable ‘elpher-selector-string’."
422 (setq elpher-selector-string "")
423 (make-network-process
424 :name "elpher-process"
425 :host (elpher-address-host address)
426 :service (elpher-address-port address)
427 :filter (lambda (proc string)
428 (setq elpher-selector-string (concat elpher-selector-string string)))
430 (process-send-string "elpher-process"
431 (concat (elpher-address-selector address) "\n")))
435 (defun elpher-get-index-node ()
436 "Getter which retrieves the current node contents as an index."
437 (let ((content (elpher-node-content elpher-current-node))
438 (address (elpher-node-address elpher-current-node)))
441 (elpher-with-clean-buffer
443 (elpher-restore-pos)))
446 (elpher-with-clean-buffer
447 (insert "LOADING DIRECTORY..."))
448 (elpher-get-selector address
450 (unless (string-prefix-p "deleted" event)
451 (elpher-with-clean-buffer
452 (elpher-insert-index elpher-selector-string)
454 (elpher-set-node-content elpher-current-node
455 (buffer-string)))))))
457 (elpher-with-clean-buffer
458 (elpher-insert-index elpher-start-index)
460 (elpher-set-node-content elpher-current-node
461 (buffer-string))))))))
465 (defconst elpher-url-regex
466 "\\([a-zA-Z]+\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
467 "Regexp used to locate and buttinofy URLs in text files loaded by elpher.")
469 (defun elpher-make-node-from-matched-url (parent &optional string)
470 "Convert most recent `elpher-url-regex' match to a node.
472 PARENT defines the node to set as the parent to the new node.
474 If STRING is non-nil, this is given as an argument to all `match-string'
475 calls, as is necessary if the match is performed by `string-match'."
476 (let ((url (match-string 0 string))
477 (protocol (downcase (match-string 1 string))))
478 (if (string= protocol "gopher")
479 (let* ((host (match-string 2 string))
480 (port (if (> (length (match-string 3 string)) 1)
481 (string-to-number (substring (match-string 3 string) 1))
483 (type-and-selector (match-string 4 string))
484 (type (if (> (length type-and-selector) 1)
485 (elt type-and-selector 1)
487 (selector (if (> (length type-and-selector) 1)
488 (substring type-and-selector 2)
490 (address (elpher-make-address type selector host port)))
491 (elpher-make-node url elpher-current-node address))
492 (let* ((host (match-string 2 string))
493 (port (if (> (length (match-string 3 string)) 1)
494 (string-to-number (substring (match-string 3 string) 1))
496 (selector (concat "URL:" url))
497 (address (elpher-make-address ?h selector host port)))
498 (elpher-make-node url elpher-current-node address)))))
501 (defun elpher-buttonify-urls (string)
502 "Turn substrings which look like urls in STRING into clickable buttons."
505 (goto-char (point-min))
506 (while (re-search-forward elpher-url-regex nil t)
507 (let ((node (elpher-make-node-from-matched-url elpher-current-node)))
508 (make-text-button (match-beginning 0)
511 'action #'elpher-click-link
513 'help-echo (elpher-node-button-help node))))
516 (defun elpher-process-text (string)
517 "Remove CRs and trailing period from the gopher text document STRING."
518 (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
519 (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
520 (elpher-buttonify-urls cleaned-str)))
522 (defun elpher-get-text-node ()
523 "Getter which retrieves the current node contents as a text document."
524 (let ((content (elpher-node-content elpher-current-node))
525 (address (elpher-node-address elpher-current-node)))
528 (elpher-with-clean-buffer
530 (elpher-restore-pos)))
532 (elpher-with-clean-buffer
533 (insert "LOADING TEXT..."))
534 (elpher-get-selector address
536 (unless (string-prefix-p "deleted" event)
537 (elpher-with-clean-buffer
538 (insert (elpher-process-text elpher-selector-string))
540 (elpher-set-node-content elpher-current-node
541 (buffer-string))))))))))
545 (defun elpher-get-image-node ()
546 "Getter which retrieves the current node contents as an image to view."
547 (let ((content (elpher-node-content elpher-current-node))
548 (address (elpher-node-address elpher-current-node)))
551 (elpher-with-clean-buffer
552 (insert-image content)
553 (elpher-restore-pos)))
554 (if (display-images-p)
556 (elpher-with-clean-buffer
557 (insert "LOADING IMAGE..."))
558 (elpher-get-selector address
560 (unless (string-prefix-p "deleted" event)
561 (let ((image (create-image
562 (encode-coding-string elpher-selector-string
565 (elpher-with-clean-buffer
567 (elpher-restore-pos))
568 (if elpher-cache-images
569 (elpher-set-node-content elpher-current-node
571 (elpher-get-node-download)))))
575 (defun elpher-get-search-node ()
576 "Getter which submits a search query to the address of the current node."
577 (let ((content (elpher-node-content elpher-current-node))
578 (address (elpher-node-address elpher-current-node))
582 (elpher-with-clean-buffer
584 (elpher-restore-pos))
585 (message "Displaying cached search results. Reload to perform a new search."))
587 (let* ((query-string (read-string "Query: "))
588 (query-selector (concat (elpher-address-selector address) "\t" query-string))
589 (search-address (elpher-make-address ?1
591 (elpher-address-host address)
592 (elpher-address-port address))))
594 (elpher-with-clean-buffer
595 (insert "LOADING RESULTS..."))
596 (elpher-get-selector search-address
598 (unless (string-prefix-p "deleted" event)
599 (elpher-with-clean-buffer
600 (elpher-insert-index elpher-selector-string))
601 (goto-char (point-min))
602 (elpher-set-node-content elpher-current-node
605 (elpher-visit-parent-node))))))
607 ;; Raw server response retrieval
609 (defun elpher-get-node-raw ()
610 "Getter which retrieves the raw server response for the current node."
611 (let* ((content (elpher-node-content elpher-current-node))
612 (address (elpher-node-address elpher-current-node)))
613 (elpher-with-clean-buffer
614 (insert "LOADING RAW SERVER RESPONSE..."))
616 (elpher-get-selector address
618 (unless (string-prefix-p "deleted" event)
619 (elpher-with-clean-buffer
620 (insert elpher-selector-string)
621 (goto-char (point-min))))))
623 (elpher-with-clean-buffer
624 (insert elpher-start-index))
625 (goto-char (point-min)))))
626 (message "Displaying raw server response. Reload or redraw to return to standard view."))
628 ;; File export retrieval
630 (defvar elpher-download-filename)
632 (defun elpher-get-node-download ()
633 "Getter which retrieves the current node and writes the result to a file."
634 (let* ((address (elpher-node-address elpher-current-node))
635 (selector (elpher-address-selector address)))
636 (elpher-visit-parent-node) ; Do first in case of non-local exits.
637 (let* ((filename-proposal (file-name-nondirectory selector))
638 (filename (read-file-name "Save file as: "
640 (if (> (length filename-proposal) 0)
643 (message "Downloading...")
644 (setq elpher-download-filename filename)
645 (elpher-get-selector address
647 (let ((coding-system-for-write 'binary))
648 (with-temp-file elpher-download-filename
649 (insert elpher-selector-string)
650 (message (format "Download complate, saved to file %s."
651 elpher-download-filename)))))))))
655 (defun elpher-get-url-node ()
656 "Getter which attempts to open the URL specified by the current node."
657 (let* ((address (elpher-node-address elpher-current-node))
658 (selector (elpher-address-selector address)))
659 (elpher-visit-parent-node) ; Do first in case of non-local exits.
660 (let ((url (elt (split-string selector "URL:") 1)))
661 (if elpher-open-urls-with-eww
665 ;; Telnet node connection
667 (defun elpher-get-telnet-node ()
668 "Getter which opens a telnet connection to the server specified by the current node."
669 (let* ((address (elpher-node-address elpher-current-node))
670 (host (elpher-address-host address))
671 (port (elpher-address-port address)))
672 (elpher-visit-parent-node)
679 (defun elpher-make-bookmark (display-string address)
680 "Make an elpher bookmark.
681 DISPLAY-STRING determines how the bookmark will appear in the
682 bookmark list, while ADDRESS is the address of the entry."
683 (list display-string address))
685 (defun elpher-bookmark-display-string (bookmark)
686 "Get the display string of BOOKMARK."
689 (defun elpher-bookmark-address (bookmark)
690 "Get the address for BOOKMARK."
693 (defun elpher-save-bookmarks (bookmarks)
694 "Record the bookmark list BOOKMARKS to the user's bookmark file.
695 Beware that this completely replaces the existing contents of the file."
696 (with-temp-file (locate-user-emacs-file "elpher-bookmarks")
698 (pp bookmarks (current-buffer))))
700 (defun elpher-load-bookmarks ()
701 "Get the list of bookmarks from the users's bookmark file."
704 (insert-file-contents (locate-user-emacs-file "elpher-bookmarks"))
705 (goto-char (point-min))
706 (read (current-buffer)))))
708 (defun elpher-add-node-bookmark (node)
709 "Add bookmark to NODE to the saved list of bookmarks."
710 (let ((bookmark (elpher-make-bookmark (elpher-node-display-string node)
711 (elpher-node-address node)))
712 (bookmarks (elpher-load-bookmarks)))
713 (add-to-list 'bookmarks bookmark)
714 (elpher-save-bookmarks bookmarks)))
716 (defun elpher-remove-node-bookmark (node)
717 "Remove bookmark to NODE from the saved list of bookmarks."
718 (let ((bookmark (elpher-make-bookmark (elpher-node-display-string node)
719 (elpher-node-address node))))
720 (elpher-save-bookmarks
721 (seq-filter (lambda (this-bookmark)
722 (not (equal bookmark this-bookmark)))
723 (elpher-load-bookmarks)))))
725 (defun elpher-display-bookmarks ()
726 "Display saved bookmark list."
728 (elpher-with-clean-buffer
729 (insert "Use 'u' to return to the previous page.\n\n"
730 "---- Bookmark list ----\n\n")
731 (let ((bookmarks (elpher-load-bookmarks)))
733 (dolist (bookmark bookmarks)
734 (let ((display-string (elpher-bookmark-display-string bookmark))
735 (address (elpher-bookmark-address bookmark)))
736 (elpher-insert-index-record-helper display-string
737 (elpher-address-type address)
738 (elpher-address-selector address)
739 (elpher-address-host address)
740 (elpher-address-port address))))
741 (insert "No bookmarks found.\n")))
742 (insert "\n-----------------------")
743 (goto-char (point-min))
746 (defun elpher-bookmark-current ()
747 "Bookmark the current node."
749 (elpher-add-node-bookmark elpher-current-node))
751 (defun elpher-bookmark-link ()
752 "Bookmark the link at point."
754 (let ((button (button-at (point))))
756 (elpher-add-node-bookmark (button-get button 'elpher-node))
757 (error "No link selected"))))
759 (defun elpher-unbookmark-current ()
760 "Remove bookmark for the current node."
762 (elpher-remove-node-bookmark elpher-current-node))
764 (defun elpher-unbookmark-link ()
765 "Remove bookmark for the link at point."
767 (let ((button (button-at (point))))
769 (elpher-remove-node-bookmark (button-get button 'elpher-node))
770 (error "No link selected"))))
772 ;;; Interactive navigation procedures
775 (defun elpher-next-link ()
776 "Move point to the next link on the current page."
780 (defun elpher-prev-link ()
781 "Move point to the previous link on the current page."
785 (defun elpher-follow-current-link ()
786 "Open the link or url at point."
791 "Go to a particular gopher site."
794 (let ((host-or-url (read-string "Gopher host or URL: ")))
795 (if (string-match elpher-url-regex host-or-url)
796 (elpher-make-node-from-matched-url elpher-current-node
798 (let ((selector (read-string "Selector (default none): " nil nil ""))
799 (port (string-to-number (read-string "Port (default 70): "
801 (elpher-make-node (concat "gopher://" host-or-url
805 (elpher-make-address ?1 selector host-or-url port)))))))
806 (switch-to-buffer "*elpher*")
807 (elpher-visit-node node)))
809 (defun elpher-redraw ()
810 "Redraw current page."
812 (if elpher-current-node
813 (elpher-visit-node elpher-current-node)
814 (message "No current site.")))
816 (defun elpher-reload ()
817 "Reload current page."
819 (if elpher-current-node
820 (elpher-reload-current-node)
821 (message "No current site.")))
823 (defun elpher-view-raw ()
824 "View current page as plain text."
826 (if elpher-current-node
827 (elpher-visit-node elpher-current-node
828 #'elpher-get-node-raw)
829 (message "No current site.")))
831 (defun elpher-back ()
832 "Go to previous site."
834 (if (elpher-node-parent elpher-current-node)
835 (elpher-visit-parent-node)
836 (error "No previous site")))
838 (defun elpher-download ()
839 "Download the link at point."
841 (let ((button (button-at (point))))
843 (let ((node (button-get button 'elpher-node)))
845 (elpher-visit-node (button-get button 'elpher-node)
846 #'elpher-get-node-download)
847 (error "Can only download gopher links, not general URLs")))
848 (error "No link selected"))))
850 (defun elpher-build-link-map ()
851 "Build alist mapping link names to destination nodes in current buffer."
853 (b (next-button (point-min) t)))
855 (add-to-list 'link-map (cons (button-label b) b))
856 (setq b (next-button (button-start b))))
859 (defun elpher-menu ()
860 "Select a directory entry by name. Similar to the info browser (m)enu command."
862 (let* ((link-map (elpher-build-link-map)))
864 (let ((key (let ((completion-ignore-case t))
865 (completing-read "Directory entry/link (tab to autocomplete): "
867 (if (and key (> (length key) 0))
868 (let ((b (cdr (assoc key link-map))))
869 (goto-char (button-start b))
870 (button-activate b)))))))
872 (defun elpher-root-dir ()
873 "Visit root of current server."
875 (let ((address (elpher-node-address elpher-current-node)))
877 (let ((host (elpher-address-host address))
878 (selector (elpher-address-selector address))
879 (port (elpher-address-port address)))
880 (if (> (length selector) 0)
881 (let ((root-address (elpher-make-address ?1 "" host port)))
883 (elpher-make-node (concat "gopher://" host
884 ":" (number-to-string port)
888 (error "Already at root directory of current server")))
889 (error "Command invalid for Elpher start page"))))
891 (defun elpher-info-node (node)
892 "Display information on NODE."
893 (let ((display-string (elpher-node-display-string node))
894 (address (elpher-node-address node)))
896 (message "`%s' on %s port %s"
897 (elpher-address-selector address)
898 (elpher-address-host address)
899 (elpher-address-port address))
900 (message "%s" display-string))))
902 (defun elpher-info-link ()
903 "Display information on node corresponding to link at point."
905 (let ((button (button-at (point))))
907 (elpher-info-node (button-get button 'elpher-node))
908 (error "No link selected"))))
910 (defun elpher-info-current ()
911 "Display information on current node."
913 (elpher-info-node elpher-current-node))
918 (defvar elpher-mode-map
919 (let ((map (make-sparse-keymap)))
920 (define-key map (kbd "TAB") 'elpher-next-link)
921 (define-key map (kbd "<backtab>") 'elpher-prev-link)
922 (define-key map (kbd "u") 'elpher-back)
923 (define-key map (kbd "O") 'elpher-root-dir)
924 (define-key map (kbd "g") 'elpher-go)
925 (define-key map (kbd "r") 'elpher-redraw)
926 (define-key map (kbd "R") 'elpher-reload)
927 (define-key map (kbd "w") 'elpher-view-raw)
928 (define-key map (kbd "d") 'elpher-download)
929 (define-key map (kbd "m") 'elpher-menu)
930 (define-key map (kbd "i") 'elpher-info-link)
931 (define-key map (kbd "I") 'elpher-info-current)
932 (when (fboundp 'evil-define-key)
933 (evil-define-key 'motion map
934 (kbd "TAB") 'elpher-next-link
935 (kbd "C-]") 'elpher-follow-current-link
936 (kbd "C-t") 'elpher-back
937 (kbd "u") 'elpher-back
938 (kbd "O") 'elpher-root-dir
940 (kbd "r") 'elpher-redraw
941 (kbd "R") 'elpher-reload
942 (kbd "w") 'elpher-view-raw
943 (kbd "d") 'elpher-download
944 (kbd "m") 'elpher-menu
945 (kbd "i") 'elpher-info-link
946 (kbd "I") 'elpher-info-current
947 (kbd "a") 'elpher-bookmark-link
948 (kbd "A") 'elpher-bookmark-current
949 (kbd "x") 'elpher-unbookmark-link
950 (kbd "X") 'elpher-unbookmark-current
951 (kbd "B") 'elpher-display-bookmarks))
953 "Keymap for gopher client.")
955 (define-derived-mode elpher-mode special-mode "elpher"
956 "Major mode for elpher, an elisp gopher client.")
958 (when (fboundp 'evil-set-initial-state)
959 (evil-set-initial-state 'elpher-mode 'motion))
961 ;;; Main start procedure
966 "Start elpher with default landing page."
968 (if (get-buffer "*elpher*")
969 (switch-to-buffer "*elpher*")
970 (switch-to-buffer "*elpher*")
971 (setq elpher-current-node nil)
972 (let ((start-node (elpher-make-node "Elpher Start Page"
973 nil elpher-start-address)))
974 (elpher-visit-node start-node)))
975 "Started Elpher.") ; Otherwise (elpher) evaluates to start page string.
977 ;;; elpher.el ends here