Skip to content

Commit 86ed523

Browse files
fix(input): stop action when input prompt is canceled
Fixes #857
1 parent 64387fc commit 86ed523

File tree

20 files changed

+58
-41
lines changed

20 files changed

+58
-41
lines changed

lua/orgmode/agenda/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,7 @@ end
485485
function Agenda:filter()
486486
local this = self
487487
self.filters:parse_available_filters(self.views)
488-
local filter_term = vim.fn.OrgmodeInput('Filter [+cat-tag/regexp/]: ', self.filters.value, function(arg_lead)
488+
local filter_term = utils.input('Filter [+cat-tag/regexp/]: ', self.filters.value, function(arg_lead)
489489
return utils.prompt_autocomplete(arg_lead, this.filters:get_completion_list(), { '+', '-' })
490490
end)
491491
if filter_term == self.filters.value then

lua/orgmode/agenda/types/search.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
local utils = require('orgmode.utils')
12
---@diagnostic disable: inject-field
23
local OrgAgendaTodosType = require('orgmode.agenda.types.todo')
34

@@ -28,7 +29,7 @@ function OrgAgendaSearchType:get_file_headlines(file)
2829
end
2930

3031
function OrgAgendaSearchType:get_search_term()
31-
return vim.fn.OrgmodeInput('Enter search term: ', self.headline_query or '')
32+
return utils.input('Enter search term: ', self.headline_query or '')
3233
end
3334

3435
function OrgAgendaSearchType:redraw()

lua/orgmode/agenda/types/tags.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ end
9797

9898
---@param files? OrgFiles
9999
function OrgAgendaTagsType:get_tags(files)
100-
local tags = vim.fn.OrgmodeInput('Match: ', self.match_query or '', function(arg_lead)
100+
local tags = utils.input('Match: ', self.match_query or '', function(arg_lead)
101101
return utils.prompt_autocomplete(arg_lead, (files or self.files):get_tags())
102102
end)
103103
if vim.trim(tags) == '' then

lua/orgmode/api/agenda.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ local function get_date(date, name)
1717
return Date.from_string(date)
1818
end
1919

20-
error(('Invalid format for "%s" date in Org Agenda'):format(name))
20+
error(('Invalid format for "%s" date in Org Agenda'):format(name), 0)
2121
end
2222

2323
local function get_shared_opts(options)

lua/orgmode/api/headline.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -171,14 +171,14 @@ function OrgHeadline:set_deadline(date)
171171
if date_instance then
172172
return headline:set_deadline_date(date_instance)
173173
end
174-
error('Invalid string format for deadline date')
174+
error('Invalid string format for deadline date', 0)
175175
end
176176

177177
if Date.is_date_instance(date) then
178178
return headline:set_deadline_date(date)
179179
end
180180

181-
error('Invalid argument to set_deadline')
181+
error('Invalid argument to set_deadline', 0)
182182
end)
183183
end
184184

@@ -211,14 +211,14 @@ function OrgHeadline:set_scheduled(date)
211211
if date_instance then
212212
return headline:set_scheduled_date(date_instance)
213213
end
214-
error('Invalid string format for schedule date')
214+
error('Invalid string format for schedule date', 0)
215215
end
216216

217217
if Date.is_date_instance(date) then
218218
return headline:set_scheduled_date(date)
219219
end
220220

221-
error('Invalid argument to set_scheduled')
221+
error('Invalid argument to set_scheduled', 0)
222222
end)
223223
end
224224

lua/orgmode/api/init.lua

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,14 @@ function OrgApi.load(name)
3838

3939
return list
4040
end
41-
error('Invalid argument to OrgApi.load')
41+
error('Invalid argument to OrgApi.load', 0)
4242
end
4343

4444
--- Get current org buffer file
4545
---@return OrgApiFile
4646
function OrgApi.current()
4747
if vim.bo.filetype ~= 'org' then
48-
error('Not an org buffer.')
48+
error('Not an org buffer.', 0)
4949
end
5050
local name = vim.api.nvim_buf_get_name(0)
5151
return OrgApi.load(name)
@@ -62,14 +62,14 @@ function OrgApi.refile(opts)
6262
})
6363

