Skip to content

Commit 4df557c

Browse files
author
troiganto
committed
feat(attach): add attachment events
1 parent 322672f commit 4df557c

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

lua/orgmode/attach/core.lua

+11
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 methods = require('orgmode.attach.methods')
34
local Promise = require('orgmode.utils.promise')
45
local config = require('orgmode.config')
@@ -394,6 +395,7 @@ function AttachCore:attach(node, file, opts)
394395
if not success then
395396
return nil
396397
end
398+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
397399
node:toggle_auto_tag(true)
398400
local link = self.links:store_link_to_attachment({ attach_dir = attach_dir, original = file })
399401
vim.fn.setreg(vim.v.register, link)
@@ -421,6 +423,7 @@ function AttachCore:attach_url(node, url, opts)
421423
if not success then
422424
return nil
423425
end
426+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
424427
node:toggle_auto_tag(true)
425428
local link = self.links:store_link_to_attachment({ attach_dir = attach_dir, original = url })
426429
vim.fn.setreg(vim.v.register, link)
@@ -448,6 +451,7 @@ function AttachCore:attach_buffer(node, bufnr, opts)
448451
if not success then
449452
return nil
450453
end
454+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
451455
node:toggle_auto_tag(true)
452456
-- Ignore all errors here, this is just to determine whether we can store
453457
-- a link to `bufname`.
@@ -491,6 +495,7 @@ function AttachCore:attach_many(node, files, opts)
491495
end, files)
492496
---@param successes boolean[]
493497
:next(function(successes)
498+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
494499
node:toggle_auto_tag(true)
495500
---@param tally orgmode.attach.core.attach_many.result
496501
---@param success boolean
@@ -531,6 +536,7 @@ function AttachCore:attach_new(node, name, opts)
531536
return self:get_dir_or_create(node, opts.set_dir_method, opts.new_dir):next(function(attach_dir)
532537
local path = vim.fs.joinpath(attach_dir, name)
533538
--TODO: the emacs version doesn't run the hook here. Is this correct?
539+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
534540
node:toggle_auto_tag(true)
535541
---@type vim.api.keyset.cmd
536542
return Promise.new(function(resolve, reject)
@@ -576,6 +582,7 @@ end
576582
function AttachCore:open(name, node)
577583
local attach_dir = self:get_dir(node)
578584
local path = vim.fs.joinpath(attach_dir, name)
585+
EventManager.dispatch(EventManager.event.AttachOpened:new(node, path))
579586
return assert(vim.ui.open(path))
580587
end
581588

@@ -587,6 +594,7 @@ end
587594
function AttachCore:open_in_vim(name, node)
588595
local attach_dir = self:get_dir(node)
589596
local path = vim.fs.joinpath(attach_dir, name)
597+
EventManager.dispatch(EventManager.event.AttachOpened:new(node, path))
590598
vim.cmd.edit(path)
591599
end
592600

@@ -603,6 +611,7 @@ function AttachCore:delete_one(node, name)
603611
local attach_dir = self:get_dir(node)
604612
local path = vim.fs.joinpath(attach_dir, name)
605613
return fileops.unlink(path):next(function()
614+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
606615
return nil
607616
end)
608617
end
@@ -637,6 +646,7 @@ function AttachCore:delete_all(node, recursive)
637646
return Promise.reject(errmsg)
638647
end
639648
return fileops.remove_directory(attach_dir, { recursive = true }):next(function()
649+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
640650
node:toggle_auto_tag(false)
641651
return attach_dir
642652
end)
@@ -671,6 +681,7 @@ function AttachCore:sync(node, delete_empty_dir)
671681
self:untag(node)
672682
return Promise.resolve()
673683
end
684+
EventManager.dispatch(EventManager.event.AttachChanged:new(node, attach_dir))
674685
local non_empty = has_any_non_litter_files(attach_dir)
675686
node:toggle_auto_tag(non_empty)
676687
if non_empty then
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
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

+2
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)