diff --git a/AppendBuffer.h b/AppendBuffer.h index 3572d8d..231a79e 100644 --- a/AppendBuffer.h +++ b/AppendBuffer.h @@ -1,8 +1,7 @@ /* * AppendBuffer.h - selector header to choose GapBuffer or PieceTable */ -#ifndef KTE_APPENDBUFFER_H -#define KTE_APPENDBUFFER_H +#pragma once #ifdef KTE_USE_PIECE_TABLE #include "PieceTable.h" @@ -10,6 +9,4 @@ using AppendBuffer = PieceTable; #else #include "GapBuffer.h" using AppendBuffer = GapBuffer; -#endif - -#endif // KTE_APPENDBUFFER_H +#endif \ No newline at end of file diff --git a/Buffer.h b/Buffer.h index faf941f..4e65236 100644 --- a/Buffer.h +++ b/Buffer.h @@ -1,8 +1,7 @@ /* * Buffer.h - editor buffer representing an open document */ -#ifndef KTE_BUFFER_H -#define KTE_BUFFER_H +#pragma once #include #include @@ -466,6 +465,4 @@ private: bool syntax_enabled_ = true; std::string filetype_; std::unique_ptr highlighter_; -}; - -#endif // KTE_BUFFER_H +}; \ No newline at end of file diff --git a/Command.h b/Command.h index 0d8f912..93d55d9 100644 --- a/Command.h +++ b/Command.h @@ -1,8 +1,7 @@ /* * Command.h - command model and registry for editor actions */ -#ifndef KTE_COMMAND_H -#define KTE_COMMAND_H +#pragma once #include #include @@ -155,6 +154,4 @@ void InstallDefaultCommands(); // Returns true if the command executed successfully. bool Execute(Editor &ed, CommandId id, const std::string &arg = std::string(), int count = 0); -bool Execute(Editor &ed, const std::string &name, const std::string &arg = std::string(), int count = 0); - -#endif // KTE_COMMAND_H \ No newline at end of file +bool Execute(Editor &ed, const std::string &name, const std::string &arg = std::string(), int count = 0); \ No newline at end of file diff --git a/Editor.h b/Editor.h index 81d276f..d788c98 100644 --- a/Editor.h +++ b/Editor.h @@ -1,9 +1,7 @@ /* * Editor.h - top-level editor state and buffer management */ -#ifndef KTE_EDITOR_H -#define KTE_EDITOR_H - +#pragma once #include #include #include @@ -561,6 +559,4 @@ public: private: std::string replace_find_tmp_; std::string replace_with_tmp_; -}; - -#endif // KTE_EDITOR_H \ No newline at end of file +}; \ No newline at end of file diff --git a/Frontend.h b/Frontend.h index 1ca1923..026f90c 100644 --- a/Frontend.h +++ b/Frontend.h @@ -1,9 +1,7 @@ /* * Frontend.h - top-level container that couples Input + Renderer and runs the loop */ -#ifndef KTE_FRONTEND_H -#define KTE_FRONTEND_H - +#pragma once class Editor; class InputHandler; @@ -21,6 +19,4 @@ public: // Shutdown/cleanup virtual void Shutdown() = 0; -}; - -#endif // KTE_FRONTEND_H +}; \ No newline at end of file diff --git a/GUIConfig.h b/GUIConfig.h index 49fb6fe..6590618 100644 --- a/GUIConfig.h +++ b/GUIConfig.h @@ -1,8 +1,7 @@ /* * GUIConfig - loads simple GUI configuration from $HOME/.config/kte/kge.ini */ -#ifndef KTE_GUI_CONFIG_H -#define KTE_GUI_CONFIG_H +#pragma once #include @@ -31,6 +30,4 @@ public: // Load from explicit path. Returns true if file existed and was parsed. bool LoadFromFile(const std::string &path); -}; - -#endif // KTE_GUI_CONFIG_H \ No newline at end of file +}; \ No newline at end of file diff --git a/GUIFrontend.h b/GUIFrontend.h index c6c0ba6..10644a3 100644 --- a/GUIFrontend.h +++ b/GUIFrontend.h @@ -1,9 +1,7 @@ /* * GUIFrontend - couples GUIInputHandler + GUIRenderer and owns SDL2/ImGui lifecycle */ -#ifndef KTE_GUI_FRONTEND_H -#define KTE_GUI_FRONTEND_H - +#pragma once #include "Frontend.h" #include "GUIConfig.h" #include "GUIInputHandler.h" @@ -35,6 +33,4 @@ private: SDL_GLContext gl_ctx_ = nullptr; int width_ = 1280; int height_ = 800; -}; - -#endif // KTE_GUI_FRONTEND_H \ No newline at end of file +}; \ No newline at end of file diff --git a/GUIInputHandler.h b/GUIInputHandler.h index 4bbece5..da8d5fb 100644 --- a/GUIInputHandler.h +++ b/GUIInputHandler.h @@ -1,9 +1,7 @@ /* * GUIInputHandler - ImGui/SDL2-based input mapping for GUI mode */ -#ifndef KTE_GUI_INPUT_HANDLER_H -#define KTE_GUI_INPUT_HANDLER_H - +#pragma once #include #include @@ -41,6 +39,4 @@ private: bool uarg_had_digits_ = false; // whether any digits were supplied int uarg_value_ = 0; // current absolute value (>=0) std::string uarg_text_; // raw digits/minus typed for status display -}; - -#endif // KTE_GUI_INPUT_HANDLER_H +}; \ No newline at end of file diff --git a/GUIRenderer.h b/GUIRenderer.h index 7a5a75c..b83a773 100644 --- a/GUIRenderer.h +++ b/GUIRenderer.h @@ -1,9 +1,7 @@ /* * GUIRenderer - ImGui-based renderer for GUI mode */ -#ifndef KTE_GUI_RENDERER_H -#define KTE_GUI_RENDERER_H - +#pragma once #include "Renderer.h" class GUIRenderer final : public Renderer { @@ -13,6 +11,4 @@ public: ~GUIRenderer() override = default; void Draw(Editor &ed) override; -}; - -#endif // KTE_GUI_RENDERER_H +}; \ No newline at end of file diff --git a/GapBuffer.h b/GapBuffer.h index 6cf5ab8..c8f7630 100644 --- a/GapBuffer.h +++ b/GapBuffer.h @@ -1,9 +1,7 @@ /* * GapBuffer.h - C++ replacement for abuf append/prepend buffer utilities */ -#ifndef KTE_GAPBUFFER_H -#define KTE_GAPBUFFER_H - +#pragma once #include @@ -75,6 +73,4 @@ private: char *buffer_ = nullptr; std::size_t size_ = 0; // number of valid bytes (excluding terminator) std::size_t capacity_ = 0; // capacity of buffer_ excluding space for terminator -}; - -#endif // KTE_GAPBUFFER_H +}; \ No newline at end of file diff --git a/HelpText.h b/HelpText.h index 1e7f922..31502ff 100644 --- a/HelpText.h +++ b/HelpText.h @@ -1,9 +1,7 @@ /* * HelpText.h - embedded/customizable help content */ -#ifndef KTE_HELPTEXT_H -#define KTE_HELPTEXT_H - +#pragma once #include class HelpText { @@ -12,6 +10,4 @@ public: // 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 +}; \ No newline at end of file diff --git a/InputHandler.h b/InputHandler.h index fe4387b..6bfb50e 100644 --- a/InputHandler.h +++ b/InputHandler.h @@ -1,9 +1,7 @@ /* * InputHandler.h - input abstraction and mapping to commands */ -#ifndef KTE_INPUT_HANDLER_H -#define KTE_INPUT_HANDLER_H - +#pragma once #include #include "Command.h" @@ -24,6 +22,4 @@ public: // Poll for input and translate it to a command. Non-blocking. // Returns true if a command is available in 'out'. Returns false if no input. virtual bool Poll(MappedInput &out) = 0; -}; - -#endif // KTE_INPUT_HANDLER_H +}; \ No newline at end of file diff --git a/KKeymap.h b/KKeymap.h index 62b86ef..e5645db 100644 --- a/KKeymap.h +++ b/KKeymap.h @@ -1,9 +1,7 @@ /* * KKeymap.h - mapping for k-command (C-k prefix) keys to CommandId */ -#ifndef KTE_KKEYMAP_H -#define KTE_KKEYMAP_H - +#pragma once #include "Command.h" @@ -29,6 +27,4 @@ KLowerAscii(const int key) if (key >= 'A' && key <= 'Z') return key + ('a' - 'A'); return key; -} - -#endif // KTE_KKEYMAP_H +} \ No newline at end of file diff --git a/OptimizedSearch.h b/OptimizedSearch.h index 3893491..c4f0976 100644 --- a/OptimizedSearch.h +++ b/OptimizedSearch.h @@ -1,7 +1,5 @@ // OptimizedSearch.h - Boyer–Moore (bad character) based substring search -#ifndef KTE_OPTIMIZED_SEARCH_H -#define KTE_OPTIMIZED_SEARCH_H - +#pragma once #include #include #include @@ -22,6 +20,4 @@ private: std::string last_pat_; void build_bad_char(const std::string &pattern); -}; - -#endif // KTE_OPTIMIZED_SEARCH_H \ No newline at end of file +}; \ No newline at end of file diff --git a/PieceTable.h b/PieceTable.h index b110485..07022d4 100644 --- a/PieceTable.h +++ b/PieceTable.h @@ -1,9 +1,7 @@ /* * PieceTable.h - Alternative to GapBuffer using a piece table representation */ -#ifndef KTE_PIECETABLE_H -#define KTE_PIECETABLE_H - +#pragma once #include #include #include @@ -93,6 +91,4 @@ private: mutable std::string materialized_; mutable bool dirty_ = true; std::size_t total_size_ = 0; -}; - -#endif // KTE_PIECETABLE_H +}; \ No newline at end of file diff --git a/Renderer.h b/Renderer.h index f5371ed..cf20156 100644 --- a/Renderer.h +++ b/Renderer.h @@ -1,9 +1,7 @@ /* * Renderer.h - rendering abstraction */ -#ifndef KTE_RENDERER_H -#define KTE_RENDERER_H - +#pragma once class Editor; @@ -12,6 +10,4 @@ public: virtual ~Renderer() = default; virtual void Draw(Editor &ed) = 0; -}; - -#endif // KTE_RENDERER_H +}; \ No newline at end of file diff --git a/TerminalFrontend.h b/TerminalFrontend.h index 81538ea..4f8b837 100644 --- a/TerminalFrontend.h +++ b/TerminalFrontend.h @@ -1,9 +1,7 @@ /* * TerminalFrontend - couples TerminalInputHandler + TerminalRenderer and owns ncurses lifecycle */ -#ifndef KTE_TERMINAL_FRONTEND_H -#define KTE_TERMINAL_FRONTEND_H - +#pragma once #include #include "Frontend.h" @@ -31,6 +29,4 @@ private: // Saved terminal attributes to restore on shutdown bool have_orig_tio_ = false; struct termios orig_tio_{}; -}; - -#endif // KTE_TERMINAL_FRONTEND_H +}; \ No newline at end of file diff --git a/TerminalInputHandler.h b/TerminalInputHandler.h index 22e1113..bbc5096 100644 --- a/TerminalInputHandler.h +++ b/TerminalInputHandler.h @@ -1,9 +1,7 @@ /* * TerminalInputHandler - ncurses-based input handling for terminal mode */ -#ifndef KTE_TERMINAL_INPUT_HANDLER_H -#define KTE_TERMINAL_INPUT_HANDLER_H - +#pragma once #include "InputHandler.h" @@ -30,6 +28,4 @@ private: bool uarg_had_digits_ = false; // whether any digits were supplied int uarg_value_ = 0; // current absolute value (>=0) std::string uarg_text_; // raw digits/minus typed for status display -}; - -#endif // KTE_TERMINAL_INPUT_HANDLER_H +}; \ No newline at end of file diff --git a/TerminalRenderer.h b/TerminalRenderer.h index bb34d60..85b1403 100644 --- a/TerminalRenderer.h +++ b/TerminalRenderer.h @@ -1,9 +1,7 @@ /* * TerminalRenderer - ncurses-based renderer for terminal mode */ -#ifndef KTE_TERMINAL_RENDERER_H -#define KTE_TERMINAL_RENDERER_H - +#pragma once #include "Renderer.h" @@ -14,6 +12,4 @@ public: ~TerminalRenderer() override; void Draw(Editor &ed) override; -}; - -#endif // KTE_TERMINAL_RENDERER_H +}; \ No newline at end of file diff --git a/TestFrontend.h b/TestFrontend.h index e87e610..7adc0e4 100644 --- a/TestFrontend.h +++ b/TestFrontend.h @@ -1,9 +1,7 @@ /* * TestFrontend.h - headless frontend for testing with programmable input */ -#ifndef KTE_TEST_FRONTEND_H -#define KTE_TEST_FRONTEND_H - +#pragma once #include "Frontend.h" #include "TestInputHandler.h" #include "TestRenderer.h" @@ -36,6 +34,4 @@ public: private: TestInputHandler input_{}; TestRenderer renderer_{}; -}; - -#endif // KTE_TEST_FRONTEND_H +}; \ No newline at end of file diff --git a/TestInputHandler.h b/TestInputHandler.h index 66d47d5..793b866 100644 --- a/TestInputHandler.h +++ b/TestInputHandler.h @@ -1,9 +1,7 @@ /* * TestInputHandler.h - programmable input handler for testing */ -#ifndef KTE_TEST_INPUT_HANDLER_H -#define KTE_TEST_INPUT_HANDLER_H - +#pragma once #include #include "InputHandler.h" @@ -29,6 +27,4 @@ public: private: std::queue queue_; -}; - -#endif // KTE_TEST_INPUT_HANDLER_H +}; \ No newline at end of file diff --git a/TestRenderer.h b/TestRenderer.h index 8d4753a..dc04e8e 100644 --- a/TestRenderer.h +++ b/TestRenderer.h @@ -1,9 +1,7 @@ /* * TestRenderer.h - minimal renderer for testing (no actual display) */ -#ifndef KTE_TEST_RENDERER_H -#define KTE_TEST_RENDERER_H - +#pragma once #include #include "Renderer.h" @@ -31,6 +29,4 @@ public: private: std::size_t draw_count_ = 0; -}; - -#endif // KTE_TEST_RENDERER_H +}; \ No newline at end of file diff --git a/UndoNode.h b/UndoNode.h index d098a67..d870214 100644 --- a/UndoNode.h +++ b/UndoNode.h @@ -1,6 +1,4 @@ -#ifndef KTE_UNDONODE_H -#define KTE_UNDONODE_H - +#pragma once #include #include @@ -20,7 +18,4 @@ struct UndoNode { std::string text; UndoNode *child = nullptr; // next in current timeline UndoNode *next = nullptr; // redo branch -}; - - -#endif // KTE_UNDONODE_H +}; \ No newline at end of file diff --git a/UndoNodePool.h b/UndoNodePool.h index f6409d1..7204825 100644 --- a/UndoNodePool.h +++ b/UndoNodePool.h @@ -1,6 +1,4 @@ -#ifndef KTE_UNDONODEPOOL_H -#define KTE_UNDONODEPOOL_H - +#pragma once #include #include #include @@ -60,6 +58,4 @@ private: std::size_t block_size_; std::vector > blocks_; std::stack available_; -}; - -#endif // KTE_UNDONODEPOOL_H \ No newline at end of file +}; \ No newline at end of file diff --git a/UndoSystem.h b/UndoSystem.h index 979e272..e8e36fd 100644 --- a/UndoSystem.h +++ b/UndoSystem.h @@ -1,6 +1,4 @@ -#ifndef KTE_UNDOSYSTEM_H -#define KTE_UNDOSYSTEM_H - +#pragma once #include #include #include @@ -54,6 +52,4 @@ private: UndoTree &tree_; // Internal hint for Delete batching: whether next Append() should prepend bool pending_prepend_ = false; -}; - -#endif // KTE_UNDOSYSTEM_H +}; \ No newline at end of file diff --git a/UndoTree.h b/UndoTree.h index 4be268b..141b8f6 100644 --- a/UndoTree.h +++ b/UndoTree.h @@ -1,6 +1,4 @@ -#ifndef KTE_UNDOTREE_H -#define KTE_UNDOTREE_H - +#pragma once #include "UndoNode.h" @@ -9,7 +7,4 @@ struct UndoTree { UndoNode *current = nullptr; // current state of buffer UndoNode *saved = nullptr; // points to node matching last save (for dirty flag) UndoNode *pending = nullptr; // in-progress batch (detached) -}; - - -#endif // KTE_UNDOTREE_H +}; \ No newline at end of file diff --git a/fonts/Font.h b/fonts/Font.h index 15925e0..934fe2a 100644 --- a/fonts/Font.h +++ b/fonts/Font.h @@ -12,7 +12,7 @@ inline const unsigned int *DefaultFontData = BrassMonoCode::DefaultFontBoldCompr class Font { public: - Font(std::string name, unsigned int *data, const unsigned int size) + Font(std::string name, const unsigned int *data, const unsigned int size) : name_(std::move(name)), data_(data), size_(size) {} @@ -28,7 +28,7 @@ public: private: std::string name_; - unsigned int *data_{nullptr}; + const unsigned int *data_{nullptr}; unsigned int size_{0}; }; } \ No newline at end of file diff --git a/fonts/FontRegistry.cc b/fonts/FontRegistry.cc index 5167658..018024d 100644 --- a/fonts/FontRegistry.cc +++ b/fonts/FontRegistry.cc @@ -7,87 +7,87 @@ InstallDefaultFonts() { FontRegistry::Instance().Register(std::make_unique( "default", - const_cast(BrassMono::DefaultFontBoldCompressedData), + BrassMono::DefaultFontBoldCompressedData, BrassMono::DefaultFontBoldCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "b612", - const_cast(B612Mono::DefaultFontRegularCompressedData), + B612Mono::DefaultFontRegularCompressedData, B612Mono::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "brassmono", - const_cast(BrassMono::DefaultFontBoldCompressedData), + BrassMono::DefaultFontBoldCompressedData, BrassMono::DefaultFontBoldCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "brassmonocode", - const_cast(BrassMonoCode::DefaultFontBoldCompressedData), + BrassMonoCode::DefaultFontBoldCompressedData, BrassMonoCode::DefaultFontBoldCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "fira", - const_cast(FiraCode::DefaultFontRegularCompressedData), + FiraCode::DefaultFontRegularCompressedData, FiraCode::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "go", - const_cast(Go::DefaultFontRegularCompressedData), + Go::DefaultFontRegularCompressedData, Go::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "ibm", - const_cast(IBMPlexMono::DefaultFontRegularCompressedData), + IBMPlexMono::DefaultFontRegularCompressedData, IBMPlexMono::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "idealist", - const_cast(Idealist::DefaultFontRegularCompressedData), + Idealist::DefaultFontRegularCompressedData, Idealist::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "inconsolata", - const_cast(Inconsolata::DefaultFontRegularCompressedData), + Inconsolata::DefaultFontRegularCompressedData, Inconsolata::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "inconsolataex", - const_cast(InconsolataExpanded::DefaultFontRegularCompressedData), + InconsolataExpanded::DefaultFontRegularCompressedData, InconsolataExpanded::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "iosevka", - const_cast(Iosoveka::DefaultFontRegularCompressedData), + Iosoveka::DefaultFontRegularCompressedData, Iosoveka::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "iosevkaex", - const_cast(IosevkaExtended::DefaultFontRegularCompressedData), + IosevkaExtended::DefaultFontRegularCompressedData, IosevkaExtended::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "sharetech", - const_cast(ShareTech::DefaultFontRegularCompressedData), + ShareTech::DefaultFontRegularCompressedData, ShareTech::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "space", - const_cast(SpaceMono::DefaultFontRegularCompressedData), + SpaceMono::DefaultFontRegularCompressedData, SpaceMono::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "syne", - const_cast(Syne::DefaultFontRegularCompressedData), + Syne::DefaultFontRegularCompressedData, Syne::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "triplicate", - const_cast(Triplicate::DefaultFontRegularCompressedData), + Triplicate::DefaultFontRegularCompressedData, Triplicate::DefaultFontRegularCompressedSize )); FontRegistry::Instance().Register(std::make_unique( "unispace", - const_cast(Unispace::DefaultFontRegularCompressedData), + Unispace::DefaultFontRegularCompressedData, Unispace::DefaultFontRegularCompressedSize )); } diff --git a/main.cc b/main.cc index 561dc2d..fee3bed 100644 --- a/main.cc +++ b/main.cc @@ -1,7 +1,10 @@ #include +#include #include +#include #include #include +#include #include #include #include @@ -41,10 +44,10 @@ main(int argc, const char *argv[]) Editor editor; // CLI parsing using getopt_long - bool req_gui = false; - bool req_term = false; - bool show_help = false; - bool show_version = false; + bool req_gui = false; + [[maybe_unused]] bool req_term = false; + bool show_help = false; + bool show_version = false; static struct option long_opts[] = { {"gui", no_argument, nullptr, 'g'}, @@ -86,10 +89,6 @@ main(int argc, const char *argv[]) return 0; } -#if !defined(KTE_BUILD_GUI) - (void) req_term; // suppress unused warning when GUI is not compiled in -#endif - // Determine frontend #if !defined(KTE_BUILD_GUI) if (req_gui) { @@ -104,11 +103,13 @@ main(int argc, const char *argv[]) } else if (req_term) { use_gui = false; } else { - // Default depends on build target: kge defaults to GUI, kte to terminal + + + // Default depends on build target: kge defaults to GUI, kte to terminal #if defined(KTE_DEFAULT_GUI) - use_gui = true; + use_gui = true; #else - use_gui = false; + use_gui = false; #endif } #endif @@ -134,7 +135,13 @@ main(int argc, const char *argv[]) // Clamp to >=1 later; 0 disables. try { unsigned long v = std::stoul(p); - pending_line = static_cast(v); + if (v > std::numeric_limits::max()) { + std::cerr << + "kte: Warning: Line number too large, ignoring\n"; + pending_line = 0; + } else { + pending_line = static_cast(v); + } } catch (...) { // Ignore malformed huge numbers pending_line = 0; @@ -193,8 +200,15 @@ main(int argc, const char *argv[]) #if defined(KTE_BUILD_GUI) && defined(__APPLE__) if (use_gui) { /* likely using the .app, so need to cd */ - if (chdir(getenv("HOME")) != 0) { - std::cerr << "kge.app: failed to chdir to HOME" << std::endl; + const char *home = getenv("HOME"); + if (!home) { + std::cerr << "kge.app: HOME environment variable not set" << std::endl; + return 1; + } + if (chdir(home) != 0) { + std::cerr << "kge.app: failed to chdir to " << home << ": " + << std::strerror(errno) << std::endl; + return 1; } } #endif @@ -212,4 +226,4 @@ main(int argc, const char *argv[]) fe->Shutdown(); return 0; -} +} \ No newline at end of file