Skip to content

Commit 8291ba6

Browse files
feat(agenda): Add mapping to preview headline in agenda with "K"
1 parent 01e0924 commit 8291ba6

File tree

5 files changed

+67
-7
lines changed

5 files changed

+67
-7
lines changed

docs/configuration.org

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,13 @@ Use vim regex flag =\c= to make it case insensitive. See
15191519
=:help vim.regex()= and =:help /magic=.
15201520
Pressing =<TAB>= in filter prompt autocompletes categories and tags.
15211521

1522+
**** org_agenda_preview
1523+
:PROPERTIES:
1524+
:CUSTOM_ID: org_agenda_preview
1525+
:END:
1526+
- Mapped to: =K=
1527+
Preview the agenda item in a floating window. For configuring the floating window, check [[#agenda][Agenda section in ui configuration]].
1528+
15221529
**** org_agenda_show_help
15231530
:PROPERTIES:
15241531
:CUSTOM_ID: org_agenda_show_help
@@ -2995,8 +3002,6 @@ require('orgmode').setup({
29953002
:PROPERTIES:
29963003
:CUSTOM_ID: input
29973004
:END:
2998-
- Type: =boolean=
2999-
- Default: =false=
30003005

30013006
By default, Vim's built-in =input()= function is used for input prompts.
30023007
To use Neovim's =vim.ui.input()= function, add this to config:
@@ -3012,3 +3017,26 @@ require('orgmode').setup({
30123017
#+end_src
30133018

30143019
📝 NOTE: If you are using a plugin for =vim.ui.input=, make sure it supports autocompletion for better experience. [[https://github.com/folke/snacks.nvim][snacks.nvim]] input module supports autocompletion.
3020+
3021+
*** Agenda
3022+
:PROPERTIES:
3023+
:CUSTOM_ID: agenda
3024+
:END:
3025+
Use this section to customize the styling of the Agenda preview floating window (See [[#org_agenda_preview][org_agenda_preview]] mapping).
3026+
To see available options for the preview window, check =:help vim.lsp.util.open_floating_preview.Opts=.
3027+
Here's an example how to configure it:
3028+
3029+
#+begin_src lua
3030+
require('orgmode').setup({
3031+
ui = {
3032+
agenda = {
3033+
preview_window = {
3034+
wrap = false, -- This option is set by default
3035+
border = 'single'
3036+
}
3037+
}
3038+
}
3039+
})
3040+
#+end_src
3041+
3042+
📝 NOTE: This preview should be used only as read only view of the headline and it's content. Modifying the file from the preview window is not supported.

lua/orgmode/agenda/init.lua

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,28 @@ function Agenda:refile()
382382
})
383383
end
384384

385+
function Agenda:preview_item()
386+
local headline = self:get_headline_at_cursor()
387+
if not headline then
388+
return
389+
end
390+
391+
local lines = headline:get_lines()
392+
local offset = 4
393+
local width = lines[1]:len() + offset
394+
395+
vim.tbl_map(function(line)
396+
width = math.max(width, line:len() + offset)
397+
end, lines)
398+
399+
local win_opts = vim.tbl_deep_extend('force', {
400+
width = width,
401+
}, config.ui.agenda.preview_window or {})
402+
403+
local buf = vim.lsp.util.open_floating_preview(lines, '', win_opts)
404+
vim.api.nvim_set_option_value('filetype', 'org', { buf = buf })
405+
end
406+
385407
function Agenda:clock_out()
386408
return self:_remote_edit({
387409
action = 'clock.org_clock_out',
@@ -591,11 +613,8 @@ function Agenda:get_headline_at_cursor()
591613

592614
for _, view in ipairs(self.views) do
593615
local agenda_line = view:get_line(line_nr)
594-
if agenda_line then
595-
local headline = agenda_line.headline
596-
if headline then
597-
return headline
598-
end
616+
if agenda_line and agenda_line.headline then
617+
return agenda_line.headline
599618
end
600619
end
601620
end

lua/orgmode/config/_meta.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@
174174
---@field folds? { colored: boolean } Should folds be colored or use the default folding highlight. Default: { colored: true }
175175
---@field menu? { handler: fun() | nil } Menu configuration
176176
---@field input? { use_vim_ui: boolean } Input configuration
177+
---@field agenda? { preview_window?: vim.lsp.util.open_floating_preview.Opts } Agenda preview window option
177178

178179
---@class OrgMappingsConfig
179180
---@field disable_all? boolean Disable all mappings. Default: false

lua/orgmode/config/defaults.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ local DefaultConfig = {
124124
org_agenda_filter = '/',
125125
org_agenda_refile = '<prefix>r',
126126
org_agenda_add_note = '<prefix>na',
127+
org_agenda_preview = 'K',
127128
org_agenda_show_help = 'g?',
128129
},
129130
capture = {
@@ -221,6 +222,11 @@ local DefaultConfig = {
221222
input = {
222223
use_vim_ui = false,
223224
},
225+
agenda = {
226+
preview_window = {
227+
wrap = false,
228+
},
229+
},
224230
},
225231
}
226232

lua/orgmode/config/mappings/init.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ return {
123123
help_desc = 'Refile headline to specific destination',
124124
},
125125
}),
126+
org_agenda_preview = m.action('agenda.preview_item', {
127+
opts = {
128+
desc = 'org preview',
129+
help_desc = 'Preview agenda item in floating window',
130+
},
131+
}),
126132
org_agenda_add_note = m.action(
127133
'agenda.add_note',
128134
{ opts = { desc = 'org add note', help_desc = 'Add a note to the current headline' } }

0 commit comments

Comments
 (0)