init.lua 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428
  1. -- Lumacs Configuration File
  2. -- This file is executed on startup and allows you to customize keybindings,
  3. -- create commands, and extend the editor with Lua.
  4. -- Lumacs Configuration File
  5. -- This file is executed on startup and allows you to customize keybindings,
  6. -- create commands, and extend the editor with Lua.
  7. -- editor:message("Loading init.lua...")
  8. -- ============================================================================
  9. -- MODE SYSTEM (Emacs-style Major and Minor Modes)
  10. -- ============================================================================
  11. -- Mode registries
  12. local major_modes = {}
  13. local minor_modes = {}
  14. -- Active modes per buffer (keyed by buffer name)
  15. local buffer_major_modes = {}
  16. local buffer_minor_modes = {}
  17. -- Define a major mode
  18. function define_major_mode(name, config)
  19. major_modes[name] = {
  20. name = name,
  21. file_patterns = config.file_patterns or {},
  22. setup = config.setup or function() end,
  23. cleanup = config.cleanup or function() end,
  24. highlight = config.highlight or nil,
  25. keybindings = config.keybindings or {},
  26. comment_syntax = config.comment_syntax or "--",
  27. }
  28. editor:message(string.format("[Mode] Registered major mode: %s", name))
  29. end
  30. -- Define a minor mode
  31. function define_minor_mode(name, config)
  32. minor_modes[name] = {
  33. name = name,
  34. setup = config.setup or function() end,
  35. cleanup = config.cleanup or function() end,
  36. keybindings = config.keybindings or {},
  37. global = config.global or false, -- If true, applies to all buffers
  38. }
  39. editor:message(string.format("[Mode] Registered minor mode: %s", name))
  40. end
  41. -- Activate a major mode for the current buffer
  42. function activate_major_mode(mode_name)
  43. local buf = editor.buffer
  44. local buf_name = buf:name()
  45. local mode = major_modes[mode_name]
  46. if not mode then
  47. editor:message("Unknown major mode: " .. mode_name)
  48. return false
  49. end
  50. -- Deactivate current major mode if any
  51. if buffer_major_modes[buf_name] then
  52. deactivate_major_mode()
  53. end
  54. editor:message(string.format("[Mode] Activating major mode '%s' for buffer '%s'", mode_name, buf_name))
  55. -- Store active mode
  56. buffer_major_modes[buf_name] = mode_name
  57. -- Set up event handler for auto-highlighting
  58. if mode.highlight then
  59. buf:on_buffer_event(function(event_data)
  60. local current_buf = editor.buffer
  61. if event_data.event == lumacs.BufferEvent.Loaded or
  62. event_data.event == lumacs.BufferEvent.LanguageChanged then
  63. mode.highlight()
  64. editor:message(string.format("[Mode] Auto-highlighted buffer with %s", mode_name))
  65. end
  66. end)
  67. -- Highlight immediately
  68. mode.highlight()
  69. end
  70. -- Apply mode-specific keybindings (these are temporary for this buffer)
  71. for key, func in pairs(mode.keybindings) do
  72. editor:bind_key(key, func)
  73. end
  74. -- Run setup function
  75. mode.setup()
  76. editor:message(string.format("Major mode: %s", mode_name))
  77. return true
  78. end
  79. -- Deactivate current major mode
  80. function deactivate_major_mode()
  81. local buf = editor.buffer
  82. local buf_name = buf:name()
  83. local mode_name = buffer_major_modes[buf_name]
  84. if not mode_name then
  85. return
  86. end
  87. local mode = major_modes[mode_name]
  88. if mode and mode.cleanup then
  89. mode.cleanup()
  90. end
  91. buffer_major_modes[buf_name] = nil
  92. editor:message(string.format("[Mode] Deactivated major mode '%s'", mode_name))
  93. end
  94. -- Toggle a minor mode
  95. function toggle_minor_mode(mode_name)
  96. local buf = editor.buffer
  97. local buf_name = buf:name()
  98. local mode = minor_modes[mode_name]
  99. if not mode then
  100. editor:message("Unknown minor mode: " .. mode_name)
  101. return
  102. end
  103. -- Initialize minor modes table for this buffer
  104. if not buffer_minor_modes[buf_name] then
  105. buffer_minor_modes[buf_name] = {}
  106. end
  107. local is_active = buffer_minor_modes[buf_name][mode_name]
  108. if is_active then
  109. -- Deactivate
  110. if mode.cleanup then
  111. mode.cleanup()
  112. end
  113. buffer_minor_modes[buf_name][mode_name] = nil
  114. editor:message(string.format("Minor mode disabled: %s", mode_name))
  115. else
  116. -- Activate
  117. buffer_minor_modes[buf_name][mode_name] = true
  118. if mode.setup then
  119. mode.setup()
  120. end
  121. editor:message(string.format("Minor mode enabled: %s", mode_name))
  122. end
  123. end
  124. -- Auto-detect and activate major mode based on file extension
  125. function auto_activate_major_mode()
  126. local buf = editor.buffer
  127. local buf_name = buf:name()
  128. -- Try to match file pattern
  129. for mode_name, mode in pairs(major_modes) do
  130. for _, pattern in ipairs(mode.file_patterns) do
  131. if string.match(buf_name, pattern) then
  132. activate_major_mode(mode_name)
  133. return
  134. end
  135. end
  136. end
  137. -- No match, use fundamental mode (default)
  138. editor:message(string.format("[Mode] No major mode matched for '%s', using fundamental-mode", buf_name))
  139. end
  140. -- Get current major mode name
  141. function current_major_mode()
  142. local buf = editor.buffer
  143. local buf_name = buf:name()
  144. return buffer_major_modes[buf_name] or "fundamental-mode"
  145. end
  146. -- ============================================================================
  147. -- MAJOR MODES
  148. -- ============================================================================
  149. -- Load individual major modes
  150. dofile("lua/major_modes/lua_mode.lua")
  151. -- ============================================================================
  152. -- MINOR MODES
  153. -- ============================================================================
  154. -- Auto-save minor mode
  155. define_minor_mode("auto-save-mode", {
  156. global = false,
  157. setup = function()
  158. -- TODO: Set up auto-save timer
  159. editor:message("[auto-save-mode] Auto-save enabled")
  160. end,
  161. cleanup = function()
  162. editor:message("[auto-save-mode] Auto-save disabled")
  163. end
  164. })
  165. -- Line numbers minor mode (conceptual - already always shown)
  166. define_minor_mode("line-numbers-mode", {
  167. global = true,
  168. setup = function()
  169. editor:message("[line-numbers-mode] Line numbers enabled")
  170. end,
  171. cleanup = function()
  172. editor:message("[line-numbers-mode] Line numbers disabled")
  173. end
  174. })
  175. -- ============================================================================
  176. -- GLOBAL KEYBINDINGS
  177. -- ============================================================================
  178. -- Example: Custom keybindings
  179. -- Syntax: editor:bind_key("key", function() ... end)
  180. -- Basic Editing Commands (moved from C++ fallback)
  181. function lumacs_insert_newline()
  182. local cursor = editor.cursor
  183. editor.buffer:insert_newline(cursor)
  184. editor:set_cursor(lumacs.Position(cursor.line + 1, 0))
  185. end
  186. editor:bind_key("Return", lumacs_insert_newline)
  187. editor:register_command("insert-newline", "Insert a new line at cursor position.", lumacs_insert_newline, true)
  188. function lumacs_backward_delete_char()
  189. local cursor_original = editor.cursor
  190. -- If at (0,0), nothing to delete before it.
  191. if cursor_original.column == 0 and cursor_original.line == 0 then
  192. return
  193. end
  194. -- Call erase_char with the current cursor position.
  195. -- erase_char will internally delete the char at (pos.column - 1).
  196. editor.buffer:erase_char(cursor_original)
  197. -- The cursor position should be adjusted *after* the erase.
  198. -- This is the same logic as before, calculating the new cursor position.
  199. local new_cursor_pos = cursor_original
  200. if new_cursor_pos.column > 0 then
  201. new_cursor_pos = lumacs.Position(new_cursor_pos.line, new_cursor_pos.column - 1)
  202. elseif new_cursor_pos.line > 0 then
  203. -- If line was joined, new cursor is at end of previous line.
  204. local prev_line_len = #editor.buffer:line(new_cursor_pos.line - 1)
  205. new_cursor_pos = lumacs.Position(new_cursor_pos.line - 1, prev_line_len)
  206. end
  207. editor.cursor = new_cursor_pos
  208. end
  209. editor:bind_key("Backspace", lumacs_backward_delete_char)
  210. editor:register_command("backward-delete-char", "Delete the character before cursor.", lumacs_backward_delete_char, true)
  211. editor:bind_key("C-m", lumacs_backward_delete_char)
  212. editor:register_command("backward-delete-char", "Delete the character before cursor.", lumacs_backward_delete_char, true)
  213. function lumacs_delete_char()
  214. local cursor = editor.cursor
  215. editor.buffer:erase_char(lumacs.Position(cursor.line, cursor.column + 1))
  216. end
  217. editor:bind_key("Delete", lumacs_delete_char)
  218. editor:register_command("delete-char", "Delete the character at cursor position.", lumacs_delete_char, true)
  219. -- Navigation Commands (explicitly bound arrow keys)
  220. editor:bind_key("ArrowUp", function() editor:move_up() end)
  221. editor:bind_key("ArrowDown", function() editor:move_down() end)
  222. editor:bind_key("ArrowLeft", function() editor:move_left() end)
  223. editor:bind_key("ArrowRight", function() editor:move_right() end)
  224. editor:bind_key("Home", function() editor:move_to_line_start() end)
  225. editor:bind_key("End", function() editor:move_to_line_end() end)
  226. -- Generic self-insert command for printable characters
  227. -- This command is special; it's called by the C++ core if no other binding matches a printable char.
  228. function self_insert_command(args)
  229. local char_to_insert = args[1]
  230. if not char_to_insert then return end
  231. local cursor = editor.cursor
  232. editor.buffer:insert(cursor, char_to_insert)
  233. editor:move_right()
  234. end
  235. editor:register_command("self-insert-command", "Insert the character pressed.", self_insert_command, true)
  236. -- Emacs-style navigation (Ctrl+N/P for next/previous line)
  237. editor:bind_key("C-n", function()
  238. editor:move_down()
  239. editor:message("Moved down")
  240. end)
  241. editor:bind_key("C-p", function()
  242. editor:move_up()
  243. editor:message("Moved up")
  244. end)
  245. editor:bind_key("C-f", function()
  246. editor:move_right()
  247. end)
  248. editor:bind_key("C-b", function()
  249. editor:move_left()
  250. end)
  251. -- Emacs-style line navigation
  252. editor:bind_key("C-a", function()
  253. editor:move_to_line_start()
  254. end)
  255. editor:bind_key("C-e", function()
  256. editor:move_to_line_end()
  257. end)
  258. -- M-f (forward-word) - Move forward one word
  259. editor:bind_key("M-f", function()
  260. editor:move_forward_word()
  261. end)
  262. -- M-b (backward-word) - Move backward one word
  263. editor:bind_key("M-b", function()
  264. editor:move_backward_word()
  265. end)
  266. -- C-v (scroll-up) - Page down
  267. editor:bind_key("C-v", function()
  268. editor:page_down()
  269. end)
  270. -- M-v (scroll-down) - Page up
  271. editor:bind_key("M-v", function()
  272. editor:page_up()
  273. end)
  274. -- M-< (beginning-of-buffer) - Go to start
  275. editor:bind_key("M-<", function()
  276. editor:goto_beginning()
  277. editor:message("Beginning of buffer")
  278. end)
  279. -- M-> (end-of-buffer) - Go to end
  280. editor:bind_key("M->", function()
  281. editor:goto_end()
  282. editor:message("End of buffer")
  283. end)
  284. -- M-g M-g (goto-line) - Jump to line number
  285. editor:bind_key("M-g g", function()
  286. editor:command_mode()
  287. -- TODO: Implement line number input in command mode
  288. end)
  289. -- Note: C-s binding moved to avoid conflicts with isearch
  290. -- Custom command: Insert timestamp
  291. editor:bind_key("C-t", function()
  292. local cursor_pos = editor.cursor
  293. local timestamp = os.date("%Y-%m-%d %H:%M:%S")
  294. editor.buffer:insert(cursor_pos, timestamp)
  295. editor:message("Inserted timestamp")
  296. end)
  297. -- Example: Helper functions you can define
  298. function goto_line(line_num)
  299. local pos = lumacs.Position(line_num - 1, 0)
  300. editor:set_cursor(pos)
  301. editor:message("Jumped to line " .. line_num)
  302. end
  303. -- Example: Buffer inspection
  304. function buffer_info()
  305. local buf = editor.buffer
  306. local cursor = editor.cursor
  307. editor:message(string.format(
  308. "Buffer: %s | Lines: %d | Cursor: %d,%d | Modified: %s",
  309. buf:name(),
  310. buf:line_count(),
  311. cursor.line + 1,
  312. cursor.column + 1,
  313. buf:is_modified() and "yes" or "no"
  314. ))
  315. end
  316. -- Bind to show buffer info
  317. -- Note: C-i and Tab are indistinguishable in most terminals (both send ASCII 9)
  318. -- Using C-u instead for buffer info
  319. editor:bind_key("C-u", buffer_info)
  320. -- Search helper function
  321. function find_next(query)
  322. local buf = editor.buffer
  323. local cursor = editor.cursor
  324. -- Start searching AFTER the current cursor position to find the next occurrence
  325. -- Otherwise we might find the same one if we are sitting on it.
  326. -- A simple way is to advance column by 1 for the search start.
  327. local search_start = lumacs.Position(cursor.line, cursor.column + 1)
  328. -- If at end of line, search from start of next line is handled by find() implementation?
  329. -- Buffer::find currently implements simple linear search from a position.
  330. -- If column is beyond end, it should handle it. Let's trust the C++ impl or adjust.
  331. local res = buf:find(query, search_start)
  332. if res then
  333. editor.cursor = res.start
  334. editor:message("Found '" .. query .. "' at " .. res.start.line .. ":" .. res.start.column)
  335. -- Optional: Highlight the found range?
  336. else
  337. editor:message("'" .. query .. "' not found")
  338. end
  339. end
  340. -- Example binding: Find "TODO"
  341. editor:bind_key("C-o", function() find_next("TODO") end)
  342. -- Line swapping functions (like Emacs M-up/down or VS Code Alt+arrows)
  343. function swap_line_up()
  344. local buf = editor.buffer
  345. local cursor = editor.cursor
  346. -- Can't move first line up
  347. if cursor.line == 0 then
  348. editor:message("Already at first line")
  349. return
  350. end
  351. editor:message("DEBUG: Starting swap_line_up, cursor at line " .. cursor.line)
  352. -- Get the current line and above line text
  353. local current_line = buf:line(cursor.line)
  354. local above_line = buf:line(cursor.line - 1)
  355. -- Strategy: Replace both lines with them swapped
  356. -- Delete from start of line above to end of current line (not including next line)
  357. local delete_start = lumacs.Position(cursor.line - 1, 0)
  358. local delete_end = lumacs.Position(cursor.line, string.len(current_line))
  359. local range = lumacs.Range(delete_start, delete_end)
  360. buf:erase(range)
  361. -- Insert them back in swapped order
  362. -- Add extra newline if above_line is empty to preserve it
  363. local insert_pos = lumacs.Position(cursor.line - 1, 0)
  364. local text = current_line .. "\n" .. above_line
  365. if above_line == "" then
  366. text = text .. "\n"
  367. end
  368. buf:insert(insert_pos, text)
  369. -- Move cursor up to follow the line
  370. editor.cursor = lumacs.Position(cursor.line - 1, cursor.column)
  371. editor:message("Swapped line up")
  372. end
  373. function swap_line_down()
  374. local buf = editor.buffer
  375. local cursor = editor.cursor
  376. -- Can't move last line down
  377. if cursor.line >= buf:line_count() - 1 then
  378. editor:message("Already at last line")
  379. return
  380. end
  381. -- Get the current line and the line below
  382. local current_line = buf:line(cursor.line)
  383. local below_line = buf:line(cursor.line + 1)
  384. -- Strategy: Replace both lines with them swapped
  385. -- Delete from start of current line to end of line below (not including line after)
  386. local delete_start = lumacs.Position(cursor.line, 0)
  387. local delete_end = lumacs.Position(cursor.line + 1, string.len(below_line))
  388. local range = lumacs.Range(delete_start, delete_end)
  389. buf:erase(range)
  390. -- Insert them back in swapped order
  391. -- Add extra newline if current_line is empty to preserve it
  392. local insert_pos = lumacs.Position(cursor.line, 0)
  393. local text = below_line .. "\n" .. current_line
  394. if current_line == "" then
  395. text = text .. "\n"
  396. end
  397. buf:insert(insert_pos, text)
  398. -- Move cursor down to follow the line
  399. editor.cursor = lumacs.Position(cursor.line + 1, cursor.column)
  400. editor:message("Swapped line down")
  401. end
  402. -- Bind to M-ArrowUp and M-ArrowDown (Meta/Alt + arrows)
  403. editor:bind_key("M-ArrowUp", swap_line_up)
  404. editor:bind_key("M-ArrowDown", swap_line_down)
  405. -- ============================================================================
  406. -- WINDOW MANAGEMENT
  407. -- ============================================================================
  408. -- Split horizontal (like Emacs C-x 2, simplified to M-2)
  409. editor:bind_key("M-2", function()
  410. editor:split_horizontally()
  411. editor:message("Split horizontally")
  412. end)
  413. -- Split vertical (like Emacs C-x 3, simplified to M-3)
  414. editor:bind_key("M-3", function()
  415. editor:split_vertically()
  416. editor:message("Split vertically")
  417. end)
  418. -- Close window (like Emacs C-x 0, simplified to M-0)
  419. editor:bind_key("M-0", function()
  420. editor:close_window()
  421. editor:message("Closed window")
  422. end)
  423. -- Command Mode (Minibuffer)
  424. editor:bind_key("M-x", function()
  425. editor:command_mode()
  426. end)
  427. -- ============================================================================
  428. -- MARK AND REGION (Emacs-style selection)
  429. -- ============================================================================
  430. -- C-@ or C-SPC (set-mark-command) - Set the mark at cursor
  431. editor:bind_key("C-@", function()
  432. local buf = editor.buffer
  433. local cursor = editor.cursor
  434. buf:set_mark(cursor)
  435. editor:message("Mark set")
  436. end)
  437. -- For terminals that don't support C-@, also bind to C-SPC (but C-SPC is hard to detect)
  438. -- Most terminals send C-@ for C-SPC, so the above should work
  439. -- C-x C-x (exchange-point-and-mark) - Swap cursor and mark
  440. editor:bind_key("C-x C-x", function()
  441. local buf = editor.buffer
  442. local mark = buf.mark
  443. if not mark then
  444. editor:message("No mark set")
  445. return
  446. end
  447. local cursor = editor.cursor
  448. buf:set_mark(cursor) -- Set mark at old cursor position
  449. editor.cursor = mark -- Move cursor to old mark position
  450. editor:message("Mark and point exchanged")
  451. end)
  452. -- C-x h (mark-whole-buffer) - Select entire buffer
  453. editor:bind_key("C-x h", function()
  454. local buf = editor.buffer
  455. -- Set mark at beginning
  456. buf:set_mark(lumacs.Position(0, 0))
  457. -- Move cursor to end
  458. local last_line = buf:line_count() - 1
  459. local last_col = #buf:line(last_line)
  460. editor.cursor = lumacs.Position(last_line, last_col)
  461. editor:message("Buffer marked")
  462. end)
  463. -- ============================================================================
  464. -- KILL RING (Emacs cut/copy/paste)
  465. -- ============================================================================
  466. -- C-w (kill-region) - Cut selection
  467. editor:bind_key("C-w", function()
  468. editor:kill_region()
  469. end)
  470. -- M-w (kill-ring-save) - Copy selection
  471. editor:bind_key("M-w", function()
  472. editor:copy_region_as_kill()
  473. end)
  474. -- C-k (kill-line) - Cut from cursor to end of line
  475. editor:bind_key("C-k", function()
  476. editor:kill_line()
  477. editor:message("Killed line")
  478. end)
  479. -- C-y (yank) - Paste
  480. editor:bind_key("C-y", function()
  481. editor:yank()
  482. end)
  483. -- M-y (yank-pop) - Cycle through kill ring after yanking
  484. editor:bind_key("M-y", function()
  485. editor:yank_pop()
  486. end)
  487. -- M-d (kill-word) - Kill word forward
  488. editor:bind_key("M-d", function()
  489. editor:kill_word()
  490. end)
  491. -- M-Backspace (backward-kill-word) - Kill word backward
  492. editor:bind_key("M-Backspace", function()
  493. editor:backward_kill_word()
  494. end)
  495. -- ============================================================================
  496. -- UNDO/REDO
  497. -- ============================================================================
  498. -- C-/ or C-_ for undo (traditional Emacs binding)
  499. editor:bind_key("C-/", function()
  500. if editor:undo() then
  501. editor:message("Undid change")
  502. else
  503. editor:message("Nothing to undo")
  504. end
  505. end)
  506. -- Also keep C-z for undo (common in other editors)
  507. editor:bind_key("C-z", function()
  508. if editor:undo() then
  509. editor:message("Undid change")
  510. else
  511. editor:message("Nothing to undo")
  512. end
  513. end)
  514. -- C-x u for redo (less common but sometimes used)
  515. editor:bind_key("C-x u", function()
  516. if editor:redo() then
  517. editor:message("Redid change")
  518. else
  519. editor:message("Nothing to redo")
  520. end
  521. end)
  522. -- Manual re-highlight key (re-applies current major mode's highlighting)
  523. editor:bind_key("C-l", function()
  524. local mode_name = current_major_mode()
  525. local mode = major_modes[mode_name]
  526. if mode and mode.highlight then
  527. mode.highlight()
  528. -- Debug: Count applied styles
  529. local buf = editor.buffer
  530. local styles_count = 0
  531. for line = 0, buf:line_count() - 1 do
  532. local styles = buf:get_line_styles(line)
  533. styles_count = styles_count + #styles
  534. end
  535. editor:message(string.format("Re-highlighted with %s (%d styles)", mode_name, styles_count))
  536. else
  537. editor:message("Current mode has no highlighting: " .. mode_name)
  538. end
  539. end)
  540. -- Mode information and control
  541. editor:bind_key("C-h m", function()
  542. local mode_name = current_major_mode()
  543. local buf = editor.buffer
  544. local buf_name = buf:name()
  545. -- Get active minor modes
  546. local minor_list = {}
  547. if buffer_minor_modes[buf_name] then
  548. for mode, _ in pairs(buffer_minor_modes[buf_name]) do
  549. table.insert(minor_list, mode)
  550. end
  551. end
  552. local minor_str = #minor_list > 0 and table.concat(minor_list, ", ") or "none"
  553. editor:message(string.format("Major: %s | Minor: %s", mode_name, minor_str))
  554. end)
  555. -- Test Escape key binding
  556. editor:bind_key("Escape", function()
  557. editor:message("Escape pressed! (Direct binding works)")
  558. end)
  559. -- C-x sequence bindings (Emacs style)
  560. editor:bind_key("C-x o", function()
  561. editor:next_window()
  562. editor:message("Switched window with C-x o")
  563. end)
  564. editor:bind_key("C-x 2", function()
  565. editor:split_horizontally()
  566. editor:message("Split horizontally with C-x 2")
  567. end)
  568. editor:bind_key("C-x 3", function()
  569. editor:split_vertically()
  570. editor:message("Split vertically with C-x 3")
  571. end)
  572. editor:bind_key("C-x 0", function()
  573. editor:close_window()
  574. editor:message("Closed window with C-x 0")
  575. end)
  576. -- ============================================================================
  577. -- BUFFER MANAGEMENT (C-x b, C-x C-b, C-x k)
  578. -- ============================================================================
  579. -- C-x b: Switch to buffer (with tab completion)
  580. editor:bind_key("C-x b", function()
  581. editor:buffer_switch_mode()
  582. end)
  583. -- C-x k: Kill buffer (with tab completion)
  584. editor:bind_key("C-x k", function()
  585. editor:kill_buffer_mode()
  586. end)
  587. -- C-x C-b: List all buffers
  588. editor:bind_key("C-x C-b", function()
  589. local buffer_info = editor:get_all_buffer_info()
  590. if #buffer_info == 0 then
  591. editor:message("No buffers open")
  592. return
  593. end
  594. -- Format buffer list
  595. local lines = {}
  596. table.insert(lines, "Buffer List:")
  597. table.insert(lines, "------------")
  598. table.insert(lines, "")
  599. table.insert(lines, string.format("% -3s % -20s % -10s %s", "Mod", "Name", "Size", "File"))
  600. table.insert(lines, string.format("% -3s % -20s % -10s %s", "---", "----"))
  601. for i, info in ipairs(buffer_info) do
  602. local modified = info.modified and " * " or " "
  603. local filepath = ""
  604. if info.filepath then
  605. filepath = tostring(info.filepath)
  606. end
  607. local line = string.format("%s % -20s % -10d %s",
  608. modified,
  609. info.name,
  610. info.size,
  611. filepath
  612. )
  613. table.insert(lines, line)
  614. end
  615. local list_text = table.concat(lines, "\n")
  616. local list_buf_name = "*Buffer List*"
  617. -- Switch to or create buffer
  618. local list_buf = editor:get_buffer_by_name(list_buf_name)
  619. if list_buf then
  620. editor:switch_buffer_in_window(list_buf_name)
  621. else
  622. editor:new_buffer(list_buf_name)
  623. end
  624. local buf = editor.buffer
  625. buf:clear()
  626. buf:insert(lumacs.Position(0,0), list_text)
  627. editor:goto_beginning()
  628. editor:message(string.format("Buffer list (%d buffers)", #buffer_info))
  629. end)
  630. -- ============================================================================
  631. -- CASE CONVERSION (M-u, M-l, M-c)
  632. -- ============================================================================
  633. -- M-u (upcase-word)
  634. editor:bind_key("M-u", function()
  635. local start_pos = editor.cursor
  636. editor:move_forward_word()
  637. local end_pos = editor.cursor
  638. local range = lumacs.Range(start_pos, end_pos)
  639. local text = editor.buffer:get_text_in_range(range)
  640. if #text > 0 then
  641. editor.buffer:replace(range, string.upper(text))
  642. end
  643. end)
  644. -- M-l (downcase-word)
  645. editor:bind_key("M-l", function()
  646. local start_pos = editor.cursor
  647. editor:move_forward_word()
  648. local end_pos = editor.cursor
  649. local range = lumacs.Range(start_pos, end_pos)
  650. local text = editor.buffer:get_text_in_range(range)
  651. if #text > 0 then
  652. editor.buffer:replace(range, string.lower(text))
  653. end
  654. end)
  655. -- M-c (capitalize-word)
  656. editor:bind_key("M-c", function()
  657. local start_pos = editor.cursor
  658. editor:move_forward_word()
  659. local end_pos = editor.cursor
  660. local range = lumacs.Range(start_pos, end_pos)
  661. local text = editor.buffer:get_text_in_range(range)
  662. if #text > 0 then
  663. local cap_text = text:sub(1,1):upper() .. text:sub(2):lower()
  664. editor.buffer:replace(range, cap_text)
  665. end
  666. end)
  667. -- C-x C-u (upcase-region)
  668. editor:bind_key("C-x C-u", function()
  669. local range = editor.buffer:get_region(editor.cursor)
  670. if not range then
  671. editor:message("No active region")
  672. return
  673. end
  674. local text = editor.buffer:get_text_in_range(range)
  675. if #text > 0 then
  676. editor.buffer:replace(range, string.upper(text))
  677. editor.buffer:deactivate_mark()
  678. end
  679. end)
  680. -- C-x C-l (downcase-region)
  681. editor:bind_key("C-x C-l", function()
  682. local range = editor.buffer:get_region(editor.cursor)
  683. if not range then
  684. editor:message("No active region")
  685. return
  686. end
  687. local text = editor.buffer:get_text_in_range(range)
  688. if #text > 0 then
  689. editor.buffer:replace(range, string.lower(text))
  690. editor.buffer:deactivate_mark()
  691. end
  692. end)
  693. -- ============================================================================
  694. -- COMMENTING (M- ;)
  695. -- ============================================================================
  696. function escape_pattern(text)
  697. return text:gsub("([^%w])", "%%%1")
  698. end
  699. function comment_dwim()
  700. local mode_name = current_major_mode()
  701. local mode = major_modes[mode_name]
  702. local prefix = mode and mode.comment_syntax or "--"
  703. local escaped_prefix = escape_pattern(prefix)
  704. local range = editor.buffer:get_region(editor.cursor)
  705. if range then
  706. -- Region handling
  707. local start_line = range.start.line
  708. local end_line = range["end"].line
  709. -- Check if all lines are commented
  710. local all_commented = true
  711. for i = start_line, end_line do
  712. local line = editor.buffer:line(i)
  713. -- Ignore empty lines for decision
  714. if #line > 0 and not string.match(line, "^%s*" .. escaped_prefix) then
  715. all_commented = false
  716. break
  717. end
  718. end
  719. -- Apply change
  720. for i = start_line, end_line do
  721. local line = editor.buffer:line(i)
  722. local line_range = lumacs.Range(lumacs.Position(i, 0), lumacs.Position(i, #line))
  723. if #line > 0 then
  724. if all_commented then
  725. -- Uncomment
  726. local s, e = string.find(line, escaped_prefix)
  727. if s then
  728. local suffix = line:sub(e+1)
  729. if suffix:sub(1,1) == " " then suffix = suffix:sub(2) end
  730. local new_line = line:sub(1, s-1) .. suffix
  731. editor.buffer:replace(line_range, new_line)
  732. end
  733. else
  734. -- Comment (if not already commented)
  735. if not string.match(line, "^%s*" .. escaped_prefix) then
  736. local indent = string.match(line, "^%s*")
  737. local content = line:sub(#indent + 1)
  738. local new_line = indent .. prefix .. " " .. content
  739. editor.buffer:replace(line_range, new_line)
  740. end
  741. end
  742. end
  743. end
  744. else
  745. -- Single line
  746. local line_num = editor.cursor.line
  747. local line = editor.buffer:line(line_num)
  748. local line_range = lumacs.Range(lumacs.Position(line_num, 0), lumacs.Position(line_num, #line))
  749. if string.match(line, "^%s*" .. escaped_prefix) then
  750. -- Uncomment
  751. local s, e = string.find(line, escaped_prefix)
  752. if s then
  753. local suffix = line:sub(e+1)
  754. if suffix:sub(1,1) == " " then suffix = suffix:sub(2) end
  755. local new_line = line:sub(1, s-1) .. suffix
  756. editor.buffer:replace(line_range, new_line)
  757. end
  758. else
  759. -- Comment
  760. local indent = string.match(line, "^%s*")
  761. local content = line:sub(#indent + 1)
  762. local new_line = indent .. prefix .. " " .. content
  763. editor.buffer:replace(line_range, new_line)
  764. end
  765. end
  766. end
  767. editor:bind_key("M-;", comment_dwim)
  768. -- ============================================================================
  769. -- REGISTERS (C-x r s, C-x r i)
  770. -- ============================================================================
  771. -- Helper function to prompt for register character
  772. function get_register_char(prompt_msg)
  773. editor:message(prompt_msg)
  774. -- For now we'll use a simple approach - this would need UI integration
  775. -- In a full implementation, this would wait for a single character input
  776. -- For demo purposes, we'll use 'a' as default
  777. return 'a'
  778. end
  779. -- C-x r s (copy-to-register) - Save region to register
  780. editor:bind_key("C-x r s", function()
  781. local register_char = get_register_char("Save to register:")
  782. editor:copy_region_to_register(register_char)
  783. end)
  784. -- C-x r i (insert-register) - Insert from register
  785. editor:bind_key("C-x r i", function()
  786. local register_char = get_register_char("Insert register:")
  787. editor:yank_from_register(register_char)
  788. end)
  789. -- ============================================================================
  790. -- RECTANGLES (C-x r k/y/t)
  791. -- ============================================================================
  792. -- C-x r k (kill-rectangle) - Cut rectangular region
  793. editor:bind_key("C-x r k", function()
  794. editor:kill_rectangle()
  795. end)
  796. -- C-x r y (yank-rectangle) - Paste rectangular region
  797. editor:bind_key("C-x r y", function()
  798. editor:yank_rectangle()
  799. end)
  800. -- C-x r t (string-rectangle) - Fill rectangle with text
  801. editor:bind_key("C-x r t", function()
  802. -- For demo purposes, we'll fill with asterisks
  803. -- In a full implementation, this would prompt for text input
  804. editor:string_rectangle("*")
  805. editor:message("Rectangle filled with '*' - C-x r t demo")
  806. end)
  807. -- ============================================================================
  808. -- KEYBOARD MACROS (F3, F4)
  809. -- ============================================================================
  810. -- F3 - start-kbd-macro
  811. editor:bind_key("F3", function()
  812. editor:start_kbd_macro()
  813. end)
  814. -- F4 - end-kbd-macro or call-last-kbd-macro
  815. editor:bind_key("F4", function()
  816. editor:end_kbd_macro_or_call()
  817. end)
  818. -- ============================================================================
  819. -- INCREMENTAL SEARCH (C-s, C-r)
  820. -- ============================================================================
  821. -- C-s (save-buffer) - changed from isearch to save for now
  822. editor:bind_key("C-s", function()
  823. local buf = editor.buffer
  824. if buf:save() then
  825. editor:message("Buffer saved: " .. buf:name())
  826. else
  827. editor:message("Failed to save buffer")
  828. end
  829. end)
  830. -- C-r (isearch-backward)
  831. editor:bind_key("C-r", function()
  832. editor:isearch_backward_mode()
  833. end)
  834. -- Note: C-k and C-s already defined above, removed duplicates
  835. -- ============================================================================
  836. -- CONFIGURATION FUNCTIONS
  837. -- ============================================================================
  838. -- Toggle line numbers on/off
  839. function toggle_line_numbers()
  840. local config = editor.config
  841. local current = config:get_bool("show_line_numbers", true)
  842. config:set("show_line_numbers", not current)
  843. if not current then
  844. editor:message("Line numbers enabled")
  845. else
  846. editor:message("Line numbers disabled")
  847. end
  848. end
  849. -- Set line number width
  850. function set_line_number_width(width)
  851. editor.config:set("line_number_width", width)
  852. editor:message("Line number width set to " .. width)
  853. end
  854. -- Show current configuration
  855. function show_config()
  856. local config = editor.config
  857. local show_nums = config:get_bool("show_line_numbers", true)
  858. local width = config:get_int("line_number_width", 6)
  859. editor:message(string.format("Line numbers: %s, Width: %d", show_nums and "on" or "off", width))
  860. end
  861. -- Toggle modeline (status bar for each window) function
  862. function toggle_modeline()
  863. local current = editor.config:get_bool("show_modeline", true)
  864. editor.config:set_bool("show_modeline", not current)
  865. if current then
  866. editor:message("Modeline disabled")
  867. else
  868. editor:message("Modeline enabled")
  869. end
  870. end
  871. -- Bind configuration functions
  872. editor:bind_key("C-x l", toggle_line_numbers) -- C-x l to toggle line numbers
  873. editor:bind_key("C-x m", toggle_modeline) -- C-x m to toggle modeline
  874. editor:bind_key("C-x C-c", function() editor:quit() end) -- C-x C-c to quit
  875. editor:bind_key("C-x C-s", show_config) -- C-x C-s to show config
  876. -- Load theme configuration
  877. -- Load individual theme definitions
  878. dofile("lua/themes/default.lua")
  879. dofile("lua/themes/dracula.lua")
  880. dofile("lua/themes/everforest-dark.lua")
  881. dofile("lua/themes/gruvbox-light.lua")
  882. dofile("lua/themes/nord.lua")
  883. dofile("lua/themes/solarized-dark.lua")
  884. -- Load theme switching functions and keybindings
  885. dofile("lua/themes/themes_init.lua")
  886. -- Load major modes
  887. dofile("lua/major_modes/c_cpp_mode.lua")
  888. -- Welcome message
  889. editor:message("Lumacs ready! C-k=kill, C-y=yank, C-@=mark, C-w=cut, M-w=copy, M-f/b=word, C-v/M-v=page")
  890. -- Auto-activate mode for initial buffer
  891. auto_activate_major_mode()
  892. -- File and Buffer Commands
  893. editor:register_command("save-buffer", "Save current buffer", function(args)
  894. if editor.buffer:save() then
  895. editor:message("Saved " .. editor.buffer:name())
  896. else
  897. editor:message("Save failed")
  898. end
  899. end, true, "b") -- "b" means prompt for existing buffer name
  900. editor:register_command("find-file", "Find file", function(args)
  901. editor:find_file_mode()
  902. end, true, "f") -- "f" means prompt for file name
  903. editor:register_command("kill-buffer", "Kill buffer", function(args)
  904. editor:kill_buffer_mode()
  905. end, true, "b")
  906. editor:register_command("switch-buffer", "Switch buffer", function(args)
  907. editor:buffer_switch_mode()
  908. end, true, "b")
  909. -- Alias for switch-buffer to match Emacs expectations
  910. editor:register_command("switch-to-buffer", "Switch buffer (Alias)", function(args)
  911. editor:buffer_switch_mode()
  912. end, true, "b")
  913. editor:register_command("list-buffers", "List all buffers", function(args)
  914. local buffer_info = editor:get_all_buffer_info()
  915. if #buffer_info == 0 then
  916. editor:message("No buffers open")
  917. return {success = true, message = "No buffers open"}
  918. end
  919. local lines = {}
  920. table.insert(lines, "Buffer List:")
  921. table.insert(lines, "------------")
  922. table.insert(lines, "")
  923. table.insert(lines, string.format("% -3s % -20s % -10s %s", "Mod", "Name", "Size", "File"))
  924. table.insert(lines, string.format("% -3s % -20s % -10s %s", "---", "----"))
  925. for i, info in ipairs(buffer_info) do
  926. local modified = info.modified and " * " or " "
  927. local filepath = ""
  928. if info.filepath then filepath = tostring(info.filepath) end
  929. table.insert(lines, string.format("%s % -20s % -10d %s", modified, info.name, info.size, filepath))
  930. end
  931. local list_text = table.concat(lines, "\n")
  932. local list_buf_name = "*Buffer List*"
  933. local list_buf = editor:get_buffer_by_name(list_buf_name)
  934. if list_buf then
  935. editor:switch_buffer_in_window(list_buf_name)
  936. else
  937. editor:new_buffer(list_buf_name)
  938. end
  939. editor.buffer:clear()
  940. editor.buffer:insert(lumacs.Position(0,0), list_text)
  941. editor:goto_beginning()
  942. return {success = true, message = string.format("Buffer list (%d buffers)", #buffer_info)}
  943. end, false) -- Not typically interactive through prompt
  944. -- Navigation
  945. editor:register_command("next-line", "Move cursor down", function() editor:move_down() end, true)
  946. editor:register_command("previous-line", "Move cursor up", function() editor:move_up() end, true)
  947. editor:register_command("forward-char", "Move cursor right", function() editor:move_right() end, true)
  948. editor:register_command("backward-char", "Move cursor left", function() editor:move_left() end, true)
  949. editor:register_command("forward-word", "Move forward one word", function() editor:move_forward_word() end, true)
  950. editor:register_command("backward-word", "Move backward one word", function() editor:move_backward_word() end, true)
  951. editor:register_command("beginning-of-buffer", "Go to beginning of buffer", function() editor:goto_beginning() end, true)
  952. editor:register_command("end-of-buffer", "Go to end of buffer", function() editor:goto_end() end, true)
  953. editor:register_command("scroll-up-command", "Page down", function() editor:page_down() end, true)
  954. editor:register_command("scroll-down-command", "Page up", function() editor:page_up() end, true)
  955. -- Window Management
  956. editor:register_command("split-window-below", "Split window horizontally", function() editor:split_horizontally() end, true)
  957. editor:register_command("split-window-right", "Split window vertically", function() editor:split_vertically() end, true)
  958. editor:register_command("delete-window", "Close current window", function() editor:close_window() end, true)
  959. editor:register_command("other-window", "Select other window", function() editor:next_window() end, true)
  960. editor:register_command("delete-other-windows", "Delete all other windows", function()
  961. -- Simplified: keep closing others until only 1?
  962. -- Or just implement properly in C++ later.
  963. -- For now, assume users use C-x 1 if implemented?
  964. -- C-x 1 isn't implemented in init.lua yet.
  965. editor:message("delete-other-windows not implemented yet")
  966. return {success = false, message = "delete-other-windows not implemented yet"}
  967. end, false)
  968. -- Editing
  969. editor:register_command("kill-line", "Kill rest of line", function() editor:kill_line() end, true)
  970. editor:register_command("kill-region", "Kill selected region", function() editor:kill_region() end, true)
  971. editor:register_command("copy-region-as-kill", "Copy region", function() editor:copy_region_as_kill() end, true)
  972. editor:register_command("yank", "Paste from kill ring", function() editor:yank() end, true)
  973. editor:register_command("undo", "Undo last change", function() if editor:undo() then editor:message("Undid") else editor:message("No undo") end end, true)
  974. editor:register_command("redo", "Redo last undo", function() if editor:redo() then editor:message("Redid") else editor:message("No redo") end end, true)
  975. -- Modes
  976. editor:register_command("lua-mode", "Switch to Lua mode", function() activate_major_mode("lua-mode") end, true)
  977. editor:register_command("fundamental-mode", "Switch to Fundamental mode", function() activate_major_mode("fundamental-mode") end, true)
  978. editor:register_command("auto-save-mode", "Toggle auto-save", function() toggle_minor_mode("auto-save-mode") end, true)
  979. editor:message("Commands loaded. Try M-x list-buffers")
  980. -- ============================================================================
  981. -- NEW COMMAND SYSTEM INTEGRATION
  982. -- ============================================================================
  983. -- Register additional commands with the new command system
  984. editor:register_command("describe-mode", "Show current major and minor modes", function(args)
  985. local mode_name = current_major_mode()
  986. local buf = editor.buffer
  987. local buf_name = buf:name()
  988. local minor_list = {}
  989. if buffer_minor_modes[buf_name] then
  990. for mode, _ in pairs(buffer_minor_modes[buf_name]) do
  991. table.insert(minor_list, mode)
  992. end
  993. end
  994. local minor_str = #minor_list > 0 and table.concat(minor_list, ", ") or "none"
  995. return {success = true, message = string.format("Major: %s | Minor: %s", mode_name, minor_str)}
  996. end, false)
  997. editor:register_command("count-lines", "Count lines in buffer or region", function(args)
  998. local buf = editor.buffer
  999. local region = buf:get_region(editor.cursor)
  1000. if region then
  1001. local lines = region["end"].line - region.start.line + 1
  1002. return {success = true, message = string.format("Region has %d lines", lines)}
  1003. else
  1004. local lines = buf:line_count()
  1005. return {success = true, message = string.format("Buffer has %d lines", lines)}
  1006. end
  1007. end, false)
  1008. editor:register_command("word-count", "Count words in buffer or region", function(args)
  1009. local buf = editor.buffer
  1010. local region = buf:get_region(editor.cursor)
  1011. local text
  1012. if region then
  1013. text = buf:get_text_in_range(region)
  1014. else
  1015. text = buf:get_all_text()
  1016. end
  1017. local words = 0
  1018. for word in text:gmatch("%S+") do
  1019. words = words + 1
  1020. end
  1021. local target = region and "region" or "buffer"
  1022. return {success = true, message = string.format("%s has %d words", target, words)}
  1023. end, false)
  1024. editor:register_command("goto-char", "Go to character position", function(args)
  1025. if #args == 0 then
  1026. return {success = false, message = "Character position required"}
  1027. end
  1028. local pos = tonumber(args[1])
  1029. if not pos then
  1030. return {success = false, message = "Invalid character position: " .. args[1]}
  1031. end
  1032. local buf = editor.buffer
  1033. local text = buf:get_all_text()
  1034. if pos < 1 or pos > #text then
  1035. return {success = false, message = "Position out of range"}
  1036. end
  1037. -- Convert character position to line/column
  1038. local line = 0
  1039. local col = 0
  1040. for i = 1, pos - 1 do
  1041. if text:sub(i, i) == '\n' then
  1042. line = line + 1
  1043. col = 0
  1044. else
  1045. col = col + 1
  1046. end
  1047. end
  1048. editor.cursor = lumacs.Position(line, col)
  1049. return {success = true, message = string.format("Moved to character %d (line %d, column %d)", pos, line + 1, col + 1)}
  1050. end, true, "n")
  1051. editor:register_command("insert-date", "Insert current date", function(args)
  1052. local cursor_pos = editor.cursor
  1053. local timestamp = os.date("%Y-%m-%d")
  1054. editor.buffer:insert(cursor_pos, timestamp)
  1055. return {success = true, message = "Inserted current date"}
  1056. end, false)
  1057. editor:register_command("insert-datetime", "Insert current date and time", function(args)
  1058. local cursor_pos = editor.cursor
  1059. local timestamp = os.date("%Y-%m-%d %H:%M:%S")
  1060. editor.buffer:insert(cursor_pos, timestamp)
  1061. return {success = true, message = "Inserted current date and time"}
  1062. end, false)
  1063. editor:register_command("revert-buffer", "Reload buffer from file", function(args)
  1064. local buf = editor.buffer
  1065. local filepath = buf:filepath()
  1066. if not filepath then
  1067. return {success = false, message = "Buffer is not visiting a file"}
  1068. end
  1069. if buf:is_modified() then
  1070. return {success = false, message = "Buffer has unsaved changes"}
  1071. end
  1072. if editor:load_file(filepath) then
  1073. return {success = true, message = "Reverted " .. buf:name()}
  1074. else
  1075. return {success = false, message = "Failed to revert buffer"}
  1076. end
  1077. end, false)
  1078. editor:register_command("rename-buffer", "Rename current buffer", function(args)
  1079. if #args == 0 then
  1080. return {success = false, message = "New buffer name required"}
  1081. end
  1082. local new_name = args[1]
  1083. local buf = editor.buffer
  1084. local old_name = buf:name()
  1085. -- Check if name is already taken
  1086. if editor:get_buffer_by_name(new_name) then
  1087. return {success = false, message = "Buffer name already exists: " .. new_name}
  1088. end
  1089. buf:set_name(new_name)
  1090. return {success = true, message = string.format("Renamed buffer '%s' to '%s'", old_name, new_name)}
  1091. end, true, "s") -- "s" means prompt for string
  1092. -- Development commands
  1093. editor:register_command("eval-expression", "Evaluate Lua expression", function(args)
  1094. if #args == 0 then
  1095. return {success = false, message = "Lua expression required"}
  1096. end
  1097. local expr = table.concat(args, " ")
  1098. local func, err = load("return " .. expr)
  1099. if not func then
  1100. return {success = false, message = "Parse error: " .. err}
  1101. end
  1102. local success, result = pcall(func)
  1103. if success then
  1104. return {success = true, message = tostring(result)}
  1105. else
  1106. return {success = false, message = "Error: " .. tostring(result)}
  1107. end
  1108. end, true, "s")
  1109. -- Example of how to define a custom command that changes theme based on time of day
  1110. editor:register_command("auto-theme", "Automatically set theme based on time of day", function(args)
  1111. local hour = tonumber(os.date("%H"))
  1112. local theme_name
  1113. if hour >= 6 and hour < 18 then
  1114. -- Daytime: use light theme
  1115. theme_name = "gruvbox-light"
  1116. elseif hour >= 18 and hour < 22 then
  1117. -- Evening: use warm theme
  1118. theme_name = "everforest-dark"
  1119. else
  1120. -- Night: use dark theme
  1121. theme_name = "nord"
  1122. end
  1123. local success, message = editor:execute_command("set-theme", {theme_name})
  1124. if success then
  1125. return {success = true, message = string.format("Auto-selected %s theme for %d:00", theme_name, hour)}
  1126. else
  1127. return {success = false, message = "Failed to auto-select theme: " .. message}
  1128. end
  1129. end, false)
  1130. editor:register_command("theme-demo", "Demonstrate theme switching", function(args)
  1131. local themes = {"solarized-dark", "nord", "gruvbox-light", "dracula"}
  1132. local demo_msg = "Theme demo - switching through: " .. table.concat(themes, ", ")
  1133. -- This would ideally be enhanced with a timer to show themes in sequence
  1134. -- For now, just switch to a demo theme
  1135. local success, message = editor:execute_command("set-theme", {themes[1]})
  1136. if success then
  1137. return {success = true, message = demo_msg .. " (switched to " .. themes[1] .. ")"}
  1138. else
  1139. return {success = false, message = "Demo failed: " .. message}
  1140. end
  1141. end, false)
  1142. -- ============================================================================
  1143. -- COMPLETION SYSTEM (Minibuffer Auto-Complete)
  1144. -- ============================================================================
  1145. -- Returns a list of completion candidates based on the current mode and input
  1146. function get_completion_candidates(mode_name, input)
  1147. local candidates = {}
  1148. if mode_name == "Command" then
  1149. -- Command completion (M-x)
  1150. for name, _ in pairs(lumacs.command_registry) do
  1151. if name:find(input, 1, true) == 1 then -- Prefix match
  1152. table.insert(candidates, name)
  1153. end
  1154. end
  1155. table.sort(candidates)
  1156. elseif mode_name == "BufferSwitch" or mode_name == "KillBuffer" then
  1157. -- Buffer name completion
  1158. local buffers = editor:get_buffer_names()
  1159. for _, name in ipairs(buffers) do
  1160. if name:find(input, 1, true) == 1 then -- Prefix match
  1161. table.insert(candidates, name)
  1162. end
  1163. end
  1164. table.sort(candidates)
  1165. elseif mode_name == "FindFile" then
  1166. -- File path completion (simple version)
  1167. -- Note: Full file system completion is complex to do in pure Lua without bindings
  1168. -- This is a placeholder or relies on a bound helper if available.
  1169. -- For now, we'll return empty list or maybe just current directory files if exposed.
  1170. -- Since we don't have 'ls' exposed, we can't do much here yet without C++ help.
  1171. end
  1172. return candidates
  1173. end