|
147 | 147 |
|
148 | 148 | ---Triggered from org file when we want to refile headline
|
149 | 149 | function Capture:refile_headline_to_destination()
|
150 |
| - local agenda_file = Files.get_current_file() |
151 |
| - local item = agenda_file:get_closest_headline() |
152 |
| - local lines = agenda_file:get_headline_lines(item) |
| 150 | + local destination_file = Files.get_current_file() |
| 151 | + local item = destination_file:get_closest_headline() |
| 152 | + local lines = destination_file:get_headline_lines(item) |
153 | 153 | return self:_refile_content_with_fallback(lines, nil, item)
|
154 | 154 | end
|
155 | 155 |
|
@@ -194,42 +194,52 @@ function Capture:_refile_content_with_fallback(lines, fallback_file, item)
|
194 | 194 | destination = vim.split(destination, '/', true)
|
195 | 195 |
|
196 | 196 | if not valid_destinations[destination[1]] then
|
| 197 | + if not default_file then -- we know that this comes from org_refile and not org_capture_refile |
| 198 | + utils.echo_error( |
| 199 | + "'" .. destination[1] .. "' is not a file specified in the 'org_agenda_files' setting. Refiling cancelled." |
| 200 | + ) |
| 201 | + return |
| 202 | + end |
197 | 203 | return self:_refile_to_end(default_file, lines, item)
|
198 | 204 | end
|
199 | 205 |
|
200 | 206 | local destination_file = valid_destinations[destination[1]]
|
201 |
| - if not destination[2] or destination[2] == '' then |
| 207 | + local destination_headline = destination[2] |
| 208 | + if not destination_headline or destination_headline == '' then |
202 | 209 | return self:_refile_to_end(destination_file, lines, item)
|
203 | 210 | end
|
204 |
| - return self:refile_to_headline(destination_file, lines, item, destination[2]) |
| 211 | + return self:refile_to_headline(destination_file, lines, item, destination_headline) |
205 | 212 | end
|
206 | 213 |
|
207 |
| ----@param destination_file string |
| 214 | +---@param destination_filename string |
208 | 215 | ---@param lines string[]
|
209 | 216 | ---@param item? Section
|
210 | 217 | ---@param headline_title? string
|
211 |
| -function Capture:refile_to_headline(destination_file, lines, item, headline_title) |
212 |
| - local agenda_file = Files.get(destination_file) |
| 218 | +function Capture:refile_to_headline(destination_filename, lines, item, headline_title) |
| 219 | + local destination_file = Files.get(destination_filename) |
213 | 220 | local headline
|
214 | 221 | if headline_title then
|
215 |
| - headline = agenda_file:find_headline_by_title(headline_title, true) |
216 |
| - end |
| 222 | + headline = destination_file:find_headline_by_title(headline_title, true) |
217 | 223 |
|
218 |
| - if not headline then |
219 |
| - return self._refile_to_end(destination_file, lines, item) |
| 224 | + if not headline then |
| 225 | + utils.echo_error( |
| 226 | + "headline '" .. headline_title .. "' does not exist in '" .. destination_filename .. "'. Aborted refiling." |
| 227 | + ) |
| 228 | + return false |
| 229 | + end |
220 | 230 | end
|
221 | 231 |
|
222 | 232 | if item and item.level <= headline.level then
|
223 | 233 | -- Refiling in same file just moves the lines from one position
|
224 | 234 | -- to another,so we need to apply demote instantly
|
225 |
| - local is_same_file = agenda_file.filename == item.root.filename |
| 235 | + local is_same_file = destination_file.filename == item.root.filename |
226 | 236 | lines = item:demote(headline.level - item.level + 1, true, not is_same_file)
|
227 | 237 | end
|
228 |
| - local refiled = self:_refile_to(destination_file, lines, item, headline.range.end_line) |
| 238 | + local refiled = self:_refile_to(destination_filename, lines, item, headline.range.end_line) |
229 | 239 | if not refiled then
|
230 | 240 | return false
|
231 | 241 | end
|
232 |
| - utils.echo_info(string.format('Wrote %s', destination_file)) |
| 242 | + utils.echo_info(string.format('Wrote %s', destination_filename)) |
233 | 243 | return true
|
234 | 244 | end
|
235 | 245 |
|
|
0 commit comments