Avoid anoying error message on homepage when using elpher-go-current
[elpher.git] / elpher.el
index 64ec7ca..e16c52d 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -6,6 +6,9 @@
 ;; Copyright (C) 2021 Omar Polo <op@omarpolo.com>
 ;; Copyright (C) 2021 Noodles! <nnoodle@chiru.no>
 ;; Copyright (C) 2020-2021 Alex Schroeder <alex@gnu.org>
+;; Copyright (C) 2020 Zhiwei Chen <chenzhiwei03@kuaishou.com>
+;; Copyright (C) 2020 condy0919 <condy0919@gmail.com>
+;; Copyright (C) 2020 Alexis <flexibeast@gmail.com>
 ;; Copyright (C) 2020 Étienne Deparis <etienne@depar.is>
 ;; Copyright (C) 2020 Simon Nicolussi <sinic@sinic.name>
 ;; Copyright (C) 2020 Michel Alexandre Salim <michel@michel-slm.name>
 ;;; Global constants
 ;;
 
-(defconst elpher-version "2.10.2"
+(defconst elpher-version "2.11.0"
   "Current version of elpher.")
 
 (defconst elpher-margin-width 6
   "Association list from types to getters, renderers, margin codes and index faces.")
 
 
+;;; Internal variables
+;;
+
+;; buffer-local
+(defvar elpher--gemini-page-headings nil
+  "List of headings on the page.")
+
+(defvar elpher--gemini-page-links nil
+  "List of links on the page.")
+
+(defvar elpher--gemini-page-links-cache (make-hash-table :test 'equal)
+  "Hash of addresses and page links.")
+
 ;;; Customization group
 ;;
 
@@ -174,7 +190,7 @@ These certificates may be used for establishing authenticated TLS connections."
   :type '(file))
 
 (defcustom elpher-default-url-type "gopher"
-  "Default URL type to assume if not explicitly given."
+  "Default URL type (i.e. scheme) to assume if not explicitly given."
   :type '(choice (const "gopher")
                  (const "gemini")))
 
@@ -217,6 +233,11 @@ some servers which do not support IPv6 can take a long time to time-out."
 Otherwise, the SOCKS proxy is only used for connections to onion services."
   :type '(boolean))
 
+(defcustom elpher-gemini-number-links nil
+  "If non-nil, number links in gemini pages when rendering.
+Links can be accessed by pressing `v' ('visit') followed by the link number."
+  :type '(boolean))
+
 ;; Face customizations
 
 (defgroup elpher-faces nil
@@ -504,12 +525,17 @@ unless NO-HISTORY is non-nil."
   (setq-local elpher-current-page page)
   (let* ((address (elpher-page-address page))
          (type (elpher-address-type address))
-         (type-record (cdr (assoc type elpher-type-map))))
+         (type-record (cdr (assoc type elpher-type-map)))
+         (page-links nil))
     (if type-record
-        (funcall (car type-record)
-                 (if renderer
-                     renderer
-                   (cadr type-record)))
+        (progn
+          (funcall (car type-record)
+                   (if renderer
+                       renderer
+                     (cadr type-record)))
+          (setq page-links (gethash address elpher--gemini-page-links-cache))
+          (if page-links
+              (setq elpher--gemini-page-links page-links)))
       (elpher-visit-previous-page)
       (pcase type
         (`(gopher ,type-char)
@@ -1403,6 +1429,7 @@ treatment that a separate function is warranted."
          (address (elpher-address-from-gemini-url url))
          (type (if address (elpher-address-type address) nil))
          (type-map-entry (cdr (assoc type elpher-type-map))))
+    (setq elpher--gemini-page-links (append elpher--gemini-page-links `(,address)))
     (when display-string
       (insert elpher-gemini-link-string)
       (if type-map-entry
@@ -1434,6 +1461,8 @@ by HEADER-LINE."
                            (/ (* fill-column
                                  (font-get (font-spec :name (face-font 'default)) :size))
                               (font-get (font-spec :name (face-font face)) :size)) fill-column)))
+      (setq elpher--gemini-page-headings (cons (cons header (point))
+                                               elpher--gemini-page-headings))
       (unless (display-graphic-p)
         (insert (make-string level ?#) " "))
       (insert (propertize header 'face face))
@@ -1464,21 +1493,38 @@ width defined by elpher-gemini-max-fill-width."
 (defun elpher-render-gemini-map (data _parameters)
   "Render DATA as a gemini map file, PARAMETERS is currently unused."
   (elpher-with-clean-buffer
-   (let ((preformatted nil))
+   (setq elpher--gemini-page-headings nil)
+   (let ((preformatted nil)
+         (link-counter 1))
      (auto-fill-mode 1)
      (setq-local fill-column (min (window-width) elpher-gemini-max-fill-width))
+     (setq elpher--gemini-page-links '())
      (dolist (line (split-string data "\n"))
        (cond
         ((string-prefix-p "```" line) (setq preformatted (not preformatted)))
         (preformatted (insert (elpher-process-text-for-display
                                (propertize line 'face 'elpher-gemini-preformatted))
                               "\n"))
-        ((string-prefix-p "=>" line) (elpher-gemini-insert-link line))
+        ((string-prefix-p "=>" line)
+         (progn
+           (if elpher-gemini-number-links
+               (insert
+                (concat
+                 "["
+                 (number-to-string link-counter)
+                 "] ")))
+           (setq link-counter (1+ link-counter))
+           (elpher-gemini-insert-link line)))
         ((string-prefix-p "#" line) (elpher-gemini-insert-header line))
         (t (elpher-gemini-insert-text line)))))
