aboutsummaryrefslogtreecommitdiffhomepage
path: root/core/gui.lua
blob: 9e657d45f90adbdbee28ebfdf021c1fe90155b74 (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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
-- Copyright 2007-2011 Mitchell mitchell<att>caladbolg.net. See LICENSE.

local L = locale.localize
local gui = gui

--[[ This comment is for LuaDoc.
--- The core gui table.
module('gui')]]

-- Markdown:
-- ## Fields
--
-- * `title` [string]: The title of the Textadept window.
-- * `menubar` [table]: A table of GTK menus defining a menubar. (Write-only)
-- * `context_menu`: A GTK menu defining the editor's context menu.
-- * `clipboard_text` [string]: The text on the clipboard. (Read-only)
-- * `statusbar_text` [string]: The text displayed by the statusbar.
-- * `docstatusbar_text` [string]: The text displayed by the doc statusbar.
--   (Write-only)
-- * `size` [table]: The size of the Textadept window (`{ width, height }`).

-- Helper function for printing messages to buffers.
-- @see gui._print
local function _print(buffer_type, ...)
  if buffer._type ~= buffer_type then
    for i, view in ipairs(_VIEWS) do
      if view.buffer._type == buffer_type then gui.goto_view(i) break end
    end
    if view.buffer._type ~= buffer_type then
      view:split()
      for i, buffer in ipairs(_BUFFERS) do
        if buffer._type == buffer_type then view:goto_buffer(i) break end
      end
      if buffer._type ~= buffer_type then
        new_buffer()._type = buffer_type
        events.emit(events.FILE_OPENED)
      end
    end
  end
  local args, n = {...}, select('#', ...)
  for i = 1, n do args[i] = tostring(args[i]) end
  buffer:append_text(table.concat(args, '\t'))
  buffer:append_text('\n')
  buffer:goto_pos(buffer.length)
  buffer:set_save_point()
end
---
-- Helper function for printing messages to buffers.
-- Splits the view and opens a new buffer for printing messages. If the message
-- buffer is already open and a view is currently showing it, the message is
-- printed to that view. Otherwise the view is split, goes to the open message
-- buffer, and prints to it.
-- @param buffer_type String type of message buffer.
-- @param ... Message strings.
-- @usage gui._print(L('[Error Buffer]'), error_message)
-- @usage gui._print(L('[Message Buffer]'), message)
-- @name _print
function gui._print(buffer_type, ...) pcall(_print, buffer_type, ...) end

---
-- Prints messages to the Textadept message buffer.
-- Opens a new buffer (if one has not already been opened) for printing
-- messages.
-- @param ... Message strings.
-- @name print
function gui.print(...) gui._print(L('[Message Buffer]'), ...) 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.
--   Not compatible with a `'--select-multiple'` filteredlist.
-- @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')
-- @name filteredlist
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,
                         ...)
  local patt = int_return and '^(%-?%d+)\n(%d+)$' or '^([^\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

