From e2aa3c7a0038beea2f6a557b8f6246a416b788b7 Mon Sep 17 00:00:00 2001 From: Rene Schallner Date: Sat, 20 Nov 2021 20:02:58 +0100 Subject: [PATCH] initial commit --- lua/telekasten.lua | 252 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 lua/telekasten.lua diff --git a/lua/telekasten.lua b/lua/telekasten.lua new file mode 100644 index 0000000..bf40ea2 --- /dev/null +++ b/lua/telekasten.lua @@ -0,0 +1,252 @@ +local pickers = require "telescope.pickers" +local finders = require "telescope.finders" +local sorters = require "telescope.sorters" +local previewers = require "telescope.previewers" +local conf = require("telescope.config").values +local builtin = require "telescope.builtin" + +local actions = require("telescope.actions") +local action_state = require "telescope.actions.state" + +zkcfg = { + home = "/home/rs/zettelkasten", + dailies = "/home/rs/zettelkasten/daily", + extension = ".md", + zkfinder = "shitzk.sh", + } + + +local path_to_linkname = function(p) + local fn = vim.split(p, "/") + fn = fn[#fn] + fn = vim.split(fn, zkcfg.extension) + fn = fn[1] + return fn +end + +local zk_entry_maker = function(entry) + return { + value = entry, + display = path_to_linkname(entry), + ordinal = entry, + } +end + + + +-- +-- find_daily_notes: +-- +-- Select from daily notes +-- +find_daily_notes = function(opts) + builtin.find_files({ + prompt_title = "Find daily note", + cwd = zkcfg.dailies, + find_command = { zkcfg.zkfinder }, + entry_maker = zk_entry_maker, + }) +end + + + +-- +-- insert_link: +-- +-- Select from all notes and put a link in the current buffer +-- +insert_link = function(opts) + builtin.find_files({ + prompt_title = "Insert link to note", + cwd = zkcfg.home, + find_command = { zkcfg.zkfinder }, + attach_mappings = function(prompt_bufnr, map) + actions.select_default:replace(function() + actions.close(prompt_bufnr) + local selection = action_state.get_selected_entry() + local fn = path_to_linkname(selection.value) + vim.api.nvim_put({ "[["..fn.."]]" }, "", false, true) + end) + return true + end, + entry_maker = zk_entry_maker, + }) +end + +-- +-- follow_link: +-- +-- find the file linked to by the word under the cursor +-- +follow_link = function(opts) + builtin.find_files({ + prompt_title = "Follow link to note...", + cwd = zkcfg.home, + find_command = { zkcfg.zkfinder }, + default_text = vim.fn.expand(""), + entry_maker = zk_entry_maker, + }) +end + +-- +-- find_notes: +-- +-- Select from daily notes +-- +local find_notes = function(opts) + local find_command = opts.find_command + local hidden = opts.hidden + local no_ignore = opts.no_ignore + local follow = opts.follow + local search_dirs = opts.search_dirs + + if search_dirs then + for k, v in pairs(search_dirs) do + search_dirs[k] = vim.fn.expand(v) + end + end + + if not find_command then + if 1 == vim.fn.executable "fd" then + find_command = { "fd", "--type", "f" } + if hidden then + table.insert(find_command, "--hidden") + end + if no_ignore then + table.insert(find_command, "--no-ignore") + end + if follow then + table.insert(find_command, "-L") + end + if search_dirs then + table.insert(find_command, ".") + for _, v in pairs(search_dirs) do + table.insert(find_command, v) + end + end + elseif 1 == vim.fn.executable "fdfind" then + find_command = { "fdfind", "--type", "f" } + if hidden then + table.insert(find_command, "--hidden") + end + if no_ignore then + table.insert(find_command, "--no-ignore") + end + if follow then + table.insert(find_command, "-L") + end + if search_dirs then + table.insert(find_command, ".") + for _, v in pairs(search_dirs) do + table.insert(find_command, v) + end + end + elseif 1 == vim.fn.executable "rg" then + find_command = { "rg", "--files" } + if hidden then + table.insert(find_command, "--hidden") + end + if no_ignore then + table.insert(find_command, "--no-ignore") + end + if follow then + table.insert(find_command, "-L") + end + if search_dirs then + for _, v in pairs(search_dirs) do + table.insert(find_command, v) + end + end + elseif 1 == vim.fn.executable "find" and vim.fn.has "win32" == 0 then + find_command = { "find", ".", "-type", "f" } + if not hidden then + table.insert(find_command, { "-not", "-path", "*/.*" }) + find_command = flatten(find_command) + end + if no_ignore ~= nil then + log.warn "The `no_ignore` key is not available for the `find` command in `find_files`." + end + if follow then + table.insert(find_command, "-L") + end + if search_dirs then + table.remove(find_command, 2) + for _, v in pairs(search_dirs) do + table.insert(find_command, 2, v) + end + end + elseif 1 == vim.fn.executable "where" then + find_command = { "where", "/r", ".", "*" } + if hidden ~= nil then + log.warn "The `hidden` key is not available for the Windows `where` command in `find_files`." + end + if no_ignore ~= nil then + log.warn "The `no_ignore` key is not available for the Windows `where` command in `find_files`." + end + if follow ~= nil then + log.warn "The `follow` key is not available for the Windows `where` command in `find_files`." + end + if search_dirs ~= nil then + log.warn "The `search_dirs` key is not available for the Windows `where` command in `find_files`." + end + end + end + + if not find_command then + print( + "You need to install either find, fd, or rg. " + .. "You can also submit a PR to add support for another file finder :)" + ) + return + end + + if opts.cwd then + opts.cwd = vim.fn.expand(opts.cwd) + end + + opts.entry_maker = opts.entry_maker or make_entry.gen_from_file(opts) + + pickers.new(opts, { + prompt_title = "Find Files", + finder = finders.new_oneshot_job(find_command, opts), + previewer = conf.file_previewer(opts), + sorter = conf.file_sorter(opts), + }):find() +end + + +--[[ + function file_exists(name) + local f=io.open(name,"r") + if f~=nil then io.close(f) return true else return false end + end + + local api = vim.api + local M = {} + function M.currentEntry() + + -- set the filename based on the current date + local filepath = vim.g['wiki_root']..'journal/'..os.date('%Y-%m-%d')..'.md' + + -- if the file doesn't exist + -- then created file and write date to the top of the file + if not file_exists(filepath) then + file = io.open(filepath, 'a') + io.output(file) + io.write(os.date("# %a, %d %B '%y")) + io.close(file) + end + api.nvim_command('edit '..filepath) + end + return M +--]] + +local M = { + zkcfg = zkcfg, + find_daily_notes = find_daily_notes, + insert_link = insert_link, + follow_link = follow_link, + find_notes = find_filenames, +} +print("rene reloaded") +return M