From ddef031a2c1d2fa02b63a41990f882587771ab79 Mon Sep 17 00:00:00 2001 From: Tim Vaughan Date: Fri, 12 Apr 2019 17:59:04 +0200 Subject: [PATCH] Links are now clickable! --- elopher.el | 70 +++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 61 insertions(+), 9 deletions(-) diff --git a/elopher.el b/elopher.el index e33d821..86e1950 100644 --- a/elopher.el +++ b/elopher.el @@ -12,7 +12,61 @@ ;; (define-derived-mode elopher-mode special-mode "elopher" ;; "Major mode for elopher, an elisp gopher client.") -(defun ) +;; (global-set-key (kbd "C-c C-b") 'eval-buffer) + +(defvar elopher-type-margin-width 5) + +(defun elopher-type-margin (&optional type-name) + (if type-name + (insert (propertize + (format (concat "%" (number-to-string elopher-type-margin-width) "s") + (concat "[" type-name "] ")) + 'face '(foreground-color . "yellow"))) + (insert (make-string elopher-type-margin-width ?\s)))) + +(defun elopher-format-i (display-string) + (elopher-type-margin) + (insert (propertize display-string 'face '(foreground-color . "white"))) + (insert "\n")) + +(defun elopher-format-0 (display-string selector hostname port) + (elopher-type-margin "T") + (insert (propertize display-string 'face '(foreground-color . "gray"))) + (insert "\n")) + +(defun elopher-format-1 (display-string selector hostname port) + (elopher-type-margin "/") + (let ((map (make-sparse-keymap))) + (define-key map [mouse-1] + (eval `(lambda () (interactive) (elopher-get-index ,hostname ,port ,selector)))) + (insert (propertize display-string + 'face '(foreground-color . "cyan") + 'mouse-face 'highlight + 'help-echo "mouse-1: follow link" + 'keymap map)) + (insert "\n"))) + +(defun elopher-process-record (line) + (let* ((type (elt line 0)) + (fields (split-string (substring line 1) "\t")) + (g-display-string (elt fields 0)) + (g-selector (elt fields 1)) + (g-hostname (elt fields 2)) + (g-port (elt fields 3))) + (pcase type + (?i (elopher-format-i g-display-string)) + (?0 (elopher-format-0 g-display-string g-selector g-hostname g-port)) + (?1 (elopher-format-1 g-display-string g-selector g-hostname g-port))))) + +(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)) + (elopher-process-record (elt lines idx)) + (setq elopher-incomplete-record (elt lines idx)))))) (defun elopher-filter (proc string) (with-current-buffer (get-buffer "*elopher*") @@ -21,30 +75,28 @@ (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*") + (switch-to-buffer "*elopher*") (erase-buffer) (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 "")))) + (process-send-string "elopher-process" (concat path "\n"))) (defun elopher () "Start gopher client." (interactive) (elopher-get-index (read-from-minibuffer "Gopher host: ") 70)) -(elopher-get-index "cosmic.voyage") - -(format "%s\n" nil) +;; (elopher-get-index "cosmic.voyage") +(elopher-get-index "gopher.floodgap.com") +;; (elopher-get-index "maurits.id.au") -(delete-process "elopher-process") - (defun elopher-quit () (interactive) (kill-buffer "*elopher*")) -- 2.20.1