2020-06-14 10:34:15 +00:00
|
|
|
;;; gemini-write.el --- Elpher for Titan -*- lexical-binding:t -*-
|
2020-06-05 12:11:07 +00:00
|
|
|
|
|
|
|
;; Copyright (C) 2020 Alex Schroeder
|
|
|
|
;; Copyright (C) 2019 Tim Vaughan
|
|
|
|
|
|
|
|
;; Author: Alex Schroeder <alex@gnu.org>
|
|
|
|
;; Keywords: comm gemini
|
|
|
|
;; Homepage: https://alexschroeder.ch/cgit/gemini-write
|
|
|
|
;; Package-Requires: ((emacs "26"))
|
|
|
|
|
|
|
|
;; This file is not part of GNU Emacs.
|
|
|
|
|
|
|
|
;; This program is free software: you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation, either version 3 of the License, or
|
|
|
|
;; (at your option) any later version.
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with this file. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; This uses Elpher to browse Gemini sites and Gemini Mode to edit
|
2020-06-19 13:19:42 +00:00
|
|
|
;; them using the Titan protocol.
|
2020-06-05 12:11:07 +00:00
|
|
|
|
|
|
|
;; - https://thelambdalab.xyz/elpher/
|
|
|
|
;; - https://git.carcosa.net/jmcbray/gemini.el
|
|
|
|
|
2020-06-14 10:34:15 +00:00
|
|
|
;; Use 'e' to edit a Gemini page on a site that has Titan enabled. Use
|
2020-07-18 06:36:22 +00:00
|
|
|
;; 'C-c C-c' to save. Customize 'elpher-gemini-tokens' to set
|
|
|
|
;; passwords, tokens, or whatever you need in order to edit sites.
|
2020-06-05 12:11:07 +00:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
(require 'elpher)
|
|
|
|
(require 'gemini-mode)
|
|
|
|
|
|
|
|
;;; gemini-write support
|
|
|
|
|
2020-07-17 21:02:58 +00:00
|
|
|
(define-key elpher-mode-map (kbd "e") 'elpher-edit)
|
2020-07-30 21:24:02 +00:00
|
|
|
(define-key elpher-mode-map (kbd "w") 'gemini-write-file)
|
2020-06-05 12:11:07 +00:00
|
|
|
|
2020-07-30 21:24:02 +00:00
|
|
|
(eval-after-load "elpher"
|
|
|
|
'(advice-add 'elpher-render-gemini-plain-text :after 'gemini-write-mime-type-text))
|
2020-07-18 06:36:22 +00:00
|
|
|
|
|
|
|
(defvar gemini-write-text-p nil
|
|
|
|
"A buffer local variable to store whether this is plain text.")
|
|
|
|
|
|
|
|
(defun gemini-write-mime-type-text (&rest ignore)
|
|
|
|
"Remember that this buffer is plain/text."
|
|
|
|
(setq-local gemini-write-text-p t))
|
|
|
|
|
2020-06-05 12:11:07 +00:00
|
|
|
(defun elpher-edit ()
|
2020-07-18 06:36:22 +00:00
|
|
|
"Edit a copy of the current Elpher buffer, if possible.
|
|
|
|
Note that this only makes sense if you're looking at the raw
|
|
|
|
gemtext. If you're looking at the rendered text, editing it
|
|
|
|
will be a mess."
|
2020-06-05 12:11:07 +00:00
|
|
|
(interactive)
|
|
|
|
(let ((address (elpher-page-address elpher-current-page)))
|
2020-07-18 06:36:22 +00:00
|
|
|
(cond ((not (equal (elpher-address-protocol address) "gemini"))
|
|
|
|
(error "Elpher does not know how to edit %s"
|
|
|
|
(elpher-address-protocol address)))
|
|
|
|
((not gemini-write-text-p)
|
|
|
|
(error "Elpher only knows how to edit text/plain"))
|
|
|
|
(t (elpher-edit-buffer (buffer-string) elpher-current-page)))))
|
|
|
|
|
|
|
|
(defun elpher-edit-buffer (text page)
|
|
|
|
"Edit TEXT using Gemini mode for PAGE.
|
|
|
|
PAGE is an Elpher page like `elpher-current-page'."
|
|
|
|
(switch-to-buffer
|
|
|
|
(get-buffer-create
|
|
|
|
(generate-new-buffer-name "*elpher edit*")))
|
|
|
|
(insert text)
|
|
|
|
(goto-char (point-min))
|
|
|
|
(elpher-edit-gemini page))
|
|
|
|
|
|
|
|
(defun elpher-edit-gemini (page)
|
|
|
|
"Edit the current buffer for PAGE."
|
|
|
|
(gemini-mode)
|
|
|
|
(setq-local elpher-current-page page)
|
|
|
|
(let ((address (elpher-page-address elpher-current-page)))
|
|
|
|
(when elpher-use-header
|
2020-08-04 05:42:52 +00:00
|
|
|
(setq header-line-format (url-unhex-string (elpher-address-to-url address)))))
|
2020-07-18 22:32:44 +00:00
|
|
|
(message "Use C-c C-c to save"))
|
2020-06-05 12:11:07 +00:00
|
|
|
|
|
|
|
(add-to-list 'gemini-mode-hook 'gemini-write-init)
|
|
|
|
|
|
|
|
(defun gemini-write-init ()
|
|
|
|
"Add editing commands to `gemini-mode'."
|
2020-07-18 06:36:22 +00:00
|
|
|
(local-set-key (kbd "C-c C-c") 'gemini-write))
|
2020-06-05 12:11:07 +00:00
|
|
|
|
|
|
|
(defcustom elpher-gemini-tokens
|
2020-06-14 21:08:28 +00:00
|
|
|
'(("alexschroeder.ch" . "hello")
|
2020-07-01 15:26:05 +00:00
|
|
|
("communitywiki.org" . "hello")
|
2020-07-17 21:02:58 +00:00
|
|
|
("transjovian.org" . "hello")
|
2020-07-13 08:21:12 +00:00
|
|
|
("127.0.0.1" . "hello")
|
2020-07-01 15:26:05 +00:00
|
|
|
("localhost" . "hello"))
|
2020-06-05 12:11:07 +00:00
|
|
|
"An alist of hostnames and authorization tokens
|
|
|
|
used when writing Gemini pages."
|
|
|
|
:type '(alist :key-type (string :tag "Host") :value-type (string :tag "Token"))
|
|
|
|
:group 'gemini-mode)
|
|
|
|
|
2020-07-18 06:36:22 +00:00
|
|
|
(defun get-elpher-buffer-showing (page)
|
|
|
|
"Return the first Elpher buffer showing PAGE."
|
|
|
|
(catch 'buf
|
2020-07-30 21:24:02 +00:00
|
|
|
(let ((address (elpher-page-address page)))
|
|
|
|
(dolist (buf (buffer-list))
|
|
|
|
(with-current-buffer buf
|
|
|
|
(when (and (eq major-mode 'elpher-mode)
|
|
|
|
(equal (elpher-page-address elpher-current-page)
|
|
|
|
address))
|
|
|
|
(throw 'buf buf)))))))
|
2020-06-05 12:11:07 +00:00
|
|
|
|
|
|
|
(defun gemini-write ()
|
|
|
|
"Save the current Gemini buffer.
|
2020-07-18 06:36:22 +00:00
|
|
|
This will be saved to `elpher-current-page'. If there's an Elpher
|
|
|
|
buffer that already shows this page, that's the buffer we're
|
|
|
|
going to use. Otherwise, a new buffer is used."
|
2020-06-05 12:11:07 +00:00
|
|
|
(interactive)
|
2020-07-30 21:24:02 +00:00
|
|
|
;; using copy-sequence such that the redirect in the original buffer
|
|
|
|
;; doesn't change our address, too
|
|
|
|
(let* ((page (copy-sequence elpher-current-page))
|
2020-07-18 22:32:44 +00:00
|
|
|
(address (elpher-page-address page))
|
|
|
|
(buf (or (get-elpher-buffer-showing page)
|
|
|
|
(with-current-buffer
|
|
|
|
(generate-new-buffer (default-value 'elpher-buffer-name))
|
|
|
|
(elpher-mode)
|
|
|
|
(current-buffer))))
|
2020-06-05 12:11:07 +00:00
|
|
|
(token (cdr (assoc (url-host address) elpher-gemini-tokens)))
|
|
|
|
(data (encode-coding-string (buffer-string) 'utf-8 t)))
|
2020-07-18 06:36:22 +00:00
|
|
|
(switch-to-buffer buf)
|
2020-07-30 21:24:02 +00:00
|
|
|
(setq-local elpher-current-page page)
|
2020-06-05 12:11:07 +00:00
|
|
|
(condition-case the-error
|
|
|
|
(progn
|
2020-07-18 06:36:22 +00:00
|
|
|
(elpher-with-clean-buffer
|
|
|
|
(insert "SAVING GEMINI... (use 'u' to cancel)\n"))
|
2020-06-05 12:11:07 +00:00
|
|
|
(setq elpher-gemini-redirect-chain nil)
|
2020-06-19 13:11:08 +00:00
|
|
|
(titan-write-response address 'elpher-render-gemini token data))
|
2020-06-05 12:11:07 +00:00
|
|
|
(error
|
2020-07-18 06:36:22 +00:00
|
|
|
(elpher-network-error address the-error)))))
|
2020-06-05 12:11:07 +00:00
|
|
|
|
2020-07-30 21:24:02 +00:00
|
|
|
(defun gemini-write-file (file url)
|
|
|
|
"Upload a file."
|
|
|
|
(interactive
|
|
|
|
(list (read-file-name "Upload file: " nil nil t nil 'file-regular-p)
|
|
|
|
(read-string "URL: "
|
|
|
|
(elpher-address-to-url
|
|
|
|
(elpher-page-address elpher-current-page)))))
|
|
|
|
;; using copy-sequence such that the redirect in the original buffer
|
|
|
|
;; doesn't change our address, too
|
|
|
|
(let* ((buf (with-current-buffer
|
|
|
|
(generate-new-buffer (default-value 'elpher-buffer-name))
|
|
|
|
(elpher-mode)
|
|
|
|
(current-buffer)))
|
|
|
|
(address (elpher-address-from-url url))
|
|
|
|
(token (cdr (assoc (url-host address) elpher-gemini-tokens)))
|
|
|
|
(mime-type (completing-read "MIME type: " (mailcap-mime-types) nil t
|
|
|
|
(mailcap-extension-to-mime
|
|
|
|
(file-name-extension file t))))
|
|
|
|
(data (with-temp-buffer
|
|
|
|
(insert-file-contents-literally file)
|
|
|
|
(buffer-string))))
|
|
|
|
(switch-to-buffer buf)
|
|
|
|
(setq-local elpher-current-page
|
|
|
|
(elpher-make-page
|
|
|
|
(format "*elpher upload of %s*" file)
|
|
|
|
address))
|
|
|
|
(condition-case the-error
|
|
|
|
(progn
|
|
|
|
(elpher-with-clean-buffer
|
|
|
|
(insert "SAVING GEMINI... (use 'u' to cancel)\n"))
|
|
|
|
(setq elpher-gemini-redirect-chain nil)
|
|
|
|
(titan-write-response address 'elpher-render-gemini token data mime-type))
|
|
|
|
(error
|
|
|
|
(elpher-network-error address the-error)))))
|
|
|
|
|
|
|
|
(defun titan-write-response (address renderer token data &optional mime-type)
|
2020-06-19 13:11:08 +00:00
|
|
|
"Write request to titan server at ADDRESS and render using RENDERER.
|
2020-07-30 21:24:02 +00:00
|
|
|
The TOKEN, MIME-TYPE, and data size are added as parameters to
|
|
|
|
the last address segment. The MIME type defaults to text/plain."
|
|
|
|
;; using copy sequence so that the buffer's address doesn't change
|
|
|
|
;; from gemini to titan
|
2020-07-18 22:32:44 +00:00
|
|
|
(let ((titan-address (copy-sequence address)))
|
|
|
|
(setf (url-type titan-address) "titan")
|
|
|
|
(elpher-get-host-response
|
|
|
|
titan-address 1965
|
|
|
|
(concat (elpher-address-to-url titan-address)
|
2020-07-30 21:24:02 +00:00
|
|
|
";mime=" (or mime-type "text/plain")
|
2020-07-18 22:32:44 +00:00
|
|
|
";size=" (number-to-string (length data))
|
|
|
|
(if token (concat ";token=" token) "")
|
|
|
|
"\r\n"
|
|
|
|
data)
|
|
|
|
(lambda (response-string)
|
|
|
|
(elpher-process-gemini-response response-string renderer))
|
|
|
|
'gemini)))
|
2020-06-05 12:11:07 +00:00
|
|
|
|
|
|
|
(provide 'gemini-write)
|
|
|
|
|
|
|
|
;;; gemini-write.el ends here
|