Improved text getter, introduced raw getter.
[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 - t: display the current page as text (i.e. \"source\")\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             ;; (let ((inhibit-read-only t))
294               ;; (insert "LOADING DIRECTORY..."))
295             (elopher-get-selector address
296                                   (lambda (proc event)
297                                     (elopher-with-clean-buffer
298                                       (elopher-insert-index elopher-selector-string))
299                                     (elopher-restore-pos)
300                                     (elopher-set-node-content elopher-current-node
301                                                               (buffer-string)))))
302         (progn
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)))))))
308
309 ;; Text retrieval
310
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)))
314
315 (defun elopher-get-text-node ()
316   (let ((content (elopher-node-content elopher-current-node))
317         (address (elopher-node-address elopher-current-node)))
318     (if content
319         (progn
320           (elopher-with-clean-buffer
321             (insert content))
322           (elopher-restore-pos))
323       (progn
324         (elopher-with-clean-buffer
325           (insert "LOADING TEXT..."))
326         (elopher-get-selector address
327                               (lambda (proc event)
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))))))))
333
334 ;; Image retrieval
335
336 (defun elopher-get-image-node ()
337   (let ((content (elopher-node-content elopher-current-node))
338         (address (elopher-node-address elopher-current-node)))
339     (if content
340         (progn
341           (elopher-with-clean-buffer
342             (insert-image content))
343           (setq cursor-type nil)
344           (elopher-restore-pos))
345       (progn
346         (elopher-with-clean-buffer
347           (insert "LOADING IMAGE..."))
348         (elopher-get-selector address
349                               (lambda (proc event)
350                                 (let ((image (create-image
351                                               (string-as-unibyte elopher-selector-string)
352                                               nil t)))
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))))))))
358
359 ;; Search retrieval
360
361 (defun elopher-get-search-node ()
362   (let* ((content (elopher-node-content elopher-current-node))
363          (address (elopher-node-address elopher-current-node)))
364     (if content
365         (progn
366           (elopher-with-clean-buffer
367             (insert content))
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
378                               (lambda (proc event)
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))))))))
384
385 (defun elopher-get-node-raw ()
386   (let* ((content (elopher-node-content elopher-current-node))
387          (address (elopher-node-address elopher-current-node)))
388     (elopher-with-clean-buffer
389      (insert "LOADING RAW SERVER RESPONSE..."))
390     (if address
391         (elopher-get-selector address
392                               (lambda (proc event)
393                                 (elopher-with-clean-buffer
394                                  (insert elopher-selector-string))
395                                 (goto-char (point-min))))
396       (progn
397         (elopher-with-clean-buffer
398          (insert elopher-start-index))
399         (goto-char (point-min)))))
400   (message "Displaying raw server response.  Reload to return to standard view."))
401  
402
403 ;;; Navigation procedures
404 ;;
405
406 (defun elopher-next-link ()
407   (interactive)
408   (forward-button 1))
409
410 (defun elopher-prev-link ()
411   (interactive)
412   (backward-button 1))
413
414 (defun elopher-click-link (button)
415   (let ((node (button-get button 'elopher-node)))
416     (elopher-visit-node node)))
417
418 (defun elopher-click-url (button)
419   (let ((url (button-get button 'elopher-url)))
420     (if elopher-open-urls-with-eww
421         (browse-web url)
422       (browse-url url))))
423
424 (defun elopher-follow-closest-link ()
425   (interactive)
426   (push-button))
427
428 (defun elopher-go ()
429   "Go to a particular gopher site."
430   (interactive)
431   (let* (
432          (hostname (read-string "Gopher host: "))
433          (selector (read-string "Selector (default none): " nil nil ""))
434          (port (read-string "Port (default 70): " nil nil 70))
435          (address (list selector hostname port)))
436     (elopher-visit-node
437      (elopher-make-node elopher-current-node
438                         address
439                         #'elopher-get-index-node))))
440
441 (defun  elopher-reload ()
442   "Reload current page."
443   (interactive)
444   (elopher-reload-current-node))
445
446 (defun elopher-view-raw ()
447   "View current page as plain text."
448   (interactive)
449   (elopher-visit-node elopher-current-node t))
450
451 (defun elopher-back ()
452   "Go to previous site."
453   (interactive)
454   (if (elopher-node-parent elopher-current-node)
455       (elopher-visit-parent-node)
456     (message "No previous site.")))
457
458
459 ;;; Mode and keymap
460 ;;
461
462 (defvar elopher-mode-map
463   (let ((map (make-sparse-keymap)))
464     (define-key map (kbd "<tab>") 'elopher-next-link)
465     (define-key map (kbd "<S-tab>") 'elopher-prev-link)
466     (define-key map (kbd "u") 'elopher-back)
467     (define-key map (kbd "g") 'elopher-go)
468     (define-key map (kbd "r") 'elopher-reload)
469     (define-key map (kbd "w") 'elopher-view-raw)
470     (when (fboundp 'evil-define-key)
471       (evil-define-key 'normal map
472         (kbd "C-]") 'elopher-follow-closest-link
473         (kbd "C-t") 'elopher-back
474         (kbd "u") 'elopher-back
475         (kbd "g") 'elopher-go
476         (kbd "r") 'elopher-reload
477         (kbd "w") 'elopher-view-raw))
478     map)
479   "Keymap for gopher client.")
480
481 (define-derived-mode elopher-mode special-mode "elopher"
482   "Major mode for elopher, an elisp gopher client.")
483
484
485 ;;; Main start procedure
486 ;;
487 (defun elopher ()
488   "Start elopher with default landing page."
489   (interactive)
490   (setq elopher-current-node nil)
491   (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
492     (elopher-visit-node start-node)))
493
494 ;;; elopher.el ends here
495