diff options
author | 2009-01-08 15:54:26 -0500 | |
---|---|---|
committer | 2009-01-08 15:54:26 -0500 | |
commit | 0194a626fcb17bcb037341fc6c2f586f54d26035 (patch) | |
tree | 9b54a08a6f30ce597b751190c113b934a3bb2b6d /src | |
parent | 1267b118ffebf604639ca7a56660a1b4b69a2b85 (diff) | |
download | textadept-0194a626fcb17bcb037341fc6c2f586f54d26035.tar.gz textadept-0194a626fcb17bcb037341fc6c2f586f54d26035.zip |
Reformatted all C and Lua code to a single standard for each language.
Diffstat (limited to 'src')
-rw-r--r-- | src/lua_interface.c | 484 | ||||
-rw-r--r-- | src/textadept.c | 85 | ||||
-rw-r--r-- | src/textadept.h | 11 |
3 files changed, 340 insertions, 240 deletions
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; |