orgmode icon indicating copy to clipboard operation
orgmode copied to clipboard

feat: Opening hyperlinks externally

Open maidl0ver opened this issue 1 year ago • 9 comments

This is a stab at https://github.com/nvim-orgmode/orgmode/issues/563 and https://github.com/nvim-orgmode/orgmode/issues/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 (https://github.com/nvim-orgmode/orgmode/issues/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.

maidl0ver avatar Jun 18 '23 14:06 maidl0ver

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 avatar Jun 18 '23 15:06 kristijanhusak

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.

maidl0ver avatar Jun 18 '23 16:06 maidl0ver

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 avatar Jun 19 '23 20:06 maidl0ver

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.

maidl0ver avatar Jun 30 '23 13:06 maidl0ver

@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.

seflue avatar Jun 10 '24 23:06 seflue

@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.

kristijanhusak avatar Jun 11 '24 08:06 kristijanhusak

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

jgollenz avatar Jun 11 '24 18:06 jgollenz

@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.

seflue avatar Jun 16 '24 09:06 seflue

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.

joshpetit avatar Jul 05 '24 23:07 joshpetit