X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=blobdiff_plain;f=elopher.el;h=609876db1876a03eae17f6a8e7ae84f3768f1480;hb=e4e93510ae1b8adac371c955ff600afbade58c05;hp=b28120badf3bd3606ec96fd60ebf88f39282ee63;hpb=42b79ed99012e13fb25ce47a59272c20ab3f5d18;p=elpher.git diff --git a/elopher.el b/elopher.el index b28120b..609876d 100644 --- a/elopher.el +++ b/elopher.el @@ -7,57 +7,142 @@ ;;; Code: ;; (defvar elopher-mode-map nil "Keymap for gopher client.") -;; (define-key elopher-mode-map (kbd "p") 'elopher-quit) ;; (define-derived-mode elopher-mode special-mode "elopher" ;; "Major mode for elopher, an elisp gopher client.") -(defun ) +(defvar elopher-margin-width 5) -(defun elopher-filter (proc string) +(defun elopher-insert-margin (&optional type-name) + (if type-name + (insert (propertize + (format (concat "%" (number-to-string elopher-margin-width) "s") + (concat "[" type-name "] ")) + 'face '(foreground-color . "yellow"))) + (insert (make-string elopher-margin-width ?\s)))) + +(defun elopher-follow-index-link (button) + (apply #'elopher-get-index (button-get button 'link-address))) + +(defun elopher-follow-text-link (button) + (apply #'elopher-get-text (button-get button 'link-address))) + +(defun elopher-follow-image-link (button) + (apply #'elopher-get-image (button-get button 'link-address))) + +(defun elopher-process-record (line) + (let* ((type (elt line 0)) + (fields (split-string (substring line 1) "\t")) + (display-string (elt fields 0)) + (selector (elt fields 1)) + (hostname (elt fields 2)) + (port (elt fields 3)) + (address (list selector hostname port))) + (pcase type + (?i (elopher-insert-margin) + (insert (propertize display-string + 'face '(foreground-color. "white")))) + (?0 (elopher-insert-margin "T") + (insert-text-button display-string + 'face '(foreground-color . "gray") + 'link-address address + 'action #'elopher-follow-text-link + 'follow-link t)) + (?1 (elopher-insert-margin "/") + (insert-text-button display-string + 'face '(foreground-color . "cyan") + 'link-address address + 'action #'elopher-follow-index-link + 'follow-link t)) + (?p (elopher-insert-margin "img") + (insert-text-button display-string + 'face '(foreground-color . "gray") + 'link-address address + 'action #'elopher-follow-image-link + 'follow-link t)) + (?.) ; Occurs at end of index, can safely ignore. + (tp (elopher-insert-margin (concat (char-to-string tp) "?")) + (insert (propertize display-string + 'face '(foreground-color . "red"))))) + (insert "\n"))) + +(defvar elopher-incomplete-record "") + +(defun elopher-process-complete-records (string) + (let* ((til-now (string-join (list elopher-incomplete-record string))) + (lines (split-string til-now "\r\n"))) + (dotimes (idx (length lines)) + (if (< idx (- (length lines) 1)) + (let ((line (elt lines idx))) + (unless (string-empty-p line) + (elopher-process-record line))) + (setq elopher-incomplete-record (elt lines idx)))))) + +(defun elopher-index-filter (proc string) (with-current-buffer (get-buffer "*elopher*") (let ((marker (process-mark proc))) (if (not (marker-position marker)) (set-marker marker 0 (current-buffer))) (save-excursion (goto-char marker) - (insert (propertize string 'face '(foreground-color . "magenta"))) + (elopher-process-complete-records string) (set-marker marker (point)))))) -(defun elopher-get-index (host &optional port path) - (switch-to-buffer-other-window "*elopher*") +(defun elopher-get-index (selector host port) + (switch-to-buffer "*elopher*") (erase-buffer) + (setq elopher-incomplete-record "") (make-network-process :name "elopher-process" :host host :service (if port port 70) - :filter #'elopher-filter) - (process-send-string "elopher-process" (format "%s\n" (if path path "")))) + :filter #'elopher-index-filter) + (process-send-string "elopher-process" (concat selector "\n"))) -(defun elopher () - "Start gopher client." - (interactive) - (elopher-get-index (read-from-minibuffer "Gopher host: ") 70)) +(defun elopher-text-filter (proc string) + (with-current-buffer (get-buffer "*elopher*") + (let ((marker (process-mark proc))) + (if (not (marker-position marker)) + (set-marker marker 0 (current-buffer))) + (save-excursion + (goto-char marker) + (dolist (line (split-string string "\r")) + (insert line)) + (set-marker marker (point)))))) + +(defun elopher-get-text (selector host port) + (switch-to-buffer "*elopher*") + (erase-buffer) + (make-network-process + :name "elopher-process" + :host host + :service port + :filter #'elopher-text-filter) + (process-send-string "elopher-process" (concat selector "\n"))) -(elopher-get-index "cosmic.voyage") +(defvar elopher-image-buffer "") -(format "%s\n" nil) +(defun elopher-image-filter (proc string) + (setq elopher-image-buffer (concat elopher-image-buffer string))) -(delete-process "elopher-process") - - ;; (address (read-from-minibuffer "Address of gopher server: "))) - ;; (message "Connecting to '%s' ..." address) - ;; (erase-buffer) - ;; (insert (propertize "Hello, world." 'face '(foreground-color . "red"))) - ;; (newline) - ;; (insert (propertize "Hello, Tim." 'face '(foreground-color . "yellow")))) +(defun elopher-get-image (selector host port) + (switch-to-buffer "*elopher*") + (erase-buffer) + (setq elopher-image-buffer "") + (make-network-process + :name "elopher-process" + :host host + :service port + :filter #'elopher-image-filter) + (process-send-string "elopher-process" (concat selector "\n")) + (insert-image (create-image elopher-image-buffer))) -(defun elopher-quit () +(defun elopher () + "Start gopher client." (interactive) - (kill-buffer "*elopher*")) + (elopher-get-index "" (read-from-minibuffer "Gopher host: ") 70)) -(start-process "ls" "*elopher*" "/bin/date") +(elopher-get-index "" "cosmic.voyage" 70) +;; (elopher-get-image "/fun/xkcd/comics/2130/2137/text_entry.png" "gopher.floodgap.com" 70) ;;; elopher.el ends here - -(list-processes)