Compare commits

..

5 Commits

Author SHA1 Message Date
Zachary Yedidia
6f302826c8 Add bracket surround 2017-09-13 10:48:53 -04:00
Zachary Yedidia
612658d9c4 Add documentation for new lua functions 2017-09-11 12:23:19 -04:00
Zachary Yedidia
c31613b2c7 Add --config-dir option 2017-09-10 23:20:21 -04:00
Zachary Yedidia
d7419d213a Merge branch 'better-lua' 2017-09-10 22:22:31 -04:00
Zachary Yedidia
67a3f86cc9 Update tcell 2017-09-10 17:21:37 -04:00
6 changed files with 125 additions and 11 deletions

View File

@@ -1852,6 +1852,68 @@ func (v *View) PreviousSplit(usePlugin bool) bool {
return false
}
func (v *View) WrapBracket(usePlugin bool) bool {
if v.Cursor.HasSelection() {
lockPollEvent = true
event := screen.PollEvent()
lockPollEvent = false
ev, ok := event.(*tcell.EventKey)
if !ok {
return false
}
if !strings.Contains("()[]{}\"'", string(ev.Rune())) {
return false
}
start := v.Cursor.CurSelection[0]
end := v.Cursor.CurSelection[1]
if start.Y != end.Y {
return false
}
close := map[string]string{
")": ")",
"]": "]",
"}": "}",
"(": ")",
"[": "]",
"{": "}",
"\"": "\"",
"'": "'",
}
open := map[string]string{
")": "(",
"]": "[",
"}": "{",
"(": "(",
"[": "[",
"{": "{",
"\"": "\"",
"'": "'",
}
if usePlugin && !PreActionCall("WrapBracket", v) {
return false
}
r := string(ev.Rune())
if start.GreaterThan(end) {
start, end = end, start
v.Buf.Insert(start, open[r])
v.Buf.Insert(end.Move(1, v.Buf), close[r])
v.Cursor.CurSelection[1] = start
} else {
v.Buf.Insert(start, open[r])
v.Buf.Insert(end.Move(1, v.Buf), close[r])
v.Cursor.CurSelection[0] = start
}
if usePlugin {
return PostActionCall("WrapBracket", v)
}
}
return false
}
var curMacro []interface{}
var recordingMacro bool

View File

@@ -69,6 +69,7 @@ var bindingActions = map[string]func(*View, bool) bool{
"PastePrimary": (*View).PastePrimary,
"SelectAll": (*View).SelectAll,
"OpenFile": (*View).OpenFile,
"WrapBracket": (*View).WrapBracket,
"Start": (*View).Start,
"End": (*View).End,
"PageUp": (*View).PageUp,
@@ -438,6 +439,7 @@ func DefaultBindings() map[string]string {
"Down": "CursorDown",
"Right": "CursorRight",
"Left": "CursorLeft",
"Alt-j": "WrapBracket",
"ShiftUp": "SelectUp",
"ShiftDown": "SelectDown",
"ShiftLeft": "SelectLeft",

View File

@@ -59,6 +59,9 @@ var (
// Event channel
events chan tcell.Event
autosave chan bool
// Read events on another thread or wait for a temporary read
lockPollEvent bool
)
// LoadInput determines which files should be loaded into buffers
@@ -143,6 +146,15 @@ func InitConfigDir() {
}
configDir = xdgHome + "/micro"
if len(*flagConfigDir) > 0 {
if _, err := os.Stat(*flagConfigDir); os.IsNotExist(err) {
TermMessage("Error: " + *flagConfigDir + " does not exist. Defaulting to " + configDir + ".")
} else {
configDir = *flagConfigDir
return
}
}
if _, err := os.Stat(xdgHome); os.IsNotExist(err) {
// If the xdgHome doesn't exist we should create it
err = os.Mkdir(xdgHome, os.ModePerm)
@@ -240,19 +252,24 @@ func LoadAll() {
// Passing -version as a flag will have micro print out the version number
var flagVersion = flag.Bool("version", false, "Show the version number and information")
var flagStartPos = flag.String("startpos", "", "LINE,COL to start the cursor at when opening a buffer.")
var flagConfigDir = flag.String("config-dir", "", "Specify a custom location for the configuration directory")
var optionFlagSet = flag.NewFlagSet("option", flag.ExitOnError)
func main() {
flag.Usage = func() {
fmt.Println("Usage: micro [OPTIONS] [FILE]...")
fmt.Print("Micro's options can be set via command line arguments for quick adjustments. For real configuration, please use the bindings.json file (see 'help options').\n\n")
flag.CommandLine.SetOutput(os.Stdout)
flag.PrintDefaults()
optionFlagSet.SetOutput(os.Stdout)
fmt.Print("\n------------------------------------------------------------------\n")
fmt.Print("Micro's options can also be set via command line arguments for quick\nadjustments. For real configuration, please use the bindings.json\nfile (see 'help options').\n\n")
optionFlagSet.PrintDefaults()
}
optionFlags := make(map[string]*string)
for k, v := range DefaultGlobalSettings() {
optionFlags[k] = flag.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'", k, v))
optionFlags[k] = optionFlagSet.String(k, "", fmt.Sprintf("The %s option. Default value: '%v'", k, v))
}
flag.Parse()
@@ -405,8 +422,9 @@ func main() {
// Here is the event loop which runs in a separate thread
go func() {
for {
if screen != nil {
if screen != nil && !lockPollEvent {
events <- screen.PollEvent()
time.Sleep(1 * time.Millisecond)
}
}
}()

File diff suppressed because one or more lines are too long

View File

@@ -147,14 +147,17 @@ The possible methods which you can call using the `messenger` variable are:
* `messenger.AddLog(msg ...interface{})`
## Note
`golang` function signatures use `.` and lua uses `:` so
Go function signatures use `.` and lua uses `:` so
```go
messenger.Message()
```
turns to
```lua
messenger:Message()
```
turns to
```lua
messenger:Message()
```
If you want a standard prompt, just use
```lua
@@ -165,9 +168,38 @@ Debug or logging your plugin can be done with below lua example code.
```lua
messenger:AddLog("Message goes here ",pluginVariableToPrintHere)
```
In Micro Editor to see your plugin logging output press `ctrl E` then type `log`
In Micro to see your plugin logging output press `CtrlE` then type `log`
A logging window will open and any logging sent from your plugin will be displayed here.
# Accessing the Go standard library
It is possible for your lua code to access many of the functions in the Go standard library.
Simply import the package you'd like and then you can use it. For example:
```lua
local ioutil = import("ioutil")
local fmt = import("fmt")
local data, err = ioutil.ReadFile("SomeFile.txt")
if err ~= nil then
messenger:Error("Error reading file: SomeFile.txt")
else
-- Data is returned as an array of bytes
-- Using Sprintf will convert it to a string
local str = fmt.Sprintf("%s", data)
-- Do something with the file you just read!
-- ...
end
```
For a full list of which packages and functions from the standard library
you can access, look at `lua.go` in the source code (it shouldn't be
too hard to look through).
# Adding help files, syntax files, or colorschemes in your plugin
You can use the `AddRuntimeFile(name, type, path string)` function to add various kinds of