+;; (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))))))