5 Commits

Author SHA1 Message Date
0cb7d36f2a bump version
Some checks failed
Release / Bump Homebrew formula (push) Has been cancelled
Release / Build Linux amd64 (push) Has been cancelled
Release / Build Linux arm64 (push) Has been cancelled
Release / Build macOS arm64 (.app) (push) Has been cancelled
Release / Create GitHub Release (push) Has been cancelled
2025-12-01 11:22:47 -08:00
09a6df0c33 Add regex search, search/replace, and buffer read-only mode functionality with help text 2025-12-01 02:54:40 -08:00
69457c424c add regex and search/replace functionality to editor 2025-11-30 23:33:17 -08:00
24c8040d8a trash flake-gui 2025-11-30 22:02:43 -08:00
e869249a7c thaaaaanks jeremy 2025-11-30 22:02:23 -08:00
19 changed files with 1328 additions and 450 deletions

2
.gitignore vendored
View File

@@ -1,3 +1,5 @@
!.idea !.idea
cmake-build* cmake-build*
build build
/imgui.ini
result

42
.idea/workspace.xml generated
View File

@@ -33,10 +33,20 @@
</configurations> </configurations>
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="e1fe3ab0-3650-4fca-8664-a247d5dfa457" name="Changes" comment="Actually add the screenshot."> <list default="true" id="e1fe3ab0-3650-4fca-8664-a247d5dfa457" name="Changes" comment="add regex and search/replace functionality to editor">
<change afterPath="$PROJECT_DIR$/HelpText.cc" afterDir="false" />
<change afterPath="$PROJECT_DIR$/HelpText.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Buffer.cc" beforeDir="false" afterPath="$PROJECT_DIR$/Buffer.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Buffer.h" beforeDir="false" afterPath="$PROJECT_DIR$/Buffer.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" /> <change beforePath="$PROJECT_DIR$/CMakeLists.txt" beforeDir="false" afterPath="$PROJECT_DIR$/CMakeLists.txt" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Command.cc" beforeDir="false" afterPath="$PROJECT_DIR$/Command.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Command.h" beforeDir="false" afterPath="$PROJECT_DIR$/Command.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/Editor.h" beforeDir="false" afterPath="$PROJECT_DIR$/Editor.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/GUIRenderer.cc" beforeDir="false" afterPath="$PROJECT_DIR$/GUIRenderer.cc" afterDir="false" /> <change beforePath="$PROJECT_DIR$/GUIRenderer.cc" beforeDir="false" afterPath="$PROJECT_DIR$/GUIRenderer.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/KKeymap.cc" beforeDir="false" afterPath="$PROJECT_DIR$/KKeymap.cc" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ROADMAP.md" beforeDir="false" afterPath="$PROJECT_DIR$/ROADMAP.md" afterDir="false" />
<change beforePath="$PROJECT_DIR$/TerminalRenderer.cc" beforeDir="false" afterPath="$PROJECT_DIR$/TerminalRenderer.cc" afterDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@@ -63,10 +73,14 @@
<component name="HighlightingSettingsPerFile"> <component name="HighlightingSettingsPerFile">
<setting file="mock:///AIAssistantSnippet.." root0="SKIP_HIGHLIGHTING" /> <setting file="mock:///AIAssistantSnippet.." root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///AIAssistantSnippet.." root0="SKIP_HIGHLIGHTING" /> <setting file="mock:///AIAssistantSnippet.." root0="SKIP_HIGHLIGHTING" />
<setting file="mock:///AIAssistantSnippet.." root0="SKIP_HIGHLIGHTING" />
</component> </component>
<component name="OptimizeOnSaveOptions"> <component name="OptimizeOnSaveOptions">
<option name="myRunOnSave" value="true" /> <option name="myRunOnSave" value="true" />
</component> </component>
<component name="ProblemsViewState">
<option name="selectedTabId" value="AISelfReview" />
</component>
<component name="ProjectApplicationVersion"> <component name="ProjectApplicationVersion">
<option name="ide" value="CLion" /> <option name="ide" value="CLion" />
<option name="majorVersion" value="2025" /> <option name="majorVersion" value="2025" />
@@ -139,7 +153,7 @@
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" /> <option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method> </method>
</configuration> </configuration>
<configuration name="kge" type="CMakeRunConfiguration" factoryName="Application" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" WORKING_DIR="file://$PROJECT_DIR$" PASS_PARENT_ENVS_2="true" PROJECT_NAME="kte" TARGET_NAME="kge" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="kte" RUN_TARGET_NAME="kge"> <configuration name="kge" type="CMakeRunConfiguration" factoryName="Application" PROGRAM_PARAMS="$PROJECT_DIR$/cmake-build-debug/test.txt" REDIRECT_INPUT="false" ELEVATE="false" USE_EXTERNAL_CONSOLE="false" EMULATE_TERMINAL="false" WORKING_DIR="file://$PROJECT_DIR$" PASS_PARENT_ENVS_2="true" PROJECT_NAME="kte" TARGET_NAME="kge" CONFIG_NAME="Debug" RUN_TARGET_PROJECT_NAME="kte" RUN_TARGET_NAME="kge">
<method v="2"> <method v="2">
<option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" /> <option name="com.jetbrains.cidr.execution.CidrBuildBeforeRunTaskProvider$BuildBeforeRunTask" enabled="true" />
</method> </method>
@@ -170,7 +184,7 @@
<workItem from="1764539556448" duration="156000" /> <workItem from="1764539556448" duration="156000" />
<workItem from="1764539725338" duration="1075000" /> <workItem from="1764539725338" duration="1075000" />
<workItem from="1764542392763" duration="3512000" /> <workItem from="1764542392763" duration="3512000" />
<workItem from="1764548345516" duration="12773000" /> <workItem from="1764548345516" duration="28341000" />
</task> </task>
<task id="LOCAL-00001" summary="Add undo/redo infrastructure and buffer management additions."> <task id="LOCAL-00001" summary="Add undo/redo infrastructure and buffer management additions.">
<option name="closed" value="true" /> <option name="closed" value="true" />
@@ -284,7 +298,23 @@
<option name="project" value="LOCAL" /> <option name="project" value="LOCAL" />
<updated>1764557759844</updated> <updated>1764557759844</updated>
</task> </task>
<option name="localTasksCounter" value="15" /> <task id="LOCAL-00015" summary="Fix void crash in kge.">
<option name="closed" value="true" />
<created>1764568264996</created>
<option name="number" value="00015" />
<option name="presentableId" value="LOCAL-00015" />
<option name="project" value="LOCAL" />
<updated>1764568264996</updated>
</task>
<task id="LOCAL-00016" summary="add regex and search/replace functionality to editor">
<option name="closed" value="true" />
<created>1764574397967</created>
<option name="number" value="00016" />
<option name="presentableId" value="LOCAL-00016" />
<option name="project" value="LOCAL" />
<updated>1764574397967</updated>
</task>
<option name="localTasksCounter" value="17" />
<servers /> <servers />
</component> </component>
<component name="TypeScriptGeneratedFilesManager"> <component name="TypeScriptGeneratedFilesManager">
@@ -312,7 +342,9 @@
<MESSAGE value="Introduce file picker and GUI configuration with enhancements.&#10;&#10;- Add visual file picker for GUI with toggle support.&#10;- Introduce `GUIConfig` class for loading GUI settings from configuration file.&#10;- Refactor window initialization to support dynamic sizing based on configuration.&#10;- Add macOS-specific handling for fullscreen behavior.&#10;- Improve header inclusion order and minor code cleanup." /> <MESSAGE value="Introduce file picker and GUI configuration with enhancements.&#10;&#10;- Add visual file picker for GUI with toggle support.&#10;- Introduce `GUIConfig` class for loading GUI settings from configuration file.&#10;- Refactor window initialization to support dynamic sizing based on configuration.&#10;- Add macOS-specific handling for fullscreen behavior.&#10;- Improve header inclusion order and minor code cleanup." />
<MESSAGE value="Add buffer position display and documentation improvements.&#10;&#10;- Display buffer position prefix &quot;[x/N]&quot; in GUI and terminal renderers.&#10;- Improve `kte` and `kge` man pages with frontend usage details and project homepage.&#10;- Update README with GUI invocation instructions.&#10;- Bump version to 1.0.0." /> <MESSAGE value="Add buffer position display and documentation improvements.&#10;&#10;- Display buffer position prefix &quot;[x/N]&quot; in GUI and terminal renderers.&#10;- Improve `kte` and `kge` man pages with frontend usage details and project homepage.&#10;- Update README with GUI invocation instructions.&#10;- Bump version to 1.0.0." />
<MESSAGE value="Actually add the screenshot." /> <MESSAGE value="Actually add the screenshot." />
<option name="LAST_COMMIT_MESSAGE" value="Actually add the screenshot." /> <MESSAGE value="Fix void crash in kge." />
<MESSAGE value="add regex and search/replace functionality to editor" />
<option name="LAST_COMMIT_MESSAGE" value="add regex and search/replace functionality to editor" />
</component> </component>
<component name="XSLT-Support.FileAssociations.UIState"> <component name="XSLT-Support.FileAssociations.UIState">
<expand /> <expand />

