From bcefbc978f53b4bb5c9f667f47c5883bc8e9b066 Mon Sep 17 00:00:00 2001 From: "K. Isom" Date: Mon, 23 Nov 2026 11:36:38 -0800 Subject: [PATCH] kge: `: font` sticks; one font per frame from the focused window `: font NAME` was reverted on the next frame because the per-window chooser re-applied the config's code/writing font, and with two windows in different modes the font flipped every frame. Fonts per edit mode now live on the primary Editor (EditorFonts), shared with secondary windows like the buffer list. `: font` sets the font for the current buffer's mode, and the frontend picks one font per frame from the window with keyboard focus. Co-Authored-By: Claude Opus 5.5 --- CONFIG.md | 10 +++++--- Command.cc | 9 ++++++- Editor.h | 45 +++++++++++++++++++++++++++++++++ HelpText.cc | 2 +- ImGuiFrontend.cc | 43 ++++++++++++++++++------------- README.md | 7 ++--- docs/kge.1 | 11 +++++++- docs/kte.1 | 3 ++- tests/test_audit_regressions.cc | 20 +++++++++++++++ 9 files changed, 122 insertions(+), 28 deletions(-) diff --git a/CONFIG.md b/CONFIG.md index 5f2f827..0197ce3 100644 --- a/CONFIG.md +++ b/CONFIG.md @@ -67,10 +67,12 @@ Each buffer is drawn in the font for its edit mode, so in practice `code` and `writing` are the fonts you see; `name` only supplies their defaults. -`"default"` is Berkeley Mono Bold (the same as `berkeley-bold`). Set -fonts here; the size can also change while kge runs, with -`: font-size NUM` (press `C-k ;` for the prompt) or Ctrl `+`/`-`/`0` -(Cmd on macOS). Ctrl+0 goes back to `size`. +`"default"` is Berkeley Mono Bold (the same as `berkeley-bold`). While +kge runs, `: font NAME` (press `C-k ;` for the prompt) changes the font +for the current buffer's edit mode, and `: font-size NUM` or Ctrl +`+`/`-`/`0` (Cmd on macOS) the size; Ctrl+0 goes back to `size`. These +last until you quit. Only one font is shown at a time, so with several +windows open the focused window's buffer decides. ### `[appearance]` diff --git a/Command.cc b/Command.cc index 5e67808..fc76ecc 100644 --- a/Command.cc +++ b/Command.cc @@ -1870,8 +1870,15 @@ cmd_font_set_by_name(const CommandContext &ctx) if (size <= 0.0f) size = 16.0f; } + // The font belongs to the current buffer's edit mode: record it there, + // or the frontend's per-mode font choice would switch straight back. + EditMode mode = EditMode::Code; + if (const Buffer *b = ctx.editor.CurrentBuffer()) + mode = b->GetEditMode(); + ctx.editor.Fonts().SetForMode(mode, name); reg.RequestLoadFont(name, size); - ctx.editor.SetStatus(std::string("Font: ") + name + " (" + std::to_string((int) std::round(size)) + ")"); + ctx.editor.SetStatus(std::string(mode == EditMode::Writing ? "Writing font: " : "Code font: ") + name + " (" + + std::to_string((int) std::round(size)) + ")"); return true; } #else diff --git a/Editor.h b/Editor.h index 8fcd3b2..1f04258 100644 --- a/Editor.h +++ b/Editor.h @@ -49,6 +49,28 @@ #include "Swap.h" +// The GUI font for each edit mode. These are application settings, not +// per-window ones: there is one loaded font, so every window's Editor must +// agree on them (secondary windows share the primary's; SetSharedFonts). +// Seeded from kge.toml; `: font` changes the current buffer's mode's entry. +struct EditorFonts { + std::string code_font = "default"; + std::string writing_font = "crimsonpro"; + + + [[nodiscard]] const std::string &ForMode(EditMode m) const + { + return m == EditMode::Writing ? writing_font : code_font; + } + + + void SetForMode(EditMode m, const std::string &name) + { + (m == EditMode::Writing ? writing_font : code_font) = name; + } +}; + + class Editor { public: Editor(); @@ -590,6 +612,26 @@ public: } + // Per-mode GUI fonts. A secondary window's Editor points at the + // primary's, like the shared buffer list. + void SetSharedFonts(EditorFonts *shared) + { + shared_fonts_ = shared; + } + + + [[nodiscard]] EditorFonts &Fonts() + { + return shared_fonts_ ? *shared_fonts_ : fonts_; + } + + + [[nodiscard]] const EditorFonts &Fonts() const + { + return shared_fonts_ ? *shared_fonts_ : fonts_; + } + + // Swap manager access (for advanced integrations/tests) [[nodiscard]] kte::SwapManager *Swap() { @@ -641,6 +683,9 @@ private: std::unique_ptr swap_; kte::SwapManager *shared_swap_ = nullptr; // set with shared_buffers_; not owned + EditorFonts fonts_; + EditorFonts *shared_fonts_ = nullptr; // the primary's, in a secondary window; not owned + // Kill ring (Emacs-like) std::vector kill_ring_; std::size_t kill_ring_max_ = 60; diff --git a/HelpText.cc b/HelpText.cc index 8ab8a52..a517aa2 100644 --- a/HelpText.cc +++ b/HelpText.cc @@ -101,7 +101,7 @@ HelpText::Text() " orbital, plan9, solarized, tufte, weyland-yutani,\n" " zenburn)\n" " : background MODE Background: light | dark\n" - " : font NAME Set font (tab completes; b612, berkeley,\n" + " : font NAME Font for this edit mode (tab completes; b612, berkeley,\n" " berkeley-bold, brassmono, brassmono-bold,\n" " brassmonocode, brassmonocode-bold, crimsonpro,\n" " default, etbook, fira, go, ibm, idealist,\n" diff --git a/ImGuiFrontend.cc b/ImGuiFrontend.cc index 3d0f7d2..5f57e7f 100644 --- a/ImGuiFrontend.cc +++ b/ImGuiFrontend.cc @@ -157,6 +157,8 @@ GUIFrontend::Init(int &argc, char **argv, Editor &ed) // Load GUI configuration (fullscreen, columns/rows, font size, theme, background) config_ = GUIConfig::Load(); + ed.Fonts().code_font = config_.code_font; + ed.Fonts().writing_font = config_.writing_font; if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) != 0) { return false; @@ -360,6 +362,7 @@ GUIFrontend::OpenNewWindow_(Editor &primary) // Secondary editor shares the primary's buffer list ws->editor.SetSharedBuffers(&primary.Buffers(), primary.Swap()); + ws->editor.SetSharedFonts(&primary.Fonts()); ws->editor.SetDimensions(primary.Rows(), primary.Cols()); // Open a new untitled buffer and switch to it in the new window. @@ -568,23 +571,6 @@ GUIFrontend::Step(Editor &ed, bool &running) running = false; } - // Switch font based on current buffer's edit mode (deferred to next frame) - { - Buffer *cur = wed.CurrentBuffer(); - if (cur) { - auto &fr = kte::Fonts::FontRegistry::Instance(); - const std::string &expected = - (cur->GetEditMode() == EditMode::Writing) - ? config_.writing_font - : config_.code_font; - if (fr.CurrentFontName() != expected && fr.HasFont(expected)) { - float sz = fr.CurrentFontSize(); - if (sz <= 0.0f) sz = config_.font_size; - fr.RequestLoadFont(expected, sz); - } - } - } - // Draw. Every frame repaints the whole window, so a full-redraw // request (C-l, reload) needs nothing more than clearing. (void) wed.ConsumeFullRedraw(); @@ -601,6 +587,29 @@ GUIFrontend::Step(Editor &ed, bool &running) SDL_GL_SwapWindow(ws.window); } + // Pick the font for the focused window's buffer's edit mode (applied at + // the start of the next Step). There is one loaded font for all windows, + // so choose once: when each window asked for its own buffer's mode, a + // code window and a writing window swapped the font every frame. + { + SDL_Window *focus = SDL_GetKeyboardFocus(); + Editor *fed = &ed; + for (std::size_t wi = 1; wi < windows_.size(); ++wi) { + if (windows_[wi]->alive && windows_[wi]->window == focus) + fed = &windows_[wi]->editor; + } + if (const Buffer *cur = fed->CurrentBuffer()) { + auto &fr = kte::Fonts::FontRegistry::Instance(); + const std::string &expected = ed.Fonts().ForMode(cur->GetEditMode()); + if (fr.CurrentFontName() != expected && fr.HasFont(expected)) { + float sz = fr.CurrentFontSize(); + if (sz <= 0.0f) + sz = config_.font_size; + fr.RequestLoadFont(expected, sz); + } + } + } + // Handle deferred new-window requests (must happen outside the render loop // to avoid corrupting an in-progress ImGui frame). for (std::size_t wi = 0; wi < windows_.size(); ++wi) { diff --git a/README.md b/README.md index 5af6b83..353b1d7 100644 --- a/README.md +++ b/README.md @@ -139,6 +139,7 @@ a command and press Enter; Tab completes. For example: : syntax off : set filetype=python : theme gruvbox (kge) +: font iosevka (kge) : font-size 20 (kge) ``` @@ -149,9 +150,9 @@ time you open the file kte asks whether to recover them. ### kge: themes, fonts and windows -kge has 15 color themes and two dozen built-in fonts. Switch themes -while you work with `: theme NAME`, and change the text size with Ctrl -`+`/`-`/`0` (Cmd on macOS). Fonts are chosen in the config file below. `C-k m` switches between code +kge has 15 color themes and two dozen built-in fonts. Switch them +while you work with `: theme NAME` and `: font NAME`, and change the +text size with Ctrl `+`/`-`/`0` (Cmd on macOS). `C-k m` switches between code mode (a monospace font) and writing mode (a proportional font, used by default for `.md`, `.txt` and similar files). Cmd+N (macOS) or Ctrl+Shift+N opens another window onto the same buffers. diff --git a/docs/kge.1 b/docs/kge.1 index 97da423..4c539e4 100644 --- a/docs/kge.1 +++ b/docs/kge.1 @@ -291,6 +291,10 @@ Switch to another color theme; see .BR background " " light | dark Choose the light or dark version of themes that have both. .TP +.BI "font " name +Use another font for the current buffer's edit mode until you quit; see +.BR "FONTS AND EDIT MODES" . +.TP .BI "font\-size " pixels Set the text size until you quit. .SH FONTS AND EDIT MODES @@ -320,7 +324,12 @@ Proportional: crimsonpro, etbook, spectral. is Berkeley Mono Bold, the same as berkeley\-bold. .RE .PP -Choose fonts in the configuration file below. +To try a font, use +.BR ":\ font" ; +it changes the font for the current buffer's mode until you quit. To +keep it, set it in the configuration file below. Only one font is shown +at a time, so with several windows open the focused window's buffer +decides. .SH THEMES amber, eink, everforest, gruvbox, kanagawa\-paper, lcars, leuchtturm, nord, old\-book, orbital, plan9, solarized, tufte, weyland\-yutani and diff --git a/docs/kte.1 b/docs/kte.1 index 50be639..e4b7f17 100644 --- a/docs/kte.1 +++ b/docs/kte.1 @@ -272,7 +272,8 @@ Switch, or set, the buffer's edit mode. .PP The GUI-only commands .BR theme , -.B background +.BR background , +.B font and .B font\-size are described in diff --git a/tests/test_audit_regressions.cc b/tests/test_audit_regressions.cc index 2c9634b..873e51b 100644 --- a/tests/test_audit_regressions.cc +++ b/tests/test_audit_regressions.cc @@ -2429,3 +2429,23 @@ TEST(Redraw_RefreshAndReloadRequestFullRedraw) ASSERT_TRUE(h.Exec(CommandId::ReloadBuffer)); ASSERT_TRUE(ed.ConsumeFullRedraw()); } + + +// Per-mode fonts are editor state shared by every window, so `: font` +// (which only sees the Editor) and the GUI's per-frame font choice agree. +TEST(EditorFonts_PerModeAndSharedAcrossWindows) +{ + Editor primary; + ASSERT_EQ(primary.Fonts().ForMode(EditMode::Code), std::string("default")); + ASSERT_EQ(primary.Fonts().ForMode(EditMode::Writing), std::string("crimsonpro")); + + primary.Fonts().SetForMode(EditMode::Code, "iosevka"); + ASSERT_EQ(primary.Fonts().code_font, std::string("iosevka")); + ASSERT_EQ(primary.Fonts().writing_font, std::string("crimsonpro")); + + Editor secondary; + secondary.SetSharedFonts(&primary.Fonts()); + secondary.Fonts().SetForMode(EditMode::Writing, "etbook"); + ASSERT_EQ(primary.Fonts().ForMode(EditMode::Writing), std::string("etbook")); + ASSERT_EQ(secondary.Fonts().ForMode(EditMode::Code), std::string("iosevka")); +}