Commit
Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: a71fc84a339bfed9cb55496751e4984ed7624894
Timestamp: Thu, 15 Jul 2021 22:51:31 +0000 (3 years ago)

+213 -0 +/-3 browse
add basic neovim support
1diff --git a/nvim/doc/note.txt b/nvim/doc/note.txt
2new file mode 100644
3index 0000000..439b538
4--- /dev/null
5+++ b/nvim/doc/note.txt
6 @@ -0,0 +1,13 @@
7+ ================================================================================
8+ *alpha.nvim*
9+
10+ note.nvim is a neovim plugin for Note.
11+
12+ Source code is available at:
13+ https://github.com/kevinschoon/note/nvim
14+
15+ :h NoteDoStuff
16+
17+ Note *NoteDoStuff*
18+ List all Notes
19+ ================================================================================
20 diff --git a/nvim/lua/note/init.lua b/nvim/lua/note/init.lua
21new file mode 100644
22index 0000000..b0f2080
23--- /dev/null
24+++ b/nvim/lua/note/init.lua
25 @@ -0,0 +1,188 @@
26+ -- taken from this blog post:
27+ -- TODO TODO
28+ -- https://www.2n.pl/blog/how-to-write-neovim-plugins-in-lua
29+
30+ local api = vim.api
31+ local buf, win
32+ local position = 0
33+
34+ local function center(str)
35+ local width = api.nvim_win_get_width(0)
36+ local shift = math.floor(width / 2) - math.floor(string.len(str) / 2)
37+ return string.rep(' ', shift) .. str
38+ end
39+
40+ local function open_window()
41+ buf = vim.api.nvim_create_buf(false, true)
42+ local border_buf = vim.api.nvim_create_buf(false, true)
43+
44+ vim.api.nvim_buf_set_option(buf, 'bufhidden', 'wipe')
45+ vim.api.nvim_buf_set_option(buf, 'filetype', 'note')
46+
47+ local width = vim.api.nvim_get_option("columns")
48+ local height = vim.api.nvim_get_option("lines")
49+
50+ local win_height = math.ceil(height * 0.8 - 4)
51+ local win_width = math.ceil(width * 0.8)
52+ local row = math.ceil((height - win_height) / 2 - 1)
53+ local col = math.ceil((width - win_width) / 2)
54+
55+ local border_opts = {
56+ style = "minimal",
57+ relative = "editor",
58+ width = win_width + 2,
59+ height = win_height + 2,
60+ row = row - 1,
61+ col = col - 1
62+ }
63+
64+ local opts = {
65+ style = "minimal",
66+ relative = "editor",
67+ width = win_width,
68+ height = win_height,
69+ row = row,
70+ col = col
71+ }
72+
73+ local border_lines = { '╔' .. string.rep('═', win_width) .. '╗' }
74+ local middle_line = '║' .. string.rep(' ', win_width) .. '║'
75+ for i=1, win_height do
76+ table.insert(border_lines, middle_line)
77+ end
78+ table.insert(border_lines, '╚' .. string.rep('═', win_width) .. '╝')
79+ vim.api.nvim_buf_set_lines(border_buf, 0, -1, false, border_lines)
80+
81+ local border_win = vim.api.nvim_open_win(border_buf, true, border_opts)
82+ win = api.nvim_open_win(buf, true, opts)
83+ api.nvim_command('au BufWipeout <buffer> exe "silent bwipeout! "'..border_buf)
84+
85+ vim.api.nvim_win_set_option(win, 'cursorline', true)
86+
87+ api.nvim_buf_set_lines(buf, 0, -1, false, { center('My Notes'), '', ''})
88+ api.nvim_buf_add_highlight(buf, -1, 'WhidHeader', 0, 0, -1)
89+ end
90+
91+ local function update_view(direction)
92+ vim.api.nvim_buf_set_option(buf, 'modifiable', true)
93+ position = position + direction
94+ if position < 0 then position = 0 end
95+
96+ local result = vim.api.nvim_call_function('systemlist', {
97+ 'note ls -style simple'
98+ })
99+
100+ if #result == 0 then table.insert(result, '') end
101+ for k,v in pairs(result) do
102+ result[k] = ' '..result[k]
103+ end
104+
105+ api.nvim_buf_set_lines(buf, 1, 2, false, {center('notes'..position)})
106+ api.nvim_buf_set_lines(buf, 3, -1, false, result)
107+
108+ api.nvim_buf_add_highlight(buf, -1, 'whidSubHeader', 1, 0, -1)
109+ vim.api.nvim_buf_set_option(buf, 'modifiable', false)
110+ end
111+
112+ local function close_window()
113+ --api.nvim_win_close(win, true)
114+ end
115+
116+ local function open_file()
117+ --local str = api.nvim_get_current_line()
118+ --close_window()
119+ --api.nvim_command('edit '..str)
120+ end
121+
122+ local function move_cursor()
123+ local new_pos = math.max(4, api.nvim_win_get_cursor(win)[1] - 1)
124+ api.nvim_win_set_cursor(win, {new_pos, 0})
125+ end
126+
127+ local function set_mappings()
128+ local mappings = {
129+ ['['] = 'update_view(-1)',
130+ [']'] = 'update_view(1)',
131+ ['<cr>'] = 'open_file()',
132+ h = 'update_view(-1)',
133+ l = 'update_view(1)',
134+ q = 'close_window()',
135+ k = 'move_cursor()'
136+ }
137+
138+ for k,v in pairs(mappings) do
139+ api.nvim_buf_set_keymap(buf, 'n', k, ':lua require"note".'..v..'<cr>', {
140+ nowait = true, noremap = true, silent = true
141+ })
142+ end
143+ local other_chars = {
144+ 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'i', 'n', 'o', 'p', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
145+ }
146+ for k,v in ipairs(other_chars) do
147+ api.nvim_buf_set_keymap(buf, 'n', v, '', { nowait = true, noremap = true, silent = true })
148+ api.nvim_buf_set_keymap(buf, 'n', v:upper(), '', { nowait = true, noremap = true, silent = true })
149+ api.nvim_buf_set_keymap(buf, 'n', '<c-'..v..'>', '', { nowait = true, noremap = true, silent = true })
150+ end
151+ end
152+
153+ local function Note()
154+ position = 0
155+ open_window()
156+ set_mappings()
157+ update_view(0)
158+ api.nvim_win_set_cursor(win, {4, 0})
159+ end
160+
161+ return {
162+ note = Note,
163+ update_view = update_view,
164+ open_file = open_file,
165+ move_cursor = move_cursor,
166+ close_window = close_window
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 diff --git a/nvim/plugin/note.vim b/nvim/plugin/note.vim
215new file mode 100644
216index 0000000..98581ba
217--- /dev/null
218+++ b/nvim/plugin/note.vim
219 @@ -0,0 +1,12 @@
220+ if exists('g:loaded_note') | finish | endif " prevent loading file twice
221+
222+ let s:save_cpo = &cpo " save user coptions
223+ set cpo&vim " reset them to defaults
224+
225+ " command to run our plugin
226+ command! Note lua require("note").note()
227+
228+ let &cpo = s:save_cpo " and restore after
229+ unlet s:save_cpo
230+
231+ let g:loaded_note = 1