]>
Commit | Line | Data |
---|---|---|
56292c79 RBR |
1 | ------------------------------------------------------------------------------- |
2 | -- Tools to deal with notes | |
3 | ------------------------------------------------------------------------------- | |
503d09fc RBR |
4 | local Notes = {} |
5 | ||
6 | local Util = require('util') | |
7 | local Configuration = require('configuration') | |
8 | local api = vim.api | |
9 | ------------------------------------------------------------------------------- | |
10 | -- Internal Functions | |
11 | ------------------------------------------------------------------------------- | |
12 | local function open_or_create_from_template(type, file_path) | |
13 | ||
14 | local journal_file = io.open(file_path, 'r') | |
15 | if not journal_file then | |
16 | local template_contents = Configuration.load_template(type) | |
17 | ||
18 | journal_file = io.open(file_path, 'w') | |
19 | journal_file:write(template_contents) | |
20 | journal_file:close() | |
21 | end | |
22 | vim.cmd('edit ' .. file_path) | |
23 | end | |
24 | ||
25 | local function open_periodic_note(type, filename) | |
26 | local file_directory_path = Configuration.path_for(Configuration.configuration.periodic_locations[type]) | |
27 | ||
28 | Util.ensure_directory_exists(file_directory_path) | |
29 | local file_path = Util.join(file_directory_path, filename) | |
30 | ||
31 | open_or_create_from_template(type, file_path) | |
32 | end | |
33 | ||
56292c79 RBR |
34 | ------------------------------------------------------------------------------- |
35 | -- Public Interface | |
36 | ------------------------------------------------------------------------------- | |
37 | ||
38 | --- Opens the daily note | |
2cc29448 | 39 | function Notes.open_daily(date) |
9e9e89d8 RBR |
40 | if not date or date == '' then |
41 | date = os.date('%Y-%m-%d') | |
42 | end | |
2cc29448 | 43 | local filename = date .. '.md' |
503d09fc | 44 | open_periodic_note('daily', filename) |
56292c79 RBR |
45 | end |
46 | ||
47 | --- Opens the weekly note | |
503d09fc RBR |
48 | function Notes.open_weekly() |
49 | local filename = os.date('%Y-w%V') | |
50 | open_periodic_note('weekly', filename) | |
56292c79 RBR |
51 | end |
52 | ||
53 | --- Opens the monthly note | |
503d09fc RBR |
54 | function Notes.open_monthly() |
55 | local filename = os.date('%Y-%m') .. '.md' | |
56 | open_periodic_note('monthly', filename) | |
56292c79 RBR |
57 | end |
58 | ||
59 | --- Opens the seasonal note | |
503d09fc RBR |
60 | function Notes.open_seasonal() |
61 | local year = os.date('%Y') | |
62 | local month = tonumber(os.date('%m')) | |
63 | local season = math.ceil(month / 3) | |
64 | ||
65 | local filename = year .. '-s' .. season .. '.md' | |
66 | open_periodic_note('seasonal', filename) | |
56292c79 RBR |
67 | end |
68 | ||
69 | --- Opens the yearly note | |
503d09fc RBR |
70 | function Notes.open_yearly() |
71 | local filename = os.date('%Y') .. '.md' | |
72 | open_periodic_note('yearly', filename) | |
56292c79 RBR |
73 | end |
74 | ||
75 | --- Opens an arbitrary note | |
503d09fc RBR |
76 | function Notes.open() |
77 | local success, module = pcall(require, 'fzf-lua') | |
78 | if success then | |
79 | local notes_path = Configuration.path_for() | |
80 | Util.ensure_directory_exists(notes_path) | |
81 | module.files({ cwd = notes_path }) | |
82 | else | |
83 | api.nvim_err_writeln('This feature requires optional dependency fzf-lua') | |
84 | end | |
56292c79 RBR |
85 | end |
86 | ||
503d09fc | 87 | return Notes |