#  /neovim/profiles/telekasten.nix - Neovim configuration
#
#  > A Neovim (lua) plugin for working with a markdown zettelkasten / wiki and
#  > mixing it with a journal, based on telescope.nvim
#  https://github.com/renerocksai/telekasten.nvim/
#
#       //_/  Jörg Kütemeier <https://kuetemeier.de>
#    ._// )   (c) Copyright 2023 - License: MPL-2.0
#
#
# {{{ MPL-2.0
#
#  This Source Code Form is subject to the terms of the Mozilla Public
#  License, v. 2.0. If a copy of the MPL was not distributed with this
#  file, You can obtain one at https://mozilla.org/MPL/2.0/.
#
# }}}
{pkgs, ...}: {
  config = {
    plugins.rene-calendar.enable = true;

    extraPlugins = with pkgs.vimPlugins; [
      telekasten-nvim
      telescope-media-files-nvim
      mattn-calendar-vim
    ];

    extraPackages = with pkgs; [
      chafa
      imagemagick
      ffmpegthumbnailer
      gnome-epub-thumbnailer
    ];

    extraConfigLua = ''
      require('telescope').load_extension('media_files')

      local home = vim.fn.expand("~/Documents/jkr-notes")
      -- NOTE for Windows users:
      -- - don't use Windows
      -- - try WSL2 on Windows and pretend you're on Linux
      -- - if you **must** use Windows, use "/Users/myname/zettelkasten" instead of "~/zettelkasten"
      -- - NEVER use "C:\Users\myname" style paths

      require('telekasten').setup({
        home = home,

        -- if true, telekasten will be enabled when opening a note within the configured home
        take_over_my_home = true,

        -- auto-set telekasten filetype: if false, the telekasten filetype will not be used
        --                               and thus the telekasten syntax will not be loaded either
        auto_set_filetype = true,

        -- dir names for special notes (absolute path or subdir name)
        dailies      = home .. '/' .. 'Journals',
        weeklies     = home .. '/' .. 'Weekly',
        templates    = home .. '/' .. 'Templates',

        -- image (sub)dir for pasting
        -- dir name (absolute path or subdir name)
        -- or nil if pasted images shouldn't go into a special subdir
        image_subdir = "assets",

        -- markdown file extension
        extension    = ".md",

        -- following a link to a non-existing note will create it
        follow_creates_nonexisting = true,
        dailies_create_nonexisting = true,
        weeklies_create_nonexisting = true,

        -- template for new notes (new_note, follow_link)
        -- set to `nil` or do not specify if you do not want a template
        template_new_note = home .. '/' .. 'Templates/Pages/new_note.md',

        -- template for newly created daily notes (goto_today)
        -- set to `nil` or do not specify if you do not want a template
        template_new_daily = home .. '/' .. 'Templates/Pages/daily.md',

        -- template for newly created weekly notes (goto_thisweek)
        -- set to `nil` or do not specify if you do not want a template
        template_new_weekly= home .. '/' .. 'Templates/Pages/weekly.md',

        -- image link style
        -- wiki:     ![[image name]]
        -- markdown: ![](image_subdir/xxxxx.png)
        image_link_style = "markdown",

        -- integrate with calendar-vim
        plug_into_calendar = true,
        calendar_opts = {
            -- calendar week display mode: 1 .. 'WK01', 2 .. 'WK 1', 3 .. 'KW01', 4 .. 'KW 1', 5 .. '1'
            weeknm = 4,
            -- use monday as first day of week: 1 .. true, 0 .. false
            calendar_monday = 1,
            -- calendar mark: where to put mark for marked days: 'left', 'right', 'left-fit'
            calendar_mark = 'left-fit',
        },

        -- telescope actions behavior
        close_after_yanking = false,
        insert_after_inserting = true,

        -- tag notation: '#tag', ':tag:', 'yaml-bare'
        tag_notation = "#tag",

        -- command palette theme: dropdown (window) or ivy (bottom panel)
        command_palette_theme = "ivy",

        -- tag list theme:
        -- get_cursor: small tag list at cursor; ivy and dropdown like above
        show_tags_theme = "ivy",

        -- when linking to a note in subdir/, create a [[subdir/title]] link
        -- instead of a [[title only]] link
        subdirs_in_links = false,

        -- template_handling
        -- What to do when creating a new note via `new_note()` or `follow_link()`
        -- to a non-existing note
        -- - prefer_new_note: use `new_note` template
        -- - smart: if day or week is detected in title, use daily / weekly templates (default)
        -- - always_ask: always ask before creating a note
        template_handling = "smart",

        -- path handling:
        --   this applies to:
        --     - new_note()
        --     - new_templated_note()
        --     - follow_link() to non-existing note
        --
        --   it does NOT apply to:
        --     - goto_today()
        --     - goto_thisweek()
        --
        --   Valid options:
        --     - smart: put daily-looking notes in daily, weekly-looking ones in weekly,
        --              all other ones in home, except for notes/with/subdirs/in/title.
        --              (default)
        --
        --     - prefer_home: put all notes in home except for goto_today(), goto_thisweek()
        --                    except for notes with subdirs/in/title.
        --
        --     - same_as_current: put all new notes in the dir of the current note if
        --                        present or else in home
        --                        except for notes/with/subdirs/in/title.
        new_note_location = "smart",

        -- should all links be updated when a file is renamed
        rename_update_links = true,

      })
    '';

    keymaps = [
      {
        key = "<leader>tp";
        mode = "n";
        action = "<cmd>Telekasten panel<CR>";
        options.desc = "Telekasten: Got to Today (current date in journal)";
        options.silent = true;
      }
      {
        key = "<leader>td";
        mode = "n";
        action = "<cmd>Telekasten goto_today<CR>";
        options.desc = "Telekasten: Got to Today (current date in journal)";
        options.silent = true;
      }
      {
        key = "<leader>tf";
        mode = "n";
        action = "<cmd>Telekasten find_notes<CR>";
        options.desc = "Telekasten: Find notes";
        options.silent = true;
      }
      {
        key = "<leader>tg";
        mode = "n";
        action = "<cmd>Telekasten search_notes<CR>";
        options.desc = "Telekasten: Search notes";
        options.silent = true;
      }
      {
        key = "<leader>tz";
        mode = "n";
        action = "<cmd>Telekasten follow_link<CR>";
        options.desc = "Telekasten: Follow Link";
        options.silent = true;
      }
      {
        key = "<leader>tn";
        mode = "n";
        action = "<cmd>Telekasten new_note<CR>";
        options.desc = "Telekasten: New Note";
        options.silent = true;
      }
      {
        key = "<leader>tc";
        mode = "n";
        action = "<cmd>Telekasten show_calendar<CR>";
        options.desc = "Telekasten: Calendar";
        options.silent = true;
      }
      {
        key = "<leader>tb";
        mode = "n";
        action = "<cmd>Telekasten show_backlinks<CR>";
        options.desc = "Telekasten: Show Backlinks";
        options.silent = true;
      }
      {
        key = "<leader>tI";
        mode = "n";
        action = "<cmd>Telekasten insert_img_link<CR>";
        options.desc = "Telekasten: Insert Image";
        options.silent = true;
      }
    ];
  };
}
