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 raw)
148 (setq elopher-current-node node)
150 (elopher-get-node-raw)
151 (funcall (elopher-node-getter node))))
153 (defun elopher-visit-parent-node ()
154 (let ((parent-node (elopher-node-parent elopher-current-node)))
156 (elopher-visit-node parent-node))))
158 (defun elopher-reload-current-node ()
159 (elopher-set-node-content elopher-current-node nil)
160 (elopher-visit-node elopher-current-node))
162 ;;; Buffer preparation
165 (defmacro elopher-with-clean-buffer (&rest args)
167 '(switch-to-buffer "*elopher*")
169 (append (list 'let '((inhibit-read-only t))
176 (defun elopher-insert-index (string)
177 "Insert the index corresponding to STRING into the current buffer."
178 (dolist (line (split-string string "\r\n"))
179 (unless (= (length line) 0)
180 (elopher-insert-index-record line))))
182 (defun elopher-insert-margin (&optional type-name)
183 "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
186 (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
188 (propertize "[" 'face '(foreground-color . "blue"))
189 (propertize type-name 'face '(foreground-color . "white"))
190 (propertize "]" 'face '(foreground-color . "blue")))))
192 (insert (make-string elopher-margin-width ?\s))))
194 (defun elopher-insert-index-record (line)
195 "Insert the index record corresponding to LINE into the current buffer."
196 (let* ((type (elt line 0))
197 (fields (split-string (substring line 1) "\t"))
198 (display-string (elt fields 0))
199 (selector (elt fields 1))
200 (host (elt fields 2))
201 (port (elt fields 3))
202 (address (elopher-make-address selector host port))
203 (help-string (format "mouse-1, RET: open %s on %s port %s"
204 selector host port)))
206 (?i (elopher-insert-margin)
207 (insert (propertize display-string
208 'face elopher-info-face)))
209 (?0 (elopher-insert-margin "T")
210 (insert-text-button display-string
211 'face elopher-text-face
212 'elopher-node (elopher-make-node elopher-current-node
214 #'elopher-get-text-node)
215 'action #'elopher-click-link
217 'help-echo help-string))
218 (?1 (elopher-insert-margin "/")
219 (insert-text-button display-string
220 'face elopher-index-face
221 'elopher-node (elopher-make-node elopher-current-node
223 #'elopher-get-index-node)
224 'action #'elopher-click-link
226 'help-echo help-string))
227 ((or ?g ?p ?I) (elopher-insert-margin "im")
228 (insert-text-button display-string
229 'face elopher-image-face
230 'elopher-node (elopher-make-node elopher-current-node
232 #'elopher-get-image-node)
233 'action #'elopher-click-link
235 'help-echo help-string))
236 (?7 (elopher-insert-margin "S")
237 (insert-text-button display-string
238 'face elopher-search-face
239 'elopher-node (elopher-make-node elopher-current-node
241 #'elopher-get-search-node)
242 'action #'elopher-click-link
244 'help-echo help-string))
245 (?h (elopher-insert-margin "W")
246 (let ((url (elt (split-string selector "URL:") 1)))
247 (insert-text-button display-string
248 'face elopher-http-face
250 'action #'elopher-click-url
252 'help-echo (format "mouse-1, RET: open url %s" url))))
253 (?.) ; Occurs at end of index, can safely ignore.
254 (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
255 (insert (propertize display-string
256 'face elopher-unknown-face))))
260 ;;; Selector retrieval (all kinds)
263 (defvar elopher-selector-string)
265 (defun elopher-get-selector (address after)
266 "Retrieve selector specified by ADDRESS, then execute AFTER.
267 The result is stored as a string in the variable elopher-selector-string."
268 (setq elopher-selector-string "")
269 (let ((p (get-process "elopher-process")))
270 (if p (delete-process p)))
271 (make-network-process
272 :name "elopher-process"
273 :host (elopher-address-host address)
274 :service (elopher-address-port address)
275 :filter (lambda (proc string)
276 (setq elopher-selector-string (concat elopher-selector-string string)))
278 (process-send-string "elopher-process"
279 (concat (elopher-address-selector address) "\n")))
283 (defun elopher-get-index-node ()
284 (let ((content (elopher-node-content elopher-current-node))
285 (address (elopher-node-address elopher-current-node)))
288 (elopher-with-clean-buffer
290 (elopher-restore-pos))
293 (elopher-with-clean-buffer
294 (insert "LOADING DIRECTORY..."))
295 (elopher-get-selector address
297 (elopher-with-clean-buffer
298 (elopher-insert-index elopher-selector-string))
299 (elopher-restore-pos)
300 (elopher-set-node-content elopher-current-node
303 (elopher-with-clean-buffer
304 (elopher-insert-index elopher-start-index))
305 (elopher-restore-pos)
306 (elopher-set-node-content elopher-current-node
307 (buffer-string)))))))
311 (defun elopher-process-text (string)
312 (let ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)))
313 (replace-regexp-in-string "\r" "" chopped-str)))
315 (defun elopher-get-text-node ()
316 (let ((content (elopher-node-content elopher-current-node))
317 (address (elopher-node-address elopher-current-node)))
320 (elopher-with-clean-buffer
322 (elopher-restore-pos))
324 (elopher-with-clean-buffer
325 (insert "LOADING TEXT..."))
326 (elopher-get-selector address
328 (elopher-with-clean-buffer
329 (insert (elopher-process-text elopher-selector-string)))
330 (elopher-restore-pos)
331 (elopher-set-node-content elopher-current-node
332 (buffer-string))))))))
336 (defun elopher-get-image-node ()
337 (let ((content (elopher-node-content elopher-current-node))
338 (address (elopher-node-address elopher-current-node)))
341 (elopher-with-clean-buffer
342 (insert-image content))
343 (setq cursor-type nil)
344 (elopher-restore-pos))
346 (elopher-with-clean-buffer
347 (insert "LOADING IMAGE..."))
348 (elopher-get-selector address
350 (let ((image (create-image
351 (string-as-unibyte elopher-selector-string)
353 (elopher-with-clean-buffer
354 (insert-image image))
355 (setq cursor-type nil)
356 (elopher-restore-pos)
357 (elopher-set-node-content elopher-current-node image))))))))
361 (defun elopher-get-search-node ()
362 (let* ((content (elopher-node-content elopher-current-node))
363 (address (elopher-node-address elopher-current-node)))
366 (elopher-with-clean-buffer
368 (elopher-restore-pos)
369 (message "Displaying cached search results. Reload to perform a new search."))
370 (let* ((query-string (read-string "Query: "))
371 (query-selector (concat (elopher-address-selector address) "\t" query-string))
372 (search-address (elopher-make-address query-selector
373 (elopher-address-host address)
374 (elopher-address-port address))))
375 (elopher-with-clean-buffer
376 (insert "LOADING RESULTS..."))
377 (elopher-get-selector search-address
379 (elopher-with-clean-buffer
380 (elopher-insert-index elopher-selector-string))
381 (goto-char (point-min))
382 (elopher-set-node-content elopher-current-node
383 (buffer-string))))))))
385 ;; Raw server response retrieval
387 (defun elopher-get-node-raw ()
388 (let* ((content (elopher-node-content elopher-current-node))
389 (address (elopher-node-address elopher-current-node)))
390 (elopher-with-clean-buffer
391 (insert "LOADING RAW SERVER RESPONSE..."))
393 (elopher-get-selector address
395 (elopher-with-clean-buffer
396 (insert elopher-selector-string))
397 (goto-char (point-min))))
399 (elopher-with-clean-buffer
400 (insert elopher-start-index))
401 (goto-char (point-min)))))
402 (message "Displaying raw server response. Reload to return to standard view."))
405 ;; File export retrieval
407 (defvar elopher-download-filename)
409 (defun elopher-download-node (node filename)
410 (let* ((address (elopher-node-address node)))
411 (message "Downloading...")
412 (setq elopher-download-filename filename)
413 (elopher-get-selector address
415 (let ((coding-system-for-write 'binary))
416 (with-temp-file elopher-download-filename
417 (insert elopher-selector-string)))
418 (message (format "Download complate, saved to file %s."
419 elopher-download-filename))))))
421 ;;; Navigation procedures
424 (defun elopher-next-link ()
428 (defun elopher-prev-link ()
432 (defun elopher-click-link (button)
433 (let ((node (button-get button 'elopher-node)))
434 (elopher-visit-node node)))
436 (defun elopher-click-url (button)
437 (let ((url (button-get button 'elopher-url)))
438 (if elopher-open-urls-with-eww
442 (defun elopher-follow-closest-link ()
447 "Go to a particular gopher site."
450 (hostname (read-string "Gopher host: "))
451 (selector (read-string "Selector (default none): " nil nil ""))
452 (port (read-string "Port (default 70): " nil nil 70))
453 (address (list selector hostname port)))
455 (elopher-make-node elopher-current-node
457 #'elopher-get-index-node))))
459 (defun elopher-reload ()
460 "Reload current page."
462 (elopher-reload-current-node))
464 (defun elopher-view-raw ()
465 "View current page as plain text."
467 (elopher-visit-node elopher-current-node t))
469 (defun elopher-back ()
470 "Go to previous site."
472 (if (elopher-node-parent elopher-current-node)
473 (elopher-visit-parent-node)
474 (message "No previous site.")))
476 (defun elopher-download ()
477 "Download the link at point."
479 (let ((button (button-at (point))))
481 (let* ((node (button-get button 'elopher-node))
482 (address (elopher-node-address node))
483 (selector (elopher-address-selector address))
484 (filename-proposal (file-name-nondirectory selector))
485 (filename (read-file-name "Name of file to write: "
487 (if (> 0 (length filename-proposal))
490 (elopher-download-node node filename))
491 (message "No link selected."))))
496 (defvar elopher-mode-map
497 (let ((map (make-sparse-keymap)))
498 (define-key map (kbd "<tab>") 'elopher-next-link)
499 (define-key map (kbd "<S-tab>") 'elopher-prev-link)
500 (define-key map (kbd "u") 'elopher-back)
501 (define-key map (kbd "g") 'elopher-go)
502 (define-key map (kbd "r") 'elopher-reload)
503 (define-key map (kbd "w") 'elopher-view-raw)
504 (define-key map (kbd "d") 'elopher-download)
505 (when (fboundp 'evil-define-key)
506 (evil-define-key 'normal map
507 (kbd "C-]") 'elopher-follow-closest-link
508 (kbd "C-t") 'elopher-back
509 (kbd "u") 'elopher-back
510 (kbd "g") 'elopher-go
511 (kbd "r") 'elopher-reload
512 (kbd "w") 'elopher-view-raw
513 (kbd "d") 'elopher-download))
515 "Keymap for gopher client.")
517 (define-derived-mode elopher-mode special-mode "elopher"
518 "Major mode for elopher, an elisp gopher client.")
521 ;;; Main start procedure
524 "Start elopher with default landing page."
526 (setq elopher-current-node nil)
527 (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
528 (elopher-visit-node start-node)))
530 ;;; elopher.el ends here