Performance optimization for gopher and finger xfers.
[elpher.git] / elpher.el
index 13ec53c..dec79ae 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -4,7 +4,7 @@
 
 ;; Author: Tim Vaughan <timv@ughan.xyz>
 ;; Created: 11 April 2019
-;; Version: 2.6.0
+;; Version: 2.6.1
 ;; Keywords: comm gopher
 ;; Homepage: http://thelambdalab.xyz/elpher
 ;; Package-Requires: ((emacs "26"))
@@ -69,7 +69,7 @@
 ;;; Global constants
 ;;
 
-(defconst elpher-version "2.6.0"
+(defconst elpher-version "2.6.1"
   "Current version of elpher.")
 
 (defconst elpher-margin-width 6
@@ -506,7 +506,7 @@ to ADDRESS."
       (let* ((kill-buffer-query-functions nil)
              (port (elpher-address-port address))
              (host (elpher-address-host address))
-             (selector-string "")
+             (selector-string-parts nil)
              (proc (open-network-stream "elpher-process"
                                         nil
                                         (if force-ipv4 (dns-query host) host)
@@ -538,8 +538,8 @@ to ADDRESS."
         (set-process-filter proc
                             (lambda (_proc string)
                               (cancel-timer timer)
-                              (setq selector-string
-                                    (concat selector-string string))))
+                              (setq selector-string-parts
+                                    (cons string selector-string-parts))))
         (set-process-sentinel proc
                               (lambda (_proc event)
                                 (condition-case the-error
@@ -553,7 +553,8 @@ to ADDRESS."
                                                  "\r\n"))))
                                      (t
                                       (cancel-timer timer)
-                                      (funcall renderer selector-string)
+                                      (funcall renderer (apply #'concat
+                                                               (reverse selector-string-parts)))
                                       (elpher-restore-pos)))
                                   (error
                                    (elpher-network-error address the-error))))))
@@ -811,7 +812,7 @@ to ADDRESS."
         (let* ((kill-buffer-query-functions nil)
                (port (elpher-address-port address))
                (host (elpher-address-host address))
-               (response-string "")
+               (response-string-parts nil)
                (proc (open-network-stream "elpher-process"
                                           nil
                                           (if force-ipv4 (dns-query host) host)
@@ -832,8 +833,8 @@ to ADDRESS."
                                 (when timer
                                   (cancel-timer timer)
                                   (setq timer nil))
-                                (setq response-string
-                                      (concat response-string string))))
+                                (setq response-string-parts
+                                      (cons string response-string-parts))))
           (set-process-sentinel proc
                                 (lambda (proc event)
                                   (condition-case the-error
@@ -845,7 +846,7 @@ to ADDRESS."
                                            (concat (elpher-address-to-url address)
                                                    "\r\n"))))
                                        ((string-prefix-p "deleted" event)) ; do nothing
-                                       ((and (string-empty-p response-string)
+                                       ((and (not response-string-parts)
                                              (not force-ipv4))
                                         ; Try again with IPv4
                                         (message "Connection failed. Retrying with IPv4.")
@@ -853,7 +854,7 @@ to ADDRESS."
                                         (elpher-get-gemini-response address renderer t))
                                        (t
                                         (funcall #'elpher-process-gemini-response
-                                                 response-string
+                                                 (apply #'concat (reverse response-string-parts))
                                                  renderer)
                                         (elpher-restore-pos)))
                                     (error
@@ -1014,18 +1015,45 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d"
                 (elpher-collapse-dot-sequences (url-filename address)))))
     address))
 
