aboutsummaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/.gui.luadoc15
-rw-r--r--core/gui.lua31
2 files changed, 36 insertions, 10 deletions
diff --git a/core/.gui.luadoc b/core/.gui.luadoc
index acc7e0d0..3adba5c8 100644
--- a/core/.gui.luadoc
+++ b/core/.gui.luadoc
@@ -84,3 +84,18 @@ function switch_buffer() end
-- filteredlist dialogs with many items.
-- @return string CocoaDialog result.
function dialog(kind, ...) end
+
+---
+-- Shortcut function for gui.dialog('filtered_list', ...) with 'Ok' and 'Cancel'
+-- buttons.
+-- @param title The title for the filteredlist dialog.
+-- @param columns A column name or list of column names.
+-- @param items An item or list of items.
+-- @param int_return If true, returns the integer index of the selected item in
+-- the filteredlist. Defaults to false which returns the string item.
+-- @param ... Additional parameters to pass to gui.dialog().
+-- @return Either a string or integer on success; nil otherwise.
+-- @usage gui.filteredlist('Title', 'Foo', { 'Bar', 'Baz' })
+-- @usage gui.filteredlist('Title', { 'Foo', 'Bar' }, { 'a', 'b', 'c', 'd' },
+-- false, '--output-column', '2')
+function filteredlist(title, columns, items, int_return, ...) end
diff --git a/core/gui.lua b/core/gui.lua
index ee0daafe..ed609929 100644
--- a/core/gui.lua
+++ b/core/gui.lua
@@ -52,23 +52,34 @@ end
function gui.print(...) gui._print(L('[Message Buffer]'), ...) end
-- LuaDoc is in core/.gui.luadoc.
+function gui.filteredlist(title, columns, items, int_return, ...)
+ local out = gui.dialog('filteredlist',
+ '--title', title,
+ '--button1', 'gtk-ok',
+ '--button2', 'gtk-cancel',
+ '--no-newline',
+ int_return and '' or '--string-output',
+ '--columns', columns,
+ '--items', items,
+ unpack{...})
+ local patt = int_return and '(%-?%d+)\n(%d+)$' or '([^\n]+)\n([^\n]+)$'
+ local response, value = out:match(patt)
+ if response == (int_return and '1' or 'gtk-ok') then
+ return not int_return and value or tonumber(value)
+ end
+end
+
+-- LuaDoc is in core/.gui.luadoc.
function gui.switch_buffer()
- local items = {}
+ local columns, items = { 'Name', 'File' }, {}
for _, buffer in ipairs(_BUFFERS) do
local filename = buffer.filename or buffer._type or L('Untitled')
local dirty = buffer.dirty and '*' or ''
items[#items + 1] = dirty..filename:match('[^/\\]+$')
items[#items + 1] = filename
end
- local response = gui.dialog('filteredlist',
- '--title', L('Switch Buffers'),
- '--button1', 'gtk-ok',
- '--button2', 'gtk-cancel',
- '--no-newline',
- '--columns', 'Name', 'File',
- '--items', items)
- local ok, i = response:match('(%-?%d+)\n(%d+)$')
- if ok == '1' then view:goto_buffer(tonumber(i) + 1, true) end
+ local i = gui.filteredlist(L('Switch Buffers'), columns, items, true)
+ if i then view:goto_buffer(i + 1, true) end
end
local connect = _G.events.connect