6464
if getmetatable(opts.source) ~= OrgHeadline then
65-
error('Source must be an OrgApiHeadline')
65+
error('Source must be an OrgApiHeadline', 0)
6666
end
6767

6868
local is_file = getmetatable(opts.destination) == OrgFile
6969
local is_headline = getmetatable(opts.destination) == OrgHeadline
7070

7171
if not is_file and not is_headline then
72-
error('Destination must be an OrgApiFile or OrgApiHeadline')
72+
error('Destination must be an OrgApiFile or OrgApiHeadline', 0)
7373
end
7474

7575
local refile_opts = {

lua/orgmode/capture/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ end
363363
function Capture:get_destination()
364364
local valid_destinations = self:_get_autocompletion_files()
365365

366-
local destination = vim.fn.OrgmodeInput('Enter destination: ', '', function(arg_lead)
366+
local destination = utils.input('Enter destination: ', '', function(arg_lead)
367367
return self:autocomplete_refile(arg_lead, valid_destinations)
368368
end)
369369

lua/orgmode/capture/template/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ function Template:_compile_prompts(content)
363363
if #parts > 2 then
364364
local completion_items = vim.list_slice(parts, 3, #parts)
365365
local prompt = string.format('%s [%s]: ', title, default)
366-
response = vim.fn.OrgmodeInput(prompt, '', function(arg_lead)
366+
response = utils.input(prompt, '', function(arg_lead)
367367
return vim.tbl_filter(function(v)
368368
return v:match('^' .. vim.pesc(arg_lead))
369369
end, completion_items)

lua/orgmode/clock/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ function Clock:org_set_effort()
102102
local item = self.files:get_closest_headline()
103103
-- TODO: Add Effort_ALL property as autocompletion
104104
local current_effort = item:get_property('Effort')
105-
local effort = vim.fn.OrgmodeInput('Effort: ', current_effort or '')
105+
local effort = utils.input('Effort: ', current_effort or '')
106106
local duration = Duration.parse(effort)
107107
if duration == nil then
108108
return utils.echo_error('Invalid duration format: ' .. effort)

lua/orgmode/config/mappings/map_entry.lua

+2-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,8 @@ function MapEntry:attach(default_mapping, user_mapping, opts)
9696

9797
if type(mapping) ~= 'table' then
9898
error(
99-
'Invalid mapping provided for ' .. tostring(self.handler) .. '. Only string and array of strings can be provided'
99+
'Invalid mapping provided for ' .. tostring(self.handler) .. '. Only string and array of strings can be provided',
100+
0
100101
)
101102
end
102103

lua/orgmode/files/file.lua

+6-3
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ end
389389
function OrgFile:get_closest_headline(cursor)
390390
local node = self:closest_headline_node(cursor)
391391
if not node then
392-
error('No headline found')
392+
error('No headline found', 0)
393393
end
394394
return Headline:new(node, self)
395395
end
@@ -504,12 +504,15 @@ end
504504
function OrgFile:get_valid_bufnr()
505505
local bufnr = vim.fn.bufnr(self.filename) or -1
506506
if bufnr < 0 then
507-
error('[orgmode] No valid buffer for file ' .. self.filename .. ' to edit')
507+
error('[orgmode] No valid buffer for file ' .. self.filename .. ' to edit', 0)
508508
end
509509
-- Do not consider unloaded buffers as valid
510510
-- Treesitter is not working in them
511511
if not vim.api.nvim_buf_is_loaded(bufnr) then
512-
error('[orgmode] Cannot edit buffer ' .. tostring(bufnr) .. ' for file ' .. self.filename .. ', it is not loaded')
512+
error(
513+
'[orgmode] Cannot edit buffer ' .. tostring(bufnr) .. ' for file ' .. self.filename .. ', it is not loaded',
514+
0
515+
)
513516
end
514517
return bufnr
515518
end

lua/orgmode/objects/edit_special/init.lua

+3-3
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,16 @@ function EditSpecial:get_context(bufnr)
5353
local exists, ctx = pcall(vim.api.nvim_buf_get_var, bufnr or self.org_bufnr, self.context_var)
5454
---@cast ctx table
5555
if not exists then
56-
error({ message = 'Unable to find context for edit special action' })
56+
error('Unable to find context for edit special action', 0)
5757
end
5858

5959
if not vim.api.nvim_buf_is_valid(ctx.org_bufnr) then
60-
error({ message = 'Org buffer associated with edit special no longer valid' })
60+
error('Org buffer associated with edit special no longer valid', 0)
6161
end
6262

6363
ctx.file = org.files:get(ctx.filename)
6464
if not ctx.file then
65-
error({ message = 'Edit special callback with invalid file: ' .. (ctx.filename or '?') })
65+
error('Edit special callback with invalid file: ' .. (ctx.filename or '?'), 0)
6666
end
6767

6868
ctx.start_extmark_pos = vim.api.nvim_buf_get_extmark_by_id(ctx.org_bufnr, ctx.extmark_ns, ctx.start_extmark, {})

lua/orgmode/org/autocompletion/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ end
3434
---@param source OrgCompletionSource
3535
function OrgCompletion:add_source(source)
3636
if self.sources_by_name[source:get_name()] then
37-
error('Completion source ' .. source:get_name() .. ' already exists')
37+
error('Completion source ' .. source:get_name() .. ' already exists', 0)
3838
end
3939
self.sources_by_name[source:get_name()] = source
4040
table.insert(self.sources, source)

lua/orgmode/org/hyperlinks/init.lua

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ function Hyperlinks.insert_link(link_location)
230230
link_location = ('id:%s'):format(selected_link.url:get_id())
231231
end
232232

233-
local link_description = vim.trim(vim.fn.OrgmodeInput('Description: ', desc or ''))
233+
local link_description = vim.trim(utils.input('Description: ', desc or ''))
234234

235235
link_location = '[' .. vim.trim(link_location) .. ']'
236236

lua/orgmode/org/links/init.lua

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function OrgLinks:insert_link(link_location, desc)
107107
link_location = ('id:%s'):format(selected_link.url:get_path())
108108
end
109109

110-
local link_description = vim.trim(vim.fn.OrgmodeInput('Description: ', desc or ''))
110+
local link_description = vim.trim(utils.input('Description: ', desc or ''))
111111

112112
link_location = '[' .. vim.trim(link_location) .. ']'
113113

@@ -148,7 +148,7 @@ end
148148
---@param link_type OrgLinkType
149149
function OrgLinks:add_type(link_type)
150150
if self.types_by_name[link_type:get_name()] then
151-
error('Link type ' .. link_type:get_name() .. ' already exists')
151+
error('Link type ' .. link_type:get_name() .. ' already exists', 0)
152152
end
153153
self.types_by_name[link_type:get_name()] = link_type
154154
table.insert(self.types, link_type)

lua/orgmode/org/mappings.lua

+2-4
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,10 @@ local config = require('orgmode.config')
99
local constants = require('orgmode.utils.constants')
1010
local ts_utils = require('orgmode.utils.treesitter')
1111
local utils = require('orgmode.utils')
12-
local fs = require('orgmode.utils.fs')
1312
local Table = require('orgmode.files.elements.table')
1413
local EventManager = require('orgmode.events')
1514
local events = EventManager.event
1615
local Babel = require('orgmode.babel')
17-
local ListItem = require('orgmode.files.elements.listitem')
1816

1917
---@class OrgMappings
2018
---@field capture OrgCapture
@@ -48,7 +46,7 @@ function OrgMappings:set_tags(tags)
4846
local current_tags = utils.tags_to_string(headline:get_own_tags())
4947

5048
if not tags then
51-
tags = vim.fn.OrgmodeInput('Tags: ', current_tags, function(arg_lead)
49+
tags = utils.input('Tags: ', current_tags, function(arg_lead)
5250
return utils.prompt_autocomplete(arg_lead, self.files:get_tags())
5351
end)
5452
elseif type(tags) == 'table' then
@@ -786,7 +784,7 @@ end
786784
-- currently on
787785
function OrgMappings:insert_link()
788786
local link = OrgHyperlink.at_cursor()
789-
local link_location = vim.fn.OrgmodeInput('Links: ', link and link.url:to_string() or '', function(arg_lead)
787+
local link_location = utils.input('Links: ', link and link.url:to_string() or '', function(arg_lead)
790788
return self.links:autocomplete(arg_lead)
791789
end)
792790
if vim.trim(link_location) == '' then

lua/orgmode/utils/init.lua

+11
Original file line numberDiff line numberDiff line change
@@ -614,4 +614,15 @@ function utils.sorted_pairs(t)
614614
end
615615
end
616616

617+
---@param prompt string
618+
---@param default? any
619+
---@param completion_fn? fun(arg_lead: string): string[]
620+
function utils.input(prompt, default, completion_fn)
621+
local result = vim.fn.OrgmodeInput(prompt, default, completion_fn)
622+
if result == vim.NIL then
623+
error('Canceled.', 0)
624+
end
625+
return result
626+
end
627+
617628
return utils

lua/orgmode/utils/treesitter/install.lua

+5-5
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ function M.get_path(url)
8787
})
8888
:next(function(code)
8989
if code ~= 0 then
90-
error('[orgmode] Failed to clone tree-sitter-org')
90+
error('[orgmode] Failed to clone tree-sitter-org', 0)
9191
end
9292
return M.exe('git', {
9393
args = { 'checkout', ts_revision },
@@ -96,7 +96,7 @@ function M.get_path(url)
9696
end)
9797
:next(function(code)
9898
if code ~= 0 then
99-
error('[orgmode] Failed to checkout to correct revision on tree-sitter-org')
99+
error('[orgmode] Failed to checkout to correct revision on tree-sitter-org', 0)
100100
end
101101
return path
102102
end)
@@ -110,7 +110,7 @@ function M.run(url)
110110
end, M.compilers)[1]
111111

112112
if not compiler then
113-
error('[orgmode] No C compiler found for installing tree-sitter grammar')
113+
error('[orgmode] No C compiler found for installing tree-sitter grammar', 0)
114114
end
115115

116116
local compiler_args = M.select_compiler_args(compiler)
@@ -127,11 +127,11 @@ function M.run(url)
127127
end)
128128
:next(vim.schedule_wrap(function(code)
129129
if code ~= 0 then
130-
error('[orgmode] Failed to compile parser')
130+
error('[orgmode] Failed to compile parser', 0)
131131
end
132132
local renamed = vim.fn.rename(path .. '/parser.so', package_path .. '/parser/org.so')
133133
if renamed ~= 0 then
134-
error('[orgmode] Failed to move generated tree-sitter parser to runtime folder')
134+
error('[orgmode] Failed to move generated tree-sitter parser to runtime folder', 0)
135135
end
136136
utils.echo_info('Done!')
137137
return true

plugin/orgmode.vim

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
" Wrapper around input() function that allows providing custom completion function
22
" @see https://github.com/neovim/neovim/issues/16301#issuecomment-968247015
33
function! OrgmodeInput(prompt, default, ...) abort
4+
let opts = {
5+
\ 'prompt': a:prompt,
6+
\ 'default': a:default,
7+
\ 'cancelreturn': v:null,
8+
\ }
9+
410
if a:0 > 0
5-
return input(a:prompt, a:default, 'customlist,'..get(a:1, 'name'))
11+
let opts.completion = 'customlist,'..get(a:1, 'name')
612
endif
7-
return input(a:prompt, a:default)
13+
return input(opts)
814
endfunction
915

1016
function OrgmodeWatchDictChanges(dict, key, change_dict) abort

tests/minimal_init.lua

+1-4
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,7 @@ function M.load_plugin(plugin_name, plugin_url)
3838
install_destination,
3939
})
4040
if vim.v.shell_error > 0 then
41-
error(
42-
string.format('>> Failed to clone plugin: "%s" to "%s"!', plugin_name, install_destination),
43-
vim.log.levels.ERROR
44-
)
41+
error(string.format('>> Failed to clone plugin: "%s" to "%s"!', plugin_name, install_destination), 0)
4542
end
4643
end
4744
end

0 commit comments

Comments
 (0)