---
-- Displays a dialog with a list of buffers to switch to and switches to the
-- selected one, if any.
-- @name switch_buffer
function gui.switch_buffer()
  local columns, items = { L('Name'), L('File') }, {}
  for _, buffer in ipairs(_BUFFERS) do
    local filename = buffer.filename or buffer._type or L('Untitled')
    items[#items + 1] = (buffer.dirty and '*' or '')..filename:match('[^/\\]+$')
    items[#items + 1] = filename
  end
  local i = gui.filteredlist(L('Switch Buffers'), columns, items, true)
  if i then view:goto_buffer(i + 1) end
end

---
-- Goes to the buffer with the given filename.
-- If the desired buffer is open in a view, goes to that view. Otherwise, opens
-- the buffer in either the `preferred_view` if given, the first view that is
-- not the current one, a split view if `split` is `true`, or the current view.
-- @param filename The filename of the buffer to go to.
-- @param split If there is only one view, split it and open the buffer in the
--   other view.
-- @param preferred_view When multiple views exist and the desired buffer is not
--   open in any of them, open it in this one.
-- @name goto_file
function gui.goto_file(filename, split, preferred_view)
  if #_VIEWS == 1 and view.buffer.filename ~= filename and split then
    view:split()
  else
    local other_view = _VIEWS[preferred_view]
    for i, v in ipairs(_VIEWS) do
      if v.buffer.filename == filename then gui.goto_view(i) return end
      if not other_view and v ~= view then other_view = i end
    end
    if other_view then gui.goto_view(other_view) end
  end
  io.open_file(filename)
end

local THEME
---
-- Sets the editor theme from the given name.
-- Themes in `_USERHOME/themes/` are checked first, followed by `_HOME/themes/`.
-- If the name contains slashes ('/' on Linux and Mac OSX and '\' on Win32), it
-- is assumed to be an absolute path so `_USERHOME` and `_HOME` are not checked.
-- Throws an error if the theme is not found. Any errors in the theme are
-- printed to `io.stderr`.
-- @param name The name or absolute path of a theme. If nil, sets the default
--   theme.
-- @name set_theme
function gui.set_theme(name)
  if not name then
    -- Read theme from ~/.textadept/theme, defaulting to 'light'.
    local f = io.open(_USERHOME..'/theme', 'rb')
    if f then
      name = f:read('*line'):match('[^\r\n]+')
      f:close()
    end
    if not name or name == '' then name = 'light' end
  end

  -- Get the path of the theme.
  local theme
  if not name:find('[/\\]') then
    if lfs.attributes(_USERHOME..'/themes/'..name) then
      theme = _USERHOME..'/themes/'..name
    elseif lfs.attributes(_HOME..'/themes/'..name) then
      theme = _HOME..'/themes/'..name
    end
  elseif lfs.attributes(name) then
    theme = name
  end
  if not theme then error(('"%s" %s'):format(name, L("theme not found."))) end

  if buffer and view then
    local current_buffer, current_view = _BUFFERS[buffer], _VIEWS[view]
    for i in ipairs(_BUFFERS) do
      view:goto_buffer(i)
      buffer.property['lexer.lpeg.color.theme'] = theme..'/lexer.lua'
      local lexer = buffer:get_lexer()
      buffer:set_lexer('null') -- lexer needs to be changed to reset styles
      buffer:set_lexer(lexer)
      local ok, err = pcall(dofile, theme..'/buffer.lua')
      if not ok then io.stderr:write(err) end
    end
    view:goto_buffer(current_buffer)
    for i in ipairs(_VIEWS) do
      gui.goto_view(i)
      local lexer = buffer:get_lexer()
      buffer:set_lexer('null') -- lexer needs to be changed to reset styles
      buffer:set_lexer(lexer)
      local ok, err = pcall(dofile, theme..'/view.lua')
      if not ok then io.stderr:write(err) end
    end
    gui.goto_view(current_view)
  end
  THEME = theme
end

---
-- Prompts the user to select an editor theme from a filtered list.
-- @name select_theme
function gui.select_theme()
  local themes, themes_found = {}, {}
  for theme in lfs.dir(_HOME..'/themes') do
    if not theme:find('^%.%.?$') then themes_found[theme] = true end
  end
  if lfs.attributes(_USERHOME..'/themes') then
    for theme in lfs.dir(_USERHOME..'/themes') do
      if not theme:find('^%.%.?$') then themes_found[theme] = true end
    end
  end
  for theme in pairs(themes_found) do themes[#themes + 1] = theme end
  table.sort(themes)
  local theme = gui.filteredlist(L('Select Theme'), L('Name'), themes)
  if not theme then return end
  gui.set_theme(theme)
  local f = io.open(_USERHOME..'/theme', 'wb')
  if not f then return end
  f:write(theme)
  f:close()
end

local connect = events.connect

-- Sets default properties for a Scintilla window.
connect(events.VIEW_NEW, function()
  local buffer = buffer
  local c = _SCINTILLA.constants

  -- Allow redefinitions of these Scintilla key commands.
  local ctrl_keys = {
    '[', ']', '/', '\\', 'Z', 'Y', 'X', 'C', 'V', 'A', 'L', 'T', 'D', 'U'
  }
  local ctrl_shift_keys = { 'L', 'T', 'U', 'Z' }
  for _, key in ipairs(ctrl_keys) do
    buffer:clear_cmd_key(string.byte(key), c.SCMOD_CTRL)
  end
  for _, key in ipairs(ctrl_shift_keys) do
    buffer:clear_cmd_key(string.byte(key), c.SCMOD_CTRL + c.SCMOD_SHIFT)
  end
  -- Load theme.
  local ok, err = pcall(dofile, THEME..'/view.lua')
  if not ok then io.stderr:write(err) end
end)
connect(events.VIEW_NEW, function() events.emit(events.UPDATE_UI) end)

local SETDIRECTFUNCTION = _SCINTILLA.properties.direct_function[1]
local SETDIRECTPOINTER = _SCINTILLA.properties.doc_pointer[2]
local SETLEXERLANGUAGE = _SCINTILLA.functions.set_lexer_language[1]
local function set_properties()
  local buffer = buffer
  -- Lexer.
  buffer:set_lexer_language('lpeg')
  buffer:private_lexer_call(SETDIRECTFUNCTION, buffer.direct_function)
  buffer:private_lexer_call(SETDIRECTPOINTER, buffer.direct_pointer)
  buffer:private_lexer_call(SETLEXERLANGUAGE, 'container')
  buffer.style_bits = 8
  -- Properties.
  buffer.property['textadept.home'] = _HOME
  buffer.property['lexer.lpeg.home'] = _LEXERPATH
  buffer.property['lexer.lpeg.script'] = _HOME..'/lexers/lexer.lua'
  buffer.property['lexer.lpeg.color.theme'] = THEME..'/lexer.lua'
  -- Buffer.
  buffer.code_page = _SCINTILLA.constants.SC_CP_UTF8
  -- Load theme.
  local ok, err = pcall(dofile, THEME..'/buffer.lua')
  if not ok then io.stderr:write(err) end
end

-- Sets default properties for a Scintilla document.
connect(events.BUFFER_NEW, function()
  -- Normally when an error occurs, a new buffer is created with the error
  -- message, but if an error occurs here, this event would be called again and
  -- again, erroring each time resulting in an infinite loop; print error to
  -- stderr instead.
  local ok, err = pcall(set_properties)
  if not ok then io.stderr:write(err) end
end)
connect(events.BUFFER_NEW, function() events.emit(events.UPDATE_UI) end)

-- Sets the title of the Textadept window to the buffer's filename.
-- @param buffer The global buffer.
local function set_title(buffer)
  local filename = buffer.filename or buffer._type or L('Untitled')
  gui.title = string.format('%s %s Textadept (%s)', filename:match('[^/\\]+$'),
                            buffer.dirty and '*' or '-', filename)
end

-- Changes Textadept title to show the buffer as being "clean".
connect(events.SAVE_POINT_REACHED, function()
  buffer.dirty = false
  set_title(buffer)
end)

-- Changes Textadept title to show thee buffer as "dirty".
connect(events.SAVE_POINT_LEFT, function()
  buffer.dirty = true
  set_title(buffer)
end)

-- Open uri(s).
connect(events.URI_DROPPED, function(utf8_uris)
  for utf8_uri in utf8_uris:gmatch('[^\r\n]+') do
    if utf8_uri:find('^file://') then
      utf8_uri = utf8_uri:match('^file://([^\r\n]+)')
      utf8_uri = utf8_uri:gsub('%%(%x%x)', function(hex)
        return string.char(tonumber(hex, 16))
      end)
      if WIN32 then utf8_uri = utf8_uri:sub(2, -1) end -- ignore leading '/'
      local uri = utf8_uri:iconv(_CHARSET, 'UTF-8')
      if lfs.attributes(uri).mode ~= 'directory' then io.open_file(utf8_uri) end
    end
  end
end)
connect(events.APPLEEVENT_ODOC,
  function(uri) return events.emit(events.URI_DROPPED, 'file://'..uri) end)

local string_format = string.format
local EOLs = { L('CRLF'), L('CR'), L('LF') }
local GETLEXERLANGUAGE = _SCINTILLA.functions.get_lexer_language[1]
-- Sets docstatusbar text.
connect(events.UPDATE_UI, function()
  local buffer = buffer
  local pos = buffer.current_pos
  local line, max = buffer:line_from_position(pos) + 1, buffer.line_count
  local col = buffer.column[pos] + 1
  local lexer = buffer:private_lexer_call(GETLEXERLANGUAGE)
  local eol = EOLs[buffer.eol_mode + 1]
  local tabs = string_format('%s %d', buffer.use_tabs and L('Tabs:') or
                             L('Spaces:'), buffer.indent)
  local enc = buffer.encoding or ''
  gui.docstatusbar_text =
    string_format('%s %d/%d    %s %d    %s    %s    %s    %s', L('Line:'), line,
                  max, L('Col:'), col, lexer, eol, tabs, enc)
end)

-- Toggles folding.
connect(events.MARGIN_CLICK, function(margin, pos, modifiers)
  if margin == 2 then buffer:toggle_fold(buffer:line_from_position(pos)) end
end)

connect(events.BUFFER_NEW, function() set_title(buffer) end)

-- Save buffer properties.
connect(events.BUFFER_BEFORE_SWITCH, function()
  local buffer = buffer
  -- Save view state.
  buffer._anchor, buffer._current_pos = buffer.anchor, buffer.current_pos
  buffer._first_visible_line = buffer.first_visible_line
  -- Save fold state.
  buffer._folds = {}
  local folds = buffer._folds
  local i = buffer:contracted_fold_next(0)
  while i >= 0 do
    folds[#folds + 1] = i
    i = buffer:contracted_fold_next(i + 1)
  end
end)

-- Restore buffer properties.
connect(events.BUFFER_AFTER_SWITCH, function()
  local buffer = buffer
  if not buffer._folds then return end
  -- Restore fold state.
  for _, i in ipairs(buffer._folds) do buffer:toggle_fold(i) end
  -- Restore view state.
  buffer:set_sel(buffer._anchor, buffer._current_pos)
  buffer:line_scroll(0,
                     buffer:visible_from_doc_line(buffer._first_visible_line) -
                     buffer.first_visible_line)
end)

-- Updates titlebar and statusbar.
connect(events.BUFFER_AFTER_SWITCH, function()
  set_title(buffer)
  events.emit(events.UPDATE_UI)
end)

-- Updates titlebar and statusbar.
connect(events.VIEW_AFTER_SWITCH, function()
  set_title(buffer)
  events.emit(events.UPDATE_UI)
end)

connect(events.RESET_AFTER, function() gui.statusbar_text = 'Lua reset' end)

-- Prompts for confirmation if any buffers are dirty.
connect(events.QUIT, function()
  local list = {}
  for _, buffer in ipairs(_BUFFERS) do
    if buffer.dirty then
      list[#list + 1] = buffer.filename or buffer._type or L('Untitled')
    end
  end
  if #list > 0 and gui.dialog('msgbox',
                              '--title', L('Quit without saving?'),
                              '--text', L('The following buffers are unsaved:'),
                              '--informative-text', table.concat(list, '\n'),
                              '--button1', 'gtk-cancel',
                              '--button2', L('Quit _without saving'),
                              '--no-newline') ~= '2' then
    return false
  end
  return true
end)

connect(events.ERROR, function(...) gui._print(L('[Error Buffer]'), ...) end)

-- The functions below are Lua C functions.

---
-- Displays a gcocoadialog of a specified type with the given string arguments.
-- Each argument is like a string in Lua's `arg` table. Tables of strings are
-- allowed as arguments and are expanded in place. This is useful for
-- filteredlist dialogs with many items.
-- @return string gcocoadialog result.
-- @class function
-- @name dialog
local dialog

---
-- Gets the current split view structure.
-- @return table of split views. Each split view entry is a table with 4
--   fields: `1`, `2`, `vertical`, and `size`. `1` and `2` have values of either
--   split view entries or the index of the buffer shown in each view;
--   `vertical` is a flag indicating if the split is vertical or not; and
--   `size` is the integer position of the split resizer.
-- @class function
-- @name get_split_table
local get_split_table

---
-- Goes to the specified view.
-- Generates `VIEW_BEFORE_SWITCH` and `VIEW_AFTER_SWITCH` events.
-- @param n A relative or absolute view index.
-- @param relative Flag indicating if n is a relative index or not. Defaults to
--   false.
-- @class function
-- @name goto_view
local goto_view

---
-- Creates a GTK menu, returning the userdata.
-- @param menu_table A table defining the menu. It is an ordered list of tables
--   with a string menu item, integer menu ID, and optional keycode and modifier
--   mask. The latter two are used to display key shortcuts in the menu. The
--   string menu item is handled as follows:
--     `'gtk-*'` - a stock menu item is created based on the GTK stock-id.
--     `'separator'` - a menu separator item is created.
--     Otherwise a regular menu item with a mnemonic is created.
--   Submenus are just nested menu-structure tables. Their title text is defined
--   with a `title` key.
-- @see keys.get_gdk_key
-- @class function
-- @name gtkmenu
local gtkmenu