aboutsummaryrefslogtreecommitdiff
path: root/modules/textadept/menu.lua
blob: e26a2ec41d930a861d58d4766d53afba774e463e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
-- Copyright 2007-2011 Mitchell mitchell<att>caladbolg.net. See LICENSE.
-- Contributions from Robert Gieseke.

local L = locale.localize
local gui = gui

---
-- Provides dynamic menus for Textadept.
-- This module should be 'require'ed last, after _m.textadept.keys since it
-- looks up defined key commands to show them in menus.
module('_m.textadept.menu', package.seeall)

local _buffer, _view = buffer, view
local m_textadept, m_editing = _m.textadept, _m.textadept.editing
local c, SEPARATOR = _SCINTILLA.constants, { 'separator' }
local utils = m_textadept.keys.utils

-- Get a string uniquely identifying a key command.
-- This is used to match menu items with key commands to show the key shortcut.
-- @param f A value in the `keys` table.
local function get_id(f)
  local id = ''
  if type(f) == 'function' then
    id = tostring(f)
  elseif type(f) == 'table' then
    for _, v in ipairs(f) do id = id..tostring(v) end
  end
  return id
end

---
-- Contains the main menubar.
-- @class table
-- @name menubar
menubar = {
  { title = L('File'),
    { L('gtk-new'), new_buffer },
    { L('gtk-open'), io.open_file },
    { L('Open Recent...'), io.open_recent_file },
    { L('Reload'), _buffer.reload },
    { L('gtk-save'), _buffer.save },
    { L('gtk-save-as'), _buffer.save_as },
    SEPARATOR,
    { L('gtk-close'), _buffer.close },
    { L('Close All'), io.close_all },
    SEPARATOR,
    { L('Load Session...'), m_textadept.session.prompt_load },
    { L('Save Session...'), m_textadept.session.prompt_save },
    SEPARATOR,
    { L('gtk-quit'), quit },
  },
  { title = L('Edit'),
    { L('gtk-undo'), _buffer.undo },
    { L('gtk-redo'), _buffer.redo },
    SEPARATOR,
    { L('gtk-cut'), _buffer.cut },
    { L('gtk-copy'), _buffer.copy },
    { L('gtk-paste'), _buffer.paste },
    { L('Duplicate Line'), _buffer.line_duplicate },
    { L('gtk-delete'), _buffer.clear },
    { L('gtk-select-all'), _buffer.select_all },
    SEPARATOR,
    { L('Match Brace'), m_editing.match_brace },
    { L('Complete Word'), { m_editing.autocomplete_word, '%w_' } },
    { L('Delete Word'), { m_editing.current_word, 'delete' } },
    { L('Highlight Word'), m_editing.highlight_word },
    { L('Toggle Block Comment'), m_editing.block_comment },
    { L('Transpose Characters'), m_editing.transpose_chars },
    { L('Join Lines'), m_editing.join_lines },
    { title = L('Select'),
      { L('Select to Matching Brace'), { m_editing.match_brace, 'select' } },
      { L('Select between XML Tags'), { m_editing.select_enclosed, '>', '<' } },
      { L('Select in XML Tag'), { m_editing.select_enclosed, '<', '>' } },
      { L('Select in Single Quotes'), { m_editing.select_enclosed, "'", "'" } },
      { L('Select in Double Quotes'), { m_editing.select_enclosed, '"', '"' } },
      { L('Select in Parentheses'), { m_editing.select_enclosed, '(', ')' } },
      { L('Select in Brackets'), { m_editing.select_enclosed, '[', ']' } },
      { L('Select in Braces'), { m_editing.select_enclosed, '{', '}' } },
      { L('Select Word'), { m_editing.current_word, 'select' } },
      { L('Select Line'), m_editing.select_line },
      { L('Select Paragraph'), m_editing.select_paragraph },
      { L('Select Indented Block'), m_editing.select_indented_block },
      { L('Select Style'), m_editing.select_style },
    },
    { title = L('Selection'),
      { L('Upper Case Selection'), _buffer.upper_case },
      { L('Lower Case Selection'), _buffer.lower_case },
      SEPARATOR,
      { L('Enclose as XML Tags'), utils.enclose_as_xml_tags },
      { L('Enclose as Single XML Tag'), { m_editing.enclose, '<', ' />' } },
      { L('Enclose in Single Quotes'), { m_editing.enclose, "'", "'" } },
      { L('Enclose in Double Quotes'), { m_editing.enclose, '"', '"' } },
      { L('Enclose in Parentheses'), { m_editing.enclose, '(', ')' } },
      { L('Enclose in Brackets'), { m_editing.enclose, '[', ']' } },
      { L('Enclose in Braces'), { m_editing.enclose, '{', '}' } },
      SEPARATOR,
      { L('Grow Selection'), { m_editing.grow_selection, 1 } },
      { L('Shrink Selection'), { m_editing.grow_selection, -1 } },
      SEPARATOR,
      { L('Move Selected Lines Up'), _buffer.move_selected_lines_up },
      { L('Move Selected Lines Down'), _buffer.move_selected_lines_down },
    },
  },
  { title = L('Search'),
    { L('gtk-find'), gui.find.focus },
    { L('Find Next'), gui.find.find_next },
    { L('Find Previous'), gui.find.find_prev },
    { L('Replace'), gui.find.replace },
    { L('Replace All'), gui.find.replace_all },
    { L('Find Incremental'), gui.find.find_incremental },
    SEPARATOR,
    { L('Find in Files'), utils.find_in_files },
    { L('Goto Next File Found'), { gui.find.goto_file_in_list, true } },
    { L('Goto Previous File Found'), { gui.find.goto_file_in_list, false } },
    SEPARATOR,
    { L('gtk-jump-to'), m_editing.goto_line },
  },
  { title = L('Tools'),
    { L('Command Entry'), gui.command_entry.focus },
    { L('Select Command'), utils.select_command },
    SEPARATOR,
    { L('Run'), m_textadept.run.run },
    { L('Compile'), m_textadept.run.compile },
    { L('Filter Through'), _m.textadept.filter_through.filter_through },
    SEPARATOR,
    { title = L('Adeptsense'),
      { L('Complete Symbol'), m_textadept.adeptsense.complete_symbol },
      { L('Show Documentation'), m_textadept.adeptsense.show_documentation },
    },
    { title = L('Bookmark'),
      { L('Toggle Bookmark'), m_textadept.bookmarks.toggle },
      { L('Clear Bookmarks'), m_textadept.bookmarks.clear },
      { L('Next Bookmark'), m_textadept.bookmarks.goto_next },
      { L('Previous Bookmark'), m_textadept.bookmarks.goto_prev },
      { L('Goto Bookmark...'), m_textadept.bookmarks.goto },
    },
    { title = L('Snapopen'),
      { L('Snapopen User Home'), { m_textadept.snapopen.open, _USERHOME } },
      { L('Snapopen Textadept Home'), { m_textadept.snapopen.open, _HOME } },
      { L('Snapopen Current Directory'), utils.snapopen_filedir },
    },
    { title = L('Snippets'),
      { L('Insert Snippet...'), m_textadept.snippets._select },
      { L('Expand Snippet/Next Placeholder'), m_textadept.snippets._insert },
      { L('Previous Snippet Placeholder'), m_textadept.snippets._previous },
      { L('Cancel Snippet'), m_textadept.snippets._cancel_current },
    },
    SEPARATOR,
    { L('Show Style'), utils.show_style },
  },
  { title = L('Buffer'),
    { L('Next Buffer'), { _view.goto_buffer, _view, 1, true } },
    { L('Previous Buffer'), { _view.goto_buffer, _view, -1, true } },
    { L('Switch to Buffer...'), gui.switch_buffer },
    SEPARATOR,
    { title = L('Indentation'),
      { L('Tab width: 2'), { utils.set_indentation, 2 } },
      { L('Tab width: 3'), { utils.set_indentation, 3 } },
      { L('Tab width: 4'), { utils.set_indentation, 4 } },
      { L('Tab width: 8'), { utils.set_indentation, 8 } },
      SEPARATOR,
      { L('Toggle Use Tabs'), { utils.toggle_property, 'use_tabs' } },
      { L('Convert Indentation'), m_editing.convert_indentation },
    },
    { title = L('EOL Mode'),
      { L('CRLF'), { utils.set_eol_mode, c.SC_EOL_CRLF } },
      { L('CR'), { utils.set_eol_mode, c.SC_EOL_CR } },
      { L('LF'), { utils.set_eol_mode, c.SC_EOL_LF } },
    },
    { title = L('Encoding'),
      { L('UTF-8 Encoding'), { utils.set_encoding, 'UTF-8' } },
      { L('ASCII Encoding'), { utils.set_encoding, 'ASCII' } },
      { L('ISO-8859-1 Encoding'), { utils.set_encoding, 'ISO-8859-1' } },
      { L('MacRoman Encoding'), { utils.set_encoding, 'MacRoman' } },
      { L('UTF-16 Encoding'), { utils.set_encoding, 'UTF-16LE' } },
    },
    SEPARATOR,
    { L('Select Lexer...'), m_textadept.mime_types.select_lexer },
    { L('Refresh Syntax Highlighting'), { _buffer.colourise, _buffer, 0, -1 } },
  },
  { title = L('View'),
    { L('Next View'), { gui.goto_view, 1, true } },
    { L('Previous View'), { gui.goto_view, -1, true } },
    SEPARATOR,
    { L('Split View Horizontal'), { _view.split, _view } },
    { L('Split View Vertical'), { _view.split, _view, true } },
    { L('Unsplit View'), { _view.unsplit, _view } },
    { L('Unsplit All Views'), utils.unsplit_all },
    { L('Grow View'), { utils.grow, 10 } },
    { L('Shrink View'), { utils.shrink, 10 } },
    SEPARATOR,
    { L('Toggle Current Fold'), utils.toggle_current_fold },
    SEPARATOR,
    { L('Toggle View EOL'), { utils.toggle_property, 'view_eol' } },
    { L('Toggle Wrap Mode'), { utils.toggle_property, 'wrap_mode' } },
    { L('Toggle Show Indent Guides'),
      { utils.toggle_property, 'indentation_guides' } },
    { L('Toggle View Whitespace'), { utils.toggle_property, 'view_ws' } },
    { L('Toggle Virtual Space'),
      { utils.toggle_property, 'virtual_space_options',
        c.SCVS_USERACCESSIBLE } },
    SEPARATOR,
    { L('Zoom In'), _buffer.zoom_in },
    { L('Zoom Out'), _buffer.zoom_out },
    { L('Reset Zoom'), utils.reset_zoom },
  },
  { title = L('Help'),
    { L('Show Manual'),
      { utils.open_webpage, _HOME..'/doc/manual/1_Introduction.html' } },
    { L('Show LuaDoc'), { utils.open_webpage, _HOME..'/doc/index.html' } },
    SEPARATOR,
    { L('gtk-about'),
      { gui.dialog, 'ok-msgbox', '--title', 'Textadept', '--informative-text',
        _RELEASE, '--no-cancel' } },
  },
}

