aboutsummaryrefslogtreecommitdiff
path: root/lua/startup.lua
blob: 39cf408d521c2e9acbc02a2a2fb54ec51c4b8f47 (plain)
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
local M = {}
local ns = vim.api.nvim_create_namespace "startup"
-- tables with tables: {line, align, cursor_should move on}
M.lines = {}
M.formatted_text = {}

local current_section = ""

local opts = { noremap = true, silent = true }
local settings = require "startup.config"

local utils = require "startup.utils"
local spaces = utils.spaces

local function create_mappings(mappings)
  vim.api.nvim_buf_set_keymap(
    0,
    "n",
    "<CR>",
    ":lua require'startup'.check_line()<CR>",
    opts
  )
  vim.api.nvim_buf_set_keymap(
    0,
    "n",
    "o",
    "<cmd>lua require('startup').open_file()<CR>",
    opts
  )
  vim.api.nvim_buf_set_keymap(
    0,
    "n",
    "<c-o>",
    "<cmd>lua require('startup').open_file_vsplit()<CR>",
    opts
  )
  if mappings ~= {} then
    for _, cmd in pairs(mappings) do
      vim.api.nvim_buf_set_keymap(
        0,
        "n",
        cmd[2],
        "<cmd>" .. cmd[1] .. "<CR>",
        opts
      )
    end
  end
end

function M.new_file()
  local name = vim.fn.input "Filename: > "
  vim.cmd("e " .. name)
end

local sections_with_mappings = {}

function M.check_line()
  local line = vim.api.nvim_get_current_line()
  for _, section in ipairs(sections_with_mappings) do
    for name, command in pairs(settings[section].content) do
      if line:match(name) then
        vim.cmd(command[1])
      end
    end
  end
end

function M.open_file()
  local line = vim.api.nvim_get_current_line()
  local filename = line
  print(filename)
  vim.cmd("e " .. filename)
end

function M.open_file_vsplit()
  local line = vim.api.nvim_get_current_line()
  local filename = line
  print(filename)
  vim.cmd("vsplit " .. filename)
end

local function align(dict, alignment)
  local margin_calculated = 0
  if settings[current_section].margin < 1 then
    margin_calculated = vim.o.columns * settings[current_section].margin
  else
    margin_calculated = settings[current_section].margin
  end
  local aligned = {}
  local max_len = utils.longest_line(dict)
  if alignment == "center" then
    local space_left = vim.o.columns - max_len
    for _, line in ipairs(dict) do
      table.insert(aligned, spaces(space_left / 2) .. line)
    end
  elseif alignment == "left" then
    for _, line in ipairs(dict) do
      table.insert(aligned, spaces(margin_calculated) .. line)
    end
  elseif alignment == "right" then
    for _, line in ipairs(dict) do
      table.insert(
        aligned,
        spaces(vim.o.columns - max_len - margin_calculated - 10) .. line
      )
    end
  end
  margin_calculated = 0
  return aligned
end

local count = 1
local function set_lines(len, text, alignment, hi, pass)
  vim.api.nvim_buf_set_lines(
    0,
    count,
    count + len,
    false,
    align(text, alignment)
  )
  vim.api.nvim_win_set_cursor(0, { count, 0 })
  if pass then
    vim.g.section_length = count
  end
  for i = count, count + len do
    vim.api.nvim_buf_add_highlight(0, ns, hi, i, 1, -1)
  end
  count = count + len
end

local function empty(amount)
  for _ = 1, amount, 1 do
    set_lines(1, { " " }, "center", "StartupTools")
    table.insert(M.lines, { " ", "center", false, "normal" })
  end
end

local function mapping_names(mappings)
  local mapnames = {}
  for name, cmd in pairs(mappings) do
    if settings.options.empty_lines_between_mappings then
      table.insert(mapnames, " ")
    end
    if settings.options.mapping_keys then
      table.insert(mapnames, name .. "  " .. cmd[2])
    else
      table.insert(mapnames, name)
    end
  end

  return mapnames
end

function M.display()
  vim.schedule(function()
    U.set_buf_options()
    local parts = { "header", "body", "footer" }
    for _, part in ipairs(parts) do
      current_section = part
      local options = settings[part]
      if options.highlight == "" then
        vim.cmd(
          "highlight Startup"
            .. part
            .. " guifg="
            .. options.default_color
            .. " guibg="
            .. settings.colors.background
        )
        options.highlight = "Startup" .. part
      end
      if options.type == "text" then
        for _, line in ipairs(options.content) do
          table.insert(
            M.lines,
            { line, options.align, false, options.highlight }
          )
        end
      elseif options.type == "mapping" then
        for _, line in ipairs(mapping_names(options.content)) do
          table.insert(
            M.lines,
            { line, options.align, true, options.highlight }
          )
        end
        table.insert(sections_with_mappings, part)
        create_mappings(options.content)
      elseif options.type == "oldfiles" then
        local old_files = utils.get_oldfiles(settings.options.oldfiles_amount)
        table.insert(M.lines, {
          "Use 'o' to open the file at the current line.",
          options.align,
          false,
          options.highlight,
        })
        table.insert(M.lines, {
          "ctrl+'o' to open the file in a split",
          options.align,
          false,
          options.highlight,
        })
        empty(1)
        for _, line in ipairs(old_files) do
          table.insert(
            M.lines,
            { line, options.align, true, options.highlight }
          )
        end
      end
      if part == "header" then
        empty(settings.options.padding.header_body)
      elseif part == "body" then
        empty(settings.options.padding.body_footer + 1)
      end
      create_mappings {}
      vim.cmd(options.command)
    end
    -- current_section = ""
    for _, line in ipairs(M.lines) do
      table.insert(M.formatted_text, align({ line[1] }, line[2])[1])
    end
    vim.api.nvim_buf_set_option(0, "modifiable", true)
    vim.api.nvim_buf_set_lines(0, 0, -1, true, {})
    vim.api.nvim_buf_set_lines(0, 0, -1, false, M.formatted_text)
    vim.cmd [[silent! %s/\s\+$//]] -- clear trailing whitespace
    for linenr, line in ipairs(M.lines) do
      vim.api.nvim_buf_add_highlight(0, ns, line[4], linenr - 1, 0, -1)
    end
    vim.api.nvim_buf_set_option(0, "modifiable", false)
    vim.api.nvim_win_set_cursor(0, { 1, 1 })
    vim.api.nvim_win_set_cursor(0, {
      #settings.header.content + settings.options.padding.header_body + 1,
      math.floor(vim.o.columns / 2),
    })
  end)
  -- print "lines:"
  -- dump(lines)
end

function M.setup(update)
  if vim.g.startup_nvim_loaded then
    return
  end
  vim.g.startup_nvim_loaded = true

  settings = vim.tbl_deep_extend("force", settings, update or {})
  vim.cmd [[
  autocmd VimEnter * lua if vim.fn.argc() == 0 then require"startup".display() end
  ]]
end

return M