View File

@@ -36,6 +36,7 @@ Buffer::Buffer(const Buffer &other)
filename_ = other.filename_; filename_ = other.filename_;
is_file_backed_ = other.is_file_backed_; is_file_backed_ = other.is_file_backed_;
dirty_ = other.dirty_; dirty_ = other.dirty_;
read_only_ = other.read_only_;
mark_set_ = other.mark_set_; mark_set_ = other.mark_set_;
mark_curx_ = other.mark_curx_; mark_curx_ = other.mark_curx_;
mark_cury_ = other.mark_cury_; mark_cury_ = other.mark_cury_;
@@ -60,6 +61,7 @@ Buffer::operator=(const Buffer &other)
filename_ = other.filename_; filename_ = other.filename_;
is_file_backed_ = other.is_file_backed_; is_file_backed_ = other.is_file_backed_;
dirty_ = other.dirty_; dirty_ = other.dirty_;
read_only_ = other.read_only_;
mark_set_ = other.mark_set_; mark_set_ = other.mark_set_;
mark_curx_ = other.mark_curx_; mark_curx_ = other.mark_curx_;
mark_cury_ = other.mark_cury_; mark_cury_ = other.mark_cury_;
@@ -82,6 +84,7 @@ Buffer::Buffer(Buffer &&other) noexcept
filename_(std::move(other.filename_)), filename_(std::move(other.filename_)),
is_file_backed_(other.is_file_backed_), is_file_backed_(other.is_file_backed_),
dirty_(other.dirty_), dirty_(other.dirty_),
read_only_(other.read_only_),
mark_set_(other.mark_set_), mark_set_(other.mark_set_),
mark_curx_(other.mark_curx_), mark_curx_(other.mark_curx_),
mark_cury_(other.mark_cury_), mark_cury_(other.mark_cury_),
@@ -112,6 +115,7 @@ Buffer::operator=(Buffer &&other) noexcept
filename_ = std::move(other.filename_); filename_ = std::move(other.filename_);
is_file_backed_ = other.is_file_backed_; is_file_backed_ = other.is_file_backed_;
dirty_ = other.dirty_; dirty_ = other.dirty_;
read_only_ = other.read_only_;
mark_set_ = other.mark_set_; mark_set_ = other.mark_set_;
mark_curx_ = other.mark_curx_; mark_curx_ = other.mark_curx_;
mark_cury_ = other.mark_cury_; mark_cury_ = other.mark_cury_;
@@ -364,9 +368,9 @@ Buffer::insert_text(int row, int col, std::string_view text)
rows_[y].insert(x, seg); rows_[y].insert(x, seg);
x += seg.size(); x += seg.size();
// Split line at x // Split line at x
std::string tail = rows_[y].substr(x); std::string tail = rows_[y].substr(x);
rows_[y].erase(x); rows_[y].erase(x);
rows_.insert(rows_.begin() + static_cast<std::ptrdiff_t>(y + 1), tail); rows_.insert(rows_.begin() + static_cast<std::ptrdiff_t>(y + 1), Line(tail));
y += 1; y += 1;
x = 0; x = 0;
remain.erase(0, pos + 1); remain.erase(0, pos + 1);
@@ -426,8 +430,8 @@ Buffer::split_line(int row, const int col)
const auto y = static_cast<std::size_t>(row); const auto y = static_cast<std::size_t>(row);
const auto x = std::min<std::size_t>(static_cast<std::size_t>(col), rows_[y].size()); const auto x = std::min<std::size_t>(static_cast<std::size_t>(col), rows_[y].size());
const auto tail = rows_[y].substr(x); const auto tail = rows_[y].substr(x);
rows_[y].erase(x); rows_[y].erase(x);
rows_.insert(rows_.begin() + static_cast<std::ptrdiff_t>(y + 1), tail); rows_.insert(rows_.begin() + static_cast<std::ptrdiff_t>(y + 1), Line(tail));
} }
@@ -455,7 +459,7 @@ Buffer::insert_row(int row, const std::string_view text)
row = 0; row = 0;
if (static_cast<std::size_t>(row) > rows_.size()) if (static_cast<std::size_t>(row) > rows_.size())
row = static_cast<int>(rows_.size()); row = static_cast<int>(rows_.size());
rows_.insert(rows_.begin() + row, std::string(text)); rows_.insert(rows_.begin() + row, Line(std::string(text)));
} }

View File

