| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- -- goto-line.lua
- -- Go to a specific line number (M-g g in Emacs)
- -- ============================================================================
- local goto_line = {}
- -- Configuration
- goto_line.config = {
- enabled = true,
- }
- -- Go to a specific line number
- function goto_line.goto_line(line_num)
- if not line_num then
- return {success = false, message = "No line number specified"}
- end
- local num = tonumber(line_num)
- if not num or num < 1 then
- return {success = false, message = "Invalid line number: " .. tostring(line_num)}
- end
- -- Convert to 0-indexed for internal use
- local target_line = num - 1
- local line_count = editor:line_count()
- if target_line >= line_count then
- target_line = line_count - 1
- end
- -- Move cursor to the beginning of the target line
- editor:set_cursor(target_line, 0)
- return {success = true, message = "Line " .. num}
- end
- -- Interactive go-to-line that prompts for line number
- function goto_line.goto_line_interactive()
- -- This will be called by M-g g, prompting via minibuffer
- local pos = editor:cursor_pos()
- local current_line = pos.line + 1 -- 1-indexed for display
- return {success = true, message = "Goto line (current: " .. current_line .. "): "}
- end
- -- Setup function
- function goto_line.setup(opts)
- opts = opts or {}
- for k, v in pairs(opts) do
- goto_line.config[k] = v
- end
- if not goto_line.config.enabled then
- return
- end
- -- Register the go-to-line command
- editor:register_command("goto-line", "Go to a specific line number", function(args)
- if #args == 0 then
- -- Show current line info
- local pos = editor:cursor_pos()
- local current_line = pos.line + 1
- local line_count = editor:line_count()
- return {success = true, message = string.format("Line %d of %d", current_line, line_count)}
- end
- return goto_line.goto_line(args[1])
- end, {}, true, "n") -- "n" for number argument
- -- M-g g binding (Emacs standard)
- editor:bind_key("M-g g", "goto-line", "Go to line number")
- editor:bind_key("M-g M-g", "goto-line", "Go to line number")
- print("[goto-line] Package loaded")
- end
- -- Auto-setup with defaults
- goto_line.setup()
- return goto_line
|