-;;; elopher.el --- gopher client
+;;; elopher.el --- elisp gopher client
;;; Commentary:
-;; Simple gopher client in elisp.
+;; An elisp gopher client.
;;; Code:
"i\tfake\tfake\t1"
"7Veronica-2 Gopher Search Engine\t/v2/vs\tgopher.floodgap.com\t70"
".")
- "\r\n"))
+ "\r\n")
+ "Source for elopher start page.")
;;; Customization group
;;
(defgroup elopher nil
- "A simple gopher client."
+ "A gopher client."
:group 'applications)
-(defcustom elopher-index-face '(foreground-color . "cyan")
- "Face used for index records."
- :type '(face))
+(defface elopher-index
+ '((((background dark)) :foreground "deep sky blue")
+ (((background light)) :foreground "blue"))
+ "Face used for index records.")
-(defcustom elopher-text-face '(foreground-color . "white")
- "Face used for text records."
- :type '(face))
+(defface elopher-text
+ '((default :weight bold)
+ (((background dark)) :foreground "white"))
+ "Face used for text records.")
-(defcustom elopher-info-face '(foreground-color . "gray")
- "Face used for info records."
- :type '(face))
+(defface elopher-info '()
+ "Face used for info records.")
-(defcustom elopher-image-face '(foreground-color . "green")
- "Face used for image records."
- :type '(face))
+(defface elopher-image
+ '((((background dark)) :foreground "green")
+ (t :foreground "dark green"))
+ "Face used for image records.")
-(defcustom elopher-search-face '(foreground-color . "orange")
- "Face used for image records."
- :type '(face))
+(defface elopher-search
+ '((((background light)) :foreground "orange")
+ (((background dark)) :foreground "dark orange"))
+ "Face used for search records.")
-(defcustom elopher-http-face '(foreground-color . "yellow")
- "Face used for image records."
- :type '(face))
+(defface elopher-url
+ '((((background dark)) :foreground "yellow")
+ (((background light)) :foreground "dark red"))
+ "Face used for url records.")
-(defcustom elopher-binary-face '(foreground-color . "magenta")
- "Face used for image records."
- :type '(face))
+(defface elopher-binary
+ '((t :foreground "magenta"))
+ "Face used for binary records.")
-(defcustom elopher-unknown-face '(foreground-color . "red")
- "Face used for unknown record types."
- :type '(face))
+(defface elopher-unknown
+ '((t :foreground "red"))
+ "Face used for unknown record types.")
+
+(defface elopher-margin-key
+ '((((background dark)) :foreground "white"))
+ "Face used for margin key.")
+
+(defface elopher-margin-brackets
+ '((t :foreground "blue"))
+ "Face used for brackets around margin key.")
(defcustom elopher-open-urls-with-eww nil
"If non-nil, open URL selectors using eww.
(progn
(insert (format (concat "%" (number-to-string (- elopher-margin-width 1)) "s")
(concat
- (propertize "[" 'face '(foreground-color . "blue"))
- (propertize type-name 'face '(foreground-color . "white"))
- (propertize "]" 'face '(foreground-color . "blue")))))
+ (propertize "[" 'face 'elopher-margin-brackets)
+ (propertize type-name 'face 'elopher-margin-key)
+ (propertize "]" 'face 'elopher-margin-brackets))))
(insert " "))
(insert (make-string elopher-margin-width ?\s))))
+(defvar elopher-type-map
+ '((?0 elopher-get-text-node "T" elopher-text)
+ (?1 elopher-get-index-node "/" elopher-index)
+ (?g elopher-get-image-node "im" elopher-image)
+ (?p elopher-get-image-node "im" elopher-image)
+ (?I elopher-get-image-node "im" elopher-image)
+ (?4 elopher-get-node-download "B" elopher-binary)
+ (?5 elopher-get-node-download "B" elopher-binary)
+ (?9 elopher-get-node-download "B" elopher-binary)
+ (?7 elopher-get-search-node "?" elopher-search))
+ "Association list from types to getters, margin codes and index faces.")
+
(defun elopher-insert-index-record (line)
"Insert the index record corresponding to LINE into the current buffer."
(let* ((type (elt line 0))
(host (elt fields 2))
(port (elt fields 3))
(address (elopher-make-address selector host port))
- (help-string (format "mouse-1, RET: open %s on %s port %s"
- selector host port)))
- (pcase type
- (?i (elopher-insert-margin) ; Information
- (insert (propertize display-string
- 'face elopher-info-face)))
- (?0 (elopher-insert-margin "T") ; Text
- (insert-text-button display-string
- 'face elopher-text-face
- 'elopher-node (elopher-make-node elopher-current-node
- address
- #'elopher-get-text-node)
- 'action #'elopher-click-link
- 'follow-link t
- 'help-echo help-string))
- (?1 (elopher-insert-margin "/") ; Index
+ (type-map-entry (alist-get type elopher-type-map)))
+ (if type-map-entry
+ (let ((getter (car type-map-entry))
+ (margin-code (cadr type-map-entry))
+ (face (caddr type-map-entry)))
+ (elopher-insert-margin margin-code)
(insert-text-button display-string
- 'face elopher-index-face
+ 'face face
'elopher-node (elopher-make-node elopher-current-node
address
- #'elopher-get-index-node)
+ getter)
'action #'elopher-click-link
'follow-link t
- 'help-echo help-string))
- ((or ?g ?p ?I) (elopher-insert-margin "im") ; Image
- (insert-text-button display-string
- 'face elopher-image-face
- 'elopher-node (elopher-make-node elopher-current-node
- address
- #'elopher-get-image-node)
- 'action #'elopher-click-link
- 'follow-link t
- 'help-echo help-string))
- ((or ?4 ?9) (elopher-insert-margin "B") ; Binary
- (insert-text-button display-string
- 'face elopher-binary-face
- 'elopher-node (elopher-make-node elopher-current-node
- address
- #'elopher-get-node-download)
- 'action #'elopher-click-link
- 'follow-link t
- 'help-echo help-string))
- (?7 (elopher-insert-margin "S") ; Query
- (insert-text-button display-string
- 'face elopher-search-face
- 'elopher-node (elopher-make-node elopher-current-node
- address
- #'elopher-get-search-node)
- 'action #'elopher-click-link
- 'follow-link t
- 'help-echo help-string))
- (?h (elopher-insert-margin "W") ; Web link
- (let ((url (elt (split-string selector "URL:") 1)))
- (insert-text-button display-string
- 'face elopher-http-face
- 'elopher-url url
- 'action #'elopher-click-url
- 'follow-link t
- 'help-echo (format "mouse-1, RET: open url %s" url))))
- (?.) ; Occurs at end of index, can safely ignore.
- (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
- (insert (propertize display-string
- 'face elopher-unknown-face))))
+ 'help-echo (format "mouse-1, RET: open %s on %s port %s"
+ selector host port)))
+ (pcase type
+ (?i (elopher-insert-margin) ; Information
+ (insert (propertize display-string
+ 'face 'elopher-info)))
+ (?h (elopher-insert-margin "W") ; Web link
+ (let ((url (elt (split-string selector "URL:") 1)))
+ (insert-text-button display-string
+ 'face 'elopher-url
+ 'elopher-url url
+ 'action #'elopher-click-url
+ 'follow-link t
+ 'help-echo (format "mouse-1, RET: open url %s" url))))
+ (?.) ; Occurs at end of index, can safely ignore.
+ (tp (elopher-insert-margin (concat (char-to-string tp) "?"))
+ (insert (propertize display-string
+ 'face elopher-unknown-face)))))
(insert "\n")))
;; Text retrieval
-(defun elopher-process-text (string)
- (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
- (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
- (elopher-buttonify-urls cleaned-str)))
-
-(defvar elopher-url-regex "\\https?://\\([a-z.\-]+\\)\\([^ \r\n\t(),]*\\)")
+(defconst elopher-url-regex "\\(https?\\|gopher\\)://\\([a-zA-Z0-9.\-]+\\)\\(?3::[0-9]+\\)?\\(?4:/[^ \r\n\t(),]*\\)?"
+ "Regexp used to locate and buttinofy URLs in text files loaded by elopher.")
(defun elopher-buttonify-urls (string)
+ "Turn substrings which look like urls in STRING into clickable buttons."
(with-temp-buffer
(insert string)
(goto-char (point-min))
(while (re-search-forward elopher-url-regex nil t)
- (let ((url (match-string 0)))
- (make-text-button (match-beginning 0)
- (match-end 0)
- 'elopher-url url
- 'action #'elopher-click-url
- 'follow-link t
- 'help-echo (format "mouse-1, RET: open url %s" url))))
+ (let ((url (match-string 0))
+ (protocol (downcase (match-string 1))))
+ (if (string= protocol "gopher")
+ (let* ((host (match-string 2))
+ (port 70)
+ (type-and-selector (match-string 4))
+ (type (if (> (length type-and-selector) 1)
+ (elt type-and-selector 1)
+ ?1))
+ (selector (if (> (length type-and-selector) 1)
+ (substring type-and-selector 2)
+ ""))
+ (address (elopher-make-address selector host port))
+ (getter (car (alist-get type elopher-type-map))))
+ (make-text-button (match-beginning 0)
+ (match-end 0)
+ 'elopher-node (elopher-make-node elopher-current-node
+ address
+ getter)
+ 'action #'elopher-click-link
+ 'follow-link t
+ 'help-echo (format "mouse-1, RET: open %s on %s port %s"
+ selector host port)))
+ (make-text-button (match-beginning 0)
+ (match-end 0)
+ 'elopher-url url
+ 'action #'elopher-click-url
+ 'follow-link t
+ 'help-echo (format "mouse-1, RET: open url %s" url)))))
(buffer-string)))
+(defun elopher-process-text (string)
+ (let* ((chopped-str (replace-regexp-in-string "\r\n\.\r\n$" "\r\n" string))
+ (cleaned-str (replace-regexp-in-string "\r" "" chopped-str)))
+ (elopher-buttonify-urls cleaned-str)))
+
(defun elopher-get-text-node ()
(let ((content (elopher-node-content elopher-current-node))
(address (elopher-node-address elopher-current-node)))
;; Search retrieval
(defun elopher-get-search-node ()
- (let* ((content (elopher-node-content elopher-current-node))
- (address (elopher-node-address elopher-current-node)))
+ (let ((content (elopher-node-content elopher-current-node))
+ (address (elopher-node-address elopher-current-node))
+ (aborted t))
(if content
(progn
(elopher-with-clean-buffer
- (insert content))
+ (insert content))
(elopher-restore-pos)
(message "Displaying cached search results. Reload to perform a new search."))
- (let* ((query-string (read-string "Query: "))
- (query-selector (concat (elopher-address-selector address) "\t" query-string))
- (search-address (elopher-make-address query-selector
- (elopher-address-host address)
- (elopher-address-port address))))
- (elopher-with-clean-buffer
- (insert "LOADING RESULTS..."))
- (elopher-get-selector search-address
- (lambda (proc event)
- (unless (string-prefix-p "deleted" event)
- (elopher-with-clean-buffer
- (elopher-insert-index elopher-selector-string))
- (goto-char (point-min))
- (elopher-set-node-content elopher-current-node
- (buffer-string)))))))))
+ (unwind-protect
+ (let* ((query-string (read-string "Query: "))
+ (query-selector (concat (elopher-address-selector address) "\t" query-string))
+ (search-address (elopher-make-address query-selector
+ (elopher-address-host address)
+ (elopher-address-port address))))
+ (setq aborted nil)
+ (elopher-with-clean-buffer
+ (insert "LOADING RESULTS..."))
+ (elopher-get-selector search-address
+ (lambda (proc event)
+ (unless (string-prefix-p "deleted" event)
+ (elopher-with-clean-buffer
+ (elopher-insert-index elopher-selector-string))
+ (goto-char (point-min))
+ (elopher-set-node-content elopher-current-node
+ (buffer-string))))))
+ (if aborted
+ (elopher-visit-parent-node))))))
;; Raw server response retrieval
(goto-char (point-min)))))
(message "Displaying raw server response. Reload to return to standard view."))
-
;; File export retrieval
(defvar elopher-download-filename)
(interactive)
(let ((button (button-at (point))))
(if button
- (elopher-visit-node (button-get button 'elopher-node)
- #'elopher-get-node-download)
+ (let ((node (button-get button 'elopher-node)))
+ (if node
+ (elopher-visit-node (button-get button 'elopher-node)
+ #'elopher-get-node-download)
+ (message "Can only download gopher links, not general URLs.")))
(message "No link selected."))))
-
;;; Mode and keymap
;;
"Started Elopher.") ; Otherwise (elopher) evaluates to start page string.
;;; elopher.el ends here
-