Skip to content

Commit

Permalink
better mode change scripting
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Feb 16, 2022
1 parent 36157ae commit de2c039
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 40 deletions.
1 change: 1 addition & 0 deletions examples/blacklist
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ Alacritty
iTerm2
Kitty
MacVim
Neovide
Terminal
32 changes: 26 additions & 6 deletions examples/svim.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,32 @@
# The variables $MODE and $CMDLINE hold the
# current editor and cmdline info

# sketchybar --set svim.mode icon="[$MODE]" \
# --set svim.cmdline label="$CMDLINE"
# COLOR=0xff9dd274
# if [ "$MODE" = "" ]; then
# COLOR=0xffff6578
# fi
#
# DRAW_CMD="off"
# if [ "$CMDLINE" != "" ]; then
# sketchybar --set center popup.drawing=on
# else
# sketchybar --set center popup.drawing=off
# DRAW_CMD="on"
# fi

#
# sketchybar --set svim.mode label="[$MODE]" \
# label.drawing=$(if [ "$MODE" = "" ]; then echo "off"; else echo "on"; fi) \
# icon.color=$COLOR \
# popup.drawing=$DRAW_CMD \
# --set svim.cmdline label="$CMDLINE"
#
# This is the setup command needed for sketchybar in sketchybarrc:
#
# sketchybar --add item svim.mode right \
# --set svim.mode popup.align=right \
# icon= \
# icon.font="Hack Nerd Font Mono:Bold:28.0" \
# label.font="Hack Nerd Font Mono:Bold:13.0" \
# icon.color=0xffff6578 \
# script="sketchybar --set svim.mode popup.drawing=off" \
# --subscribe svim.mode front_app_switched window_focus \
# --add item svim.cmdline popup.svim.mode \
# --set svim.cmdline icon="Command: "
#
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION = "1.0.4"
VERSION = "1.0.5"
CC = clang
DEFINES = -DHAVE_CONFIG_H -DMACOS_X -DMACOS_X_DARWIN
LIBS = lib/libvim.a -lm -lncurses -liconv -framework Carbon -framework Cocoa
Expand Down
12 changes: 9 additions & 3 deletions src/ax.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ bool ax_get_selected_element(struct ax* ax) {
}

ax->role = role;
if (ax->selected_element) CFRelease(ax->selected_element);
ax->selected_element = selected_element;

return (error == kAXErrorSuccess) && role;
Expand Down Expand Up @@ -229,33 +228,40 @@ CGEventRef ax_process_event(struct ax* ax, CGEventRef event) {
// case K: {
// const UniChar down = DOWN;
// CGEventKeyboardSetUnicodeString(event, 1, &down);
// CGEventSetIntegerValueField(event, kCGKeyboardEventAutorepeat, false);
// CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 125);
// } break;
// case L: {
// const UniChar up = UP;
// CGEventKeyboardSetUnicodeString(event, 1, &up);
// CGEventSetIntegerValueField(event, kCGKeyboardEventAutorepeat, false);
// CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 126);
// } break;
// case J: {
// const UniChar left = LEFT;
// CGEventKeyboardSetUnicodeString(event, 1, &left);
// CGEventSetIntegerValueField(event, kCGKeyboardEventAutorepeat, false);
// CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 123);
// } break;
// case OE: {
// const UniChar right = RIGHT;
// CGEventKeyboardSetUnicodeString(event, 1, &right);
// CGEventSetIntegerValueField(event, kCGKeyboardEventAutorepeat, false);
// CGEventSetIntegerValueField(event, kCGKeyboardEventKeycode, 124);
// } break;
// }
// }
// }

return event;
}

