Skip to content

Commit

Permalink
Added events.UNFOCUS for when Textadept loses focus.
Browse files Browse the repository at this point in the history
Do not emit this event when a dialog is active.
  • Loading branch information
orbitalquark committed Nov 17, 2020
1 parent b421b22 commit 4d661eb
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 6 deletions.
5 changes: 4 additions & 1 deletion core/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,9 @@ local M = {}
-- * _`ctrl`_: The "Control" modifier key is held down.
-- * _`alt`_: The "Alt"/"Option" modifier key is held down.
-- * _`cmd`_: The "Command" modifier key on macOS is held down.
-- @field UNFOCUS (string)
-- Emitted when Textadept loses focus.
-- This event is never emitted when Textadept is running in the terminal.
-- @field UPDATE_UI (string)
-- Emitted after the view is visually updated.
-- Arguments:
Expand Down Expand Up @@ -399,7 +402,7 @@ end)

-- Set event constants.
for _, v in pairs(_SCINTILLA.events) do M[v[1]:upper()] = v[1] end
local textadept_events = {'appleevent_odoc','buffer_after_switch','buffer_before_switch','buffer_deleted','buffer_new','csi','command_text_changed','error','find','find_text_changed','focus','initialized','keypress','menu_clicked','mouse','quit','replace','replace_all','reset_after','reset_before','resume','suspend', 'tab_clicked','view_after_switch','view_before_switch','view_new'}
local textadept_events = {'appleevent_odoc','buffer_after_switch','buffer_before_switch','buffer_deleted','buffer_new','csi','command_text_changed','error','find','find_text_changed','focus','initialized','keypress','menu_clicked','mouse','quit','replace','replace_all','reset_after','reset_before','resume','suspend', 'tab_clicked','unfocus','view_after_switch','view_before_switch','view_new'}
for _, v in pairs(textadept_events) do M[v:upper()] = v end

return M
6 changes: 6 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -4158,6 +4158,12 @@ Emitted when the user clicks on a buffer tab.
* _`alt`_: The "Alt"/"Option" modifier key is held down.
* _`cmd`_: The "Command" modifier key on macOS is held down.

<a id="events.UNFOCUS"></a>
#### `events.UNFOCUS` (string)

Emitted when Textadept loses focus.
This event is never emitted when Textadept is running in the terminal.

<a id="events.UPDATE_UI"></a>
#### `events.UPDATE_UI` (string)