+(defun elpher-gemini-insert-link (link-line)
+  (let* ((url (elpher-gemini-get-link-url link-line))
+         (display-string (let ((s (elpher-gemini-get-link-display-string link-line)))
+                           (if (string-empty-p s) url s)))
+         (address (elpher-address-from-gemini-url url))
+         (type (if address (elpher-address-type address) nil))
+         (type-map-entry (cdr (assoc type elpher-type-map))))
+    (insert "→ ")
+    (if type-map-entry
+        (let* ((face (elt type-map-entry 3))
+               (filtered-display-string (ansi-color-filter-apply display-string))
+               (page (elpher-make-page filtered-display-string address)))
+          (insert-text-button filtered-display-string
+                              'face face
+                              'elpher-page page
+                              'action #'elpher-click-link
+                              'follow-link t
+                              'help-echo (elpher-page-button-help page)))
+      (insert (propertize display-string 'face 'elpher-unknown)))
+    (insert "\n")))
+  
+(defun elpher-gemini-insert-header (header-line)
+  (insert header-line "\n"))
+
+(defun elpher--trim-prefix-p (prefix string)
+  (string-prefix-p prefix (string-trim-left string)))
+
 (defun elpher-render-gemini-map (data _parameters)
   "Render DATA as a gemini map file, PARAMETERS is currently unused."
   (elpher-with-clean-buffer
-   (dolist (line (split-string data "\n"))
-     (if (string-prefix-p "=>" line)
-         (let* ((url (elpher-gemini-get-link-url line))
-                (display-string (elpher-gemini-get-link-display-string line))
-                (address (elpher-address-from-gemini-url url)))
-           (if (> (length display-string) 0)
-               (elpher-insert-index-record display-string address)
-             (elpher-insert-index-record url address)))
-       (elpher-insert-index-record line)))
+   (let ((preformatted nil))
+     (auto-fill-mode 1)
+     (dolist (line (split-string data "\n"))
+       (cond
+        ((elpher--trim-prefix-p "```" line) (setq preformatted (not preformatted)))
+        (preformatted (insert (elpher-process-text-for-display line) "\n"))
+        ((elpher--trim-prefix-p "=>" line) (elpher-gemini-insert-link line))
+        ((elpher--trim-prefix-p "#" line) (elpher-gemini-insert-header line))
+        (t (insert (elpher-process-text-for-display line)) (newline)))))
    (elpher-cache-content
     (elpher-page-address elpher-current-page)
     (buffer-string))))
@@ -1059,7 +1087,7 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d"
                  (port (let ((given-port (elpher-address-port address)))
                          (if (> given-port 0) given-port 79)))
                  (host (elpher-address-host address))
-                 (selector-string "")
+                 (selector-string-parts nil)
                  (proc (open-network-stream "elpher-process"
                                             nil
                                             (if force-ipv4 (dns-query host) host)
@@ -1080,8 +1108,8 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d"
             (set-process-filter proc
                                 (lambda (_proc string)
                                   (cancel-timer timer)
-                                  (setq selector-string
-                                        (concat selector-string string))))
+                                  (setq selector-string-parts
+                                        (cons string selector-string-parts))))
             (set-process-sentinel proc
                                   (lambda (_proc event)
                                     (condition-case the-error
@@ -1094,7 +1122,8 @@ For instance, the filename /a/b/../c/./d will reduce to /a/c/d"
                                              (concat user "\r\n"))))
                                          (t
                                           (cancel-timer timer)
-                                          (funcall renderer selector-string)
+                                          (funcall renderer (apply #'concat
+                                                                   (reverse selector-string-parts)))
                                           (elpher-restore-pos)))))))
         (error
          (elpher-network-error address the-error))))))
@@ -1433,9 +1462,7 @@ When run interactively HOST-OR-URL is read from the minibuffer."
           (let ((address-copy (elpher-address-from-url
                                (elpher-address-to-url address))))
             (setf (url-filename address-copy) "")
-            (elpher-visit-page
-             (elpher-make-page (elpher-address-to-url address-copy)
-                               address-copy))))
+            (elpher-go (elpher-address-to-url address-copy))))
       (error "Command invalid for %s" (elpher-page-display-string elpher-current-page)))))
 
 (defun elpher-bookmarks-current-p ()