Repository: zyedidia/micro
File: internal/buffer/buffer.go (Go)

Code before:
	switch b.Endings {
	case FFUnix:
		b.Settings["fileformat"] = "unix"
	case FFDos:
		b.Settings["fileformat"] = "dos"
	}

	b.UpdateRules()
	// we know the filetype now, so update per-filetype settings
	config.UpdateFileTypeLocals(b.Settings, b.Settings["filetype"].(string))

	if _, err := os.Stat(filepath.Join(config.ConfigDir, "buffers")); errors.Is(err, fs.ErrNotExist) {
		os.Mkdir(filepath.Join(config.ConfigDir, "buffers"), os.ModePerm)
	}

	if bcommand.StartCursor.X != -1 && bcommand.StartCursor.Y != -1 {
		b.StartCursor = bcommand.StartCursor
	} else if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
		err := b.Unserialize()
		if err != nil {
			screen.TermMessage(err)
		}
	}

	b.AddCursor(NewCursor(b, b.StartCursor))
	b.GetActiveCursor().Relocate()

	switch bcommand.CommandOrder {
	case None, StartCursorSearchText:
		// We can just search from current cursor and move it accordingly
		// If the search text is empty FindNext won't do anything
		match, found, _ := b.FindNext(bcommand.SearchText, b.Start(), b.End(), b.Start(), true, false)
		if found {
			b.GetActiveCursor().SetSelectionStart(match[0])
			b.GetActiveCursor().SetSelectionEnd(match[1])
			b.GetActiveCursor().GotoLoc(match[1])
			b.LastSearch = bcommand.SearchText
			b.LastSearchRegex = false
			b.HighlightSearch = b.Settings["hlsearch"].(bool)
		}
	case SearchTextStartCursor:
		// We only want to highlight the search text
		_, found, _ := b.FindNext(bcommand.SearchText, b.Start(), b.End(), b.Start(), true, false)
		if found {
			b.LastSearch = bcommand.SearchText
			b.LastSearchRegex = false
			b.HighlightSearch = b.Settings["hlsearch"].(bool)
		}
	}

	if !b.Settings["fastdirty"].(bool) && !found {

Review comment (suggestion):
I'm afraid we need to update `OrigSelection` as well. Try using Shift+Right or Shift+Down after opening a file with `+/TEXT`.

Code after:
		if !hasBackup {
			reader := bufio.NewReader(transform.NewReader(r, b.encoding.NewDecoder()))

			var ff FileFormat = FFAuto

			if size == 0 {
				// for empty files, use the fileformat setting instead of
				// autodetection
				switch b.Settings["fileformat"] {
				case "unix":
					ff = FFUnix
				case "dos":
					ff = FFDos
				}
			} else {
				// in case of autodetection treat as locally set
				b.LocalSettings["fileformat"] = true
			}

			b.LineArray = NewLineArray(uint64(size), ff, reader)
		}
		b.EventHandler = NewEventHandler(b.SharedBuffer, b.cursors)

		// The last time this file was modified
		b.UpdateModTime()
	}

	if b.Settings["readonly"].(bool) && b.Type == BTDefault {
		b.Type.Readonly = true
	}

	switch b.Endings {
	case FFUnix:
		b.Settings["fileformat"] = "unix"
	case FFDos:
		b.Settings["fileformat"] = "dos"
	}

	b.UpdateRules()
	// we know the filetype now, so update per-filetype settings
	config.UpdateFileTypeLocals(b.Settings, b.Settings["filetype"].(string))

	if _, err := os.Stat(filepath.Join(config.ConfigDir, "buffers")); errors.Is(err, fs.ErrNotExist) {
		os.Mkdir(filepath.Join(config.ConfigDir, "buffers"), os.ModePerm)
	}

	if cmd.StartCursor.X != -1 && cmd.StartCursor.Y != -1 {
		b.StartCursor = cmd.StartCursor
	} else if b.Settings["savecursor"].(bool) || b.Settings["saveundo"].(bool) {
		err := b.Unserialize()
		if err != nil {