1
0
mirror of https://github.com/jdhao/nvim-config.git synced 2025-06-08 14:14:33 +02:00

Compare commits

..

4 Commits

Author SHA1 Message Date
jdhao
a9fc298063 update documentation for resuming cursor position 2024-08-30 01:03:13 +02:00
jdhao
9071e045eb Make * and # search case aware
By default, when you press `*` to search word under the cursor, the
option `smartcase` is ignored. This means that if you press `*` when
your cursor is on `The`, the lower case one `the` is also searched.

In this commit, we translate star and # search into normal search so that
'smartcase' option is respected.
2024-08-30 01:01:04 +02:00
jdhao
fa79647a63 Fix type for option relativenumber
"relativenumber" should be window local option, not buffer local option.
2024-08-29 21:45:07 +02:00
jdhao
4b4ff5f549 Add error handling for resuming the cursor
This will close issue #328.
2024-08-29 21:41:13 +02:00
2 changed files with 32 additions and 9 deletions

View File

@ -35,33 +35,48 @@ keymap.set("n", "N", "", {
end,
})
local no_word_under_cursor = function()
local check_cursor_word = function()
local cursor_word = vim.fn.expand("<cword>")
local result = cursor_word == ""
if result then
local msg = "E348: No string under cursor"
api.nvim_err_writeln(msg)
end
return result
return result, cursor_word
end
keymap.set("n", "*", "", {
callback = function()
if no_word_under_cursor() then
local cursor_word_empty, cursor_word = check_cursor_word()
if cursor_word_empty then
return
end
vim.fn.execute("normal! *N")
local cmd = string.format([[normal! /\v<%s>]], cursor_word)
-- In order to say that we are pressing Enter key, instead of typing literally the character,
-- we need to replace special notation with their internal representation.
local escaped_enter = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
-- character `N` is used to keep the cursor when pressing `*`
local full_cmd = cmd .. escaped_enter .. "N"
vim.fn.execute(full_cmd)
hlslens.start()
end,
})
keymap.set("n", "#", "", {
callback = function()
if no_word_under_cursor() then
local cursor_word_empty, cursor_word = check_cursor_word()
if cursor_word_empty then
return
end
vim.fn.execute("normal! #N")
local cmd = string.format([[normal! ?\v<%s>]], cursor_word)
local escaped_enter = vim.api.nvim_replace_termcodes("<CR>", true, false, true)
local full_cmd = cmd .. escaped_enter .. "N"
vim.fn.execute(full_cmd)
hlslens.start()
end,
})

View File

@ -163,8 +163,16 @@ api.nvim_create_autocmd("FileType", {
-- vim.print(string.format("mark_pos: %s", vim.inspect(mark_pos)))
-- it seems that without vim.schedule, the cursor position can not be set correctly
vim.schedule(function()
api.nvim_win_set_cursor(0, mark_pos)
local status, result = pcall(api.nvim_win_set_cursor, 0, mark_pos)
if not status then
api.nvim_err_writeln(string.format("Failed to resume cursor position. Context %s, error: %s",
vim.inspect(ev), result))
end
end)
-- the following two ways also seem to work,
-- ref: https://www.reddit.com/r/neovim/comments/104lc26/how_can_i_press_escape_key_using_lua/
-- vim.api.nvim_feedkeys("g`\"", "n", true)
-- vim.fn.execute("normal! g`\"")
end
end,
})
@ -257,7 +265,7 @@ api.nvim_create_autocmd("BufReadPre", {
if fn.getfsize(f) > file_size_limit or fn.getfsize(f) == -2 then
vim.o.eventignore = "all"
-- turning off relative number helps a lot
vim.bo.relativenumber = false
vim.wo.relativenumber = false
vim.bo.swapfile = false
vim.bo.bufhidden = "unload"