japanoise / emsys

ersatz-emacs text editor
MIT License
3 stars 2 forks source link

Adds syntax sugar focusWin on the editorConfig struct #31

Closed nicholascarroll closed 1 month ago

nicholascarroll commented 1 month ago

This PR is from issue #29. Now that I've done it and looked at it, I realized that for this small project, the benefit is pretty small. Hmm!

A new way to access the current window:

struct editorWindow *currentWindow = E->focusWin;
struct editorBuffer *buf = E->focusWin->buf;

// Check if a specific window is focused
if (E->windows[i] == E->focusWin) {
    // This window is focused
}

// Switch to the next window
int nextIdx = (E->focusWin - E->windows + 1) % E->nwindows;
setWindowFocus(&E, E->windows[nextIdx]);

Or you can still do it the old way:

int idx = windowFocusedIdx(&E);
struct editorWindow *win = E.windows[idx];
struct editorBuffer *buf = win->buf;

// Check if a specific window is focused
if (E.windows[i]->focused) {
    // This window is focused
}

// Switch to the next window
E.windows[idx]->focused = 0;
idx = (idx + 1) % E->nwindows;
E.windows[idx]->focused = 1;
E.focusBuf = E.windows[idx]->buf;

But you can only read _focused on the editorWindow - you must NEVER update that field directly. To update it, use the new setWindowFocus function:

// To change focus to a different window
setWindowFocus(&E, E.windows[newFocusIdx]);

// This will update _focused, E.focusWin, and E.focusBuf all at once
nicholascarroll commented 1 month ago

I changed my mind. As a beginner in C, I am not sure enough that this is worthwhile doing.