Add savehistory option

When savehistory is enabled, micro will save your command history across
sessions. This includes command-mode, shell-mode, open, jump-to-line...
Anything that uses up-arrow for history in the infobar.

This option is on by default.

Closes #874
This commit is contained in:
Zachary Yedidia 2017-10-21 15:31:04 -04:00
parent 19ee4b281e
commit 7b6430af1c
6 changed files with 61 additions and 3 deletions

View file

@ -1708,6 +1708,7 @@ func (v *View) Quit(usePlugin bool) bool {
}
screen.Fini()
messenger.SaveHistory()
os.Exit(0)
}
}
@ -1751,6 +1752,7 @@ func (v *View) QuitAll(usePlugin bool) bool {
}
screen.Fini()
messenger.SaveHistory()
os.Exit(0)
}
}

View file

@ -3,6 +3,7 @@ package main
import (
"bufio"
"bytes"
"encoding/gob"
"fmt"
"os"
"strconv"
@ -485,6 +486,55 @@ func (m *Messenger) Display() {
}
}
// LoadHistory attempts to load user history from configDir/buffers/history
// into the history map
// The savehistory option must be on
func (m *Messenger) LoadHistory() {
if GetGlobalOption("savehistory").(bool) {
file, err := os.Open(configDir + "/buffers/history")
var decodedMap map[string][]string
if err == nil {
decoder := gob.NewDecoder(file)
err = decoder.Decode(&decodedMap)
file.Close()
}
if err != nil {
m.Error("Error loading history:", err)
return
}
m.history = decodedMap
} else {
m.history = make(map[string][]string)
}
}
// SaveHistory saves the user's command history to configDir/buffers/history
// only if the savehistory option is on
func (m *Messenger) SaveHistory() {
if GetGlobalOption("savehistory").(bool) {
// Don't save history past 100
for k, v := range m.history {
if len(v) > 100 {
m.history[k] = v[0:100]
}
}
file, err := os.Create(configDir + "/buffers/history")
if err == nil {
encoder := gob.NewEncoder(file)
err = encoder.Encode(m.history)
if err != nil {
m.Error("Error saving history:", err)
return
}
file.Close()
}
}
}
// A GutterMessage is a message displayed on the side of the editor
type GutterMessage struct {
lineNum int

View file

@ -342,7 +342,7 @@ func main() {
// Create a new messenger
// This is used for sending the user messages in the bottom of the editor
messenger = new(Messenger)
messenger.history = make(map[string][]string)
messenger.LoadHistory()
// Now we load the input
buffers := LoadInput()

File diff suppressed because one or more lines are too long

View file

@ -211,6 +211,7 @@ func DefaultGlobalSettings() map[string]interface{} {
"rmtrailingws": false,
"ruler": true,
"savecursor": false,
"savehistory": true,
"saveundo": false,
"scrollmargin": float64(3),
"scrollspeed": float64(2),

View file

@ -136,6 +136,11 @@ Here are the options that you can set:
default value: `off`
* `savehistory`: remember command history between closing and re-opening
micro.
default value: `on`
* `saveundo`: when this option is on, undo is saved even after you close a file
so if you close and reopen a file, you can keep undoing.