init.lua 47 KB

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