aboutsummaryrefslogtreecommitdiff
path: root/config/nvim/lua/brain.lua
blob: 4dfd2314c204c8cc3a0ad742aef4bb8c7ff8ed4a (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
local brain_home = os.getenv('HOME') .. '/brain'
local templates_path = brain_home .. '/1 periodic/99 templates'

local function load_template(template_name)
  local template_path = brain_home .. templates_path .. '/' .. template_name

  local template_file = io.open(template_path, "r")
  if not template_file then
      print("Template file not found")
      return
  end
  local content = template_file:read("*a")
  template_file:close()
end

local function open_or_create_from_template(template_name, file)
  local journal_file = io.open(file, "r")
  if not journal_file then
    local template_contents = load_template(template_name)

    journal_file = io.open(file, "w")
    journal_file:write(template_contents)
    journal_file:close()
  end
  vim.cmd("edit " .. file)
end

function open_daily_note()
    local filename_format = os.date('%Y-%m-%d')
    local template_name = '001 daily.md'

    local daily_note = brain_home .. "/1 periodic/01 journal/" .. filename_format .. ".md"
    open_or_create_from_template(template_name, daily_note)
end

function open_weekly_note()
    local filename_format = os.date('%Y-w%V')
    local template_name = '002 weekly.md'

    local weekly_note = brain_home .. "/1 periodic/02 weeks/" .. filename_format .. ".md"
    open_or_create_from_template(template_name, weekly_note)
end

function toggle_task()
    local line_num = vim.api.nvim_win_get_cursor(0)[1]
    local line = vim.api.nvim_get_current_line()

    local unchecked_pattern = '^%s*%- %[ %]'
    local checked_pattern = '^%s*%- %[x%]'

    if line:match(unchecked_pattern) then
        line = line:gsub(unchecked_pattern, '- [x]', 1)
    elseif line:match(checked_pattern) then
        line = line:gsub(checked_pattern, '- [ ]', 1)
    end

    vim.api.nvim_buf_set_lines(0, line_num - 1, line_num, false, {line})
end

-- Keybinds
vim.api.nvim_set_keymap('n', '<leader>od', '<cmd>lua open_daily_note()<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>ow', '<cmd>lua open_weekly_note()<CR>', { noremap = true, silent = true })
vim.api.nvim_set_keymap('n', '<leader>t', '<cmd>lua toggle_task()<CR>', { noremap = true, silent = true })