Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Opening hyperlinks externally #578

Open
wants to merge 10 commits into
base: master
Choose a base branch
from

Conversation

maidl0ver
Copy link
Contributor

@maidl0ver maidl0ver commented Jun 18, 2023

This is a stab at #563 and #506. <Leader>oo now opens linked files with xdg-open.
There is also an option to specify custom: links, which I would like to read your thoughts on:

[[custom:nsxiv:~/some-gif.gif][]] - This link opens a GIF file in nsxiv
[[file:~/some-gif.gif][]] - This link opens a GIF file with the specified default opener
[[file:~/org/notes.org]] - Regular behaviour on open

Issues

This functionality depends on nvim's vim.filetype.match() behaviour, which returns nil on unrecognized filetypes inferred from the filename. The issue with this is that some plaintext (i.e. .txt or files without an extension) would be opened externally. Directories are also treated as externally openable (#556). If this is not desired behaviour for directories, this could be fixed by checking the type with vim.loop.fs_stat and made configurable.

@kristijanhusak
Copy link
Member

Is custom: something that exists in the Emacs Orgmode?

Also, does Emacs have a way to provide this custom executable for opening things? I didn't find anything in the docs, only some custom functions that allow this https://emacs.stackexchange.com/questions/57506/open-file-in-orgmode-with-external-application-check-if-file-exists-and-create

@kristijanhusak kristijanhusak self-requested a review June 18, 2023 15:54
@maidl0ver
Copy link
Contributor Author

custom: is not something implemented in Emacs, I was mostly looking at the types of links listed here: https://orgmode.org/guide/Hyperlinks.html.
And here https://orgmode.org/manual/External-Links.html.
The shell: type is pretty much what custom: is, but with an admittedly prettier name (which is easily fixed on our end, of course). Most of the remaining link types are either not relevant for nvim or are already implemented.
There is also this feature: https://orgmode.org/manual/Adding-Hyperlink-Types.html.
Perhaps a more true-to-Emacs way to do this would be to have the link types and their respective openers defined in the config, which I could give a try.

lua/orgmode/org/mappings.lua Outdated Show resolved Hide resolved
@maidl0ver
Copy link
Contributor Author

Renamed custom: to shell:, added a config option (that I couldn't find a good short descriptive name for) to make the external opening behaviour optional. That should make the changes non-breaking.
When it is enabled, however, the issue with some plaintext not recognized as such is still there. Do you know of a better way of inferring filetypes other than with vim.filetype.match()?

I could also give implementing user-defined links a shot, if this is desired functionality.

@maidl0ver maidl0ver requested a review from jgollenz June 19, 2023 20:25
@maidl0ver
Copy link
Contributor Author

Added an option replicating what is described in Adding Hyperlink Types, or at least a way of describing how to follow links:

org_link_types = {
    -- Opens shell: links
    -- For example [[shell:notify-send Ding!]] sends a notification on open-at-point
    shell = {
      handler = function(url)
        url = string.gsub(url, '^shell:', '')
        vim.fn.jobstart(url, { detach = true })
        return
      end,
    },
    -- Opens print: links
    -- An arbitrary example, just prints the entire link
    print = { 
      handler = function(url)
        print(url)
      end,
    },
  }

This allows the user to define any prefix in a table and give it any kind of handler one would want.

@seflue
Copy link
Contributor

seflue commented Jun 10, 2024

@kristijanhusak, @jgollenz What should we do with this PR? Actually I would like to have the possibility to open file links from orgmode with XDG_OPEN (e.g. PDFs, images), because it doesn't make any sense to open them in Neovim as text. I am used to reference PDFs in my notes, but these links are currently only of little use. While I already thought to implement something myself, I found this open PR, which looks promising.

@kristijanhusak
Copy link
Member

@seflue we will probably close this.
I plan to refactor hyperlinks to support multiple sources, like it's done for the autocompletion https://github.com/nvim-orgmode/orgmode/tree/master/lua/orgmode/org/autocompletion, and then expose the API to add own sources. I just didn't get to it yet.

@jgollenz
Copy link
Contributor

@seflue nothing to add from my part, except a sincere sorry to @maidl0ver for the radio silence on this MR.

@seflue
Copy link
Contributor

seflue commented Jun 16, 2024

@seflue we will probably close this. I plan to refactor hyperlinks to support multiple sources, like it's done for the autocompletion https://github.com/nvim-orgmode/orgmode/tree/master/lua/orgmode/org/autocompletion, and then expose the API to add own sources. I just didn't get to it yet.

I wasn't aware how much you actually refactored autocompletion - this just looks much more structured and extendable then what I remember from when I did my first refactoring on hyperlinks.

@joshpetit
Copy link
Contributor

Hey, I'm looking forward to this possibly landing in orgmode. A note I'd love to make is how orgmode has the hyper link types (which you can see here). If the refactor could in some way include adding hyperlink types, that would be awesome.

That would also (I imagine) give direction towards custom link transformation when exporting a pdf (which you can see here). These are some powerful org-mode utils.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants