@@ -215,6 +215,18 @@ const (
215215
216216type DiffStatus byte
217217
218+ type Command struct {
219+ StartCursor Loc
220+ SearchRegex string
221+ SearchAfterStart bool
222+ }
223+
224+ var emptyCommand = Command {
225+ StartCursor : Loc {- 1 , - 1 },
226+ SearchRegex : "" ,
227+ SearchAfterStart : false ,
228+ }
229+
218230// Buffer stores the main information about a currently open file including
219231// the actual text (in a LineArray), the undo/redo stack (in an EventHandler)
220232// all the cursors, the syntax highlighting info, the settings for the buffer
@@ -256,19 +268,19 @@ type Buffer struct {
256268 OverwriteMode bool
257269}
258270
259- // NewBufferFromFileAtLoc opens a new buffer with a given cursor location
260- // If cursorLoc is {-1, -1} the location does not overwrite what the cursor location
271+ // NewBufferFromFileWithCommand opens a new buffer with a given command
272+ // If cmd.StartCursor is {-1, -1} the location does not overwrite what the cursor location
261273// would otherwise be (start of file, or saved cursor position if `savecursor` is
262274// enabled)
263- func NewBufferFromFileAtLoc (path string , btype BufType , cursorLoc Loc ) (* Buffer , error ) {
275+ func NewBufferFromFileWithCommand (path string , btype BufType , cmd Command ) (* Buffer , error ) {
264276 var err error
265277 filename := path
266- if config .GetGlobalOption ("parsecursor" ).(bool ) && cursorLoc . X == - 1 && cursorLoc .Y == - 1 {
278+ if config .GetGlobalOption ("parsecursor" ).(bool ) && cmd . StartCursor . X == - 1 && cmd . StartCursor .Y == - 1 {
267279 var cursorPos []string
268280 filename , cursorPos = util .GetPathAndCursorPosition (filename )
269- cursorLoc , err = ParseCursorLocation (cursorPos )
281+ cmd . StartCursor , err = ParseCursorLocation (cursorPos )
270282 if err != nil {
271- cursorLoc = Loc {- 1 , - 1 }
283+ cmd . StartCursor = Loc {- 1 , - 1 }
272284 }
273285 }
274286
@@ -304,7 +316,7 @@ func NewBufferFromFileAtLoc(path string, btype BufType, cursorLoc Loc) (*Buffer,
304316 } else if err != nil {
305317 return nil , err
306318 } else {
307- buf = NewBuffer (file , util .FSize (file ), filename , cursorLoc , btype )
319+ buf = NewBuffer (file , util .FSize (file ), filename , btype , cmd )
308320 if buf == nil {
309321 return nil , errors .New ("could not open file" )
310322 }
@@ -323,25 +335,26 @@ func NewBufferFromFileAtLoc(path string, btype BufType, cursorLoc Loc) (*Buffer,
323335// It will return an empty buffer if the path does not exist
324336// and an error if the file is a directory
325337func NewBufferFromFile (path string , btype BufType ) (* Buffer , error ) {
326- return NewBufferFromFileAtLoc (path , btype , Loc { - 1 , - 1 } )
338+ return NewBufferFromFileWithCommand (path , btype , emptyCommand )
327339}
328340
329- // NewBufferFromStringAtLoc creates a new buffer containing the given string with a cursor loc
330- func NewBufferFromStringAtLoc (text , path string , btype BufType , cursorLoc Loc ) * Buffer {
331- return NewBuffer (strings .NewReader (text ), int64 (len (text )), path , cursorLoc , btype )
341+ // NewBufferFromStringWithCommand creates a new buffer containing the given string
342+ // with a cursor loc and a search text
343+ func NewBufferFromStringWithCommand (text , path string , btype BufType , cmd Command ) * Buffer {
344+ return NewBuffer (strings .NewReader (text ), int64 (len (text )), path , btype , cmd )
332345}
333346
334347// NewBufferFromString creates a new buffer containing the given string
335348func NewBufferFromString (text , path string , btype BufType ) * Buffer {
336- return NewBuffer (strings .NewReader (text ), int64 (len (text )), path , Loc { - 1 , - 1 }, btype )
349+ return NewBuffer (strings .NewReader (text ), int64 (len (text )), path , btype , emptyCommand )
337350}
338351
339352// NewBuffer creates a new buffer from a given reader with a given path
340353// Ensure that ReadSettings and InitGlobalSettings have been called before creating
341354// a new buffer
342355// Places the cursor at startcursor. If startcursor is -1, -1 places the
343356// cursor at an autodetected location (based on savecursor or :LINE:COL)
344- func NewBuffer (r io.Reader , size int64 , path string , startcursor Loc , btype BufType ) * Buffer {
357+ func NewBuffer (r io.Reader , size int64 , path string , btype BufType , cmd Command ) * Buffer {
345358 absPath , err := filepath .Abs (path )
346359 if err != nil {
347360 absPath = path
@@ -436,8 +449,8 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
436449 os .Mkdir (filepath .Join (config .ConfigDir , "buffers" ), os .ModePerm )
437450 }
438451
439- if startcursor . X != - 1 && startcursor .Y != - 1 {
440- b .StartCursor = startcursor
452+ if cmd . StartCursor . X != - 1 && cmd . StartCursor .Y != - 1 {
453+ b .StartCursor = cmd . StartCursor
441454 } else if b .Settings ["savecursor" ].(bool ) || b .Settings ["saveundo" ].(bool ) {
442455 err := b .Unserialize ()
443456 if err != nil {
@@ -448,6 +461,23 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
448461 b .AddCursor (NewCursor (b , b .StartCursor ))
449462 b .GetActiveCursor ().Relocate ()
450463
464+ if cmd .SearchRegex != "" {
465+ match , found , _ := b .FindNext (cmd .SearchRegex , b .Start (), b .End (), b .StartCursor , true , true )
466+ if found {
467+ if cmd .SearchAfterStart {
468+ // Search from current cursor and move it accordingly
469+ b .GetActiveCursor ().SetSelectionStart (match [0 ])
470+ b .GetActiveCursor ().SetSelectionEnd (match [1 ])
471+ b .GetActiveCursor ().OrigSelection [0 ] = b .GetActiveCursor ().CurSelection [0 ]
472+ b .GetActiveCursor ().OrigSelection [1 ] = b .GetActiveCursor ().CurSelection [1 ]
473+ b .GetActiveCursor ().GotoLoc (match [1 ])
474+ }
475+ b .LastSearch = cmd .SearchRegex
476+ b .LastSearchRegex = true
477+ b .HighlightSearch = b .Settings ["hlsearch" ].(bool )
478+ }
479+ }
480+
451481 if ! b .Settings ["fastdirty" ].(bool ) && ! found {
452482 if size > LargeFileThreshold {
453483 // If the file is larger than LargeFileThreshold fastdirty needs to be on
0 commit comments