Skip to content

Commit 815327c

Browse files
committed
feat(attach): add attachment events
1 parent 5a7853b commit 815327c

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')
@@ -428,6 +429,7 @@ function AttachCore:attach(node, file, opts)
428429
if not success then
429430
return nil
430431
end
432+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
431433
node:add_auto_tag()
432434
local link = self.links:store_link_to_attachment({ attach_dir = attach_dir, original = file })
433435
vim.fn.setreg(vim.v.register, link)
@@ -456,6 +458,7 @@ function AttachCore:attach_buffer(node, bufnr, opts)
456458
local attach_file = vim.fs.joinpath(attach_dir, basename)
457459
local data = table.concat(vim.api.nvim_buf_get_lines(bufnr, 0, -1, false), '\n')
458460
return utils.writefile(attach_file, data, { excl = true }):next(function()
461+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
459462
node:add_auto_tag()
460463
-- Ignore all errors here, this is just to determine whether we can store
461464
-- a link to `bufname`.
@@ -499,6 +502,7 @@ function AttachCore:attach_many(node, files, opts)
499502
end, files)
500503
---@param successes boolean[]
501504
:next(function(successes)
505+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
502506
node:add_auto_tag()
503507
---@param tally orgmode.attach.core.attach_many.result
504508
---@param success boolean
@@ -539,6 +543,7 @@ function AttachCore:attach_new(node, name, opts)
539543
return self:get_dir_or_create(node, opts.set_dir_method, opts.new_dir):next(function(attach_dir)
540544
local path = vim.fs.joinpath(attach_dir, name)
541545
--TODO: the emacs version doesn't run the hook here. Is this correct?
546+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
542547
node:add_auto_tag()
543548
---@type vim.api.keyset.cmd
544549
return Promise.new(function(resolve, reject)
@@ -584,6 +589,7 @@ end
584589
function AttachCore:open(name, node)
585590
local attach_dir = self:get_dir(node)
586591
local path = vim.fs.joinpath(attach_dir, name)
592+
EventManager.dispatch(EventManager.event.AttachOpened:new(node, path))
587593
return assert(vim.ui.open(path))
588594
end
589595

@@ -595,6 +601,7 @@ end
595601
function AttachCore:open_in_vim(name, node)
596602
local attach_dir = self:get_dir(node)
597603
local path = vim.fs.joinpath(attach_dir, name)
604+
EventManager.dispatch(EventManager.event.AttachOpened:new(node, path))
598605
vim.cmd.edit(path)
599606
end
600607

@@ -611,6 +618,7 @@ function AttachCore:delete_one(node, name)
611618
local attach_dir = self:get_dir(node)
612619
local path = vim.fs.joinpath(attach_dir, name)
613620
return fileops.unlink(path):next(function()
621+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
614622
return nil
615623
end)
616624
end
@@ -645,6 +653,7 @@ function AttachCore:delete_all(node, recursive)
645653
return Promise.reject(errmsg)
646654
end
647655
return fileops.remove_directory(attach_dir, { recursive = true }):next(function()
656+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
648657
node:remove_auto_tag()
649658
return attach_dir
650659
end)
@@ -679,6 +688,7 @@ function AttachCore:sync(node, delete_empty_dir)
679688
self:untag(node)
680689
return Promise.resolve()
681690
end
691+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
682692
local non_empty = has_any_non_litter_files(attach_dir)
683693
if non_empty then
684694
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)