8 "github.com/zyedidia/micro/v2/internal/config"
11 // LoadHistory attempts to load user history from configDir/buffers/history
12 // into the history map
13 // The savehistory option must be on
14 func (i *InfoBuf) LoadHistory() {
15 if config.GetGlobalOption("savehistory").(bool) {
16 file, err := os.Open(filepath.Join(config.ConfigDir, "buffers", "history"))
17 var decodedMap map[string][]string
20 decoder := gob.NewDecoder(file)
21 err = decoder.Decode(&decodedMap)
24 i.Error("Error loading history:", err)
29 if decodedMap != nil {
30 i.History = decodedMap
32 i.History = make(map[string][]string)
35 i.History = make(map[string][]string)
39 // SaveHistory saves the user's command history to configDir/buffers/history
40 // only if the savehistory option is on
41 func (i *InfoBuf) SaveHistory() {
42 if config.GetGlobalOption("savehistory").(bool) {
43 // Don't save history past 100
44 for k, v := range i.History {
46 i.History[k] = v[len(i.History[k])-100:]
50 file, err := os.Create(filepath.Join(config.ConfigDir, "buffers", "history"))
53 encoder := gob.NewEncoder(file)
55 err = encoder.Encode(i.History)
57 i.Error("Error saving history:", err)
64 // AddToHistory adds a new item to the history for the prompt type `ptype`.
65 // This function is not used by micro itself. It is useful for plugins
66 // which add their own items to the history, bypassing the infobar command line.
67 func (i *InfoBuf) AddToHistory(ptype string, item string) {
68 if i.HasPrompt && i.PromptType == ptype {
72 if _, ok := i.History[ptype]; !ok {
73 i.History[ptype] = []string{item}
75 i.History[ptype] = append(i.History[ptype], item)
79 for j := len(h) - 2; j >= 0; j-- {
80 if h[j] == h[len(h)-1] {
81 i.History[ptype] = append(h[:j], h[j+1:]...)
88 // UpHistory fetches the previous item in the history
89 func (i *InfoBuf) UpHistory(history []string) {
90 if i.HistoryNum > 0 && i.HasPrompt && !i.HasYN {
92 i.Replace(i.Start(), i.End(), history[i.HistoryNum])
93 i.Buffer.GetActiveCursor().GotoLoc(i.End())
97 // DownHistory fetches the next item in the history
98 func (i *InfoBuf) DownHistory(history []string) {
99 if i.HistoryNum < len(history)-1 && i.HasPrompt && !i.HasYN {
101 i.Replace(i.Start(), i.End(), history[i.HistoryNum])
102 i.Buffer.GetActiveCursor().GotoLoc(i.End())