Fixed out-of-date start page help.
[elpher.git] / elopher.el
1 ;;; elopher.el --- gopher client
2
3 ;;; Commentary:
4
5 ;; Simple gopher client in elisp.
6
7 ;;; Code:
8
9 ;;; Global constants
10 ;;
11
12 (defconst elopher-version "1.0.0"
13   "Current version of elopher.")
14
15 (defconst elopher-margin-width 6
16   "Width of left-hand margin used when rendering indicies.")
17
18 (defconst elopher-start-index
19   (mapconcat 'identity
20    (list "i\tfake\tfake\t1"
21          "i--------------------------------------------\tfake\tfake\t1"
22          "i          Elopher Gopher Client             \tfake\tfake\t1"
23          (format "i              version %s\tfake\tfake\t1" elopher-version)
24          "i--------------------------------------------\tfake\tfake\t1"
25          "i\tfake\tfake\t1"
26          "iBasic usage:\tfake\tfake\t1"
27          "i\tfake\tfake\t1"
28          "i - tab/shift-tab: next/prev directory entry on current page\tfake\tfake\t1"
29          "i - RET/mouse-1: open directory entry under cursor\tfake\tfake\t1"
30          "i - u: return to parent directory entry\tfake\tfake\t1"
31          "i - g: go to a particular page\tfake\tfake\t1"
32          "i - r: reload current page\tfake\tfake\t1"
33          "i - w: display the raw server response for the current page\tfake\tfake\t1"
34          "i\tfake\tfake\t1"
35          "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
36          "i\tfake\tfake\t1"
37          "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
38          "i\tfake\tfake\t1"
39          "iAlternatively, select the following item and enter some\tfake\tfake\t1"
40          "isearch terms:\tfake\tfake\t1"
41          "i\tfake\tfake\t1"
42          "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
43          ".")
44    "\r\n"))
45
46
47 ;;; Customization group
48 ;;
49
50 (defgroup elopher nil
51   "A simple gopher client."
52   :group 'applications)
53
54 (defcustom elopher-index-face '(foreground-color . "cyan")
55   "Face used for index records."
56   :type '(face))
57
58 (defcustom elopher-text-face '(foreground-color . "white")
59   "Face used for text records."
60   :type '(face))
61
62 (defcustom elopher-info-face '(foreground-color . "gray")
63   "Face used for info records."
64   :type '(face))
65
66 (defcustom elopher-image-face '(foreground-color . "green")
67   "Face used for image records."
68   :type '(face))
69
70 (defcustom elopher-search-face '(foreground-color . "orange")
71   "Face used for image records."
72   :type '(face))
73
74 (defcustom elopher-http-face '(foreground-color . "yellow")
75   "Face used for image records."
76   :type '(face))
77
78 (defcustom elopher-unknown-face '(foreground-color . "red")
79   "Face used for unknown record types."
80   :type '(face))
81
82 (defcustom elopher-open-urls-with-eww nil
83   "If non-nil, open URL selectors using eww.
84 Otherwise, use the system browser via the BROWSE-URL function."
85   :type '(boolean))
86
87 ;;; Model
88 ;;
89
90 ;; Address
91
92 (defun elopher-make-address (selector host port)
93   (list selector host port))
94
95 (defun elopher-address-selector (address)
96   (car address))
97
98 (defun elopher-address-host (address)
99   (cadr address))
100
101 (defun elopher-address-port (address)
102   (caddr address))
103
104 ;; Node
105
106 (defun elopher-make-node (parent address getter &optional content pos)
107   (list parent address getter content pos))
108
109 (defun elopher-node-parent (node)
110   (elt node 0))
111
112 (defun elopher-node-address (node)
113   (elt node 1))
114
115 (defun elopher-node-getter (node)
116   (elt node 2))
117
118 (defun elopher-node-content (node)
119   (elt node 3))
120
121 (defun elopher-node-pos (node)
122   (elt node 4))
123
124 (defun elopher-set-node-content (node content)
125   (setcar (nthcdr 3 node) content))
126
127 (defun elopher-set-node-pos (node pos)
128   (setcar (nthcdr 4 node) pos))
129
130 (defun elopher-save-pos ()
131   (when elopher-current-node
132     (elopher-set-node-pos elopher-current-node (point))))
133
134 (defun elopher-restore-pos ()
135   (let ((pos (elopher-node-pos elopher-current-node)))
136     (if pos
137         (goto-char pos)
138       (goto-char (point-min)))))
139
140 ;; Node graph traversal
141
142 (defvar elopher-current-node)
143
144 (defun elopher-visit-node (node &optional raw)
145   (elopher-save-pos)
146   (setq elopher-current-node node)
147   (if raw
148       (elopher-get-node-raw)
149     (funcall (elopher-node-getter node))))
150
151 (defun elopher-visit-parent-node ()
152   (let ((parent-node (elopher-node-parent elopher-current-node)))
153     (when parent-node
154       (elopher-visit-node parent-node))))
155       
156 (defun elopher-reload-current-node ()
157   (elopher-set-node-content elopher-current-node nil)
158   (elopher-visit-node elopher-current-node))
159
160 ;;; Buffer preparation
161 ;;
162
163 (defmacro elopher-with-clean-buffer (&rest args)
164   (list 'progn
165         '(switch-to-buffer "*elopher*")
166         '(elopher-mode)
167         (append (list 'let '((inhibit-read-only t))
168                       '(erase-buffer))
169                 args)))
170
171 ;;; Index rendering
172 ;;
173
174 (defun elopher-insert-index (string)
175   "Insert the index corresponding to STRING into the current buffer."
176   (dolist (line (split-string string "\r\n"))
177     (unless (= (length line) 0)
178       (elopher-insert-index-record line))))
179
180 (defun elopher-insert-margin (&optional type-name)
181   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
182   (if type-name
183       (progn
184         (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
185                         (concat
186                          (propertize "[" 'face '(foreground-color . "blue"))
187                          (propertize type-name 'face '(foreground-color . "white"))
188                          (propertize "]" 'face '(foreground-color . "blue")))))
189         (insert " "))
190     (insert (make-string elopher-margin-width ?\s))))
191
192 (defun elopher-insert-index-record (line)
193   "Insert the index record corresponding to LINE into the current buffer."
194   (let* ((type (elt line 0))
195          (fields (split-string (substring line 1) "\t"))
196          (display-string (elt fields 0))
197          (selector (elt fields 1))
198          (host (elt fields 2))
199          (port (elt fields 3))
200          (address (elopher-make-address selector host port))
201          (help-string (format "mouse-1, RET: open %s on %s port %s"
202                               selector host port)))
203     (pcase type
204       (?i (elopher-insert-margin)
205           (insert (propertize display-string
206                               'face elopher-info-face)))
207       (?0 (elopher-insert-margin "T")
208           (insert-text-button display-string
209                               'face elopher-text-face
210                               'elopher-node (elopher-make-node elopher-current-node
211                                                                address
212                                                                #'elopher-get-text-node)
213                               'action #'elopher-click-link
214                               'follow-link t
215                               'help-echo help-string))
216       (?1 (elopher-insert-margin "/")
217           (insert-text-button display-string
218                               'face elopher-index-face
219                               'elopher-node (elopher-make-node elopher-current-node
220                                                                address
221                                                                #'elopher-get-index-node)
222                               'action #'elopher-click-link
223                               'follow-link t
224                               'help-echo help-string))
225       ((or ?g ?p ?I) (elopher-insert-margin "im")
226           (insert-text-button display-string
227                               'face elopher-image-face
228                               'elopher-node (elopher-make-node elopher-current-node
229                                                                address
230                                                                #'elopher-get-image-node)
231                               'action #'elopher-click-link
232                               'follow-link t
233                               'help-echo help-string))
234       (?7 (elopher-insert-margin "S")
235           (insert-text-button display-string
236                               'face elopher-search-face
237                               'elopher-node (elopher-make-node elopher-current-node
238                                                               address
239                                                               #'elopher-get-search-node)
240                               'action #'elopher-click-link
241                               'follow-link t
242                               'help-echo help-string))
243       (?h (elopher-insert-margin "W")
244           (let ((url (elt (split-string selector "URL:") 1)))
245             (insert-text-button display-string
246                                 'face elopher-http-face
247                                 'elopher-url url
248                                 'action #'elopher-click-url
249                                 'follow-link t
250                                 'help-echo (format "mouse-1, RET: open url %s" url))))
251       (?.) ; Occurs at end of index, can safely ignore.
252       (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
253           (insert (propertize display-string
254                               'face elopher-unknown-face))))
255     (insert "\n")))
256
257
258 ;;; Selector retrieval (all kinds)
259 ;;
260
261 (defvar elopher-selector-string)
262
263 (defun elopher-get-selector (address after)
264   "Retrieve selector specified by ADDRESS, then execute AFTER.
265 The result is stored as a string in the variable elopher-selector-string."
266   (setq elopher-selector-string "")
267   (let ((p (get-process "elopher-process")))
268     (if p (delete-process p)))
269   (make-network-process
270    :name "elopher-process"
271    :host (elopher-address-host address)
272    :service (elopher-address-port address)
273    :filter (lambda (proc string)
274              (setq elopher-selector-string (concat elopher-selector-string string)))
275    :sentinel after)
276   (process-send-string "elopher-process"
277                        (concat (elopher-address-selector address) "\n")))
278
279 ;; Index retrieval
280
281 (defun elopher-get-index-node ()
282   (let ((content (elopher-node-content elopher-current-node))
283         (address (elopher-node-address elopher-current-node)))
284     (if content
285         (progn
286           (elopher-with-clean-buffer
287             (insert content))
288           (elopher-restore-pos))
289       (if address
290           (progn
291             (elopher-with-clean-buffer
292              (insert "LOADING DIRECTORY..."))
293             (elopher-get-selector address
294                                   (lambda (proc event)
295                                     (elopher-with-clean-buffer
296                                       (elopher-insert-index elopher-selector-string))
297                                     (elopher-restore-pos)
298                                     (elopher-set-node-content elopher-current-node
299                                                               (buffer-string)))))
300         (progn
301           (elopher-with-clean-buffer
302             (elopher-insert-index elopher-start-index))
303           (elopher-restore-pos)
304           (elopher-set-node-content elopher-current-node
305                                     (buffer-string)))))))
306
307 ;; Text retrieval
308
309 (defun elopher-process-text (string)
310   (let ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string)))
311     (replace-regexp-in-string "\r" "" chopped-str)))
312
313 (defun elopher-get-text-node ()
314   (let ((content (elopher-node-content elopher-current-node))
315         (address (elopher-node-address elopher-current-node)))
316     (if content
317         (progn
318           (elopher-with-clean-buffer
319             (insert content))
320           (elopher-restore-pos))
321       (progn
322         (elopher-with-clean-buffer
323           (insert "LOADING TEXT..."))
324         (elopher-get-selector address
325                               (lambda (proc event)
326                                 (elopher-with-clean-buffer
327                                   (insert (elopher-process-text elopher-selector-string)))
328                                 (elopher-restore-pos)
329                                 (elopher-set-node-content elopher-current-node
330                                                           (buffer-string))))))))
331
332 ;; Image retrieval
333
334 (defun elopher-get-image-node ()
335   (let ((content (elopher-node-content elopher-current-node))
336         (address (elopher-node-address elopher-current-node)))
337     (if content
338         (progn
339           (elopher-with-clean-buffer
340             (insert-image content))
341           (setq cursor-type nil)
342           (elopher-restore-pos))
343       (progn
344         (elopher-with-clean-buffer
345           (insert "LOADING IMAGE..."))
346         (elopher-get-selector address
347                               (lambda (proc event)
348                                 (let ((image (create-image
349                                               (string-as-unibyte elopher-selector-string)
350                                               nil t)))
351                                   (elopher-with-clean-buffer
352                                    (insert-image image))
353                                   (setq cursor-type nil)
354                                   (elopher-restore-pos)
355                                   (elopher-set-node-content elopher-current-node image))))))))
356
357 ;; Search retrieval
358
359 (defun elopher-get-search-node ()
360   (let* ((content (elopher-node-content elopher-current-node))
361          (address (elopher-node-address elopher-current-node)))
362     (if content
363         (progn
364           (elopher-with-clean-buffer
365             (insert content))
366           (elopher-restore-pos)
367           (message "Displaying cached search results.  Reload to perform a new search."))
368       (let* ((query-string (read-string "Query: "))
369              (query-selector (concat (elopher-address-selector address) "\t" query-string))
370              (search-address (elopher-make-address query-selector
371                                                    (elopher-address-host address)
372                                                    (elopher-address-port address))))
373         (elopher-with-clean-buffer
374          (insert "LOADING RESULTS..."))
375         (elopher-get-selector search-address
376                               (lambda (proc event)
377                                 (elopher-with-clean-buffer
378                                   (elopher-insert-index elopher-selector-string))
379                                 (goto-char (point-min))
380                                 (elopher-set-node-content elopher-current-node
381                                                           (buffer-string))))))))
382
383 (defun elopher-get-node-raw ()
384   (let* ((content (elopher-node-content elopher-current-node))
385          (address (elopher-node-address elopher-current-node)))
386     (elopher-with-clean-buffer
387      (insert "LOADING RAW SERVER RESPONSE..."))
388     (if address
389         (elopher-get-selector address
390                               (lambda (proc event)
391                                 (elopher-with-clean-buffer
392                                  (insert elopher-selector-string))
393                                 (goto-char (point-min))))
394       (progn
395         (elopher-with-clean-buffer
396          (insert elopher-start-index))
397         (goto-char (point-min)))))
398   (message "Displaying raw server response.  Reload to return to standard view."))
399  
400
401 ;;; Navigation procedures
402 ;;
403
404 (defun elopher-next-link ()
405   (interactive)
406   (forward-button 1))
407
408 (defun elopher-prev-link ()
409   (interactive)
410   (backward-button 1))
411
412 (defun elopher-click-link (button)
413   (let ((node (button-get button 'elopher-node)))
414     (elopher-visit-node node)))
415
416 (defun elopher-click-url (button)
417   (let ((url (button-get button 'elopher-url)))
418     (if elopher-open-urls-with-eww
419         (browse-web url)
420       (browse-url url))))
421
422 (defun elopher-follow-closest-link ()
423   (interactive)
424   (push-button))
425
426 (defun elopher-go ()
427   "Go to a particular gopher site."
428   (interactive)
429   (let* (
430          (hostname (read-string "Gopher host: "))
431          (selector (read-string "Selector (default none): " nil nil ""))
432          (port (read-string "Port (default 70): " nil nil 70))
433          (address (list selector hostname port)))
434     (elopher-visit-node
435      (elopher-make-node elopher-current-node
436                         address
437                         #'elopher-get-index-node))))
438
439 (defun  elopher-reload ()
440   "Reload current page."
441   (interactive)
442   (elopher-reload-current-node))
443
444 (defun elopher-view-raw ()
445   "View current page as plain text."
446   (interactive)
447   (elopher-visit-node elopher-current-node t))
448
449 (defun elopher-back ()
450   "Go to previous site."
451   (interactive)
452   (if (elopher-node-parent elopher-current-node)
453       (elopher-visit-parent-node)
454     (message "No previous site.")))
455
456
457 ;;; Mode and keymap
458 ;;
459
460 (defvar elopher-mode-map
461   (let ((map (make-sparse-keymap)))
462     (define-key map (kbd "<tab>") 'elopher-next-link)
463     (define-key map (kbd "<S-tab>") 'elopher-prev-link)
464     (define-key map (kbd "u") 'elopher-back)
465     (define-key map (kbd "g") 'elopher-go)
466     (define-key map (kbd "r") 'elopher-reload)
467     (define-key map (kbd "w") 'elopher-view-raw)
468     (when (fboundp 'evil-define-key)
469       (evil-define-key 'normal map
470         (kbd "C-]") 'elopher-follow-closest-link
471         (kbd "C-t") 'elopher-back
472         (kbd "u") 'elopher-back
473         (kbd "g") 'elopher-go
474         (kbd "r") 'elopher-reload
475         (kbd "w") 'elopher-view-raw))
476     map)
477   "Keymap for gopher client.")
478
479 (define-derived-mode elopher-mode special-mode "elopher"
480   "Major mode for elopher, an elisp gopher client.")
481
482
483 ;;; Main start procedure
484 ;;
485 (defun elopher ()
486   "Start elopher with default landing page."
487   (interactive)
488   (setq elopher-current-node nil)
489   (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
490     (elopher-visit-node start-node)))
491
492 ;;; elopher.el ends here
493