Fixed up special page predicate and associated tests.
[elpher.git] / elpher.el
index 7c68b5c..39e32a2 100644 (file)
--- a/elpher.el
+++ b/elpher.el
   (defvar ansi-color-context)
   (defvar bookmark-make-record-function)
   (defvar mu4e~view-beginning-of-url-regexp)
+  (defvar eww-use-browse-url)
   (defvar thing-at-point-uri-schemes)
   (defvar xterm-color-preserve-properties))
 
@@ -224,6 +225,11 @@ Otherwise, \\[elpher-show-bookmarks] will visit a special elpher bookmark
 page within which all of the standard elpher keybindings are active."
   :type '(boolean))
 
+(defcustom elpher-start-page "about:welcome"
+  "Specify the page displayed initially by elpher.
+The default welcome screen \"about:welcome\", while the bookmarks list
+is \"about:bookmarks\".  You can also specify local files via \"file:\".")
+
 ;; Face customizations
 
 (defgroup elpher-faces nil
@@ -384,9 +390,7 @@ requiring gopher-over-TLS."
 
 (defun elpher-address-to-url (address)
   "Get string representation of ADDRESS, or nil if ADDRESS is special."
-  (if (elpher-address-special-p address)
-      nil
-    (url-encode-url (url-recreate-url address))))
+  (url-encode-url (url-recreate-url address)))
 
 (defun elpher-address-type (address)
   "Retrieve type of ADDRESS object.
@@ -409,10 +413,7 @@ address refers to, via the table `elpher-type-map'."
 
 (defun elpher-address-special-p (address)
   "Return non-nil if ADDRESS is a  special address."
-  (let ((type (url-type address)))
-    (and type
-         (listp type)
-         (eq (car type) 'special))))
+  (pcase (elpher-address-type address) (`(special ,subtype) t)))
 
 (defun elpher-address-protocol (address)
   "Retrieve the transport protocol for ADDRESS.  This is nil for special addresses."
@@ -481,10 +482,10 @@ If no address is defined, returns 0.  (This is for compatibility with the URL li
   "Create a page with DISPLAY-STRING and ADDRESS."
   (list display-string address))
 
-(defun elpher-make-welcome-page ()
+(defun elpher-make-start-page ()
   "Create the welcome page."
-  (elpher-make-page "Elpher Welcome Page"
-                    (elpher-make-special-address 'welcome)))
+  (elpher-make-page "Start Page"
+                    (elpher-address-from-url elpher-start-page)))
 
 (defun elpher-page-display-string (page)
   "Retrieve the display string corresponding to PAGE."
@@ -1453,7 +1454,7 @@ treatment that a separate function is warranted."
           (if (string-empty-p (url-filename address))
               (setf (url-filename address) "/")) ;ensure empty filename is marked as absolute
         (setf (url-host address) (url-host current-address))
-        (setf (url-port address) (url-port current-address))
+        (setf (url-portspec address) (url-portspec current-address)) ; (url-port) too slow!
         (unless (string-prefix-p "/" (url-filename address)) ;deal with relative links
           (setf (url-filename address)
                 (concat (file-name-directory (url-filename current-address))
@@ -2079,21 +2080,23 @@ supports the old protocol elpher, where the link is self-contained."
   "Go to a particular gopher site HOST-OR-URL.
 When run interactively HOST-OR-URL is read from the minibuffer."
   (interactive "sGopher or Gemini URL: ")
-  (let* ((cleaned-host-or-url (string-trim host-or-url))
-         (address (elpher-address-from-url cleaned-host-or-url))
-         (page (elpher-make-page cleaned-host-or-url address)))
-    (switch-to-buffer elpher-buffer-name)
-    (elpher-with-clean-buffer
-     (elpher-visit-page page))
-    nil))
+  (let ((trimmed-host-or-url (string-trim host-or-url)))
+    (unless (string-empty-p trimmed-host-or-url)
+      (let* ((address (elpher-address-from-url trimmed-host-or-url))
+             (page (elpher-make-page trimmed-host-or-url address)))
+        (switch-to-buffer elpher-buffer-name)
+        (elpher-with-clean-buffer
+         (elpher-visit-page page))
+        nil)))) ; non-nil value is displayed by eshell
 
 (defun elpher-go-current ()
   "Go to a particular site read from the minibuffer, initialized with the current URL."
   (interactive)
-  (let ((address (elpher-page-address elpher-current-page)))
-    (let ((url (read-string "Gopher or Gemini URL: "
-                            (unless (elpher-address-special-p address)
-                              (elpher-address-to-url address)))))
+  (let* ((address (elpher-page-address elpher-current-page))
+         (url (read-string "Gopher or Gemini URL: "
+                           (unless (elpher-address-special-p address)
+                             (elpher-address-to-url address)))))
+    (unless (string-empty-p (string-trim url))
       (elpher-visit-page (elpher-make-page url (elpher-address-from-url url))))))
 
 (defun elpher-redraw ()
@@ -2135,7 +2138,7 @@ When run interactively HOST-OR-URL is read from the minibuffer."
   (interactive)
   (setq-local elpher-current-page nil)
   (setq-local elpher-history nil)
-  (elpher-visit-page (elpher-make-welcome-page)))
+  (elpher-visit-page (elpher-make-start-page)))
 
 (defun elpher-download ()
   "Download the link at point."
@@ -2143,11 +2146,12 @@ When run interactively HOST-OR-URL is read from the minibuffer."
   (let ((button (button-at (point))))
     (if button
         (let ((page (button-get button 'elpher-page)))
-          (if (elpher-address-special-p (elpher-page-address page))
-              (error "Cannot download %s"
-                     (elpher-page-display-string page))
-            (elpher-visit-page (button-get button 'elpher-page)
-                               #'elpher-render-download)))
+          (unless page
+            (error "Not an elpher page"))
+          (when (elpher-address-special-p (elpher-page-address page))
+            (error "Cannot download %s" (elpher-page-display-string page)))
+          (elpher-visit-page (button-get button 'elpher-page)
+                             #'elpher-render-download))
       (error "No link selected"))))
 
 (defun elpher-download-current ()
@@ -2203,17 +2207,18 @@ When run interactively HOST-OR-URL is read from the minibuffer."
   "Display information on PAGE."
   (let ((display-string (elpher-page-display-string page))
         (address (elpher-page-address page)))
-    (if (elpher-address-special-p address)
-        (message "Special page: %s" display-string)
-      (message "%s" (elpher-address-to-url address)))))
+    (message "%s" (elpher-address-to-url address))))
 
 (defun elpher-info-link ()
   "Display information on page corresponding to link at point."
   (interactive)
   (let ((button (button-at (point))))
-    (if button
-        (elpher-info-page (button-get button 'elpher-page))
-      (error "No item selected"))))
+    (unless button
+      (error "No item selected"))
+    (let ((page (button-get button 'elpher-page)))
+      (unless page
+        (error "Not an elpher page"))
+      (elpher-info-page page))))
 
 (defun elpher-info-current ()
   "Display information on current page."
@@ -2222,20 +2227,21 @@ When run interactively HOST-OR-URL is read from the minibuffer."
 
 (defun elpher-copy-page-url (page)
   "Copy URL representation of address of PAGE to `kill-ring'."
-  (let ((address (elpher-page-address page)))
-    (if (elpher-address-special-p address)
-        (error (format "Cannot represent %s as URL" (elpher-page-display-string page)))
-      (let ((url (elpher-address-to-url address)))
-        (message "Copied \"%s\" to kill-ring/clipboard." url)
-        (kill-new url)))))
+  (let* ((address (elpher-page-address page))
+         (url (elpher-address-to-url address)))
+    (message "Copied \"%s\" to kill-ring/clipboard." url)
+    (kill-new url)))
 
 (defun elpher-copy-link-url ()
   "Copy URL of item at point to `kill-ring'."
   (interactive)
   (let ((button (button-at (point))))
-    (if button
-        (elpher-copy-page-url (button-get button 'elpher-page))
-      (error "No item selected"))))
+    (unless button
+      (error "No item selected"))
+    (let ((page (button-get button 'elpher-page)))
+      (unless page
+        (error "Not an elpher page"))
+      (elpher-copy-page-url page))))
 
 (defun elpher-copy-current-url ()
   "Copy URL of current page to `kill-ring'."
@@ -2363,7 +2369,7 @@ to create a new session.  Returns the buffer selected (or created)."
     (pop-to-buffer-same-window buf)
     (unless (buffer-modified-p)
       (elpher-mode)
-      (elpher-visit-page (elpher-make-welcome-page))
+      (elpher-visit-page (elpher-make-start-page))
       "Started Elpher."))); Otherwise (elpher) evaluates to start page string.
 
 ;;; elpher.el ends here