From: Alexis Date: Thu, 3 Sep 2020 08:01:28 +0000 (+1000) Subject: Add ability to number links on gemini pages. X-Git-Tag: v3.0.0~22^2~34^2~1 X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?a=commitdiff_plain;h=79d8cf963148534c8ec23a9dbaa6db1325232d85;p=elpher.git Add ability to number links on gemini pages. This patch adds a defcustom, elpher-gemini-number-links, which can be set to t to display numbers next to links, à la e.g. AV-98. Links can then be visited by pressing `v` and entering the link number. --- diff --git a/elpher.el b/elpher.el index 40e21dc..8940f08 100644 --- a/elpher.el +++ b/elpher.el @@ -100,6 +100,13 @@ "Association list from types to getters, renderers, margin codes and index faces.") +;;; Internal variables +;; + +(defvar elpher--gemini-page-links '() + "Internal variable containing list of links on page.") + + ;;; Customization group ;; @@ -178,6 +185,11 @@ This can be useful when browsing from a computer that supports IPv6, because some servers which do not support IPv6 can take a long time to time-out." :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 @@ -1343,6 +1355,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 @@ -1397,16 +1410,27 @@ 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)) + (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))))) (elpher-cache-content @@ -1502,6 +1526,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" @@ -1696,6 +1721,15 @@ When run interactively HOST-OR-URL is read from the minibuffer." (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))))))) +(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." (interactive) @@ -1953,6 +1987,7 @@ 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 @@ -1980,7 +2015,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.")