diff --git a/edit/history.go b/edit/history.go index b0dfaa70..7a232327 100644 --- a/edit/history.go +++ b/edit/history.go @@ -54,7 +54,7 @@ func (ed *Editor) prevHistory() bool { if ed.store != nil { // Persistent history upto := ed.cmdSeq + min(0, ed.history.current) - i, line, err := ed.store.LastCmd(upto, ed.history.prefix) + i, line, err := ed.store.LastCmd(upto, ed.history.prefix, true) if err == nil { ed.history.jump(i-ed.cmdSeq, line) return true @@ -69,7 +69,7 @@ func (ed *Editor) nextHistory() bool { // Persistent history if ed.history.current < -1 { from := ed.cmdSeq + ed.history.current + 1 - i, line, err := ed.store.FirstCmd(from, ed.history.prefix) + i, line, err := ed.store.FirstCmd(from, ed.history.prefix, true) if err == nil { ed.history.jump(i-ed.cmdSeq, line) return true diff --git a/store/cmd_hist.go b/store/cmd_hist.go index cdd63cd1..2736f32f 100644 --- a/store/cmd_hist.go +++ b/store/cmd_hist.go @@ -95,14 +95,14 @@ func convertCmd(row *sql.Row) (int, string, error) { // LastCmd finds the last command before the given sequence number (exclusive) // with the given prefix. -func (s *Store) LastCmd(upto int, prefix string) (int, string, error) { - row := s.db.QueryRow(`select rowid, content from cmd where rowid < ? and substr(content, 1, ?) = ? order by rowid desc limit 1`, upto, len(prefix), prefix) +func (s *Store) LastCmd(upto int, prefix string, uniq bool) (int, string, error) { + row := s.db.QueryRow(`select rowid, content from cmd where rowid < ? and substr(content, 1, ?) = ? and (? or lastAmongDup) order by rowid desc limit 1`, upto, len(prefix), prefix, !uniq) return convertCmd(row) } // FirstCmd finds the first command after the given sequence number (inclusive) // with the given prefix. -func (s *Store) FirstCmd(from int, prefix string) (int, string, error) { - row := s.db.QueryRow(`select rowid, content from cmd where rowid >= ? and substr(content, 1, ?) = ? order by rowid asc limit 1`, from, len(prefix), prefix) +func (s *Store) FirstCmd(from int, prefix string, uniq bool) (int, string, error) { + row := s.db.QueryRow(`select rowid, content from cmd where rowid >= ? and substr(content, 1, ?) = ? and (? or lastAmongDup) order by rowid asc limit 1`, from, len(prefix), prefix, !uniq) return convertCmd(row) }