4.3 KiB
4.3 KiB
Constants Refactoring Documentation
Overview
This document describes the refactoring of #defines and common constants into a single centralized header file for Kyle's Editor (ke).
Changes Made
1. Created ke_constants.h
A new header file was created to centralize all common constants and preprocessor definitions used throughout the project.
Location: /Users/kyle/src/ke2/ke_constants.h
Contents:
- Version Information:
KE_VERSION- Editor version string - Terminal Sequences:
ESCSEQ- ANSI escape sequence prefix - Keyboard Macros:
CTRL_KEY(key)- Control key combination macro - Display Constants:
TAB_STOP(8) - Tab stop widthMSG_TIMEO(3) - Message timeout in seconds
- Memory Management:
INITIAL_CAPACITY(64) - Initial buffer capacity - Keyboard Modes:
MODE_NORMAL(0) - Normal editing modeMODE_KCOMMAND(1) - Command mode (^k commands)MODE_ESCAPE(2) - Escape key handling mode
- Kill Ring Operations:
KILLRING_NO_OP(0) - No operationKILLRING_APPEND(1) - Append deleted charactersKILLRING_PREPEND(2) - Prepend deleted charactersKILLING_SET(3) - Set killring to deleted characterKILLRING_FLUSH(4) - Clear the killring
- Legacy Initializers:
ABUF_INIT- C struct initializer for append buffer
2. Updated main.c
- Added
#include "ke_constants.h"at the top with other includes - Removed all duplicate #define statements that were moved to the constants header
- Eliminated the duplicate
TAB_STOPdefinition (was defined twice on lines 36 and 50) - Cleaned up approximately 30+ lines of redundant definitions
3. Updated erow.cpp
- Added
#include "ke_constants.h"with extern "C" linkage for C++ compatibility - Removed local
constexpr int TAB_STOP = 8definition - Now uses the centralized
TAB_STOPconstant fromke_constants.h
Benefits
- Single Source of Truth: All constants are now defined in one location, making them easier to maintain and modify
- Eliminated Duplication: Removed duplicate definitions (e.g., TAB_STOP was defined twice in main.c)
- Better Organization: Constants are grouped logically by category with clear comments
- Cross-Language Compatibility: Header works with both C (main.c) and C++ (erow.cpp) code
- Easier Maintenance: Future changes to constants only need to be made in one place
- Improved Readability: Cleaner source files with less clutter from #define statements
Build System Compatibility
Both build systems have been tested and verified to work correctly:
Makefile Build
make clean
make
Successfully compiles all files (main.c, abuf.cpp, erow.cpp) and links the executable.
CMake Build
cd cmake-build-debug
cmake --build . --clean-first
Successfully builds the project with all components.
Header Guard
The header uses standard include guards:
#ifndef KE_CONSTANTS_H
#define KE_CONSTANTS_H
...
#endif /* KE_CONSTANTS_H */
C/C++ Interoperability
When including in C++ files, use extern "C" linkage:
extern "C" {
#include "ke_constants.h"
}
This ensures proper linkage and prevents name mangling issues.
Future Considerations
- Additional Constants: As the project grows, any new constants should be added to
ke_constants.hrather than being scattered across source files - Namespacing: For C++ constants, consider creating a dedicated C++ header with namespaced constants
- Type Safety: For C++ code, consider using
constexprorinline constexprvariables in a C++ header for better type safety - Documentation: Keep this document updated as new constants are added or modified
Testing
All compilation tests passed:
- ✅ Makefile build: Clean compilation with no errors or warnings
- ✅ CMake build: Clean compilation with no errors or warnings
- ✅ All source files (C and C++) successfully include and use the constants
- ✅ No duplicate definition errors
- ✅ Proper linkage between C and C++ components
Related Files
ke_constants.h- The new constants header (created)main.c- Updated to use new header (modified)erow.cpp- Updated to use new header (modified)Makefile- No changes required (working)CMakeLists.txt- No changes required (working)
Date
November 24, 2025