caio.co/de/dotfiles


Move home_cache_dir helper to custom mod by Caio a year ago (log)

Blob config/nvim/lua/custom/init.lua

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
local M = {}

-- If the current file has been opened before, try to jump to the
-- last cursor position the editor was at. If there are fewer
-- lines than remembered, it jumps to the end instead
function M.jump_to_last_position()
    local previous_position = vim.fn.line("'\"")

    if previous_position > 0 then
        local max_line_nr = vim.fn.line("$")

        if previous_position < max_line_nr then
            vim.cmd("normal '\"")
        else
            vim.cmd("normal $")
        end
    end
end

-- It's very inconvenient to use the ^ motion (first non blank)
-- in a intl keyboard with dead keys. This functions acts as
-- a "smart" wrapper that flip-flops between ^ or 0, depending
-- on the cursor position.
function M.caret_or_zero()
    -- select what's between the cursor and the beginning of the line
    local linepart = vim.fn.strpart(vim.fn.getline('.'), -1, vim.fn.col('.'))

    -- Are there only whitespace characters in it?
    if string.match(linepart, "^%s+$") then
        vim.cmd("normal! 0")
    else
        vim.cmd("normal! ^")
    end
end

function M.home_cache_dir(tail)
  local base = vim.env.XDG_CACHE_HOME
  if not base and vim.env.HOME then
    base = vim.env.HOME .. "/.cache"
  end
  -- fallback to a hidden dir on cwd
  if not base then
    return '.' .. tail
  end

  return base .. '/' .. tail
end


return M