aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/textadept.c
diff options
context:
space:
mode:
authorGravatar mitchell <70453897+667e-11@users.noreply.github.com>2020-07-25 16:09:09 -0400
committerGravatar mitchell <70453897+667e-11@users.noreply.github.com>2020-07-25 16:09:09 -0400
commit876596d8951eafebce3af8381f32ede291127304 (patch)
tree1c844c6d5f6e956813d0713dafb54b09c6255556 /src/textadept.c
parent8fd378c09636e3c9a2275dcd1b9195a21a38e5de (diff)
More code cleanup.
Use C99's stdbool.h.
Diffstat (limited to 'src/textadept.c')
-rw-r--r--src/textadept.c302
1 files changed, 152 insertions, 150 deletions
diff --git a/src/textadept.c b/src/textadept.c
index 9a3ae33e..d5b26e97 100644
--- a/src/textadept.c
+++ b/src/textadept.c
@@ -11,6 +11,7 @@
#include <iconv.h>
#include <math.h> // for fmax
//#include <stdarg.h>
+#include <stdbool.h>
//#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -75,9 +76,9 @@ typedef GtkWidget Scintilla;
#define ID "\\\\.\\pipe\\textadept.editor"
// Win32 single-instance functionality.
#define g_application_command_line_get_arguments(_,__) \
- g_strsplit((char *)buf, "\n", 0); argc = g_strv_length(argv)
+ g_strsplit(buf, "\n", 0); argc = g_strv_length(argv)
#define g_application_command_line_get_cwd(_) argv[0]
-#define g_application_register(_,__,___) TRUE
+#define g_application_register(_,__,___) true
#define g_application_get_is_remote(_) \
(WaitNamedPipe(ID, NMPWAIT_WAIT_FOREVER) != 0)
#define gtk_main() \
@@ -154,9 +155,9 @@ static CDKENTRY *find_entry, *repl_entry, *focused_entry;
static char *find_text, *repl_text, *find_label, *repl_label, *prev_text;
#define set_entry_text(entry, text) copyfree(&entry, text)
typedef enum {find_next, replace, find_prev, replace_all} FindButton;
-static int find_options[4];
-static int *match_case = &find_options[0], *whole_word = &find_options[1],
- *regex = &find_options[2], *in_files = &find_options[3];
+static bool find_options[4], *match_case = &find_options[0],
+ *whole_word = &find_options[1], *regex = &find_options[2],
+ *in_files = &find_options[3];
static char *button_labels[4], *option_labels[4];
typedef char *ListStore;
static ListStore find_history[10], repl_history[10];
@@ -164,7 +165,7 @@ static ListStore find_history[10], repl_history[10];
// Use pointer arithmetic to highlight/unhighlight options as necessary.
#define toggle(o, on) do { \
if (*o != on) *o = on, option_labels[o - match_case] += *o ? -4 : 4; \
-} while (0)
+} while (false)
#define set_label_text(label, text) copyfree(&label, text)
#define set_button_label(button, label) copyfree(&button_labels[button], label)
// Prepend "</R>" to each option label because pointer arithmetic will be used
@@ -175,24 +176,24 @@ static ListStore find_history[10], repl_history[10];
if (option_labels[i] && !*option) option_labels[i] -= 4; \
copyfree(&option_labels[i], lua_tostring(L, -1)); \
if (!*option) option_labels[i] += 4; \
-} while (0)
+} while (false)
// Curses command entry and statusbar.
-static int command_entry_focused, statusbar_length[2];
+static bool command_entry_focused;
+int statusbar_length[2];
#endif
// Lua objects.
static lua_State *lua;
#if CURSES
-static int quitting;
+static bool quitting;
#endif
-static int initing, closing;
-static int show_tabs = TRUE, tab_sync;
+static bool initing, closing, show_tabs = true, tab_sync;
enum {SVOID, SINT, SLEN, SINDEX, SCOLOR, SBOOL, SKEYMOD, SSTRING, SSTRINGRET};
// Forward declarations.
static void new_buffer(sptr_t);
static Scintilla *new_view(sptr_t);
-static int init_lua(lua_State *, int, char **, int);
+static bool init_lua(lua_State *, int, char **, bool);
LUALIB_API int luaopen_lpeg(lua_State *), luaopen_lfs(lua_State *);
LUALIB_API int os_spawn_pushfds(lua_State *), os_spawn_readfds(lua_State *);
@@ -205,11 +206,11 @@ LUALIB_API int os_spawn_pushfds(lua_State *), os_spawn_readfds(lua_State *);
* LUA_TTABLE types, push the data values to the stack and give the value
* returned by luaL_ref(); luaL_unref() will be called appropriately. The list
* must be terminated with a -1.
- * @return TRUE or FALSE depending on the boolean value returned by the event
+ * @return true or false depending on the boolean value returned by the event
* handler, if any.
*/
-static int emit(lua_State *L, const char *name, ...) {
- int ret = FALSE;
+static bool emit(lua_State *L, const char *name, ...) {
+ bool ret = false;
if (lua_getglobal(L, "events") != LUA_TTABLE) return (lua_pop(L, 1), ret);
if (lua_getfield(L, -1, "emit") != LUA_TFUNCTION) return (lua_pop(L, 2), ret);
lua_pushstring(L, name);
@@ -234,8 +235,7 @@ static int emit(lua_State *L, const char *name, ...) {
const char *argv[] = {"--title", "Error", "--text", lua_tostring(L, -1)};
free(gtdialog(GTDIALOG_TEXTBOX, 4, argv));
return (lua_pop(L, 2), ret); // result, events
- }
- ret = lua_toboolean(L, -1);
+ } else ret = lua_toboolean(L, -1);
return (lua_pop(L, 2), ret); // result, events
}
@@ -315,7 +315,7 @@ static void find_clicked(FindButton button, void *L) {
L, "find", LUA_TSTRING, find_text, LUA_TBOOLEAN, button == find_next, -1);
else if (button == replace) {
emit(L, "replace", LUA_TSTRING, repl_text, -1);
- emit(L, "find", LUA_TSTRING, find_text, LUA_TBOOLEAN, TRUE, -1);
+ emit(L, "find", LUA_TSTRING, find_text, LUA_TBOOLEAN, true, -1);
} else if (button == replace_all)
emit(L, "replace_all", LUA_TSTRING, find_text, LUA_TSTRING, repl_text, -1);
}
@@ -381,7 +381,7 @@ static int find_keypress(EObjectType _, void *object, void *data, chtype key) {
for (i = 9; i >= 0; i--) if (store[i] && strcmp(store[i], text) == 0) break;
key == CDK_PREV ? i++ : i--;
if (i >= 0 && i <= 9 && store[i])
- setCDKEntryValue(entry, store[i]), drawCDKEntry(entry, FALSE);
+ setCDKEntryValue(entry, store[i]), drawCDKEntry(entry, false);
} else if (key >= KEY_F(1) && key <= KEY_F(4)) {
toggle(&find_options[key - KEY_F(1)], !find_options[key - KEY_F(1)]);
// Redraw the optionbox.
@@ -390,8 +390,8 @@ static int find_keypress(EObjectType _, void *object, void *data, chtype key) {
destroyCDKButtonbox(*optionbox);
*optionbox = newCDKButtonbox(
findbox, RIGHT, TOP, 2, width, NULL, 2, 2, option_labels, 4, A_NORMAL,
- FALSE, FALSE);
- drawCDKButtonbox(*optionbox, FALSE);
+ false, false);
+ drawCDKButtonbox(*optionbox, false);
} else if (key == KEY_UP || key == KEY_DOWN) {
focused_entry = entry == find_entry ? repl_entry : find_entry;
injectCDKEntry(entry, KEY_ENTER); // exit this entry
@@ -399,7 +399,7 @@ static int find_keypress(EObjectType _, void *object, void *data, chtype key) {
if (emit(lua, "find_text_changed", -1)) refresh_all();
copyfree(&prev_text, text);
}
- return TRUE;
+ return true;
}
#endif
@@ -422,17 +422,17 @@ static int focus_find(lua_State *L) {
int e_width = COLS - o_width - b_width - l_width - 1;
find_entry = newCDKEntry(
findbox, l_width - strlen(find_label), TOP, NULL, find_label, A_NORMAL, '_',
- vMIXED, e_width, 0, 64, FALSE, FALSE);
+ vMIXED, e_width, 0, 64, false, false);
repl_entry = newCDKEntry(
findbox, l_width - strlen(repl_label), BOTTOM, NULL, repl_label, A_NORMAL,
- '_', vMIXED, e_width, 0, 64, FALSE, FALSE);
+ '_', vMIXED, e_width, 0, 64, false, false);
CDKBUTTONBOX *buttonbox, *optionbox;
buttonbox = newCDKButtonbox(
findbox, COLS - o_width - b_width, TOP, 2, b_width, NULL, 2, 2,
- button_labels, 4, A_REVERSE, FALSE, FALSE);
+ button_labels, 4, A_REVERSE, false, false);
optionbox = newCDKButtonbox(
findbox, RIGHT, TOP, 2, o_width, NULL, 2, 2, option_labels, 4, A_NORMAL,
- FALSE, FALSE);
+ false, false);
// TODO: ideally no #define here.
#define bind(k, d) (bindCDKObject(vENTRY, find_entry, k, find_keypress, d), \
bindCDKObject(vENTRY, repl_entry, k, find_keypress, d))
@@ -591,8 +591,8 @@ static int dialog(lua_State *L) {
* @see add_view
*/
static void lua_pushview(lua_State *L, Scintilla *view) {
- lua_getfield(L, LUA_REGISTRYINDEX, "ta_views");
- lua_pushlightuserdata(L, view), lua_gettable(L, -2), lua_replace(L, -2);
+ lua_getfield(L, LUA_REGISTRYINDEX, "ta_views"),
+ lua_pushlightuserdata(L, view), lua_gettable(L, -2), lua_replace(L, -2);
}
/**
@@ -668,7 +668,7 @@ static void sync_tabbar() {
lua_gettable(lua, -2), lua_tointeger(lua, -1) - 1);
lua_pop(lua, 2); // index and buffers
GtkNotebook *tabs = GTK_NOTEBOOK(tabbar);
- tab_sync = TRUE, gtk_notebook_set_current_page(tabs, i), tab_sync = FALSE;
+ tab_sync = true, gtk_notebook_set_current_page(tabs, i), tab_sync = false;
//#elif CURSES
// TODO: tabs
#endif
@@ -680,10 +680,10 @@ static void sync_tabbar() {
* @param index The stack index of the value to test.
* @param tname The name of the expected metatable.
*/
-static int is_type(lua_State *L, int index, const char *tname) {
- if (!lua_getmetatable(L, index)) return FALSE;
+static bool is_type(lua_State *L, int index, const char *tname) {
+ if (!lua_getmetatable(L, index)) return false;
luaL_getmetatable(L, tname);
- int has_metatable = lua_rawequal(L, -1, -2);
+ bool has_metatable = lua_rawequal(L, -1, -2);
return (lua_pop(L, 2), has_metatable); // metatable, metatable
}
@@ -757,7 +757,7 @@ static int get_int_field(lua_State *L, int index, int n) {
* @param f An optional GTK callback function associated with each menu item.
* @param submenu Flag indicating whether or not this menu is a submenu.
*/
-static void lua_pushmenu(lua_State *L, int index, GCallback f, int submenu) {
+static void lua_pushmenu(lua_State *L, int index, GCallback f, bool submenu) {
GtkWidget *menu = gtk_menu_new(), *submenu_root = NULL;
if (lua_getfield(L, index, "title") != LUA_TNIL || submenu) { // submenu title
const char *label = !lua_isnil(L, -1) ? lua_tostring(L, -1) : "no title";
@@ -767,9 +767,9 @@ static void lua_pushmenu(lua_State *L, int index, GCallback f, int submenu) {
lua_pop(L, 1); // title
for (size_t i = 1; i <= lua_rawlen(L, index); lua_pop(L, 1), i++) {
if (lua_rawgeti(L, -1, i) != LUA_TTABLE) continue; // popped on loop
- int is_submenu = lua_getfield(L, -1, "title") != LUA_TNIL;
+ bool is_submenu = lua_getfield(L, -1, "title") != LUA_TNIL;
if (lua_pop(L, 1), is_submenu) {
- lua_pushmenu(L, -1, f, TRUE);
+ lua_pushmenu(L, -1, f, true);
gtk_menu_shell_append(GTK_MENU_SHELL(menu), lua_touserdata(L, -1));
lua_pop(L, 1); // menu
continue;
@@ -800,7 +800,7 @@ static void menu_clicked(GtkWidget *_, void *id) {
static int menu(lua_State *L) {
luaL_checktype(L, 1, LUA_TTABLE);
#if GTK
- return (lua_pushmenu(L, -1, G_CALLBACK(menu_clicked), FALSE), 1);
+ return (lua_pushmenu(L, -1, G_CALLBACK(menu_clicked), false), 1);
#elif CURSES
return (lua_pushnil(L), 1);
#endif
@@ -825,7 +825,7 @@ static int ui_index(lua_State *L) {
GdkWindowState state = gdk_window_get_state(gtk_widget_get_window(window));
lua_pushboolean(L, state & GDK_WINDOW_STATE_MAXIMIZED);
#elif CURSES
- lua_pushboolean(L, FALSE);
+ lua_pushboolean(L, false);
#endif
} else if (strcmp(key, "size") == 0) {
int width, height;
@@ -887,7 +887,7 @@ static int ui_newindex(lua_State *L) {
(lua_rawgeti(L, 3, i), lua_touserdata(L, -1))); // popped on loop
GtkWidget *vbox = gtk_widget_get_parent(menubar);
gtk_container_remove(GTK_CONTAINER(vbox), menubar);
- gtk_box_pack_start(GTK_BOX(vbox), menubar = new_menubar, FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(vbox), menubar = new_menubar, false, false, 0);
gtk_box_reorder_child(GTK_BOX(vbox), new_menubar, 0);
if (lua_rawlen(L, 3) > 0) gtk_widget_show_all(new_menubar);
#if __APPLE__
@@ -966,7 +966,7 @@ static Scintilla *view_for_doc(lua_State *L, int index) {
* n of -1 represents the last document.
* @param relative Flag indicating whether or not n is relative.
*/
-static void goto_doc(lua_State *L, Scintilla *view, int n, int relative) {
+static void goto_doc(lua_State *L, Scintilla *view, int n, bool relative) {
if (relative && n == 0) return;
lua_getfield(L, LUA_REGISTRYINDEX, "ta_buffers");
if (relative) {
@@ -1017,7 +1017,7 @@ static void remove_doc(lua_State *L, sptr_t doc) {
lua_getfield(L, LUA_REGISTRYINDEX, "ta_views");
for (size_t i = 1; i <= lua_rawlen(L, -1); lua_pop(L, 1), i++) {
Scintilla *view = (lua_rawgeti(L, -1, i), lua_toview(L, -1)); // ^popped
- if (doc == SS(view, SCI_GETDOCPOINTER, 0, 0)) goto_doc(L, view, -1, TRUE);
+ if (doc == SS(view, SCI_GETDOCPOINTER, 0, 0)) goto_doc(L, view, -1, true);
}
lua_pop(L, 1); // views
lua_newtable(L);
@@ -1062,10 +1062,9 @@ static int delete_buffer_lua(lua_State *L) {
sptr_t doc = SS(view, SCI_GETDOCPOINTER, 0, 0);
if (lua_getfield(L, LUA_REGISTRYINDEX, "ta_buffers"), lua_rawlen(L, -1) == 1)
new_buffer(0);
- goto_doc(L, focused_view, -1, TRUE);
+ goto_doc(L, focused_view, -1, true);
delete_buffer(doc);
- emit(L, "buffer_after_switch", -1), emit(L, "buffer_deleted", -1);
- return 0;
+ return (emit(L, "buffer_after_switch", -1), emit(L, "buffer_deleted", -1), 0);
}
/** `_G.buffer_new()` Lua function. */
@@ -1091,8 +1090,7 @@ static sptr_t luaL_checkscintilla(lua_State *L, int *arg, int type) {
int i = luaL_checkinteger(L, (*arg)++);
return i >= 0 ? i - 1 : i; // do not adjust significant values like -1
}
- if (type >= SINT && type <= SKEYMOD) return luaL_checkinteger(L, (*arg)++);
- return 0;
+ return type >= SINT && type <= SKEYMOD ? luaL_checkinteger(L, (*arg)++) : 0;
}
/**
@@ -1115,7 +1113,8 @@ static int call_scintilla(
{
uptr_t wparam = 0;
sptr_t lparam = 0, len = 0;
- int params_needed = 2, string_return = FALSE, nresults = 0;
+ int params_needed = 2, nresults = 0;
+ bool string_return = false;
char *text = NULL;
// Even though the SCI_PRIVATELEXERCALL interface has ltype int, the LPeg
@@ -1138,7 +1137,7 @@ static int call_scintilla(
lparam = (sptr_t)luaL_checkstring(L, arg);
params_needed = 0;
} else if (ltype == SSTRINGRET || rtype == SSTRINGRET)
- string_return = TRUE, params_needed = wtype == SLEN ? 0 : 1;
+ string_return = true, params_needed = wtype == SLEN ? 0 : 1;
if (params_needed > 0) wparam = luaL_checkscintilla(L, &arg, wtype);
if (params_needed > 1) lparam = luaL_checkscintilla(L, &arg, ltype);
if (string_return) { // create a buffer for the return string
@@ -1198,7 +1197,7 @@ static void show_context_menu(lua_State *L, GdkEventButton *event, char *k) {
}
/** Signal for a tab label mouse click. */
-static int tab_clicked(GtkWidget *label, GdkEventButton *event, void *L) {
+static bool tab_clicked(GtkWidget *label, GdkEventButton *event, void *L) {
GtkNotebook *tabs = GTK_NOTEBOOK(tabbar);
for (int i = 0; i < gtk_notebook_get_n_pages(tabs); i++) {
GtkWidget *page = gtk_notebook_get_nth_page(tabs, i);
@@ -1212,7 +1211,7 @@ static int tab_clicked(GtkWidget *label, GdkEventButton *event, void *L) {
if (event->button == 3) show_context_menu(L, event, "tab_context_menu");
break;
}
- return TRUE;
+ return true;
}
#endif
@@ -1324,7 +1323,7 @@ static int buffer_newindex(lua_State *L) {
lua_getfield(L, 1, "tab_pointer");
#if GTK
GtkWidget *box = gtk_event_box_new();
- gtk_event_box_set_visible_window(GTK_EVENT_BOX(box), FALSE);
+ gtk_event_box_set_visible_window(GTK_EVENT_BOX(box), false);
GtkWidget *label = gtk_label_new(luaL_checkstring(L, 3));
gtk_container_add(GTK_CONTAINER(box), label), gtk_widget_show(label);
gtk_notebook_set_tab_label(
@@ -1362,7 +1361,7 @@ static void add_doc(lua_State *L, sptr_t doc) {
lua_newtable(L);
lua_pushlightuserdata(L, (sptr_t *)doc), lua_setfield(L, -2, "doc_pointer");
#if GTK
- GtkWidget *tab = gtk_vbox_new(FALSE, 0); // placeholder in GtkNotebook
+ GtkWidget *tab = gtk_vbox_new(false, 0); // placeholder in GtkNotebook
lua_pushlightuserdata(L, tab), lua_setfield(L, -2, "tab_pointer");
//#elif CURSES
// TODO: tabs
@@ -1389,17 +1388,17 @@ static void new_buffer(sptr_t doc) {
if (!doc) {
emit(lua, "buffer_before_switch", -1);
add_doc(lua, doc = SS(focused_view, SCI_CREATEDOCUMENT, 0, 0));
- goto_doc(lua, focused_view, -1, FALSE);
+ goto_doc(lua, focused_view, -1, false);
} else add_doc(lua, doc), SS(focused_view, SCI_ADDREFDOCUMENT, 0, doc);
#if GTK
// Add a tab to the tabbar.
GtkWidget *tab = (lua_pushdoc(lua, SS(focused_view, SCI_GETDOCPOINTER, 0, 0)),
lua_getfield(lua, -1, "tab_pointer"), lua_touserdata(lua, -1));
- tab_sync = TRUE;
+ tab_sync = true;
int i = gtk_notebook_append_page(GTK_NOTEBOOK(tabbar), tab, NULL);
gtk_widget_show(tab), gtk_widget_set_visible(tabbar, show_tabs && i > 0);
gtk_notebook_set_current_page(GTK_NOTEBOOK(tabbar), i);
- tab_sync = FALSE;
+ tab_sync = false;
lua_pop(lua, 2); // tab_pointer and buffer
//#elif CURSES
// TODO: tabs
@@ -1411,7 +1410,7 @@ static void new_buffer(sptr_t doc) {
/** `_G.quit()` Lua function. */
static int quit(lua_State *L) {
#if GTK
- GdkEventAny event = {GDK_DELETE, gtk_widget_get_window(window), TRUE};
+ GdkEventAny event = {GDK_DELETE, gtk_widget_get_window(window), true};
gdk_event_put((GdkEvent *)&event);
#elif CURSES
quitting = !emit(L, "quit", -1);
@@ -1423,12 +1422,12 @@ static int quit(lua_State *L) {
* Loads and runs the given file.
* @param L The Lua state.
* @param filename The file name relative to textadept_home.
- * @return 1 if there are no errors or 0 in case of errors.
+ * @return true if there are no errors or false in case of errors.
*/
-static int run_file(lua_State *L, const char *filename) {
+static bool run_file(lua_State *L, const char *filename) {
char *file = malloc(strlen(textadept_home) + 1 + strlen(filename) + 1);
sprintf(file, "%s/%s", textadept_home, filename);
- int ok = luaL_dofile(L, file) == LUA_OK;
+ bool ok = luaL_dofile(L, file) == LUA_OK;
if (!ok) {
const char *argv[] = {
"--title", "Initialization Error", "--text", lua_tostring(L, -1)
@@ -1444,7 +1443,7 @@ static int reset(lua_State *L) {
int persist_ref = (lua_newtable(L), luaL_ref(L, LUA_REGISTRYINDEX));
lua_rawgeti(L, LUA_REGISTRYINDEX, persist_ref); // emit will unref
emit(L, "reset_before", LUA_TTABLE, luaL_ref(L, LUA_REGISTRYINDEX), -1);
- init_lua(L, 0, NULL, TRUE);
+ init_lua(L, 0, NULL, true);
lua_pushview(L, focused_view), lua_setglobal(L, "view");
lua_pushdoc(L, SS(focused_view, SCI_GETDOCPOINTER, 0, 0)),
lua_setglobal(L, "buffer");
@@ -1457,14 +1456,13 @@ static int reset(lua_State *L) {
/** Runs the timeout function passed to `_G.timeout()`. */
static int timed_out(void *args) {
- int *refs = args;
+ int *refs = args, nargs = 0;
lua_rawgeti(lua, LUA_REGISTRYINDEX, refs[0]); // function
- int nargs = 0, repeat = TRUE;
while (refs[++nargs]) lua_rawgeti(lua, LUA_REGISTRYINDEX, refs[nargs]);
- int ok = lua_pcall(lua, nargs - 1, 1, 0) == LUA_OK;
+ bool ok = lua_pcall(lua, nargs - 1, 1, 0) == LUA_OK, repeat = true;
if (!ok || !lua_toboolean(lua, -1)) {
while (--nargs >= 0) luaL_unref(lua, LUA_REGISTRYINDEX, refs[nargs]);
- repeat = FALSE;
+ repeat = false;
if (!ok) emit(lua, "error", LUA_TSTRING, lua_tostring(lua, -1), -1);
}
return (lua_pop(lua, 1), repeat); // result
@@ -1518,9 +1516,9 @@ static int iconv_lua(lua_State *L) {
* @param argc The number of command line parameters.
* @param argv The array of command line parameters.
* @param reinit Flag indicating whether or not to reinitialize the Lua state.
- * @return TRUE on success, FALSE otherwise.
+ * @return true on success, false otherwise.
*/
-static int init_lua(lua_State *L, int argc, char **argv, int reinit) {
+static bool init_lua(lua_State *L, int argc, char **argv, bool reinit) {
if (!reinit) {
lua_newtable(L);
for (int i = 0; i < argc; i++)
@@ -1578,10 +1576,10 @@ static int init_lua(lua_State *L, int argc, char **argv, int reinit) {
lua_setglobal(L, "_BUFFERS");
lua_getfield(L, LUA_REGISTRYINDEX, "ta_views"), lua_setglobal(L, "_VIEWS");
lua_pushstring(L, textadept_home), lua_setglobal(L, "_HOME");
- if (platform) lua_pushboolean(L, TRUE), lua_setglobal(L, platform);
+ if (platform) lua_pushboolean(L, true), lua_setglobal(L, platform);
#if CURSES
- lua_pushboolean(L, TRUE), lua_setglobal(L, "CURSES");
- show_tabs = FALSE; // TODO: tabs
+ lua_pushboolean(L, true), lua_setglobal(L, "CURSES");
+ show_tabs = false; // TODO: tabs
#endif
const char *charset = NULL;
#if GTK
@@ -1599,7 +1597,7 @@ static int init_lua(lua_State *L, int argc, char **argv, int reinit) {
#endif
lua_pushstring(L, charset), lua_setglobal(L, "_CHARSET");
- if (!run_file(L, "core/init.lua")) return (lua_close(L), FALSE);
+ if (!run_file(L, "core/init.lua")) return (lua_close(L), false);
lua_getglobal(L, "_SCINTILLA");
lua_getfield(L, -1, "constants"),
lua_setfield(L, LUA_REGISTRYINDEX, "ta_constants");
@@ -1608,24 +1606,24 @@ static int init_lua(lua_State *L, int argc, char **argv, int reinit) {
lua_getfield(L, -1, "properties"),
lua_setfield(L, LUA_REGISTRYINDEX, "ta_properties");
lua_pop(L, 1); // _SCINTILLA
- return TRUE;
+ return true;
}
#if GTK
/** Signal for a Textadept window focus change. */
-static int window_focused(GtkWidget *_, GdkEventFocus *__, void *L) {
- if (command_entry_focused) return FALSE; // keep command entry focused
+static bool window_focused(GtkWidget *_, GdkEventFocus *__, void *L) {
+ if (command_entry_focused) return false; // keep command entry focused
if (focused_view && !gtk_widget_has_focus(focused_view))
gtk_widget_grab_focus(focused_view);
- return (emit(L, "focus", -1), FALSE);
+ return (emit(L, "focus", -1), false);
}
/** Signal for a Textadept keypress. */
-static int window_keypress(GtkWidget *_, GdkEventKey *event, void *__) {
+static bool window_keypress(GtkWidget *_, GdkEventKey *event, void *__) {
if (event->keyval != GDK_Escape || !gtk_widget_get_visible(findbox) ||
gtk_widget_has_focus(command_entry))
- return FALSE;
- return (gtk_widget_hide(findbox), gtk_widget_grab_focus(focused_view), TRUE);
+ return false;
+ return (gtk_widget_hide(findbox), gtk_widget_grab_focus(focused_view), true);
}
#endif
@@ -1716,14 +1714,14 @@ static void resize_pane(Pane *pane, int rows, int cols, int y, int x) {
* @param pane The pane that contains the view to unsplit.
* @param view The view to unsplit.
* @param parent The parent of pane. Used recursively.
- * @return TRUE if the view can be split and was; FALSE otherwise
+ * @return true if the view can be split and was; false otherwise
* @see unsplit_view
*/
-static int unsplit_pane(Pane *pane, Scintilla *view, Pane *parent) {
+static bool unsplit_pane(Pane *pane, Scintilla *view, Pane *parent) {
if (pane->type != SINGLE)
return unsplit_pane(pane->child1, view, pane) ||
unsplit_pane(pane->child2, view, pane);
- if (pane->view != view) return FALSE;
+ if (pane->view != view) return false;
remove_views_from_pane(
pane == parent->child1 ? parent->child2 : parent->child1);
delwin(parent->win); // delete split bar
@@ -1734,7 +1732,7 @@ static int unsplit_pane(Pane *pane, Scintilla *view, Pane *parent) {
free(pane);
// Update.
resize_pane(parent, parent->rows, parent->cols, parent->y, parent->x);
- return TRUE;
+ return true;
}
#endif
@@ -1742,14 +1740,14 @@ static int unsplit_pane(Pane *pane, Scintilla *view, Pane *parent) {
* Unsplits the pane the given Scintilla view is in and keeps the view.
* All views in the other pane are deleted.
* @param view The Scintilla view to keep when unsplitting.
- * @return TRUE if the view was split; FALSE otherwise
+ * @return true if the view was split; false otherwise
* @see remove_views_from_pane
* @see delete_view
*/
-static int unsplit_view(Scintilla *view) {
+static bool unsplit_view(Scintilla *view) {
#if GTK
GtkWidget *pane = gtk_widget_get_parent(view);
- if (!GTK_IS_PANED(pane)) return FALSE;
+ if (!GTK_IS_PANED(pane)) return false;
GtkWidget *other = gtk_paned_get_child1(GTK_PANED(pane)) != view ?
gtk_paned_get_child1(GTK_PANED(pane)) :
gtk_paned_get_child2(GTK_PANED(pane));
@@ -1769,10 +1767,10 @@ static int unsplit_view(Scintilla *view) {
gtk_widget_grab_focus(GTK_WIDGET(view));
g_object_unref(view), g_object_unref(other);
#elif CURSES
- if (pane->type == SINGLE) return FALSE;
+ if (pane->type == SINGLE) return false;
unsplit_pane(pane, view, NULL), scintilla_noutrefresh(view);
#endif
- return TRUE;
+ return true;
}
/**
@@ -1782,7 +1780,7 @@ static int unsplit_view(Scintilla *view) {
* @param L The Lua state.
*/
static void close_lua(lua_State *L) {
- closing = TRUE;
+ closing = true;
while (unsplit_view(focused_view)) ; // need space to fix compiler warning
lua_getfield(L, LUA_REGISTRYINDEX, "ta_buffers");
for (size_t i = 1; i <= lua_rawlen(L, -1); i++)
@@ -1800,11 +1798,11 @@ static void close_lua(lua_State *L) {
* Closes the Lua state and releases resources.
* @see close_lua
*/
-static int exiting(GtkWidget *_, GdkEventAny *__, void *L) {
- if (emit(L, "quit", -1)) return TRUE; // halt
+static bool exiting(GtkWidget *_, GdkEventAny *__, void *L) {
+ if (emit(L, "quit", -1)) return true; // halt
close_lua(L);
scintilla_release_resources();
- return (gtk_main_quit(), FALSE);
+ return (gtk_main_quit(), false);
}
#if (__APPLE__ && !CURSES)
@@ -1812,15 +1810,15 @@ static int exiting(GtkWidget *_, GdkEventAny *__, void *L) {
* Signal for opening files from OSX.
* Generates an 'appleevent_odoc' event for each document sent.
*/
-static int open_file(GtkosxApplication*_, char *path, void *L) {
- return (emit(L, "appleevent_odoc", LUA_TSTRING, path, -1), TRUE);
+static bool open_file(GtkosxApplication*_, char *path, void *L) {
+ return (emit(L, "appleevent_odoc", LUA_TSTRING, path, -1), true);
}
/**
* Signal for block terminating Textadept from OSX.
* Generates a 'quit' event.
*/
-static int terminating(GtkosxApplication *_, void *L) {
+static bool terminating(GtkosxApplication *_, void *L) {
return emit(L, "quit", -1);
}
@@ -1897,7 +1895,7 @@ static void notified(Scintilla *view, int _, SCNotification *n, void *L) {
#if GTK
/** Signal for a Scintilla keypress. */
-static int keypress(GtkWidget *_, GdkEventKey *event, void *L) {
+static bool keypress(GtkWidget *_, GdkEventKey *event, void *L) {
return emit(
L, "keypress", LUA_TNUMBER, event->keyval,
LUA_TBOOLEAN, event->state & GDK_SHIFT_MASK,
@@ -1908,16 +1906,16 @@ static int keypress(GtkWidget *_, GdkEventKey *event, void *L) {
}
/** Signal for a Scintilla mouse click. */
-static int mouse_clicked(GtkWidget *_, GdkEventButton *event, void *L) {
- if (event->type != GDK_BUTTON_PRESS || event->button != 3) return FALSE;
- return (show_context_menu(L, event, "context_menu"), TRUE);
+static bool mouse_clicked(GtkWidget *_, GdkEventButton *event, void *L) {
+ if (event->type != GDK_BUTTON_PRESS || event->button != 3) return false;
+ return (show_context_menu(L, event, "context_menu"), true);
}
#endif
/** `view.goto_buffer()` Lua function. */
static int goto_doc_lua(lua_State *L) {
Scintilla *view = luaL_checkview(L, 1), *prev_view = focused_view;
- int relative = lua_isnumber(L, 2);
+ bool relative = lua_isnumber(L, 2);
if (!relative) {
lua_getfield(L, LUA_REGISTRYINDEX, "ta_buffers"), lua_pushvalue(L, 2),
lua_gettable(L, -2), lua_replace(L, 2);
@@ -1949,21 +1947,23 @@ static Pane *new_pane(Scintilla *view) {
* Helper for splitting a view.
* Recursively propagates a split to child panes.
* @param pane The pane to split.
- * @param vert Whether to split the pane vertically or horizontally.
+ * @param vertical Whether to split the pane vertically or horizontally.
* @param view The first Scintilla view to place in the split view.
* @param view2 The second Scintilla view to place in the split view.
* @see split_view
*/
-static int split_pane(Pane *pane, int vert, Scintilla *view, Scintilla *view2) {
+static bool split_pane(
+ Pane *pane, bool vertical, Scintilla *view, Scintilla *view2)
+{
if (pane->type != SINGLE)
- return split_pane(pane->child1, vert, view, view2) ||
- split_pane(pane->child2, vert, view, view2);
- if (view != pane->view) return FALSE;
+ return split_pane(pane->child1, vertical, view, view2) ||
+ split_pane(pane->child2, vertical, view, view2);
+ if (view != pane->view) return false;
Pane *child1 = new_pane(view), *child2 = new_pane(view2);
- pane->type = vert ? VSPLIT : HSPLIT;
+ pane->type = vertical ? VSPLIT : HSPLIT;
pane->child1 = child1, pane->child2 = child2, pane->view = NULL;
// Resize children and create a split bar.
- if (vert) {
+ if (vertical) {
pane->split_size = pane->cols / 2;
resize_pane(child1, pane->rows, pane->split_size, pane->y, pane->x);
resize_pane(
@@ -1979,7 +1979,7 @@ static int split_pane(Pane *pane, int vert, Scintilla *view, Scintilla *view2) {
pane->win = newwin(1, pane->cols, pane->y + pane->split_size, pane->x);
}
refresh_pane(pane);
- return TRUE;
+ return true;
}
#endif
@@ -1990,7 +1990,7 @@ static int split_pane(Pane *pane, int vert, Scintilla *view, Scintilla *view2) {
* @param vertical Flag indicating whether to split the view vertically or
* horozontally.
*/
-static void split_view(Scintilla *view, int vertical) {
+static void split_view(Scintilla *view, bool vertical) {
sptr_t curdoc = SS(view, SCI_GETDOCPOINTER, 0, 0);
int first_line = SS(view, SCI_GETFIRSTVISIBLELINE, 0, 0),
x_offset = SS(view, SCI_GETXOFFSET, 0, 0),
@@ -2022,8 +2022,8 @@ static void split_view(Scintilla *view, int vertical) {
focus_view(view2);
SS(view2, SCI_SETSEL, anchor, current_pos);
- int new_first_line = SS(view2, SCI_GETFIRSTVISIBLELINE, 0, 0);
- SS(view2, SCI_LINESCROLL, first_line - new_first_line, 0);
+ SS(view2, SCI_LINESCROLL, first_line - SS(
+ view2, SCI_GETFIRSTVISIBLELINE, 0, 0), 0);
SS(view2, SCI_SETXOFFSET, x_offset, 0);
}
@@ -2160,16 +2160,18 @@ static Scintilla *new_view(sptr_t doc) {
* Creates and returns for the findbox a new GtkComboBoxEntry, storing its
* GtkLabel, GtkEntry, and GtkListStore in the given pointers.
*/
-static GtkWidget *new_combo(GtkWidget **label, GtkWidget **entry, ListStore **history) {
+static GtkWidget *new_combo(
+ GtkWidget **label, GtkWidget **entry, ListStore **history)
+{
*label = gtk_label_new(""); // localized label text set later via Lua
*history = gtk_list_store_new(1, G_TYPE_STRING);
GtkWidget *combo = gtk_combo_box_entry_new_with_model(
GTK_TREE_MODEL(*history), 0);
g_object_unref(*history);
gtk_combo_box_entry_set_text_column(GTK_COMBO_BOX_ENTRY(combo), 0);
- gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combo), FALSE);
+ gtk_combo_box_set_focus_on_click(GTK_COMBO_BOX(combo), false);
*entry = gtk_bin_get_child(GTK_BIN(combo));
- gtk_entry_set_activates_default(GTK_ENTRY(*entry), TRUE);
+ gtk_entry_set_activates_default(GTK_ENTRY(*entry), true);
gtk_label_set_mnemonic_widget(GTK_LABEL(*label), *entry);
return combo;
}
@@ -2183,20 +2185,20 @@ static void find_changed(GtkEditable *_, void *L) {
static GtkWidget *new_button() {
GtkWidget *button = gtk_button_new_with_mnemonic(""); // localized via Lua
g_signal_connect(button, "clicked", G_CALLBACK(find_clicked), lua);
- gtk_widget_set_can_focus(button, FALSE);
+ gtk_widget_set_can_focus(button, false);
return button;
}
/** Creates and returns a new checkbox option for the findbox. */
static GtkWidget *new_option() {
GtkWidget *option = gtk_check_button_new_with_mnemonic(""); // localized later
- gtk_widget_set_can_focus(option, FALSE);
+ gtk_widget_set_can_focus(option, false);
return option;
}
/** Creates the Find box. */
static GtkWidget *new_findbox() {
- findbox = gtk_table_new(2, 6, FALSE);
+ findbox = gtk_table_new(2, 6, false);
GtkWidget *find_combo = new_combo(&find_label, &find_entry, &find_history),
*replace_combo = new_combo(&repl_label, &repl_entry, &repl_history);
@@ -2205,7 +2207,7 @@ static GtkWidget *new_findbox() {
find_next = new_button(), find_prev = new_button(), replace = new_button(),
replace_all = new_button(), match_case = new_option(),
whole_word = new_option(), regex = new_option(), in_files = new_option();
- gtk_widget_set_can_default(find_next, TRUE);
+ gtk_widget_set_can_default(find_next, true);
GtkTable *table = GTK_TABLE(findbox);
int expand = GTK_FILL | GTK_EXPAND, shrink = GTK_FILL | GTK_SHRINK;
@@ -2229,10 +2231,10 @@ static GtkWidget *new_findbox() {
* Emit "Escape" key for the command entry on focus lost unless the window is
* losing focus or the application is quitting.
*/
-static int focus_lost(GtkWidget *widget, GdkEvent *_, void *L) {
- if (widget == window && command_entry_focused) return TRUE; // halt
- if (widget != command_entry || closing) return FALSE;
- return (emit(L, "keypress", LUA_TNUMBER, GDK_Escape, -1), FALSE);
+static bool focus_lost(GtkWidget *widget, GdkEvent *_, void *L) {
+ if (widget == window && command_entry_focused) return true; // halt
+ if (widget != command_entry || closing) return false;
+ return (emit(L, "keypress", LUA_TNUMBER, GDK_Escape, -1), false);
}
#endif // if GTK
@@ -2268,7 +2270,7 @@ static void new_window() {
accel = gtk_accel_group_new();
#if (__APPLE__ && !CURSES)
- gtkosx_application_set_use_quartz_accelerators(osxapp, FALSE);
+ gtkosx_application_set_use_quartz_accelerators(osxapp, false);
g_signal_connect(osxapp, "NSApplicationOpenFile", G_CALLBACK(open_file), lua);
g_signal_connect(
osxapp, "NSApplicationBlockTermination", G_CALLBACK(terminating), lua);
@@ -2276,30 +2278,30 @@ static void new_window() {
osxapp, "NSApplicationWillTerminate", G_CALLBACK(terminate), lua);
#endif
- GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
+ GtkWidget *vbox = gtk_vbox_new(false, 0);
gtk_container_add(GTK_CONTAINER(window), vbox);
menubar = gtk_menu_bar_new();
- gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 0);
+ gtk_box_pack_start(GTK_BOX(vbox), menubar, false, false, 0);
tabbar = gtk_notebook_new();
g_signal_connect(tabbar, "switch-page", G_CALLBACK(tab_changed), lua);
- gtk_notebook_set_scrollable(GTK_NOTEBOOK(tabbar), TRUE);
- gtk_widget_set_can_focus(tabbar, FALSE);
- gtk_box_pack_start(GTK_BOX(vbox), tabbar, FALSE, FALSE, 0);
+ gtk_notebook_set_scrollable(GTK_NOTEBOOK(tabbar), true);
+ gtk_widget_set_can_focus(tabbar, false);
+ gtk_box_pack_start(GTK_BOX(vbox), tabbar, false, false, 0);
GtkWidget *paned = gtk_vpaned_new();
- gtk_box_pack_start(GTK_BOX(vbox), paned, TRUE, TRUE, 0);
+ gtk_box_pack_start(GTK_BOX(vbox), paned, true, true, 0);
- GtkWidget *vboxp = gtk_vbox_new(FALSE, 0);
+ GtkWidget *vboxp = gtk_vbox_new(false, 0);
gtk_paned_add1(GTK_PANED(paned), vboxp);
- GtkWidget *hbox = gtk_hbox_new(FALSE, 0);
- gtk_box_pack_start(GTK_BOX(vboxp), hbox, TRUE, TRUE, 0);
+ GtkWidget *hbox = gtk_hbox_new(false, 0);
+ gtk_box_pack_start(GTK_BOX(vboxp), hbox, true, true, 0);
- gtk_box_pack_start(GTK_BOX(hbox), new_view(0), TRUE, TRUE, 0);
+ gtk_box_pack_start(GTK_BOX(hbox), new_view(0), true, true, 0);
- gtk_box_pack_start(GTK_BOX(vboxp), new_findbox(), FALSE, FALSE, 5);
+ gtk_box_pack_start(GTK_BOX(vboxp), new_findbox(), false, false, 5);
command_entry = scintilla_new();
gtk_widget_set_size_request(command_entry, 1, 1);
@@ -2308,15 +2310,15 @@ static void new_window() {
command_entry, "focus-out-event", G_CALLBACK(focus_lost), lua);
gtk_paned_add2(GTK_PANED(paned), command_entry);
gtk_container_child_set(
- GTK_CONTAINER(paned), command_entry, "shrink", FALSE, NULL);
+ GTK_CONTAINER(paned), command_entry, "shrink", false, NULL);
- GtkWidget *hboxs = gtk_hbox_new(FALSE, 0);
- gtk_box_pack_start(GTK_BOX(vbox), hboxs, FALSE, FALSE, 1);
+ GtkWidget *hboxs = gtk_hbox_new(false, 0);
+ gtk_box_pack_start(GTK_BOX(vbox), hboxs, false, false, 1);
statusbar[0] = gtk_label_new(NULL), statusbar[1] = gtk_label_new(NULL);
- gtk_box_pack_start(GTK_BOX(hboxs), statusbar[0], TRUE, TRUE, 5);
+ gtk_box_pack_start(GTK_BOX(hboxs), statusbar[0], true, true, 5);
gtk_misc_set_alignment(GTK_MISC(statusbar[0]), 0, 0);
- gtk_box_pack_start(GTK_BOX(hboxs), statusbar[1], TRUE, TRUE, 5);
+ gtk_box_pack_start(GTK_BOX(hboxs), statusbar[1], true, true, 5);
gtk_misc_set_alignment(GTK_MISC(statusbar[1]), 1, 0);
gtk_widget_show_all(window);
@@ -2336,19 +2338,19 @@ static void new_window() {
#if GTK && _WIN32
/** Reads and processes a remote Textadept's command line arguments. */
-static int read_pipe(GIOChannel *source, GIOCondition _, HANDLE pipe) {
+static bool read_pipe(GIOChannel *source, GIOCondition _, HANDLE pipe) {
char *buf;
size_t len;
g_io_channel_read_to_end(source, &buf, &len, NULL);
for (char *p = buf; p < buf + len - 2; p++) if (!*p) *p = '\n'; // '\0\0' end
process(NULL, NULL, buf);
g_free(buf), DisconnectNamedPipe(pipe);
- return FALSE;
+ return false;
}
/** Listens for remote Textadept communications. */
static DWORD WINAPI pipe_listener(HANDLE pipe) {
- while (1)
+ while (true)
if (pipe != INVALID_HANDLE_VALUE && ConnectNamedPipe(pipe, NULL)) {
GIOChannel *channel = g_io_channel_win32_new_fd(
_open_osfhandle((intptr_t)pipe, _O_RDONLY));
@@ -2398,13 +2400,13 @@ static void signalled(int signal) {
/** Replacement for `termkey_waitkey()` that handles asynchronous I/O. */
static TermKeyResult textadept_waitkey(TermKey *tk, TermKeyKey *key) {
#if !_WIN32
- int force = FALSE;
+ bool force = false;
struct timeval timeout = {0, termkey_get_waittime(tk)};
- while (1) {
+ while (true) {
TermKeyResult res = !force ?
termkey_getkey(tk, key) : termkey_getkey_force(tk, key);
if (res != TERMKEY_RES_AGAIN && res != TERMKEY_RES_NONE) return res;
- if (res == TERMKEY_RES_AGAIN) force = TRUE;
+ if (res == TERMKEY_RES_AGAIN) force = true;
// Wait for input.
int nfds = os_spawn_pushfds(lua);
fd_set *fds = lua_touserdata(lua, -1);
@@ -2480,21 +2482,21 @@ int main(int argc, char **argv) {
#endif
#if GTK
- int force = FALSE;
+ bool force = false;
for (int i = 0; i < argc; i++)
if (strcmp("-f", argv[i]) == 0 || strcmp("--force", argv[i]) == 0) {
- force = TRUE;
+ force = true;
break;
}
GApplication *app = g_application_new(ID, G_APPLICATION_HANDLES_COMMAND_LINE);
g_signal_connect(app, "command-line", G_CALLBACK(process), NULL);
- int registered = g_application_register(app, NULL, NULL);
+ bool registered = g_application_register(app, NULL, NULL);
if (!registered || !g_application_get_is_remote(app) || force) {
#endif
setlocale(LC_COLLATE, "C"), setlocale(LC_NUMERIC, "C"); // for Lua
- if (lua = luaL_newstate(), !init_lua(lua, argc, argv, FALSE)) return 1;
- initing = TRUE, new_window(), run_file(lua, "init.lua"), initing = FALSE;
+ if (lua = luaL_newstate(), !init_lua(lua, argc, argv, false)) return 1;
+ initing = true, new_window(), run_file(lua, "init.lua"), initing = false;
emit(lua, "buffer_new", -1), emit(lua, "view_new", -1); // first ones
lua_pushdoc(lua, SS(command_entry, SCI_GETDOCPOINTER, 0, 0)),
lua_setglobal(lua, "buffer");
@@ -2565,9 +2567,9 @@ int main(int argc, char **argv) {
millis = ticks.QuadPart / 10000; // each tick is a 100-nanosecond interval
#endif
} else continue; // skip unknown types
- int shift = key.modifiers & TERMKEY_KEYMOD_SHIFT;
- int ctrl = key.modifiers & TERMKEY_KEYMOD_CTRL;
- int alt = key.modifiers & TERMKEY_KEYMOD_ALT;
+ bool shift = key.modifiers & TERMKEY_KEYMOD_SHIFT;
+ bool ctrl = key.modifiers & TERMKEY_KEYMOD_CTRL;
+ bool alt = key.modifiers & TERMKEY_KEYMOD_ALT;
if (ch && !emit(
lua, "keypress", LUA_TNUMBER, ch, LUA_TBOOLEAN, shift,
LUA_TBOOLEAN, ctrl, LUA_TBOOLEAN, alt, -1))