+   (setq elpher--gemini-page-headings (nreverse elpher--gemini-page-headings))
    (elpher-cache-content
     (elpher-page-address elpher-current-page)
-    (buffer-string))))
+    (buffer-string))
+   (puthash
+    (elpher-page-address elpher-current-page)
+    elpher--gemini-page-links
+    elpher--gemini-page-links-cache)))
 
 (defun elpher-render-gemini-plain-text (data _parameters)
   "Render DATA as plain text file.  PARAMETERS is currently unused."
@@ -1569,6 +1615,7 @@ The result is rendered using RENDERER."
            " - u/mouse-3/U: return to previous page or to the start page\n"
            " - o/O: visit different selector or the root menu of the current server\n"
            " - g: go to a particular address (gopher, gemini, finger)\n"
+           " - v: visit a numbered link on a gemini page\n"
            " - d/D: download item under cursor or current page\n"
            " - i/I: info on item under cursor or current page\n"
            " - c/C: copy URL representation of item under cursor or current page\n"
@@ -1591,8 +1638,8 @@ The result is rendered using RENDERER."
            "Alternatively, select a search engine and enter some search terms:\n")
    (elpher-insert-index-record "Gopher Search Engine (Veronica-2)"
                                (elpher-make-gopher-address ?7 "/v2/vs" "gopher.floodgap.com" 70))
-   (elpher-insert-index-record "Gemini Search Engine (GUS)"
-                               (elpher-address-from-url "gemini://gus.guru/search"))
+   (elpher-insert-index-record "Gemini Search Engine (geminispace.info)"
+                               (elpher-address-from-url "gemini://geminispace.info/search"))
    (insert "\n"
            "This page contains your bookmarked sites (also visit with B):\n")
    (elpher-insert-index-record "Your Bookmarks" 'bookmarks)
@@ -1807,10 +1854,19 @@ When run interactively HOST-OR-URL is read from the minibuffer."
   "Go to a particular site read from the minibuffer, initialized with the current URL."
   (interactive)
   (let ((address (elpher-page-address elpher-current-page)))
-    (if (elpher-address-special-p address)
-        (error "Command invalid for this page")
-      (let ((url (read-string "Gopher or Gemini URL: " (elpher-address-to-url address))))
-        (elpher-visit-page (elpher-make-page url (elpher-address-from-url url)))))))
+    (let ((url (read-string "Gopher or Gemini URL: "
+                            (unless (elpher-address-special-p address)
+                              (elpher-address-to-url address)))))
+      (elpher-visit-page (elpher-make-page url (elpher-address-from-url url))))))
+
+(defun elpher-visit-gemini-numbered-link (n)
+  "Visit link designated by a number."
+  (interactive "nLink number: ")
+  (if (or (> n (length elpher--gemini-page-links))
+          (< n 1))
+      (user-error "Invalid link number"))
+  (let ((address (nth (1- n) elpher--gemini-page-links)))
+    (elpher-go (url-recreate-url address))))
 
 (defun elpher-redraw ()
   "Redraw current page."
@@ -2070,23 +2126,26 @@ When run interactively HOST-OR-URL is read from the minibuffer."
     (define-key map (kbd "B") 'elpher-bookmarks)
     (define-key map (kbd "S") 'elpher-set-gopher-coding-system)
     (define-key map (kbd "F") 'elpher-forget-current-certificate)
+    (define-key map (kbd "v") 'elpher-visit-gemini-numbered-link)
     (when (fboundp 'evil-define-key*)
       (evil-define-key* 'motion map
         (kbd "TAB") 'elpher-next-link
         (kbd "C-") 'elpher-follow-current-link
         (kbd "C-t") 'elpher-back
         (kbd "u") 'elpher-back
+        (kbd "-") 'elpher-back
+        (kbd "^") 'elpher-back
         (kbd "U") 'elpher-back-to-start
         [mouse-3] 'elpher-back
-        (kbd "g") 'elpher-go
-        (kbd "o") 'elpher-go-current
+        (kbd "o") 'elpher-go
+        (kbd "O") 'elpher-go-current
         (kbd "r") 'elpher-redraw
         (kbd "R") 'elpher-reload
         (kbd "T") 'elpher-toggle-tls
         (kbd ".") 'elpher-view-raw
         (kbd "d") 'elpher-download
         (kbd "D") 'elpher-download-current
-        (kbd "m") 'elpher-jump
+        (kbd "J") 'elpher-jump
         (kbd "i") 'elpher-info-link
         (kbd "I") 'elpher-info-current
         (kbd "c") 'elpher-copy-link-url
@@ -2097,7 +2156,8 @@ When run interactively HOST-OR-URL is read from the minibuffer."
         (kbd "X") 'elpher-unbookmark-current
         (kbd "B") 'elpher-bookmarks
         (kbd "S") 'elpher-set-gopher-coding-system
-        (kbd "F") 'elpher-forget-current-certificate))
+        (kbd "F") 'elpher-forget-current-certificate
+        (kbd "v") 'elpher-visit-gemini-numbered-link))
     map)
   "Keymap for gopher client.")
 
@@ -2107,9 +2167,14 @@ When run interactively HOST-OR-URL is read from the minibuffer."
 This mode is automatically enabled by the interactive
 functions which initialize the gopher client, namely
 `elpher', `elpher-go' and `elpher-bookmarks'."
+  (setq-local elpher--gemini-page-headings nil)
   (setq-local elpher-current-page nil)
   (setq-local elpher-history nil)
-  (setq-local elpher-buffer-name (buffer-name)))
+  (setq-local elpher-buffer-name (buffer-name))
+
+  (setq-local imenu-create-index-function
+              (lambda ()
+                elpher--gemini-page-headings)))
 
 (when (fboundp 'evil-set-initial-state)
   (evil-set-initial-state 'elpher-mode 'motion))