+ (insert content)
+ (elpher-restore-pos))
+ (elpher-with-clean-buffer
+ (insert "LOADING... (use 'u' to cancel)\n"))
+ (condition-case the-error
+ (let* ((kill-buffer-query-functions nil)
+ (user (let ((filename (elpher-address-filename address)))
+ (if (> (length filename) 1)
+ (substring filename 1)
+ (elpher-address-user address)))))
+ (elpher-get-host-response address 79
+ (concat user "\r\n")
+ renderer))
+ (error
+ (elpher-network-error address the-error))))))
+
+
+;; Telnet page connection
+
+(defun elpher-get-telnet-page (renderer)
+ "Opens a telnet connection to the current page address (RENDERER must be nil)."
+ (when renderer
+ (elpher-visit-previous-page)
+ (error "Command not supported for telnet URLs"))
+ (let* ((address (elpher-page-address elpher-current-page))
+ (host (elpher-address-host address))
+ (port (elpher-address-port address)))
+ (elpher-visit-previous-page)
+ (if (> port 0)
+ (telnet host port)
+ (telnet host))))
+
+
+;; 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)."
+ (when renderer
+ (elpher-visit-previous-page)
+ (error "Command not supported for general URLs"))
+ (let* ((address (elpher-page-address elpher-current-page))
+ (url (elpher-address-to-url address)))
+ (elpher-visit-previous-page) ; Do first in case of non-local exits.
+ (message "Opening URL...")
+ (if elpher-open-urls-with-eww
+ (browse-web url)
+ (browse-url url))))
+
+;; File page
+
+(defun elpher-get-file-page (renderer)
+ "Getter which renders a local file using RENDERER.
+Assumes UTF-8 encoding for all text files."
+ (let* ((address (elpher-page-address elpher-current-page))
+ (filename (elpher-address-filename address)))
+ (unless (file-exists-p filename)
+ (elpher-visit-previous-page)
+ (error "File not found"))
+ (unless (file-readable-p filename)
+ (elpher-visit-previous-page)
+ (error "Could not read from file"))
+ (let ((body (with-temp-buffer
+ (let ((coding-system-for-read 'binary)
+ (coding-system-for-write 'binary))
+ (insert-file-contents-literally filename)
+ (encode-coding-string (buffer-string) 'raw-text)))))
+ (if renderer
+ (funcall renderer body nil)
+ (pcase (file-name-extension filename)
+ ((or "gmi" "gemini")
+ (elpher-render-gemini-map (decode-coding-string body 'utf-8) nil))
+ ((or "htm" "html")
+ (elpher-render-html (decode-coding-string body 'utf-8)))
+ ((or "txt" "")
+ (elpher-render-text (decode-coding-string body 'utf-8)))
+ ((or "jpg" "jpeg" "gif" "png" "bmp" "tif" "tiff")
+ (elpher-render-image body))
+ ((or "gopher" "gophermap")
+ (elpher-render-index (elpher-decode body)))
+ (_
+ (elpher-render-download body))))
+ (elpher-restore-pos))))
+
+
+;; Welcome page retrieval
+
+(defun elpher-get-welcome-page (renderer)
+ "Getter which displays the welcome page (RENDERER must be nil)."
+ (when renderer
+ (elpher-visit-previous-page)
+ (error "Command not supported for welcome page"))
+ (elpher-with-clean-buffer
+ (insert " --------------------------------------------\n"
+ " Elpher Gopher and Gemini Client \n"
+ " version " elpher-version "\n"
+ " --------------------------------------------\n"
+ "\n"
+ "Default bindings:\n"
+ "\n"
+ " - TAB/Shift-TAB: next/prev item on current page\n"
+ " - RET/mouse-1: open item under cursor\n"
+ " - m: select an item on current page by name (autocompletes)\n"
+ " - u/mouse-3/U: return to previous page or to the start page\n"
+ " - g: go to a particular address (gopher, gemini, finger)\n"
+ " - o/O: open a different address selector or the root menu of the current server\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"
+ " - a/A: bookmark the item under cursor or current page\n"
+ " - B: list all bookmarks\n"
+ " - s/S: show current history stack or all previously visted pages\n"
+ " - r: redraw current page (using cached contents if available)\n"
+ " - R: reload current page (regenerates cache)\n"
+ " - !: set character coding system for gopher (default is to autodetect)\n"
+ " - T: toggle TLS gopher mode\n"
+ " - F: forget/discard current TLS client certificate\n"
+ " - .: display the raw server response for the current page\n"
+ "\n"
+ "Start your exploration of gopher space and gemini:\n")
+ (elpher-insert-index-record "Floodgap Systems Gopher Server"
+ (elpher-make-gopher-address ?1 "" "gopher.floodgap.com" 70))
+ (elpher-insert-index-record "Project Gemini home page"
+ (elpher-address-from-url "gemini://gemini.circumlunar.space/"))
+ (insert "\n"
+ "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 (geminispace.info)"
+ (elpher-address-from-url "gemini://geminispace.info/search"))
+ (insert "\n"
+ "Your bookmarks are stored in your ")
+ (insert-text-button "bookmark list"
+ 'face 'link
+ 'action #'elpher-click-link
+ 'follow-link t
+ 'help-echo #'elpher--page-button-help
+ 'elpher-page
+ (elpher-make-page "Elpher Bookmarks"
+ (elpher-make-about-address 'bookmarks)))
+ (insert ".\n")
+ (insert (propertize
+ "(Bookmarks from legacy elpher-bookmarks files will be automatically imported.)\n"
+ 'face 'shadow))
+ (insert "\n"
+ "The gopher home of the Elpher project is here:\n")
+ (elpher-insert-index-record "The Elpher Project Page"
+ (elpher-make-gopher-address ?1
+ "/projects/elpher/"
+ "thelambdalab.xyz"
+ 70))
+ (let ((help-string "RET,mouse-1: Open Elpher info manual (if available)"))
+ (insert "\n"
+ "The following info documentation is available:\n"
+ " - ")
+ (insert-text-button "Elpher Manual"
+ 'face 'link
+ 'action (lambda (_)
+ (interactive)
+ (info "(elpher)"))
+ 'follow-link t
+ 'help-echo help-string)
+ (insert "\n - ")
+ (insert-text-button "Changes introduced by the latest release"
+ 'face 'link
+ 'action (lambda (_)
+ (interactive)
+ (info "(elpher)News"))
+ 'follow-link t
+ '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")
+ 'face 'shadow))
+ (elpher-restore-pos)))
+
+;; History page retrieval
+
+(defun elpher-show-history ()
+ "Show the current contents of elpher's history stack.
+Use \\[elpher-show-visited-pages] to see the entire history.
+This is rendered using `elpher-get-history-page' via `elpher-type-map'."
+ (interactive)
+ (elpher-visit-page
+ (elpher-make-page "Current History Stack"
+ (elpher-make-about-address 'history))))
+
+(defun elpher-show-visited-pages ()
+ "Show the all the pages you've visited using Elpher.
+Use \\[elpher-show-history] to see just the current history stack.
+This is rendered using `elpher-get-visited-pages-page' via `elpher-type-map'."
+ (interactive)
+ (elpher-visit-page
+ (elpher-make-page "Elpher Visted Pages"
+ (elpher-make-about-address 'visited-pages))))
+
+(defun elpher-get-history-page (renderer)
+ "Getter which displays the history page (RENDERER must be nil)."
+ (when renderer
+ (elpher-visit-previous-page)
+ (error "Command not supported for history page"))
+ (elpher-display-history-links elpher-history "Current history stack"))
+
+(defun elpher-get-visited-pages-page (renderer)
+ "Getter which displays the list of visited pages (RENDERER must be nil)."
+ (when renderer
+ (elpher-visit-previous-page)
+ (error "Command not supported for history page"))
+ (elpher-display-history-links
+ (seq-filter (lambda (page)
+ (not (elpher-address-about-p (elpher-page-address page))))
+ elpher-visited-pages)
+ "All visited pages"))
+
+(defun elpher-display-history-links (pages title)
+ "Show all PAGES in an Elpher buffer with a given TITLE."
+ (let* ((title-line (concat " ---- " title " ----"))
+ (footer-line (make-string (length title-line) ?-)))
+ (elpher-with-clean-buffer
+ (insert title-line "\n\n")
+ (if pages
+ (dolist (page pages)
+ (when page
+ (let ((display-string (elpher-page-display-string page))
+ (address (elpher-page-address page)))
+ (elpher-insert-index-record display-string address))))
+ (insert "No history items found.\n"))
+ (insert "\n " footer-line "\n"
+ "Select an entry or press 'u' to return to the previous page.")
+ (elpher-restore-pos))))
+
+
+;;; Bookmarks
+
+;; This code allows Elpher to use the standard Emacs bookmarks: `C-x r
+;; m' to add a bookmark, `C-x r l' to list bookmarks (which is where
+;; you can anotate bookmarks!), `C-x r b' to jump to a bookmark, and
+;; so on. See the Bookmarks section in the Emacs info manual for more.
+
+(defvar elpher-bookmark-link nil
+ "Prefer bookmarking a link or the current page.
+Bind this variable dynamically, or set it to t.
+If you set it to t, the commands \\[bookmark-set-no-overwrite]
+and \\[elpher-set-bookmark-no-overwrite] do the same thing.")
+
+(defun elpher-bookmark-make-record ()
+ "Return a bookmark record.
+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)))
+ (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)
+ "Handler used to open a bookmark using elpher.
+The argument BOOKMARK is a bookmark record passed to the function.
+This handler is responsible for loading the bookmark in some buffer,
+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)))
+ (elpher-with-clean-buffer
+ (elpher-visit-page page))
+ (set-buffer (get-buffer elpher-buffer-name))
+ nil))
+
+(defun elpher-bookmark-link ()
+ "Bookmark the link at point.
+To bookmark the current page, use \\[elpher-bookmark-current]."
+ (interactive)
+ (let ((elpher-bookmark-link t))
+ (bookmark-set-no-overwrite)))
+
+(defun elpher-bookmark-current ()
+ "Bookmark the current page.
+To bookmark the link at point use \\[elpher-bookmark-link]."
+ (interactive)
+ (call-interactively #'bookmark-set-no-overwrite))
+
+(defun elpher-bookmark-import (file)
+ "Import legacy Elpher bookmarks file FILE into Emacs bookmarks."
+ (interactive (list (if (and (boundp 'elpher-bookmarks-file)
+ (file-readable-p elpher-bookmarks-file))
+ elpher-bookmarks-file
+ (read-file-name "Old Elpher bookmarks: "
+ user-emacs-directory nil t
+ "elpher-bookmarks"))))
+ (dolist (bookmark (with-temp-buffer
+ (insert-file-contents file)
+ (read (current-buffer))))
+ (let* ((display-string (car bookmark))
+ (url (cadr bookmark))
+ (record `(,display-string
+ (location . ,url)
+ (handler . elpher-bookmark-jump))))
+ (bookmark-store display-string (cdr record) t)))
+ (bookmark-save))
+
+(defun elpher-get-bookmarks-page (renderer)
+ "Getter which displays the bookmarks (RENDERER must be nil)."
+ (when renderer
+ (elpher-visit-previous-page)
+ (error "Command not supported for bookmarks page"))
+
+ (let ((old-bookmarks-file (or (and (boundp 'elpher-bookmarks-file)
+ elpher-bookmarks-file)
+ (locate-user-emacs-file "elpher-bookmarks"))))
+ (when (and (file-readable-p old-bookmarks-file)
+ (y-or-n-p (concat "Legacy elpher-bookmarks file \""
+ old-bookmarks-file
+ "\" found. Import now?")))
+ (elpher-bookmark-import old-bookmarks-file)
+ (rename-file old-bookmarks-file (concat old-bookmarks-file "-legacy"))))
+
+ (if (and elpher-use-emacs-bookmark-menu
+ elpher-history)
+ (progn
+ (elpher-visit-previous-page)
+ (call-interactively #'bookmark-bmenu-list))
+ (elpher-with-clean-buffer
+ (insert " ---- Elpher Bookmarks ---- \n\n")
+ (bookmark-maybe-load-default-file)
+ (dolist (bookmark (bookmark-maybe-sort-alist))
+ (when (eq #'elpher-bookmark-jump (alist-get 'handler (cdr bookmark)))
+ (let* ((name (car bookmark))
+ (url (alist-get 'location (cdr bookmark)))
+ (address (elpher-address-from-url url)))
+ (elpher-insert-index-record name address))))
+ (when (<= (line-number-at-pos) 3)
+ (insert "No bookmarked pages found.\n"))
+ (insert "\n --------------------------\n\n"
+ "Select an entry or press 'u' to return to the previous page.\n\n"
+ "Bookmarks can be renamed or deleted via the ")
+ (insert-text-button "Emacs bookmark menu"
+ 'action (lambda (_)
+ (interactive)
+ (call-interactively #'bookmark-bmenu-list))
+ 'follow-link t
+ 'help-echo "RET,mouse-1: open Emacs bookmark menu")
+ (insert (substitute-command-keys
+ ",\nwhich can also be opened from anywhere using '\\[bookmark-bmenu-list]'."))
+ (elpher-restore-pos))))
+
+(defun elpher-show-bookmarks ()
+ "Interactive function to display the current list of elpher bookmarks."
+ (interactive)
+ (elpher-visit-page
+ (elpher-make-page "Elpher Bookmarks"
+ (elpher-make-about-address 'bookmarks))))
+
+
+;;; Integrations
+;;
+
+;;; Org
+
+(defun elpher-org-export-link (link description format protocol)
+ "Export a LINK with DESCRIPTION for the given PROTOCOL and FORMAT.
+
+FORMAT is an Org export backend. DESCRIPTION may be nil. PROTOCOL may be one
+of gemini, gopher or finger."
+ (let* ((url (if (equal protocol "elpher")
+ (string-remove-prefix "elpher:" link)
+ (format "%s:%s" protocol link)))
+ (desc (or description url)))
+ (pcase format
+ (`gemini (format "=> %s %s" url desc))
+ (`html (format "<a href=\"%s\">%s</a>" url desc))
+ (`latex (format "\\href{%s}{%s}" url desc))
+ (_ (if (not description)
+ url
+ (format "%s (%s)" desc url))))))
+
+(defun elpher-org-store-link ()
+ "Store link to an `elpher' page in Org."
+ (when (eq major-mode 'elpher-mode)
+ (let* ((url (elpher-info-current))
+ (desc (car elpher-current-page))
+ (protocol (cond
+ ((string-prefix-p "gemini:" url) "gemini")
+ ((string-prefix-p "gopher:" url) "gopher")
+ ((string-prefix-p "finger:" url) "finger")
+ (t "elpher"))))
+ (when (equal "elpher" protocol)
+ ;; Weird link. Or special inner link?
+ (setq url (concat "elpher:" url)))
+ (org-link-store-props :type protocol :link url :description desc)
+ t)))
+
+(defun elpher-org-follow-link (link protocol)
+ "Visit a LINK for the given PROTOCOL.
+
+PROTOCOL may be one of gemini, gopher or finger. This method also
+supports the old protocol elpher, where the link is self-contained."
+ (let ((url (if (equal protocol "elpher")
+ (string-remove-prefix "elpher:" link)
+ (format "%s:%s" protocol link))))
+ (elpher-go url)))
+
+(defun elpher-org-mode-integration ()
+ "Set up `elpher' integration for `org-mode'."
+ (org-link-set-parameters
+ "elpher"
+ :store #'elpher-org-store-link
+ :export (lambda (link description format _plist)
+ (elpher-org-export-link link description format "elpher"))
+ :follow (lambda (link _arg) (elpher-org-follow-link link "elpher")))
+ (org-link-set-parameters
+ "gemini"
+ :export (lambda (link description format _plist)
+ (elpher-org-export-link link description format "gemini"))
+ :follow (lambda (link _arg) (elpher-org-follow-link link "gemini")))
+ (org-link-set-parameters
+ "gopher"
+ :export (lambda (link description format _plist)
+ (elpher-org-export-link link description format "gopher"))
+ :follow (lambda (link _arg) (elpher-org-follow-link link "gopher")))
+ (org-link-set-parameters
+ "finger"
+ :export (lambda (link description format _plist)
+ (elpher-org-export-link link description format "finger"))
+ :follow (lambda (link _arg) (elpher-org-follow-link link "finger"))))
+
+(add-hook 'org-mode-hook #'elpher-org-mode-integration)
+
+;;; Browse URL
+
+;;;###autoload
+(defun elpher-browse-url-elpher (url &rest _args)
+ "Browse URL using Elpher. This function is used by `browse-url'."
+ (interactive (browse-url-interactive-arg "Elpher URL: "))
+ (elpher-go url))
+
+;; Use elpher to open gopher, finger and gemini links
+;; For recent version of `browse-url' package
+(if (boundp 'browse-url-default-handlers)
+ (add-to-list
+ 'browse-url-default-handlers
+ '("^\\(gopher\\|finger\\|gemini\\)://" . elpher-browse-url-elpher))
+ ;; Patch `browse-url-browser-function' for older ones. The value of
+ ;; 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)
+ (advice-add browse-url-browser-function :before-while
+ (lambda (url &rest _args)
+ "Handle gemini, gopher, and finger schemes using Elpher."
+ (let ((scheme (downcase (car (split-string url ":" t)))))
+ (if (member scheme '("gemini" "gopher" "finger"))
+ ;; `elpher-go' always returns nil, which will stop the
+ ;; advice chain here in a before-while
+ (elpher-go url)
+ ;; chain must continue, then return t.
+ t))))))
+
+;; Register "gemini://" as a URI scheme so `browse-url' does the right thing
+(with-eval-after-load 'thingatpt
+ (add-to-list 'thing-at-point-uri-schemes "gemini://"))
+
+;;; Mu4e:
+
+;; Make mu4e aware of the gemini world
+(setq mu4e~view-beginning-of-url-regexp
+ "\\(?:https?\\|gopher\\|finger\\|gemini\\)://\\|mailto:")
+
+;;; eww:
+
+;; Let elpher handle gemini, gopher links in eww buffer.
+(setq eww-use-browse-url
+ "\\`mailto:\\|\\(\\`gemini\\|\\`gopher\\|\\`finger\\)://")
+
+
+;;; Interactive procedures