]> git.lizzy.rs Git - micro.git/blob - internal/info/history.go
Fix noregex interactive replace
[micro.git] / internal / info / history.go
1 package info
2
3 import (
4         "encoding/gob"
5         "os"
6         "path/filepath"
7
8         "github.com/zyedidia/micro/v2/internal/config"
9 )
10
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
18                 if err == nil {
19                         defer file.Close()
20                         decoder := gob.NewDecoder(file)
21                         err = decoder.Decode(&decodedMap)
22
23                         if err != nil {
24                                 i.Error("Error loading history:", err)
25                                 return
26                         }
27                 }
28
29                 if decodedMap != nil {
30                         i.History = decodedMap
31                 } else {
32                         i.History = make(map[string][]string)
33                 }
34         } else {
35                 i.History = make(map[string][]string)
36         }
37 }
38
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 {
45                         if len(v) > 100 {
46                                 i.History[k] = v[len(i.History[k])-100:]
47                         }
48                 }
49
50                 file, err := os.Create(filepath.Join(config.ConfigDir, "buffers", "history"))
51                 if err == nil {
52                         defer file.Close()
53                         encoder := gob.NewEncoder(file)
54
55                         err = encoder.Encode(i.History)
56                         if err != nil {
57                                 i.Error("Error saving history:", err)
58                                 return
59                         }
60                 }
61         }
62 }
63
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 {
69                 return
70         }
71
72         if _, ok := i.History[ptype]; !ok {
73                 i.History[ptype] = []string{item}
74         } else {
75                 i.History[ptype] = append(i.History[ptype], item)
76
77                 // avoid duplicates
78                 h := i.History[ptype]
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:]...)
82                                 break
83                         }
84                 }
85         }
86 }
87
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 {
91                 i.HistoryNum--
92                 i.Replace(i.Start(), i.End(), history[i.HistoryNum])
93                 i.Buffer.GetActiveCursor().GotoLoc(i.End())
94         }
95 }
96
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 {
100                 i.HistoryNum++
101                 i.Replace(i.Start(), i.End(), history[i.HistoryNum])
102                 i.Buffer.GetActiveCursor().GotoLoc(i.End())
103         }
104 }