diff options
-rw-r--r-- | core/events.lua | 41 | ||||
-rw-r--r-- | core/ext/find.lua | 20 | ||||
-rw-r--r-- | core/ext/key_commands.lua | 41 | ||||
-rw-r--r-- | core/ext/key_commands_mac.lua | 41 | ||||
-rw-r--r-- | core/ext/key_commands_std.lua | 43 | ||||
-rw-r--r-- | core/ext/keys.lua | 21 | ||||
-rw-r--r-- | core/ext/menu.lua | 9 | ||||
-rw-r--r-- | core/ext/mime_types.lua | 2 | ||||
-rw-r--r-- | core/ext/pm.lua | 8 | ||||
-rw-r--r-- | core/ext/pm/buffer_browser.lua | 13 | ||||
-rw-r--r-- | core/ext/pm/ctags_browser.lua | 12 | ||||
-rw-r--r-- | core/ext/pm/file_browser.lua | 15 | ||||
-rw-r--r-- | core/ext/pm/find_browser.lua | 3 | ||||
-rw-r--r-- | core/ext/pm/macro_browser.lua | 4 | ||||
-rw-r--r-- | core/ext/pm/modules_browser.lua | 28 | ||||
-rw-r--r-- | core/ext/pm/project_browser.lua | 142 | ||||
-rw-r--r-- | core/file_io.lua | 67 | ||||
-rw-r--r-- | core/init.lua | 4 | ||||
-rw-r--r-- | core/locale.lua | 4 | ||||
-rw-r--r-- | modules/textadept/editing.lua | 65 | ||||
-rw-r--r-- | modules/textadept/lsnippets.lua | 124 | ||||
-rw-r--r-- | modules/textadept/macros.lua | 50 | ||||
-rw-r--r-- | modules/textadept/mlines.lua | 4 | ||||
-rw-r--r-- | modules/textadept/snippets.lua | 121 | ||||
-rw-r--r-- | src/lua_interface.c | 484 | ||||
-rw-r--r-- | src/textadept.c | 85 | ||||
-rw-r--r-- | src/textadept.h | 11 |
27 files changed, 833 insertions, 629 deletions
diff --git a/core/events.lua b/core/events.lua index f8c94cc4..afb119e0 100644 --- a/core/events.lua +++ b/core/events.lua @@ -97,7 +97,7 @@ function handle(event, ...) local handlers = events[plural] if not handlers then return end for _, f in ipairs(handlers) do - local result = f( unpack{...} ) + local result = f(unpack{...}) if result == true or result == false then return result end end end @@ -138,12 +138,12 @@ function keypress(code, shift, control, alt) end function menu_clicked(menu_item) local text, menu_id = menu_item:match('^(.+)|(%d+)$') - return handle( 'menu_clicked', text, tonumber(menu_id) ) + return handle('menu_clicked', text, tonumber(menu_id)) end -- Scintilla notifications. function char_added(n) - if n.ch < 256 then return handle( 'char_added', string.char(n.ch) ) end + if n.ch < 256 then return handle('char_added', string.char(n.ch)) end end function save_point_reached() return handle('save_point_reached') @@ -251,8 +251,8 @@ add_handler('view_new', buffer:set_sel_fore(1, 3355443) -- 0x33 | 0x33 << 8 | 0x33 << 16 buffer:set_sel_back(1, 10066329) -- 0x99 | 0x99 << 8 | 0x99 << 16 - buffer.margin_width_n[0] = 4 + 3 * -- line number margin - buffer:text_width(c.STYLE_LINENUMBER, '9') + buffer.margin_width_n[0] = -- line number margin + 4 + 3 * buffer:text_width(c.STYLE_LINENUMBER, '9') buffer.margin_width_n[1] = 0 -- marker margin invisible @@ -363,6 +363,8 @@ add_handler('char_added', end end) +local title_text = '%s %s Textadept (%s)' + --- -- [Local function] Sets the title of the Textadept window to the buffer's -- filename. @@ -370,8 +372,9 @@ add_handler('char_added', local function set_title(buffer) local buffer, textadept = buffer, textadept local filename = buffer.filename or textadept.locale.UNTITLED - local d = buffer.dirty and ' * ' or ' - ' - textadept.title = filename:match('[^/\\]+$')..d..'Textadept ('..filename..')' + local d = buffer.dirty and '*' or '-' + textadept.title = + string.format(title_text, filename:match('[^/\\]+$'), d, filename) end add_handler('save_point_reached', @@ -401,8 +404,10 @@ add_handler('save_point_left', -- @class table -- @name _error_details local _error_details = { - lua = { pattern = '^lua: ([^:]+):(%d+): (.+)$', - filename = 1, line = 2, message = 3 } + lua = { + pattern = '^lua: ([^:]+):(%d+): (.+)$', + filename = 1, line = 2, message = 3 + } } add_handler('double_click', @@ -412,8 +417,8 @@ add_handler('double_click', for _, error_detail in pairs(_error_details) do local captures = { line:match(error_detail.pattern) } if #captures > 0 then - textadept.io.open( captures[error_detail.filename] ) - _m.textadept.editing.goto_line( captures[error_detail.line] ) + textadept.io.open(captures[error_detail.filename]) + _m.textadept.editing.goto_line(captures[error_detail.line]) local msg = captures[error_detail.message] if msg then buffer:call_tip_show(buffer.current_pos, msg) end break @@ -450,8 +455,8 @@ local _braces = { -- () [] {} <> -- @param current_pos The position to match braces at. local function match_brace(current_pos) local buffer = buffer - if _braces[ buffer.char_at[current_pos] ] and - buffer:get_style_name( buffer.style_at[current_pos] ) == 'operator' then + if _braces[buffer.char_at[current_pos]] and + buffer:get_style_name(buffer.style_at[current_pos]) == 'operator' then local pos = buffer:brace_match(current_pos) if pos ~= -1 then buffer:brace_highlight(current_pos, pos) @@ -530,13 +535,13 @@ add_handler('quit', end end if any then - if cocoa_dialog( 'yesno-msgbox', { + if cocoa_dialog('yesno-msgbox', { title = locale.EVENTS_QUIT_TITLE, text = locale.EVENTS_QUIT_TEXT, ['informative-text'] = - string.format( locale.EVENTS_QUIT_MSG, table.concat(list, '\n') ), + string.format(locale.EVENTS_QUIT_MSG, table.concat(list, '\n')), ['no-newline'] = true - } ) ~= '2' then return false end + }) ~= '2' then return false end end textadept.io.save_session() return true @@ -554,7 +559,7 @@ end function error(...) local function handle_error(...) local textadept = textadept - local error_message = table.concat( {...} , '\n' ) + local error_message = table.concat({...} , '\n') local error_buffer for index, buffer in ipairs(textadept.buffers) do if buffer.shows_errors then @@ -572,7 +577,7 @@ function error(...) error_buffer:append_text(error_message..'\n') error_buffer:set_save_point() end - pcall( handle_error, unpack{...} ) -- prevent endless loops if this errors + pcall(handle_error, unpack{...}) -- prevent endless loops if this errors end textadept.print = error diff --git a/core/ext/find.lua b/core/ext/find.lua index 94371689..5def64e8 100644 --- a/core/ext/find.lua +++ b/core/ext/find.lua @@ -42,7 +42,7 @@ function find.find(text, next, flags, nowrap, wrapped) if find.lua then flags = flags + 8 end end if flags < 8 then - buffer:goto_pos( buffer[next and 'current_pos' or 'anchor'] + increment ) + buffer:goto_pos(buffer[next and 'current_pos' or 'anchor'] + increment) buffer:search_anchor() if next then result = buffer:search_next(flags, text) @@ -101,23 +101,23 @@ function find.replace(rtext) rtext = rtext:gsub('%%'..i, v) end end - local ret, rtext = pcall( rtext.gsub, rtext, '%%(%b())', + local ret, rtext = pcall(rtext.gsub, rtext, '%%(%b())', function(code) local locale = textadept.locale - local ret, val = pcall( loadstring('return '..code) ) + local ret, val = pcall(loadstring('return '..code)) if not ret then - cocoa_dialog( 'msgbox', { + cocoa_dialog('msgbox', { title = locale.FIND_ERROR_DIALOG_TITLE, text = locale.FIND_ERROR_DIALOG_TEXT, ['informative-text'] = val:gsub('"', '\\"') - } ) + }) error() end return val - end ) + end) if ret then rtext = rtext:gsub('\\037', '%%') -- unescape '%' - buffer:replace_target( rtext:gsub('\\[abfnrtv\\]', escapes) ) + buffer:replace_target(rtext:gsub('\\[abfnrtv\\]', escapes)) buffer:goto_pos(buffer.target_end + 1) -- 'find' text after this replacement else -- Since find is called after replace returns, have it 'find' the current @@ -138,10 +138,10 @@ function find.replace_all(ftext, rtext, flags) local textadept = textadept buffer:goto_pos(0) local count = 0 - while( find.find(ftext, true, flags, true) ) do + while(find.find(ftext, true, flags, true)) do find.replace(rtext) count = count + 1 end - textadept.statusbar_text = tostring(count)..' '.. - textadept.locale.FIND_REPLACEMENTS_MADE + textadept.statusbar_text = + string.format(textadept.locale.FIND_REPLACEMENTS_MADE, tostring(count)) end diff --git a/core/ext/key_commands.lua b/core/ext/key_commands.lua index 8985a1fa..6e3f0145 100644 --- a/core/ext/key_commands.lua +++ b/core/ext/key_commands.lua @@ -45,18 +45,22 @@ keys.cs = { t.find.focus } -- find/replace -- Recent files. local RECENT_FILES = 1 t.events.add_handler('user_list_selection', - function(type, text) if type == RECENT_FILES then t.io.open(text) end end) -keys.ar = { function() - local buffer = buffer - local list = '' - local sep = buffer.auto_c_separator - buffer.auto_c_separator = ('|'):byte() - for _, filename in ipairs(t.io.recent_files) do - list = filename..'|'..list + function(type, text) + if type == RECENT_FILES then t.io.open(text) end + end) +keys.ar = { + function() + local buffer = buffer + local files = {} + for _, filename in ipairs(t.io.recent_files) do + table.insert(files, 1, filename) + end + local sep = buffer.auto_c_separator + buffer.auto_c_separator = ('|'):byte() + buffer:user_list_show(RECENT_FILES, table.concat(files, '|')) + buffer.auto_c_separator = sep end - buffer:user_list_show( RECENT_FILES, list:sub(1, -2) ) - buffer.auto_c_separator = sep -end } +} -- Buffer/view commands. keys.an = { 'goto_buffer', v, 1, false } @@ -185,7 +189,10 @@ keys.cam = { m_macro.toggle_record } keys.csam = { m_macro.play } -- Project manager commands. -local function pm_activate(text) t.pm.entry_text = text t.pm.activate() end +local function pm_activate(text) + t.pm.entry_text = text + t.pm.activate() +end keys['c\t'] = { t.pm.focus } keys.ct.b = { pm_activate, 'buffers' } keys.ct.c = { pm_activate, 'ctags' } @@ -213,8 +220,10 @@ keys.cc = { t.command_entry.focus } local m_events = t.events keys.cab = { m_events.handle, 'call_tip_click', 1 } keys.caf = { m_events.handle, 'call_tip_click', 2 } -keys.ct.f = { function() - local buffer = buffer - buffer:toggle_fold( buffer:line_from_position(buffer.current_pos) ) -end } +keys.ct.f = { + function() + local buffer = buffer + buffer:toggle_fold(buffer:line_from_position(buffer.current_pos)) + end +} keys.f5 = { 'colourise', b, 0, -1 } diff --git a/core/ext/key_commands_mac.lua b/core/ext/key_commands_mac.lua index d4e0ed31..11378804 100644 --- a/core/ext/key_commands_mac.lua +++ b/core/ext/key_commands_mac.lua @@ -153,12 +153,17 @@ keys.cv = { } -- Project Manager -local function pm_activate(text) t.pm.entry_text = text t.pm.activate() end +local function pm_activate(text) + t.pm.entry_text = text + t.pm.activate() +end keys.sap = { function() if t.pm.width > 0 then t.pm.toggle_visible() end end } -keys.ap = { function() - if t.pm.width == 0 then t.pm.toggle_visible() end - t.pm.focus() -end } +keys.ap = { + function() + if t.pm.width == 0 then t.pm.toggle_visible() end + t.pm.focus() + end +} keys.cap = { c = { pm_activate, 'ctags' }, b = { pm_activate, 'buffers' }, @@ -171,18 +176,22 @@ keys.cap = { -- Recent files. local RECENT_FILES = 1 t.events.add_handler('user_list_selection', - function(type, text) if type == RECENT_FILES then t.io.open(text) end end) -keys.co = { function() - local buffer = buffer - local list = '' - local sep = buffer.auto_c_separator - buffer.auto_c_separator = ('|'):byte() - for _, filename in ipairs(t.io.recent_files) do - list = filename..'|'..list + function(type, text) + if type == RECENT_FILES then t.io.open(text) end + end) +keys.co = { + function() + local buffer = buffer + local files = {} + for _, filename in ipairs(t.io.recent_files) do + table.insert(files, 1, filename) + end + local sep = buffer.auto_c_separator + buffer.auto_c_separator = ('|'):byte() + buffer:user_list_show(RECENT_FILES, table.concat(files, '|')) + buffer.auto_c_separator = sep end - buffer:user_list_show( RECENT_FILES, list:sub(1, -2) ) - buffer.auto_c_separator = sep -end } +} -- Movement/selection commands keys.cf = { 'char_right', b } diff --git a/core/ext/key_commands_std.lua b/core/ext/key_commands_std.lua index 0e52ac98..499305f6 100644 --- a/core/ext/key_commands_std.lua +++ b/core/ext/key_commands_std.lua @@ -27,7 +27,7 @@ keys.clear_sequence = 'esc' local b, v = 'buffer', 'view' local t = textadept -keys.ct = {} -- Textadept command chain +keys.ct = {} -- Textadept command chain -- File keys.cn = { t.new_buffer } @@ -153,12 +153,17 @@ keys.cav = { } -- Project Manager -local function pm_activate(text) t.pm.entry_text = text t.pm.activate() end +local function pm_activate(text) + t.pm.entry_text = text + t.pm.activate() +end keys.csp = { function() if t.pm.width > 0 then t.pm.toggle_visible() end end } -keys.cp = { function() - if t.pm.width == 0 then t.pm.toggle_visible() end - t.pm.focus() -end } +keys.cp = { + function() + if t.pm.width == 0 then t.pm.toggle_visible() end + t.pm.focus() + end +} keys.cap = { c = { pm_activate, 'ctags' }, b = { pm_activate, 'buffers' }, @@ -171,15 +176,19 @@ keys.cap = { -- Recent files. local RECENT_FILES = 1 t.events.add_handler('user_list_selection', - function(type, text) if type == RECENT_FILES then t.io.open(text) end end) -keys.ao = { function() - local buffer = buffer - local list = '' - local sep = buffer.auto_c_separator - buffer.auto_c_separator = ('|'):byte() - for _, filename in ipairs(t.io.recent_files) do - list = filename..'|'..list + function(type, text) + if type == RECENT_FILES then t.io.open(text) end + end) +keys.ao = { + function() + local buffer = buffer + local files = {} + for _, filename in ipairs(t.io.recent_files) do + table.insert(files, 1, filename) + end + local sep = buffer.auto_c_separator + buffer.auto_c_separator = ('|'):byte() + buffer:user_list_show(RECENT_FILES, table.concat(files, '|')) + buffer.auto_c_separator = sep end - buffer:user_list_show( RECENT_FILES, list:sub(1, -2) ) - buffer.auto_c_separator = sep -end } +} diff --git a/core/ext/keys.lua b/core/ext/keys.lua index b9c17ab3..818479a9 100644 --- a/core/ext/keys.lua +++ b/core/ext/keys.lua @@ -111,7 +111,10 @@ local try_get_cmd1, try_get_cmd2, try_get_cmd3, try_get_cmd --- -- Clears the current key sequence. -function clear_key_sequence() keychain = {} textadept.statusbar_text = '' end +function clear_key_sequence() + keychain = {} + textadept.statusbar_text = '' +end --- -- [Local function] Handles Textadept keypresses. @@ -170,10 +173,12 @@ local function keypress(code, shift, control, alt) if ret then clear_key_sequence() if type(func) == 'function' then - local ret, retval = pcall( func, unpack(args) ) + local ret, retval = pcall(func, unpack(args)) if ret then if type(retval) == 'boolean' then return retval end - else textadept.events.error(retval) end -- error + else + textadept.events.error(retval) + end end return true else @@ -186,7 +191,9 @@ local function keypress(code, shift, control, alt) textadept.statusbar_text = textadept.locale.KEYS_INVALID return true end - else return true end + else + return true + end end end textadept.events.add_handler('keypress', keypress, 1) @@ -195,13 +202,13 @@ textadept.events.add_handler('keypress', keypress, 1) -- [Local function] Tries to get a key command based on the lexer and current -- scope. try_get_cmd1 = function(keys, lexer, scope) - return try_get_cmd( keys[lexer][scope] ) + return try_get_cmd(keys[lexer][scope]) end --- -- [Local function] Tries to get a key command based on the lexer. try_get_cmd2 = function(keys, lexer) - return try_get_cmd( keys[lexer] ) + return try_get_cmd(keys[lexer]) end --- @@ -234,7 +241,7 @@ try_get_cmd = function(active_table) return view[func], { view, unpack(active_table, 3) } end else - error( locale.KEYS_UNKNOWN_COMMAND..tostring(func) ) + error(locale.KEYS_UNKNOWN_COMMAND..tostring(func)) end end end diff --git a/core/ext/menu.lua b/core/ext/menu.lua index 44a70e2d..e7f57d5a 100644 --- a/core/ext/menu.lua +++ b/core/ext/menu.lua @@ -415,7 +415,10 @@ local m_mlines = _m.textadept.mlines local m_bookmarks = _m.textadept.bookmarks local m_macros = _m.textadept.macros -local function pm_activate(text) t.pm.entry_text = text t.pm.activate() end +local function pm_activate(text) + t.pm.entry_text = text + t.pm.activate() +end local function toggle_setting(setting) local state = buffer[setting] if type(state) == 'boolean' then @@ -579,10 +582,10 @@ t.events.add_handler('menu_clicked', end end if f and args then - local ret, retval = pcall( f, unpack(args) ) + local ret, retval = pcall(f, unpack(args)) if not ret then textadept.events.error(retval) end -- error else - error( l.MENU_UNKNOWN_COMMAND..tostring(func) ) + error(l.MENU_UNKNOWN_COMMAND..tostring(func)) end end end) diff --git a/core/ext/mime_types.lua b/core/ext/mime_types.lua index 2ad0a3ed..6d4ec37a 100644 --- a/core/ext/mime_types.lua +++ b/core/ext/mime_types.lua @@ -276,7 +276,7 @@ local function set_lexer_from_sh_bang() line = line:gsub('[\\/]', ' ') for word in line:gmatch('%S+') do if shebangs[word] then - buffer:set_lexer_language( shebangs[word] ) + buffer:set_lexer_language(shebangs[word]) return true end end diff --git a/core/ext/pm.lua b/core/ext/pm.lua index fa5d21d0..8122c419 100644 --- a/core/ext/pm.lua +++ b/core/ext/pm.lua @@ -66,7 +66,7 @@ local pm = textadept.pm -- are expanded, this function is called again to get that level of data. function pm.get_contents_for(full_path, expanding) for _, browser in pairs(pm.browsers) do - if browser.matches( full_path[1] ) then + if browser.matches(full_path[1]) then return browser.get_contents_for(full_path, expanding) end end @@ -79,7 +79,7 @@ end -- @see pm.get_contents_for function pm.perform_action(selected_item) for _, browser in pairs(pm.browsers) do - if browser.matches( selected_item[1] ) then + if browser.matches(selected_item[1]) then return browser.perform_action(selected_item) end end @@ -98,7 +98,7 @@ end -- @see pm.get_contents_for function pm.get_context_menu(selected_item) for _, browser in pairs(pm.browsers) do - if browser.matches( selected_item[1] ) then + if browser.matches(selected_item[1]) then return browser.get_context_menu(selected_item) end end @@ -113,7 +113,7 @@ end -- @see pm.get_contents_for function pm.perform_menu_action(menu_item, menu_id, selected_item) for _, browser in pairs(pm.browsers) do - if browser.matches( selected_item[1] ) then + if browser.matches(selected_item[1]) then return browser.perform_menu_action(menu_item, menu_id, selected_item) end end diff --git a/core/ext/pm/buffer_browser.lua b/core/ext/pm/buffer_browser.lua index f4822226..73b09d55 100644 --- a/core/ext/pm/buffer_browser.lua +++ b/core/ext/pm/buffer_browser.lua @@ -23,8 +23,11 @@ end function perform_action(selected_item) local index = selected_item[2] - local buffer = textadept.buffers[ tonumber(index) ] - if buffer then view:goto_buffer(index) view:focus() end + local buffer = textadept.buffers[tonumber(index)] + if buffer then + view:goto_buffer(index) + view:focus() + end end local ID = { NEW = 1, OPEN = 2, SAVE = 3, SAVEAS = 4, CLOSE = 5 } @@ -47,13 +50,13 @@ function perform_menu_action(menu_item, menu_id, selected_item) elseif menu_id == ID.OPEN then textadept.io.open() elseif menu_id == ID.SAVE then - view:goto_buffer( tonumber( selected_item[2] ) ) + view:goto_buffer(tonumber(selected_item[2])) buffer:save() elseif menu_id == ID.SAVEAS then - view:goto_buffer( tonumber( selected_item[2] ) ) + view:goto_buffer(tonumber(selected_item[2])) buffer:save_as() elseif menu_id == ID.CLOSE then - view:goto_buffer( tonumber( selected_item[2] ) ) + view:goto_buffer(tonumber(selected_item[2])) buffer:close() end textadept.pm.activate() diff --git a/core/ext/pm/ctags_browser.lua b/core/ext/pm/ctags_browser.lua index 196eba5c..e1b4586d 100644 --- a/core/ext/pm/ctags_browser.lua +++ b/core/ext/pm/ctags_browser.lua @@ -112,7 +112,7 @@ function get_contents_for(full_path, expanding) tags = {} current_file = nil current_dir = '' -- ctags file will specify absolute paths - os.execute( 'ctags -f '..FILE_OUT..' '..(buffer.filename or '') ) + os.execute('ctags -f '..FILE_OUT..' '..(buffer.filename or '')) f = io.open(FILE_OUT) if not f then return {} end elseif not expanding then @@ -182,16 +182,16 @@ function get_contents_for(full_path, expanding) end entry.text = name -- The following keys are ignored by caller. - entry.filepath = filepath:sub(1, 1) == '/' and - filepath or current_dir..filepath + entry.filepath = + filepath:sub(1, 1) == '/' and filepath or current_dir..filepath entry.pattern = pattern entry.line_num = line_num if not entry.set then tags[name] = entry end else - print( string.format(locale.PM_BROWSER_CTAGS_BAD_EXT, file_ext) ) + print(string.format(locale.PM_BROWSER_CTAGS_BAD_EXT, file_ext)) end else - print( string.format(locale.PM_BROWSER_CTAGS_UNMATCHED, line) ) + print(string.format(locale.PM_BROWSER_CTAGS_UNMATCHED, line)) end end end @@ -217,7 +217,7 @@ function perform_action(selected_item) buffer:goto_line(line) else error( - string.format(textadept.locale.PM_BROWSER_CTAGS_NOT_FOUND, item.text) ) + string.format(textadept.locale.PM_BROWSER_CTAGS_NOT_FOUND, item.text)) end elseif item.line_num then textadept.io.open(item.filepath) diff --git a/core/ext/pm/file_browser.lua b/core/ext/pm/file_browser.lua index 17ae3be1..c658b6c0 100644 --- a/core/ext/pm/file_browser.lua +++ b/core/ext/pm/file_browser.lua @@ -58,17 +58,18 @@ function perform_menu_action(menu_item, menu_id, selected_item) elseif menu_id == ID.FILE_INFO then local date_format = '%D %T' local attr = lfs.attributes(filepath) - local out = string.format( locale.PM_BROWSER_FILE_DATA, - attr.mode, attr.size, attr.uid, attr.gid, attr.dev, - os.date(date_format, attr.access), - os.date(date_format, attr.modification), - os.date(date_format, attr.change) ) - cocoa_dialog( 'textbox', { + local out = + string.format(locale.PM_BROWSER_FILE_DATA, + attr.mode, attr.size, attr.uid, attr.gid, attr.dev, + os.date(date_format, attr.access), + os.date(date_format, attr.modification), + os.date(date_format, attr.change)) + cocoa_dialog('textbox', { ['informative-text'] = string.format(locale.PM_BROWSER_FILE_INFO_TEXT, filepath), text = out, button1 = locale.PM_BROWSER_FILE_INFO_OK, editable = false - } ) + }) end end diff --git a/core/ext/pm/find_browser.lua b/core/ext/pm/find_browser.lua index f722664e..a8722e78 100644 --- a/core/ext/pm/find_browser.lua +++ b/core/ext/pm/find_browser.lua @@ -59,7 +59,8 @@ function get_contents_for(full_path, expanding) p = io.popen('grep -'..opts..' "'..search_string..'" "'.. search_directory..'"') for line in p:lines() do - local filename, line_num, line_text = line:match('^([^:]+):(%d+):%s-(.+)$') + local filename, line_num, line_text = + line:match('^([^:]+):(%d+):%s-(.+)$') if filename and line_num and line_text then if not find_matches[filename] then find_matches[filename] = { diff --git a/core/ext/pm/macro_browser.lua b/core/ext/pm/macro_browser.lua index e1394e5c..c2892fa7 100644 --- a/core/ext/pm/macro_browser.lua +++ b/core/ext/pm/macro_browser.lua @@ -19,7 +19,7 @@ function get_contents_for() end function perform_action(selected_item) - _m.textadept.macros.play( selected_item[2] ) + _m.textadept.macros.play(selected_item[2]) view:focus() end @@ -33,7 +33,7 @@ end function perform_menu_action(menu_item, menu_id, selected_item) local m_macros = _m.textadept.macros if menu_id == ID.DELETE then - m_macros.delete( selected_item[2] ) + m_macros.delete(selected_item[2]) end textadept.pm.activate() end diff --git a/core/ext/pm/modules_browser.lua b/core/ext/pm/modules_browser.lua index 95a5bd9f..4fc97206 100644 --- a/core/ext/pm/modules_browser.lua +++ b/core/ext/pm/modules_browser.lua @@ -123,15 +123,17 @@ end function perform_menu_action(menu_item, menu_id, selected_item) local locale = textadept.locale if menu_id == ID.NEW then - local status, module_name = cocoa_dialog( 'standard-inputbox', { - ['title'] = locale.PM_BROWSER_MODULE_NEW_TITLE, - ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_INFO_TEXT - } ):match('^(%d)%s+([^\n]+)%s+$') + local status, module_name = + cocoa_dialog('standard-inputbox', { + ['title'] = locale.PM_BROWSER_MODULE_NEW_TITLE, + ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_INFO_TEXT + }):match('^(%d)%s+([^\n]+)%s+$') if status ~= '1' then return end - local status, lang_name = cocoa_dialog( 'standard-inputbox', { - ['title'] = locale.PM_BROWSER_MODULE_NEW_LANG_TITLE, - ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_LANG_INFO_TEXT - } ):match('^(%d)%s+([^\n]+)%s+$') + local status, lang_name = + cocoa_dialog('standard-inputbox', { + ['title'] = locale.PM_BROWSER_MODULE_NEW_LANG_TITLE, + ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_LANG_INFO_TEXT + }):match('^(%d)%s+([^\n]+)%s+$') if status ~= '1' then return end local module_dir = _HOME..'/modules/'..module_name if lfs.mkdir(module_dir) then @@ -151,22 +153,22 @@ function perform_menu_action(menu_item, menu_id, selected_item) f:write(out) f:close() else - cocoa_dialog( 'ok-msgbox', { + cocoa_dialog('ok-msgbox', { ['text'] = locale.PM_BROWSER_MODULE_NEW_ERROR, ['informative-text'] = locale.PM_BROWSER_MODULE_NEW_ERROR_TEXT, ['no-cancel'] = true - } ) + }) return end elseif menu_id == ID.DELETE then local module_name = selected_item[2] - if cocoa_dialog( 'yesno-msgbox', { + if cocoa_dialog('yesno-msgbox', { ['text'] = locale.PM_BROWSER_MODULE_DELETE_TITLE, ['informative-text'] = - string.format( locale.PM_BROWSER_MODULE_DELETE_TEXT, module_name ), + string.format(locale.PM_BROWSER_MODULE_DELETE_TEXT, module_name), ['no-cancel'] = true, ['no-newline'] = true - } ) == '1' then + }) == '1' then local function remove_directory(dirpath) for name in lfs.dir(dirpath) do if not name:match('^%.%.?$') then os.remove(dirpath..'/'..name) end diff --git a/core/ext/pm/project_browser.lua b/core/ext/pm/project_browser.lua index 2dab8deb..30c6a847 100644 --- a/core/ext/pm/project_browser.lua +++ b/core/ext/pm/project_browser.lua @@ -69,7 +69,7 @@ end --- Opens the selected project file. function perform_action(selected_item) - textadept.io.open( selected_item[#selected_item] ) + textadept.io.open(selected_item[#selected_item]) view:focus() end @@ -101,11 +101,12 @@ function perform_menu_action(menu_item, menu_id, selected_item) if menu_id == ID.NEW then -- Close all open files and prompt the user to save a project file. if textadept.io.close_all() then - local file = cocoa_dialog( 'filesave', { - title = locale.PM_BROWSER_PROJECT_NEW_TITLE, - ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), - ['no-newline'] = true - } ) + local file = + cocoa_dialog('filesave', { + title = locale.PM_BROWSER_PROJECT_NEW_TITLE, + ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), + ['no-newline'] = true + }) if #file > 0 then project_file = file project_root = {} @@ -116,11 +117,12 @@ function perform_menu_action(menu_item, menu_id, selected_item) elseif menu_id == ID.OPEN then -- Close all open files and prompt the user for a project file to open. if textadept.io.close_all() then - local file = cocoa_dialog( 'fileselect', { - title = locale.PM_BROWSER_PROJECT_OPEN_TITLE, - ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), - ['no-newline'] = true - } ) + local file = + cocoa_dialog('fileselect', { + title = locale.PM_BROWSER_PROJECT_OPEN_TITLE, + ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), + ['no-newline'] = true + }) if #file > 0 then load_project(file) refresh_view() @@ -139,11 +141,12 @@ function perform_menu_action(menu_item, menu_id, selected_item) -- If a project is open, prompts the user to save the new file. if project_file then local dir = get_live_parent_folder(selected_item) - local file = cocoa_dialog( 'filesave', { - title = locale.PM_BROWSER_PROJECT_NEW_FILE_TITLE, - ['with-directory'] = dir or project_file:match('.+[/\\]'), - ['no-newline'] = true - } ) + local file = + cocoa_dialog('filesave', { + title = locale.PM_BROWSER_PROJECT_NEW_FILE_TITLE, + ['with-directory'] = dir or project_file:match('.+[/\\]'), + ['no-newline'] = true + }) if #file > 0 then local function add_file_to(pfolder) local exists = false @@ -162,17 +165,20 @@ function perform_menu_action(menu_item, menu_id, selected_item) -- caution them about unexpected behavior and ask to save in the -- project root instead. if dir and file:match('^(.+)[/\\]') ~= dir then - local ret = cocoa_dialog( 'yesno-msgbox', { - text = locale.PM_BROWSER_PROJECT_NEW_FILE_LIVE_FOLDER_TITLE, - ['informative-text'] = - locale.PM_BROWSER_PROJECT_NEW_FILE_LIVE_FOLDER_TEXT, - ['no-newline'] = true - } ) + local ret = + cocoa_dialog('yesno-msgbox', { + text = locale.PM_BROWSER_PROJECT_NEW_FILE_LIVE_FOLDER_TITLE, + ['informative-text'] = + locale.PM_BROWSER_PROJECT_NEW_FILE_LIVE_FOLDER_TEXT, + ['no-newline'] = true + }) if ret == '1' then add_file_to(project_root) end if ret == '3' then return end end end - local f = io.open(file, 'w') f:write('') f:close() + local f = io.open(file, 'w') + f:write('') + f:close() update_project() refresh_view() end @@ -186,14 +192,15 @@ function perform_menu_action(menu_item, menu_id, selected_item) -- Files are added if they do not already exist in the project. This does -- not always apply when live folders are in a project. if project_file then - local files = cocoa_dialog( 'fileselect', { - title = locale.PM_BROWSER_PROJECT_ADD_FILES_TITLE, - text = locale.PM_BROWSER_PROJECT_ADD_FILES_TEXT, - -- in Windows, dialog:get_filenames() is unavailable; only allow single - -- selection - ['select-multiple'] = not WIN32 or nil, - ['with-directory'] = (buffer.filename or project_file):match('.+[/\\]') - } ) + local files = + cocoa_dialog('fileselect', { + title = locale.PM_BROWSER_PROJECT_ADD_FILES_TITLE, + text = locale.PM_BROWSER_PROJECT_ADD_FILES_TEXT, + -- in Windows, dialog:get_filenames() is unavailable; only allow single + -- selection + ['select-multiple'] = not WIN32 or nil, + ['with-directory'] = (buffer.filename or project_file):match('.+[/\\]') + }) if #files > 0 then local function add_files_to(pfolder) for file in files:gmatch('[^\n]+') do @@ -210,12 +217,12 @@ function perform_menu_action(menu_item, menu_id, selected_item) if not project_folder.is_live_folder then add_files_to(project_folder) else - if cocoa_dialog( 'yesno-msgbox', { + if cocoa_dialog('yesno-msgbox', { text = locale.PM_BROWSER_PROJECT_ADD_FILES_LIVE_FOLDER_TITLE, ['informative-text'] = locale.PM_BROWSER_PROJECT_ADD_FILES_LIVE_FOLDER_TEXT, ['no-newline'] = true, - } ) == '1' then add_files_to(project_root) end + }) == '1' then add_files_to(project_root) end end end end @@ -227,16 +234,17 @@ function perform_menu_action(menu_item, menu_id, selected_item) -- The directory is added if it does not already exist in the project. This -- does not always apply when live folders are in a project. if project_file then - local ret, name = cocoa_dialog( 'standard-inputbox', { - ['informative-text'] = locale.PM_BROWSER_PROJECT_NEW_DIR_TITLE, - ['no-newline'] = true - } ):match('^(%d)\n([^\n]+)$') + local ret, name = + cocoa_dialog('standard-inputbox', { + ['informative-text'] = locale.PM_BROWSER_PROJECT_NEW_DIR_TITLE, + ['no-newline'] = true + }):match('^(%d)\n([^\n]+)$') if ret == '1' and name and #name > 0 then local project_folder = get_parent_folder(selected_item) if not project_folder.is_live_folder then if not project_folder[name] then project_folder[name] = {} end else - lfs.mkdir( get_live_parent_folder(selected_item)..'/'..name ) + lfs.mkdir(get_live_parent_folder(selected_item)..'/'..name) end update_project() refresh_view() @@ -251,13 +259,14 @@ function perform_menu_action(menu_item, menu_id, selected_item) -- The directory is added if it does not already exist in the project. This -- does not always apply when live folders are in a project. if project_file then - local dir = cocoa_dialog( 'fileselect', { - title = locale.PM_BROWSER_PROJECT_ADD_DIR_TITLE, - text = locale.PM_BROWSER_PROJECT_ADD_DIR_TEXT, - ['select-only-directories'] = true, - ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), - ['no-newline'] = true - } ) + local dir = + cocoa_dialog('fileselect', { + title = locale.PM_BROWSER_PROJECT_ADD_DIR_TITLE, + text = locale.PM_BROWSER_PROJECT_ADD_DIR_TEXT, + ['select-only-directories'] = true, + ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), + ['no-newline'] = true + }) if #dir > 0 then local function add_directory_to(pfolder) if not pfolder[dir] then @@ -270,12 +279,12 @@ function perform_menu_action(menu_item, menu_id, selected_item) if not project_folder.is_live_folder then add_directory_to(project_folder) else - if cocoa_dialog( 'yesno-msgbox', { + if cocoa_dialog('yesno-msgbox', { text = locale.PM_BROWSER_PROJECT_ADD_DIR_LIVE_FOLDER_TITLE, ['informative-text'] = locale.PM_BROWSER_PROJECT_ADD_DIR_LIVE_FOLDER_TEXT, ['no-newline'] = true, - } ) == '1' then add_directory_to(project_root) end + }) == '1' then add_directory_to(project_root) end end end end @@ -294,12 +303,13 @@ function perform_menu_action(menu_item, menu_id, selected_item) else -- file for i, file in ipairs(project_folder) do if file == item then - local ret = cocoa_dialog( 'yesno-msgbox', { - text = locale.PM_BROWSER_PROJECT_DELETE_FILE_TITLE, - ['informative-text'] = - locale.PM_BROWSER_PROJECT_DELETE_FILE_TEXT, - ['no-newline'] = true - } ) + local ret = + cocoa_dialog('yesno-msgbox', { + text = locale.PM_BROWSER_PROJECT_DELETE_FILE_TITLE, + ['informative-text'] = + locale.PM_BROWSER_PROJECT_DELETE_FILE_TEXT, + ['no-newline'] = true + }) if ret == '2' then os.remove(file) end if ret == '3' then return end table.remove(project_folder, i) @@ -321,16 +331,17 @@ function perform_menu_action(menu_item, menu_id, selected_item) table.remove(selected_item, #selected_item) local parent_folder = get_parent_folder(selected_item) if item_is_dir and not parent_folder.is_live_folder then - local ret = cocoa_dialog( 'yesno-msgbox', { - text = locale.PM_BROWSER_PROJECT_DELETE_DIR_TITLE, - ['informative-text'] = locale.PM_BROWSER_PROJECT_DELETE_DIR_TEXT, - ['no-newline'] = true - } ) + local ret = + cocoa_dialog('yesno-msgbox', { + text = locale.PM_BROWSER_PROJECT_DELETE_DIR_TITLE, + ['informative-text'] = locale.PM_BROWSER_PROJECT_DELETE_DIR_TEXT, + ['no-newline'] = true + }) if ret == '2' then remove_directory(item) end if ret == '3' then return end parent_folder[item] = nil else - if cocoa_dialog( 'msgbox', { + if cocoa_dialog('msgbox', { text = locale.PM_BROWSER_PROJECT_DELETE_LIVE_FILE_TITLE, ['informative-text'] = locale.PM_BROWSER_PROJECT_DELETE_LIVE_FILE_TEXT, @@ -338,7 +349,7 @@ function perform_menu_action(menu_item, menu_id, selected_item) button1 = locale.PM_BROWSER_PROJECT_DELETE_LIVE_FILE_BUTTON1, button2 = locale.PM_BROWSER_PROJECT_DELETE_LIVE_FILE_BUTTON2, button3 = locale.PM_BROWSER_PROJECT_DELETE_LIVE_FILE_BUTTON3 - } ) ~= '2' then return end + }) ~= '2' then return end if item_is_dir then remove_directory(item) else os.remove(item) end end end @@ -349,10 +360,11 @@ function perform_menu_action(menu_item, menu_id, selected_item) elseif menu_id == ID.RENAME_FILE then -- If a project is open, prompts the user for a new file/directory name. if project_file then - local ret, name = cocoa_dialog( 'standard-inputbox', { - ['informative-text'] = locale.PM_BROWSER_PROJECT_RENAME_FILE_TEXT, - ['no-newline'] = true - } ):match('^(%d)\n([^\n]+)$') + local ret, name = + cocoa_dialog('standard-inputbox', { + ['informative-text'] = locale.PM_BROWSER_PROJECT_RENAME_FILE_TEXT, + ['no-newline'] = true + }):match('^(%d)\n([^\n]+)$') if ret == '1' and name and #name > 0 then local oldname = selected_item[#selected_item] local newname = oldname:match('^.+[/\\]')..name @@ -457,7 +469,7 @@ get_live_parent_folder = function(selected_item) local dir = nil if get_parent_folder(selected_item).is_live_folder then if lfs.attributes( - selected_item[#selected_item], 'mode' ) == 'directory' then + selected_item[#selected_item], 'mode') == 'directory' then dir = selected_item[#selected_item] else dir = selected_item[#selected_item - 1] diff --git a/core/file_io.lua b/core/file_io.lua index 44fbe444..943bc6f0 100644 --- a/core/file_io.lua +++ b/core/file_io.lua @@ -28,7 +28,7 @@ local function open_helper(filename) local buffer = textadept.new_buffer() local f, err = io.open(filename) if f then - buffer:set_text( f:read('*all') ) + buffer:set_text(f:read('*all')) buffer:empty_undo_buffer() f:close() end @@ -45,14 +45,15 @@ end -- @usage textadept.io.open(filename) function open(filenames) local locale = textadept.locale - filenames = filenames or cocoa_dialog( 'fileselect', { - title = locale.IO_OPEN_TITLE, - text = locale.IO_OPEN_TEXT, - -- in Windows, dialog:get_filenames() is unavailable; only allow single - -- selection - ['select-multiple'] = not WIN32 or nil, - ['with-directory'] = (buffer.filename or ''):match('.+[/\\]') - } ) + filenames = + filenames or cocoa_dialog('fileselect', { + title = locale.IO_OPEN_TITLE, + text = locale.IO_OPEN_TEXT, + -- in Windows, dialog:get_filenames() is unavailable; only allow single + -- selection + ['select-multiple'] = not WIN32 or nil, + ['with-directory'] = (buffer.filename or ''):match('.+[/\\]') + }) for filename in filenames:gmatch('[^\n]+') do open_helper(filename) end end @@ -67,7 +68,7 @@ function reload(buffer) local f, err = io.open(buffer.filename) if f then local pos = buffer.current_pos - buffer:set_text( f:read('*all') ) + buffer:set_text(f:read('*all')) buffer.current_pos = pos buffer:set_save_point() f:close() @@ -103,12 +104,13 @@ end function save_as(buffer, filename) textadept.check_focused_buffer(buffer) if not filename then - filename = cocoa_dialog( 'filesave', { - title = textadept.locale.IO_SAVE_TITLE, - ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), - ['with-file'] = (buffer.filename or ''):match('[^/\\]+$'), - ['no-newline'] = true - } ) + filename = + cocoa_dialog('filesave', { + title = textadept.locale.IO_SAVE_TITLE, + ['with-directory'] = (buffer.filename or ''):match('.+[/\\]'), + ['with-file'] = (buffer.filename or ''):match('[^/\\]+$'), + ['no-newline'] = true + }) end if #filename > 0 then buffer.filename = filename @@ -141,12 +143,12 @@ end function close(buffer) local locale = textadept.locale textadept.check_focused_buffer(buffer) - if buffer.dirty and cocoa_dialog( 'yesno-msgbox', { + if buffer.dirty and cocoa_dialog('yesno-msgbox', { title = locale.IO_CLOSE_TITLE, text = locale.IO_CLOSE_TEXT, ['informative-text'] = locale.IO_CLOSE_MSG, ['no-newline'] = true - } ) ~= '2' then return false end + }) ~= '2' then return false end buffer:delete() return true end @@ -197,20 +199,19 @@ function load_session(filename, only_pm) local buffer = buffer buffer._anchor, buffer._current_pos = anchor, current_pos buffer._first_visible_line = first_visible_line - buffer:line_scroll( 0, - buffer:visible_from_doc_line(first_visible_line) ) + buffer:line_scroll(0, + buffer:visible_from_doc_line(first_visible_line)) buffer:set_sel(anchor, current_pos) elseif line:match('^%s*split%d:') then local level, num, type, size = line:match('^(%s*)split(%d): (%S+) (%d+)') - local view = - splits[#level] and splits[#level][ tonumber(num) ] or view + local view = splits[#level] and splits[#level][tonumber(num)] or view splits[#level + 1] = { view:split(type == 'true') } splits[#level + 1][1].size = tonumber(size) -- could be 1 or 2 elseif line:match('^%s*view%d:') then local level, num, buf_idx = line:match('^(%s*)view(%d): (%d+)$') - local view = splits[#level][ tonumber(num) ] or view - view:goto_buffer( tonumber(buf_idx) ) + local view = splits[#level][tonumber(num)] or view + view:goto_buffer(tonumber(buf_idx)) elseif line:match('^current_view:') then local view_idx, buf_idx = line:match('^current_view: (%d+)') current_view = tonumber(view_idx) or 1 @@ -252,11 +253,12 @@ function save_session(filename) local current = buffer.doc_pointer == textadept.focused_doc_pointer local anchor = current and 'anchor' or '_anchor' local current_pos = current and 'current_pos' or '_current_pos' - local first_visible_line = current and 'first_visible_line' or - '_first_visible_line' + local first_visible_line = + current and 'first_visible_line' or '_first_visible_line' session[#session + 1] = - buffer_line:format(buffer[anchor] or 0, buffer[current_pos] or 0, - buffer[first_visible_line] or 0, buffer.filename) + buffer_line:format(buffer[anchor] or 0, + buffer[current_pos] or 0, + buffer[first_visible_line] or 0, buffer.filename) buffer_indices[buffer.doc_pointer] = idx - offset else offset = offset + 1 -- don't save untitled files in session @@ -294,7 +296,7 @@ function save_session(filename) session[#session + 1] = ("current_view: %d"):format(current_view) -- Write out other things. local size = textadept.size - session[#session + 1] = ("size: %d %d"):format( size[1], size[2] ) + session[#session + 1] = ("size: %d %d"):format(size[1], size[2]) local pm = textadept.pm session[#session + 1] = ("pm: %d %s"):format(pm.width, pm.entry_text) -- Write the session. @@ -302,7 +304,10 @@ function save_session(filename) if not user_dir then return end local ta_session = user_dir..'/.ta_session' local f = io.open(filename or ta_session, 'w') - if f then f:write( table.concat(session, '\n') ) f:close() end + if f then + f:write(table.concat(session, '\n')) + f:close() + end end --- @@ -327,7 +332,7 @@ function read_api_file(filename, word_chars) line:match('(['..word_chars..']+)%s*(%b())(.*)$') if func and params and desc then if not api[func] then api[func] = {} end - api[func][ #api[func] + 1 ] = { params, desc } + api[func][#api[func] + 1] = { params, desc } end end f:close() diff --git a/core/init.lua b/core/init.lua index 57d06a3d..8a7b96de 100644 --- a/core/init.lua +++ b/core/init.lua @@ -35,7 +35,7 @@ end -- Displays a CocoaDialog of a specified type with given arguments returning -- the result. -- @param kind The CocoaDialog type. --- @param ... A table of key, value arguments. Each key is a --key switch with +-- @param opts A table of key, value arguments. Each key is a --key switch with -- a "value" value. If value is nil, it is omitted and just the switch is -- used. -- @return string CocoaDialog result. @@ -57,7 +57,7 @@ function cocoa_dialog(kind, opts) return lua_dialog.run(args) else local cocoa_dialog = '/CocoaDialog.app/Contents/MacOS/CocoaDialog ' - local p = io.popen( _HOME..cocoa_dialog..table.concat(args, ' ') ) + local p = io.popen(_HOME..cocoa_dialog..table.concat(args, ' ')) local out = p:read('*all') p:close() return out diff --git a/core/locale.lua b/core/locale.lua index 930f1151..ae89ae18 100644 --- a/core/locale.lua +++ b/core/locale.lua @@ -84,8 +84,8 @@ FIND_NO_RESULTS = 'No results found' FIND_ERROR_DIALOG_TITLE = 'Error' -- An error occured: FIND_ERROR_DIALOG_TEXT = 'An error occured' --- replacement(s) made -FIND_REPLACEMENTS_MADE = 'replacement(s) made' +-- "%d replacement(s) made" +FIND_REPLACEMENTS_MADE = '%d replacement(s) made' -- core/ext/keys.lua diff --git a/modules/textadept/editing.lua b/modules/textadept/editing.lua index 04e9a1a1..47166cf5 100644 --- a/modules/textadept/editing.lua +++ b/modules/textadept/editing.lua @@ -70,9 +70,7 @@ local comment_strings = { textadept.events.add_handler('char_added', function(c) -- matches characters specified in char_matches - if char_matches[c] then - buffer:insert_text( -1, char_matches[c] ) - end + if char_matches[c] then buffer:insert_text(-1, char_matches[c]) end end) -- local functions @@ -106,7 +104,7 @@ end function autocomplete_word(word_chars) local buffer = buffer local caret, length = buffer.current_pos, buffer.length - local completions, c_list_str = {}, '' + local completions, c_list = {}, {} local buffer_text = buffer:get_text(length) local root = buffer_text:sub(1, caret):match('['..word_chars..']+$') if not root or #root == 0 then return end @@ -115,12 +113,12 @@ function autocomplete_word(word_chars) local s, e = buffer_text:find('^['..word_chars..']+', match_pos + 1) local match = buffer_text:sub(s, e) if not completions[match] and #match > #root then - c_list_str = c_list_str..match..' ' + c_list[#c_list + 1] = match completions[match] = true end match_pos = buffer:find(root, 1048580, match_pos + 1) end - if #c_list_str > 0 then buffer:auto_c_show( #root, c_list_str:sub(1, -2) ) end + if #c_list > 0 then buffer:auto_c_show(#root, table.concat(c_list, ' ')) end end --- @@ -134,12 +132,12 @@ function autocomplete_word_from_dict(dict) if #root == 0 then return end local f = io.open(dict) if not f then return end - local c_list_str = '' + local c_list = {} for line in f:lines() do - if line:match('^'..root) then c_list_str = c_list_str..line..' ' end + if line:match('^'..root) then c_list[#c_list + 1] = line end end f:close() - if #c_list_str > 0 then buffer:auto_c_show( #root, c_list_str:sub(1, -2) ) end + if #c_list > 0 then buffer:auto_c_show(#root, table.concat(c_list, ' ')) end end --- @@ -205,7 +203,7 @@ textadept.events.add_handler('call_tip_click', function block_comment(comment) local buffer = buffer if not comment then - comment = comment_strings[ buffer:get_lexer_language() ] + comment = comment_strings[buffer:get_lexer_language()] if not comment then return end end local caret, anchor = buffer.current_pos, buffer.anchor @@ -236,12 +234,13 @@ end function goto_line(line) local buffer, locale = buffer, textadept.locale if not line then - line = cocoa_dialog( 'standard-inputbox', { - title = locale.M_TEXTADEPT_EDITING_GOTO_TITLE, - text = locale.M_TEXTADEPT_EDITING_GOTO_TEXT, - ['no-newline'] = true - } ) - line = tonumber( line:match('%-?%d+$') ) + line = + cocoa_dialog('standard-inputbox', { + title = locale.M_TEXTADEPT_EDITING_GOTO_TITLE, + text = locale.M_TEXTADEPT_EDITING_GOTO_TEXT, + ['no-newline'] = true + }) + line = tonumber(line:match('%-?%d+$')) if not line or line < 0 then return end end buffer:ensure_visible_enforce_policy(line - 1) @@ -328,10 +327,12 @@ function smart_paste(action, reindent) txt = kill_ring[kill_ring.pos] if txt then if reindent then - local indent = buffer.line_indentation[ - buffer:line_from_position(buffer.current_pos) ] - local padding = string.rep(buffer.use_tabs and '\t' or ' ', - buffer.use_tabs and indent / buffer.tab_width or indent) + local indent = + buffer.line_indentation[ + buffer:line_from_position(buffer.current_pos)] + local padding = + string.rep(buffer.use_tabs and '\t' or ' ', + buffer.use_tabs and indent / buffer.tab_width or indent) txt = txt:gsub('\n', '\n'..padding) end buffer:replace_sel(txt) @@ -367,7 +368,7 @@ function transpose_chars() else buffer:char_right() end - buffer:insert_text( -1, string.char(char) ) + buffer:insert_text(-1, string.char(char)) buffer:end_undo_action() buffer:goto_pos(caret) end @@ -384,7 +385,7 @@ function squeeze(char) while buffer.char_at[s] == char do s = s - 1 end while buffer.char_at[e] == char do e = e + 1 end buffer:set_sel(s + 1, e) - buffer:replace_sel( string.char(char) ) + buffer:replace_sel(string.char(char)) end --- @@ -392,7 +393,10 @@ end function join_lines() local buffer = buffer buffer:begin_undo_action() - buffer:line_end() buffer:clear() buffer:add_text(' ') squeeze() + buffer:line_end() + buffer:clear() + buffer:add_text(' ') + squeeze() buffer:end_undo_action() end @@ -471,21 +475,21 @@ function select_enclosed(str) local buffer = buffer if str then buffer:search_anchor() - local s = buffer:search_prev( 0, enclosure[str].left ) - local e = buffer:search_next( 0, enclosure[str].right ) + local s = buffer:search_prev(0, enclosure[str].left) + local e = buffer:search_next(0, enclosure[str].right) if s and e then buffer:set_sel(s + 1, e) end else -- TODO: ignore enclosures in comment scopes? s, e = buffer.anchor, buffer.current_pos if s > e then s, e = e, s end - local char = string.char( buffer.char_at[s - 1] ) + local char = string.char(buffer.char_at[s - 1]) if s ~= e and char_matches[char] then s, e = s - 2, e + 1 -- don't match the same enclosure end while s >= 0 do - char = string.char( buffer.char_at[s] ) + char = string.char(buffer.char_at[s]) if char_matches[char] then - local _, e = buffer:find( char_matches[char], 0, e ) + local _, e = buffer:find(char_matches[char], 0, e) if e then buffer:set_sel(s + 1, e - 1) break end end s = s - 1 @@ -510,7 +514,8 @@ end -- Selects the current line. function select_line() local buffer = buffer - buffer:home() buffer:line_end_extend() + buffer:home() + buffer:line_end_extend() end --- @@ -657,7 +662,7 @@ get_preceding_number = function() local caret = buffer.current_pos local char = buffer.char_at[caret - 1] local txt = '' - while tonumber( string.char(char) ) do + while tonumber(string.char(char)) do txt = txt..string.char(char) caret = caret - 1 char = buffer.char_at[caret - 1] diff --git a/modules/textadept/lsnippets.lua b/modules/textadept/lsnippets.lua index ad64c886..47819ae5 100644 --- a/modules/textadept/lsnippets.lua +++ b/modules/textadept/lsnippets.lua @@ -108,10 +108,10 @@ function insert(s_text) local caret = buffer.current_pos local lexer, style, start, s_name if not s_text then - lexer = buffer:get_lexer_language() - style = buffer:get_style_name( buffer.style_at[caret] ) + lexer = buffer:get_lexer_language() + style = buffer:get_style_name(buffer.style_at[caret]) buffer:word_left_extend() - start = buffer.current_pos + start = buffer.current_pos s_name = buffer:get_sel_text() end if s_name then @@ -133,13 +133,14 @@ function insert(s_text) -- Execute Lua and shell code. s_text = s_text:gsub('%%(%b())', run_lua_code) - s_text = s_text:gsub('`([^`]+)`', - function(code) - local p = io.popen(code) - local out = p:read('*all'):sub(1, -2) - p:close() - return out - end) + s_text = + s_text:gsub('`([^`]+)`', + function(code) + local p = io.popen(code) + local out = p:read('*all'):sub(1, -2) + p:close() + return out + end) -- Initialize the new snippet. If one is running, push it onto the stack. if snippet.index then snippet_stack[#snippet_stack + 1] = snippet end @@ -154,7 +155,8 @@ function insert(s_text) end -- Insert the snippet and set a mark defining the end of it. - buffer:replace_sel(s_text) buffer:add_text('\n') + buffer:replace_sel(s_text) + buffer:add_text('\n') local line = buffer:line_from_position(buffer.current_pos) snippet.end_marker = buffer:marker_add(line, MARK_SNIPPET) buffer:marker_set_back(MARK_SNIPPET, MARK_SNIPPET_COLOR) @@ -195,23 +197,25 @@ function next() local ph_text = buffer:text_range(snippet.ph_pos, caret) -- Transform mirror. - s_text = s_text:gsub('%%'..index..'(%b())', - function(mirror) - local pattern, replacement = mirror:match('^%(([^|]+)|(.+)%)$') - if not pattern and not replacement then return ph_text end - return ph_text:gsub( unhandle_escapes(pattern), - function(...) - local arg = {...} - local repl = replacement:gsub('%%(%d)', - function(i) return arg[ tonumber(i) ] or '' end) - return repl:gsub('#(%b())', run_lua_code) - end ) - end) + s_text = + s_text:gsub('%%'..index..'(%b())', + function(mirror) + local pattern, replacement = mirror:match('^%(([^|]+)|(.+)%)$') + if not pattern and not replacement then return ph_text end + return ph_text:gsub(unhandle_escapes(pattern), + function(...) + local arg = {...} + local repl = replacement:gsub('%%(%d)', + function(i) return arg[tonumber(i)] or '' end) + return repl:gsub('#(%b())', run_lua_code) + end) + end) -- Regular mirror. s_text = s_text:gsub('%%'..index, ph_text) - buffer:set_sel(s_start, s_end) buffer:replace_sel(s_text) + buffer:set_sel(s_start, s_end) + buffer:replace_sel(s_text) s_start, s_end = snippet_info() buffer:end_undo_action() end @@ -223,8 +227,9 @@ function next() if next_item and not next_item:find('|') then -- placeholder s, e = buffer:find('%'..index..next_item, 0, s_start) next_item = next_item:gsub('#(%b())', run_lua_code) - next_item = unhandle_escapes( next_item:sub(2, -2) ) - buffer:set_sel(s, e) buffer:replace_sel(next_item) + next_item = unhandle_escapes(next_item:sub(2, -2)) + buffer:set_sel(s, e) + buffer:replace_sel(next_item) buffer:set_sel(s, s + #next_item) else -- use the first mirror as a placeholder s, e = buffer:find('%'..index..'[^(]', 2097152, s_start) -- regexp @@ -234,17 +239,25 @@ function next() if e then e = e + 1 end end if not s then snippet.index = index + 1 return next() end - buffer:set_sel(s, e - 1) buffer:replace_sel('') + buffer:set_sel(s, e - 1) + buffer:replace_sel('') end snippet.ph_pos = s snippet.index = index else - s_text = unescape( unhandle_escapes( s_text:gsub('%%0', '%%__caret') ) ) - buffer:set_sel(s_start, s_end) buffer:replace_sel(s_text) + s_text = unescape(unhandle_escapes(s_text:gsub('%%0', '%%__caret'))) + buffer:set_sel(s_start, s_end) + buffer:replace_sel(s_text) s_start, s_end = snippet_info() - if s_end then buffer:goto_pos(s_end + 1) buffer:delete_back() end + if s_end then + buffer:goto_pos(s_end + 1) + buffer:delete_back() + end local s, e = buffer:find('%__caret', 4, s_start) - if s and s <= s_end then buffer:set_sel(s, e) buffer:replace_sel('') end + if s and s <= s_end then + buffer:set_sel(s, e) + buffer:replace_sel('') + end buffer:marker_delete_handle(snippet.end_marker) snippet = #snippet_stack > 0 and table.remove(snippet_stack) or {} end @@ -261,7 +274,8 @@ function prev() if index > 1 then local s_start, s_end = snippet_info() local s_text = snippet.snapshots[index - 2] - buffer:set_sel(s_start, s_end) buffer:replace_sel(s_text) + buffer:set_sel(s_start, s_end) + buffer:replace_sel(s_text) snippet.index = index - 2 next() end @@ -276,9 +290,11 @@ function cancel_current() local s_start, s_end = snippet_info() buffer:begin_undo_action() if s_start and s_end then - buffer:set_sel(s_start, s_end) buffer:replace_sel('') + buffer:set_sel(s_start, s_end) + buffer:replace_sel('') s_start, s_end = snippet_info() - buffer:goto_pos(s_end + 1) buffer:delete_back() + buffer:goto_pos(s_end + 1) + buffer:delete_back() end if snippet.prev_sel_text then buffer:add_text(snippet.prev_sel_text) end buffer:end_undo_action() @@ -291,24 +307,22 @@ end -- Global snippets and snippets in the current lexer and style are used. function list() local buffer = buffer - local list, list_str = {}, '' + local list = {} local function add_snippets(snippets) for s_name in pairs(snippets) do list[#list + 1] = s_name end end local snippets = _G.snippets add_snippets(snippets) local lexer = buffer:get_lexer_language() - local style = buffer:get_style_name( buffer.style_at[buffer.current_pos] ) - if snippets[lexer] and type( snippets[lexer] ) == 'table' then - add_snippets( snippets[lexer] ) - if snippets[lexer][style] then add_snippets( snippets[lexer][style] ) end + local style = buffer:get_style_name(buffer.style_at[buffer.current_pos]) + if snippets[lexer] and type(snippets[lexer]) == 'table' then + add_snippets(snippets[lexer]) + if snippets[lexer][style] then add_snippets(snippets[lexer][style]) end end table.sort(list) - local sep = string.char(buffer.auto_c_separator) - for _, v in ipairs(list) do list_str = list_str..v..sep end - list_str = list_str:sub(1, -2) local caret = buffer.current_pos - buffer:auto_c_show(caret - buffer:word_start_position(caret, true), list_str) + buffer:auto_c_show(caret - buffer:word_start_position(caret, true), + table.concat(list, string.char(buffer.auto_c_separator))) end --- @@ -318,8 +332,9 @@ function show_style() local lexer = buffer:get_lexer_language() local style_num = buffer.style_at[buffer.current_pos] local style = buffer:get_style_name(style_num) - local text = string.format( - textadept.locale.M_TEXTADEPT_SNIPPETS_SHOW_STYLE, lexer, style, style_num ) + local text = + string.format(textadept.locale.M_TEXTADEPT_SNIPPETS_SHOW_STYLE, lexer, + style, style_num) buffer:call_tip_show(buffer.current_pos, text) end @@ -330,8 +345,9 @@ end snippet_info = function() local buffer = buffer local s = snippet.start_pos - local e = buffer:position_from_line( - buffer:marker_line_from_handle(snippet.end_marker) ) - 1 + local e = + buffer:position_from_line( + buffer:marker_line_from_handle(snippet.end_marker)) - 1 if e >= s then return s, e, buffer:text_range(s, e) end end @@ -339,9 +355,9 @@ end -- [Local function] Runs the given Lua code. run_lua_code = function(code) code = unhandle_escapes(code) - local env = setmetatable( - { selected_text = buffer:get_sel_text() }, { __index = _G } ) - local _, val = pcall( setfenv( loadstring('return '..code), env ) ) + local env = + setmetatable({ selected_text = buffer:get_sel_text() }, { __index = _G }) + local _, val = pcall(setfenv(loadstring('return '..code), env)) return val or '' end @@ -351,7 +367,9 @@ end -- '%%' is the escape character used. handle_escapes = function(s) return s:gsub('%%([%%`%)|#])', - function(char) return ("\\%03d"):format( char:byte() ) end) + function(char) + return ("\\%03d"):format(char:byte()) + end) end --- @@ -359,7 +377,9 @@ end -- a given string. unhandle_escapes = function(s) return s:gsub('\\(%d%d%d)', - function(value) return '%'..string.char(value) end) + function(value) + return '%'..string.char(value) + end) end --- diff --git a/modules/textadept/macros.lua b/modules/textadept/macros.lua index 70be5ef4..978a3482 100644 --- a/modules/textadept/macros.lua +++ b/modules/textadept/macros.lua @@ -64,11 +64,12 @@ function stop_recording() buffer:stop_record() recording = false local textadept, locale = textadept, textadept.locale - local ret, macro_name = cocoa_dialog( 'standard-inputbox', { - ['informative-text'] = locale.M_TEXTADEPT_MACRO_SAVE_TITLE, - text = locale.M_TEXTADEPT_MACRO_SAVE_TEXT, - ['no-newline'] = true - } ):match('^(%d)\n([^\n]+)$') + local ret, macro_name = + cocoa_dialog('standard-inputbox', { + ['informative-text'] = locale.M_TEXTADEPT_MACRO_SAVE_TITLE, + text = locale.M_TEXTADEPT_MACRO_SAVE_TEXT, + ['no-newline'] = true + }):match('^(%d)\n([^\n]+)$') if ret == '1' and macro_name and #macro_name > 0 then for _, command in ipairs(current) do @@ -99,17 +100,18 @@ end function play(macro_name) if not macro_name then local locale = textadept.locale - local macro_list = '' - for name in pairs(list) do macro_list = macro_list..'"'..name..'"'..' ' end - if #macro_list > 0 then + local macros = {} + for name, _ in pairs(list) do macros[#macros + 1] = name end + if #macros > 0 then local ret - ret, macro_name = cocoa_dialog( 'standard-dropdown', { - title = locale.M_TEXTADEPT_MACRO_SELECT_TITLE, - text = locale.M_TEXTADEPT_MACRO_SELECT_TEXT, - items = macro_list, - ['string-output'] = true, - ['no-newline'] = true - } ):match('^([^\n]+)\n([^\n]+)$') + ret, macro_name = + cocoa_dialog('standard-dropdown', { + title = locale.M_TEXTADEPT_MACRO_SELECT_TITLE, + text = locale.M_TEXTADEPT_MACRO_SELECT_TEXT, + items = '"'..table.concat(macros, '" "')..'"', + ['string-output'] = true, + ['no-newline'] = true + }):match('^([^\n]+)\n([^\n]+)$') if ret == 'Cancel' then return end end end @@ -118,7 +120,7 @@ function play(macro_name) local buffer, bf = buffer, textadept.buffer_functions for _, command in ipairs(macro) do local cmd, wParam, lParam = unpack(command) - local _, _, p1_type, p2_type = unpack( bf[cmd] ) + local _, _, p1_type, p2_type = unpack(bf[cmd]) if p2_type == 7 and p1_type == 0 or p1_type == 2 then -- single string param buffer[cmd](buffer, lParam) else @@ -143,16 +145,18 @@ end -- @param filename The absolute path to the file to save the macros to. function save(filename) if not filename then filename = MACRO_FILE end - local f = assert( io.open(filename, 'w') ) + local f = assert(io.open(filename, 'w')) for name, macro in pairs(list) do f:write(name, '\n') for _, command in ipairs(macro) do local msg, wParam, lParam = unpack(command) if type(lParam) == 'string' then - lParam = lParam:gsub( '[\t\n\r\f]', - { ['\t'] = '\\t', ['\n'] = '\\n', ['\r'] = '\\r', ['\f'] = '\\f' } ) + lParam = + lParam:gsub('[\t\n\r\f]', + { ['\t'] = '\\t', ['\n'] = '\\n', ['\r'] = '\\r', + ['\f'] = '\\f' }) end - f:write( ("%s\t%s\t%s\n"):format(msg, wParam, lParam) ) + f:write(("%s\t%s\t%s\n"):format(msg, wParam, lParam)) end f:write('\n') end @@ -178,8 +182,10 @@ function load(filename) else local cmd, wParam, lParam = line:match('^([^\t]+)\t([^\t]+)\t(.*)$') if cmd and wParam and lParam then - lParam = lParam:gsub( '\\[tnrf]', - { ['\\t'] = '\t', ['\\n'] = '\n', ['\\r'] = '\r', ['\\f'] = '\f' } ) + lParam = + lParam:gsub('\\[tnrf]', + { ['\\t'] = '\t', ['\\n'] = '\n', ['\\r'] = '\r', + ['\\f'] = '\f' }) local num = wParam:match('^-?%d+$') if num then wParam = tonumber(num) end num = lParam:match('^-?%d+$') diff --git a/modules/textadept/mlines.lua b/modules/textadept/mlines.lua index c0963b8d..ce1997ce 100644 --- a/modules/textadept/mlines.lua +++ b/modules/textadept/mlines.lua @@ -105,10 +105,10 @@ end function update() local buffer = buffer local curr_line = buffer:line_from_position(buffer.current_pos) - local curr_col = buffer.column[buffer.current_pos] + local curr_col = buffer.column[buffer.current_pos] buffer:begin_undo_action() if mlines[curr_line] then - local s = buffer:find_column( curr_line, mlines[curr_line].start_col ) + local s = buffer:find_column(curr_line, mlines[curr_line].start_col) local e = buffer:find_column(curr_line, curr_col) local delta = e - s local txt = '' diff --git a/modules/textadept/snippets.lua b/modules/textadept/snippets.lua index 01e42986..ec8cd11a 100644 --- a/modules/textadept/snippets.lua +++ b/modules/textadept/snippets.lua @@ -59,11 +59,11 @@ function insert(snippet_arg) local sel_text = buffer:get_sel_text() if not snippet_arg then orig_pos = buffer.current_pos buffer:word_left_extend() - new_pos = buffer.current_pos - lexer = buffer:get_lexer_language() - style = buffer.style_at[orig_pos] - scope = buffer:get_style_name(style) - s_name = buffer:get_sel_text() + new_pos = buffer.current_pos + lexer = buffer:get_lexer_language() + style = buffer.style_at[orig_pos] + scope = buffer:get_style_name(style) + s_name = buffer:get_sel_text() else if buffer.current_pos > buffer.anchor then buffer.current_pos, buffer.anchor = buffer.anchor, buffer.current_pos @@ -96,10 +96,10 @@ function insert(snippet_arg) _DEBUG('s_text escaped:\n'..s_text) -- Replace Lua code return. - local env = setmetatable( { selected_text = sel_text }, { __index = _G } ) + local env = setmetatable({ selected_text = sel_text }, { __index = _G }) s_text = s_text:gsub('$(%b())', function(s) - local f = loadstring( 'return '..s:sub(2, -2) ) + local f = loadstring('return '..s:sub(2, -2)) setfenv(f, env) local ret, val = pcall(f) if ret then return val or '' end @@ -120,17 +120,17 @@ function insert(snippet_arg) if snippet.index then snippet_stack[#snippet_stack + 1] = snippet end snippet = {} - snippet.index = 0 + snippet.index = 0 snippet.start_pos = buffer.current_pos - snippet.cursor = nil - snippet.sel_text = sel_text + snippet.cursor = nil + snippet.sel_text = sel_text -- Make a table of placeholders and tab stops. local patt, patt2 = '($%b{})', '^%${(%d+):.*}$' local s, _, item = s_text:find(patt) while item do local num = item:match(patt2) - if num then snippet[ tonumber(num) ] = unescape(item) end + if num then snippet[tonumber(num)] = unescape(item) end local i = s + 1 s, _, item = s_text:find(patt, i) end @@ -154,7 +154,7 @@ function insert(snippet_arg) count = count + 1 i = s_text:find('\n', i + 1) until i == nil - match_indention( buffer:line_from_position(orig_pos), count ) + match_indention(buffer:line_from_position(orig_pos), count) else buffer:goto_pos(orig_pos) end @@ -192,35 +192,36 @@ next_snippet_item = function() -- Regex mirror. patt = '%${'..snippet.index..'/(.-)/(.-)/([iomxneus]*)}' - s_text = s_text:gsub(patt, - function(pattern, replacement, options) - local script = [[ - li = %q(last_item) - rep = %q(replacement) - li =~ /pattern/options - if data = $~ - rep.gsub!(/\#\{(.+?)\}/) do - expr = $1.gsub(/\$(\d\d?)/, 'data[\1]') - eval expr + s_text = + s_text:gsub(patt, + function(pattern, replacement, options) + local script = [[ + li = %q(last_item) + rep = %q(replacement) + li =~ /pattern/options + if data = $~ + rep.gsub!(/\#\{(.+?)\}/) do + expr = $1.gsub(/\$(\d\d?)/, 'data[\1]') + eval expr + end + puts rep.gsub(/\$(\d\d?)/) { data[$1.to_i] } end - puts rep.gsub(/\$(\d\d?)/) { data[$1.to_i] } - end - ]] - pattern = unescape(pattern) - replacement = unescape(replacement) - script = script:gsub('last_item', last_item) - script = script:gsub('pattern', pattern) - script = script:gsub('options', options) - script = script:gsub('replacement', replacement) - _DEBUG('script:\n'..script) - - local p = io.popen("ruby 2>&1 <<'_EOF'\n"..script..'\n_EOF') - local out = p:read('*all') - p:close() - _DEBUG('regex out:\n'..out) - if out:sub(-1) == '\n' then out = out:sub(1, -2) end -- chomp - return out - end) + ]] + pattern = unescape(pattern) + replacement = unescape(replacement) + script = script:gsub('last_item', last_item) + script = script:gsub('pattern', pattern) + script = script:gsub('options', options) + script = script:gsub('replacement', replacement) + _DEBUG('script:\n'..script) + + local p = io.popen("ruby 2>&1 <<'_EOF'\n"..script..'\n_EOF') + local out = p:read('*all') + p:close() + _DEBUG('regex out:\n'..out) + if out:sub(-1) == '\n' then out = out:sub(1, -2) end -- chomp + return out + end) _DEBUG('patterns replaced:\n'..s_text) -- Plain text mirror. @@ -305,7 +306,8 @@ function cancel_current() local s_start, s_end = snippet_text() if s_start and s_end then buffer:set_sel(s_start, s_end) - buffer:replace_sel('') join_lines() + buffer:replace_sel('') + join_lines() end if snippet.sel_text then buffer:add_text(snippet.sel_text) @@ -323,7 +325,7 @@ end -- Global snippets and snippets in the current lexer and scope are used. function list() local buffer = buffer - local list, list_str = {}, '' + local list = {} local function add_snippets(snippets) for s_name in pairs(snippets) do table.insert(list, s_name) end @@ -335,17 +337,15 @@ function list() local lexer = buffer:get_lexer_language() local style = buffer.style_at[buffer.current_pos] local scope = buffer:get_style_name(style) - if snippets[lexer] and type( snippets[lexer] ) == 'table' then - add_snippets( snippets[lexer] ) - if snippets[lexer][scope] then add_snippets( snippets[lexer][scope] ) end + if snippets[lexer] and type(snippets[lexer]) == 'table' then + add_snippets(snippets[lexer]) + if snippets[lexer][scope] then add_snippets(snippets[lexer][scope]) end end end - table.sort(list) - local sep = string.char(buffer.auto_c_separator) - for _, v in pairs(list) do list_str = list_str..v..sep end - list_str = list_str:sub(1, -2) -- chop - buffer:auto_c_show(0, list_str) + + buffer:auto_c_show(0, + table.concat(list, string.char(buffer.auto_c_separator))) end --- @@ -355,8 +355,9 @@ function show_scope() local buffer = buffer local lexer = buffer:get_lexer_language() local scope = buffer.style_at[buffer.current_pos] - local text = string.format( - textadept.locale.M_TEXTADEPT_SNIPPETS_SHOW_STYLE, lexer, style, style_num ) + local text = + string.format(textadept.locale.M_TEXTADEPT_SNIPPETS_SHOW_STYLE, lexer, + style, style_num) buffer:call_tip_show(buffer.current_pos, text) end @@ -367,8 +368,9 @@ end snippet_text = function() local buffer = buffer local s = snippet.start_pos - local e = buffer:position_from_line( - buffer:marker_line_from_handle(snippet.end_marker) ) - 1 + local e = + buffer:position_from_line( + buffer:marker_line_from_handle(snippet.end_marker) ) - 1 if e >= s then return s, e, buffer:text_range(s, e) end end @@ -377,7 +379,9 @@ end -- equivalents. escape = function(text) return text:gsub('\\([$/}`])', - function(char) return ("\\%03d"):format( char:byte() ) end) + function(char) + return ("\\%03d"):format(char:byte()) + end) end --- @@ -385,7 +389,9 @@ end -- equivalents. unescape = function(text) return text:gsub('\\(%d%d%d)', - function(value) return '\\'..string.char(value) end) + function(value) + return '\\'..string.char(value) + end) end --- @@ -416,7 +422,8 @@ end -- This is used to remove the empty line containing the end of snippet marker. join_lines = function() local buffer = buffer - buffer:line_down() buffer:vc_home() + buffer:line_down() + buffer:vc_home() if buffer.column[buffer.current_pos] == 0 then buffer:vc_home() end buffer:home_extend() if #buffer:get_sel_text() > 0 then buffer:delete_back() end diff --git a/src/lua_interface.c b/src/lua_interface.c index 15cd3d95..cc9103f3 100644 --- a/src/lua_interface.c +++ b/src/lua_interface.c @@ -2,64 +2,81 @@ #include "textadept.h" -#define LS lua_State -#define LF static int #define streq(s1, s2) strcmp(s1, s2) == 0 -#define l_insert(l, i) lua_insert(l, i < 0 ? lua_gettop(l) + i : i) +#define l_insert(l, i) lua_insert(l, (i < 0) ? lua_gettop(l) + i : i) #define l_append(l, i) lua_rawseti(l, i, lua_objlen(l, i) + 1) -#define l_cfunc(l, f, k) { lua_pushcfunction(l, f); lua_setfield(l, -2, k); } +#define l_cfunc(l, f, k) { \ + lua_pushcfunction(l, f); \ + lua_setfield(l, -2, k); \ +} #define l_todocpointer(l, i) static_cast<sptr_t>(lua_tonumber(l, i)) #define l_togtkwidget(l, i) reinterpret_cast<GtkWidget*>(lua_touserdata(l, i)) #define l_mt(l, k, i, ni) { \ if (luaL_newmetatable(l, k)) { \ l_cfunc(l, i, "__index"); \ l_cfunc(l, ni, "__newindex"); \ - } lua_setmetatable(l, -2); } + } \ + lua_setmetatable(l, -2); \ +} #ifdef MAC using namespace Scintilla; #endif -LS *lua; +lua_State *lua; bool closing = false; -static int // parameter/return types - tVOID = 0, /*tINT = 1,*/ tLENGTH = 2, /*tPOSITION = 3,*/ /*tCOLOUR = 4,*/ - tBOOL = 5, tKEYMOD = 6, tSTRING = 7, tSTRINGRESULT = 8; - -static void clear_table(LS *lua, int index); -static void warn(const char *s) { printf("Warning: %s\n", s); } - -LF l_buffer_mt_index(LS *lua), l_buffer_mt_newindex(LS *lua), - l_bufferp_mt_index(LS *lua), l_bufferp_mt_newindex(LS *lua), - l_view_mt_index(LS *lua), l_view_mt_newindex(LS *lua), - l_ta_mt_index(LS *lua), l_ta_mt_newindex(LS *lua), - l_pm_mt_index(LS *lua), l_pm_mt_newindex(LS *lua), - l_find_mt_index(LS *lua), l_find_mt_newindex(LS *lua), - l_ce_mt_index(LS *lua), l_ce_mt_newindex(LS *lua); - -LF l_cf_ta_buffer_new(LS *lua), - l_cf_buffer_delete(LS *lua), - l_cf_buffer_find(LS *lua), - l_cf_buffer_text_range(LS *lua), - l_cf_view_focus(LS *lua), - l_cf_view_split(LS *lua), l_cf_view_unsplit(LS *lua), - l_cf_ta_get_split_table(LS *lua), - l_cf_ta_goto_window(LS *lua), - l_cf_view_goto_buffer(LS *lua), - l_cf_ta_gtkmenu(LS *lua), - l_cf_ta_popupmenu(LS *lua), - l_cf_ta_reset(LS *lua), - l_cf_ta_quit(LS *lua), - l_cf_pm_focus(LS *lua), l_cf_pm_clear(LS *lua), l_cf_pm_activate(LS *lua), - l_cf_find_focus(LS *lua), - l_cf_call_find_next(LS *lua), l_cf_call_find_prev(LS *lua), - l_cf_call_replace(LS *lua), l_cf_call_replace_all(LS *lua), - l_cf_ce_focus(LS *lua); +static int tVOID = 0, /*tINT = 1,*/ tLENGTH = 2, /*tPOSITION = 3,*/ + /*tCOLOUR = 4,*/ tBOOL = 5, tKEYMOD = 6, tSTRING = 7, + tSTRINGRESULT = 8; + +static void clear_table(lua_State *lua, int index); +static void warn(const char *s) { + printf("Warning: %s\n", s); +} + +static int l_buffer_mt_index(lua_State *lua), + l_buffer_mt_newindex(lua_State *lua), + l_bufferp_mt_index(lua_State *lua), + l_bufferp_mt_newindex(lua_State *lua), + l_view_mt_index(lua_State *lua), + l_view_mt_newindex(lua_State *lua), + l_ta_mt_index(lua_State *lua), + l_ta_mt_newindex(lua_State *lua), + l_pm_mt_index(lua_State *lua), + l_pm_mt_newindex(lua_State *lua), + l_find_mt_index(lua_State *lua), + l_find_mt_newindex(lua_State *lua), + l_ce_mt_index(lua_State *lua), + l_ce_mt_newindex(lua_State *lua); + +static int l_cf_ta_buffer_new(lua_State *lua), + l_cf_buffer_delete(lua_State *lua), + l_cf_buffer_find(lua_State *lua), + l_cf_buffer_text_range(lua_State *lua), + l_cf_view_focus(lua_State *lua), + l_cf_view_split(lua_State *lua), + l_cf_view_unsplit(lua_State *lua), + l_cf_ta_get_split_table(lua_State *lua), + l_cf_ta_goto_window(lua_State *lua), + l_cf_view_goto_buffer(lua_State *lua), + l_cf_ta_gtkmenu(lua_State *lua), + l_cf_ta_popupmenu(lua_State *lua), + l_cf_ta_reset(lua_State *lua), + l_cf_ta_quit(lua_State *lua), + l_cf_pm_focus(lua_State *lua), + l_cf_pm_clear(lua_State *lua), + l_cf_pm_activate(lua_State *lua), + l_cf_find_focus(lua_State *lua), + l_cf_call_find_next(lua_State *lua), + l_cf_call_find_prev(lua_State *lua), + l_cf_call_replace(lua_State *lua), + l_cf_call_replace_all(lua_State *lua), + l_cf_ce_focus(lua_State *lua); const char - *views_dne = "textadept.views doesn't exist or was overwritten.", - *buffers_dne = "textadept.buffers doesn't exist or was overwritten."; + *views_dne = "textadept.views does not exist or was overwritten.", + *buffers_dne = "textadept.buffers does not exist or was overwritten."; /** * Inits or re-inits the Lua State. @@ -78,10 +95,13 @@ bool l_init(int argc, char **argv, bool reinit) { lua_rawseti(lua, -2, i); } lua_setfield(lua, LUA_REGISTRYINDEX, "arg"); - lua_newtable(lua); lua_setfield(lua, LUA_REGISTRYINDEX, "buffers"); - lua_newtable(lua); lua_setfield(lua, LUA_REGISTRYINDEX, "views"); + lua_newtable(lua); + lua_setfield(lua, LUA_REGISTRYINDEX, "buffers"); + lua_newtable(lua); + lua_setfield(lua, LUA_REGISTRYINDEX, "views"); } else { // clear package.loaded and _G - lua_getglobal(lua, "package"); lua_getfield(lua, -1, "loaded"); + lua_getglobal(lua, "package"); + lua_getfield(lua, -1, "loaded"); clear_table(lua, lua_gettop(lua)); lua_pop(lua, 2); // package and package.loaded clear_table(lua, LUA_GLOBALSINDEX); @@ -121,13 +141,16 @@ bool l_init(int argc, char **argv, bool reinit) { l_mt(lua, "_textadept_mt", l_ta_mt_index, l_ta_mt_newindex); lua_setglobal(lua, "textadept"); - lua_getfield(lua, LUA_REGISTRYINDEX, "arg"); lua_setglobal(lua, "arg"); - lua_pushstring(lua, textadept_home); lua_setglobal(lua, "_HOME"); -#ifdef WIN32 - lua_pushboolean(lua, 1); lua_setglobal(lua, "WIN32"); -#endif -#ifdef MAC - lua_pushboolean(lua, 1); lua_setglobal(lua, "MAC"); + lua_getfield(lua, LUA_REGISTRYINDEX, "arg"); + lua_setglobal(lua, "arg"); + lua_pushstring(lua, textadept_home); + lua_setglobal(lua, "_HOME"); +#if WIN32 + lua_pushboolean(lua, 1); + lua_setglobal(lua, "WIN32"); +#elif MAC + lua_pushboolean(lua, 1); + lua_setglobal(lua, "MAC"); #endif return l_load_script("core/init.lua"); @@ -144,8 +167,9 @@ bool l_load_script(const char *script_file) { const char *errmsg = lua_tostring(lua, -1); lua_settop(lua, 0); #ifndef WIN32 - GtkWidget *dialog = gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, - GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "%s\n", errmsg); + GtkWidget *dialog = + gtk_message_dialog_new(NULL, GTK_DIALOG_MODAL, GTK_MESSAGE_ERROR, + GTK_BUTTONS_OK, "%s\n", errmsg); gtk_dialog_run(GTK_DIALOG(dialog)); #else MessageBox(0, static_cast<LPCSTR>(errmsg), "Error", 0); @@ -161,9 +185,10 @@ bool l_load_script(const char *script_file) { * @param lua The Lua State. * @param k The string key to lookup. */ -bool l_ta_get(LS *lua, const char *k) { +bool l_ta_get(lua_State *lua, const char *k) { lua_getglobal(lua, "textadept"); - lua_pushstring(lua, k); lua_rawget(lua, -2); + lua_pushstring(lua, k); + lua_rawget(lua, -2); lua_remove(lua, -2); // textadept return lua_istable(lua, -1); } @@ -174,9 +199,11 @@ bool l_ta_get(LS *lua, const char *k) { * @param lua The Lua State. * @param k The string key to set the value of. */ -void l_ta_set(LS *lua, const char *k) { +void l_ta_set(lua_State *lua, const char *k) { lua_getglobal(lua, "textadept"); - lua_pushstring(lua, k); lua_pushvalue(lua, -3); lua_rawset(lua, -3); + lua_pushstring(lua, k); + lua_pushvalue(lua, -3); + lua_rawset(lua, -3); lua_pop(lua, 2); // value and textadept } @@ -187,7 +214,7 @@ void l_ta_set(LS *lua, const char *k) { * @param lua The Lua State. * @param k The string key to set the value of. */ -void l_reg_set(LS *lua, const char *k) { +void l_reg_set(lua_State *lua, const char *k) { lua_setfield(lua, LUA_REGISTRYINDEX, k); lua_getfield(lua, LUA_REGISTRYINDEX, k); l_ta_set(lua, k); @@ -202,10 +229,10 @@ void l_reg_set(LS *lua, const char *k) { * Scintilla window. * Defaults to "View argument expected.". */ -static GtkWidget *l_checkview(LS *lua, int narg, const char *errstr=0) { +static GtkWidget *l_checkview(lua_State *lua, int narg, const char *errstr=0) { if (lua_type(lua, narg) == LUA_TTABLE) { lua_pushstring(lua, "widget_pointer"); - lua_rawget(lua, narg > 0 ? narg : narg - 1); + lua_rawget(lua, (narg > 0) ? narg : narg - 1); if (lua_type(lua, -1) != LUA_TLIGHTUSERDATA) luaL_error(lua, errstr ? errstr : "View argument expected."); } else luaL_error(lua, errstr ? errstr : "View argument expected."); @@ -241,7 +268,7 @@ void l_remove_scintilla_window(GtkWidget *editor) { if (!l_ta_get(lua, "views")) luaL_error(lua, views_dne); lua_pushnil(lua); while (lua_next(lua, -2)) - editor != l_checkview(lua, -1) ? l_append(lua, -4) : lua_pop(lua, 1); + (editor != l_checkview(lua, -1)) ? l_append(lua, -4) : lua_pop(lua, 1); lua_pop(lua, 1); // views l_reg_set(lua, "views"); } @@ -266,8 +293,10 @@ void l_goto_scintilla_window(GtkWidget *editor, int n, bool absolute) { break; } else lua_pop(lua, 1); // value idx += n; - if (idx > lua_objlen(lua, -1)) idx = 1; - else if (idx < 1) idx = lua_objlen(lua, -1); + if (idx > lua_objlen(lua, -1)) + idx = 1; + else if (idx < 1) + idx = lua_objlen(lua, -1); lua_rawgeti(lua, -1, idx); } else lua_rawgeti(lua, -1, n); editor = l_checkview(lua, -1, "No view exists at that index."); @@ -301,10 +330,11 @@ void l_set_view_global(GtkWidget *editor) { * buffer table. * Defaults to "Buffer argument expected.". */ -static sptr_t l_checkdocpointer(LS *lua, int narg, const char *errstr=0) { +static sptr_t l_checkdocpointer(lua_State *lua, int narg, + const char *errstr=0) { if (lua_type(lua, narg) == LUA_TTABLE) { lua_pushstring(lua, "doc_pointer"); - lua_rawget(lua, narg > 0 ? narg : narg - 1); + lua_rawget(lua, (narg > 0) ? narg : narg - 1); if (lua_type(lua, -1) != LUA_TNUMBER) luaL_error(lua, errstr ? errstr : "Buffer argument expected."); } else luaL_error(lua, errstr ? errstr : "Buffer argument expected."); @@ -316,12 +346,15 @@ static sptr_t l_checkdocpointer(LS *lua, int narg, const char *errstr=0) { /** * Adds a Scintilla document to the global 'buffers' table with a metatable. * @param doc The Scintilla document to add. + * @return integer index of the new buffer in textadept.buffers. */ int l_add_scintilla_buffer(sptr_t doc) { if (!l_ta_get(lua, "buffers")) luaL_error(lua, buffers_dne); lua_newtable(lua); - lua_pushnumber(lua, doc); lua_setfield(lua, -2, "doc_pointer"); - lua_pushboolean(lua, false); lua_setfield(lua, -2, "dirty"); + lua_pushnumber(lua, doc); + lua_setfield(lua, -2, "doc_pointer"); + lua_pushboolean(lua, false); + lua_setfield(lua, -2, "dirty"); l_cfunc(lua, l_cf_buffer_find, "find"); l_cfunc(lua, l_cf_buffer_text_range, "text_range"); l_cfunc(lua, l_cf_buffer_delete, "delete"); @@ -346,12 +379,13 @@ void l_remove_scintilla_buffer(sptr_t doc) { sptr_t that_doc = SS(SCINTILLA(editor), SCI_GETDOCPOINTER); if (that_doc == doc) l_goto_scintilla_buffer(editor, -1, false); lua_pop(lua, 1); // value - } lua_pop(lua, 1); // views + } + lua_pop(lua, 1); // views lua_newtable(lua); if (!l_ta_get(lua, "buffers")) luaL_error(lua, buffers_dne); lua_pushnil(lua); while (lua_next(lua, -2)) - doc != l_checkdocpointer(lua, -1) ? l_append(lua, -4) : lua_pop(lua, 1); + (doc != l_checkdocpointer(lua, -1)) ? l_append(lua, -4) : lua_pop(lua, 1); lua_pop(lua, 1); // buffers l_reg_set(lua, "buffers"); } @@ -375,10 +409,15 @@ unsigned int l_get_docpointer_index(sptr_t doc) { return idx; } -#define l_set_bufferp(k, v) \ - { lua_pushstring(lua, k); lua_pushinteger(lua, v); lua_rawset(lua, -3); } -#define l_get_bufferp(k, i) \ - { lua_pushstring(lua, k); lua_rawget(lua, i < 0 ? i - 1 : i); } +#define l_set_bufferp(k, v) { \ + lua_pushstring(lua, k); \ + lua_pushinteger(lua, v); \ + lua_rawset(lua, -3); \ +} +#define l_get_bufferp(k, i) { \ + lua_pushstring(lua, k); \ + lua_rawget(lua, (i < 0) ? i - 1 : i); \ +} /** * Changes a Scintilla window's document to one in the global 'buffers' table. @@ -398,8 +437,10 @@ void l_goto_scintilla_buffer(GtkWidget *editor, int n, bool absolute) { sptr_t doc = SS(sci, SCI_GETDOCPOINTER); unsigned int idx = l_get_docpointer_index(doc); idx += n; - if (idx > lua_objlen(lua, -1)) idx = 1; - else if (idx < 1) idx = lua_objlen(lua, -1); + if (idx > lua_objlen(lua, -1)) + idx = 1; + else if (idx < 1) + idx = lua_objlen(lua, -1); lua_rawgeti(lua, -1, idx); } else lua_rawgeti(lua, -1, n); sptr_t doc = l_checkdocpointer(lua, -1, "No buffer exists at that index."); @@ -408,9 +449,10 @@ void l_goto_scintilla_buffer(GtkWidget *editor, int n, bool absolute) { if (lua_istable(lua, -1)) { l_set_bufferp("_anchor", SS(sci, SCI_GETANCHOR)); l_set_bufferp("_current_pos", SS(sci, SCI_GETCURRENTPOS)); - l_set_bufferp("_first_visible_line", - SS(sci, SCI_DOCLINEFROMVISIBLE, SS(sci, SCI_GETFIRSTVISIBLELINE))); - } lua_pop(lua, 1); // buffer + l_set_bufferp("_first_visible_line", SS(sci, SCI_DOCLINEFROMVISIBLE, + SS(sci, SCI_GETFIRSTVISIBLELINE))); + } + lua_pop(lua, 1); // buffer // Change the view. SS(sci, SCI_SETDOCPOINTER, 0, doc); l_set_buffer_global(sci); @@ -422,7 +464,7 @@ void l_goto_scintilla_buffer(GtkWidget *editor, int n, bool absolute) { l_get_bufferp("_first_visible_line", -3); SS(sci, SCI_LINESCROLL, 0, SS(sci, SCI_VISIBLEFROMDOCLINE, lua_tointeger(lua, -1)) - - SS(sci, SCI_GETFIRSTVISIBLELINE)); + SS(sci, SCI_GETFIRSTVISIBLELINE)); lua_pop(lua, 4); // _anchor, _current_pos, _first_visible_line, and buffer if (!closing) l_handle_event("buffer_switch"); lua_pop(lua, 2); // buffer table and buffers @@ -460,7 +502,8 @@ void l_close() { sptr_t doc = l_checkdocpointer(lua, -1); remove_scintilla_buffer(doc); lua_pop(lua, 1); // value - } lua_pop(lua, 1); // buffers + } + lua_pop(lua, 1); // buffers gtk_widget_destroy(focused_editor); lua_close(lua); } @@ -473,11 +516,12 @@ void l_close() { * @param lua The Lua State. * @param abs_index The absolute stack index of the table to clear. */ -static void clear_table(LS *lua, int abs_index) { +static void clear_table(lua_State *lua, int abs_index) { lua_pushnil(lua); while (lua_next(lua, abs_index)) { lua_pop(lua, 1); // value - lua_pushnil(lua); lua_rawset(lua, abs_index); + lua_pushnil(lua); + lua_rawset(lua, abs_index); lua_pushnil(lua); // get 'new' first key } } @@ -488,7 +532,7 @@ static void clear_table(LS *lua, int abs_index) { * @param lua The Lua State. * @param narg The relative stack position of the buffer table. */ -static void l_check_focused_buffer(LS *lua, int narg) { +static void l_check_focused_buffer(lua_State *lua, int narg) { ScintillaObject *sci = SCINTILLA(focused_editor); sptr_t cur_doc = SS(sci, SCI_GETDOCPOINTER); if (cur_doc != l_checkdocpointer(lua, narg)) @@ -502,7 +546,8 @@ static void l_check_focused_buffer(LS *lua, int narg) { * @param table The table in 'textadept' to check for key in. * @param key String key to check for in table. */ -static bool l_is_ta_table_key(LS *lua, const char *table, const char *key) { +static bool l_is_ta_table_key(lua_State *lua, const char *table, + const char *key) { if (l_ta_get(lua, table)) { lua_getfield(lua, -1, key); lua_remove(lua, -2); // table @@ -542,7 +587,7 @@ bool l_is_ta_table_function(const char *table, const char *function) { bool l_call_function(int nargs, int retn=0, bool keep_return=false) { int ret = lua_pcall(lua, nargs, retn, 0); if (ret == 0) { - bool result = retn > 0 ? lua_toboolean(lua, -1) == 1 : true; + bool result = (retn > 0) ? lua_toboolean(lua, -1) == 1 : true; if (retn > 0 && !keep_return) lua_pop(lua, retn); // retn return result; } else l_handle_error(lua, NULL); @@ -555,7 +600,7 @@ bool l_call_function(int nargs, int retn=0, bool keep_return=false) { * @param index The relative index of the table to rawget from. * @param n The index in the table to rawget. */ -static int l_rawgeti_int(LS *lua, int index, int n) { +static int l_rawgeti_int(lua_State *lua, int index, int n) { lua_rawgeti(lua, index, n); int ret = static_cast<int>(lua_tointeger(lua, -1)); lua_pop(lua, 1); // integer @@ -568,8 +613,9 @@ static int l_rawgeti_int(LS *lua, int index, int n) { * @param index The relative index of the table to rawget from. * @param k String key in the table to rawget. */ -static const char *l_rawget_str(LS *lua, int index, const char *k) { - lua_pushstring(lua, k); lua_rawget(lua, index); +static const char *l_rawget_str(lua_State *lua, int index, const char *k) { + lua_pushstring(lua, k); + lua_rawget(lua, index); const char *str = lua_tostring(lua, -1); lua_pop(lua, 1); // string return str; @@ -583,7 +629,7 @@ static const char *l_rawget_str(LS *lua, int index, const char *k) { * @param arg_idx The initial stack index to start converting at. It is * incremented as parameters are read from the stack. */ -static long l_toscintillaparam(LS *lua, int type, int &arg_idx) { +static long l_toscintillaparam(lua_State *lua, int type, int &arg_idx) { if (type == tSTRING) return reinterpret_cast<long>(lua_tostring(lua, arg_idx++)); else if (type == tBOOL) @@ -594,7 +640,8 @@ static long l_toscintillaparam(LS *lua, int type, int &arg_idx) { (SCMOD_SHIFT | SCMOD_CTRL | SCMOD_ALT)) << 16); else if (type > tVOID && type < tBOOL) return luaL_checklong(lua, arg_idx++); - else return 0; + else + return 0; } /** @@ -604,7 +651,7 @@ static long l_toscintillaparam(LS *lua, int type, int &arg_idx) { * @param callback A GCallback associated with each menu item. * @param submenu Flag indicating whether or not this menu is a submenu. */ -GtkWidget *l_create_gtkmenu(LS *lua, GCallback callback, bool submenu) { +GtkWidget *l_create_gtkmenu(lua_State *lua, GCallback callback, bool submenu) { GtkWidget *menu = gtk_menu_new(), *menu_item = 0, *submenu_root = 0; const char *label; lua_getfield(lua, -1, "title"); @@ -612,7 +659,8 @@ GtkWidget *l_create_gtkmenu(LS *lua, GCallback callback, bool submenu) { label = !lua_isnil(lua, -1) ? lua_tostring(lua, -1) : "notitle"; submenu_root = gtk_menu_item_new_with_mnemonic(label); gtk_menu_item_set_submenu(GTK_MENU_ITEM(submenu_root), menu); - } lua_pop(lua, 1); // title + } + lua_pop(lua, 1); // title lua_pushnil(lua); while (lua_next(lua, -2)) { if (lua_istable(lua, -1)) { @@ -634,14 +682,17 @@ GtkWidget *l_create_gtkmenu(LS *lua, GCallback callback, bool submenu) { menu_item = gtk_image_menu_item_new_from_stock(label, NULL); else if (streq(label, "separator")) menu_item = gtk_separator_menu_item_new(); - else menu_item = gtk_menu_item_new_with_mnemonic(label); + else + menu_item = gtk_menu_item_new_with_mnemonic(label); g_signal_connect(menu_item, "activate", callback, reinterpret_cast<gpointer>(menu_id)); gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item); } } else warn("gtkmenu: { 'menu label', id_number } expected."); - } lua_pop(lua, 1); // value - } return !submenu_root ? menu : submenu_root; + } + lua_pop(lua, 1); // value + } + return !submenu_root ? menu : submenu_root; } // Notification/event handlers @@ -652,7 +703,7 @@ GtkWidget *l_create_gtkmenu(LS *lua, GCallback callback, bool submenu) { * @param lua The Lua State. * @param errmsg An additional error message to display. */ -void l_handle_error(LS *lua, const char *errmsg) { +void l_handle_error(lua_State *lua, const char *errmsg) { if (focused_editor && l_is_ta_table_function("events", "error")) { l_insert(lua, -1); // shift error message down if (errmsg) lua_pushstring(lua, errmsg); @@ -691,8 +742,14 @@ bool l_handle_keypress(int keyval, bool shift, bool control, bool alt) { return l_call_function(4, 1); } -#define l_scn_int(i, n) { lua_pushinteger(lua, i); lua_setfield(lua, idx, n); } -#define l_scn_str(s, n) { lua_pushstring(lua, s); lua_setfield(lua, idx, n); } +#define l_scn_int(i, n) { \ + lua_pushinteger(lua, i); \ + lua_setfield(lua, idx, n); \ +} +#define l_scn_str(s, n) { \ + lua_pushstring(lua, s); \ + lua_setfield(lua, idx, n); \ +} /** * Handles a Scintilla notification. @@ -718,10 +775,14 @@ void l_handle_scnnotification(SCNotification *n) { if (l_rawgeti_int(lua, -1, 1) == n->message) { if (l_rawgeti_int(lua, -1, 3) == tSTRING) { l_scn_str(reinterpret_cast<char*>(n->wParam), "wParam"); - } else { l_scn_int(static_cast<int>(n->wParam), "wParam"); } + } else { + l_scn_int(static_cast<int>(n->wParam), "wParam"); + } if (l_rawgeti_int(lua, -1, 4) == tSTRING) { l_scn_str(reinterpret_cast<char*>(n->lParam), "lParam"); - } else { l_scn_int(static_cast<int>(n->lParam), "lParam"); } + } else { + l_scn_int(static_cast<int>(n->lParam), "lParam"); + } lua_pop(lua, 2); // key and value break; } else lua_pop(lua, 1); // value @@ -782,7 +843,8 @@ void l_cec_populate() { gtk_tree_store_set(cec_store, &iter, 0, lua_tostring(lua, -1), -1); } else warn("command_entry.get_completions_for: string value expected."); lua_pop(lua, 1); // value - } lua_pop(lua, 1); // returned table + } + lua_pop(lua, 1); // returned table } // Project Manager @@ -842,7 +904,8 @@ void l_pm_populate(GtkTreeIter *initial_iter) { lua_pop(lua, 1); // display text } else warn("pm.populate: string id key must have table value."); lua_pop(lua, 1); // value - } lua_pop(lua, 1); // returned table + } + lua_pop(lua, 1); // returned table } /** @@ -971,7 +1034,7 @@ void l_find_replace_all(const char *ftext, const char *rtext) { * @param rt_type The Lua type of the Scintilla return parameter. * @param arg The index on the Lua stack where arguments to Scintilla begin. */ -LF l_call_scintilla(LS *lua, ScintillaObject *sci, int msg, +static int l_call_scintilla(lua_State *lua, ScintillaObject *sci, int msg, int p1_type, int p2_type, int rt_type, int arg) { if (!sci) luaL_error(lua, "Scintilla object not initialized."); long params[2] = {0, 0}; @@ -986,14 +1049,15 @@ LF l_call_scintilla(LS *lua, ScintillaObject *sci, int msg, params_needed = 0; } else if (p2_type == tSTRINGRESULT) { string_return = true; - params_needed = p1_type == tLENGTH ? 0 : 1; + params_needed = (p1_type == tLENGTH) ? 0 : 1; } if (params_needed > 0) params[0] = l_toscintillaparam(lua, p1_type, arg); if (params_needed > 1) params[1] = l_toscintillaparam(lua, p2_type, arg); if (string_return) { // if a string return, create a buffer for it int len = SS(sci, msg, params[0], 0); if (p1_type == tLENGTH) params[0] = len; - return_string = new char[len + 1]; return_string[len] = '\0'; + return_string = reinterpret_cast<char*>(malloc(sizeof(char) * len + 1)); + return_string[len] = '\0'; params[1] = reinterpret_cast<long>(return_string); } @@ -1003,7 +1067,7 @@ LF l_call_scintilla(LS *lua, ScintillaObject *sci, int msg, if (string_return) lua_pushstring(lua, return_string); if (rt_type == tBOOL) lua_pushboolean(lua, result); if (rt_type > tVOID && rt_type < tBOOL) lua_pushnumber(lua, result); - delete[] return_string; + g_free(return_string); return lua_gettop(lua) - arg; } @@ -1012,7 +1076,7 @@ LF l_call_scintilla(LS *lua, ScintillaObject *sci, int msg, * @param lua The Lua State. * @see l_buffer_mt_index */ -LF l_call_buffer_function(LS *lua) { +static int l_call_buffer_function(lua_State *lua) { int sci_idx = lua_upvalueindex(1); // closure from __index ScintillaObject *sci = reinterpret_cast<ScintillaObject*>(lua_touserdata(lua, sci_idx)); @@ -1032,7 +1096,7 @@ LF l_call_buffer_function(LS *lua) { * metatable to access buffer property indices. * @param lua The Lua State. */ -LF l_buffer_mt_index(LS *lua) { +static int l_buffer_mt_index(lua_State *lua) { ScintillaObject *sci = SCINTILLA(focused_editor); const char *key = luaL_checkstring(lua, 2); if (l_is_ta_table_key(lua, "buffer_functions", key)) { @@ -1050,8 +1114,10 @@ LF l_buffer_mt_index(LS *lua) { if (p1_type != tVOID) { // indexible property sptr_t doc = SS(sci, SCI_GETDOCPOINTER); lua_newtable(lua); - lua_pushstring(lua, key); lua_setfield(lua, -2, "property"); - lua_pushnumber(lua, doc); lua_setfield(lua, -2, "doc_pointer"); + lua_pushstring(lua, key); + lua_setfield(lua, -2, "property"); + lua_pushnumber(lua, doc); + lua_setfield(lua, -2, "doc_pointer"); l_mt(lua, "_bufferp_mt", l_bufferp_mt_index, l_bufferp_mt_newindex); } else return l_call_scintilla(lua, sci, msg, p1_type, tVOID, rt_type, 2); } else lua_rawget(lua, 1); @@ -1067,55 +1133,60 @@ LF l_buffer_mt_index(LS *lua) { * For setter properties, it is 3 because the index is not an argument. For * getter and setter properties, it is 2 because the index is an argument. */ -LF l_bufferp_mt_(LS *lua, int n, const char *prop, int arg) { +static int l_bufferp_mt_(lua_State *lua, int n, const char *prop, int arg) { ScintillaObject *sci = SCINTILLA(focused_editor); if (l_is_ta_table_key(lua, "buffer_properties", prop)) { l_check_focused_buffer(lua, 1); int msg = l_rawgeti_int(lua, -1, n); // getter (1) or setter (2) - int rt_type = n == 1 ? l_rawgeti_int(lua, -1, 3) : tVOID; - int p1_type = l_rawgeti_int(lua, -1, n == 1 ? 4 : 3); - int p2_type = n == 2 ? l_rawgeti_int(lua, -1, 4) : tVOID; + int rt_type = (n == 1) ? l_rawgeti_int(lua, -1, 3) : tVOID; + int p1_type = l_rawgeti_int(lua, -1, (n == 1) ? 4 : 3); + int p2_type = (n == 2) ? l_rawgeti_int(lua, -1, 4) : tVOID; if (n == 2 && (p2_type != tVOID || (p2_type == tVOID && p1_type == tSTRING))) { - int temp = p1_type; p1_type = p2_type; p2_type = temp; // swap + int temp = p1_type; + p1_type = p2_type; + p2_type = temp; } if (msg != 0) return l_call_scintilla(lua, sci, msg, p1_type, p2_type, rt_type, arg); - else luaL_error(lua, "The property '%s' is %s-only.", prop, - n == 1 ? "write" : "read"); + else + luaL_error(lua, "The property '%s' is %s-only.", prop, + (n == 1) ? "write" : "read"); } else (lua_gettop(lua) > 2) ? lua_rawset(lua, 1) : lua_rawget(lua, 1); return 0; } -LF l_buffer_mt_newindex(LS *lua) { - return streq(lua_tostring(lua, 2), "doc_pointer") - ? luaL_error(lua, "'doc_pointer' is read-only") - : l_bufferp_mt_(lua, 2, lua_tostring(lua, 2), 3); +static int l_buffer_mt_newindex(lua_State *lua) { + if (streq(lua_tostring(lua, 2), "doc_pointer")) + return luaL_error(lua, "'doc_pointer' is read-only"); + else + return l_bufferp_mt_(lua, 2, lua_tostring(lua, 2), 3); } -LF l_bufferp_mt_index(LS *lua) { +static int l_bufferp_mt_index(lua_State *lua) { return l_bufferp_mt_(lua, 1, l_rawget_str(lua, 1, "property"), 2); } -LF l_bufferp_mt_newindex(LS *lua) { +static int l_bufferp_mt_newindex(lua_State *lua) { return l_bufferp_mt_(lua, 2, l_rawget_str(lua, 1, "property"), 2); } -LF l_view_mt_index(LS *lua) { +static int l_view_mt_index(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "doc_pointer")) lua_pushnumber(lua, SS(SCINTILLA(l_checkview(lua, 1)), SCI_GETDOCPOINTER)); else if (streq(key, "size")) { GtkWidget *editor = l_checkview(lua, 1); - if (GTK_IS_PANED(gtk_widget_get_parent(editor))) - lua_pushnumber(lua, - gtk_paned_get_position(GTK_PANED(gtk_widget_get_parent(editor)))); - else lua_pushnil(lua); + if (GTK_IS_PANED(gtk_widget_get_parent(editor))) { + int pos = + gtk_paned_get_position(GTK_PANED(gtk_widget_get_parent(editor))); + lua_pushnumber(lua, pos); + } else lua_pushnil(lua); } else lua_rawget(lua, 1); return 1; } -LF l_view_mt_newindex(LS *lua) { +static int l_view_mt_newindex(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "doc_pointer") || streq(key, "widget_pointer")) luaL_error(lua, "'%s' is read-only.", key); @@ -1128,28 +1199,30 @@ LF l_view_mt_newindex(LS *lua) { return 0; } -LF l_ta_mt_index(LS *lua) { +static int l_ta_mt_index(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "title")) lua_pushstring(lua, gtk_window_get_title(GTK_WINDOW(window))); else if (streq(key, "focused_doc_pointer")) lua_pushnumber(lua, SS(SCINTILLA(focused_editor), SCI_GETDOCPOINTER)); else if (streq(key, "clipboard_text")) { - char *text = gtk_clipboard_wait_for_text( - gtk_clipboard_get(GDK_SELECTION_CLIPBOARD)); + char *text = + gtk_clipboard_wait_for_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD)); if (text) lua_pushstring(lua, text); g_free(text); } else if (streq(key, "size")) { lua_newtable(lua); int width, height; gtk_window_get_size(GTK_WINDOW(window), &width, &height); - lua_pushnumber(lua, width); lua_rawseti(lua, -2, 1); - lua_pushnumber(lua, height); lua_rawseti(lua, -2, 2); + lua_pushnumber(lua, width); + lua_rawseti(lua, -2, 1); + lua_pushnumber(lua, height); + lua_rawseti(lua, -2, 2); } else lua_rawget(lua, 1); return 1; } -LF l_ta_mt_newindex(LS *lua) { +static int l_ta_mt_newindex(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "title")) gtk_window_set_title(GTK_WINDOW(window), lua_tostring(lua, 3)); @@ -1169,12 +1242,14 @@ LF l_ta_mt_newindex(LS *lua) { GtkWidget *menu_item = l_togtkwidget(lua, -1); gtk_menu_bar_append(GTK_MENU_BAR(menubar), menu_item); lua_pop(lua, 1); // value - } set_menubar(menubar); + } + set_menubar(menubar); } else if (streq(key, "size")) { const char *errmsg = "textadept.size must be a table ({ width, height })."; if (!lua_istable(lua, 3) || lua_objlen(lua, 3) != 2) luaL_error(lua, errmsg); - lua_rawgeti(lua, 3, 1); lua_rawgeti(lua, 3, 2); + lua_rawgeti(lua, 3, 1); + lua_rawgeti(lua, 3, 2); int width = static_cast<int>(lua_tonumber(lua, -2)); int height = static_cast<int>(lua_tonumber(lua, -1)); lua_pop(lua, 2); // width, height @@ -1184,30 +1259,32 @@ LF l_ta_mt_newindex(LS *lua) { return 0; } -LF l_pm_mt_index(LS *lua) { +static int l_pm_mt_index(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "entry_text")) lua_pushstring(lua, gtk_entry_get_text(GTK_ENTRY(pm_entry))); - else if (streq(key, "width")) - lua_pushnumber(lua, - gtk_paned_get_position(GTK_PANED(gtk_widget_get_parent(pm_container)))); - else lua_rawget(lua, 1); + else if (streq(key, "width")) { + int pos = + gtk_paned_get_position(GTK_PANED(gtk_widget_get_parent(pm_container))); + lua_pushnumber(lua, pos); + } else lua_rawget(lua, 1); return 1; } -LF l_pm_mt_newindex(LS *lua) { +static int l_pm_mt_newindex(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "entry_text")) gtk_entry_set_text(GTK_ENTRY(pm_entry), lua_tostring(lua, 3)); else if (streq(key, "width")) gtk_paned_set_position(GTK_PANED(gtk_widget_get_parent(pm_container)), - luaL_checkinteger(lua, 3)); - else lua_rawset(lua, 1); + luaL_checkinteger(lua, 3)); + else + lua_rawset(lua, 1); return 0; } #define toggled(w) gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(w)) -LF l_find_mt_index(LS *lua) { +static int l_find_mt_index(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "find_entry_text")) lua_pushstring(lua, gtk_entry_get_text(GTK_ENTRY(find_entry))); @@ -1219,12 +1296,13 @@ LF l_find_mt_index(LS *lua) { lua_pushboolean(lua, toggled(whole_word_opt)); else if (streq(key, "lua")) lua_pushboolean(lua, toggled(lua_opt)); - else lua_rawget(lua, 1); + else + lua_rawget(lua, 1); return 1; } #define toggle(w, b) gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(w), b) -LF l_find_mt_newindex(LS *lua) { +static int l_find_mt_newindex(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "find_entry_text")) gtk_entry_set_text(GTK_ENTRY(find_entry), lua_tostring(lua, 3)); @@ -1236,36 +1314,39 @@ LF l_find_mt_newindex(LS *lua) { toggle(whole_word_opt, lua_toboolean(lua, -1) ? TRUE : FALSE); else if (streq(key, "lua")) toggle(lua_opt, lua_toboolean(lua, -1) ? TRUE : FALSE); - else lua_rawset(lua, 1); + else + lua_rawset(lua, 1); return 0; } -LF l_ce_mt_index(LS *lua) { +static int l_ce_mt_index(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "entry_text")) lua_pushstring(lua, gtk_entry_get_text(GTK_ENTRY(command_entry))); - else lua_rawget(lua, 1); + else + lua_rawget(lua, 1); return 1; } -LF l_ce_mt_newindex(LS *lua) { +static int l_ce_mt_newindex(lua_State *lua) { const char *key = lua_tostring(lua, 2); if (streq(key, "entry_text")) gtk_entry_set_text(GTK_ENTRY(command_entry), lua_tostring(lua, 3)); - else lua_rawset(lua, 1); + else + lua_rawset(lua, 1); return 0; } // Lua CFunctions. For documentation, consult the LuaDoc. -LF l_cf_ta_buffer_new(LS *lua) { +static int l_cf_ta_buffer_new(lua_State *lua) { new_scintilla_buffer(SCINTILLA(focused_editor), true, true); if (!l_ta_get(lua, "buffers")) luaL_error(lua, buffers_dne); lua_rawgeti(lua, -1, lua_objlen(lua, -1)); return 1; } -LF l_cf_buffer_delete(LS *lua) { +static int l_cf_buffer_delete(lua_State *lua) { l_check_focused_buffer(lua, 1); sptr_t doc = l_checkdocpointer(lua, 1); if (!l_ta_get(lua, "buffers")) luaL_error(lua, buffers_dne); @@ -1278,15 +1359,15 @@ LF l_cf_buffer_delete(LS *lua) { return 0; } -LF l_cf_buffer_find(LS *lua) { +static int l_cf_buffer_find(lua_State *lua) { l_check_focused_buffer(lua, 1); TextToFind ttf = {{0, 0}, 0, {0, 0}}; ttf.lpstrText = const_cast<char*>(luaL_checkstring(lua, 2)); int args = lua_gettop(lua), flags = 0; if (args > 2) flags = luaL_checkinteger(lua, 3); if (args > 3) ttf.chrg.cpMin = luaL_checkinteger(lua, 4); - ttf.chrg.cpMax = args > 4 ? luaL_checkinteger(lua, 5) - : SS(SCINTILLA(focused_editor), SCI_GETLENGTH); + ttf.chrg.cpMax = (args > 4) ? luaL_checkinteger(lua, 5) + : SS(SCINTILLA(focused_editor), SCI_GETLENGTH); int pos = SS(SCINTILLA(focused_editor), SCI_FINDTEXT, flags, reinterpret_cast<sptr_t>(&ttf)); if (pos > -1) { @@ -1296,7 +1377,7 @@ LF l_cf_buffer_find(LS *lua) { } else return 0; } -LF l_cf_buffer_text_range(LS *lua) { +static int l_cf_buffer_text_range(lua_State *lua) { l_check_focused_buffer(lua, 1); #ifndef MAC TextRange tr; @@ -1305,22 +1386,23 @@ LF l_cf_buffer_text_range(LS *lua) { #endif tr.chrg.cpMin = luaL_checkinteger(lua, 2); tr.chrg.cpMax = luaL_checkinteger(lua, 3); - char *text = new char[tr.chrg.cpMax - tr.chrg.cpMin + 1]; + int length = tr.chrg.cpMax - tr.chrg.cpMin; + char *text = reinterpret_cast<char*>(malloc(sizeof(char) * length + 1)); tr.lpstrText = text; SS(SCINTILLA(focused_editor), SCI_GETTEXTRANGE, 0, reinterpret_cast<long>(&tr)); lua_pushstring(lua, text); - delete[] text; + g_free(text); return 1; } -LF l_cf_view_focus(LS *lua) { +static int l_cf_view_focus(lua_State *lua) { GtkWidget *editor = l_checkview(lua, 1); gtk_widget_grab_focus(editor); return 0; } -LF l_cf_view_split(LS *lua) { +static int l_cf_view_split(lua_State *lua) { GtkWidget *editor = l_checkview(lua, 1); bool vertical = true; if (lua_gettop(lua) > 1) vertical = lua_toboolean(lua, 2) == 1; @@ -1330,7 +1412,7 @@ LF l_cf_view_split(LS *lua) { return 2; } -LF l_cf_view_unsplit(LS *lua) { +static int l_cf_view_unsplit(lua_State *lua) { GtkWidget *editor = l_checkview(lua, 1); lua_pushboolean(lua, unsplit_window(editor)); return 1; @@ -1338,53 +1420,55 @@ LF l_cf_view_unsplit(LS *lua) { #define child1(p) gtk_paned_get_child1(GTK_PANED(p)) #define child2(p) gtk_paned_get_child2(GTK_PANED(p)) +#define editor_dpi(e) \ + l_get_docpointer_index(SS(SCINTILLA(e), SCI_GETDOCPOINTER)) -void l_create_entry(LS *lua, GtkWidget *c1, GtkWidget *c2, bool vertical) { +void l_create_entry(lua_State *lua, GtkWidget *c1, GtkWidget *c2, + bool vertical) { lua_newtable(lua); if (GTK_IS_PANED(c1)) l_create_entry(lua, child1(c1), child2(c1), GTK_IS_HPANED(c1) == 1); else - lua_pushinteger(lua, - l_get_docpointer_index(SS(SCINTILLA(c1), SCI_GETDOCPOINTER))); + lua_pushinteger(lua, editor_dpi(c1)); lua_rawseti(lua, -2, 1); if (GTK_IS_PANED(c2)) l_create_entry(lua, child1(c2), child2(c2), GTK_IS_HPANED(c2) == 1); else - lua_pushinteger(lua, - l_get_docpointer_index(SS(SCINTILLA(c2), SCI_GETDOCPOINTER))); + lua_pushinteger(lua, editor_dpi(c2)); lua_rawseti(lua, -2, 2); - lua_pushboolean(lua, vertical); lua_setfield(lua, -2, "vertical"); + lua_pushboolean(lua, vertical); + lua_setfield(lua, -2, "vertical"); int size = gtk_paned_get_position(GTK_PANED(gtk_widget_get_parent(c1))); - lua_pushinteger(lua, size); lua_setfield(lua, -2, "size"); + lua_pushinteger(lua, size); + lua_setfield(lua, -2, "size"); } -LF l_cf_ta_get_split_table(LS *lua) { +static int l_cf_ta_get_split_table(lua_State *lua) { if (!l_ta_get(lua, "views")) luaL_error(lua, views_dne); if (lua_objlen(lua, -1) > 1) { GtkWidget *pane = gtk_widget_get_parent(focused_editor); while (GTK_IS_PANED(gtk_widget_get_parent(pane))) pane = gtk_widget_get_parent(pane); l_create_entry(lua, child1(pane), child2(pane), GTK_IS_HPANED(pane) == 1); - } else lua_pushinteger(lua, l_get_docpointer_index( - SS(SCINTILLA(focused_editor), SCI_GETDOCPOINTER))); + } else lua_pushinteger(lua, editor_dpi(focused_editor)); return 1; } -LF l_cf_ta_goto_(LS *lua, GtkWidget *editor, bool buffer) { +static int l_cf_ta_goto_(lua_State *lua, GtkWidget *editor, bool buffer) { int n = static_cast<int>(luaL_checkinteger(lua, 1)); - bool absolute = lua_gettop(lua) > 1 ? lua_toboolean(lua, 2) == 1 : true; + bool absolute = (lua_gettop(lua) > 1) ? lua_toboolean(lua, 2) == 1 : true; buffer ? l_goto_scintilla_buffer(editor, n, absolute) : l_goto_scintilla_window(editor, n, absolute); return 0; } -LF l_cf_ta_goto_window(LS *lua) { +static int l_cf_ta_goto_window(lua_State *lua) { return l_cf_ta_goto_(lua, focused_editor, false); } // If the indexed view is not currently focused, temporarily focus it so calls // to handlers will not throw 'indexed buffer is not the focused one' error. -LF l_cf_view_goto_buffer(LS *lua) { +static int l_cf_view_goto_buffer(lua_State *lua) { GtkWidget *editor = l_checkview(lua, 1); bool switch_focus = editor != focused_editor; GtkWidget *orig_focused_editor = focused_editor; @@ -1408,14 +1492,14 @@ static void t_menu_activate(GtkWidget *menu_item, gpointer menu_id) { g_free(param); } -LF l_cf_ta_gtkmenu(LS *lua) { +static int l_cf_ta_gtkmenu(lua_State *lua) { luaL_checktype(lua, 1, LUA_TTABLE); GtkWidget *menu = l_create_gtkmenu(lua, G_CALLBACK(t_menu_activate), false); lua_pushlightuserdata(lua, const_cast<GtkWidget*>(menu)); return 1; } -LF l_cf_ta_popupmenu(LS *lua) { +static int l_cf_ta_popupmenu(lua_State *lua) { if (!lua_isuserdata(lua, 1)) luaL_error(lua, "Menu userdata expected."); GtkWidget *menu = l_togtkwidget(lua, 1); gtk_widget_show_all(menu); @@ -1423,20 +1507,26 @@ LF l_cf_ta_popupmenu(LS *lua) { return 0; } -LF l_cf_ta_reset(LS *lua) { +static int l_cf_ta_reset(lua_State *lua) { l_handle_event("resetting"); - lua_getglobal(lua, "buffer"); lua_setfield(lua, LUA_REGISTRYINDEX, "buffer"); - lua_getglobal(lua, "view"); lua_setfield(lua, LUA_REGISTRYINDEX, "view"); + lua_getglobal(lua, "buffer"); + lua_setfield(lua, LUA_REGISTRYINDEX, "buffer"); + lua_getglobal(lua, "view"); + lua_setfield(lua, LUA_REGISTRYINDEX, "view"); l_init(0, NULL, true); - lua_pushboolean(lua, true); lua_setglobal(lua, "RESETTING"); + lua_pushboolean(lua, true); + lua_setglobal(lua, "RESETTING"); l_load_script("init.lua"); - lua_pushnil(lua); lua_setglobal(lua, "RESETTING"); - lua_getfield(lua, LUA_REGISTRYINDEX, "buffer"); lua_setglobal(lua, "buffer"); - lua_getfield(lua, LUA_REGISTRYINDEX, "view"); lua_setglobal(lua, "view"); + lua_pushnil(lua); + lua_setglobal(lua, "RESETTING"); + lua_getfield(lua, LUA_REGISTRYINDEX, "buffer"); + lua_setglobal(lua, "buffer"); + lua_getfield(lua, LUA_REGISTRYINDEX, "view"); + lua_setglobal(lua, "view"); return 0; } -LF l_cf_ta_quit(LS *) { +static int l_cf_ta_quit(lua_State *) { GdkEventAny event; event.type = GDK_DELETE; event.window = window->window; @@ -1445,47 +1535,47 @@ LF l_cf_ta_quit(LS *) { return 0; } -LF l_cf_pm_focus(LS *) { +static int l_cf_pm_focus(lua_State *) { pm_toggle_focus(); return 0; } -LF l_cf_pm_clear(LS *) { +static int l_cf_pm_clear(lua_State *) { gtk_tree_store_clear(pm_store); return 0; } -LF l_cf_pm_activate(LS *) { +static int l_cf_pm_activate(lua_State *) { g_signal_emit_by_name(G_OBJECT(pm_entry), "activate"); return 0; } -LF l_cf_find_focus(LS *) { +static int l_cf_find_focus(lua_State *) { find_toggle_focus(); return 0; } -LF l_cf_call_find_next(LS *) { +static int l_cf_call_find_next(lua_State *) { g_signal_emit_by_name(G_OBJECT(fnext_button), "clicked"); return 0; } -LF l_cf_call_find_prev(LS *) { +static int l_cf_call_find_prev(lua_State *) { g_signal_emit_by_name(G_OBJECT(fprev_button), "clicked"); return 0; } -LF l_cf_call_replace(LS *) { +static int l_cf_call_replace(lua_State *) { g_signal_emit_by_name(G_OBJECT(r_button), "clicked"); return 0; } -LF l_cf_call_replace_all(LS *) { +static int l_cf_call_replace_all(lua_State *) { g_signal_emit_by_name(G_OBJECT(ra_button), "clicked"); return 0; } -LF l_cf_ce_focus(LS *) { +static int l_cf_ce_focus(lua_State *) { ce_toggle_focus(); return 0; } diff --git a/src/textadept.c b/src/textadept.c index 4d08af59..fe4fab1d 100644 --- a/src/textadept.c +++ b/src/textadept.c @@ -10,10 +10,13 @@ using namespace Scintilla; #endif +#if WIN32 || MAC +char *textadept_home; +#endif + // Textadept -GtkWidget - *window, *focused_editor, *command_entry, - *menubar, *statusbar, *docstatusbar; +GtkWidget *window, *focused_editor, *command_entry, *menubar, *statusbar, + *docstatusbar; GtkEntryCompletion *command_entry_completion; GtkTreeStore *cec_store; @@ -56,19 +59,15 @@ static gbool pm_popup_menu(GtkWidget *, gpointer); static void pm_menu_activate(GtkWidget *menu_item, gpointer menu_id); // Find/Replace -GtkWidget *findbox, *find_entry, *replace_entry; -GtkWidget *fnext_button, *fprev_button, *r_button, *ra_button; -GtkWidget *match_case_opt, *whole_word_opt, /**incremental_opt,*/ *lua_opt; +GtkWidget *findbox, *find_entry, *replace_entry, *fnext_button, *fprev_button, + *r_button, *ra_button, *match_case_opt, *whole_word_opt, + /**incremental_opt,*/ *lua_opt; GtkAttachOptions ao_normal = static_cast<GtkAttachOptions>(GTK_SHRINK | GTK_FILL), ao_expand = static_cast<GtkAttachOptions>(GTK_EXPAND | GTK_FILL); static void button_clicked(GtkWidget *button, gpointer); -#if WIN32 || MAC -char *textadept_home; -#endif - /** * Runs Textadept in Linux or Mac. * Inits the Lua State, creates the user interface, and loads the core/init.lua @@ -84,9 +83,9 @@ int main(int argc, char **argv) { CFURLRef bundle_url = CFBundleCopyBundleURL(bundle); cfurlref_to_char(bundle_url, bundle_path, FILENAME_MAX); char *res_path = g_strconcat(bundle_path, "/Contents/Resources/", NULL); - textadept_home = static_cast<char*>(res_path); - g_free(bundle_path); - CFRelease(bundle_url); + textadept_home = static_cast<char*>(res_path); + g_free(bundle_path); + CFRelease(bundle_url); } else textadept_home = ""; #endif gtk_init(&argc, &argv); @@ -132,18 +131,18 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR lpCmdLine, int) { void create_ui() { GList *icons = NULL; const char *icon_files[] = { - "ta_16x16.png", "ta_32x32.png", "ta_48x48.png", - "ta_64x64.png", "ta_128x128.png" + "ta_16x16.png", "ta_32x32.png", "ta_48x48.png", "ta_64x64.png", + "ta_128x128.png" }; for (int i = 0; i < 5; i++) { - char *icon_file = g_strconcat(textadept_home, "/core/images/", - icon_files[i], NULL); + char *icon_file = + g_strconcat(textadept_home, "/core/images/", icon_files[i], NULL); GdkPixbuf *pb = gdk_pixbuf_new_from_file(icon_file, NULL); if (pb) icons = g_list_prepend(icons, pb); g_free(icon_file); } gtk_window_set_default_icon_list(icons); - g_list_foreach(icons, (GFunc) g_object_unref, NULL); + g_list_foreach(icons, reinterpret_cast<GFunc>(g_object_unref), NULL); g_list_free(icons); window = gtk_window_new(GTK_WINDOW_TOPLEVEL); @@ -236,7 +235,8 @@ GtkWidget *new_scintilla_window(sptr_t buffer_id) { signal(editor, "command", t_command); signal(editor, SCINTILLA_NOTIFY, t_notification); l_add_scintilla_window(editor); - gtk_widget_grab_focus(editor); focused_editor = editor; + gtk_widget_grab_focus(editor); + focused_editor = editor; if (buffer_id) { SS(SCINTILLA(editor), SCI_SETDOCPOINTER, 0, buffer_id); new_scintilla_buffer(SCINTILLA(editor), false, false); @@ -310,8 +310,8 @@ void split_window(GtkWidget *editor, bool vertical) { int first_line = SS(SCINTILLA(editor), SCI_GETFIRSTVISIBLELINE); int current_pos = SS(SCINTILLA(editor), SCI_GETCURRENTPOS); int anchor = SS(SCINTILLA(editor), SCI_GETANCHOR); - int middle = (vertical ? editor->allocation.width - : editor->allocation.height) / 2; + int middle = + (vertical ? editor->allocation.width : editor->allocation.height) / 2; sptr_t curdoc = SS(SCINTILLA(editor), SCI_GETDOCPOINTER); GtkWidget *neweditor = new_scintilla_window(curdoc); @@ -358,7 +358,8 @@ bool unsplit_window(GtkWidget *editor) { if (!GTK_IS_PANED(pane)) return false; GtkWidget *other = gtk_paned_get_child1(GTK_PANED(pane)); if (other == editor) other = gtk_paned_get_child2(GTK_PANED(pane)); - g_object_ref(editor); g_object_ref(other); + g_object_ref(editor); + g_object_ref(other); gtk_container_remove(GTK_CONTAINER(pane), editor); gtk_container_remove(GTK_CONTAINER(pane), other); GTK_IS_PANED(other) ? remove_scintilla_windows_in_pane(other) @@ -374,7 +375,8 @@ bool unsplit_window(GtkWidget *editor) { gtk_container_add(GTK_CONTAINER(parent), editor); gtk_widget_show_all(parent); gtk_widget_grab_focus(GTK_WIDGET(editor)); - g_object_unref(editor); g_object_unref(other); + g_object_unref(editor); + g_object_unref(other); return true; } @@ -415,11 +417,13 @@ void set_statusbar_text(const char *text, bool docbar) { */ void ce_toggle_focus() { if (!GTK_WIDGET_HAS_FOCUS(command_entry)) { - gtk_widget_hide(statusbar); gtk_widget_hide(docstatusbar); + gtk_widget_hide(statusbar); + gtk_widget_hide(docstatusbar); gtk_widget_show(command_entry); gtk_widget_grab_focus(command_entry); } else { - gtk_widget_show(statusbar); gtk_widget_show(docstatusbar); + gtk_widget_show(statusbar); + gtk_widget_show(docstatusbar); gtk_widget_hide(command_entry); gtk_widget_grab_focus(focused_editor); } @@ -581,8 +585,8 @@ static OSErr w_ae_open(const AppleEvent *event, AppleEvent*, long) { for (int i = 1; i <= count; i++) { FSRef fsref; char *path = static_cast<char*>(malloc(FILENAME_MAX * sizeof(char))); - AEGetNthPtr( - &file_list, i, typeFSRef, NULL, NULL, &fsref, sizeof(FSRef), NULL); + AEGetNthPtr(&file_list, i, typeFSRef, NULL, NULL, &fsref, sizeof(FSRef), + NULL); CFURLRef url = CFURLCreateFromFSRef(kCFAllocatorDefault, &fsref); if (url) { cfurlref_to_char(url, path, FILENAME_MAX); @@ -672,6 +676,7 @@ GtkWidget *pm_create_ui() { signal(pm_view, "row_activated", pm_row_activated); signal(pm_view, "button_press_event", pm_button_press); signal(pm_view, "popup-menu", pm_popup_menu); + return pm_container; } @@ -764,8 +769,8 @@ void pm_process_selected_menu_item(GtkWidget *menu_item, int menu_id) { * window. */ void pm_toggle_focus() { - gtk_widget_grab_focus( - GTK_WIDGET_HAS_FOCUS(focused_editor) ? pm_entry : focused_editor); + gtk_widget_grab_focus(GTK_WIDGET_HAS_FOCUS(focused_editor) ? pm_entry + : focused_editor); } /** @@ -794,10 +799,14 @@ static int pm_sort_iter_compare_func(GtkTreeModel *model, GtkTreeIter *a, const char *a_text, *b_text; gtk_tree_model_get(model, a, 1, &a_text, -1); gtk_tree_model_get(model, b, 1, &b_text, -1); - if (a_text == NULL && b_text == NULL) return 0; - else if (a_text == NULL) return -1; - else if (b_text == NULL) return 1; - else return strcasecmp(a_text, b_text); + if (a_text == NULL && b_text == NULL) + return 0; + else if (a_text == NULL) + return -1; + else if (b_text == NULL) + return 1; + else + return strcasecmp(a_text, b_text); } // Signals @@ -860,7 +869,8 @@ static void pm_row_activated(GtkTreeView *, GtkTreePath *, GtkTreeViewColumn *, */ static gbool pm_button_press(GtkTreeView *, GdkEventButton *event, gpointer) { if (event->type != GDK_BUTTON_PRESS || event->button != 3) return FALSE; - pm_popup_context_menu(event); return TRUE; + pm_popup_context_menu(event); + return TRUE; } /** @@ -869,7 +879,8 @@ static gbool pm_button_press(GtkTreeView *, GdkEventButton *event, gpointer) { * @see pm_popup_context_menu */ static gbool pm_popup_menu(GtkWidget *, gpointer) { - pm_popup_context_menu(NULL); return TRUE; + pm_popup_context_menu(NULL); + return TRUE; } /** @@ -884,8 +895,6 @@ static void pm_menu_activate(GtkWidget *menu_item, gpointer menu_id) { #define attach(w, x1, x2, y1, y2, xo, yo, xp, yp) \ gtk_table_attach(GTK_TABLE(findbox), w, x1, x2, y1, y2, xo, yo, xp, yp) -#define find_text gtk_entry_get_text(GTK_ENTRY(find_entry)) -#define repl_text gtk_entry_get_text(GTK_ENTRY(replace_entry)) /** * Creates the Find/Replace text frame. @@ -965,6 +974,8 @@ void find_toggle_focus() { * Performs the appropriate action depending on the button clicked. */ static void button_clicked(GtkWidget *button, gpointer) { + const char *find_text = gtk_entry_get_text(GTK_ENTRY(find_entry)); + const char *repl_text = gtk_entry_get_text(GTK_ENTRY(replace_entry)); if (button == ra_button) l_find_replace_all(find_text, repl_text); else if (button == r_button) { diff --git a/src/textadept.h b/src/textadept.h index bbc49767..5ccea139 100644 --- a/src/textadept.h +++ b/src/textadept.h @@ -30,12 +30,11 @@ using namespace Scintilla; #endif // globals -extern GtkWidget - *window, *focused_editor, *command_entry, - *pm_container, *pm_entry, *pm_view, - *findbox, *find_entry, *replace_entry, - *fnext_button, *fprev_button, *r_button, *ra_button, - *match_case_opt, *whole_word_opt, /**incremental_opt,*/ *lua_opt; +extern GtkWidget *window, *focused_editor, *command_entry, *pm_container, + *pm_entry, *pm_view, *findbox, *find_entry, *replace_entry, + *fnext_button, *fprev_button, *r_button, *ra_button, + *match_case_opt, *whole_word_opt, /**incremental_opt,*/ + *lua_opt; extern GtkEntryCompletion *command_entry_completion; extern GtkTreeStore *cec_store, *pm_store; extern lua_State *lua; |