Merge pull request #1 from Conni2461/no_more_globals

fix: no more globals
This commit is contained in:
Rene Schallner
2021-11-24 12:18:31 +01:00
committed by GitHub
2 changed files with 61 additions and 74 deletions

View File

@@ -2,9 +2,9 @@
A Neovim (lua) plugin for working with a text-based, markdown [zettelkasten](https://takesmartnotes.com/) / Wiki and mixing it with a journal, based on [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim). A Neovim (lua) plugin for working with a text-based, markdown [zettelkasten](https://takesmartnotes.com/) / Wiki and mixing it with a journal, based on [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim).
Find notes by name, daily and weekly notes by date, search within all notes, place and follow links to your notes or create new ones, with templates. Current daily and weekly notes are (optionally) created if not present when searching for dailies or weeklies. Following a link to a non-existing note can also create the missing note (optional). Find notes by name, daily and weekly notes by date, search within all notes, place and follow links to your notes or create new ones, with templates. Current daily and weekly notes are (optionally) created if not present when searching for dailies or weeklies. Following a link to a non-existing note can also create the missing note (optional).
Telekasten.nvim can optionally plug into [calendar-vim](https://github.com/mattn/calendar-vim): Selecting a day in the calendar will open up a telescope search with preview that lets you open the daily note (or cancel out and keep browsing your calendar). The daily note will be created if it doesn't exist. Days with daily notes get marked in the calendar. Telekasten.nvim can optionally plug into [calendar-vim](https://github.com/mattn/calendar-vim): Selecting a day in the calendar will open up a telescope search with preview that lets you open the daily note (or cancel out and keep browsing your calendar). The daily note will be created if it doesn't exist. Days with daily notes get marked in the calendar.
After having written the infamous [sublime_zk](https://github.com/renerocksai/sublime_zk) for SublimeText, having moved on to my standalone [sublimeless_zk](https://github.com/renerocksai/sublimeless_zk), having tried [Roam Research](https://roamresearch.com) and [Obsidian.md](https://obsidian.md) (which I still use sparingly), I have eventually arrived back at the editor I feel at home the most: Neovim 😄! I can literally **live** inside of nvim now, not only for writing code. After having written the infamous [sublime_zk](https://github.com/renerocksai/sublime_zk) for SublimeText, having moved on to my standalone [sublimeless_zk](https://github.com/renerocksai/sublimeless_zk), having tried [Roam Research](https://roamresearch.com) and [Obsidian.md](https://obsidian.md) (which I still use sparingly), I have eventually arrived back at the editor I feel at home the most: Neovim 😄! I can literally **live** inside of nvim now, not only for writing code.
@@ -18,7 +18,7 @@ This is the result of my first days of hacking neovim with lua:
## Search-based navigation ## Search-based navigation
Every navigation action, like following a link, is centered around a Telescope search: a Telescope search popup is opened, and in the case of following a link, the search-text is pre-filled with the target. So, instead of opening the linked note, you get a preview in Telescope and can decide if you actually want to go there. Since the search is often likely to show up more than one result, you can preview related notes immediately. Every navigation action, like following a link, is centered around a Telescope search: a Telescope search popup is opened, and in the case of following a link, the search-text is pre-filled with the target. So, instead of opening the linked note, you get a preview in Telescope and can decide if you actually want to go there. Since the search is often likely to show up more than one result, you can preview related notes immediately.
### The preview is a powerful feature ### The preview is a powerful feature
Leaving the opening of the note to Telescope, you can decide with one keypress whether you want to open the note in a split or in the current window - or if you've seen enough. Leaving the opening of the note to Telescope, you can decide with one keypress whether you want to open the note in a split or in the current window - or if you've seen enough.
@@ -28,7 +28,7 @@ I find that pressing the enter key to confirm the search does not interrupt my f
## Install and setup ## Install and setup
### 0. Prerequisites ### 0. Prerequisites
#### Telescope #### Telescope
Since this plugin uses [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim), you need to install it first. Since this plugin uses [telescope.nvim](https://github.com/nvim-telescope/telescope.nvim), you need to install it first.
@@ -36,7 +36,7 @@ Since this plugin uses [telescope.nvim](https://github.com/nvim-telescope/telesc
[Neovim (v0.5.1)](https://github.com/neovim/neovim/releases/tag/v0.5.1) or the latest neovim nighly commit is required for `telescope.nvim` to work. [Neovim (v0.5.1)](https://github.com/neovim/neovim/releases/tag/v0.5.1) or the latest neovim nighly commit is required for `telescope.nvim` to work.
#### Ripgrep #### Ripgrep
For proper sort order of daily notes, the `rg` executable ([Ripgrep](https://github.com/BurntSushi/ripgrep)) is required and needs to be installed so that nvim can find it. So make sure it's in your path. For proper sort order of daily notes, the `rg` executable ([Ripgrep](https://github.com/BurntSushi/ripgrep)) is required and needs to be installed so that nvim can find it. So make sure it's in your path.
If rg isn't found at `setup()` time, it will not be used. In that case, the sort order of daily and weekly notes are likely to be reversed or total garbage. I do accept pull requests, though, for a lua implementation 😁! If rg isn't found at `setup()` time, it will not be used. In that case, the sort order of daily and weekly notes are likely to be reversed or total garbage. I do accept pull requests, though, for a lua implementation 😁!
@@ -44,7 +44,7 @@ If you can't use `rg`, I recommend using `goto_today()` and `goto_thisweek()` in
#### calendar-vim Plugin (optional) #### calendar-vim Plugin (optional)
Telekasten.nvim can optionally plug into [calendar-vim](https://github.com/mattn/calendar-vim): Selecting a day in the calendar will open up a telescope search with preview that lets you open the daily note (or cancel out). The daily note will be created if it doesn't exist. Days with daily notes get marked in the calendar. Telekasten.nvim can optionally plug into [calendar-vim](https://github.com/mattn/calendar-vim): Selecting a day in the calendar will open up a telescope search with preview that lets you open the daily note (or cancel out). The daily note will be created if it doesn't exist. Days with daily notes get marked in the calendar.
See below for installing and using it. See below for installing and using it.
@@ -70,7 +70,7 @@ Somewhere in your vim config, put a snippet like this:
lua << END lua << END
local home = vim.fn.expand("~/zettelkasten") local home = vim.fn.expand("~/zettelkasten")
require('telekasten').setup({ require('telekasten').setup({
home = home home = home,
dailies = home .. '/' .. 'daily', dailies = home .. '/' .. 'daily',
weeklies = home .. '/' .. 'weekly', weeklies = home .. '/' .. 'weekly',
templates = home .. '/' .. 'templates', templates = home .. '/' .. 'templates',
@@ -104,7 +104,7 @@ require('telekasten').setup({
END END
``` ```
| setting | description | example | | setting | description | example |
| --- | --- | --- | | --- | --- | --- |
| `home` | path to your zettelkasten folder (folder with markdown files) | ~/zettelkasten | | `home` | path to your zettelkasten folder (folder with markdown files) | ~/zettelkasten |
| `dailies` | path where your daily notes go | ~/zettelkasten/daily | | `dailies` | path where your daily notes go | ~/zettelkasten/daily |
| `weeklies` | path where your weekly notes go | ~/zettelkasten/weekly | | `weeklies` | path where your weekly notes go | ~/zettelkasten/weekly |
@@ -113,27 +113,27 @@ END
| `follow_creates_nonexisting` | following a link to a non-existing note will create it | true | | `follow_creates_nonexisting` | following a link to a non-existing note will create it | true |
| `dailies_create_nonexisting` | following a link to a non-existing daily note will create it | true | | `dailies_create_nonexisting` | following a link to a non-existing daily note will create it | true |
| `weekly_create_nonexisting` | following a link to a non-existing weekly note will create it | true | | `weekly_create_nonexisting` | following a link to a non-existing weekly note will create it | true |
| `template_new_note` | markdown template for new notes | ~/zettelkasten/templates/new_note.md | | `template_new_note` | markdown template for new notes | ~/zettelkasten/templates/new_note.md |
| `template_new_daily` | markdown template for new daily notes | ~/zettelkasten/templates/daily.md | | `template_new_daily` | markdown template for new daily notes | ~/zettelkasten/templates/daily.md |
| `template_new_weekly` | markdown template for new weekly notes | ~/zettelkasten/templates/weekly.md | | `template_new_weekly` | markdown template for new weekly notes | ~/zettelkasten/templates/weekly.md |
| `plug_into_calendar` | activate calendar support if true (needs calendar-vim plugin) | true | | `plug_into_calendar` | activate calendar support if true (needs calendar-vim plugin) | true |
| `calendar_opts` | options for calendar, see below | see below | | `calendar_opts` | options for calendar, see below | see below |
The calendar support has its own options, contained in `calendar_opts`: The calendar support has its own options, contained in `calendar_opts`:
| calendar setting | description | example | | calendar setting | description | example |
| --- | --- | --- | | --- | --- | --- |
| `weeknm` | calendar week display mode | 1 | | `weeknm` | calendar week display mode | 1 |
| | 1 .. 'WK01' | | | | 1 .. 'WK01' | |
| | 2 .. 'WK 1' | | | | 2 .. 'WK 1' | |
| | 3 .. 'KW01' | | | | 3 .. 'KW01' | |
| | 4 .. 'KW 1' | | | | 4 .. 'KW 1' | |
| | 5 .. '1' | | | | 5 .. '1' | |
| `calendar_monday` | use monday as start of week if 1 | 1 | | `calendar_monday` | use monday as start of week if 1 | 1 |
| `calendar_mark` | where to put marks to mark days with daily notes | 'left-fit' | | `calendar_mark` | where to put marks to mark days with daily notes | 'left-fit' |
| | 'left' : ugly | | | | 'left' : ugly | |
| | 'left-fit' : mark to the left of the day| | | | 'left-fit' : mark to the left of the day| |
| | 'right' : mark to the right of the day| | | | 'right' : mark to the right of the day| |
### 3. Configure your own colors ### 3. Configure your own colors
@@ -157,7 +157,7 @@ hi tkBrackets ctermfg=gray
hi tkLink ctermfg=72 cterm=bold,underline hi tkLink ctermfg=72 cterm=bold,underline
hi tkBrackets ctermfg=gray hi tkBrackets ctermfg=gray
" highlight ==highlighted== text " highlight ==highlighted== text
hi tkHighlight ctermbg=yellow ctermfg=darkred cterm=bold hi tkHighlight ctermbg=yellow ctermfg=darkred cterm=bold
``` ```
@@ -179,7 +179,7 @@ The plugin defines the following functions.
- `show_calendar()` : opens up the calendar in a properly-sized vertical split at the very right - `show_calendar()` : opens up the calendar in a properly-sized vertical split at the very right
- `setup(opts)`: used for configuring paths, file extension, etc. - `setup(opts)`: used for configuring paths, file extension, etc.
To use one of the functions above, just run them with the `:lua ...` command. To use one of the functions above, just run them with the `:lua ...` command.
```vim ```vim
:lua require("telekasten").find_daily_notes() :lua require("telekasten").find_daily_notes()
@@ -266,7 +266,7 @@ When invoking `show_calendar()`, a calendar showing the previous, current, and n
- pressing enter on a day will open up a telescope finder with the associated daily note selected and previewed. The daily note will be created if it doesn't exist. If you choose to not open the note, you will return to the calender so you can preview other notes. - pressing enter on a day will open up a telescope finder with the associated daily note selected and previewed. The daily note will be created if it doesn't exist. If you choose to not open the note, you will return to the calender so you can preview other notes.
## Bind it ## Bind it
Usually, you would set up some key bindings, though: Usually, you would set up some key bindings, though:
```vim ```vim
@@ -287,20 +287,19 @@ nnoremap <leader>zc :lua require('telekasten').show_calendar()<CR>
inoremap <leader>[ <ESC>:lua require('telekasten').insert_link()<CR> inoremap <leader>[ <ESC>:lua require('telekasten').insert_link()<CR>
" ----- the following are for syntax-coloring [[links]] and ==highlighted text== " ----- the following are for syntax-coloring [[links]] and ==highlighted text==
" ----- (see the section about coloring in README.md) " ----- (see the section about coloring in README.md)
" for gruvbox " for gruvbox
hi tkLink ctermfg=72 cterm=bold,underline hi tkLink ctermfg=72 cterm=bold,underline
hi tkBrackets ctermfg=gray hi tkBrackets ctermfg=gray
" highlight ==highlighted== text " highlight ==highlighted== text
hi tkHighlight ctermbg=yellow ctermfg=darkred cterm=bold hi tkHighlight ctermbg=yellow ctermfg=darkred cterm=bold
``` ```
## The hardcoded stuff ## The hardcoded stuff
Currently, the following things are hardcoded: Currently, the following things are hardcoded:
- the file naming format for daily note files: `YYYY-MM-DD.ext` (e.g. `2021-11-21.md`) - the file naming format for daily note files: `YYYY-MM-DD.ext` (e.g. `2021-11-21.md`)
- the file naming format for weekly note files: `YYYY-Www.ext` (e.g. `2021-W46.md`) - the file naming format for weekly note files: `YYYY-Www.ext` (e.g. `2021-W46.md`)

View File

@@ -1,5 +1,6 @@
local builtin = require "telescope.builtin" local builtin = require "telescope.builtin"
local actions = require("telescope.actions") local action_state = require "telescope.actions.state" local actions = require("telescope.actions")
local action_state = require "telescope.actions.state"
-- declare locals for the nvim api stuff to avoid more lsp warnings -- declare locals for the nvim api stuff to avoid more lsp warnings
local vim = vim local vim = vim
@@ -10,7 +11,7 @@ local vim = vim
-- ---------------------------------------------------------------------------- -- ----------------------------------------------------------------------------
local home = vim.fn.expand("~/zettelkasten") local home = vim.fn.expand("~/zettelkasten")
ZkCfg = { local ZkCfg = {
home = home, home = home,
dailies = home .. '/' .. 'daily', dailies = home .. '/' .. 'daily',
weeklies = home .. '/' .. 'weekly', weeklies = home .. '/' .. 'weekly',
@@ -123,7 +124,7 @@ end
-- --
-- Select from daily notes -- Select from daily notes
-- --
FindDailyNotes = function(opts) local FindDailyNotes = function(opts)
opts = opts or {} opts = opts or {}
local today = os.date("%Y-%m-%d") local today = os.date("%Y-%m-%d")
@@ -147,7 +148,7 @@ end
-- --
-- Select from daily notes -- Select from daily notes
-- --
FindWeeklyNotes = function(opts) local FindWeeklyNotes = function(opts)
opts = opts or {} opts = opts or {}
local title = os.date("%Y-W%V") local title = os.date("%Y-W%V")
@@ -161,7 +162,7 @@ FindWeeklyNotes = function(opts)
prompt_title = "Find weekly note", prompt_title = "Find weekly note",
cwd = ZkCfg.weeklies, cwd = ZkCfg.weeklies,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
@@ -171,13 +172,11 @@ end
-- --
-- Select from all notes and put a link in the current buffer -- Select from all notes and put a link in the current buffer
-- --
InsertLink = function(opts) local InsertLink = function(_)
opts = opts or {}
builtin.find_files({ builtin.find_files({
prompt_title = "Insert link to note", prompt_title = "Insert link to note",
cwd = ZkCfg.home, cwd = ZkCfg.home,
attach_mappings = function(prompt_bufnr, map) attach_mappings = function(prompt_bufnr, _)
map = map -- get rid of lsp error
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local selection = action_state.get_selected_entry() local selection = action_state.get_selected_entry()
@@ -187,7 +186,7 @@ InsertLink = function(opts)
return true return true
end, end,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
@@ -197,7 +196,7 @@ end
-- --
-- find the file linked to by the word under the cursor -- find the file linked to by the word under the cursor
-- --
FollowLink = function(opts) local FollowLink = function(opts)
opts = opts or {} opts = opts or {}
vim.cmd('normal yi]') vim.cmd('normal yi]')
local title = vim.fn.getreg('"0') local title = vim.fn.getreg('"0')
@@ -227,7 +226,7 @@ end
-- --
-- Create and yank a [[link]] from the current note. -- Create and yank a [[link]] from the current note.
-- --
YankLink = function() local YankLink = function()
local title = '[[' .. path_to_linkname(vim.fn.expand('%')) .. ']]' local title = '[[' .. path_to_linkname(vim.fn.expand('%')) .. ']]'
vim.fn.setreg('"', title) vim.fn.setreg('"', title)
print('yanked ' .. title) print('yanked ' .. title)
@@ -240,9 +239,7 @@ end
-- --
-- find today's daily note and create it if necessary. -- find today's daily note and create it if necessary.
-- --
local GotoToday = function(opts)
GotoToday = function(opts)
opts = opts or calenderinfo_today() opts = opts or calenderinfo_today()
local word = opts.date or os.date("%Y-%m-%d") local word = opts.date or os.date("%Y-%m-%d")
@@ -257,8 +254,7 @@ GotoToday = function(opts)
cwd = ZkCfg.home, cwd = ZkCfg.home,
default_text = word, default_text = word,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
attach_mappings = function(prompt_bufnr, map) attach_mappings = function(prompt_bufnr, _)
map = map -- get rid of lsp error
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
@@ -280,13 +276,12 @@ end
-- --
-- Select from notes -- Select from notes
-- --
FindNotes = function(opts) local FindNotes = function(_)
opts = opts or {}
builtin.find_files({ builtin.find_files({
prompt_title = "Find notes by name", prompt_title = "Find notes by name",
cwd = ZkCfg.home, cwd = ZkCfg.home,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
}) })
end end
@@ -296,9 +291,7 @@ end
-- --
-- find the file linked to by the word under the cursor -- find the file linked to by the word under the cursor
-- --
SearchNotes = function(opts) local SearchNotes = function(_)
opts = opts or {}
builtin.live_grep({ builtin.live_grep({
prompt_title = "Search in notes", prompt_title = "Search in notes",
cwd = ZkCfg.home, cwd = ZkCfg.home,
@@ -332,8 +325,7 @@ local function on_create(title)
}) })
end end
CreateNote = function(opts) local CreateNote = function(_)
opts = opts or {}
vim.ui.input({prompt = 'Title: '}, on_create) vim.ui.input({prompt = 'Title: '}, on_create)
end end
@@ -360,8 +352,7 @@ local function on_create_with_template(title)
prompt_title = "Select template...", prompt_title = "Select template...",
cwd = ZkCfg.templates, cwd = ZkCfg.templates,
find_command = ZkCfg.find_command, find_command = ZkCfg.find_command,
attach_mappings = function(prompt_bufnr, map) attach_mappings = function(prompt_bufnr, _)
map = map -- get rid of lsp error
actions.select_default:replace(function() actions.select_default:replace(function()
actions.close(prompt_bufnr) actions.close(prompt_bufnr)
local template = ZkCfg.templates .. '/' .. action_state.get_selected_entry().value local template = ZkCfg.templates .. '/' .. action_state.get_selected_entry().value
@@ -374,8 +365,7 @@ local function on_create_with_template(title)
}) })
end end
CreateNoteSelectTemplate = function(opts) local CreateNoteSelectTemplate = function(_)
opts = opts or {}
vim.ui.input({prompt = 'Title: '}, on_create_with_template) vim.ui.input({prompt = 'Title: '}, on_create_with_template)
end end
@@ -386,7 +376,7 @@ end
-- --
-- find this week's weekly note and create it if necessary. -- find this week's weekly note and create it if necessary.
-- --
GotoThisWeek = function(opts) local GotoThisWeek = function(opts)
opts = opts or {} opts = opts or {}
local title = os.date("%Y-W%V") local title = os.date("%Y-W%V")
@@ -411,7 +401,7 @@ end
-- -------------- -- --------------
-- return if a daily 'note exists' indicator (sign) should be displayed for a particular day -- return if a daily 'note exists' indicator (sign) should be displayed for a particular day
CalendarSignDay = function(day, month, year) local CalendarSignDay = function(day, month, year)
local fn = ZkCfg.dailies .. '/' .. string.format('%04d-%02d-%02d', year, month, day) .. ZkCfg.extension local fn = ZkCfg.dailies .. '/' .. string.format('%04d-%02d-%02d', year, month, day) .. ZkCfg.extension
if file_exists(fn) then if file_exists(fn) then
return 1 return 1
@@ -420,10 +410,7 @@ CalendarSignDay = function(day, month, year)
end end
-- action on enter on a specific day: preview in telescope, stay in calendar on cancel, open note in other window on accept -- action on enter on a specific day: preview in telescope, stay in calendar on cancel, open note in other window on accept
CalendarAction = function(day, month, year, weekday, dir) local CalendarAction = function(day, month, year, weekday, _)
-- lsp
dir = dir
local today = string.format('%04d-%02d-%02d', year, month, day) local today = string.format('%04d-%02d-%02d', year, month, day)
local opts = {} local opts = {}
opts.date = today opts.date = today
@@ -436,7 +423,7 @@ CalendarAction = function(day, month, year, weekday, dir)
GotoToday(opts) GotoToday(opts)
end end
ShowCalendar = function(opts) local ShowCalendar = function(opts)
local defaults = {} local defaults = {}
defaults.cmd = 'CalendarVR' defaults.cmd = 'CalendarVR'
defaults.vertical_resize = 1 defaults.vertical_resize = 1
@@ -449,22 +436,22 @@ ShowCalendar = function(opts)
end end
-- set up calendar integration: forward to our lua functions -- set up calendar integration: forward to our lua functions
SetupCalendar = function(opts) local SetupCalendar = function(opts)
local defaults = ZkCfg.calendar_opts local defaults = ZkCfg.calendar_opts
opts = opts or defaults opts = opts or defaults
local cmd = [[ local cmd = [[
function! MyCalSign(day, month, year) function! MyCalSign(day, month, year)
return luaeval('CalendarSignDay(_A[1], _A[2], _A[3])', [a:day, a:month, a:year]) return luaeval('require("telekasten").CalendarSignDay(_A[1], _A[2], _A[3])', [a:day, a:month, a:year])
endfunction endfunction
function! MyCalAction(day, month, year, weekday, dir) function! MyCalAction(day, month, year, weekday, dir)
" day : day " day : day
" month : month " month : month
" year year " year year
" weekday : day of week (monday=1) " weekday : day of week (monday=1)
" dir : direction of calendar " dir : direction of calendar
return luaeval('CalendarAction(_A[1], _A[2], _A[3], _A[4], _A[5])', [a:day, a:month, a:year, a:weekday, a:dir]) return luaeval('require("telekasten").CalendarAction(_A[1], _A[2], _A[3], _A[4], _A[5])', [a:day, a:month, a:year, a:weekday, a:dir])
endfunction endfunction
function! MyCalBegin() function! MyCalBegin()
@@ -492,7 +479,7 @@ end
-- --
-- Overrides config with elements from cfg. See top of file for defaults. -- Overrides config with elements from cfg. See top of file for defaults.
-- --
Setup = function(cfg) local Setup = function(cfg)
cfg = cfg or {} cfg = cfg or {}
for k, v in pairs(cfg) do for k, v in pairs(cfg) do
-- merge everything but calendar opts -- merge everything but calendar opts
@@ -533,6 +520,7 @@ local M = {
yank_notelink = YankLink, yank_notelink = YankLink,
create_note_sel_template = CreateNoteSelectTemplate, create_note_sel_template = CreateNoteSelectTemplate,
show_calendar = ShowCalendar, show_calendar = ShowCalendar,
CalendarSignDay = CalendarSignDay,
CalendarAction = CalendarAction,
} }
return M return M