Skip to content

Commit 2a5bdc7

Browse files
Add option to use custom window cmd for agenda and capture and align tags to the right
1 parent 65c7190 commit 2a5bdc7

File tree

8 files changed

+100
-13
lines changed

8 files changed

+100
-13
lines changed

DOCS.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,33 @@ NOTE: Make sure fast access keys do not overlap. If that happens, first entry in
7474
*default value*: `true`<br />
7575
Should error diagnostics be shown. If you are using Neovim 0.6.0 or higher, these will be shown via `vim.diagnostic`.<br />
7676

77+
#### **win_split_mode**
78+
*type*: `string`<br />
79+
*default value*: `horizontal`<br />
80+
Available options:
81+
* horizontal - Always split horizontally
82+
* vertical - Always split vertically
83+
* auto - Determine between horizontal and vertical split depending on the current window size
84+
85+
This option determines how to open agenda and capture window.<br />
86+
If none of the options above suit your needs, you can provide custom command (see `:help <mods>`). Here are few examples:<br />
87+
88+
Always open in tab:
89+
```
90+
win_split_mode = 'tabnew'
91+
```
92+
93+
Always open vertically:
94+
```
95+
win_split_mode = 'tabnew'
96+
```
97+
98+
Always open horizontally with specific height of 20 lines:
99+
```
100+
win_split_mode = '20split'
101+
```
102+
103+
77104
#### **org_todo_keyword_faces**
78105
*type*: `table<string, string>`<br />
79106
*default value*: `{}`<br />

lua/orgmode/agenda/init.lua

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,36 +32,55 @@ function Agenda:new(opts)
3232
end
3333

3434
function Agenda:agenda()
35+
self:open_window()
3536
local view = AgendaView:new({ filters = self.filters }):build()
3637
self.views = { view }
3738
return self:_render()
3839
end
3940

4041
-- TODO: Introduce searching ALL/DONE
4142
function Agenda:todos()
43+
self:open_window()
4244
local view = AgendaTodosView:new({ filters = self.filters }):build()
4345
self.views = { view }
4446
return self:_render()
4547
end
4648

4749
function Agenda:search()
50+
self:open_window()
4851
local view = AgendaSearchView:new({ filters = self.filters }):build()
4952
self.views = { view }
5053
return self:_render()
5154
end
5255

5356
function Agenda:tags()
57+
self:open_window()
5458
local view = AgendaTagsView:new({ filters = self.filters }):build()
5559
self.views = { view }
5660
return self:_render()
5761
end
5862

5963
function Agenda:tags_todo()
64+
self:open_window()
6065
local view = AgendaTagsView:new({ todo_only = true, filters = self.filters }):build()
6166
self.views = { view }
6267
return self:_render()
6368
end
6469

