1
1
local neorg = require (" neorg.core" )
2
- local modules , lib , log = neorg .modules , neorg . lib , neorg .log
2
+ local modules , log = neorg .modules , neorg .log
3
3
4
4
local dirman --- @type core.dirman
5
+ local neorgcmd --- @type core.neorgcmd
5
6
local refactor --- @type external.refactor
6
7
7
8
local module = modules .create (" external.archive" )
8
9
9
10
module .config .public = {
10
11
-- (Optional) Archive workspace name, defaults to "archive"
11
12
workspace = " archive" ,
13
+ -- (Optional) Enable/disable confirming archive operations
14
+ confirm = true
12
15
}
13
16
14
17
module .events .subscribed = {
15
18
[" core.neorgcmd" ] = {
16
- [" external. archive.current-file" ] = true ,
17
- [" external. archive.current-directory" ] = true ,
18
- [" external. archive.restore" ] = true ,
19
+ [" archive.current-file" ] = true ,
20
+ [" archive.current-directory" ] = true ,
21
+ [" archive.restore" ] = true ,
19
22
},
20
23
}
21
24
@@ -25,43 +28,115 @@ module.setup = function()
25
28
success = true ,
26
29
requires = {
27
30
" core.dirman" ,
28
- " external.interim-ls"
31
+ " core.neorgcmd" ,
32
+ " external.refactor"
29
33
},
30
34
}
31
35
end
32
36
33
37
module .load = function ()
34
38
dirman = module .required [" core.dirman" ]
35
39
refactor = module .required [" external.refactor" ]
40
+ neorgcmd = module .required [" core.neorgcmd" ]
41
+
42
+ local archive_workspace = dirman .get_workspace (module .config .public .workspace )
43
+ if (not archive_workspace ) then
44
+ log .fatal (" [neorg-archive] Archive workspace not found! Please add one to your Neorg config" )
45
+ return
46
+ end
47
+
48
+ neorgcmd .add_commands_from_table ({
49
+ archive = {
50
+ min_args = 1 ,
51
+ max_args = 1 ,
52
+ args = 1 ,
53
+ condition = " norg" ,
54
+ subcommands = {
55
+ [" current-file" ] = {
56
+ args = 0 ,
57
+ name = " archive.current-file" ,
58
+ },
59
+ [" current-directory" ] = {
60
+ args = 0 ,
61
+ name = " archive.current-directory" ,
62
+ },
63
+ [" restore" ] = {
64
+ args = 0 ,
65
+ name = " archive.restore" ,
66
+ },
67
+
68
+
69
+ },
70
+ },
71
+ })
36
72
end
37
73
38
74
module .on_event = function (event )
39
75
if event .split_type [1 ] == " core.neorgcmd" then
40
76
if event .split_type [2 ] == " archive.current-file" then
41
77
module .public .archive_current_file ()
42
78
elseif event .split_type [2 ] == " archive.current-directory" then
43
- module .public .archive_current_file ()
79
+ module .public .archive_current_directory ()
44
80
elseif event .split_type [2 ] == " archive.restore" then
45
- module .public .archive_current_file ()
81
+ module .public .restore ()
46
82
end
47
83
end
48
84
end
49
85
50
86
module .public .archive_current_file = function ()
51
- local workspace = dirman .get_current_workspace ()
87
+ if (not module .private .confirm_archive_operation (" archive current file" )) then
88
+ return
89
+ end
90
+
91
+ local workspace = dirman .get_workspace_match ()
92
+ if (workspace == module .config .public .workspace ) then
93
+ log .error (" Cannot archive files within the archive workspace!" )
94
+ return
95
+ end
52
96
53
- local _ , archive_path = dirman .get_workspace (module .config .public .workspace )
97
+ local archive_path = tostring (dirman .get_workspace (module .config .public .workspace ))
98
+ local current_path = vim .api .nvim_buf_get_name (0 )
99
+ local current_workspace_path = tostring (dirman .get_workspace (workspace ))
54
100
55
- -- get current workspace
56
- -- get workspace path
57
- -- get archive workspace path
58
- -- use refactor module to move file archive workspace path / workspace-name / subpath of prior workspace
101
+ local new_path = current_path :gsub (" ^" .. current_workspace_path , archive_path .. " /" .. workspace )
102
+
103
+ local success = refactor .rename_file (current_path , new_path )
104
+ if (not success ) then
105
+ log .error (" Failed to archive " .. current_path )
106
+ else
107
+ log .info (" Archived file under " .. new_path )
108
+ end
59
109
end
60
110
61
111
module .public .archive_current_directory = function ()
112
+ if (not module .private .confirm_archive_operation (" archive current directory" )) then
113
+ return
114
+ end
115
+
116
+ local workspace = dirman .get_current_workspace ()
117
+ if (workspace == module .config .public .workspace ) then
118
+ log .error (" Cannot archive files within the archive workspace!" )
119
+ return
120
+ end
121
+ -- TODO
62
122
end
63
123
64
124
module .public .archive_restore = function ()
125
+ if (not module .private .confirm_archive_operation (" restore current file" )) then
126
+ return
127
+ end
128
+ -- TODO
129
+ end
130
+
131
+
132
+ --- Confirm archive operations based on module configuration
133
+ --- @param operation string #The name of the operation to confirm, used in user prompt
134
+ --- @return boolean #Confirmation status, if false abort operation
135
+ module .private .confirm_archive_operation = function (operation )
136
+ if (module .config .public .confirm ) then
137
+ return vim .fn .confirm (" Are you sure you want to " .. operation .. " ? y/n" , " &Yes\n &No" , 2 ) == 1
138
+ end
139
+ return true
65
140
end
66
141
67
142
return module
0 commit comments