micro/internal/action/command.go

760 lines
18 KiB
Go
Raw Normal View History

2019-01-02 07:29:25 +03:00
package action
import (
2019-01-15 00:52:25 +03:00
"errors"
"fmt"
2019-01-02 07:29:25 +03:00
"os"
2019-01-14 08:57:39 +03:00
"path/filepath"
2019-01-16 06:45:28 +03:00
"regexp"
2019-01-14 08:57:39 +03:00
"strconv"
"strings"
2019-01-16 06:45:28 +03:00
"unicode/utf8"
2019-01-02 07:29:25 +03:00
2019-08-04 01:19:28 +03:00
luar "layeh.com/gopher-luar"
lua "github.com/yuin/gopher-lua"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/buffer"
"github.com/zyedidia/micro/internal/config"
2019-08-04 01:19:28 +03:00
ulua "github.com/zyedidia/micro/internal/lua"
2019-02-04 07:17:24 +03:00
"github.com/zyedidia/micro/internal/screen"
"github.com/zyedidia/micro/internal/shell"
"github.com/zyedidia/micro/internal/util"
"github.com/zyedidia/micro/pkg/shellwords"
2019-01-02 07:29:25 +03:00
)
2019-01-21 01:49:20 +03:00
// A Command contains information about how to execute a command
// It has the action for that command as well as a completer function
2019-01-02 07:29:25 +03:00
type Command struct {
2019-01-21 01:49:20 +03:00
action func(*BufPane, []string)
completer buffer.Completer
2019-01-02 07:29:25 +03:00
}
var commands map[string]Command
func InitCommands() {
2019-01-21 01:49:20 +03:00
commands = map[string]Command{
2019-01-21 03:38:23 +03:00
"set": Command{(*BufPane).SetCmd, OptionValueComplete},
"reset": Command{(*BufPane).ResetCmd, OptionValueComplete},
2019-01-21 03:38:23 +03:00
"setlocal": Command{(*BufPane).SetLocalCmd, OptionValueComplete},
"show": Command{(*BufPane).ShowCmd, OptionComplete},
2019-01-21 01:49:20 +03:00
"showkey": Command{(*BufPane).ShowKeyCmd, nil},
"run": Command{(*BufPane).RunCmd, nil},
"bind": Command{(*BufPane).BindCmd, nil},
"unbind": Command{(*BufPane).UnbindCmd, nil},
"quit": Command{(*BufPane).QuitCmd, nil},
"save": Command{(*BufPane).SaveCmd, nil},
"replace": Command{(*BufPane).ReplaceCmd, nil},
"replaceall": Command{(*BufPane).ReplaceAllCmd, nil},
"vsplit": Command{(*BufPane).VSplitCmd, buffer.FileComplete},
"hsplit": Command{(*BufPane).HSplitCmd, buffer.FileComplete},
"tab": Command{(*BufPane).NewTabCmd, buffer.FileComplete},
2019-01-21 03:38:23 +03:00
"help": Command{(*BufPane).HelpCmd, HelpComplete},
2019-01-21 01:49:20 +03:00
"eval": Command{(*BufPane).EvalCmd, nil},
"togglelog": Command{(*BufPane).ToggleLogCmd, nil},
"plugin": Command{(*BufPane).PluginCmd, nil},
"reload": Command{(*BufPane).ReloadCmd, nil},
"reopen": Command{(*BufPane).ReopenCmd, nil},
2019-01-21 01:49:20 +03:00
"cd": Command{(*BufPane).CdCmd, buffer.FileComplete},
"pwd": Command{(*BufPane).PwdCmd, nil},
"open": Command{(*BufPane).OpenCmd, buffer.FileComplete},
"tabswitch": Command{(*BufPane).TabSwitchCmd, nil},
"term": Command{(*BufPane).TermCmd, nil},
"memusage": Command{(*BufPane).MemUsageCmd, nil},
"retab": Command{(*BufPane).RetabCmd, nil},
"raw": Command{(*BufPane).RawCmd, nil},
2019-01-02 07:29:25 +03:00
}
}
// MakeCommand is a function to easily create new commands
// This can be called by plugins in Lua so that plugins can define their own commands
2019-08-04 01:19:28 +03:00
func LuaMakeCommand(name, function string, completer buffer.Completer) {
action := LuaFunctionCommand(function)
commands[name] = Command{action, completer}
}
// LuaFunctionCommand returns a normal function
// so that a command can be bound to a lua function
func LuaFunctionCommand(fn string) func(*BufPane, []string) {
luaFn := strings.Split(fn, ".")
plName, plFn := luaFn[0], luaFn[1]
pl := config.FindPlugin(plName)
return func(bp *BufPane, args []string) {
var luaArgs []lua.LValue
luaArgs = append(luaArgs, luar.New(ulua.L, bp))
for _, v := range args {
luaArgs = append(luaArgs, luar.New(ulua.L, v))
}
_, err := pl.Call(plFn, luaArgs...)
if err != nil {
screen.TermMessage(err)
}
}
}
2019-01-02 07:29:25 +03:00
// CommandEditAction returns a bindable function that opens a prompt with
// the given string and executes the command when the user presses
// enter
func CommandEditAction(prompt string) BufKeyAction {
2019-01-19 23:37:59 +03:00
return func(h *BufPane) bool {
2019-01-04 01:07:28 +03:00
InfoBar.Prompt("> ", prompt, "Command", nil, func(resp string, canceled bool) {
2019-01-02 07:29:25 +03:00
if !canceled {
2019-01-11 22:49:22 +03:00
MainTab().CurPane().HandleCommand(resp)
2019-01-02 07:29:25 +03:00
}
})
return false
}
}
// CommandAction returns a bindable function which executes the
// given command
func CommandAction(cmd string) BufKeyAction {
2019-01-19 23:37:59 +03:00
return func(h *BufPane) bool {
2019-01-11 22:49:22 +03:00
MainTab().CurPane().HandleCommand(cmd)
2019-01-02 07:29:25 +03:00
return false
}
}
// PluginCmd installs, removes, updates, lists, or searches for given plugins
2019-01-19 23:37:59 +03:00
func (h *BufPane) PluginCmd(args []string) {
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// RetabCmd changes all spaces to tabs or all tabs to spaces
2019-01-02 07:29:25 +03:00
// depending on the user's settings
2019-01-19 23:37:59 +03:00
func (h *BufPane) RetabCmd(args []string) {
2019-01-15 06:44:06 +03:00
h.Buf.Retab()
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// RawCmd opens a new raw view which displays the escape sequences micro
2019-01-02 07:29:25 +03:00
// is receiving in real-time
2019-01-19 23:37:59 +03:00
func (h *BufPane) RawCmd(args []string) {
width, height := screen.Screen.Size()
iOffset := config.GetInfoBarOffset()
tp := NewTabFromPane(0, 0, width, height-iOffset, NewRawPane())
Tabs.AddTab(tp)
Tabs.SetActive(len(Tabs.List) - 1)
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// TabSwitchCmd switches to a given tab either by name or by number
2019-01-19 23:37:59 +03:00
func (h *BufPane) TabSwitchCmd(args []string) {
2019-01-14 08:57:39 +03:00
if len(args) > 0 {
num, err := strconv.Atoi(args[0])
if err != nil {
// Check for tab with this name
found := false
for i, t := range Tabs.List {
if t.Panes[t.active].Name() == args[0] {
Tabs.SetActive(i)
found = true
}
}
if !found {
InfoBar.Error("Could not find tab: ", err)
}
} else {
num--
if num >= 0 && num < len(Tabs.List) {
Tabs.SetActive(num)
} else {
InfoBar.Error("Invalid tab index")
}
}
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// CdCmd changes the current working directory
2019-01-19 23:37:59 +03:00
func (h *BufPane) CdCmd(args []string) {
2019-01-14 08:57:39 +03:00
if len(args) > 0 {
path, err := util.ReplaceHome(args[0])
if err != nil {
InfoBar.Error(err)
return
}
err = os.Chdir(path)
if err != nil {
InfoBar.Error(err)
return
}
wd, _ := os.Getwd()
for _, b := range buffer.OpenBuffers {
if len(b.Path) > 0 {
b.Path, _ = util.MakeRelative(b.AbsPath, wd)
if p, _ := filepath.Abs(b.Path); !strings.Contains(p, wd) {
b.Path = b.AbsPath
}
}
}
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// MemUsageCmd prints micro's memory usage
2019-01-02 07:29:25 +03:00
// Alloc shows how many bytes are currently in use
// Sys shows how many bytes have been requested from the operating system
// NumGC shows how many times the GC has been run
// Note that Go commonly reserves more memory from the OS than is currently in-use/required
// Additionally, even if Go returns memory to the OS, the OS does not always claim it because
// there may be plenty of memory to spare
2019-01-19 23:37:59 +03:00
func (h *BufPane) MemUsageCmd(args []string) {
2019-01-02 07:29:25 +03:00
InfoBar.Message(util.GetMemStats())
}
2019-01-11 22:49:22 +03:00
// PwdCmd prints the current working directory
2019-01-19 23:37:59 +03:00
func (h *BufPane) PwdCmd(args []string) {
2019-01-02 07:29:25 +03:00
wd, err := os.Getwd()
if err != nil {
InfoBar.Message(err.Error())
} else {
InfoBar.Message(wd)
}
}
2019-01-11 22:49:22 +03:00
// OpenCmd opens a new buffer with a given filename
2019-01-19 23:37:59 +03:00
func (h *BufPane) OpenCmd(args []string) {
2019-01-14 08:57:39 +03:00
if len(args) > 0 {
filename := args[0]
// the filename might or might not be quoted, so unquote first then join the strings.
args, err := shellwords.Split(filename)
if err != nil {
InfoBar.Error("Error parsing args ", err)
return
}
filename = strings.Join(args, " ")
open := func() {
b, err := buffer.NewBufferFromFile(filename, buffer.BTDefault)
if err != nil {
InfoBar.Error(err)
return
}
h.OpenBuffer(b)
}
if h.Buf.Modified() {
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
if !canceled && !yes {
open()
} else if !canceled && yes {
h.Save()
open()
}
})
} else {
open()
}
} else {
InfoBar.Error("No filename")
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// ToggleLogCmd toggles the log view
2019-01-19 23:37:59 +03:00
func (h *BufPane) ToggleLogCmd(args []string) {
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// ReloadCmd reloads all files (syntax files, colorschemes...)
2019-01-19 23:37:59 +03:00
func (h *BufPane) ReloadCmd(args []string) {
2019-01-02 07:29:25 +03:00
}
// ReopenCmd reopens the buffer (reload from disk)
func (h *BufPane) ReopenCmd(args []string) {
if h.Buf.Modified() {
InfoBar.YNPrompt("Save file before reopen?", func(yes, canceled bool) {
if !canceled && yes {
h.Save()
h.Buf.ReOpen()
} else if !canceled {
h.Buf.ReOpen()
}
})
} else {
h.Buf.ReOpen()
}
}
2019-01-19 23:37:59 +03:00
func (h *BufPane) openHelp(page string) error {
2019-01-15 00:52:25 +03:00
if data, err := config.FindRuntimeFile(config.RTHelp, page).Data(); err != nil {
return errors.New(fmt.Sprint("Unable to load help text", page, "\n", err))
} else {
helpBuffer := buffer.NewBufferFromString(string(data), page+".md", buffer.BTHelp)
helpBuffer.SetName("Help " + page)
if h.Buf.Type == buffer.BTHelp {
h.OpenBuffer(helpBuffer)
} else {
h.HSplitBuf(helpBuffer)
}
}
return nil
}
2019-01-11 22:49:22 +03:00
// HelpCmd tries to open the given help page in a horizontal split
2019-01-19 23:37:59 +03:00
func (h *BufPane) HelpCmd(args []string) {
2019-01-15 00:52:25 +03:00
if len(args) < 1 {
// Open the default help if the user just typed "> help"
h.openHelp("help")
} else {
if config.FindRuntimeFile(config.RTHelp, args[0]) != nil {
err := h.openHelp(args[0])
if err != nil {
InfoBar.Error(err)
}
} else {
InfoBar.Error("Sorry, no help for ", args[0])
}
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// VSplitCmd opens a vertical split with file given in the first argument
2019-01-02 07:29:25 +03:00
// If no file is given, it opens an empty buffer in a new split
2019-01-19 23:37:59 +03:00
func (h *BufPane) VSplitCmd(args []string) {
2019-01-14 02:18:23 +03:00
if len(args) == 0 {
// Open an empty vertical split
h.VSplitAction()
return
}
2019-01-05 01:40:56 +03:00
buf, err := buffer.NewBufferFromFile(args[0], buffer.BTDefault)
if err != nil {
InfoBar.Error(err)
return
}
2019-01-14 02:18:23 +03:00
h.VSplitBuf(buf)
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// HSplitCmd opens a horizontal split with file given in the first argument
2019-01-02 07:29:25 +03:00
// If no file is given, it opens an empty buffer in a new split
2019-01-19 23:37:59 +03:00
func (h *BufPane) HSplitCmd(args []string) {
2019-01-14 02:18:23 +03:00
if len(args) == 0 {
// Open an empty horizontal split
h.HSplitAction()
return
}
2019-01-05 01:40:56 +03:00
buf, err := buffer.NewBufferFromFile(args[0], buffer.BTDefault)
if err != nil {
InfoBar.Error(err)
return
}
2019-01-14 02:18:23 +03:00
h.HSplitBuf(buf)
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// EvalCmd evaluates a lua expression
2019-01-19 23:37:59 +03:00
func (h *BufPane) EvalCmd(args []string) {
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// NewTabCmd opens the given file in a new tab
2019-01-19 23:37:59 +03:00
func (h *BufPane) NewTabCmd(args []string) {
2019-01-11 22:49:22 +03:00
width, height := screen.Screen.Size()
2019-01-15 06:16:44 +03:00
iOffset := config.GetInfoBarOffset()
2019-01-11 22:49:22 +03:00
if len(args) > 0 {
for _, a := range args {
b, err := buffer.NewBufferFromFile(a, buffer.BTDefault)
if err != nil {
InfoBar.Error(err)
return
}
2019-01-15 06:16:44 +03:00
tp := NewTabFromBuffer(0, 0, width, height-1-iOffset, b)
2019-01-11 22:49:22 +03:00
Tabs.AddTab(tp)
Tabs.SetActive(len(Tabs.List) - 1)
}
} else {
b := buffer.NewBufferFromString("", "", buffer.BTDefault)
2019-01-15 06:16:44 +03:00
tp := NewTabFromBuffer(0, 0, width, height-iOffset, b)
2019-01-11 22:49:22 +03:00
Tabs.AddTab(tp)
Tabs.SetActive(len(Tabs.List) - 1)
}
2019-01-02 07:29:25 +03:00
}
func SetGlobalOptionNative(option string, nativeValue interface{}) error {
2019-01-14 05:06:58 +03:00
config.GlobalSettings[option] = nativeValue
if option == "colorscheme" {
// LoadSyntaxFiles()
config.InitColorscheme()
for _, b := range buffer.OpenBuffers {
b.UpdateRules()
}
2019-06-15 23:13:04 +03:00
} else if option == "infobar" || option == "keymenu" {
2019-01-15 06:16:44 +03:00
Tabs.Resize()
2019-06-15 23:13:04 +03:00
} else if option == "mouse" {
2019-01-14 05:06:58 +03:00
if !nativeValue.(bool) {
screen.Screen.DisableMouse()
} else {
screen.Screen.EnableMouse()
}
} else {
for _, pl := range config.Plugins {
2019-08-03 09:46:25 +03:00
if option == pl.Name {
if nativeValue.(bool) && !pl.Loaded {
pl.Load()
pl.Call("init")
} else if !nativeValue.(bool) && pl.Loaded {
pl.Call("deinit")
}
}
}
2019-01-14 05:06:58 +03:00
}
for _, b := range buffer.OpenBuffers {
b.SetOptionNative(option, nativeValue)
2019-01-14 05:06:58 +03:00
}
return config.WriteSettings(config.ConfigDir + "/settings.json")
}
2019-01-14 05:06:58 +03:00
func SetGlobalOption(option, value string) error {
if _, ok := config.GlobalSettings[option]; !ok {
return config.ErrInvalidOption
}
nativeValue, err := config.GetNativeValue(option, config.GlobalSettings[option], value)
if err != nil {
return err
}
return SetGlobalOptionNative(option, nativeValue)
}
// ResetCmd resets a setting to its default value
func (h *BufPane) ResetCmd(args []string) {
if len(args) < 1 {
InfoBar.Error("Not enough arguments")
return
}
option := args[0]
defaultGlobals := config.DefaultGlobalSettings()
defaultLocals := config.DefaultLocalSettings()
if _, ok := defaultGlobals[option]; ok {
SetGlobalOptionNative(option, defaultGlobals[option])
return
}
if _, ok := defaultLocals[option]; ok {
h.Buf.SetOptionNative(option, defaultLocals[option])
return
}
InfoBar.Error(config.ErrInvalidOption)
2019-01-14 05:06:58 +03:00
}
2019-01-11 22:49:22 +03:00
// SetCmd sets an option
2019-01-19 23:37:59 +03:00
func (h *BufPane) SetCmd(args []string) {
2019-01-14 05:06:58 +03:00
if len(args) < 2 {
InfoBar.Error("Not enough arguments")
return
}
option := args[0]
value := args[1]
err := SetGlobalOption(option, value)
if err == config.ErrInvalidOption {
err := h.Buf.SetOption(option, value)
if err != nil {
InfoBar.Error(err)
}
} else if err != nil {
InfoBar.Error(err)
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// SetLocalCmd sets an option local to the buffer
2019-01-19 23:37:59 +03:00
func (h *BufPane) SetLocalCmd(args []string) {
2019-01-14 05:06:58 +03:00
if len(args) < 2 {
InfoBar.Error("Not enough arguments")
return
}
option := args[0]
value := args[1]
err := h.Buf.SetOption(option, value)
if err != nil {
InfoBar.Error(err)
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// ShowCmd shows the value of the given option
2019-01-19 23:37:59 +03:00
func (h *BufPane) ShowCmd(args []string) {
2019-01-14 08:57:39 +03:00
if len(args) < 1 {
InfoBar.Error("Please provide an option to show")
return
}
var option interface{}
if opt, ok := h.Buf.Settings[args[0]]; ok {
option = opt
} else if opt, ok := config.GlobalSettings[args[0]]; ok {
option = opt
}
if option == nil {
InfoBar.Error(args[0], " is not a valid option")
return
}
InfoBar.Message(option)
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// ShowKeyCmd displays the action that a key is bound to
2019-01-19 23:37:59 +03:00
func (h *BufPane) ShowKeyCmd(args []string) {
2019-01-09 23:17:51 +03:00
if len(args) < 1 {
InfoBar.Error("Please provide a key to show")
return
}
2019-01-11 23:33:16 +03:00
if action, ok := config.Bindings[args[0]]; ok {
2019-01-09 23:17:51 +03:00
InfoBar.Message(action)
} else {
InfoBar.Message(args[0], " has no binding")
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// BindCmd creates a new keybinding
2019-01-19 23:37:59 +03:00
func (h *BufPane) BindCmd(args []string) {
2019-01-15 00:09:46 +03:00
if len(args) < 2 {
InfoBar.Error("Not enough arguments")
return
}
_, err := TryBindKey(args[0], args[1], true)
if err != nil {
InfoBar.Error(err)
}
}
// UnbindCmd binds a key to its default action
2019-01-19 23:37:59 +03:00
func (h *BufPane) UnbindCmd(args []string) {
2019-01-15 00:09:46 +03:00
if len(args) < 1 {
InfoBar.Error("Not enough arguements")
return
}
err := UnbindKey(args[0])
if err != nil {
InfoBar.Error(err)
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// RunCmd runs a shell command in the background
2019-01-19 23:37:59 +03:00
func (h *BufPane) RunCmd(args []string) {
2019-01-11 00:37:05 +03:00
runf, err := shell.RunBackgroundShell(shellwords.Join(args...))
if err != nil {
InfoBar.Error(err)
} else {
go func() {
InfoBar.Message(runf())
screen.Redraw()
}()
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// QuitCmd closes the main view
2019-01-19 23:37:59 +03:00
func (h *BufPane) QuitCmd(args []string) {
2019-01-14 08:57:39 +03:00
h.Quit()
2019-01-02 07:29:25 +03:00
}
// SaveCmd saves the buffer optionally with an argument file name
2019-01-19 23:37:59 +03:00
func (h *BufPane) SaveCmd(args []string) {
if len(args) == 0 {
h.Save()
} else {
h.Buf.SaveAs(args[0])
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// ReplaceCmd runs search and replace
2019-01-19 23:37:59 +03:00
func (h *BufPane) ReplaceCmd(args []string) {
2019-01-16 06:45:28 +03:00
if len(args) < 2 || len(args) > 4 {
// We need to find both a search and replace expression
InfoBar.Error("Invalid replace statement: " + strings.Join(args, " "))
return
}
all := false
noRegex := false
if len(args) > 2 {
for _, arg := range args[2:] {
switch arg {
case "-a":
all = true
case "-l":
noRegex = true
default:
InfoBar.Error("Invalid flag: " + arg)
return
}
}
}
search := args[0]
if noRegex {
search = regexp.QuoteMeta(search)
}
replace := []byte(args[1])
replaceStr := args[1]
2019-01-16 06:45:28 +03:00
var regex *regexp.Regexp
var err error
if h.Buf.Settings["ignorecase"].(bool) {
regex, err = regexp.Compile("(?im)" + search)
} else {
regex, err = regexp.Compile("(?m)" + search)
}
if err != nil {
// There was an error with the user's regex
InfoBar.Error(err)
return
}
nreplaced := 0
start := h.Buf.Start()
end := h.Buf.End()
if h.Cursor.HasSelection() {
start = h.Cursor.CurSelection[0]
end = h.Cursor.CurSelection[1]
}
if all {
nreplaced = h.Buf.ReplaceRegex(start, end, regex, replace)
} else {
inRange := func(l buffer.Loc) bool {
return l.GreaterEqual(start) && l.LessThan(end)
}
searchLoc := start
searching := true
var doReplacement func()
doReplacement = func() {
locs, found, err := h.Buf.FindNext(search, start, end, searchLoc, true, !noRegex)
if err != nil {
InfoBar.Error(err)
return
}
if !found || !inRange(locs[0]) || !inRange(locs[1]) {
h.Cursor.ResetSelection()
2019-01-25 02:25:59 +03:00
h.Buf.RelocateCursors()
2019-01-16 06:45:28 +03:00
return
}
h.Cursor.SetSelectionStart(locs[0])
h.Cursor.SetSelectionEnd(locs[1])
InfoBar.YNPrompt("Perform replacement (y,n,esc)", func(yes, canceled bool) {
if !canceled && yes {
h.Buf.Replace(locs[0], locs[1], replaceStr)
2019-01-16 06:45:28 +03:00
searchLoc = locs[0]
searchLoc.X += utf8.RuneCount(replace)
h.Cursor.Loc = searchLoc
nreplaced++
} else if !canceled && !yes {
searchLoc = locs[0]
searchLoc.X += utf8.RuneCount(replace)
} else if canceled {
h.Cursor.ResetSelection()
2019-01-25 02:25:59 +03:00
h.Buf.RelocateCursors()
2019-01-16 06:45:28 +03:00
return
}
if searching {
doReplacement()
}
})
}
doReplacement()
}
2019-01-25 02:25:59 +03:00
h.Buf.RelocateCursors()
2019-01-16 06:45:28 +03:00
if nreplaced > 1 {
InfoBar.Message("Replaced ", nreplaced, " occurrences of ", search)
} else if nreplaced == 1 {
InfoBar.Message("Replaced ", nreplaced, " occurrence of ", search)
} else {
InfoBar.Message("Nothing matched ", search)
}
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// ReplaceAllCmd replaces search term all at once
2019-01-19 23:37:59 +03:00
func (h *BufPane) ReplaceAllCmd(args []string) {
2019-01-19 23:53:02 +03:00
// aliased to Replace command
h.ReplaceCmd(append(args, "-a"))
2019-01-02 07:29:25 +03:00
}
2019-01-11 22:49:22 +03:00
// TermCmd opens a terminal in the current view
2019-01-19 23:37:59 +03:00
func (h *BufPane) TermCmd(args []string) {
2019-01-11 22:49:22 +03:00
ps := MainTab().Panes
2019-01-13 06:58:16 +03:00
if len(args) == 0 {
sh := os.Getenv("SHELL")
if sh == "" {
InfoBar.Error("Shell environment not found")
return
}
args = []string{sh}
}
2019-01-15 08:24:53 +03:00
term := func(i int, newtab bool) {
2019-01-14 02:18:23 +03:00
2019-01-11 22:49:22 +03:00
t := new(shell.Terminal)
t.Start(args, false, true)
2019-01-14 02:18:23 +03:00
id := h.ID()
if newtab {
h.AddTab()
i = 0
id = MainTab().Panes[0].ID()
2019-01-14 05:06:58 +03:00
} else {
MainTab().Panes[i].Close()
2019-01-14 02:18:23 +03:00
}
v := h.GetView()
2019-01-19 23:37:59 +03:00
MainTab().Panes[i] = NewTermPane(v.X, v.Y, v.Width, v.Height, t, id)
2019-01-11 22:49:22 +03:00
MainTab().SetActive(i)
}
2019-01-15 08:24:53 +03:00
// If there is only one open file we make a new tab instead of overwriting it
newtab := len(MainTab().Panes) == 1 && len(Tabs.List) == 1
if newtab {
term(0, true)
return
}
2019-01-11 22:49:22 +03:00
for i, p := range ps {
if p.ID() == h.ID() {
if h.Buf.Modified() {
InfoBar.YNPrompt("Save changes to "+h.Buf.GetName()+" before closing? (y,n,esc)", func(yes, canceled bool) {
if !canceled && !yes {
2019-01-15 08:24:53 +03:00
term(i, false)
2019-01-11 22:49:22 +03:00
} else if !canceled && yes {
h.Save()
2019-01-15 08:24:53 +03:00
term(i, false)
2019-01-11 22:49:22 +03:00
}
})
} else {
2019-01-15 08:24:53 +03:00
term(i, false)
2019-01-11 22:49:22 +03:00
}
}
}
2019-01-02 07:29:25 +03:00
}
// HandleCommand handles input from the user
2019-01-19 23:37:59 +03:00
func (h *BufPane) HandleCommand(input string) {
2019-01-02 07:29:25 +03:00
args, err := shellwords.Split(input)
if err != nil {
InfoBar.Error("Error parsing args ", err)
return
}
inputCmd := args[0]
if _, ok := commands[inputCmd]; !ok {
InfoBar.Error("Unknown command ", inputCmd)
} else {
2019-01-11 22:49:22 +03:00
commands[inputCmd].action(h, args[1:])
2019-01-02 07:29:25 +03:00
}
}