├── LICENSE ├── README.md └── lua └── nvim-lastplace └── init.lua /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 ethanholz 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # NO LONGER MAINTAINED 2 | This is plugin is no longer maintained and works well enough for my use case. 3 | 4 | Feel free to fork and update as needed. 5 | 6 | ## nvim-lastplace 7 | A Lua rewrite of vim-lastplace 8 | 9 | Heavily inspired by https://github.com/farmergreg/vim-lastplace 10 | 11 | ## Installation 12 | [packer.nvim](https://github.com/wbthomason/packer.nvim) 13 | ```lua 14 | use 'ethanholz/nvim-lastplace' 15 | 16 | ``` 17 | [paq](https://github.com/savq/paq-nvim) 18 | ```lua 19 | paq 'ethanholz/nvim-lastplace' 20 | ``` 21 | 22 | Then add the following to your init.lua: 23 | ```lua 24 | require'nvim-lastplace'.setup{} 25 | ``` 26 | You may set options using the following: 27 | ```lua 28 | require'nvim-lastplace'.setup { 29 | lastplace_ignore_buftype = {"quickfix", "nofile", "help"}, 30 | lastplace_ignore_filetype = {"gitcommit", "gitrebase", "svn", "hgcommit"}, 31 | lastplace_open_folds = true 32 | } 33 | ``` 34 | 35 | For those of you still using Vimscript to configure your init.vim: 36 | ```vim 37 | lua require'nvim-lastplace'.setup{} 38 | ``` 39 | You can now set options using: 40 | ```vim 41 | let g:lastplace_ignore_buftype = "quickfix,nofile,help" 42 | let g:lastplace_ignore_filetype = "gitcommit,gitrebase,svn,hgcommit" 43 | let g:lastplace_open_folds = 1 44 | ``` 45 | -------------------------------------------------------------------------------- /lua/nvim-lastplace/init.lua: -------------------------------------------------------------------------------- 1 | local fn = vim.fn 2 | local lastplace = {} 3 | 4 | local function split_on_comma(str) 5 | local ret_tab = {} 6 | if str == nil then 7 | return nil 8 | end 9 | for word in string.gmatch(str, "([^,]+)") do 10 | table.insert(ret_tab, word) 11 | end 12 | return ret_tab 13 | end 14 | 15 | local function set_option(option, default) 16 | -- Coalesce boolean options to integer 0 or 1 17 | if type(lastplace.options[option]) == "boolean" then 18 | lastplace.options[option] = lastplace.options[option] and 1 or 0 19 | end 20 | 21 | -- Set option to either the option value or the default 22 | lastplace.options[option] = lastplace.options[option] or split_on_comma(vim.g[option]) or default 23 | end 24 | 25 | function lastplace.setup(options) 26 | options = options or {} 27 | lastplace.options = options 28 | set_option("lastplace_ignore_buftype", { "quickfix", "nofile", "help" }) 29 | set_option("lastplace_ignore_filetype", { "gitcommit", "gitrebase", "svn", "hgcommit" }) 30 | set_option("lastplace_open_folds", 1) 31 | if vim.fn.has("nvim-0.7") then 32 | local group_name = "NvimLastplace" 33 | vim.api.nvim_create_augroup(group_name, { clear = true }) 34 | vim.api.nvim_create_autocmd("BufRead", { 35 | group = group_name, 36 | callback = function(opts) 37 | vim.api.nvim_create_autocmd("BufWinEnter", { 38 | group = group_name, 39 | buffer = opts.buf, 40 | callback = function() 41 | lastplace.lastplace_ft(opts.buf) 42 | end, 43 | }) 44 | end, 45 | }) 46 | else 47 | vim.cmd([[augroup NvimLastplace]]) 48 | vim.cmd([[ autocmd!]]) 49 | vim.cmd([[ autocmd BufWinEnter * lua require('nvim-lastplace').lastplace_buf()]]) 50 | if not vim.fn.has("nvim-0.5.1") then 51 | vim.cmd([[ autocmd FileType * lua require('nvim-lastplace').lastplace_ft()]]) 52 | end 53 | vim.cmd([[augroup end]]) 54 | end 55 | end 56 | 57 | local set_cursor_position = function() 58 | local last_line = fn.line([['"]]) 59 | local buff_last_line = fn.line("$") 60 | local window_last_line = fn.line("w$") 61 | local window_first_line = fn.line("w0") 62 | -- If the last line is set and the less than the last line in the buffer 63 | if last_line > 0 and last_line <= buff_last_line then 64 | -- Check if the last line of the buffer is the same as the window 65 | if window_last_line == buff_last_line then 66 | -- Set line to last line edited 67 | vim.api.nvim_command([[keepjumps normal! g`"]]) 68 | -- Try to center 69 | elseif buff_last_line - last_line > ((window_last_line - window_first_line) / 2) - 1 then 70 | vim.api.nvim_command([[keepjumps normal! g`"zz]]) 71 | else 72 | vim.api.nvim_command([[keepjumps normal! G'"]]) 73 | end 74 | end 75 | if fn.foldclosed(".") ~= -1 and lastplace.options.lastplace_open_folds == 1 then 76 | vim.api.nvim_command([[normal! zvzz]]) 77 | end 78 | end 79 | 80 | function lastplace.lastplace_buf() 81 | -- Check if the buffer should be ignored 82 | if vim.tbl_contains(lastplace.options.lastplace_ignore_buftype, vim.api.nvim_buf_get_option(0, "buftype")) then 83 | return 84 | end 85 | 86 | if vim.fn.has("nvim-0.5.1") then 87 | -- Check if the filetype should be ignored 88 | if 89 | vim.tbl_contains(lastplace.options.lastplace_ignore_filetype, vim.api.nvim_buf_get_option(0, "filetype")) 90 | then 91 | -- reset cursor to first line 92 | vim.api.nvim_command([[normal! gg]]) 93 | return 94 | end 95 | end 96 | 97 | -- If a line has already been specified on the command line, we are done 98 | -- nvim file +num 99 | if fn.line(".") > 1 then 100 | return 101 | end 102 | 103 | set_cursor_position() 104 | end 105 | 106 | function lastplace.lastplace_ft(buffer) 107 | -- Check if the buffer should be ignored 108 | if vim.tbl_contains(lastplace.options.lastplace_ignore_buftype, vim.api.nvim_buf_get_option(buffer, "buftype")) then 109 | return 110 | end 111 | 112 | -- Check if the filetype should be ignored 113 | if 114 | vim.tbl_contains(lastplace.options.lastplace_ignore_filetype, vim.api.nvim_buf_get_option(buffer, "filetype")) 115 | then 116 | -- reset cursor to first line 117 | vim.api.nvim_command([[normal! gg]]) 118 | return 119 | end 120 | 121 | -- If a line has already been set by the BufReadPost event or on the command 122 | -- line, we are done. 123 | if fn.line(".") > 1 then 124 | return 125 | end 126 | 127 | -- This shouldn't be reached but, better have it ;-) 128 | set_cursor_position() 129 | end 130 | 131 | return lastplace 132 | --------------------------------------------------------------------------------