init.lua 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. -- goto-line.lua
  2. -- Go to a specific line number (M-g g in Emacs)
  3. -- ============================================================================
  4. local goto_line = {}
  5. -- Configuration
  6. goto_line.config = {
  7. enabled = true,
  8. }
  9. -- Go to a specific line number
  10. function goto_line.goto_line(line_num)
  11. if not line_num then
  12. return {success = false, message = "No line number specified"}
  13. end
  14. local num = tonumber(line_num)
  15. if not num or num < 1 then
  16. return {success = false, message = "Invalid line number: " .. tostring(line_num)}
  17. end
  18. -- Convert to 0-indexed for internal use
  19. local target_line = num - 1
  20. local line_count = editor:line_count()
  21. if target_line >= line_count then
  22. target_line = line_count - 1
  23. end
  24. -- Move cursor to the beginning of the target line
  25. editor:set_cursor(target_line, 0)
  26. return {success = true, message = "Line " .. num}
  27. end
  28. -- Interactive go-to-line that prompts for line number
  29. function goto_line.goto_line_interactive()
  30. -- This will be called by M-g g, prompting via minibuffer
  31. local pos = editor:cursor_pos()
  32. local current_line = pos.line + 1 -- 1-indexed for display
  33. return {success = true, message = "Goto line (current: " .. current_line .. "): "}
  34. end
  35. -- Setup function
  36. function goto_line.setup(opts)
  37. opts = opts or {}
  38. for k, v in pairs(opts) do
  39. goto_line.config[k] = v
  40. end
  41. if not goto_line.config.enabled then
  42. return
  43. end
  44. -- Register the go-to-line command
  45. editor:register_command("goto-line", "Go to a specific line number", function(args)
  46. if #args == 0 then
  47. -- Show current line info
  48. local pos = editor:cursor_pos()
  49. local current_line = pos.line + 1
  50. local line_count = editor:line_count()
  51. return {success = true, message = string.format("Line %d of %d", current_line, line_count)}
  52. end
  53. return goto_line.goto_line(args[1])
  54. end, {}, true, "n") -- "n" for number argument
  55. -- M-g g binding (Emacs standard)
  56. editor:bind_key("M-g g", "goto-line", "Go to line number")
  57. editor:bind_key("M-g M-g", "goto-line", "Go to line number")
  58. print("[goto-line] Package loaded")
  59. end
  60. -- Auto-setup with defaults
  61. goto_line.setup()
  62. return goto_line