1 ;;; elopher.el --- gopher client
5 ;; Simple gopher client in elisp.
12 (defconst elopher-version "1.0.0"
13 "Current version of elopher.")
15 (defconst elopher-margin-width 6
16 "Width of left-hand margin used when rendering indicies.")
18 (defconst elopher-start-index
21 (list "i\tfake\tfake\t1"
22 "i--------------------------------------------\tfake\tfake\t1"
23 "i Elopher Gopher Client \tfake\tfake\t1"
24 (format "i version %s\tfake\tfake\t1" elopher-version)
25 "i--------------------------------------------\tfake\tfake\t1"
27 "iBasic usage:\tfake\tfake\t1"
29 "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
30 "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
31 "i - u: return to parent directory entry\tfake\tfake\t1"
32 "i - g: go to a particular page\tfake\tfake\t1"
33 "i - r: reload current page\tfake\tfake\t1"
34 "i - d: download directory entry under cursor\tfake\tfake\t1"
35 "i - w: display the raw server response for the current page\tfake\tfake\t1"
37 "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
39 "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
41 "iAlternatively, select the following item and enter some\tfake\tfake\t1"
42 "isearch terms:\tfake\tfake\t1"
44 "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
49 ;;; Customization group
53 "A simple gopher client."
56 (defcustom elopher-index-face '(foreground-color . "cyan")
57 "Face used for index records."
60 (defcustom elopher-text-face '(foreground-color . "white")
61 "Face used for text records."
64 (defcustom elopher-info-face '(foreground-color . "gray")
65 "Face used for info records."
68 (defcustom elopher-image-face '(foreground-color . "green")
69 "Face used for image records."
72 (defcustom elopher-search-face '(foreground-color . "orange")
73 "Face used for image records."
76 (defcustom elopher-http-face '(foreground-color . "yellow")
77 "Face used for image records."
80 (defcustom elopher-unknown-face '(foreground-color . "red")
81 "Face used for unknown record types."
84 (defcustom elopher-open-urls-with-eww nil
85 "If non-nil, open URL selectors using eww.
86 Otherwise, use the system browser via the BROWSE-URL function."
94 (defun elopher-make-address (selector host port)
95 (list selector host port))
97 (defun elopher-address-selector (address)
100 (defun elopher-address-host (address)
103 (defun elopher-address-port (address)
108 (defun elopher-make-node (parent address getter &optional content pos)
109 (list parent address getter content pos))
111 (defun elopher-node-parent (node)
114 (defun elopher-node-address (node)
117 (defun elopher-node-getter (node)
120 (defun elopher-node-content (node)
123 (defun elopher-node-pos (node)
126 (defun elopher-set-node-content (node content)
127 (setcar (nthcdr 3 node) content))
129 (defun elopher-set-node-pos (node pos)
130 (setcar (nthcdr 4 node) pos))
132 (defun elopher-save-pos ()
133 (when elopher-current-node
134 (elopher-set-node-pos elopher-current-node (point))))
136 (defun elopher-restore-pos ()
137 (let ((pos (elopher-node-pos elopher-current-node)))
140 (goto-char (point-min)))))
142 ;; Node graph traversal
144 (defvar elopher-current-node)
146 (defun elopher-visit-node (node &optional getter)
148 (elopher-process-cleanup)
149 (setq elopher-current-node node)
152 (funcall (elopher-node-getter node))))
154 (defun elopher-visit-parent-node ()
155 (let ((parent-node (elopher-node-parent elopher-current-node)))
157 (elopher-visit-node parent-node))))
159 (defun elopher-reload-current-node ()
160 (elopher-set-node-content elopher-current-node nil)
161 (elopher-visit-node elopher-current-node))
163 ;;; Buffer preparation
166 (defmacro elopher-with-clean-buffer (&rest args)
167 "Evaluate ARGS with a clean *elopher* buffer as current."
169 '(switch-to-buffer "*elopher*")
171 (append (list 'let '((inhibit-read-only t))
178 (defun elopher-insert-index (string)
179 "Insert the index corresponding to STRING into the current buffer."
180 (dolist (line (split-string string "\r\n"))
181 (unless (= (length line) 0)
182 (elopher-insert-index-record line))))
184 (defun elopher-insert-margin (&optional type-name)
185 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
188 (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
190 (propertize "[" 'face '(foreground-color . "blue"))
191 (propertize type-name 'face '(foreground-color . "white"))
192 (propertize "]" 'face '(foreground-color . "blue")))))
194 (insert (make-string elopher-margin-width ?\s))))
196 (defun elopher-insert-index-record (line)
197 "Insert the index record corresponding to LINE into the current buffer."
198 (let* ((type (elt line 0))
199 (fields (split-string (substring line 1) "\t"))
200 (display-string (elt fields 0))
201 (selector (elt fields 1))
202 (host (elt fields 2))
203 (port (elt fields 3))
204 (address (elopher-make-address selector host port))
205 (help-string (format "mouse-1, RET: open %s on %s port %s"
206 selector host port)))
208 (?i (elopher-insert-margin)
209 (insert (propertize display-string
210 'face elopher-info-face)))
211 (?0 (elopher-insert-margin "T")
212 (insert-text-button display-string
213 'face elopher-text-face
214 'elopher-node (elopher-make-node elopher-current-node
216 #'elopher-get-text-node)
217 'action #'elopher-click-link
219 'help-echo help-string))
220 (?1 (elopher-insert-margin "/")
221 (insert-text-button display-string
222 'face elopher-index-face
223 'elopher-node (elopher-make-node elopher-current-node
225 #'elopher-get-index-node)
226 'action #'elopher-click-link
228 'help-echo help-string))
229 ((or ?g ?p ?I) (elopher-insert-margin "im")
230 (insert-text-button display-string
231 'face elopher-image-face
232 'elopher-node (elopher-make-node elopher-current-node
234 #'elopher-get-image-node)
235 'action #'elopher-click-link
237 'help-echo help-string))
238 (?7 (elopher-insert-margin "S")
239 (insert-text-button display-string
240 'face elopher-search-face
241 'elopher-node (elopher-make-node elopher-current-node
243 #'elopher-get-search-node)
244 'action #'elopher-click-link
246 'help-echo help-string))
247 (?h (elopher-insert-margin "W")
248 (let ((url (elt (split-string selector "URL:") 1)))
249 (insert-text-button display-string
250 'face elopher-http-face
252 'action #'elopher-click-url
254 'help-echo (format "mouse-1, RET: open url %s" url))))
255 (?.) ; Occurs at end of index, can safely ignore.
256 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
257 (insert (propertize display-string
258 'face elopher-unknown-face))))
262 ;;; Selector retrieval (all kinds)
265 (defun elopher-process-cleanup ()
266 "Immediately shut down any extant elopher process."
267 (let ((p (get-process "elopher-process")))
268 (if p (delete-process p))))
270 (defvar elopher-selector-string)
272 (defun elopher-get-selector (address after)
273 "Retrieve selector specified by ADDRESS, then execute AFTER.
274 The result is stored as a string in the variable elopher-selector-string."
275 (setq elopher-selector-string "")
276 (make-network-process
277 :name "elopher-process"
278 :host (elopher-address-host address)
279 :service (elopher-address-port address)
280 :filter (lambda (proc string)
281 (setq elopher-selector-string (concat elopher-selector-string string)))
283 (process-send-string "elopher-process"
284 (concat (elopher-address-selector address) "\n")))
288 (defun elopher-get-index-node ()
289 (let ((content (elopher-node-content elopher-current-node))
290 (address (elopher-node-address elopher-current-node)))
293 (elopher-with-clean-buffer
295 (elopher-restore-pos))
298 (elopher-with-clean-buffer
299 (insert "LOADING DIRECTORY..."))
300 (elopher-get-selector address
302 (unless (string-prefix-p "deleted" event)
303 (elopher-with-clean-buffer
304 (elopher-insert-index elopher-selector-string))
305 (elopher-restore-pos)
306 (elopher-set-node-content elopher-current-node
309 (elopher-with-clean-buffer
310 (elopher-insert-index elopher-start-index))
311 (elopher-restore-pos)
312 (elopher-set-node-content elopher-current-node
313 (buffer-string)))))))
317 (defun elopher-process-text (string)
318 (let ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)))
319 (replace-regexp-in-string "\r" "" chopped-str)))
321 (defun elopher-get-text-node ()
322 (let ((content (elopher-node-content elopher-current-node))
323 (address (elopher-node-address elopher-current-node)))
326 (elopher-with-clean-buffer
328 (elopher-restore-pos))
330 (elopher-with-clean-buffer
331 (insert "LOADING TEXT..."))
332 (elopher-get-selector address
334 (unless (string-prefix-p "deleted" event)
335 (elopher-with-clean-buffer
336 (insert (elopher-process-text elopher-selector-string)))
337 (elopher-restore-pos)
338 (elopher-set-node-content elopher-current-node
339 (buffer-string)))))))))
343 (defun elopher-get-image-node ()
344 (let ((content (elopher-node-content elopher-current-node))
345 (address (elopher-node-address elopher-current-node)))
348 (elopher-with-clean-buffer
349 (insert-image content))
350 (setq cursor-type nil)
351 (elopher-restore-pos))
353 (elopher-with-clean-buffer
354 (insert "LOADING IMAGE..."))
355 (elopher-get-selector address
357 (unless (string-prefix-p "deleted" event)
358 (let ((image (create-image
359 (string-as-unibyte elopher-selector-string)
361 (elopher-with-clean-buffer
362 (insert-image image))
363 (setq cursor-type nil)
364 (elopher-restore-pos)
365 (elopher-set-node-content elopher-current-node
370 (defun elopher-get-search-node ()
371 (let* ((content (elopher-node-content elopher-current-node))
372 (address (elopher-node-address elopher-current-node)))
375 (elopher-with-clean-buffer
377 (elopher-restore-pos)
378 (message "Displaying cached search results. Reload to perform a new search."))
379 (let* ((query-string (read-string "Query: "))
380 (query-selector (concat (elopher-address-selector address) "\t" query-string))
381 (search-address (elopher-make-address query-selector
382 (elopher-address-host address)
383 (elopher-address-port address))))
384 (elopher-with-clean-buffer
385 (insert "LOADING RESULTS..."))
386 (elopher-get-selector search-address
388 (unless (string-prefix-p "deleted" event)
389 (elopher-with-clean-buffer
390 (elopher-insert-index elopher-selector-string))
391 (goto-char (point-min))
392 (elopher-set-node-content elopher-current-node
393 (buffer-string)))))))))
395 ;; Raw server response retrieval
397 (defun elopher-get-node-raw ()
398 (let* ((content (elopher-node-content elopher-current-node))
399 (address (elopher-node-address elopher-current-node)))
400 (elopher-with-clean-buffer
401 (insert "LOADING RAW SERVER RESPONSE..."))
403 (elopher-get-selector address
405 (unless (string-prefix-p "deleted" event)
406 (elopher-with-clean-buffer
407 (insert elopher-selector-string))
408 (goto-char (point-min)))))
410 (elopher-with-clean-buffer
411 (insert elopher-start-index))
412 (goto-char (point-min)))))
413 (message "Displaying raw server response. Reload to return to standard view."))
416 ;; File export retrieval
418 (defvar elopher-download-filename)
420 (defun elopher-get-node-download ()
421 (let* ((address (elopher-node-address elopher-current-node))
422 (selector (elopher-address-selector address)))
424 (let* ((filename-proposal (file-name-nondirectory selector))
425 (filename (read-file-name "Save file as: "
427 (if (> (length filename-proposal) 0)
430 (message "Downloading...")
431 (setq elopher-download-filename filename)
432 (elopher-get-selector address
434 (let ((coding-system-for-write 'binary))
435 (with-temp-file elopher-download-filename
436 (insert elopher-selector-string)))
437 (message (format "Download complate, saved to file %s."
438 elopher-download-filename)))))
439 (elopher-visit-parent-node))))
442 ;;; Navigation procedures
445 (defun elopher-next-link ()
449 (defun elopher-prev-link ()
453 (defun elopher-click-link (button)
454 (let ((node (button-get button 'elopher-node)))
455 (elopher-visit-node node)))
457 (defun elopher-click-url (button)
458 (let ((url (button-get button 'elopher-url)))
459 (if elopher-open-urls-with-eww
463 (defun elopher-follow-closest-link ()
468 "Go to a particular gopher site."
471 (hostname (read-string "Gopher host: "))
472 (selector (read-string "Selector (default none): " nil nil ""))
473 (port (read-string "Port (default 70): " nil nil 70))
474 (address (list selector hostname port)))
476 (elopher-make-node elopher-current-node
478 #'elopher-get-index-node))))
480 (defun elopher-reload ()
481 "Reload current page."
483 (elopher-reload-current-node))
485 (defun elopher-view-raw ()
486 "View current page as plain text."
488 (elopher-visit-node elopher-current-node
489 #'elopher-get-node-raw))
491 (defun elopher-back ()
492 "Go to previous site."
494 (if (elopher-node-parent elopher-current-node)
495 (elopher-visit-parent-node)
496 (message "No previous site.")))
498 (defun elopher-download ()
499 "Download the link at point."
501 (let ((button (button-at (point))))
503 (elopher-visit-node (button-get button 'elopher-node)
504 #'elopher-get-node-download)
505 (message "No link selected."))))
511 (defvar elopher-mode-map
512 (let ((map (make-sparse-keymap)))
513 (define-key map (kbd "<tab>") 'elopher-next-link)
514 (define-key map (kbd "<S-tab>") 'elopher-prev-link)
515 (define-key map (kbd "u") 'elopher-back)
516 (define-key map (kbd "g") 'elopher-go)
517 (define-key map (kbd "r") 'elopher-reload)
518 (define-key map (kbd "w") 'elopher-view-raw)
519 (define-key map (kbd "d") 'elopher-download)
520 (when (fboundp 'evil-define-key)
521 (evil-define-key 'normal map
522 (kbd "C-]") 'elopher-follow-closest-link
523 (kbd "C-t") 'elopher-back
524 (kbd "u") 'elopher-back
525 (kbd "g") 'elopher-go
526 (kbd "r") 'elopher-reload
527 (kbd "w") 'elopher-view-raw
528 (kbd "d") 'elopher-download))
530 "Keymap for gopher client.")
532 (define-derived-mode elopher-mode special-mode "elopher"
533 "Major mode for elopher, an elisp gopher client.")
536 ;;; Main start procedure
540 "Start elopher with default landing page."
542 (setq elopher-current-node nil)
543 (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
544 (elopher-visit-node start-node))
545 "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
547 ;;; elopher.el ends here