init.lua 46 KB

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