@@ -193,6 +193,7 @@ M.setup = function(opts)
193193 ChatNew = { " popup" , " split" , " vsplit" , " tabnew" },
194194 ChatPaste = { " popup" , " split" , " vsplit" , " tabnew" },
195195 ChatToggle = { " popup" , " split" , " vsplit" , " tabnew" },
196+ ChatLast = { " popup" , " split" , " vsplit" , " tabnew" },
196197 Context = { " popup" , " split" , " vsplit" , " tabnew" },
197198 Agent = agent_completion ,
198199 }
642643
643644--- @param file_name string
644645--- @param target number | nil # buf target
645- --- @param kind number # nil or a toggle kind
646+ --- @param kind number | nil # nil or a toggle kind, must be toggle kind when toggle is true
646647--- @param toggle boolean # whether to toggle
647648--- @return number # buffer number
648649M .open_buf = function (file_name , target , kind , toggle )
@@ -652,6 +653,7 @@ M.open_buf = function(file_name, target, kind, toggle)
652653 M ._toggle_close (M ._toggle_kind .popup )
653654
654655 if toggle then
656+ --- @cast kind number
655657 M ._toggle_close (kind )
656658 end
657659
@@ -865,6 +867,46 @@ M.cmd.ChatToggle = function(params, system_prompt, agent)
865867 M .new_chat (params , true , system_prompt , agent )
866868end
867869
870+ --- @param params table
871+ --- @return number | nil # buffer number or nil if no last chat
872+ M .cmd .ChatLast = function (params )
873+ local toggle = false
874+ -- if the range is 2, we want to create a new chat file with the selection
875+ if M ._toggle_close (M ._toggle_kind .chat ) then
876+ params .args = params .args or " "
877+ if params .args == " " then
878+ params .args = M .config .toggle_target
879+ end
880+ toggle = true
881+ end
882+ local last = M ._state .last_chat
883+ if last and vim .fn .filereadable (last ) == 1 then
884+ last = vim .fn .resolve (last )
885+ -- get current buffer, for pasting selection if necessary
886+ local cbuf = vim .api .nvim_get_current_buf ()
887+ local buf = M .helpers .get_buffer (last )
888+ local win_found = false
889+ if buf then
890+ for _ , w in ipairs (vim .api .nvim_list_wins ()) do
891+ if vim .api .nvim_win_get_buf (w ) == buf then
892+ vim .api .nvim_set_current_win (w )
893+ vim .api .nvim_set_current_buf (buf )
894+ win_found = true
895+ break
896+ end
897+ end
898+ end
899+ buf = win_found and buf or M .open_buf (last , M .resolve_buf_target (params ), toggle and M ._toggle_kind .chat or nil , toggle )
900+ -- if there is a selection, paste it
901+ if params .range == 2 then
902+ M .render .append_selection (params , cbuf , buf , M .config .template_selection )
903+ M .helpers .feedkeys (" G" , " xn" )
904+ end
905+ return buf
906+ end
907+ return nil
908+ end
909+
868910M .cmd .ChatPaste = function (params )
869911 -- if there is no selection, do nothing
870912 if params .range ~= 2 then
0 commit comments