@@ -70,14 +70,15 @@ function state:backup_qflist(scope)
7070 vim .cmd (" silent! wundo " .. vim .fn .fnameescape (tmpfile ))
7171
7272 self .backup [name ] = { bufnr = bufnr , tmp = tmpfile , seq = seq }
73- log .debug (scope , " saved undo for %s -> %s (seq=%d)" , name , tmpfile , seq )
73+ -- keep for debug but way too spammy
74+ -- log.debug(scope, "saved undo for '%s' -> '%s' with seq '%d'", name, tmpfile, seq)
7475
7576 total = total + 1
7677 end
7778 end
7879 end
7980
80- log .debug (scope , " stored %d items in backup" , total )
81+ log .debug (scope , " stored '%d' items in backup" , total )
8182
8283 return true
8384end
@@ -92,10 +93,10 @@ function state:restore_backup(scope)
9293 return
9394 end
9495
95- log .debug (scope , " restoring %d items" , vim .fn .len (self .backup ))
96+ log .debug (scope , " restoring '%d' items" , vim .fn .len (self .backup ))
9697
9798 for name , data in pairs (self .backup ) do
98- log .debug (scope , " restoring %s " , name )
99+ log .debug (scope , " restoring '%s' " , name )
99100
100101 if vim .api .nvim_buf_is_valid (data .bufnr ) then
101102 vim .api .nvim_set_current_buf (data .bufnr )
@@ -104,12 +105,12 @@ function state:restore_backup(scope)
104105 vim .cmd (" update" )
105106 log .debug (
106107 scope ,
107- " restored undo for %s (seq=%d) " ,
108+ " restored undo for '%s' " ,
108109 vim .api .nvim_buf_get_name (data .bufnr ),
109110 data .seq
110111 )
111112 else
112- log .debug (scope , " skipped %s (invalid buffer or missing file)" , name )
113+ log .debug (scope , " skipped '%s' (invalid buffer or missing file)" , name )
113114 end
114115 end
115116
@@ -138,7 +139,16 @@ function state:create_buffer(scope, cb)
138139 end
139140 end )
140141
141- vim .api .nvim_command (" startinsert!" )
142+ if not _G .SearchAndReplace .config .default_replace_prompt_to_normal_mode then
143+ vim .api .nvim_command (" startinsert!" )
144+ end
145+
146+ if
147+ _G .SearchAndReplace .config .default_replace_prompt_to_selection
148+ and self .selection ~= " "
149+ then
150+ vim .api .nvim_buf_set_text (self .buffer , 0 , 0 , 0 , 0 , { self .selection })
151+ end
142152 end ,
143153 group = self .augroup_name ,
144154 desc = " Keeps track of the state after entering new windows" ,
@@ -149,15 +159,10 @@ function state:create_buffer(scope, cb)
149159
150160 cb (self .selection , replace )
151161
152- log .debug (scope , " replace done" )
162+ log .debug (scope , " replace with '%s' done" , replace )
153163 end )
154164
155- vim .keymap .set ({ " i" , " n" }, " <Esc>" , function ()
156- log .debug (scope , " requested closing" )
157- self .cleanup (self , scope )
158- end , { buffer = self .buffer })
159-
160- log .debug (scope , " prompt buffer created" )
165+ log .debug (scope , " prompt buffer created, searching for '%s'" , self .selection )
161166
162167 self .save (self )
163168end
@@ -170,19 +175,19 @@ function state:create_window(scope)
170175 self .window = vim .api .nvim_open_win (self .buffer , true , {
171176 style = " minimal" ,
172177 relative = " editor" ,
173- width = vim .o .columns ,
178+ width = math.floor ( vim .o .columns / 3 ) ,
174179 height = 1 ,
175180 row = math.floor ((vim .o .lines - 1 ) / 2 ),
176- col = math.floor (vim .o .columns / 2 ),
181+ col = math.floor (vim .o .columns / 3 ),
177182 border = " rounded" ,
178- title = string.format (' Replace %s: "%s" ' , scope , self .selection ),
183+ title = string.format (" Replacing '%s' " , self .selection ),
179184 title_pos = " center" ,
180185 })
181186 vim .keymap .set (" i" , " <Esc>" , function ()
182187 vim .api .nvim_win_close (self .window , true )
183188 end , { buffer = self .buffer })
184189
185- log .debug (scope , " window for buffer %d created" , self .buffer )
190+ log .debug (scope , " window for buffer '%d' created" , self .buffer )
186191
187192 self .save (self )
188193end
@@ -207,7 +212,9 @@ function state:cleanup(scope)
207212
208213 self .reset (self )
209214
210- vim .api .nvim_command (" stopinsert" )
215+ if not _G .SearchAndReplace .config .default_replace_prompt_to_normal_mode then
216+ vim .api .nvim_command (" stopinsert" )
217+ end
211218end
212219
213220return state
0 commit comments