@@ -16,7 +16,7 @@
class Buffer { class Buffer {
public: public:
Buffer(); Buffer();
Buffer(const Buffer &other); Buffer(const Buffer &other);
@@ -77,13 +77,13 @@ public:
Line() = default; Line() = default;
Line(const char *s) explicit Line(const char *s)
{ {
assign_from(s ? std::string(s) : std::string()); assign_from(s ? std::string(s) : std::string());
} }
Line(const std::string &s) explicit Line(const std::string &s)
{ {
assign_from(s); assign_from(s);
} }
@@ -139,29 +139,38 @@ public:
// conversions // conversions
operator std::string() const explicit operator std::string() const
{ {
return std::string(buf_.Data() ? buf_.Data() : "", buf_.Size()); return {buf_.Data() ? buf_.Data() : "", buf_.Size()};
} }
// string-like API used by command/renderer layers (implemented via materialization for now) // string-like API used by command/renderer layers (implemented via materialization for now)
std::string substr(std::size_t pos) const [[nodiscard]] std::string substr(std::size_t pos) const
{ {
const std::size_t n = buf_.Size(); const std::size_t n = buf_.Size();
if (pos >= n) if (pos >= n)
return std::string(); return {};
return std::string(buf_.Data() + pos, n - pos); return {buf_.Data() + pos, n - pos};
} }
std::string substr(std::size_t pos, std::size_t len) const [[nodiscard]] std::string substr(std::size_t pos, std::size_t len) const
{ {
const std::size_t n = buf_.Size(); const std::size_t n = buf_.Size();
if (pos >= n) if (pos >= n)
return std::string(); return {};
const std::size_t take = (pos + len > n) ? (n - pos) : len; const std::size_t take = (pos + len > n) ? (n - pos) : len;
return std::string(buf_.Data() + pos, take); return {buf_.Data() + pos, take};
}
// minimal find() to support search within a line
[[nodiscard]] std::size_t find(const std::string &needle, const std::size_t pos = 0) const
{
// Materialize to std::string for now; Line is backed by AppendBuffer
const auto s = static_cast<std::string>(*this);
return s.find(needle, pos);
} }
@@ -253,6 +262,14 @@ public:
return filename_; return filename_;
} }
// Set a virtual (non file-backed) display name for this buffer, e.g. "+HELP+"
// This does not mark the buffer as file-backed.
void SetVirtualName(const std::string &name)
{
filename_ = name;
is_file_backed_ = false;
}
[[nodiscard]] bool IsFileBacked() const [[nodiscard]] bool IsFileBacked() const
{ {
@@ -260,26 +277,42 @@ public:
} }
[[nodiscard]] bool Dirty() const [[nodiscard]] bool Dirty() const
{ {
return dirty_; return dirty_;
} }
// Read-only flag
[[nodiscard]] bool IsReadOnly() const
{
return read_only_;
}
void SetReadOnly(bool ro)
{
read_only_ = ro;
}
void ToggleReadOnly()
{
read_only_ = !read_only_;
}
void SetCursor(std::size_t x, std::size_t y) void SetCursor(const std::size_t x, const std::size_t y)
{ {
curx_ = x; curx_ = x;
cury_ = y; cury_ = y;
} }
void SetRenderX(std::size_t rx) void SetRenderX(const std::size_t rx)
{ {
rx_ = rx; rx_ = rx;
} }
void SetOffsets(std::size_t row, std::size_t col) void SetOffsets(const std::size_t row, const std::size_t col)
{ {
rowoffs_ = row; rowoffs_ = row;
coloffs_ = col; coloffs_ = col;
@@ -299,7 +332,7 @@ public:
} }
void SetMark(std::size_t x, std::size_t y) void SetMark(const std::size_t x, const std::size_t y)
{ {
mark_set_ = true; mark_set_ = true;
mark_curx_ = x; mark_curx_ = x;
@@ -344,20 +377,21 @@ public:
// Undo system accessors (created per-buffer) // Undo system accessors (created per-buffer)
UndoSystem *Undo(); UndoSystem *Undo();
const UndoSystem *Undo() const; [[nodiscard]] const UndoSystem *Undo() const;
private: private:
// State mirroring original C struct (without undo_tree) // State mirroring original C struct (without undo_tree)
std::size_t curx_ = 0, cury_ = 0; // cursor position in characters std::size_t curx_ = 0, cury_ = 0; // cursor position in characters
std::size_t rx_ = 0; // render x (tabs expanded) std::size_t rx_ = 0; // render x (tabs expanded)
std::size_t nrows_ = 0; // number of rows std::size_t nrows_ = 0; // number of rows
std::size_t rowoffs_ = 0, coloffs_ = 0; // viewport offsets std::size_t rowoffs_ = 0, coloffs_ = 0; // viewport offsets
std::vector<Line> rows_; // buffer rows (without trailing newlines) std::vector<Line> rows_; // buffer rows (without trailing newlines)
std::string filename_; std::string filename_;
bool is_file_backed_ = false; bool is_file_backed_ = false;
bool dirty_ = false; bool dirty_ = false;
bool mark_set_ = false; bool read_only_ = false;
std::size_t mark_curx_ = 0, mark_cury_ = 0; bool mark_set_ = false;
std::size_t mark_curx_ = 0, mark_cury_ = 0;
// Per-buffer undo state // Per-buffer undo state
std::unique_ptr<struct UndoTree> undo_tree_; std::unique_ptr<struct UndoTree> undo_tree_;

View File

@@ -4,14 +4,15 @@ project(kte)
include(GNUInstallDirs) include(GNUInstallDirs)
set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD 17)
set(KTE_VERSION "1.0.4") set(KTE_VERSION "1.0.5")
# Default to terminal-only build to avoid SDL/OpenGL dependency by default. # Default to terminal-only build to avoid SDL/OpenGL dependency by default.
# Enable with -DBUILD_GUI=ON when SDL2/OpenGL/Freetype are available. # Enable with -DBUILD_GUI=ON when SDL2/OpenGL/Freetype are available.
set(BUILD_GUI OFF CACHE BOOL "Enable building the graphical version.") set(BUILD_GUI ON CACHE BOOL "Enable building the graphical version.")
set(BUILD_TESTS OFF CACHE BOOL "Enable building test programs.") set(BUILD_TESTS OFF CACHE BOOL "Enable building test programs.")
option(KTE_USE_PIECE_TABLE "Use PieceTable instead of GapBuffer implementation" ON) option(KTE_USE_PIECE_TABLE "Use PieceTable instead of GapBuffer implementation" ON)
set(KTE_FONT_SIZE "18.0" CACHE STRING "Default font size for GUI") set(KTE_FONT_SIZE "18.0" CACHE STRING "Default font size for GUI")
option(KTE_UNDO_DEBUG "Enable undo instrumentation logs" OFF)
if (CMAKE_HOST_UNIX) if (CMAKE_HOST_UNIX)
message(STATUS "Build system is POSIX.") message(STATUS "Build system is POSIX.")
@@ -55,6 +56,7 @@ set(COMMON_SOURCES
Buffer.cc Buffer.cc
Editor.cc Editor.cc
Command.cc Command.cc
HelpText.cc
KKeymap.cc KKeymap.cc
TerminalInputHandler.cc TerminalInputHandler.cc
TerminalRenderer.cc TerminalRenderer.cc
@@ -74,6 +76,7 @@ set(COMMON_HEADERS
Editor.h Editor.h
AppendBuffer.h AppendBuffer.h
Command.h Command.h
HelpText.h
KKeymap.h KKeymap.h
InputHandler.h InputHandler.h
TerminalInputHandler.h TerminalInputHandler.h
@@ -99,6 +102,9 @@ add_executable(kte
if (KTE_USE_PIECE_TABLE) if (KTE_USE_PIECE_TABLE)
target_compile_definitions(kte PRIVATE KTE_USE_PIECE_TABLE=1) target_compile_definitions(kte PRIVATE KTE_USE_PIECE_TABLE=1)
endif () endif ()
if (KTE_UNDO_DEBUG)
target_compile_definitions(kte PRIVATE KTE_UNDO_DEBUG=1)
endif ()
target_link_libraries(kte ${CURSES_LIBRARIES}) target_link_libraries(kte ${CURSES_LIBRARIES})
@@ -121,6 +127,10 @@ if (BUILD_TESTS)
target_compile_definitions(test_undo PRIVATE KTE_USE_PIECE_TABLE=1) target_compile_definitions(test_undo PRIVATE KTE_USE_PIECE_TABLE=1)
endif () endif ()
if (KTE_UNDO_DEBUG)
target_compile_definitions(test_undo PRIVATE KTE_UNDO_DEBUG=1)
endif ()
target_link_libraries(test_undo ${CURSES_LIBRARIES}) target_link_libraries(test_undo ${CURSES_LIBRARIES})
endif () endif ()
@@ -153,6 +163,9 @@ if (${BUILD_GUI})
GUIFrontend.cc GUIFrontend.cc
GUIFrontend.h) GUIFrontend.h)
target_compile_definitions(kge PRIVATE KTE_BUILD_GUI=1 KTE_DEFAULT_GUI=1 KTE_FONT_SIZE=${KTE_FONT_SIZE}) target_compile_definitions(kge PRIVATE KTE_BUILD_GUI=1 KTE_DEFAULT_GUI=1 KTE_FONT_SIZE=${KTE_FONT_SIZE})
if (KTE_UNDO_DEBUG)
target_compile_definitions(kge PRIVATE KTE_UNDO_DEBUG=1)
endif ()
target_link_libraries(kge ${CURSES_LIBRARIES} imgui) target_link_libraries(kge ${CURSES_LIBRARIES} imgui)
# On macOS, build kge as a proper .app bundle # On macOS, build kge as a proper .app bundle

File diff suppressed because it is too large Load Diff

View File

@@ -23,8 +23,10 @@ enum class CommandId {
Refresh, // force redraw Refresh, // force redraw
KPrefix, // show "C-k _" prompt in status when entering k-command KPrefix, // show "C-k _" prompt in status when entering k-command
FindStart, // begin incremental search (placeholder) FindStart, // begin incremental search (placeholder)
RegexFindStart, // begin regex search (C-r)
RegexpReplace, // begin regex search & replace (C-t)
SearchReplace, // begin search & replace (two-step prompt)
OpenFileStart, // begin open-file prompt OpenFileStart, // begin open-file prompt
// GUI: visual file picker
VisualFilePickerToggle, VisualFilePickerToggle,
// Buffers // Buffers
BufferSwitchStart, // begin buffer switch prompt BufferSwitchStart, // begin buffer switch prompt
@@ -71,6 +73,8 @@ enum class CommandId {
IndentRegion, // indent region (C-k =) IndentRegion, // indent region (C-k =)
UnindentRegion, // unindent region (C-k -) UnindentRegion, // unindent region (C-k -)
ReflowParagraph, // reflow paragraph to column width (ESC q) ReflowParagraph, // reflow paragraph to column width (ESC q)
// Read-only buffers
ToggleReadOnly, // toggle current buffer read-only (C-k ')
// Buffer operations // Buffer operations
ReloadBuffer, // reload buffer from disk (C-k l) ReloadBuffer, // reload buffer from disk (C-k l)
MarkAllAndJumpEnd, // set mark at beginning, jump to end (C-k a) MarkAllAndJumpEnd, // set mark at beginning, jump to end (C-k a)
@@ -78,6 +82,8 @@ enum class CommandId {
JumpToLine, // prompt for line and jump (C-k g) JumpToLine, // prompt for line and jump (C-k g)
ShowWorkingDirectory, // Display the current working directory in the editor message. ShowWorkingDirectory, // Display the current working directory in the editor message.
ChangeWorkingDirectory, // Change the editor's current directory. ChangeWorkingDirectory, // Change the editor's current directory.
// Help
ShowHelp, // open +HELP+ buffer with manual text (C-k h)
// Meta // Meta
UnknownKCommand, // arg: single character that was not recognized after C-k UnknownKCommand, // arg: single character that was not recognized after C-k
}; };

View File

@@ -301,8 +301,22 @@ public:
} }
// --- Generic Prompt subsystem (for search, open-file, save-as, etc.) --- // --- Generic Prompt subsystem (for search, open-file, save-as, etc.) ---
enum class PromptKind { None = 0, Search, OpenFile, SaveAs, Confirm, BufferSwitch, GotoLine, Chdir }; enum class PromptKind {
None = 0,
Search,
RegexSearch,
RegexReplaceFind, // step 1 of Regex Search & Replace: find pattern
RegexReplaceWith, // step 2 of Regex Search & Replace: replacement text
OpenFile,
SaveAs,
Confirm,
BufferSwitch,
GotoLine,
Chdir,
ReplaceFind, // step 1 of Search & Replace: find what
ReplaceWith // step 2 of Search & Replace: replace with
};
void StartPrompt(PromptKind kind, const std::string &label, const std::string &initial) void StartPrompt(PromptKind kind, const std::string &label, const std::string &initial)
@@ -504,9 +518,20 @@ private:
std::string prompt_text_; std::string prompt_text_;
std::string pending_overwrite_path_; std::string pending_overwrite_path_;
// GUI-only state (safe no-op in terminal builds) // GUI-only state (safe no-op in terminal builds)
bool file_picker_visible_ = false; bool file_picker_visible_ = false;
std::string file_picker_dir_; std::string file_picker_dir_;
// Temporary state for Search & Replace flow
public:
void SetReplaceFindTmp(const std::string &s) { replace_find_tmp_ = s; }
void SetReplaceWithTmp(const std::string &s) { replace_with_tmp_ = s; }
[[nodiscard]] const std::string &ReplaceFindTmp() const { return replace_find_tmp_; }
[[nodiscard]] const std::string &ReplaceWithTmp() const { return replace_with_tmp_; }
private:
std::string replace_find_tmp_;
std::string replace_with_tmp_;
}; };
#endif // KTE_EDITOR_H #endif // KTE_EDITOR_H

