@@ -9,7 +9,6 @@ local dict_watcher = require('orgmode.utils.dict_watcher')
9
9
local VirtualIndent = {
10
10
_ns_id = vim .api .nvim_create_namespace (' orgmode.ui.indent' ),
11
11
_bufnrs = {},
12
- _watcher_running = false ,
13
12
}
14
13
15
14
--- Creates a new instance of VirtualIndent for a given buffer or returns the existing instance if
@@ -31,6 +30,7 @@ function VirtualIndent:new(bufnr)
31
30
32
31
new ._bufnr = bufnr
33
32
new ._attached = false
33
+ new ._watcher_running = false
34
34
return new
35
35
end
36
36
@@ -100,31 +100,31 @@ end
100
100
101
101
--- Make all VirtualIndent instances react to changes in `org_indent_mode`
102
102
function VirtualIndent :start_watch_org_indent ()
103
- if not self ._watcher_running then
104
- self ._watcher_running = true
105
- dict_watcher .watch_buffer_variable (' org_indent_mode' , function (indent_mode , _ , buf_vars )
106
- local vindent = VirtualIndent ._bufnrs [buf_vars .org_bufnr ]
107
- local indent_mode_enabled = indent_mode .new or false
108
- --- @diagnostic disable-next-line : invisible
109
- if indent_mode_enabled and not vindent ._attached then
110
- vindent :attach ()
111
- --- @diagnostic disable-next-line : invisible
112
- elseif not indent_mode_enabled and vindent ._attached then
113
- vindent :detach ()
114
- end
115
- end )
103
+ if self ._watcher_running then
104
+ return
116
105
end
106
+ dict_watcher .watch_buffer_variable (' org_indent_mode' , function (indent_mode , _ , buf_vars )
107
+ local vindent = VirtualIndent ._bufnrs [buf_vars .org_bufnr ]
108
+ local indent_mode_enabled = indent_mode .new or false
109
+ if indent_mode_enabled then
110
+ return vindent :attach ()
111
+ end
112
+ return vindent :detach ()
113
+ end )
114
+ self ._watcher_running = true
117
115
end
118
116
119
117
--- Stops VirtualIndent instances from reacting to changes in `vim.b.org_indent_mode`
120
118
function VirtualIndent :stop_watch_org_indent ()
121
- self ._watcher_running = false
122
119
dict_watcher .unwatch_buffer_variable (' org_indent_mode' )
120
+ self ._watcher_running = false
123
121
end
124
122
125
123
--- Enables virtual indentation in registered buffer
126
124
function VirtualIndent :attach ()
127
- self ._attached = true
125
+ if self ._attached then
126
+ return
127
+ end
128
128
self :set_indent (0 , vim .api .nvim_buf_line_count (self ._bufnr ) - 1 , true )
129
129
self :start_watch_org_indent ()
130
130
@@ -139,12 +139,15 @@ function VirtualIndent:attach()
139
139
end )
140
140
end ,
141
141
})
142
+ self ._attached = true
142
143
end
143
144
144
145
function VirtualIndent :detach ()
145
- self ._attached = false
146
- vim .api .nvim_buf_set_var (self ._bufnr , ' org_indent_mode' , false )
146
+ if not self ._attached then
147
+ return
148
+ end
147
149
self :_delete_old_extmarks (0 , vim .api .nvim_buf_line_count (self ._bufnr ) - 1 )
150
+ self ._attached = false
148
151
end
149
152
150
153
return VirtualIndent
0 commit comments