From b3a58db72e190073682a1c54b7ed3f6f0c512adf Mon Sep 17 00:00:00 2001 From: Yuta Katayama <8683947+yutkat@users.noreply.github.com> Date: Sun, 1 May 2022 04:39:48 +0900 Subject: [PATCH] feat: Add sorting feature [closes #85] * feat: Add sorting feature #85 * fix: Remove the lines that should not be sorted --- README.md | 3 +++ doc/telekasten.txt | 12 ++++++++++++ lua/telekasten.lua | 26 +++++++++++++++++++++----- 3 files changed, 36 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index a5490f0..4e11f9f 100644 --- a/README.md +++ b/README.md @@ -271,6 +271,9 @@ require('telekasten').setup({ -- markdown: ![](image_subdir/xxxxx.png) image_link_style = "markdown", + -- default sort option: 'filename', 'modified' + sort = "filename", + -- integrate with calendar-vim plug_into_calendar = true, calendar_opts = { diff --git a/doc/telekasten.txt b/doc/telekasten.txt index 4478bc5..f0345e0 100644 --- a/doc/telekasten.txt +++ b/doc/telekasten.txt @@ -92,6 +92,8 @@ telekasten.setup({opts}) -- markdown: ![](image_subdir/xxxxx.png) image_link_style = "wiki", + -- default sort option: 'filename', 'modified' + sort = "filename", -- specific note templates -- set to `nil` or do not specify if you do not want a template @@ -245,6 +247,16 @@ telekasten.setup({opts}) Default: 'markdown' + *telekasten.settings.sort* + sort: ~ + Order the notes by the option. + + Valid options are: + - 'filename' + - 'modified' + + Default: 'filename' + *telekasten.settings.follow_create_nonexisting* follow_creates_nonexisting: ~ Flag that determines whether non-existing notes should be created when diff --git a/lua/telekasten.lua b/lua/telekasten.lua index 1a2f6b6..7a17f32 100644 --- a/lua/telekasten.lua +++ b/lua/telekasten.lua @@ -99,6 +99,9 @@ M.Cfg = { -- markdown: ![](image_subdir/xxxxx.png) image_link_style = "markdown", + -- default sort option: 'filename', 'modified' + sort = "filename", + -- when linking to a note in subdir/, create a [[subdir/title]] link -- instead of a [[title only]] link subdirs_in_links = true, @@ -933,10 +936,6 @@ function Pinfo:resolve_link(title, opts) return self end -local function order_numeric(a, b) - return a > b -end - -- local function endswith(s, ending) -- return ending == "" or s:sub(-#ending) == ending -- end @@ -1013,7 +1012,16 @@ local function find_files_sorted(opts) local file_list = scan.scan_dir(opts.cwd, {}) local filter_extensions = opts.filter_extensions or M.Cfg.filter_extensions file_list = filter_filetypes(file_list, filter_extensions) - table.sort(file_list, order_numeric) + local sort_option = opts.sort or "filename" + if sort_option == "modified" then + table.sort(file_list, function(a, b) + return vim.fn.getftime(a) > vim.fn.getftime(b) + end) + else + table.sort(file_list, function(a, b) + return a > b + end) + end local counts = nil if opts.show_link_counts then @@ -1322,6 +1330,7 @@ local function FindDailyNotes(opts) map("n", "", picker_actions.close(opts)) return true end, + sort = M.Cfg.sort, }) end @@ -1371,6 +1380,7 @@ local function FindWeeklyNotes(opts) map("n", "", picker_actions.close(opts)) return true end, + sort = M.Cfg.sort, }) end @@ -1422,6 +1432,7 @@ local function InsertLink(opts) return true end, find_command = M.Cfg.find_command, + sort = M.Cfg.sort, }) end @@ -1506,6 +1517,7 @@ local function PreviewImg(opts) map("n", "", picker_actions.paste_img_link(opts)) return true end, + sort = M.Cfg.sort, }) else print("File not found: " .. M.Cfg.home .. "/" .. fname) @@ -1556,6 +1568,7 @@ local function BrowseImg(opts) map("n", "", picker_actions.paste_img_link(opts)) return true end, + sort = M.Cfg.sort, }) end @@ -1793,6 +1806,7 @@ local function FindNotes(opts) map("n", "", picker_actions.paste_link(opts)) return true end, + sort = M.Cfg.sort, }) end @@ -1843,6 +1857,7 @@ local function InsertImgLink(opts) map("n", "", picker_actions.paste_img_link(opts)) return true end, + sort = M.Cfg.sort, }) end @@ -2204,6 +2219,7 @@ local function FollowLink(opts) map("n", "", picker_actions.close(opts)) return true end, + sort = M.Cfg.sort, }) end