70+
function Agenda:open_window()
71+
local opened = self:is_opened()
72+
if opened then
73+
return
74+
end
75+
76+
utils.open_window('orgagenda', math.max(34, config.org_agenda_min_height), config.win_split_mode)
77+
78+
vim.cmd([[setf orgagenda]])
79+
vim.cmd([[setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap nospell]])
80+
vim.w.org_window_pos = vim.fn.win_screenpos(0)
81+
config:setup_mappings('agenda')
82+
end
83+
6584
function Agenda:prompt()
6685
self.filters:reset()
6786
return utils.menu('Press key for an agenda command', {
@@ -115,15 +134,12 @@ function Agenda:_render(skip_rebuild)
115134
end
116135
end
117136
local opened = self:is_opened()
118-
local win_height = math.max(math.min(34, #self.content), config.org_agenda_min_height)
119137
if not opened then
120-
vim.cmd(string.format('%dsplit orgagenda', win_height))
121-
vim.cmd([[setf orgagenda]])
122-
vim.cmd([[setlocal buftype=nofile bufhidden=wipe nobuflisted nolist noswapfile nowrap nospell]])
123-
vim.w.org_window_pos = vim.fn.win_screenpos(0)
124-
config:setup_mappings('agenda')
125-
else
126-
vim.cmd(vim.fn.win_id2win(opened) .. 'wincmd w')
138+
self:open_window()
139+
end
140+
vim.cmd(vim.fn.win_id2win(opened) .. 'wincmd w')
141+
if vim.w.org_window_split_mode == 'horizontal' then
142+
local win_height = math.max(math.min(34, #self.content), config.org_agenda_min_height)
127143
if vim.w.org_window_pos and vim.deep_equal(vim.fn.win_screenpos(0), vim.w.org_window_pos) then
128144
vim.cmd(string.format('resize %d', win_height))
129145
vim.w.org_window_pos = vim.fn.win_screenpos(0)

lua/orgmode/agenda/views/agenda.lua

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,12 @@ function AgendaView.build_agenda_item_content(agenda_item, longest_category, lon
280280
todo_keyword = todo_padding .. todo_keyword
281281
local line = string.format('%s%s%s %s', category, date, todo_keyword, headline.title)
282282
local todo_keyword_pos = string.format('%s%s%s', category, date, todo_padding):len()
283+
local winwidth = utils.winwidth()
283284
if #headline.tags > 0 then
284-
local padding_length = math.max(0, 99 - vim.api.nvim_strwidth(line))
285+
local tags_string = headline:tags_to_string()
286+
local padding_length = math.max(1, winwidth - vim.api.nvim_strwidth(line) - vim.api.nvim_strwidth(tags_string))
285287
local indent = string.rep(' ', padding_length)
286-
line = string.format('%s%s %s', line, indent, headline:tags_to_string())
288+
line = string.format('%s%s%s', line, indent, tags_string)
287289
end
288290

289291
local item_highlights = {}

lua/orgmode/agenda/views/todos.lua

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ function AgendaTodosView.generate_todo_item(headline, longest_category, line_nr)
7878
local todo_keyword = headline.todo_keyword.value
7979
local todo_keyword_padding = todo_keyword ~= '' and ' ' or ''
8080
local line = string.format(' %s%s%s %s', category, todo_keyword_padding, todo_keyword, headline.title)
81+
local winwidth = utils.winwidth()
8182
if #headline.tags > 0 then
82-
line = string.format('%-99s %s', line, headline:tags_to_string())
83+
local tags_string = headline:tags_to_string()
84+
local padding_length = math.max(1, winwidth - vim.api.nvim_strwidth(line) - vim.api.nvim_strwidth(tags_string))
85+
local indent = string.rep(' ', padding_length)
86+
line = string.format('%s%s%s', line, indent, tags_string)
8387
end
8488
local todo_keyword_pos = category:len() + 4
8589
local highlights = {}

lua/orgmode/capture/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ end
6868
---@param template table
6969
function Capture:open_template(template)
7070
local content = self.templates:compile(template)
71-
vim.cmd('16split ' .. vim.fn.tempname())
71+
utils.open_window(vim.fn.tempname(), 16, config.win_split_mode)
7272
vim.cmd([[setf org]])
7373
vim.cmd([[setlocal bufhidden=wipe nobuflisted nolist noswapfile nofoldenable]])
7474
vim.api.nvim_buf_set_lines(0, 0, -1, true, content)

lua/orgmode/config/defaults.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ return {
3939
org_src_window_setup = 'top 16new',
4040
org_edit_src_content_indentation = 0,
4141
diagnostics = true,
42+
win_split_mode = 'horizontal',
4243
notifications = {
4344
enabled = false,
4445
cron_enabled = true,

lua/orgmode/objects/date.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ function Date:is_between(from, to, span)
517517
end
518518

519519
---@param date Date
520-
---@param span string
520+
---@param span? string
521521
---@return boolean
522522
function Date:is_before(date, span)
523523
return not self:is_same_or_after(date, span)

lua/orgmode/utils/init.lua

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,4 +499,41 @@ function utils.current_file_path()
499499
return vim.api.nvim_buf_get_name(0)
500500
end
501501

502+
---@param winnr? number
503+
function utils.winwidth(winnr)
504+
winnr = winnr or 0
505+
local winwidth = vim.api.nvim_win_get_width(winnr)
506+
local window_numbers = vim.api.nvim_win_get_option(winnr, 'number')
507+
local window_relnumbers = vim.api.nvim_win_get_option(winnr, 'relativenumber')
508+
if window_numbers or window_relnumbers then
509+
winwidth = winwidth - vim.wo.numberwidth
510+
end
511+
return winwidth
512+
end
513+
514+
---@param name string
515+
---@param height number
516+
function utils.open_window(name, height, split_mode)
517+
local cmd_by_split_mode = {
518+
horizontal = string.format('%dsplit %s', height, name),
519+
vertical = string.format('vsplit %s', name),
520+
}
521+
522+
if cmd_by_split_mode[split_mode] then
523+
vim.cmd(cmd_by_split_mode[split_mode])
524+
vim.w.org_window_split_mode = split_mode
525+
elseif split_mode == 'auto' then
526+
local winwidth = utils.winwidth()
527+
if (winwidth / 2) >= 80 then
528+
vim.cmd(cmd_by_split_mode.vertical)
529+
vim.w.org_window_split_mode = 'vertical'
530+
else
531+
vim.cmd(cmd_by_split_mode.horizontal)
532+
vim.w.org_window_split_mode = 'horizontal'
533+
end
534+
else
535+
vim.cmd(string.format('%s %s', split_mode, name))
536+
end
537+
end
538+
502539
return utils

0 commit comments

Comments
 (0)