| 1 |
package info |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/gob" |
| 6 |
"errors" |
| 7 |
"io/fs" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"strings" |
| 11 |
|
| 12 |
"github.com/zyedidia/micro/v2/internal/config" |
| 13 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 14 |
"github.com/zyedidia/micro/v2/internal/util" |
| 15 |
) |
| 16 |
|
| 17 |
// LoadHistory attempts to load user history from configDir/buffers/history |
| 18 |
// into the history map |
| 19 |
// The savehistory option must be on |
| 20 |
func (i *InfoBuf) LoadHistory() { |
| 21 |
if config.GetGlobalOption("savehistory").(bool) { |
| 22 |
file, err := os.Open(filepath.Join(config.ConfigDir, "buffers", "history")) |
| 23 |
var decodedMap map[string][]string |
| 24 |
if err != nil { |
| 25 |
if !errors.Is(err, fs.ErrNotExist) { |
| 26 |
i.Error("Error loading history: ", err) |
| 27 |
} |
| 28 |
return |
| 29 |
} |
| 30 |
|
| 31 |
defer file.Close() |
| 32 |
err = gob.NewDecoder(file).Decode(&decodedMap) |
| 33 |
if err != nil { |
| 34 |
i.Error("Error decoding history: ", err) |
| 35 |
return |
| 36 |
} |
| 37 |
|
| 38 |
if decodedMap != nil { |
| 39 |
i.History = decodedMap |
| 40 |
} |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
// SaveHistory saves the user's command history to configDir/buffers/history |
| 45 |
// only if the savehistory option is on |
| 46 |
func (i *InfoBuf) SaveHistory() { |
| 47 |
if config.GetGlobalOption("savehistory").(bool) { |
| 48 |
// Don't save history past 100 |
| 49 |
for k, v := range i.History { |
| 50 |
if len(v) > 100 { |
| 51 |
i.History[k] = v[len(i.History[k])-100:] |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
var buf bytes.Buffer |
| 56 |
err := gob.NewEncoder(&buf).Encode(i.History) |
| 57 |
if err != nil { |
| 58 |
screen.TermMessage("Error encoding history: ", err) |
| 59 |
return |
| 60 |
} |
| 61 |
|
| 62 |
filename := filepath.Join(config.ConfigDir, "buffers", "history") |
| 63 |
err = util.SafeWrite(filename, buf.Bytes(), true) |
| 64 |
if err != nil { |
| 65 |
screen.TermMessage("Error saving history: ", err) |
| 66 |
return |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
// AddToHistory adds a new item to the history for the prompt type `ptype`. |
| 72 |
// This function is not used by micro itself. It is useful for plugins |
| 73 |
// which add their own items to the history, bypassing the infobar command line. |
| 74 |
func (i *InfoBuf) AddToHistory(ptype string, item string) { |
| 75 |
if i.HasPrompt && i.PromptType == ptype { |
| 76 |
return |
| 77 |
} |
| 78 |
|
| 79 |
if _, ok := i.History[ptype]; !ok { |
| 80 |
i.History[ptype] = []string{item} |
| 81 |
} else { |
| 82 |
i.History[ptype] = append(i.History[ptype], item) |
| 83 |
|
| 84 |
// avoid duplicates |
| 85 |
h := i.History[ptype] |
| 86 |
for j := len(h) - 2; j >= 0; j-- { |
| 87 |
if h[j] == h[len(h)-1] { |
| 88 |
i.History[ptype] = append(h[:j], h[j+1:]...) |
| 89 |
break |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// UpHistory fetches the previous item in the history |
| 96 |
func (i *InfoBuf) UpHistory(history []string) { |
| 97 |
if i.HistoryNum > 0 && i.HasPrompt && !i.HasYN { |
| 98 |
i.HistoryNum-- |
| 99 |
i.Replace(i.Start(), i.End(), history[i.HistoryNum]) |
| 100 |
i.Buffer.GetActiveCursor().GotoLoc(i.End()) |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
// DownHistory fetches the next item in the history |
| 105 |
func (i *InfoBuf) DownHistory(history []string) { |
| 106 |
if i.HistoryNum < len(history)-1 && i.HasPrompt && !i.HasYN { |
| 107 |
i.HistoryNum++ |
| 108 |
i.Replace(i.Start(), i.End(), history[i.HistoryNum]) |
| 109 |
i.Buffer.GetActiveCursor().GotoLoc(i.End()) |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
// SearchUpHistory fetches the previous item in the history |
| 114 |
// beginning with the text in the infobuffer before cursor |
| 115 |
func (i *InfoBuf) SearchUpHistory(history []string) { |
| 116 |
if i.HistoryNum > 0 && i.HasPrompt && !i.HasYN { |
| 117 |
i.searchHistory(history, false) |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
// SearchDownHistory fetches the next item in the history |
| 122 |
// beginning with the text in the infobuffer before cursor |
| 123 |
func (i *InfoBuf) SearchDownHistory(history []string) { |
| 124 |
if i.HistoryNum < len(history)-1 && i.HasPrompt && !i.HasYN { |
| 125 |
i.searchHistory(history, true) |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
func (i *InfoBuf) searchHistory(history []string, down bool) { |
| 130 |
line := string(i.LineBytes(0)) |
| 131 |
c := i.Buffer.GetActiveCursor() |
| 132 |
|
| 133 |
if !i.HistorySearch || !strings.HasPrefix(line, i.HistorySearchPrefix) { |
| 134 |
i.HistorySearch = true |
| 135 |
i.HistorySearchPrefix = util.SliceStartStr(line, c.X) |
| 136 |
} |
| 137 |
|
| 138 |
found := -1 |
| 139 |
if down { |
| 140 |
for j := i.HistoryNum + 1; j < len(history); j++ { |
| 141 |
if strings.HasPrefix(history[j], i.HistorySearchPrefix) { |
| 142 |
found = j |
| 143 |
break |
| 144 |
} |
| 145 |
} |
| 146 |
} else { |
| 147 |
for j := i.HistoryNum - 1; j >= 0; j-- { |
| 148 |
if strings.HasPrefix(history[j], i.HistorySearchPrefix) { |
| 149 |
found = j |
| 150 |
break |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
if found != -1 { |
| 155 |
i.HistoryNum = found |
| 156 |
i.Replace(i.Start(), i.End(), history[found]) |
| 157 |
c.GotoLoc(i.End()) |
| 158 |
} |
| 159 |
} |