Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Replaced the context menu in Textadept with a list of buffers.
--- a/menu.lua 2010-10-01 03:52:19.000000000 +0200
+++ b/menu.lua 2010-10-22 17:29:37.136666674 +0200
@@ -11,6 +11,7 @@
local gui = gui
local l = locale
local gtkmenu = gui.gtkmenu
+local context_mbl = {} -- Context menu buffer list
local SEPARATOR = 'separator'
local ID = {
@@ -128,6 +129,9 @@
MANUAL = 901,
LUADOC = 902,
ABOUT = 903,
+ -- Buffers (context menu) (will be generated dynamically)
+ -- The limit of the context menu IDs is 1999
+ BUFFER_START = 1000,
}
@@ -502,6 +506,13 @@
active_table =
{ set_lexer, lexer_menu[menu_id - ID.LEXER_START + 1][1] }
end
+
+ -- Go to buffer
+ if menu_id >= ID.BUFFER_START and menu_id <= ID.BUFFER_START + 999 then
+ active_table =
+ { 'goto_buffer', v, context_mbl[menu_id][2] - ID.BUFFER_START, true }
+ end
+
local f, args
if active_table and #active_table > 0 then
local func = active_table[1]
@@ -525,14 +536,56 @@
end)
-- Right-click context menu.
-gui.context_menu = gtkmenu {
- { l.MENU_EDIT_UNDO, ID.UNDO },
- { l.MENU_EDIT_REDO, ID.REDO },
- { SEPARATOR, ID.SEPARATOR },
- { l.MENU_EDIT_CUT, ID.CUT },
- { l.MENU_EDIT_COPY, ID.COPY },
- { l.MENU_EDIT_PASTE, ID.PASTE },
- { l.MENU_EDIT_DELETE, ID.DELETE },
- { SEPARATOR, ID.SEPARATOR },
- { l.MENU_EDIT_SELECT_ALL, ID.SELECT_ALL }
-}
+--gui.context_menu = gtkmenu {
+-- { l.MENU_EDIT_UNDO, ID.UNDO },
+-- { l.MENU_EDIT_REDO, ID.REDO },
+-- { SEPARATOR, ID.SEPARATOR },
+-- { l.MENU_EDIT_CUT, ID.CUT },
+-- { l.MENU_EDIT_COPY, ID.COPY },
+-- { l.MENU_EDIT_PASTE, ID.PASTE },
+-- { l.MENU_EDIT_DELETE, ID.DELETE },
+-- { SEPARATOR, ID.SEPARATOR },
+-- { l.MENU_EDIT_SELECT_ALL, ID.SELECT_ALL }
+--}
+
+-- Create an empty context menu
+gui.context_menu = gtkmenu {}
+
+function create_buffer_list()
+ index = 1
+ context_mbl = {}
+
+ table.foreach(_G._BUFFERS, function(key, buffer)
+
+ -- Get a(file)name
+ fn = buffer.filename or buffer._type or locale.UNTITLED
+
+ -- Check if buffer content is modified
+ dirty = buffer.dirty and ' *' or ''
+
+ -- Find current buffer
+ if gui.focused_doc_pointer == buffer.doc_pointer then
+ current = ' (current)'
+ else
+ current = ''
+ end
+
+ -- Create menu data
+ context_mbl[ID.BUFFER_START + index] = {
+ fn:match('[^/\\]+$'):gsub('_', '__') .. current .. dirty,
+ ID.BUFFER_START + index
+ }
+ index = index + 1
+ end)
+
+ -- Update context menu
+ gui.context_menu = gtkmenu(context_mbl)
+end
+
+-- Events that update the context menu
+events.connect('file_opened', create_buffer_list)
+events.connect('buffer_new', create_buffer_list)
+events.connect('buffer_deleted', create_buffer_list)
+events.connect('save_point_left', create_buffer_list)
+events.connect('save_point_reached', create_buffer_list)
+events.connect('buffer_after_switch', create_buffer_list)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.