Expand Down
2 changes: 2 additions & 0 deletions docs/manual.md
Original file line number Diff line number Diff line change
Expand Up @@ -1884,6 +1884,7 @@ N/A |Added |[FIND_RESULT_FOUND][]
N/A |Added |[FIND_TEXT_CHANGED][]
N/A |Added |[SESSION_SAVE][]
N/A |Added |[SESSION_LOAD][]
N/A |Added |[UNFOCUS][]
**io** | |
reload_file() |Renamed |[buffer:reload()][]
save_file() |Renamed |[buffer:save()][]
Expand Down Expand Up @@ -1950,6 +1951,7 @@ section below.
[FIND_TEXT_CHANGED]: api.html#events.FIND_TEXT_CHANGED
[SESSION_SAVE]: api.html#events.SESSION_SAVE
[SESSION_LOAD]: api.html#events.SESSION_LOAD
[UNFOCUS]: api.html#events.UNFOCUS
[buffer:reload()]: api.html#buffer.reload
[buffer:save()]: api.html#buffer.save
[buffer:save_as()]: api.html#buffer.save_as
Expand Down
1 change: 1 addition & 0 deletions modules/lua/ta_api
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ TD_STRIKEOUT view.TD_STRIKEOUT (number, Read-only)\n
TIME_FOREVER view.TIME_FOREVER (number, Read-only)\n
TYPE lexer.TYPE (string)\nThe token name for type tokens.
TYPEDEF textadept.editing.XPM_IMAGES.TYPEDEF (table)\nThe image number for type definitions.
UNFOCUS events.UNFOCUS (string)\nEmitted when Textadept loses focus.\nThis event is never emitted when Textadept is running in the terminal.
UPDATE_CONTENT buffer.UPDATE_CONTENT (number, Read-only)\n
UPDATE_H_SCROLL view.UPDATE_H_SCROLL (number, Read-only)\n
UPDATE_SELECTION buffer.UPDATE_SELECTION (number, Read-only)\n
Expand Down
1 change: 1 addition & 0 deletions modules/lua/ta_tags
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ TD_STRIKEOUT _HOME/core/.view.luadoc /^module('view')$/;" F class:view
TIME_FOREVER _HOME/core/.view.luadoc /^module('view')$/;" F class:view
TYPE _HOME/lexers/lexer.lua /^module('lexer')]=]$/;" F class:lexer
TYPEDEF _HOME/modules/textadept/editing.lua /^M.XPM_IMAGES = {not CURSES and '/* XPM */static char *class[] = {/* columns rows colors chars-per-pixel */"16 16 10 1 "," c #000000",". c #001CD0","X c #008080","o c #0080E8","O c #00C0C0","+ c #24D0FC","@ c #00FFFF","# c #A4E8FC","$ c #C0FFFF","% c None",/* pixels */"%%%%% %%%%%%%%%","%%%% ## %%%%%%%","%%% ###++ %%%%%%","%% +++++. %%%%","%% oo++.. $$ %%","%% ooo.. $$$@@ %","%% ooo. @@@@@X %","%%% . OO@@XX %","%%% ## OOOXXX %","%% ###++ OOXX %%","% +++++. OX %%%","% oo++.. % %%%%","% ooo... %%%%%%%","% ooo.. %%%%%%%%","%% o. %%%%%%%%%","%%%% %%%%%%%%%%"};' or '*',not CURSES and '/* XPM */static char *namespace[] = {/* columns rows colors chars-per-pixel */"16 16 7 1 "," c #000000",". c #1D1D1D","X c #393939","o c #555555","O c #A8A8A8","+ c #AAAAAA","@ c None",/* pixels */"@@@@@@@@@@@@@@@@","@@@@+@@@@@@@@@@@","@@@.o@@@@@@@@@@@","@@@ +@@@@@@@@@@@","@@@ +@@@@@@@@@@@","@@+.@@@@@@@+@@@@","@@+ @@@@@@@o.@@@","@@@ +@@@@@@+ @@@","@@@ +@@@@@@+ @@@","@@@.X@@@@@@@.+@@","@@@@+@@@@@@@ @@@","@@@@@@@@@@@+ @@@","@@@@@@@@@@@+ @@@","@@@@@@@@@@@X.@@@","@@@@@@@@@@@+@@@@","@@@@@@@@@@@@@@@@"};' or '@',not CURSES and '/* XPM */static char *method[] = {/* columns rows colors chars-per-pixel */"16 16 5 1 "," c #000000",". c #E0BC38","X c #F0DC5C","o c #FCFC80","O c None",/* pixels */"OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOO OOOO","OOOOOOOOO oo OO","OOOOOOOO ooooo O","OOOOOOO ooooo. O","OOOO O XXoo.. O","OOO oo XXX... O","OO ooooo XX.. OO","O ooooo. X. OOO","O XXoo.. O OOOO","O XXX... OOOOOOO","O XXX.. OOOOOOOO","OO X. OOOOOOOOO","OOOO OOOOOOOOOO"};' or '+',not CURSES and '/* XPM */static char *signal[] = {/* columns rows colors chars-per-pixel */"16 16 6 1 "," c #000000",". c #FF0000","X c #E0BC38","o c #F0DC5C","O c #FCFC80","+ c None",/* pixels */"++++++++++++++++","++++++++++++++++","++++++++++++++++","++++++++++ ++++","+++++++++ OO ++","++++++++ OOOOO +","+++++++ OOOOOX +","++++ + ooOOXX +","+++ OO oooXXX +","++ OOOOO ooXX ++","+ OOOOOX oX +++","+ ooOOXX + ++++","+ oooXXX +++++++","+ oooXX +++++..+","++ oX ++++++..+","++++ ++++++++++"};' or '~',not CURSES and '/* XPM */static char *slot[] = {/* columns rows colors chars-per-pixel */"16 16 5 1 "," c #000000",". c #E0BC38","X c #F0DC5C","o c #FCFC80","O c None",/* pixels */"OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOO OOOO","OOOOOOOOO oo OO","OOOOOOOO ooooo O","OOOOOOO ooooo. O","OOOO O XXoo.. O","OOO oo XXX... O","OO ooooo XX.. OO","O ooooo. X. OOO","O XXoo.. O OOOO","O XXX... OOOOOOO","O XXX.. OOOOO ","OO X. OOOOOO O ","OOOO OOOOOOO "};' or '-',not CURSES and '/* XPM */static char *variable[] = {/* columns rows colors chars-per-pixel */"16 16 5 1 "," c #000000",". c #8C748C","X c #9C94A4","o c #ACB4C0","O c None",/* pixels */"OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOOOOOOOOO","OOOOOOOOO OOOOO","OOOOOOOO oo OOO","OOOOOOO ooooo OO","OOOOOO ooooo. OO","OOOOOO XXoo.. OO","OOOOOO XXX... OO","OOOOOO XXX.. OOO","OOOOOOO X. OOOO","OOOOOOOOO OOOOO","OOOOOOOOOOOOOOOO"};' or '.',not CURSES and '/* XPM */static char *struct[] = {/* columns rows colors chars-per-pixel */"16 16 14 1 "," c #000000",". c #008000","X c #00C000","o c #00FF00","O c #808000","+ c #C0C000","@ c #FFFF00","# c #008080","$ c #00C0C0","% c #00FFFF","& c #C0FFC0","* c #FFFFC0","= c #C0FFFF","- c None",/* pixels */"----- ---------","---- && -------","--- &&&oo ------","-- ooooo. ----","-- XXoo.. == --","-- XXX.. ===%% -","-- XXX. %%%%%# -","--- . $$%%## -","--- ** $$$### -","-- ***@@ $$## --","- @@@@@O $# ---","- ++@@OO - ----","- +++OOO -------","- +++OO --------","-- +O ---------","---- ----------"};' or '}',not CURSES and '/* XPM */static char *typedef[] = {/* columns rows colors chars-per-pixel */"16 16 10 1 "," c #000000",". c #404040","X c #6D6D6D","o c #777777","O c #949494","+ c #ACACAC","@ c #BBBBBB","# c #DBDBDB","$ c #EEEEEE","% c None",/* pixels */"%%%%% %%%%%%%%%","%%%% ## %%%%%%%","%%% ###++ %%%%%%","%% +++++. %%%%","%% oo++.. $$ %%","%% ooo.. $$$@@ %","%% ooo. @@@@@X %","%%% . OO@@XX %","%%% ## OOOXXX %","%% ###++ OOXX %%","% +++++. OX %%%","% oo++.. % %%%%","% ooo... %%%%%%%","% ooo.. %%%%%%%%","%% o. %%%%%%%%%","%%%% %%%%%%%%%%"};' or ':',CLASS=1,NAMESPACE=2,METHOD=3,SIGNAL=4,SLOT=5,VARIABLE=6,STRUCT=7,TYPEDEF=8}$/;" F class:textadept.editing.XPM_IMAGES
UNFOCUS _HOME/core/events.lua /^module('events')]]$/;" F class:events
UPDATE_CONTENT _HOME/core/.buffer.luadoc /^module('buffer')$/;" F class:buffer
UPDATE_H_SCROLL _HOME/core/.view.luadoc /^module('view')$/;" F class:view
UPDATE_SELECTION _HOME/core/.buffer.luadoc /^module('buffer')$/;" F class:buffer
Expand Down
15 changes: 10 additions & 5 deletions src/textadept.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ static lua_State *lua;
#if CURSES
static bool quitting;
#endif
static bool initing, closing, show_tabs = true, tab_sync;
static bool initing, closing, show_tabs = true, tab_sync, dialog_active;
enum {SVOID, SINT, SLEN, SINDEX, SCOLOR, SBOOL, SKEYMOD, SSTRING, SSTRINGRET};

