Skip to content

Commit 8efeb25

Browse files
author
troiganto
committed
feat(attach): add event to delete attachments of archived headlines
This corresponds to the Emacs function `org-attach-archive-delete-maybe`.
1 parent 6098f4e commit 8efeb25

File tree

9 files changed

+60
-0
lines changed

9 files changed

+60
-0
lines changed

docs/configuration.org

+10
Original file line numberDiff line numberDiff line change
@@ -1232,6 +1232,16 @@ If not =false=, store a link with [[#org_store_link][org_store_link]] when attac
12321232
- =file= - store a =[[file:attach_dir/name]]= link
12331233
- =original= - store a =[[file:original/location]]= link
12341234

1235+
*** org_attach_archive_delete
1236+
:PROPERTIES:
1237+
:CUSTOM_ID: org_attach_archive_delete
1238+
:END:
1239+
- Type: ='always' | 'ask' | 'never'=
1240+
- Default: ='never'=
1241+
1242+
Determines whether attachments are deleted automatically whenever a subtree
1243+
is moved to an archive file. The value ='ask'= means to ask the user.
1244+
12351245
*** org_attach_id_to_path_function_list
12361246
:PROPERTIES:
12371247
:CUSTOM_ID: org_attach_id_to_path_function_list

lua/orgmode/attach/init.lua

+17
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,23 @@ function Attach:delete_all(force, node)
594594
:wait(MAX_TIMEOUT)
595595
end
596596

597+
---Maybe delete subtree attachments when archiving.
598+
---
599+
---This function is called via the `OrgHeadlineArchivedEvent`. The option
600+
---`org_attach_archive_delete' controls its behavior."
601+
---
602+
---@param headline OrgHeadline
603+
---@return nil
604+
function Attach:maybe_delete_archived(headline)
605+
local delete = config.org_attach_archive_delete
606+
if delete == 'always' then
607+
self:delete_all(true, AttachNode.from_headline(headline))
608+
end
609+
if delete == 'ask' then
610+
self:delete_all(false, AttachNode.from_headline(headline))
611+
end
612+
end
613+
597614
---Synchronize the current outline node with its attachments.
598615
---
599616
---Useful after files have been added/removed externally. The Option

lua/orgmode/capture/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ local fs = require('orgmode.utils.fs')
33
local config = require('orgmode.config')
44
local Templates = require('orgmode.capture.templates')
55
local Template = require('orgmode.capture.template')
6+
local EventManager = require('orgmode.events')
67
local Menu = require('orgmode.ui.menu')
78
local Range = require('orgmode.files.elements.range')
89
local CaptureWindow = require('orgmode.capture.window')
@@ -300,6 +301,7 @@ function Capture:refile_file_headline_to_archive(headline)
300301
local headline_category = headline:get_category()
301302
local outline_path = headline:get_outline_path()
302303

304+
EventManager.dispatch(EventManager.event.HeadlineArchived:new(headline, destination_file))
303305
return self
304306
:_refile_from_org_file({
305307
source_headline = headline,

lua/orgmode/config/_meta.lua

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@
246246
---@field org_attach_visit_command string | fun(dir: string) Command or Lua function used to open a directory. Default: 'edit'
247247
---@field org_attach_use_inheritance 'always' | 'selective' | 'never' Determines whether headlines inherit the attachments directory of their parents. Default: 'selective'
248248
---@field org_attach_store_link_p 'original' | 'file' | 'attached' | false If true, attaching a file stores a link to it. Default: 'attached'
249+
---@field org_attach_archive_delete 'always' | 'ask' | 'never' Determines whether to delete a headline's attachments when it is archived. Default: 'never'
249250
---@field org_attach_id_to_path_function_list (string | fun(id: string): (string|nil))[] List of functions used to derive the attachments directory from an ID property.
250251
---@field org_attach_sync_delete_empty_dir 'always' | 'ask' | 'never' Determines whether to delete empty directories when using `org.attach.sync()`. Default: 'ask'
251252
---@field win_split_mode? 'horizontal' | 'vertical' | 'auto' | 'float' | string[] How to open agenda and capture windows. Default: 'horizontal'

lua/orgmode/config/defaults.lua

+1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ local DefaultConfig = {
7979
org_attach_visit_command = 'edit',
8080
org_attach_use_inheritance = 'selective',
8181
org_attach_store_link_p = 'attached',
82+
org_attach_archive_delete = 'never',
8283
org_attach_id_to_path_function_list = {
8384
'uuid_folder_format',
8485
'ts_folder_format',
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---@param event OrgHeadlineArchivedEvent
2+
return function(event)
3+
require('orgmode.attach'):maybe_delete_archived(event.headline)
4+
end

lua/orgmode/events/listeners/init.lua

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
local Events = require('orgmode.events.types')
22
local AlignTags = require('orgmode.events.listeners.align_tags')
3+
local AttachMaybeDeleteArchived = require('orgmode.events.listeners.attach_maybe_delete_archived')
34

45
return {
56
[Events.TodoChanged] = {
@@ -11,4 +12,7 @@ return {
1112
[Events.HeadlinePromoted] = {
1213
AlignTags,
1314
},
15+
[Events.HeadlineArchived] = {
16+
AttachMaybeDeleteArchived,
17+
},
1418
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
---@class OrgHeadlineArchivedEvent: OrgEvent
2+
---@field type string
3+
---@field headline OrgHeadline
4+
---@field destination_file OrgFile
5+
local HeadlineArchivedEvent = {
6+
type = 'orgmode.headline_archived',
7+
}
8+
9+
---@param headline OrgHeadline
10+
---@param destination_file OrgFile
11+
---@return OrgHeadlineArchivedEvent
12+
function HeadlineArchivedEvent:new(headline, destination_file)
13+
local obj = setmetatable({}, self)
14+
self.__index = self
15+
obj.headline = headline
16+
obj.destination_file = destination_file
17+
return obj
18+
end
19+
20+
return HeadlineArchivedEvent

lua/orgmode/events/types/init.lua

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ return {
88
HeadingToggled = require('orgmode.events.types.heading_toggled'),
99
AttachChanged = require('orgmode.events.types.attach_changed_event'),
1010
AttachOpened = require('orgmode.events.types.attach_opened_event'),
11+
HeadlineArchived = require('orgmode.events.types.headline_archived_event'),
1112
}

0 commit comments

Comments
 (0)