Fixed search 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
20    'identity
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"
26          "i\tfake\tfake\t1"
27          "iBasic usage:\tfake\tfake\t1"
28          "i\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"
36          "i\tfake\tfake\t1"
37          "iPlaces to start exploring Gopherspace:\tfake\tfake\t1"
38          "i\tfake\tfake\t1"
39          "1Floodgap Systems Gopher Server\t\tgopher.floodgap.com\t70"
40          "i\tfake\tfake\t1"
41          "iAlternatively, select the following item and enter some\tfake\tfake\t1"
42          "isearch terms:\tfake\tfake\t1"
43          "i\tfake\tfake\t1"
44          "7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
45          ".")
46    "\r\n"))
47
48
49 ;;; Customization group
50 ;;
51
52 (defgroup elopher nil
53   "A simple gopher client."
54   :group 'applications)
55
56 (defcustom elopher-index-face '(foreground-color . "cyan")
57   "Face used for index records."
58   :type '(face))
59
60 (defcustom elopher-text-face '(foreground-color . "white")
61   "Face used for text records."
62   :type '(face))
63
64 (defcustom elopher-info-face '(foreground-color . "gray")
65   "Face used for info records."
66   :type '(face))
67
68 (defcustom elopher-image-face '(foreground-color . "green")
69   "Face used for image records."
70   :type '(face))
71
72 (defcustom elopher-search-face '(foreground-color . "orange")
73   "Face used for image records."
74   :type '(face))
75
76 (defcustom elopher-http-face '(foreground-color . "yellow")
77   "Face used for image records."
78   :type '(face))
79
80 (defcustom elopher-binary-face '(foreground-color . "magenta")
81   "Face used for image records."
82   :type '(face))
83
84 (defcustom elopher-unknown-face '(foreground-color . "red")
85   "Face used for unknown record types."
86   :type '(face))
87
88 (defcustom elopher-open-urls-with-eww nil
89   "If non-nil, open URL selectors using eww.
90 Otherwise, use the system browser via the BROWSE-URL function."
91   :type '(boolean))
92
93 ;;; Model
94 ;;
95
96 ;; Address
97
98 (defun elopher-make-address (selector host port)
99   (list selector host port))
100
101 (defun elopher-address-selector (address)
102   (car address))
103
104 (defun elopher-address-host (address)
105   (cadr address))
106
107 (defun elopher-address-port (address)
108   (caddr address))
109
110 ;; Node
111
112 (defun elopher-make-node (parent address getter &optional content pos)
113   (list parent address getter content pos))
114
115 (defun elopher-node-parent (node)
116   (elt node 0))
117
118 (defun elopher-node-address (node)
119   (elt node 1))
120
121 (defun elopher-node-getter (node)
122   (elt node 2))
123
124 (defun elopher-node-content (node)
125   (elt node 3))
126
127 (defun elopher-node-pos (node)
128   (elt node 4))
129
130 (defun elopher-set-node-content (node content)
131   (setcar (nthcdr 3 node) content))
132
133 (defun elopher-set-node-pos (node pos)
134   (setcar (nthcdr 4 node) pos))
135
136 (defun elopher-save-pos ()
137   (when elopher-current-node
138     (elopher-set-node-pos elopher-current-node (point))))
139
140 (defun elopher-restore-pos ()
141   (let ((pos (elopher-node-pos elopher-current-node)))
142     (if pos
143         (goto-char pos)
144       (goto-char (point-min)))))
145
146 ;; Node graph traversal
147
148 (defvar elopher-current-node)
149
150 (defun elopher-visit-node (node &optional getter)
151   (elopher-save-pos)
152   (elopher-process-cleanup)
153   (setq elopher-current-node node)
154   (if getter
155       (funcall getter)
156     (funcall (elopher-node-getter node))))
157
158 (defun elopher-visit-parent-node ()
159   (let ((parent-node (elopher-node-parent elopher-current-node)))
160     (when parent-node
161       (elopher-visit-node parent-node))))
162       
163 (defun elopher-reload-current-node ()
164   (elopher-set-node-content elopher-current-node nil)
165   (elopher-visit-node elopher-current-node))
166
167 ;;; Buffer preparation
168 ;;
169
170 (defmacro elopher-with-clean-buffer (&rest args)
171   "Evaluate ARGS with a clean *elopher* buffer as current."
172   (list 'progn
173         '(switch-to-buffer "*elopher*")
174         '(elopher-mode)
175         (append (list 'let '((inhibit-read-only t))
176                       '(erase-buffer))
177                 args)))
178
179 ;;; Index rendering
180 ;;
181
182 (defun elopher-insert-index (string)
183   "Insert the index corresponding to STRING into the current buffer."
184   (dolist (line (split-string string "\r\n"))
185     (unless (= (length line) 0)
186       (elopher-insert-index-record line))))
187
188 (defun elopher-insert-margin (&optional type-name)
189   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
190   (if type-name
191       (progn
192         (insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
193                         (concat
194                          (propertize "[" 'face '(foreground-color . "blue"))
195                          (propertize type-name 'face '(foreground-color . "white"))
196                          (propertize "]" 'face '(foreground-color . "blue")))))
197         (insert " "))
198     (insert (make-string elopher-margin-width ?\s))))
199
200 (defvar elopher-type-map
201   `((?0 elopher-get-text-node "T" ,elopher-text-face)
202     (?1 elopher-get-index-node "/" ,elopher-index-face)
203     (?g elopher-get-image-node "im" ,elopher-image-face)
204     (?p elopher-get-image-node "im" ,elopher-image-face)
205     (?I elopher-get-image-node "im" ,elopher-image-face)
206     (?4 elopher-get-node-download "B" ,elopher-binary-face)
207     (?5 elopher-get-node-download "B" ,elopher-binary-face)
208     (?9 elopher-get-node-download "B" ,elopher-binary-face)
209     (?7 elopher-get-search-node "?" ,elopher-search-face)))
210
211 (defun elopher-insert-index-record (line)
212   "Insert the index record corresponding to LINE into the current buffer."
213   (let* ((type (elt line 0))
214          (fields (split-string (substring line 1) "\t"))
215          (display-string (elt fields 0))
216          (selector (elt fields 1))
217          (host (elt fields 2))
218          (port (elt fields 3))
219          (address (elopher-make-address selector host port))
220          (type-map-entry (alist-get type elopher-type-map)))
221     (if type-map-entry
222         (let ((getter (car type-map-entry))
223               (margin-code (cadr type-map-entry))
224               (face (caddr type-map-entry)))
225           (elopher-insert-margin margin-code)
226           (insert-text-button display-string
227                               'face face
228                               'elopher-node (elopher-make-node elopher-current-node
229                                                                address
230                                                                getter)
231                               'action #'elopher-click-link
232                               'follow-link t
233                               'help-echo (format "mouse-1, RET: open %s on %s port %s"
234                                                  selector host port)))
235       (pcase type
236         (?i (elopher-insert-margin) ; Information 
237             (insert (propertize display-string
238                                 'face elopher-info-face)))
239         (?h (elopher-insert-margin "W") ; Web link
240             (let ((url (elt (split-string selector "URL:") 1)))
241               (insert-text-button display-string
242                                   'face elopher-http-face
243                                   'elopher-url url
244                                   'action #'elopher-click-url
245                                   'follow-link t
246                                   'help-echo (format "mouse-1, RET: open url %s" url))))
247         (?.) ; Occurs at end of index, can safely ignore.
248         (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
249             (insert (propertize display-string
250                                 'face elopher-unknown-face)))))
251     (insert "\n")))
252
253
254 ;;; Selector retrieval (all kinds)
255 ;;
256
257 (defun elopher-process-cleanup ()
258   "Immediately shut down any extant elopher process."
259   (let ((p (get-process "elopher-process")))
260     (if p (delete-process p))))
261
262 (defvar elopher-selector-string)
263
264 (defun elopher-get-selector (address after)
265   "Retrieve selector specified by ADDRESS, then execute AFTER.
266 The result is stored as a string in the variable elopher-selector-string."
267   (setq elopher-selector-string "")
268   (make-network-process
269    :name "elopher-process"
270    :host (elopher-address-host address)
271    :service (elopher-address-port address)
272    :filter (lambda (proc string)
273              (setq elopher-selector-string (concat elopher-selector-string string)))
274    :sentinel after)
275   (process-send-string "elopher-process"
276                        (concat (elopher-address-selector address) "\n")))
277
278 ;; Index retrieval
279
280 (defun elopher-get-index-node ()
281   (let ((content (elopher-node-content elopher-current-node))
282         (address (elopher-node-address elopher-current-node)))
283     (if content
284         (progn
285           (elopher-with-clean-buffer
286            (insert content))
287           (elopher-restore-pos))
288       (if address
289           (progn
290             (elopher-with-clean-buffer
291              (insert "LOADING DIRECTORY..."))
292             (elopher-get-selector address
293                                   (lambda (proc event)
294                                     (unless (string-prefix-p "deleted" 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 (defvar elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)")
310
311 (defun elopher-buttonify-urls (string)
312   "Turn substrings which look like urls in STRING into clickable buttons."
313   (with-temp-buffer
314     (insert string)
315     (goto-char (point-min))
316     (while (re-search-forward elopher-url-regex nil t)
317       (let ((url (match-string 0))
318             (protocol (downcase (match-string 1))))
319         (if (string= protocol "gopher")
320             (let* ((host (match-string 2))
321                    (port 70)
322                    (type-and-selector (match-string 4))
323                    (type (if (> (length type-and-selector) 1)
324                              (elt type-and-selector 1)
325                            ?1))
326                    (selector (if (> (length type-and-selector) 1)
327                                  (substring type-and-selector 2)
328                                ""))
329                    (address (elopher-make-address selector host port))
330                    (getter (car (alist-get type elopher-type-map))))
331               (make-text-button (match-beginning 0)
332                                 (match-end 0)
333                                 'elopher-node (elopher-make-node elopher-current-node
334                                                                  address
335                                                                  getter)
336                                 'action #'elopher-click-link
337                                 'follow-link t
338                                 'help-echo (format "mouse-1, RET: open %s on %s port %s"
339                                                    selector host port)))
340           (make-text-button (match-beginning 0)
341                             (match-end 0)
342                             'elopher-url url
343                             'action #'elopher-click-url
344                             'follow-link t
345                             'help-echo (format "mouse-1, RET: open url %s" url)))))
346     (buffer-string)))
347
348 (defun elopher-process-text (string)
349   (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
350          (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
351     (elopher-buttonify-urls cleaned-str)))
352
353 (defun elopher-get-text-node ()
354   (let ((content (elopher-node-content elopher-current-node))
355         (address (elopher-node-address elopher-current-node)))
356     (if content
357         (progn
358           (elopher-with-clean-buffer
359            (insert content))
360           (elopher-restore-pos))
361       (progn
362         (elopher-with-clean-buffer
363          (insert "LOADING TEXT..."))
364         (elopher-get-selector address
365                               (lambda (proc event)
366                                 (unless (string-prefix-p "deleted" event)
367                                   (elopher-with-clean-buffer
368                                    (insert (elopher-process-text elopher-selector-string)))
369                                   (elopher-restore-pos)
370                                   (elopher-set-node-content elopher-current-node
371                                                             (buffer-string)))))))))
372
373 ;; Image retrieval
374
375 (defun elopher-get-image-node ()
376   (let ((content (elopher-node-content elopher-current-node))
377         (address (elopher-node-address elopher-current-node)))
378     (if content
379         (progn
380           (elopher-with-clean-buffer
381            (insert-image content))
382           (setq cursor-type nil)
383           (elopher-restore-pos))
384       (progn
385         (elopher-with-clean-buffer
386          (insert "LOADING IMAGE..."))
387         (elopher-get-selector address
388                               (lambda (proc event)
389                                 (unless (string-prefix-p "deleted" event)
390                                   (let ((image (create-image
391                                                 (string-as-unibyte elopher-selector-string)
392                                                 nil t)))
393                                     (elopher-with-clean-buffer
394                                      (insert-image image))
395                                     (setq cursor-type nil)
396                                     (elopher-restore-pos)
397                                     (elopher-set-node-content elopher-current-node
398                                                               image)))))))))
399
400 ;; Search retrieval
401
402 (defun elopher-get-search-node ()
403   (let ((content (elopher-node-content elopher-current-node))
404         (address (elopher-node-address elopher-current-node))
405         (aborted t))
406     (if content
407         (progn
408           (elopher-with-clean-buffer
409            (insert content))
410           (elopher-restore-pos)
411           (message "Displaying cached search results.  Reload to perform a new search."))
412       (unwind-protect
413           (let* ((query-string (read-string "Query: "))
414                  (query-selector (concat (elopher-address-selector address) "\t" query-string))
415                  (search-address (elopher-make-address query-selector
416                                                        (elopher-address-host address)
417                                                        (elopher-address-port address))))
418             (setq aborted nil)
419             (elopher-with-clean-buffer
420              (insert "LOADING RESULTS..."))
421             (elopher-get-selector search-address
422                                   (lambda (proc event)
423                                     (unless (string-prefix-p "deleted" event)
424                                       (elopher-with-clean-buffer
425                                        (elopher-insert-index elopher-selector-string))
426                                       (goto-char (point-min))
427                                       (elopher-set-node-content elopher-current-node
428                                                                 (buffer-string))))))
429         (if aborted
430             (elopher-visit-parent-node))))))
431
432 ;; Raw server response retrieval
433
434 (defun elopher-get-node-raw ()
435   (let* ((content (elopher-node-content elopher-current-node))
436          (address (elopher-node-address elopher-current-node)))
437     (elopher-with-clean-buffer
438      (insert "LOADING RAW SERVER RESPONSE..."))
439     (if address
440         (elopher-get-selector address
441                               (lambda (proc event)
442                                 (unless (string-prefix-p "deleted" event)
443                                   (elopher-with-clean-buffer
444                                    (insert elopher-selector-string))
445                                   (goto-char (point-min)))))
446       (progn
447         (elopher-with-clean-buffer
448          (insert elopher-start-index))
449         (goto-char (point-min)))))
450   (message "Displaying raw server response.  Reload to return to standard view."))
451  
452 ;; File export retrieval
453
454 (defvar elopher-download-filename)
455
456 (defun elopher-get-node-download ()
457   (let* ((address (elopher-node-address elopher-current-node))
458          (selector (elopher-address-selector address)))
459     (elopher-visit-parent-node) ; Do first in case of non-local exits.
460     (let* ((filename-proposal (file-name-nondirectory selector))
461            (filename (read-file-name "Save file as: "
462                                      nil nil nil
463                                      (if (> (length filename-proposal) 0)
464                                          filename-proposal
465                                        "gopher.file"))))
466       (message "Downloading...")
467       (setq elopher-download-filename filename)
468       (elopher-get-selector address
469                             (lambda (proc event)
470                               (let ((coding-system-for-write 'binary))
471                                 (with-temp-file elopher-download-filename
472                                   (insert elopher-selector-string)
473                                   (message (format "Download complate, saved to file %s."
474                                                    elopher-download-filename)))))))))
475
476
477 ;;; Navigation procedures
478 ;;
479
480 (defun elopher-next-link ()
481   (interactive)
482   (forward-button 1))
483
484 (defun elopher-prev-link ()
485   (interactive)
486   (backward-button 1))
487
488 (defun elopher-click-link (button)
489   (let ((node (button-get button 'elopher-node)))
490     (elopher-visit-node node)))
491
492 (defun elopher-click-url (button)
493   (let ((url (button-get button 'elopher-url)))
494     (if elopher-open-urls-with-eww
495         (browse-web url)
496       (browse-url url))))
497
498 (defun elopher-follow-closest-link ()
499   (interactive)
500   (push-button))
501
502 (defun elopher-go ()
503   "Go to a particular gopher site."
504   (interactive)
505   (let* (
506          (hostname (read-string "Gopher host: "))
507          (selector (read-string "Selector (default none): " nil nil ""))
508          (port (read-string "Port (default 70): " nil nil 70))
509          (address (list selector hostname port)))
510     (elopher-visit-node
511      (elopher-make-node elopher-current-node
512                         address
513                         #'elopher-get-index-node))))
514
515 (defun  elopher-reload ()
516   "Reload current page."
517   (interactive)
518   (elopher-reload-current-node))
519
520 (defun elopher-view-raw ()
521   "View current page as plain text."
522   (interactive)
523   (elopher-visit-node elopher-current-node
524                       #'elopher-get-node-raw))
525
526 (defun elopher-back ()
527   "Go to previous site."
528   (interactive)
529   (if (elopher-node-parent elopher-current-node)
530       (elopher-visit-parent-node)
531     (message "No previous site.")))
532
533 (defun elopher-download ()
534   "Download the link at point."
535   (interactive)
536   (let ((button (button-at (point))))
537     (if button
538         (let ((node (button-get button 'elopher-node)))
539           (if node
540               (elopher-visit-node (button-get button 'elopher-node)
541                                   #'elopher-get-node-download)
542             (message "Can only download gopher links, not general URLs.")))
543       (message "No link selected."))))
544
545 ;;; Mode and keymap
546 ;;
547
548 (defvar elopher-mode-map
549   (let ((map (make-sparse-keymap)))
550     (define-key map (kbd "<tab>") 'elopher-next-link)
551     (define-key map (kbd "<S-tab>") 'elopher-prev-link)
552     (define-key map (kbd "u") 'elopher-back)
553     (define-key map (kbd "g") 'elopher-go)
554     (define-key map (kbd "r") 'elopher-reload)
555     (define-key map (kbd "w") 'elopher-view-raw)
556     (define-key map (kbd "d") 'elopher-download)
557     (when (fboundp 'evil-define-key)
558       (evil-define-key 'normal map
559         (kbd "C-]") 'elopher-follow-closest-link
560         (kbd "C-t") 'elopher-back
561         (kbd "u") 'elopher-back
562         (kbd "g") 'elopher-go
563         (kbd "r") 'elopher-reload
564         (kbd "w") 'elopher-view-raw
565         (kbd "d") 'elopher-download))
566     map)
567   "Keymap for gopher client.")
568
569 (define-derived-mode elopher-mode special-mode "elopher"
570   "Major mode for elopher, an elisp gopher client.")
571
572
573 ;;; Main start procedure
574 ;;
575
576 (defun elopher ()
577   "Start elopher with default landing page."
578   (interactive)
579   (setq elopher-current-node nil)
580   (let ((start-node (elopher-make-node nil nil #'elopher-get-index-node)))
581     (elopher-visit-node start-node))
582   "Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
583
584 ;;; elopher.el ends here
585