X-Git-Url: https://thelambdalab.xyz/gitweb/index.cgi?p=elpher.git;a=blobdiff_plain;f=elpher.el;h=4498c6c6396ccd2056b201008c0cafcb81f22f95;hp=6bb36b3c9d499b78eed0990605f6f90621aa7c48;hb=eb650bff13f8ec7c9504c0937b4395617cfdfd53;hpb=fbf5fbcd3e0d82c9d7de7d4db5166620dbb31791 diff --git a/elpher.el b/elpher.el index 6bb36b3..4498c6c 100644 --- a/elpher.el +++ b/elpher.el @@ -1,27 +1,11 @@ ;;; elpher.el --- A friendly gopher and gemini client -*- lexical-binding: t -*- -;; Copyright (C) 2021 Jens Östlund -;; Copyright (C) 2021 F. Jason Park -;; Copyright (C) 2021 Christopher Brannon -;; Copyright (C) 2021 Omar Polo -;; Copyright (C) 2021 Noodles! -;; Copyright (C) 2021 Abhiseck Paira -;; Copyright (C) 2021 Daniel Semyonov -;; Copyright (C) 2020-2021 Alex Schroeder -;; Copyright (C) 2020 Zhiwei Chen -;; Copyright (C) 2020 condy0919 -;; Copyright (C) 2020 Alexis -;; Copyright (C) 2020 Étienne Deparis -;; Copyright (C) 2020 Simon Nicolussi -;; Copyright (C) 2020 Michel Alexandre Salim -;; Copyright (C) 2020 Koushk Roy -;; Copyright (C) 2020 Vee -;; Copyright (C) 2020 Simon South -;; Copyright (C) 2019-2021 Tim Vaughan +;; Copyright (C) 2019-2022 Tim Vaughan +;; Copyright (C) 2020-2022 Elpher contributors (See info manual for full list) ;; Author: Tim Vaughan ;; Created: 11 April 2019 -;; Version: 3.2.2 +;; Version: 3.4.0 ;; Keywords: comm gopher ;; Homepage: https://thelambdalab.xyz/elpher ;; Package-Requires: ((emacs "27.1")) @@ -86,7 +70,7 @@ ;;; Global constants ;; -(defconst elpher-version "3.2.2" +(defconst elpher-version "3.4.0" "Current version of elpher.") (defconst elpher-margin-width 6 @@ -128,11 +112,12 @@ (declare-function org-link-store-props "ol") (declare-function org-link-set-parameters "ol") (defvar ansi-color-context) + (defvar xterm-color--current-fg) + (defvar xterm-color--current-bg) (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)) + (defvar thing-at-point-uri-schemes)) ;;; Customization group @@ -166,8 +151,8 @@ plain text without user input." (defcustom elpher-filter-ansi-from-text nil "If non-nil, filter ANSI escape sequences from text. -The default behaviour is to use the ansi-color package to interpret these -sequences." +The default behaviour is to use the ansi-color package (or xterm-color if it is +available) to interpret these sequences." :type '(boolean)) (defcustom elpher-certificate-directory @@ -237,6 +222,24 @@ Emacs bookmark menu being accessible via \\[elpher-show-bookmarks] from the start page." :type '(string)) +(defcustom elpher-gemini-hide-preformatted nil + "Cause elpher to hide preformatted gemini text by default. +When this option is enabled, preformatted text in text/gemini documents +is replaced with a button which can be used to toggle its display. + +This is intended to improve accessibility, as preformatted text often +includes art which can be difficult for screen readers to interpret +meaningfully." + :type '(boolean)) + +(defcustom elpher-gemini-preformatted-toggle-bullet "‣ " + "Margin symbol used to distinguish the preformatted text toggle." + :type '(string)) + +(defcustom elpher-gemini-preformatted-toggle-label "[Toggle Preformatted Text]" + "Label of button used to toggle formatted text." + :type '(string)) + ;; Face customizations (defgroup elpher-faces nil @@ -315,6 +318,10 @@ the start page." '((t :inherit font-lock-doc-face)) "Face used for gemini quoted texts.") +(defface elpher-gemini-preformatted-toggle + '((t :inherit button)) + "Face used for buttons used to toggle display of preformatted text.") + ;;; Model ;; @@ -440,7 +447,17 @@ For gopher addresses this is a combination of the selector type and selector." (defun elpher-address-host (address) "Retrieve host from ADDRESS object." - (url-host address)) + (let ((host-pre (url-host address))) + ;; The following strips out square brackets which sometimes enclose IPv6 + ;; addresses. Doing this here rather than at the parsing stage may seem + ;; weird, but this lets us way we avoid having to muck with both URL parsing + ;; and reconstruction. It's also more efficient, as this method is not + ;; called during page rendering. + (if (and (> (length host-pre) 2) + (eq (elt host-pre 0) ?\[) + (eq (elt host-pre (- (length host-pre) 1)) ?\])) + (substring host-pre 1 (- (length host-pre) 1)) + host-pre))) (defun elpher-address-user (address) "Retrieve user from ADDRESS object." @@ -448,7 +465,8 @@ For gopher addresses this is a combination of the selector type and selector." (defun elpher-address-port (address) "Retrieve port from ADDRESS object. -If no address is defined, returns 0. (This is for compatibility with the URL library.)" +If no address is defined, returns 0. (This is for compatibility with +the URL library.)" (url-port address)) (defun elpher-gopher-address-selector (address) @@ -601,7 +619,7 @@ previously-visited pages,unless NO-HISTORY is non-nil." (goto-char (point-min))))) (defun elpher-get-default-url-scheme () - "Suggest a default URL scheme to use for visiting addresses based on the current page." + "Suggest default URL scheme for visiting addresses based on the current page." (if elpher-current-page (let* ((address (elpher-page-address elpher-current-page)) (current-type (elpher-address-type address))) @@ -609,8 +627,6 @@ previously-visited pages,unless NO-HISTORY is non-nil." ((or (and 'file (guard (not elpher-history))) `(about ,_)) elpher-default-url-type) - (`(about ,_) - elpher-default-url-type) (_ (url-type address)))) elpher-default-url-type)) @@ -645,7 +661,9 @@ previously-visited pages,unless NO-HISTORY is non-nil." ;; avoid resetting buffer-local variables (elpher-mode)) (let ((inhibit-read-only t) - (ansi-color-context nil)) ;; clean ansi interpreter state + (ansi-color-context nil)) ;; clean ansi interpreter state (also next 2 lines) + (setq-local xterm-color--current-fg nil) + (setq-local xterm-color--current-bg nil) (setq-local network-security-level (default-value 'network-security-level)) (erase-buffer) @@ -1074,7 +1092,7 @@ once they are retrieved from the gopher server." ;; Index rendering (defun elpher-insert-margin (&optional type-name) - "Insert index margin, optionally containing the TYPE-NAME, into the current buffer." + "Insert index margin, optionally containing the TYPE-NAME, into current buffer." (if type-name (progn (insert (format (concat "%" (number-to-string (- elpher-margin-width 1)) "s") @@ -1129,7 +1147,7 @@ If ADDRESS is not supplied or nil the record is rendered as an (insert "\n"))) (defun elpher-click-link (button) - "Function called when the gopher link BUTTON is activated (via mouse or keypress)." + "Function called when the gopher link BUTTON is activated." (let ((page (button-get button 'elpher-page))) (elpher-visit-page page))) @@ -1523,21 +1541,20 @@ treatment that a separate function is warranted." (type (if address (elpher-address-type address) nil)) (type-map-entry (cdr (assoc type elpher-type-map))) (fill-prefix (make-string (+ 1 (length elpher-gemini-link-string)) ?\s))) - (insert elpher-gemini-link-string) - (if type-map-entry - (let* ((face (elt type-map-entry 3)) - (display-string (or given-display-string - (elpher-address-to-iri address))) - (page (elpher-make-page display-string - address))) - (insert-text-button display-string - 'face face - 'elpher-page page - 'action #'elpher-click-link - 'follow-link t - 'help-echo #'elpher--page-button-help)) - (insert (propertize display-string 'face 'elpher-unknown))) - (newline))))) + (when type-map-entry + (insert elpher-gemini-link-string) + (let* ((face (elt type-map-entry 3)) + (display-string (or given-display-string + (elpher-address-to-iri address))) + (page (elpher-make-page display-string + address))) + (insert-text-button display-string + 'face face + 'elpher-page page + 'action #'elpher-click-link + 'follow-link t + 'help-echo #'elpher--page-button-help)) + (newline)))))) (defun elpher-gemini-insert-header (header-line) "Insert header described by HEADER-LINE into a text/gemini document. @@ -1569,7 +1586,6 @@ This function uses Emacs' auto-fill to wrap text sensibly to a maximum width defined by `elpher-gemini-max-fill-width'." (string-match (rx (: line-start - (* (any " \t")) (optional (group (or (: "*" (+ (any " \t"))) (: ">" (* (any " \t")))))))) @@ -1587,34 +1603,76 @@ width defined by `elpher-gemini-max-fill-width'." (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) + (fill-prefix (if line-prefix (make-string (length (match-string 0 text-line)) ?\s) - nil))) + ""))) (insert (elpher-process-text-for-display processed-text-line)) (newline))) +(defun elpher-gemini-pref-expand-collapse (button) + "Function called when the preformatted text toggle BUTTON is activated." + (let ((id (button-get button 'pref-id))) + (if (invisible-p id) + (remove-from-invisibility-spec id) + (add-to-invisibility-spec id)) + (redraw-display))) + +(defun elpher-gemini-insert-preformatted-toggler (alt-text) + "Insert a button for toggling the visibility of preformatted text. +If non-nil, ALT-TEXT is displayed alongside the button." + (let* ((url-string (url-recreate-url (elpher-page-address elpher-current-page))) + (pref-id (intern (concat "pref-" + (number-to-string (point)) + "-" + url-string)))) + (insert elpher-gemini-preformatted-toggle-bullet) + (when alt-text + (insert (propertize (concat alt-text " ") + 'face 'elpher-gemin-preformatted))) + (insert-text-button elpher-gemini-preformatted-toggle-label + 'action #'elpher-gemini-pref-expand-collapse + 'pref-id pref-id + 'face 'elpher-gemini-preformatted-toggle) + (add-to-invisibility-spec pref-id) + (newline) + pref-id)) + +(defun elpher-gemini-insert-preformatted-line (line &optional pref-id) + "Insert a LINE of preformatted text. +PREF-ID is the value assigned to the \"invisible\" text attribute, which +can be used to toggle the display of the preformatted text." + (insert (propertize (concat (elpher-process-text-for-display line) "\n") + 'face 'elpher-gemini-preformatted + 'invisible pref-id + 'rear-nonsticky t))) + (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)) - (auto-fill-mode 1) + (auto-fill-mode 1) + (setq-local buffer-invisibility-spec nil) + (let ((preformatted nil) + (adaptive-fill-mode nil)) ;Prevent automatic setting of fill-prefix (setq-local fill-column (min (window-width) elpher-gemini-max-fill-width)) (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) (elpher-gemini-insert-header line)) - (t (elpher-gemini-insert-text line))))) + (pcase line + ((rx (: "```" (opt (let alt-text (+ any))))) + (setq preformatted + (if preformatted + nil + (if elpher-gemini-hide-preformatted + (elpher-gemini-insert-preformatted-toggler alt-text) + t)))) + ((guard preformatted) + (elpher-gemini-insert-preformatted-line line preformatted)) + ((pred (string-prefix-p "=>")) + (elpher-gemini-insert-link line)) + ((pred (string-prefix-p "#")) + (elpher-gemini-insert-header line)) + (_ (elpher-gemini-insert-text line)))) (elpher-cache-content (elpher-page-address elpher-current-page) - (buffer-string)))) + (buffer-string))))) (defun elpher-render-gemini-plain-text (data _parameters) "Render DATA as plain text file. PARAMETERS is currently unused." @@ -1625,6 +1683,7 @@ width defined by `elpher-gemini-max-fill-width'." (buffer-string)))) (defun elpher-build-current-imenu-index () + "Build imenu index for current elpher buffer." (save-excursion (goto-char (point-min)) (let ((match nil) @@ -1637,6 +1696,7 @@ width defined by `elpher-gemini-max-fill-width'." headers)) (reverse headers)))) + ;; Finger page connection (defun elpher-get-finger-page (renderer) @@ -1682,7 +1742,8 @@ The result is rendered using RENDERER." ;; Other URL page opening (defun elpher-get-other-url-page (renderer) - "Getter which attempts to open the URL specified by the current page (RENDERER must be nil)." + "Getter which attempts to open the URL specified by the current page. +The RENDERER argument to this getter must be nil." (when renderer (elpher-visit-previous-page) (error "Command not supported for general URLs")) @@ -1694,6 +1755,7 @@ The result is rendered using RENDERER." (browse-web url) (browse-url url)))) + ;; File page (defun elpher-get-file-page (renderer) @@ -1817,11 +1879,13 @@ Assumes UTF-8 encoding for all text files." 'help-echo help-string)) (insert "\n") (insert (propertize - (concat "(These documents should be available if you have installed Elpher \n" - " using MELPA. Otherwise you may have to install the manual yourself.)\n") + (concat "(These documents should be available if you have installed Elpher\n" + " from MELPA or non-GNU ELPA. Otherwise you may have to install the\n" + " manual yourself.)\n") 'face 'shadow)) (elpher-restore-pos))) + ;; History page retrieval (defun elpher-show-history () @@ -1923,10 +1987,11 @@ then making that buffer the current buffer. It should not switch to the buffer." (let* ((url (cdr (assq 'location bookmark))) (cleaned-url (string-trim url)) - (page (elpher-page-from-url cleaned-url))) + (page (elpher-page-from-url cleaned-url)) + (buffer (get-buffer-create elpher-buffer-name))) (elpher-with-clean-buffer (elpher-visit-page page)) - (set-buffer (get-buffer elpher-buffer-name)) + (set-buffer buffer) nil)) (defun elpher-bookmark-link () @@ -2106,7 +2171,8 @@ supports the old protocol elpher, where the link is self-contained." ;; that variable is `browse-url-default-browser' by default, so ;; that's the function that gets advised. If the value is an alist, ;; however, we don't know what to do. Better not interfere? - (when (fboundp browse-url-browser-function) + (when (and (symbolp browse-url-browser-function) + (fboundp browse-url-browser-function)) (advice-add browse-url-browser-function :before-while (lambda (url &rest _args) "Handle gemini, gopher, and finger schemes using Elpher." @@ -2163,13 +2229,16 @@ When run interactively HOST-OR-URL is read from the minibuffer." (unless (string-empty-p trimmed-host-or-url) (let ((page (elpher-page-from-url trimmed-host-or-url (elpher-get-default-url-scheme)))) - (switch-to-buffer elpher-buffer-name) + (unless (get-buffer-window elpher-buffer-name t) + (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." + "Go to a particular URL which is read from the minibuffer. +Unlike `elpher-go', the reader is initialized with the URL of the +current page." (interactive) (let* ((address (elpher-page-address elpher-current-page)) (url (read-string (format "Visit URL (default scheme %s): " (elpher-get-default-url-scheme)) @@ -2418,8 +2487,7 @@ functions which initialize the client, namely (setq-local elpher-history nil) (setq-local elpher-buffer-name (buffer-name)) (setq-local bookmark-make-record-function #'elpher-bookmark-make-record) - (setq-local imenu-create-index-function #'elpher-build-current-imenu-index) - (setq-local xterm-color-preserve-properties t)) + (setq-local imenu-create-index-function #'elpher-build-current-imenu-index)) (when (fboundp 'evil-set-initial-state) (evil-set-initial-state 'elpher-mode 'motion))