|
| 1 | +local config = require('orgmode.config') |
| 2 | +local utils = require('orgmode.utils') |
| 3 | +local OrgLinkUrl = require('orgmode.org.links.url') |
| 4 | +local OrgHyperlink = require('orgmode.org.links.hyperlink') |
| 5 | + |
| 6 | +---@class OrgLinks:OrgLinkType |
| 7 | +---@field private files OrgFiles |
| 8 | +---@field private types OrgLinkType[] |
| 9 | +---@field private types_by_name table<string, OrgLinkType> |
| 10 | +---@field private stored_links table<string, string> |
| 11 | +---@field private headline_search OrgLinkHeadlineSearch |
| 12 | +local OrgLinks = { |
| 13 | + stored_links = {}, |
| 14 | +} |
| 15 | +OrgLinks.__index = OrgLinks |
| 16 | + |
| 17 | +---@param opts { files: OrgFiles } |
| 18 | +function OrgLinks:new(opts) |
| 19 | + local this = setmetatable({ |
| 20 | + files = opts.files, |
| 21 | + types = {}, |
| 22 | + types_by_name = {}, |
| 23 | + }, OrgLinks) |
| 24 | + this:_setup_builtin_types() |
| 25 | + return this |
| 26 | +end |
| 27 | + |
| 28 | +---@param link string |
| 29 | +---@return boolean |
| 30 | +function OrgLinks:follow(link) |
| 31 | + for _, source in ipairs(self.types) do |
| 32 | + if source:follow(link) then |
| 33 | + return true |
| 34 | + end |
| 35 | + end |
| 36 | + |
| 37 | + local org_link_url = OrgLinkUrl:new(link) |
| 38 | + if org_link_url.protocol and org_link_url.protocol ~= 'file' and org_link_url.protocol ~= 'id' then |
| 39 | + utils.echo_warning(string.format('Unsupported link protocol: %q', org_link_url.protocol)) |
| 40 | + return false |
| 41 | + end |
| 42 | + |
| 43 | + return self.headline_search:follow(link) |
| 44 | +end |
| 45 | + |
| 46 | +---@param link string |
| 47 | +---@return string[] |
| 48 | +function OrgLinks:autocomplete(link) |
| 49 | + local pattern = '^' .. vim.pesc(link:lower()) |
| 50 | + |
| 51 | + local items = vim.tbl_filter(function(stored_link) |
| 52 | + return stored_link:lower():match(pattern) |
| 53 | + end, vim.tbl_keys(self.stored_links)) |
| 54 | + |
| 55 | + for _, source in ipairs(self.types) do |
| 56 | + utils.concat(items, source:autocomplete(link)) |
| 57 | + end |
| 58 | + |
| 59 | + utils.concat(items, self.headline_search:autocomplete(link)) |
| 60 | + return items |
| 61 | +end |
| 62 | + |
| 63 | +---@param headline OrgHeadline |
| 64 | +function OrgLinks:store_link_to_headline(headline) |
| 65 | + self.stored_links[self:get_link_to_headline(headline)] = headline:get_title() |
| 66 | +end |
| 67 | + |
| 68 | +---@param headline OrgHeadline |
| 69 | +---@return string |
| 70 | +function OrgLinks:get_link_to_headline(headline) |
| 71 | + local title = headline:get_title() |
| 72 | + |
| 73 | + if config.org_id_link_to_org_use_id then |
| 74 | + local id = headline:id_get_or_create() |
| 75 | + if id then |
| 76 | + return ('id:%s::*%s'):format(id, title) |
| 77 | + end |
| 78 | + end |
| 79 | + |
| 80 | + return ('file:%s::*%s'):format(headline.file.filename, title) |
| 81 | +end |
| 82 | + |
| 83 | +---@param file OrgFile |
| 84 | +---@return string |
| 85 | +function OrgLinks:get_link_to_file(file) |
| 86 | + local title = file:get_title() |
| 87 | + |
| 88 | + if config.org_id_link_to_org_use_id then |
| 89 | + local id = file:id_get_or_create() |
| 90 | + if id then |
| 91 | + return ('id:%s::*%s'):format(id, title) |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + return ('file:%s::*%s'):format(file.filename, title) |
| 96 | +end |
| 97 | + |
| 98 | +---@param link_location string |
| 99 | +function OrgLinks:insert_link(link_location) |
| 100 | + local selected_link = OrgHyperlink:new(link_location) |
| 101 | + local desc = selected_link.url:get_target() |
| 102 | + if desc and (desc:match('^%*') or desc:match('^#')) then |
| 103 | + desc = desc:sub(2) |
| 104 | + end |
| 105 | + |
| 106 | + if selected_link.url:get_protocol() == 'id' then |
| 107 | + link_location = ('id:%s'):format(selected_link.url:get_path()) |
| 108 | + end |
| 109 | + |
| 110 | + local link_description = vim.trim(vim.fn.OrgmodeInput('Description: ', desc or '')) |
| 111 | + |
| 112 | + link_location = '[' .. vim.trim(link_location) .. ']' |
| 113 | + |
| 114 | + if link_description ~= '' then |
| 115 | + link_description = '[' .. link_description .. ']' |
| 116 | + end |
| 117 | + |
| 118 | + local insert_from |
| 119 | + local insert_to |
| 120 | + local target_col = #link_location + #link_description + 2 |
| 121 | + |
| 122 | + -- check if currently on link |
| 123 | + local link, position = OrgHyperlink.at_cursor() |
| 124 | + if link and position then |
| 125 | + insert_from = position.from - 1 |
| 126 | + insert_to = position.to + 1 |
| 127 | + target_col = target_col + position.from |
| 128 | + else |
| 129 | + local colnr = vim.fn.col('.') |
| 130 | + insert_from = colnr |
| 131 | + insert_to = colnr + 1 |
| 132 | + target_col = target_col + colnr |
| 133 | + end |
| 134 | + |
| 135 | + local linenr = vim.fn.line('.') or 0 |
| 136 | + local curr_line = vim.fn.getline(linenr) |
| 137 | + local new_line = string.sub(curr_line, 0, insert_from) |
| 138 | + .. '[' |
| 139 | + .. link_location |
| 140 | + .. link_description |
| 141 | + .. ']' |
| 142 | + .. string.sub(curr_line, insert_to, #curr_line) |
| 143 | + |
| 144 | + vim.fn.setline(linenr, new_line) |
| 145 | + vim.fn.cursor(linenr, target_col) |
| 146 | +end |
| 147 | + |
| 148 | +---@param link_type OrgLinkType |
| 149 | +function OrgLinks:add_type(link_type) |
| 150 | + if self.types_by_name[link_type:get_name()] then |
| 151 | + error('Link type ' .. link_type:get_name() .. ' already exists') |
| 152 | + end |
| 153 | + self.types_by_name[link_type:get_name()] = link_type |
| 154 | + table.insert(self.types, link_type) |
| 155 | +end |
| 156 | + |
| 157 | +---@private |
| 158 | +function OrgLinks:_setup_builtin_types() |
| 159 | + self:add_type(require('orgmode.org.links.types.http'):new({ files = self.files })) |
| 160 | + self:add_type(require('orgmode.org.links.types.id'):new({ files = self.files })) |
| 161 | + self:add_type(require('orgmode.org.links.types.line_number'):new({ files = self.files })) |
| 162 | + self:add_type(require('orgmode.org.links.types.custom_id'):new({ files = self.files })) |
| 163 | + self:add_type(require('orgmode.org.links.types.headline'):new({ files = self.files })) |
| 164 | + |
| 165 | + self.headline_search = require('orgmode.org.links.types.headline_search'):new({ files = self.files }) |
| 166 | +end |
| 167 | + |
| 168 | +return OrgLinks |
0 commit comments