Skip to content

Commit a986c82

Browse files
committed
feat(attach): add attachment events
These are implemented in Emacs as the hooks `org-attach-after-change-hook` and `org-attach-open-hook`.
1 parent 627af65 commit a986c82

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

lua/orgmode/attach/core.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
local AttachNode = require('orgmode.attach.node')
2+
local EventManager = require('orgmode.events')
23
local Promise = require('orgmode.utils.promise')
34
local config = require('orgmode.config')
45
local fileops = require('orgmode.attach.fileops')
@@ -289,6 +290,7 @@ function AttachCore:attach(node, file, opts)
289290
if not success then
290291
return nil
291292
end
293+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
292294
node:add_auto_tag()
293295
local link = self.links:store_link_to_attachment({ attach_dir = attach_dir, original = file })
294296
vim.fn.setreg(vim.v.register, link)
@@ -317,6 +319,7 @@ function AttachCore:attach_buffer(node, bufnr, opts)
317319
local attach_file = vim.fs.joinpath(attach_dir, basename)
318320
local data = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false), '\n')
319321
return utils.writefile(attach_file, data, { excl = true }):next(function()
322+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
320323
node:add_auto_tag()
321324
-- Ignore all errors here, this is just to determine whether we can store
322325
-- a link to `bufname`.
@@ -360,6 +363,7 @@ function AttachCore:attach_many(node, files, opts)
360363
end, files)
361364
---@param successes boolean[]
362365
:next(function(successes)
366+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
363367
node:add_auto_tag()
364368
---@param tally orgmode.attach.core.attach_many.result
365369
---@param success boolean
@@ -400,6 +404,7 @@ function AttachCore:attach_new(node, name, opts)
400404
return self:get_dir_or_create(node, opts.set_dir_method, opts.new_dir):next(function(attach_dir)
401405
local path = vim.fs.joinpath(attach_dir, name)
402406
--TODO: the emacs version doesn't run the hook here. Is this correct?
407+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
403408
node:add_auto_tag()
404409
---@type vim.api.keyset.cmd
405410
return Promise.new(function(resolve, reject)
@@ -445,6 +450,7 @@ end
445450
function AttachCore:open(name, node)
446451
local attach_dir = self:get_dir(node)
447452
local path = vim.fs.joinpath(attach_dir, name)
453+
EventManager.dispatch(EventManager.event.AttachOpened:new(node, path))
448454
return assert(vim.ui.open(path))
449455
end
450456

@@ -456,6 +462,7 @@ end
456462
function AttachCore:open_in_vim(name, node)
457463
local attach_dir = self:get_dir(node)
458464
local path = vim.fs.joinpath(attach_dir, name)
465+
EventManager.dispatch(EventManager.event.AttachOpened:new(node, path))
459466
vim.cmd.edit(path)
460467
end
461468

@@ -472,6 +479,7 @@ function AttachCore:delete_one(node, name)
472479
local attach_dir = self:get_dir(node)
473480
local path = vim.fs.joinpath(attach_dir, name)
474481
return fileops.unlink(path):next(function()
482+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
475483
return nil
476484
end)
477485
end
@@ -506,6 +514,7 @@ function AttachCore:delete_all(node, recursive)
506514
return Promise.reject(errmsg)
507515
end
508516
return fileops.remove_directory(attach_dir, { recursive = true }):next(function()
517+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
509518
node:remove_auto_tag()
510519
return attach_dir
511520
end)
@@ -540,6 +549,7 @@ function AttachCore:sync(node, delete_empty_dir)
540549
self:untag(node)
541550
return Promise.resolve()
542551
end
552+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
543553
local non_empty = has_any_non_litter_files(attach_dir)
544554
if non_empty then
545555
node:add_auto_tag()
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---@class OrgAttachChangedEvent: OrgEvent
2+
---@field type string
3+
---@field node OrgAttachNode
4+
---@field attach_dir string
5+
local AttachChangedEvent = {
6+
type = 'orgmode.attach_changed',
7+
}
8+
9+
---@param node OrgAttachNode
10+
---@param attach_dir string
11+
---@return OrgAttachChangedEvent
12+
function AttachChangedEvent:new(node, attach_dir)
13+
local obj = setmetatable({}, self)
14+
self.__index = self
15+
obj.node = node
16+
obj.attach_dir = attach_dir
17+
return obj
18+
end
19+
20+
return AttachChangedEvent
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---@class OrgAttachOpenedEvent: OrgEvent
2+
---@field type string
3+
---@field node OrgAttachNode
4+
---@field path string
5+
local AttachOpenedEvent = {
6+
type = 'orgmode.attach_opened',
7+
}
8+
9+
---@param node OrgAttachNode
10+
---@param path string
11+
---@return OrgAttachOpenedEvent
12+
function AttachOpenedEvent:new(node, path)
13+
local obj = setmetatable({}, self)
14+
self.__index = self
15+
obj.node = node
16+
obj.path = path
17+
return obj
18+
end
19+
20+
return AttachOpenedEvent

lua/orgmode/events/types/init.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,6 @@ return {
66
HeadlinePromoted = require('orgmode.events.types.headline_promoted_event'),
77
HeadlineDemoted = require('orgmode.events.types.headline_demoted_event'),
88
HeadingToggled = require('orgmode.events.types.heading_toggled'),
9+
AttachChanged = require('orgmode.events.types.attach_changed_event'),
10+
AttachOpened = require('orgmode.events.types.attach_opened_event'),
911
}

0 commit comments

Comments
 (0)