Skip to content

Commit 7979893

Browse files
authored
Fix broken priority cycling (#418)
1 parent 5f489c0 commit 7979893

File tree

3 files changed

+96
-8
lines changed

3 files changed

+96
-8
lines changed

lua/orgmode/config/init.lua

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,95 @@ function Config:extend(opts)
3434
self.todo_keywords = nil
3535
opts = opts or {}
3636
self:_deprecation_notify(opts)
37+
if not self:_are_priorities_valid(opts) then
38+
opts.org_priority_highest = self.opts.org_priority_highest
39+
opts.org_priority_lowest = self.opts.org_priority_lowest
40+
opts.org_priority_default = self.opts.org_priority_default
41+
end
3742
self.opts = vim.tbl_deep_extend('force', self.opts, opts)
3843
return self
3944
end
4045

46+
function Config:_are_priorities_valid(opts)
47+
local high = opts.org_priority_highest
48+
local low = opts.org_priority_lowest
49+
local default = opts.org_priority_default
50+
51+
if high or low or default then
52+
-- assert that all three options are set
53+
if not (high and low and default) then
54+
utils.echo_warning(
55+
'org_priority_highest, org_priority_lowest and org_priority_default can only be set together.'
56+
.. 'Falling back to default priorities'
57+
)
58+
return false
59+
end
60+
61+
-- numbers
62+
if type(high) == 'number' and type(low) == 'number' and type(default) == 'number' then
63+
if high < 0 or low < 0 or default < 0 then
64+
utils.echo_warning(
65+
'org_priority_highest, org_priority_lowest and org_priority_default cannot be negative.'
66+
.. 'Falling back to default priorities'
67+
)
68+
return false
69+
end
70+
if high > low then
71+
utils.echo_warning(
72+
'org_priority_highest cannot be bigger than org_priority_lowest. Falling back to default priorities'
73+
)
74+
return false
75+
end
76+
if default < high or default > low then
77+
utils.echo_warning(
78+
'org_priority_default must be bigger than org_priority_highest and smaller than org_priority_lowest.'
79+
.. 'Falling back to default priorities'
80+
)
81+
return false
82+
end
83+
-- one-char strings
84+
elseif
85+
(type(high) == 'string' and #high == 1)
86+
and (type(low) == 'string' and #low == 1)
87+
and (type(default) == 'string' and #default == 1)
88+
then
89+
if not high:match('%a') or not low:match('%a') or not default:match('%a') then
90+
utils.echo_warning(
91+
'org_priority_highest, org_priority_lowest and org_priority_default must be letters.'
92+
.. 'Falling back to default priorities'
93+
)
94+
return false
95+
end
96+
97+
high = string.byte(high)
98+
low = string.byte(low)
99+
default = string.byte(default)
100+
if high > low then
101+
utils.echo_warning(
102+
'org_priority_highest cannot be bigger than org_priority_lowest. Falling back to default priorities'
103+
)
104+
return false
105+
end
106+
if default < high or default > low then
107+
utils.echo_warning(
108+
'org_priority_default must be bigger than org_priority_highest and smaller than org_priority_lowest.'
109+
.. 'Falling back to default priorities'
110+
)
111+
return false
112+
end
113+
else
114+
utils.echo_warning(
115+
'org_priority_highest, org_priority_lowest and org_priority_default must be either of type'
116+
.. "'number' or of type 'string' of length one. All three options need to agree on this type."
117+
.. 'Falling back to default priorities'
118+
)
119+
return false
120+
end
121+
end
122+
123+
return true
124+
end
125+
41126
function Config:_deprecation_notify(opts)
42127
local messages = {}
43128
if

lua/orgmode/objects/priority_state.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ end
8181
---@param direction number
8282
---@return string
8383
function PriorityState:_apply(direction)
84+
if type(tonumber(self.priority)) == 'number' then
85+
return tostring(tonumber(self.priority) + direction)
86+
end
8487
return string.char(string.byte(self.priority) + direction)
8588
end
8689

tests/plenary/object/priority_state_spec.lua

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,22 +12,22 @@ describe('Priority state', function()
1212

1313
local numeric_config = function()
1414
config:extend({
15-
org_priority_highest = 10,
15+
org_priority_highest = 1,
1616
org_priority_default = 5,
17-
org_priority_lowest = 1,
17+
org_priority_lowest = 15,
1818
})
1919
end
2020

2121
it('should increase single numeric priority', function()
2222
numeric_config()
23-
local priority = PriorityState:new(3)
24-
assert.are.same('2', priority:increase())
23+
local priority = PriorityState:new(10)
24+
assert.are.same('9', priority:increase())
2525
end)
2626

2727
it('should decrease single numeric priority', function()
2828
numeric_config()
29-
local priority = PriorityState:new(3)
30-
assert.are.same('4', priority:decrease())
29+
local priority = PriorityState:new(9)
30+
assert.are.same('10', priority:decrease())
3131
end)
3232

3333
it('should increase single alpha priority', function()
@@ -44,13 +44,13 @@ describe('Priority state', function()
4444

4545
it('should change to empty priority when numeric increased beyond highest', function()
4646
numeric_config()
47-
local priority = PriorityState:new('10')
47+
local priority = PriorityState:new('1')
4848
assert.are.same('', priority:increase())
4949
end)
5050

5151
it('should change to empty priority when numeric decreased beyond lowest', function()
5252
numeric_config()
53-
local priority = PriorityState:new('1')
53+
local priority = PriorityState:new('15')
5454
assert.are.same('', priority:decrease())
5555
end)
5656

0 commit comments

Comments
 (0)