Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 31 additions & 29 deletions src/actiontype_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 46 additions & 10 deletions src/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@ import (

// History struct represents input history
type History struct {
path string
lines []string
modified map[int]string
maxSize int
cursor int
path string
lines []string
modified map[int]string
maxSize int
cursor int
lastSearch string
}

// NewHistory returns the pointer to a new History struct
Expand Down Expand Up @@ -43,11 +44,12 @@ func NewHistory(path string, maxSize int) (*History, error) {
lines = append(lines, "")
}
return &History{
path: path,
maxSize: maxSize,
lines: lines,
modified: make(map[int]string),
cursor: len(lines) - 1}, nil
path: path,
maxSize: maxSize,
lines: lines,
modified: make(map[int]string),
cursor: len(lines) - 1,
lastSearch: ""}, nil
}

func (h *History) append(line string) error {
Expand Down Expand Up @@ -93,3 +95,37 @@ func (h *History) next() string {
}
return h.current()
}

func (h *History) prevSearch(substr string) string {
if substr == "" {
return h.previous()
}
for {
if h.cursor > 0 {
h.cursor--
} else {
return ""
}
curstr := h.current()
if strings.Contains(curstr, substr) {
return curstr
}
}
}

func (h *History) nextSearch(substr string) string {
if substr == "" {
return h.next()
}
for {
if h.cursor < len(h.lines)-1 {
h.cursor++
} else {
return ""
}
curstr := h.current()
if strings.Contains(curstr, substr) {
return curstr
}
}
}
6 changes: 5 additions & 1 deletion src/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -1435,7 +1435,7 @@ const (

func init() {
executeRegexp = regexp.MustCompile(
`(?si)[:+](become|execute(?:-multi|-silent)?|reload(?:-sync)?|preview|(?:change|bg-transform|transform)-(?:query|prompt|(?:border|list|preview|input|header|footer)-label|header|footer|search|nth|pointer|ghost)|bg-transform|transform|change-(?:preview-window|preview|multi)|(?:re|un|toggle-)bind|pos|put|print|search)`)
`(?si)[:+](become|execute(?:-multi|-silent)?|reload(?:-sync)?|preview|(?:change|bg-transform|transform)-(?:query|prompt|(?:border|list|preview|input|header|footer)-label|header|footer|search|nth|pointer|ghost)|bg-transform|transform|change-(?:preview-window|preview|multi)|(?:re|un|toggle-)bind|pos|put|print|search)|((?:prev|next)-history-search)`)
splitRegexp = regexp.MustCompile("[,:]+")
actionNameRegexp = regexp.MustCompile("(?i)^[a-z-]+")
}
Expand Down Expand Up @@ -1864,6 +1864,10 @@ func isExecuteAction(str string) actionType {
return actPrint
case "put":
return actPut
case "prev-history-search":
return actPrevHistorySearch
case "next-history-search":
return actNextHistorySearch
case "transform":
return actTransform
case "transform-list-label":
Expand Down
34 changes: 34 additions & 0 deletions src/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -627,10 +627,12 @@ const (
actPreviewHalfPageUp
actPreviewHalfPageDown
actPrevHistory
actPrevHistorySearch
actPrevSelected
actPrint
actPut
actNextHistory
actNextHistorySearch
actNextSelected
actExecute
actExecuteSilent
Expand Down Expand Up @@ -6081,6 +6083,9 @@ func (t *Terminal) Loop() error {
prefix := copySlice(t.input[:t.cx])
t.input = append(append(prefix, event.Char), t.input[t.cx:]...)
t.cx++
if t.history != nil {
t.history.lastSearch = ""
}
case actPrevHistory:
if t.history != nil {
t.history.override(string(t.input))
Expand All @@ -6093,6 +6098,28 @@ func (t *Terminal) Loop() error {
t.input = trimQuery(t.history.next())
t.cx = len(t.input)
}
case actNextHistorySearch, actPrevHistorySearch:
if t.history != nil {
t.history.override(string(t.input))
histSearch := t.history.nextSearch
if a.t == actPrevHistorySearch {
histSearch = t.history.prevSearch
}
toSearch := t.history.lastSearch
if len(toSearch) == 0 {
toSearch = a.a
if len(toSearch) == 0 {
toSearch = string(t.input)
}
t.history.lastSearch = toSearch
}
hist := histSearch(toSearch)
fmt.Fprintf(os.Stderr, "DEBUGPRINT[2]: terminal.go:6116: toSearch=%+v\n", toSearch)
if len(hist) != 0 {
t.input = trimQuery(hist)
t.cx = len(t.input)
}
}
case actToggleSearch:
t.paused = !t.paused
changed = !t.paused
Expand Down Expand Up @@ -6610,6 +6637,13 @@ func (t *Terminal) Loop() error {
}
}

// if t.history != nil &&
// a.t != actPrevHistory &&
// a.t != actNextHistory &&
// a.t != actPrevHistorySearch &&
// a.t != actNextHistorySearch {
// t.history.lastSearch = ""
// }
if !processExecution(a.t) {
Copy link
Contributor Author

@phanen phanen Jun 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure when/how to reset current search...

  • Readline reset current search and reset history pointer when cursor moved or cmdline changed.
  • Vim's :h c_<up> reset current search but don't reset history pointer. (And it's actually search prefix like rather then substr).

t.lastAction = a.t
}
Expand Down