// Forward declarations.
Expand Down Expand Up @@ -590,7 +590,8 @@ static int dialog(lua_State *L) {
gtdialog_set_progressbar_callback(work, L);
} else argv[i++] = luaL_checkstring(L, j);
argv[argc] = NULL;
char *out = gtdialog(type, argc, argv);
char *out;
dialog_active = true, out = gtdialog(type, argc, argv), dialog_active = false;
return (lua_pushstring(L, out), free(out), 1);
}

Expand Down Expand Up @@ -2278,13 +2279,17 @@ static GtkWidget *new_findbox() {
}

/**
* Signal for window or command entry focus loss.
* Emit "Escape" key for the command entry on focus lost unless the window is
* losing focus or the application is quitting.
*/
static bool focus_lost(GtkWidget *widget, GdkEvent *_, void *L) {
if (widget == window && command_entry_active) return true; // halt
if (widget != command_entry || closing) return false;
return (emit(L, "keypress", LUA_TNUMBER, GDK_KEY_Escape, -1), false);
if (widget == window) {
if (!dialog_active) emit(L, "unfocus", -1);
if (command_entry_active) return true; // keep focus if window losing focus
} else if (!closing)
emit(L, "keypress", LUA_TNUMBER, GDK_KEY_Escape, -1);
return false;
}
#endif // if GTK

Expand Down

0 comments on commit 4d661eb

Please sign in to comment.