View File

@@ -7,6 +7,7 @@
#include <string> #include <string>
#include <imgui.h> #include <imgui.h>
#include <regex>
#include "GUIRenderer.h" #include "GUIRenderer.h"
#include "Buffer.h" #include "Buffer.h"
@@ -190,7 +191,7 @@ GUIRenderer::Draw(Editor &ed)
} else { } else {
// Convert pixel X to a render-column target including horizontal col offset // Convert pixel X to a render-column target including horizontal col offset
// Use our own tab expansion of width 8 to match command layer logic. // Use our own tab expansion of width 8 to match command layer logic.
const std::string &line_clicked = lines[by]; std::string line_clicked = static_cast<std::string>(lines[by]);
const std::size_t tabw = 8; const std::size_t tabw = 8;
// We iterate source columns computing absolute rendered column (rx_abs) from 0, // We iterate source columns computing absolute rendered column (rx_abs) from 0,
// then translate to viewport-space by subtracting Coloffs. // then translate to viewport-space by subtracting Coloffs.
@@ -241,31 +242,92 @@ GUIRenderer::Draw(Editor &ed)
} }
// Cache current horizontal offset in rendered columns // Cache current horizontal offset in rendered columns
const std::size_t coloffs_now = buf->Coloffs(); const std::size_t coloffs_now = buf->Coloffs();
for (std::size_t i = rowoffs; i < lines.size(); ++i) { for (std::size_t i = rowoffs; i < lines.size(); ++i) {
// Capture the screen position before drawing the line // Capture the screen position before drawing the line
ImVec2 line_pos = ImGui::GetCursorScreenPos(); ImVec2 line_pos = ImGui::GetCursorScreenPos();
const std::string &line = lines[i]; std::string line = static_cast<std::string>(lines[i]);
// Expand tabs to spaces with width=8 and apply horizontal scroll offset // Expand tabs to spaces with width=8 and apply horizontal scroll offset
const std::size_t tabw = 8; const std::size_t tabw = 8;
std::string expanded; std::string expanded;
expanded.reserve(line.size() + 16); expanded.reserve(line.size() + 16);
std::size_t rx_abs_draw = 0; // rendered column for drawing std::size_t rx_abs_draw = 0; // rendered column for drawing
// Emit entire line (ImGui child scrolling will handle clipping) // Compute search highlight ranges for this line in source indices
for (std::size_t src = 0; src < line.size(); ++src) { bool search_mode = ed.SearchActive() && !ed.SearchQuery().empty();
char c = line[src]; std::vector<std::pair<std::size_t, std::size_t>> hl_src_ranges;
if (c == '\t') { if (search_mode) {
std::size_t adv = (tabw - (rx_abs_draw % tabw)); // If we're in RegexSearch or RegexReplaceFind mode, compute ranges using regex; otherwise plain substring
// Emit spaces for the tab if (ed.PromptActive() && (ed.CurrentPromptKind() == Editor::PromptKind::RegexSearch || ed.CurrentPromptKind() == Editor::PromptKind::RegexReplaceFind)) {
expanded.append(adv, ' '); try {
rx_abs_draw += adv; std::regex rx(ed.SearchQuery());
} else { for (auto it = std::sregex_iterator(line.begin(), line.end(), rx);
expanded.push_back(c); it != std::sregex_iterator(); ++it) {
rx_abs_draw += 1; const auto &m = *it;
} std::size_t sx = static_cast<std::size_t>(m.position());
} std::size_t ex = sx + static_cast<std::size_t>(m.length());
hl_src_ranges.emplace_back(sx, ex);
}
} catch (const std::regex_error &) {
// ignore invalid patterns here; status line already shows the error
}
} else {
const std::string &q = ed.SearchQuery();
std::size_t pos = 0;
while (!q.empty() && (pos = line.find(q, pos)) != std::string::npos) {
hl_src_ranges.emplace_back(pos, pos + q.size());
pos += q.size();
}
}
}
auto src_to_rx = [&](std::size_t upto_src_exclusive) -> std::size_t {
std::size_t rx = 0;
std::size_t s = 0;
while (s < upto_src_exclusive && s < line.size()) {
if (line[s] == '\t')
rx += (tabw - (rx % tabw));
else
rx += 1;
++s;
}
return rx;
};
// Draw background highlights (under text)
if (search_mode && !hl_src_ranges.empty()) {
// Current match emphasis
bool has_current = ed.SearchMatchLen() > 0 && ed.SearchMatchY() == i;
std::size_t cur_x = has_current ? ed.SearchMatchX() : 0;
std::size_t cur_end = has_current ? (ed.SearchMatchX() + ed.SearchMatchLen()) : 0;
for (const auto &rg : hl_src_ranges) {
std::size_t sx = rg.first, ex = rg.second;
std::size_t rx_start = src_to_rx(sx);
std::size_t rx_end = src_to_rx(ex);
// Apply horizontal scroll offset
if (rx_end <= coloffs_now) continue; // fully left of view
std::size_t vx0 = (rx_start > coloffs_now) ? (rx_start - coloffs_now) : 0;
std::size_t vx1 = rx_end - coloffs_now;
ImVec2 p0 = ImVec2(line_pos.x + static_cast<float>(vx0) * space_w, line_pos.y);
ImVec2 p1 = ImVec2(line_pos.x + static_cast<float>(vx1) * space_w, line_pos.y + line_h);
// Choose color: current match stronger
bool is_current = has_current && sx == cur_x && ex == cur_end;
ImU32 col = is_current ? IM_COL32(255, 220, 120, 140) : IM_COL32(200, 200, 0, 90);
ImGui::GetWindowDrawList()->AddRectFilled(p0, p1, col);
}
}
// Emit entire line (ImGui child scrolling will handle clipping)
for (std::size_t src = 0; src < line.size(); ++src) {
char c = line[src];
if (c == '\t') {
std::size_t adv = (tabw - (rx_abs_draw % tabw));
// Emit spaces for the tab
expanded.append(adv, ' ');
rx_abs_draw += adv;
} else {
expanded.push_back(c);
rx_abs_draw += 1;
}
}
ImGui::TextUnformatted(expanded.c_str()); ImGui::TextUnformatted(expanded.c_str());
// Draw a visible cursor indicator on the current line // Draw a visible cursor indicator on the current line
if (i == cy) { if (i == cy) {
@@ -304,8 +366,7 @@ GUIRenderer::Draw(Editor &ed)
ImGui::GetWindowDrawList()->AddRectFilled(p0, p1, bg_col); ImGui::GetWindowDrawList()->AddRectFilled(p0, p1, bg_col);
// If a prompt is active, replace the entire status bar with the prompt text // If a prompt is active, replace the entire status bar with the prompt text
if (ed.PromptActive()) { if (ed.PromptActive()) {
std::string msg = ed.PromptLabel(); std::string label = ed.PromptLabel();
if (!msg.empty()) msg += ": ";
std::string ptext = ed.PromptText(); std::string ptext = ed.PromptText();
auto kind = ed.CurrentPromptKind(); auto kind = ed.CurrentPromptKind();
if (kind == Editor::PromptKind::OpenFile || kind == Editor::PromptKind::SaveAs || if (kind == Editor::PromptKind::OpenFile || kind == Editor::PromptKind::SaveAs ||
@@ -322,14 +383,62 @@ GUIRenderer::Draw(Editor &ed)
} }
} }
} }
msg += ptext;
float pad = 6.f; float pad = 6.f;
ImVec2 msg_sz = ImGui::CalcTextSize(msg.c_str());
float left_x = p0.x + pad; float left_x = p0.x + pad;
float right_x = p1.x - pad;
float max_px = std::max(0.0f, right_x - left_x);
std::string prefix;
if (!label.empty()) prefix = label + ": ";
// Compose showing right-end of filename portion when too long for space
std::string final_msg;
ImVec2 prefix_sz = ImGui::CalcTextSize(prefix.c_str());
float avail_px = std::max(0.0f, max_px - prefix_sz.x);
if ((kind == Editor::PromptKind::OpenFile || kind == Editor::PromptKind::SaveAs || kind == Editor::PromptKind::Chdir) && avail_px > 0.0f) {
// Trim from left until it fits by pixel width
std::string tail = ptext;
ImVec2 tail_sz = ImGui::CalcTextSize(tail.c_str());
if (tail_sz.x > avail_px) {
// Remove leading chars until it fits
// Use a simple loop; text lengths are small here
size_t start = 0;
// To avoid O(n^2) worst-case, remove chunks
while (start < tail.size()) {
// Estimate how many chars to skip based on ratio
float ratio = tail_sz.x / avail_px;
size_t skip = ratio > 1.5f ? std::min(tail.size() - start, (size_t)std::max<size_t>(1, (size_t)(tail.size() / 4))) : 1;
start += skip;
std::string candidate = tail.substr(start);
ImVec2 cand_sz = ImGui::CalcTextSize(candidate.c_str());
if (cand_sz.x <= avail_px) {
tail = candidate;
tail_sz = cand_sz;
break;
}
}
if (ImGui::CalcTextSize(tail.c_str()).x > avail_px && !tail.empty()) {
// As a last resort, ensure fit by chopping exactly
// binary reduce
size_t lo = 0, hi = tail.size();
while (lo < hi) {
size_t mid = (lo + hi) / 2;
std::string cand = tail.substr(mid);
if (ImGui::CalcTextSize(cand.c_str()).x <= avail_px) hi = mid; else lo = mid + 1;
}
tail = tail.substr(lo);
}
}
final_msg = prefix + tail;
} else {
final_msg = prefix + ptext;
}
ImVec2 msg_sz = ImGui::CalcTextSize(final_msg.c_str());
ImGui::PushClipRect(ImVec2(p0.x, p0.y), ImVec2(p1.x, p1.y), true); ImGui::PushClipRect(ImVec2(p0.x, p0.y), ImVec2(p1.x, p1.y), true);
ImGui::SetCursorScreenPos(ImVec2(left_x, p0.y + (bar_h - msg_sz.y) * 0.5f)); ImGui::SetCursorScreenPos(ImVec2(left_x, p0.y + (bar_h - msg_sz.y) * 0.5f));
ImGui::TextUnformatted(msg.c_str()); ImGui::TextUnformatted(final_msg.c_str());
ImGui::PopClipRect(); ImGui::PopClipRect();
// Advance cursor to after the bar to keep layout consistent // Advance cursor to after the bar to keep layout consistent
ImGui::Dummy(ImVec2(x1 - x0, bar_h)); ImGui::Dummy(ImVec2(x1 - x0, bar_h));

55
HelpText.cc Normal file
View File

@@ -0,0 +1,55 @@
/*
* HelpText.cc - embedded/customizable help content
*/
#include "HelpText.h"
std::string
HelpText::Text()
{
// Customize the help text here. This string will be used by C-k h first.
// You can keep it empty to fall back to the manpage or built-in defaults.
// Note: keep newline characters as-is; the renderer splits lines on '\n'.
return std::string(
"KTE - Kyle's Text Editor\n\n"
"About:\n"
" kte is Kyle's Text Editor and is probably ill-suited to everyone else. It was\n"
" inspired by Antirez' kilo text editor by way of someone's writeup of the\n"
" process of writing a text editor from scratch. It has keybindings inspired by\n"
" VDE (and the Wordstar family) and emacs; its spiritual parent is mg(1).\n"
"\n"
"Core keybindings:\n"
" C-k ' Toggle read-only\n"
" C-k - Unindent region\n"
" C-k = Indent region\n"
" C-k C-d Kill entire line\n"
" C-k C-q Quit now (no confirm)\n"
" C-k a Mark all and jump to end\n"
" C-k b Switch buffer\n"
" C-k c Close current buffer\n"
" C-k d Kill to end of line\n"
" C-k e Open file (prompt)\n"
" C-k g Jump to line\n"
" C-k h Show this help\n"
" C-k l Reload buffer from disk\n"
" C-k n Previous buffer\n"
" C-k o Change working directory (prompt)\n"
" C-k p Next buffer\n"
" C-k q Quit (confirm if dirty)\n"
" C-k r Redo\n"
" C-k s Save buffer\n"
" C-k u Undo\n"
" C-k v Toggle visual file picker (GUI)\n"
" C-k w Show working directory\n"
" C-k x Save and quit\n"
"\n"
"ESC/Alt commands:\n"
" ESC q Reflow paragraph\n"
" ESC BACKSPACE Delete previous word\n"
" ESC d Delete next word\n"
" Alt-w Copy region to kill ring\n\n"
"Buffers:\n +HELP+ is read-only. Press C-k ' to toggle if you need to edit; C-k h restores it.\n"
);
}

17
HelpText.h Normal file
View File

@@ -0,0 +1,17 @@
/*
* HelpText.h - embedded/customizable help content
*/
#ifndef KTE_HELPTEXT_H
#define KTE_HELPTEXT_H
#include <string>
class HelpText {
public:
// Returns the embedded help text as a single string with newlines.
// Project maintainers can customize the returned string below
// (in HelpText.cc) without touching the help command logic.
static std::string Text();
};
#endif // KTE_HELPTEXT_H

View File

@@ -33,6 +33,10 @@ KLookupKCommand(const int ascii_key, const bool ctrl, CommandId &out) -> bool
out = CommandId::Redo; // C-k r (redo) out = CommandId::Redo; // C-k r (redo)
return true; return true;
} }
if (ascii_key == '\'') {
out = CommandId::ToggleReadOnly; // C-k ' (toggle read-only)
return true;
}
switch (k_lower) { switch (k_lower) {
case 'a': case 'a':
@@ -59,6 +63,9 @@ KLookupKCommand(const int ascii_key, const bool ctrl, CommandId &out) -> bool
case 'g': case 'g':
out = CommandId::JumpToLine; out = CommandId::JumpToLine;
return true; return true;
case 'h':
out = CommandId::ShowHelp;
return true;
case 'j': case 'j':
out = CommandId::JumpToMark; out = CommandId::JumpToMark;
return true; return true;
@@ -114,7 +121,7 @@ auto
KLookupCtrlCommand(const int ascii_key, CommandId &out) -> bool KLookupCtrlCommand(const int ascii_key, CommandId &out) -> bool
{ {
const int k = KLowerAscii(ascii_key); const int k = KLowerAscii(ascii_key);
switch (k) { switch (k) {
case 'w': case 'w':
out = CommandId::KillRegion; // C-w out = CommandId::KillRegion; // C-w
return true; return true;
@@ -145,6 +152,15 @@ KLookupCtrlCommand(const int ascii_key, CommandId &out) -> bool
case 's': case 's':
out = CommandId::FindStart; out = CommandId::FindStart;
return true; return true;
case 'r':
out = CommandId::RegexFindStart; // C-r regex search
return true;
case 't':
out = CommandId::RegexpReplace; // C-t regex search & replace
return true;
case 'h':
out = CommandId::SearchReplace; // C-h: search & replace
return true;
case 'l': case 'l':
out = CommandId::Refresh; out = CommandId::Refresh;
return true; return true;

View File

@@ -1,10 +1,10 @@
ROADMAP / TODO: ROADMAP / TODO:
- [ ] Search + Replace - [x] Search + Replace
- [ ] Regex search + replace - [x] Regex search + replace
- [ ] The undo system should actually work - [ ] The undo system should actually work
- [ ] Able to mark buffers as read-only - [x] Able to mark buffers as read-only
- [ ] Built-in help text - [x] Built-in help text
- [ ] Shorten paths in the homedir with ~ - [x] Shorten paths in the homedir with ~
- [ ] When the filename is longer than the message window, scoot left to - [x] When the filename is longer than the message window, scoot left to
to keep it in view keep it in view

View File

@@ -3,6 +3,7 @@
#include <filesystem> #include <filesystem>
#include <cstdlib> #include <cstdlib>
#include <ncurses.h> #include <ncurses.h>
#include <regex>
#include <string> #include <string>
#include "TerminalRenderer.h" #include "TerminalRenderer.h"
@@ -40,100 +41,140 @@ TerminalRenderer::Draw(Editor &ed)
std::size_t coloffs = buf->Coloffs(); std::size_t coloffs = buf->Coloffs();
const int tabw = 8; const int tabw = 8;
for (int r = 0; r < content_rows; ++r) { for (int r = 0; r < content_rows; ++r) {
move(r, 0); move(r, 0);
std::size_t li = rowoffs + static_cast<std::size_t>(r); std::size_t li = rowoffs + static_cast<std::size_t>(r);
std::size_t render_col = 0; std::size_t render_col = 0;
std::size_t src_i = 0; std::size_t src_i = 0;
bool do_hl = ed.SearchActive() && li == ed.SearchMatchY() && ed.SearchMatchLen() > 0; // Compute matches for this line if search highlighting is active
std::size_t mx = do_hl ? ed.SearchMatchX() : 0; bool search_mode = ed.SearchActive() && !ed.SearchQuery().empty();
std::size_t mlen = do_hl ? ed.SearchMatchLen() : 0; std::vector<std::pair<std::size_t, std::size_t>> ranges; // [start, end)
bool hl_on = false; if (search_mode && li < lines.size()) {
int written = 0; std::string sline = static_cast<std::string>(lines[li]);
if (li < lines.size()) { // If regex search prompt is active (RegexSearch or RegexReplaceFind), use regex to compute highlight ranges
const std::string &line = lines[li]; if (ed.PromptActive() && (ed.CurrentPromptKind() == Editor::PromptKind::RegexSearch || ed.CurrentPromptKind() == Editor::PromptKind::RegexReplaceFind)) {
src_i = 0; try {
render_col = 0; std::regex rx(ed.SearchQuery());
while (written < cols) { for (auto it = std::sregex_iterator(sline.begin(), sline.end(), rx);
char ch = ' '; it != std::sregex_iterator(); ++it) {
bool from_src = false; const auto &m = *it;
if (src_i < line.size()) { std::size_t sx = static_cast<std::size_t>(m.position());
unsigned char c = static_cast<unsigned char>(line[src_i]); std::size_t ex = sx + static_cast<std::size_t>(m.length());
if (c == '\t') { ranges.emplace_back(sx, ex);
std::size_t next_tab = tabw - (render_col % tabw); }
if (render_col + next_tab <= coloffs) { } catch (const std::regex_error &) {
render_col += next_tab; // ignore invalid patterns here; status shows error
++src_i; }
continue; } else {
} const std::string &q = ed.SearchQuery();
// Emit spaces for tab std::size_t pos = 0;
if (render_col < coloffs) { while (!q.empty() && (pos = sline.find(q, pos)) != std::string::npos) {
// skip to coloffs ranges.emplace_back(pos, pos + q.size());
std::size_t to_skip = std::min<std::size_t>( pos += q.size();
next_tab, coloffs - render_col); }
render_col += to_skip; }
next_tab -= to_skip; }
} auto is_src_in_hl = [&](std::size_t si) -> bool {
// Now render visible spaces if (ranges.empty()) return false;
while (next_tab > 0 && written < cols) { // ranges are non-overlapping and ordered by construction
bool in_hl = do_hl && src_i >= mx && src_i < mx + mlen; // linear scan is fine for now
// highlight by source index for (const auto &rg : ranges) {
if (in_hl && !hl_on) { if (si < rg.first) break;
attron(A_STANDOUT); if (si >= rg.first && si < rg.second) return true;
hl_on = true; }
} return false;
if (!in_hl && hl_on) { };
attroff(A_STANDOUT); // Track current-match to optionally emphasize
hl_on = false; const bool has_current = ed.SearchActive() && ed.SearchMatchLen() > 0;
} const std::size_t cur_mx = has_current ? ed.SearchMatchX() : 0;
addch(' '); const std::size_t cur_my = has_current ? ed.SearchMatchY() : 0;
++written; const std::size_t cur_mend = has_current ? (ed.SearchMatchX() + ed.SearchMatchLen()) : 0;
++render_col; bool hl_on = false;
--next_tab; bool cur_on = false;
} int written = 0;
++src_i; if (li < lines.size()) {
continue; std::string line = static_cast<std::string>(lines[li]);
} else { src_i = 0;
// normal char render_col = 0;
if (render_col < coloffs) { while (written < cols) {
++render_col; char ch = ' ';
++src_i; bool from_src = false;
continue; if (src_i < line.size()) {
} unsigned char c = static_cast<unsigned char>(line[src_i]);
ch = static_cast<char>(c); if (c == '\t') {
from_src = true; std::size_t next_tab = tabw - (render_col % tabw);
} if (render_col + next_tab <= coloffs) {
} else { render_col += next_tab;
// beyond EOL, fill spaces ++src_i;
ch = ' '; continue;
from_src = false; }
} // Emit spaces for tab
if (do_hl) { if (render_col < coloffs) {
bool in_hl = from_src && src_i >= mx && src_i < mx + mlen; // skip to coloffs
if (in_hl && !hl_on) { std::size_t to_skip = std::min<std::size_t>(
attron(A_STANDOUT); next_tab, coloffs - render_col);
hl_on = true; render_col += to_skip;
} next_tab -= to_skip;
if (!in_hl && hl_on) { }
attroff(A_STANDOUT); // Now render visible spaces
hl_on = false; while (next_tab > 0 && written < cols) {
} bool in_hl = search_mode && is_src_in_hl(src_i);
} bool in_cur = has_current && li == cur_my && src_i >= cur_mx && src_i < cur_mend;
addch(static_cast<unsigned char>(ch)); // Toggle highlight attributes
++written; int attr = 0;
++render_col; if (in_hl) attr |= A_STANDOUT;
if (from_src) if (in_cur) attr |= A_BOLD;
++src_i; if ((attr & A_STANDOUT) && !hl_on) { attron(A_STANDOUT); hl_on = true; }
if (src_i >= line.size() && written >= cols) if (!(attr & A_STANDOUT) && hl_on) { attroff(A_STANDOUT); hl_on = false; }
break; if ((attr & A_BOLD) && !cur_on) { attron(A_BOLD); cur_on = true; }
} if (!(attr & A_BOLD) && cur_on) { attroff(A_BOLD); cur_on = false; }
} addch(' ');
if (hl_on) { ++written;
attroff(A_STANDOUT); ++render_col;
hl_on = false; --next_tab;
} }
clrtoeol(); ++src_i;
} continue;
} else {
// normal char
if (render_col < coloffs) {
++render_col;
++src_i;
continue;
}
ch = static_cast<char>(c);
from_src = true;
}
} else {
// beyond EOL, fill spaces
ch = ' ';
from_src = false;
}
bool in_hl = search_mode && from_src && is_src_in_hl(src_i);
bool in_cur = has_current && li == cur_my && from_src && src_i >= cur_mx && src_i < cur_mend;
if (in_hl && !hl_on) { attron(A_STANDOUT); hl_on = true; }
if (!in_hl && hl_on) { attroff(A_STANDOUT); hl_on = false; }
if (in_cur && !cur_on) { attron(A_BOLD); cur_on = true; }
if (!in_cur && cur_on) { attroff(A_BOLD); cur_on = false; }
addch(static_cast<unsigned char>(ch));
++written;
++render_col;
if (from_src)
++src_i;
if (src_i >= line.size() && written >= cols)
break;
}
}
if (hl_on) {
attroff(A_STANDOUT);
hl_on = false;
}
if (cur_on) {
attroff(A_BOLD);
cur_on = false;
}
clrtoeol();
}
// Place terminal cursor at logical position accounting for tabs and coloffs // Place terminal cursor at logical position accounting for tabs and coloffs
std::size_t cy = buf->Cury(); std::size_t cy = buf->Cury();
@@ -161,9 +202,7 @@ TerminalRenderer::Draw(Editor &ed)
// If a prompt is active, replace the status bar with the full prompt text // If a prompt is active, replace the status bar with the full prompt text
if (ed.PromptActive()) { if (ed.PromptActive()) {
// Build prompt text: "Label: text" and shorten HOME path for file-related prompts // Build prompt text: "Label: text" and shorten HOME path for file-related prompts
std::string msg = ed.PromptLabel(); std::string label = ed.PromptLabel();
if (!msg.empty())
msg += ": ";
std::string ptext = ed.PromptText(); std::string ptext = ed.PromptText();
auto kind = ed.CurrentPromptKind(); auto kind = ed.CurrentPromptKind();
if (kind == Editor::PromptKind::OpenFile || kind == Editor::PromptKind::SaveAs || if (kind == Editor::PromptKind::OpenFile || kind == Editor::PromptKind::SaveAs ||
@@ -181,7 +220,30 @@ TerminalRenderer::Draw(Editor &ed)
} }
} }
} }
msg += ptext; // Prefer keeping the tail of the filename visible when it exceeds the window
std::string msg;
if (!label.empty()) {
msg = label + ": ";
}
// When dealing with file-related prompts, left-trim the filename text so the tail stays visible
if ((kind == Editor::PromptKind::OpenFile || kind == Editor::PromptKind::SaveAs || kind == Editor::PromptKind::Chdir) && cols > 0) {
int avail = cols - static_cast<int>(msg.size());
if (avail <= 0) {
// No room for label; fall back to showing the rightmost portion of the whole string
std::string whole = msg + ptext;
if ((int)whole.size() > cols)
whole = whole.substr(whole.size() - cols);
msg = whole;
} else {
if ((int)ptext.size() > avail) {
ptext = ptext.substr(ptext.size() - avail);
}
msg += ptext;
}
} else {
// Non-file prompts: simple concatenation and clip by terminal
msg += ptext;
}
// Draw left-aligned, clipped to width // Draw left-aligned, clipped to width
if (!msg.empty()) if (!msg.empty())
@@ -233,6 +295,9 @@ TerminalRenderer::Draw(Editor &ed)
left += fname; left += fname;
if (b && b->Dirty()) if (b && b->Dirty())
left += " *"; left += " *";
// Append read-only indicator
if (b && b->IsReadOnly())
left += " [RO]";
// Append total line count as "<n>L" // Append total line count as "<n>L"
if (b) { if (b) {
unsigned long lcount = static_cast<unsigned long>(b->Rows().size()); unsigned long lcount = static_cast<unsigned long>(b->Rows().size());

View File

@@ -1,53 +0,0 @@
{
lib,
stdenv,
cmake,
ncurses,
SDL2,
libGL,
xorg,
installShellFiles,
...
}:
let
cmakeContent = builtins.readFile ./CMakeLists.txt;
cmakeLines = lib.splitString "\n" cmakeContent;
versionLine = lib.findFirst (l: builtins.match ".*set\\(KTE_VERSION \".+\"\\).*" l != null) (throw "KTE_VERSION not found in CMakeLists.txt") cmakeLines;
version = builtins.head (builtins.match ".*set\\(KTE_VERSION \"(.+)\"\\).*" versionLine);
in
stdenv.mkDerivation {
pname = "kte";
inherit version;
src = lib.cleanSource ./.;
nativeBuildInputs = [
cmake
ncurses
SDL2
libGL
xorg.libX11
installShellFiles
];
cmakeFlags = [
"-DBUILD_GUI=ON"
"-DCMAKE_BUILD_TYPE=Debug"
];
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp kte $out/bin/
cp kge $out/bin/
installManPage ../docs/kte.1
installManPage ../docs/kge.1
mkdir -p $out/share/icons
cp ../kge.png $out/share/icons/
runHook postInstall
'';
}

View File

@@ -1,42 +0,0 @@
{
lib,
stdenv,
cmake,
ncurses,
installShellFiles,
...
}:
let
cmakeContent = builtins.readFile ./CMakeLists.txt;
cmakeLines = lib.splitString "\n" cmakeContent;
versionLine = lib.findFirst (l: builtins.match ".*set\\(KTE_VERSION \".+\"\\).*" l != null) (throw "KTE_VERSION not found in CMakeLists.txt") cmakeLines;
version = builtins.head (builtins.match ".*set\\(KTE_VERSION \"(.+)\"\\).*" versionLine);
in
stdenv.mkDerivation {
pname = "kte";
inherit version;
src = lib.cleanSource ./.;
nativeBuildInputs = [
cmake
ncurses
installShellFiles
];
cmakeFlags = [
"-DBUILD_GUI=OFF"
"-DCMAKE_BUILD_TYPE=Debug"
];
installPhase = ''
runHook preInstall
mkdir -p $out/bin
cp kte $out/bin/
installManPage ../docs/kte.1
runHook postInstall
'';
}

View File

@@ -7,12 +7,16 @@
libGL, libGL,
xorg, xorg,
installShellFiles, installShellFiles,
graphical ? false,
... ...
}: }:
let let
cmakeContent = builtins.readFile ./CMakeLists.txt; cmakeContent = builtins.readFile ./CMakeLists.txt;
cmakeLines = lib.splitString "\n" cmakeContent; cmakeLines = lib.splitString "\n" cmakeContent;
versionLine = lib.findFirst (l: builtins.match ".*set\\(KTE_VERSION \".+\"\\).*" l != null) (throw "KTE_VERSION not found in CMakeLists.txt") cmakeLines; versionLine = lib.findFirst (
l: builtins.match ".*set\\(KTE_VERSION \".+\"\\).*" l != null
) (throw "KTE_VERSION not found in CMakeLists.txt") cmakeLines;
version = builtins.head (builtins.match ".*set\\(KTE_VERSION \"(.+)\"\\).*" versionLine); version = builtins.head (builtins.match ".*set\\(KTE_VERSION \"(.+)\"\\).*" versionLine);
in in
stdenv.mkDerivation { stdenv.mkDerivation {
@@ -24,14 +28,16 @@ stdenv.mkDerivation {
nativeBuildInputs = [ nativeBuildInputs = [
cmake cmake
ncurses ncurses
installShellFiles
]
++ lib.optionals graphical [
SDL2 SDL2
libGL libGL
xorg.libX11 xorg.libX11
installShellFiles
]; ];
cmakeFlags = [ cmakeFlags = [
"-DBUILD_GUI=ON" "-DBUILD_GUI=${if graphical then "ON" else "OFF"}"
"-DCMAKE_BUILD_TYPE=Debug" "-DCMAKE_BUILD_TYPE=Debug"
]; ];
@@ -40,14 +46,17 @@ stdenv.mkDerivation {
mkdir -p $out/bin mkdir -p $out/bin
cp kte $out/bin/ cp kte $out/bin/
cp kge $out/bin/
installManPage ../docs/kte.1 installManPage ../docs/kte.1
installManPage ../docs/kge.1
''
+ lib.optionalString graphical ''
cp kge $out/bin/
installManPage ../docs/kge.1
mkdir -p $out/share/icons mkdir -p $out/share/icons
cp ../kge.png $out/share/icons/ cp ../kge.png $out/share/icons/
''
+ ''
runHook postInstall runHook postInstall
''; '';
} }

View File

@@ -1,55 +0,0 @@
# flake.nix
{
description = "kte ImGui/SDL2 text editor";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = { self, nixpkgs, flake-utils }:
flake-utils.lib.eachDefaultSystem (system:
let
pkgs = nixpkgs.legacyPackages.${system};
in {
packages.default = pkgs.stdenv.mkDerivation {
pname = "kte";
version = "0.1.0";
src = ./.;
nativeBuildInputs = [ pkgs.cmake pkgs.pkg-config ];
buildInputs = with pkgs; [
ncurses
SDL2
libGL
xorg.libX11
];
cmakeFlags = [
"-DBUILD_GUI=ON"
"-DCURSES_NEED_NCURSES=TRUE"
"-DCURSES_NEED_WIDE=TRUE"
];
# Alternative (even stronger): completely hide the broken module
preConfigure = ''
# If the project ships its own FindSDL2.cmake in cmake/, hide it
if [ -f cmake/FindSDL2.cmake ]; then
mv cmake/FindSDL2.cmake cmake/FindSDL2.cmake.disabled
echo "Disabled bundled FindSDL2.cmake"
fi
'';
meta = with pkgs.lib; {
description = "kte ImGui/SDL2 GUI editor";
mainProgram = "kte";
platforms = platforms.linux;
};
};
devShells.default = pkgs.mkShell {
inputsFrom = [ self.packages.${system}.default ];
packages = with pkgs; [ gdb clang-tools ];
};
});
}

View File

@@ -3,16 +3,18 @@
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable"; inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
outputs = inputs @ { self, nixpkgs, ... }: outputs =
inputs@{ self, nixpkgs, ... }:
let let
eachSystem = nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed; eachSystem = nixpkgs.lib.genAttrs nixpkgs.lib.systems.flakeExposed;
pkgsFor = system: import nixpkgs { inherit system; }; pkgsFor = system: import nixpkgs { inherit system; };
in { in
packages = eachSystem (system: { {
default = (pkgsFor system).callPackage ./default-nogui.nix { }; packages = eachSystem (system: rec {
kge = (pkgsFor system).callPackage ./default-gui.nix { }; default = kte;
kte = (pkgsFor system).callPackage ./default-nogui.nix { }; full = kge;
full = (pkgsFor system).callPackage ./default.nix { }; kte = (pkgsFor system).callPackage ./default.nix { graphical = false; };
kge = (pkgsFor system).callPackage ./default.nix { graphical = true; };
}); });
}; };
} }