Merge remote-tracking branch 'zyedidia/master' into pm

This commit is contained in:
boombuler
2016-09-27 20:57:49 +02:00
9 changed files with 82 additions and 26 deletions

View File

@@ -711,7 +711,7 @@ func (v *View) Save(usePlugin bool) bool {
return false
}
if v.Help {
if v.Type == vtHelp {
// We can't save the help text
return false
}
@@ -1258,7 +1258,7 @@ func (v *View) ToggleHelp(usePlugin bool) bool {
return false
}
if !v.Help {
if v.Type != vtHelp {
// Open the default help
v.openHelp("help")
} else {

View File

@@ -26,20 +26,21 @@ type StrCommand struct {
var commands map[string]Command
var commandActions = map[string]func([]string){
"Set": Set,
"SetLocal": SetLocal,
"Show": Show,
"Run": Run,
"Bind": Bind,
"Quit": Quit,
"Save": Save,
"Replace": Replace,
"VSplit": VSplit,
"HSplit": HSplit,
"Tab": NewTab,
"Help": Help,
"Eval": Eval,
"Plugin": PluginCmd,
"Set": Set,
"SetLocal": SetLocal,
"Show": Show,
"Run": Run,
"Bind": Bind,
"Quit": Quit,
"Save": Save,
"Replace": Replace,
"VSplit": VSplit,
"HSplit": HSplit,
"Tab": NewTab,
"Help": Help,
"Eval": Eval,
"ToggleLog": ToggleLog,
"Plugin": PluginCmd,
}
// InitCommands initializes the default commands
@@ -85,6 +86,7 @@ func DefaultCommands() map[string]StrCommand {
"tab": {"Tab", []Completion{FileCompletion, NoCompletion}},
"help": {"Help", []Completion{HelpCompletion, NoCompletion}},
"eval": {"Eval", []Completion{NoCompletion}},
"log": {"ToggleLog", []Completion{NoCompletion}},
"plugin": {"Plugin", []Completion{NoCompletion}},
}
}
@@ -122,6 +124,16 @@ func PluginCmd(args []string) {
}
}
func ToggleLog(args []string) {
buffer := messenger.getBuffer()
if CurView().Type != vtLog {
CurView().HSplit(buffer)
CurView().Type = vtLog
} else {
CurView().Quit(true)
}
}
// Help tries to open the given help page in a horizontal split
func Help(args []string) {
if len(args) < 1 {

View File

@@ -24,6 +24,7 @@ func TermMessage(msg ...interface{}) {
}
fmt.Println(msg...)
messenger.AddLog(fmt.Sprint(msg...))
fmt.Print("\nPress enter to continue")
reader := bufio.NewReader(os.Stdin)
@@ -43,6 +44,7 @@ func TermError(filename string, lineNum int, err string) {
// Messenger is an object that makes it easy to send messages to the user
// and get input from the user
type Messenger struct {
log *Buffer
// Are we currently prompting the user?
hasPrompt bool
// Is there a message to print
@@ -67,16 +69,30 @@ type Messenger struct {
gutterMessage bool
}
func (m *Messenger) AddLog(msg string) {
buffer := m.getBuffer()
buffer.Insert(buffer.End(), msg+"\n")
buffer.Cursor.Loc = buffer.End()
buffer.Cursor.Relocate()
}
func (m *Messenger) getBuffer() *Buffer {
if m.log == nil {
m.log = NewBuffer([]byte{}, "")
m.log.Name = "Log"
}
return m.log
}
// Message sends a message to the user
func (m *Messenger) Message(msg ...interface{}) {
buf := new(bytes.Buffer)
fmt.Fprint(buf, msg...)
m.message = buf.String()
m.message = fmt.Sprint(msg...)
m.style = defStyle
if _, ok := colorscheme["message"]; ok {
m.style = colorscheme["message"]
}
m.AddLog(m.message)
m.hasMessage = true
}
@@ -92,6 +108,7 @@ func (m *Messenger) Error(msg ...interface{}) {
if _, ok := colorscheme["error-message"]; ok {
m.style = colorscheme["error-message"]
}
m.AddLog(m.message)
m.hasMessage = true
}
@@ -113,13 +130,16 @@ func (m *Messenger) YesNoPrompt(prompt string) (bool, bool) {
switch e.Key() {
case tcell.KeyRune:
if e.Rune() == 'y' {
m.AddLog("\t--> y")
m.hasPrompt = false
return true, false
} else if e.Rune() == 'n' {
m.AddLog("\t--> n")
m.hasPrompt = false
return false, false
}
case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
m.AddLog("\t--> (cancel)")
m.hasPrompt = false
return false, true
}
@@ -146,6 +166,7 @@ func (m *Messenger) LetterPrompt(prompt string, responses ...rune) (rune, bool)
case tcell.KeyRune:
for _, r := range responses {
if e.Rune() == r {
m.AddLog("\t--> " + string(r))
m.Clear()
m.Reset()
m.hasPrompt = false
@@ -153,6 +174,7 @@ func (m *Messenger) LetterPrompt(prompt string, responses ...rune) (rune, bool)
}
}
case tcell.KeyCtrlC, tcell.KeyCtrlQ, tcell.KeyEscape:
m.AddLog("\t--> (cancel)")
m.Clear()
m.Reset()
m.hasPrompt = false
@@ -198,9 +220,11 @@ func (m *Messenger) Prompt(prompt, historyType string, completionTypes ...Comple
switch e.Key() {
case tcell.KeyCtrlQ, tcell.KeyCtrlC, tcell.KeyEscape:
// Cancel
m.AddLog("\t--> (cancel)")
m.hasPrompt = false
case tcell.KeyEnter:
// User is done entering their response
m.AddLog("\t--> " + m.response)
m.hasPrompt = false
response, canceled = m.response, false
m.history[historyType][len(m.history[historyType])-1] = response

View File

@@ -326,6 +326,7 @@ func main() {
L.SetGlobal("ReadRuntimeFile", luar.New(L, PluginReadRuntimeFile))
L.SetGlobal("ListRuntimeFiles", luar.New(L, PluginListRuntimeFiles))
L.SetGlobal("AddRuntimeFile", luar.New(L, PluginAddRuntimeFile))
L.SetGlobal("AddRuntimeFilesFromDirectory", luar.New(L, PluginAddRuntimeFilesFromDirectory))
jobs = make(chan JobFunction, 100)
events = make(chan tcell.Event, 100)

File diff suppressed because one or more lines are too long

View File

@@ -37,7 +37,7 @@ func (sline *Statusline) Display() {
file += " " + sline.view.Buf.FileType()
rightText := helpBinding + " for help "
if sline.view.Help {
if sline.view.Type == vtHelp {
rightText = helpBinding + " to close help "
}

View File

@@ -11,6 +11,14 @@ import (
"github.com/zyedidia/tcell"
)
type ViewType int
const (
vtDefault ViewType = iota
vtHelp
vtLog
)
// The View struct stores information about a view into a buffer.
// It stores information about the cursor, and the viewport
// that the user sees the buffer from.
@@ -28,7 +36,7 @@ type View struct {
heightPercent int
// Specifies whether or not this view holds a help buffer
Help bool
Type ViewType
// Actual with and height
width int
@@ -185,7 +193,7 @@ func (v *View) ScrollDown(n int) {
// If there are unsaved changes, the user will be asked if the view can be closed
// causing them to lose the unsaved changes
func (v *View) CanClose() bool {
if v.Buf.IsModified {
if v.Type == vtDefault && v.Buf.IsModified {
char, canceled := messenger.LetterPrompt("Save changes to "+v.Buf.Name+" before closing? (y,n,esc) ", 'y', 'n')
if !canceled {
if char == 'y' {
@@ -536,11 +544,11 @@ func (v *View) openHelp(helpPage string) {
helpBuffer := NewBuffer(data, helpPage+".md")
helpBuffer.Name = "Help"
if v.Help {
if v.Type == vtHelp {
v.OpenBuffer(helpBuffer)
} else {
v.HSplit(helpBuffer)
CurView().Help = true
CurView().Type = vtHelp
}
}
}
@@ -553,9 +561,15 @@ func (v *View) drawCell(x, y int, ch rune, combc []rune, style tcell.Style) {
// DisplayView renders the view to the screen
func (v *View) DisplayView() {
if v.Type == vtLog {
// Log views should always follow the cursor...
v.Relocate()
}
if v.Buf.Settings["syntax"].(bool) {
v.matches = Match(v)
}
// The charNum we are currently displaying
// starts at the start of the viewport
charNum := Loc{0, v.Topline}

View File

@@ -42,6 +42,8 @@ Here are the possible commands that you can use.
* `tab filename`: opens the given file in a new tab.
* `log`: opens a log of all messages and debug statements
---
The following commands are provided by the default plugins:

View File

@@ -127,6 +127,9 @@ called `test`, you would create the `test.md` file for example, and runt the fun
AddRuntimeFile("test", "help", "test.md")
```
Use `AddRuntimeFilesFromDirectory(name, type, dir, pattern)` to add a number of files
to the runtime.
# Autocomplete command arguments
See this example to learn how to use `MakeCompletion` and `MakeCommand`