void ax_clear(struct ax* ax) {
buffer_clear(&ax->buffer);
if (ax->selected_element) CFRelease(ax->selected_element);
if (ax->selected_element && ax->role == ROLE_TEXT) {
buffer_call_script(&ax->buffer, false);
}

if (ax->selected_element) CFRelease(ax->selected_element);
ax->role = 0;
ax->selected_element = NULL;
ax->is_supported = false;
Expand Down
3 changes: 0 additions & 3 deletions src/ax.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@
#define K 0x6B
#define L 0x6C
#define OE 0xF6
//////////////////////

#define kAXWebAreaRole CFSTR("AXWebArea")

extern char* cfstring_get_cstring(CFStringRef text_ref);

Expand Down
51 changes: 26 additions & 25 deletions src/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,12 @@ bool buffer_sync_cmdline(struct buffer* buffer) {
return true;
}

// TODO: Hook the event when an item is deselected and call cleanup script
void buffer_sync(struct buffer* buffer) {
bool did_change = buffer_sync_mode(buffer);

buffer_sync_text(buffer);

did_change |= buffer_sync_cmdline(buffer);
buffer_sync_cursor(buffer);
void buffer_call_script(struct buffer* buffer, bool supported) {
struct env_vars env_vars;
env_vars_init(&env_vars);
char mode_str[3] = "";

if (did_change) {
char mode_str[3];
if (supported) {
if (buffer->cursor.mode & INSERT) {
snprintf(mode_str, 2, "I");
}
Expand All @@ -170,28 +165,34 @@ void buffer_sync(struct buffer* buffer) {
else if (buffer->cursor.mode & CMDLINE) {
snprintf(mode_str, 2, "C");
}
// TODO: Add pending command modes
else
snprintf(mode_str, 2, "");

mode_str[2] = '\0';

struct env_vars env_vars;
env_vars_init(&env_vars);
env_vars_set(&env_vars, string_copy("MODE"),
string_copy(mode_str));
snprintf(mode_str, 2, "_");

env_vars_set(&env_vars, string_copy("CMDLINE"),
string_copy((buffer->command_line.raw
? buffer->command_line.raw
: "")));

char* home = getenv("HOME");
char buf[512];
snprintf(buf, sizeof(buf), "%s/%s", home, ".config/svim/svim.sh");
vfork_exec(buf, &env_vars);
env_vars_destroy(&env_vars);
}

env_vars_set(&env_vars, string_copy("MODE"),
string_copy(mode_str));

char* home = getenv("HOME");
char buf[512];
snprintf(buf, sizeof(buf), "%s/%s", home, ".config/svim/svim.sh");
vfork_exec(buf, &env_vars);
env_vars_destroy(&env_vars);
}

void buffer_sync(struct buffer* buffer) {
bool did_change = buffer_sync_mode(buffer);

buffer_sync_text(buffer);

did_change |= buffer_sync_cmdline(buffer);
buffer_sync_cursor(buffer);

if (did_change) buffer_call_script(buffer, true);
}

void buffer_input(struct buffer* buffer, UniChar key, UniCharCount count) {
Expand Down
1 change: 1 addition & 0 deletions src/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ void buffer_input(struct buffer* buffer, UniChar key, UniCharCount count);
void buffer_clear(struct buffer* buffer);
void buffer_revsync_text(struct buffer* buffer);
void buffer_revsync_cursor(struct buffer* buffer);
void buffer_call_script(struct buffer* buffer, bool supported);

struct line* line_create();
void line_destroy(struct line* line);
6 changes: 5 additions & 1 deletion src/event_tap.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,12 @@ static CGEventRef key_handler(CGEventTapProxy proxy, CGEventType type,
CGEventTapEnable(((struct event_tap*) reference)->handle, true);
} break;
case kCGEventKeyDown: {
if (event_tap_pid_blacklisted((struct event_tap*) reference))
if (event_tap_pid_blacklisted((struct event_tap*) reference)) {
if (g_ax.selected_element && g_ax.role) {
ax_clear(&g_ax);
}
return event;
}

return ax_process_event(&g_ax, event);
} break;
Expand Down
2 changes: 1 addition & 1 deletion src/workspace.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <Cocoa/Cocoa.h>
#pragma once
#include <Cocoa/Cocoa.h>

char* g_front_app;

Expand Down

0 comments on commit de2c039

Please sign in to comment.