~jhn/remember.nvim

ref: 80ad0e69ccc0239b0598a76b9aece13e7d696e34 remember.nvim/lua/remember.lua -rw-r--r-- 2.6 KiB
80ad0e69 — Johan Add metadata and fix typo 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
--
-- Author: johan@pwd.re
-- Version: 1.0.0
--
-- Based on https://github.com/farmergreg/vim-lastplace/
--
-- This work is licensed under the terms of the MIT license.
-- For a copy, see <https://opensource.org/licenses/MIT>.
--

local g   = vim.g
local bo  = vim.bo
local fn  = vim.fn
local api = vim.api

local M = {}

local config = {
  ignore_filetype = { "gitcommit", "gitrebase", "svn", "hgcommit" },
  ignore_buftype  = { "quickfix", "nofile", "help" },
  open_folds      = true
}

function M.setup(options)
  if options['ignore_filetype'] then
    config['ignore_filetype'] = options['ignore_filetype']
  end

  if options['ignore_buftype'] then
    config['ignore_buftype'] = options['ignore_buftype']
  end

  if options['open_folds'] then
    config['open_folds'] = options['open_folds']
  end
end

function set_cursor_position()
  -- Return if we have a buffer or filetype we want to ignore
  for _, k in pairs(config['ignore_buftype']) do
    if bo.buftype == k then
      return
    end
  end

  for _, k in pairs(config['ignore_filetype']) do
    if bo.filetype == k then
      return
    end
  end

  -- Return if the file doesn't exist, like a new and unsaved file
  if fn.empty(fn.glob(fn.expand('%'))) ~= 0 then
    return
  end

  local cursor_position = api.nvim_buf_get_mark(0, '"')
  local row = cursor_position[1]
  local col = cursor_position[2]

  -- If the saved row is less than the number of rows in the buffer,
  -- then continue
  if row > 0 and row <= api.nvim_buf_line_count(0) then

    -- If the last row is visible within this window, like in a very short
    -- file, just set the cursor position to the saved position
    if api.nvim_buf_line_count(0) == fn.line("w$") then
      api.nvim_win_set_cursor(0, cursor_position)

    -- If we're in the middle of the file, set the cursor position and center
    -- the screen
    elseif api.nvim_buf_line_count(0) - row > ((fn.line("w$") - fn.line("w0")) / 2) - 1 then
      api.nvim_win_set_cursor(0, cursor_position)
      api.nvim_input("zz")

    -- If we're at the end of the screen, set the cursor position and move
    -- the window up by one with C-e. This is to show that we are at the end
    -- of the file. If we did "zz" half the screen would be blank.
    else
      api.nvim_win_set_cursor(0, cursor_position)
      api.nvim_input("<c-e>")
    end
  end

  -- If the row is within a fold, make the row visible and recenter the screen
  if api.nvim_eval("foldclosed('.')") ~= -1 and config['open_folds'] then
    api.nvim_input("zvzz")
  end
end

api.nvim_exec([[
  augroup remember_plugin
    autocmd BufWinEnter * lua set_cursor_position()
  augroup END
]], false)

return M