@@ -348,35 +348,43 @@ function OrgMappings:todo_prev_state()
348
348
end
349
349
350
350
function OrgMappings :toggle_heading ()
351
- local line = vim .fn .getline (' .' )
352
- -- TODO: allow nil
351
+ local line_number = vim .fn .line (' .' )
352
+ local line = vim . fn . getline ( line_number )
353
353
local parent = self .files :get_closest_headline_or_nil ()
354
+
355
+ local set_line_and_dispatch_event = function (line_content , action )
356
+ vim .fn .setline (line_number , line_content )
357
+ EventManager .dispatch (
358
+ events .HeadingToggled :new (line_number , action , self .files :get_closest_headline_or_nil ({ line_number , 0 }))
359
+ )
360
+ end
361
+ -- Convert to headline
354
362
if not parent then
355
- line = ' * ' .. line
356
- vim .fn .setline (' .' , line )
357
- return
363
+ return set_line_and_dispatch_event (' * ' .. line , ' line_to_headline' )
358
364
end
359
365
366
+ -- Convert headline to plain text
360
367
if parent :get_range ().start_line == vim .api .nvim_win_get_cursor (0 )[1 ] then
361
368
line = line :gsub (' ^%*+%s' , ' ' )
362
- else
363
- line = line :gsub (' ^(%s*)' , ' ' )
364
- if line :match (' ^[%*-]%s' ) then -- handle lists
365
- line = line :gsub (' ^[%*-]%s' , ' ' ) -- strip bullet
366
- local todo_keywords = config :get_todo_keywords ()
367
- line = line :gsub (' ^%[([X%s])%]%s' , function (checkbox_state )
368
- if checkbox_state == ' X' then
369
- return todo_keywords :first_by_type (' DONE' ).value .. ' '
370
- else
371
- return todo_keywords :first_by_type (' TODO' ).value .. ' '
372
- end
373
- end )
374
- end
369
+ return set_line_and_dispatch_event (line , ' headline_to_line' )
370
+ end
375
371
376
- line = string.rep (' *' , parent :get_level () + 1 ) .. ' ' .. line
372
+ line = line :gsub (' ^(%s*)' , ' ' )
373
+ if line :match (' ^[%*-]%s' ) then -- handle lists
374
+ line = line :gsub (' ^[%*-]%s' , ' ' ) -- strip bullet
375
+ local todo_keywords = config :get_todo_keywords ()
376
+ line = line :gsub (' ^%[([X%s])%]%s' , function (checkbox_state )
377
+ if checkbox_state == ' X' then
378
+ return todo_keywords :first_by_type (' DONE' ).value .. ' '
379
+ else
380
+ return todo_keywords :first_by_type (' TODO' ).value .. ' '
381
+ end
382
+ end )
377
383
end
378
384
379
- vim .fn .setline (' .' , line )
385
+ line = string.rep (' *' , parent :get_level () + 1 ) .. ' ' .. line
386
+
387
+ return set_line_and_dispatch_event (line , ' line_to_child_headline' )
380
388
end
381
389
382
390
--- Prompt for a note
0 commit comments