Sanitize display strings for use in header.
[elpher.git] / elpher.el
index 3426458..b9c43f8 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -20,7 +20,7 @@
 
 ;; Author: Tim Vaughan <plugd@thelambdalab.xyz>
 ;; Created: 11 April 2019
-;; Version: 3.1.0
+;; Version: 3.2.1
 ;; Keywords: comm gopher
 ;; Homepage: https://thelambdalab.xyz/elpher
 ;; Package-Requires: ((emacs "27.1"))
@@ -85,7 +85,7 @@
 ;;; Global constants
 ;;
 
-(defconst elpher-version "3.1.0"
+(defconst elpher-version "3.2.1"
   "Current version of elpher.")
 
 (defconst elpher-margin-width 6
@@ -577,13 +577,14 @@ previously-visited pages,unless NO-HISTORY is non-nil."
   "If `elpher-use-header' is true, display current page info in window header."
   (if elpher-use-header
       (let* ((display-string (elpher-page-display-string elpher-current-page))
+             (sanitized-display-string (replace-regexp-in-string "%" "%%" display-string))
              (address (elpher-page-address elpher-current-page))
              (tls-string (if (and (not (elpher-address-about-p address))
                                   (member (elpher-address-protocol address)
                                           '("gophers" "gemini")))
                              " [TLS encryption]"
                            ""))
-             (header (concat display-string
+             (header (concat sanitized-display-string
                              (propertize tls-string 'face 'bold))))
         (setq header-line-format header))))
 
@@ -773,7 +774,7 @@ the host operating system and the local network capabilities.)"
                                      (elpher-process-cleanup)
                                      (cond
                                         ; Try again with IPv4
-                                      ((not (or force-ipv4 socks))
+                                      ((not (or elpher-ipv4-always force-ipv4 socks))
                                        (message "Connection timed out.  Retrying with IPv4.")
                                        (elpher-get-host-response address default-port
                                                                  query-string
@@ -794,7 +795,9 @@ the host operating system and the local network capabilities.)"
                (proc (if socks (socks-open-network-stream "elpher-process" nil host service)
                        (make-network-process :name "elpher-process"
                                              :host host
-                                             :family (and force-ipv4 'ipv4)
+                                             :family (and (or force-ipv4
+                                                              elpher-ipv4-always)
+                                                          'ipv4)
                                              :service service
                                              :buffer nil
                                              :nowait t
@@ -1022,25 +1025,6 @@ once they are retrieved from the gopher server."
 
 ;; Index rendering
 
-(defun elpher-insert-index (string)
-  "Insert the index corresponding to STRING into the current buffer."
-  ;; Should be able to split directly on CRLF, but some non-conformant
-  ;; LF-only servers sadly exist, hence the following.
-  (let ((str-processed (elpher-preprocess-text-response string)))
-    (dolist (line (split-string str-processed "\n"))
-      (ignore-errors
-        (unless (= (length line) 0)
-          (let* ((type (elt line 0))
-                 (fields (split-string (substring line 1) "\t"))
-                 (display-string (elt fields 0))
-                 (selector (elt fields 1))
-                 (host (elt fields 2))
-                 (port (if (elt fields 3)
-                           (string-to-number (elt fields 3))
-                         nil))
-                 (address (elpher-make-gopher-address type selector host port)))
-            (elpher-insert-index-record display-string address)))))))
-
 (defun elpher-insert-margin (&optional type-name)
   "Insert index margin, optionally containing the TYPE-NAME, into the current buffer."
   (if type-name
@@ -1108,7 +1092,20 @@ If ADDRESS is not supplied or nil the record is rendered as an
   (elpher-with-clean-buffer
    (if (not data)
        t
-     (elpher-insert-index data)
+     (let ((data-processed (elpher-preprocess-text-response data)))
+       (dolist (line (split-string data-processed "\n"))
+         (ignore-errors
+           (unless (= (length line) 0)
+             (let* ((type (elt line 0))
+                    (fields (split-string (substring line 1) "\t"))
+                    (display-string (elt fields 0))
+                    (selector (elt fields 1))
+                    (host (elt fields 2))
+                    (port (if (elt fields 3)
+                              (string-to-number (elt fields 3))
+                            nil))
+                    (address (elpher-make-gopher-address type selector host port)))
+               (elpher-insert-index-record display-string address))))))
      (elpher-cache-content (elpher-page-address elpher-current-page)
                            (buffer-string)))))
 
@@ -1136,8 +1133,8 @@ If ADDRESS is not supplied or nil the record is rendered as an
                        nil t))
                (window (get-buffer-window elpher-buffer-name)))
           (when window
-            (setf (image-property image :max-width) (window-pixel-width window))
-            (setf (image-property image :max-height) (window-pixel-height window)))
+            (setf (image-property image :max-width) (window-body-width window t))
+            (setf (image-property image :max-height) (window-body-height window t)))
           (elpher-with-clean-buffer
            (insert-image image)
            (elpher-restore-pos)))
@@ -1517,28 +1514,36 @@ by HEADER-LINE."
   "Insert a plain non-preformatted TEXT-LINE into a text/gemini document.
 This function uses Emacs' auto-fill to wrap text sensibly to a maximum
 width defined by `elpher-gemini-max-fill-width'."
-  (string-match "\\(^[ \t]*\\)\\(\\*[ \t]+\\|>[ \t]*\\)?" text-line)
-  (let* ((line-prefix (match-string 2 text-line))
-         (processed-text-line
-          (if line-prefix
-              (cond ((string-prefix-p "*" line-prefix)
-                     (concat
-                      (replace-regexp-in-string "\\*"
-                                                elpher-gemini-bullet-string
-                                                (match-string 0 text-line))
-                      (substring text-line (match-end 0))))
-                    ((string-prefix-p ">" line-prefix)
-                     (propertize text-line 'face 'elpher-gemini-quoted))
-                    (t text-line))
-            text-line))
-         (adaptive-fill-mode t)
-        ;; fill-prefix is important for adaptive-fill-mode: without
-        ;; it, multi-line list items are not indented correct
-         (fill-prefix (if (match-string 2 text-line)
-                          (replace-regexp-in-string "[>\*]" " " (match-string 0 text-line))
-                        nil)))
-    (insert (elpher-process-text-for-display processed-text-line))
-    (newline)))
+  (if (string-empty-p text-line)
+      (insert "\n")
+    (string-match
+     (rx (: line-start
+            (* (any " \t"))
+            (optional
+             (group (or (: "*" (+ (any " \t")))
+                        (: ">" (* (any " \t"))))))))
+     text-line)
+    (let* ((line-prefix (match-string 1 text-line))
+           (processed-text-line
+            (if line-prefix
+                (cond ((string-prefix-p "*" line-prefix)
+                       (concat
+                        (replace-regexp-in-string "\\*"
+                                                  elpher-gemini-bullet-string
+                                                  (match-string 0 text-line))
+                        (substring text-line (match-end 0))))
+                      ((string-prefix-p ">" line-prefix)
+                       (propertize text-line 'face 'elpher-gemini-quoted))
+                      (t text-line))
+              text-line))
+           (adaptive-fill-mode t)
+          ;; fill-prefix is important for adaptive-fill-mode: without
+          ;; it, multi-line list items are not indented correct
+           (fill-prefix (if (match-string 1 text-line)
+                            (make-string (length (match-string 0 text-line)) ?\s)
+                          nil)))
+      (insert (elpher-process-text-for-display processed-text-line))
+      (newline))))
 
 (defun elpher-render-gemini-map (data _parameters)
   "Render DATA as a gemini map file, PARAMETERS is currently unused."
@@ -1829,20 +1834,22 @@ If `elpher-bookmark-link' is non-nil and point is on a link button,
 return a bookmark record for that link.  Otherwise, return a bookmark
 record for the current elpher page."
   (let* ((button (and elpher-bookmark-link (button-at (point))))
-        (page (if button
-                  (button-get button 'elpher-page)
-                elpher-current-page))
-        (address (elpher-page-address page))
-        (url (elpher-address-to-url address))
-        (display-string (elpher-page-display-string page))
-        (pos (if button nil (point))))
-    (if (elpher-address-about-p address)
-       (error "Cannot bookmark %s" display-string)
-      `(,display-string
-       (defaults . (,display-string))
-       (position . ,pos)
-       (location . ,url)
-       (handler . elpher-bookmark-jump)))))
+         (page (if button
+                   (button-get button 'elpher-page)
+                 elpher-current-page)))
+    (unless page
+      (error "Cannot bookmark this link"))
+    (let* ((address (elpher-page-address page))
+           (url (elpher-address-to-url address))
+           (display-string (elpher-page-display-string page))
+           (pos (if button nil (point))))
+      (if (elpher-address-about-p address)
+          (error "Cannot bookmark %s" display-string)
+        `(,display-string
+          (defaults . (,display-string))
+          (position . ,pos)
+          (location . ,url)
+          (handler . elpher-bookmark-jump))))))
 
 ;;;###autoload
 (defun elpher-bookmark-jump (bookmark)