michal-h21 / vim-zettel

VimWiki addon for managing notes according to Zettelkasten method
MIT License
555 stars 72 forks source link

Would it be possible to use Telescope rather than fzf? #124

Closed leandrochiarini closed 1 year ago

leandrochiarini commented 2 years ago

Hi there, I was wondering if can use Tejlescope.nvim rather that fzf for the ZettelSearchMap and so on? I am aware of the g:zettel_fzf_command variable. But if I understand correctly, this is only for changing the command used by fzf itself.

michal-h21 commented 2 years ago

I don't know much about telescope.nvim unfortunatelly. g:zettel_fzf_command just sets the grep like command, so it cannot be used to set a different search frontend. It would be quite a lot of work to add a support for a new tool like FZF, but I can add that code if somebody does it.

leandrochiarini commented 2 years ago

Thanks for the quick reply, I do not have much experience with vimscript nor lua, but I will take a try on this, if I manage to pull it off, I will open a pull request. Cheers!

brianfabre commented 1 year ago

Were you able to find a solution? I would also like to move to telescope but I like vim-zettel too much

michal-h21 commented 1 year ago

I weren't, unfortunately. Hopefully someone will take a look at this issue, because I don't think I will be able to do that in the near future.

brianfabre commented 1 year ago

I found a telescope plugin that completes file path based on any type of telescope search. After playing around with it, it seems like it would be a good alternative. Here is the link

michal-h21 commented 1 year ago

I've found that you can use the following command to search and open Vimwiki files:

  :lua require'telescope.builtin'.find_files {cwd=vim.fn["vimwiki#vars#get_wikilocal"]('path',0)} 

The number after path should be the wiki number, 0 is the default wiki.

I've also took a look at the developer guide, and it seems that it shouldn't be that hard to provide functionality provided by the FZF searcher - you needs only to create actions that will open wiki files, or insert a link.

Here is what I've come with:

local pickers = require "telescope.pickers"
local finders = require "telescope.finders"
local builtin = require "telescope.builtin"
local conf = require("telescope.config").values
local actions = require "telescope.actions"
local previewers = require "telescope.previewers"
local action_state = require "telescope.actions.state"
local Path = require("plenary.path")
-- our picker function: colors
--

local function get_files(dir)
  local files = {}
  local doc_path = Path:new(dir, "*.*")
  local maybe_doc = vim.split(vim.fn.glob(doc_path.filename), "\n")
  for _, filepath in ipairs(maybe_doc) do
    local file = Path:new(filepath)
    if file:is_file() then
      files[#files+1] = filepath
    end
  end
  return files
end

local telescope_zettel = function(opts)
  opts = opts or {}
  -- get the path to Zettelkasen 
  local dir = vim.g.zettel_dir
  if not dir then
    vim.fn["zettel#vimwiki#initialize_wiki_number"]()
    dir = vim.fn["vimwiki#vars#get_wikilocal"]('path')
  end
  if not dir then 
    echo("Cannot find your Zettelkasten. Is the g:zettel_dir variable set?")
    return false
  end
  print(dir)
  pickers.new(opts, {
    prompt_title = "Zettelkasten",
    -- finder = builtin.find_files ({cwd = dir}),
    finder = finders.new_table {
      results = get_files(dir)
    },
    sorter = conf.generic_sorter(opts),
    -- attach_mappings = function(prompt_bufnr, map)
    --   actions.select_default:replace(function()
    --     actions.close(prompt_bufnr)
    --     local selection = action_state.get_selected_entry()
    --     -- print(vim.inspect(selection))
    --     -- just insert the filename to the current buffer
    --     vim.api.nvim_put({ selection[1] }, "", false, true)
    --   end)
    --   return true
    -- end,
  }):find()
end

-- to execute the function
telescope_zettel()
-- colors(require("telescope.themes").get_dropdown{})

I haven't found how to create a preview window yet.

brianfabre commented 1 year ago

Cool, cheers mate. I'll definitely take a look at it!

brianfabre commented 1 year ago

I finally had a chance to look into it and I came up with something similar to the existing search.

local actions = require("telescope.actions")
local action_state = require("telescope.actions.state")

local function get_path(prompt_bufnr)
    local prompt_bufnr = vim.api.nvim_get_current_buf()
    local picker = action_state.get_current_picker(prompt_bufnr)
    local entry = action_state.get_selected_entry(prompt_bufnr)

    actions.close(prompt_bufnr)

    local bufnr = vim.api.nvim_get_current_buf()
    local pos = vim.api.nvim_win_get_cursor(0)
    local row = pos[1] - 1
    local col = pos[2]

    local text = string.format("[](%s)", entry.filename)

    vim.api.nvim_buf_set_text(bufnr, row, col, row, col, { text })
    vim.cmd([[normal! lli]])
end

path_link = function(prompt_bufnr)
    return get_path(prompt_bufnr)
end

In the telescope.setup file, you just have to add local path = require("path/to/function") and then map ["<C-o>"] = path_link (or any other mapping) to i and n in the telescope setup.

Basically, in any type of telescope search, e.g. files or live_grep, just press ctrl+o when the selection is highlighted in either normal and insert mode and it will automatically create a link like ()[link] at the cursor.

leandrochiarini commented 1 year ago

Thanks! This is great!