💾 Archived View for epi.benthic.zone › posts › 2021-08-02_OrgGeminiLinks.gmi captured on 2022-07-16 at 15:05:40. Gemini links have been rewritten to link to archived content
⬅️ Previous capture (2021-11-30)
-=-=-=-=-=-=-
2021-08-02
Earlier this evening, I found myself wanting to insert a link to a gemini site into an org file. Unfortunately, org-mode doesn't understand `gemini://
` links out of the box. I think I may have seen some Emacs Lisp code that does this somewhere, but I couldn't remember exactly where I found it, and since I've been on a mission to learn more Emacs Lisp, I figured I would try and write one myself.
To add a new link type to org-mode, we basically need to define two functions. One function will open and one function will store links of that type. The open function is pretty simple
(defun org-gemini-open (path) (elpher-go (concat "gemini:" path)))
This function takes the path to the link that org-mode gives it. Org-mode strips the protocol name by default, so the path looks like "//path/to/gemini.gmi", we add that back so our URL is now `"gemini:://path/to/gemini.gmi"
`. Then we give that URL to `elpher-go
`, which is the function that the Gopher/Gemini browser elpher uses to open URLs.
The second function stores a link to a gemini file.
(defun org-gemini-store-link () (interactive) (when (eq major-mode 'elpher-mode) (let* ((url (elpher-address-to-url (elpher-page-address elpher-current-page))) ) (org-store-link-props :type "gemini" :link url))))
I'm assuming that I will be browsing the gemini file that I want to link to in elpher, so we first check to see if the major mode of our file is elpher-mode. I might also want to link to a local gemini file, in which case I would need to change this to check if it is either elpher-mode or gemini-mode. But for now, just elpher-mode works for me. If we aren't in elpher-mode, then this returns nil, and org-mode will try and find a better function to store the link.
If we are in elpher-mode, we need to construct the URL of the page we are currently visiting. This took a little while to figure out, including some browsing of the elpher source code. The global variable `elpher-current-page
` holds the current page that elpher is visiting, and we find the address of that page using `elpher-page-address
`. Elpher stores the address in its own data structure, so we convert that to a string using `elpher-address-to-url
`. Finally, we want our function to return a property list describing the link, which we create using `org-store-link-props
`.
We could do some other things in here like prompt for a link description, but I haven't figured out how to do that yet.
Finally, we need to inform org-mode to use our link functions. This was also hard to figure out because the org-mode documentation appears to be a little outdated. The new way to do this, apparently, is with the function `org-link-set-parameters
`:
(org-link-set-parameters "gemini" :follow 'org-gemini-open :store 'org-gemini-store-link)
which defines a link type called "gemini" and a function for following a link of that type (our open function) and a function for storing a link of that type (our store function). There are other functions that we could define, but this does what I needed it to do today.