---
-- Contains the right-click context menu.
-- @class table
-- @name context_menu
context_menu = {
  { L('gtk-undo'), _buffer.undo },
  { L('gtk-redo'), _buffer.redo },
  SEPARATOR,
  { L('gtk-cut'), _buffer.cut },
  { L('gtk-copy'), _buffer.copy },
  { L('gtk-paste'), _buffer.paste },
  { L('gtk-delete'), _buffer.clear },
  SEPARATOR,
  { L('gtk-select-all'), _buffer.select_all }
}

local key_shortcuts = {}
local menu_actions = {}
local contextmenu_actions = {}

-- Creates a menu suitable for gui.gtkmenu from the menu table format.
-- Also assigns key commands.
-- @param menu The menu to create a gtkmenu from.
-- @return gtkmenu that can be passed to gui.gtkmenu.
local function read_menu_table(menu)
  local gtkmenu = {}
  gtkmenu.title = menu.title
  for _, menuitem in ipairs(menu) do
    if menuitem.title then
      gtkmenu[#gtkmenu + 1] = read_menu_table(menuitem)
    else
      local label, f, menu_id = menuitem[1], menuitem[2], #menu_actions + 1
      local key, mods = keys.get_gdk_key(key_shortcuts[get_id(f)])
      gtkmenu[#gtkmenu + 1] = { label, menu_id, key, mods }
      if f then menu_actions[menu_id] = f end
    end
  end
  return gtkmenu
end

---
-- Sets gui.menubar from the given table of menus.
-- @param menubar The table of menus to create the menubar from. Each table
--   entry is another table that corresponds to a particular menu. A menu can
--   have a 'title' key with string value. Each menu item is either a submenu
--   (another menu table) or a table consisting of two items: string menu text
--   and a function or action table just like in `keys`. The table can
--   optionally contain 2 more number values: a GDK keycode and modifier mask
--   for setting a menu accelerator. If the menu text is 'separator', a menu
--   separator is created and no action table is required.
-- @see keys.get_gdk_key
function set_menubar(menubar)
  key_shortcuts = {}
  for key, f in pairs(keys) do key_shortcuts[get_id(f)] = key end
  menu_actions = {}
  local _menubar = {}
  for i = 1, #menubar do
    _menubar[#_menubar + 1] = gui.gtkmenu(read_menu_table(menubar[i]))
  end
  gui.menubar = _menubar
end

---
-- Sets gui.context_menu from the given menu table.
-- @param menu_table The menu table to create the context menu from. Each table
--   entry is either a submenu or menu text and a function or action table.
-- @see set_menubar
function set_contextmenu(menu_table)
  context_actions = {}
  local context_menu = {}
  for menu_id, menuitem in ipairs(menu_table) do
    context_menu[#context_menu + 1] = { menuitem[1], menu_id + 1000 }
    if menuitem[2] then context_actions[menu_id] = menuitem[2] end
  end
  gui.context_menu = gui.gtkmenu(context_menu)
end

set_menubar(menubar)
set_contextmenu(context_menu)

events.connect(events.MENU_CLICKED, function(menu_id)
  local action, action_type
  if menu_id > 1000 then
    action = context_actions[menu_id - 1000]
  else
    action = menu_actions[menu_id]
  end
  action_type = type(action)
  if action_type ~= 'function' and action_type ~= 'table' then
    error(L('Unknown command:')..' '..tostring(action))
  end
  keys.run_command(action, action_type)
end)

local items, commands
local columns = { L('Command'), L('Key Command') }

-- Builds the item and commands tables for the filteredlist dialog.
-- @param menu The menu to read from.
-- @param title The title of the menu.
-- @param items The current list of items.
-- @param commands The current list of commands.
local function build_command_tables(menu, title, items, commands)
  for _, menuitem in ipairs(menu) do
    if menuitem.title then
      build_command_tables(menuitem, menuitem.title, items, commands)
    elseif menuitem[1] ~= 'separator' then
      local label, f = menuitem[1], menuitem[2]
      if title then label = title..': '..label end
      items[#items + 1] = label:gsub('_([^_])', '%1'):gsub('^gtk%-', '')
      items[#items + 1] = key_shortcuts[get_id(f)] or ''
      commands[#commands + 1] = f
    end
  end
end

---
-- Prompts the user with a filteredlist to run menu commands.
function select_command()
  local i = gui.filteredlist(L('Run Command'), columns, items, true)
  if i then keys.run_command(commands[i + 1], type(commands[i + 1])) end
end

---
-- Rebuilds the tables used by select_command().
-- This should be called every time set_menubar() is called.
function rebuild_command_tables()
  items, commands = {}, {}
  build_command_tables(menubar, nil, items, commands)
end

rebuild_command_tables()