| 1 |
package action |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"os" |
| 8 |
"os/exec" |
| 9 |
"path/filepath" |
| 10 |
"reflect" |
| 11 |
"regexp" |
| 12 |
"strconv" |
| 13 |
"strings" |
| 14 |
|
| 15 |
shellquote "github.com/kballard/go-shellquote" |
| 16 |
"github.com/zyedidia/micro/v2/internal/buffer" |
| 17 |
"github.com/zyedidia/micro/v2/internal/clipboard" |
| 18 |
"github.com/zyedidia/micro/v2/internal/config" |
| 19 |
"github.com/zyedidia/micro/v2/internal/screen" |
| 20 |
"github.com/zyedidia/micro/v2/internal/shell" |
| 21 |
"github.com/zyedidia/micro/v2/internal/util" |
| 22 |
) |
| 23 |
|
| 24 |
// A Command contains information about how to execute a command |
| 25 |
// It has the action for that command as well as a completer function |
| 26 |
type Command struct { |
| 27 |
action func(*BufPane, []string) |
| 28 |
completer buffer.Completer |
| 29 |
} |
| 30 |
|
| 31 |
var commands map[string]Command |
| 32 |
|
| 33 |
func InitCommands() { |
| 34 |
commands = map[string]Command{ |
| 35 |
"set": {(*BufPane).SetCmd, OptionValueComplete}, |
| 36 |
"setlocal": {(*BufPane).SetLocalCmd, OptionValueComplete}, |
| 37 |
"toggle": {(*BufPane).ToggleCmd, OptionValueComplete}, |
| 38 |
"togglelocal": {(*BufPane).ToggleLocalCmd, OptionValueComplete}, |
| 39 |
"reset": {(*BufPane).ResetCmd, OptionValueComplete}, |
| 40 |
"show": {(*BufPane).ShowCmd, OptionComplete}, |
| 41 |
"showkey": {(*BufPane).ShowKeyCmd, nil}, |
| 42 |
"run": {(*BufPane).RunCmd, nil}, |
| 43 |
"bind": {(*BufPane).BindCmd, nil}, |
| 44 |
"unbind": {(*BufPane).UnbindCmd, nil}, |
| 45 |
"quit": {(*BufPane).QuitCmd, nil}, |
| 46 |
"goto": {(*BufPane).GotoCmd, nil}, |
| 47 |
"jump": {(*BufPane).JumpCmd, nil}, |
| 48 |
"save": {(*BufPane).SaveCmd, nil}, |
| 49 |
"replace": {(*BufPane).ReplaceCmd, nil}, |
| 50 |
"replaceall": {(*BufPane).ReplaceAllCmd, nil}, |
| 51 |
"vsplit": {(*BufPane).VSplitCmd, buffer.FileComplete}, |
| 52 |
"hsplit": {(*BufPane).HSplitCmd, buffer.FileComplete}, |
| 53 |
"tab": {(*BufPane).NewTabCmd, buffer.FileComplete}, |
| 54 |
"help": {(*BufPane).HelpCmd, HelpComplete}, |
| 55 |
"eval": {(*BufPane).EvalCmd, nil}, |
| 56 |
"log": {(*BufPane).ToggleLogCmd, nil}, |
| 57 |
"plugin": {(*BufPane).PluginCmd, PluginComplete}, |
| 58 |
"reload": {(*BufPane).ReloadCmd, nil}, |
| 59 |
"reopen": {(*BufPane).ReopenCmd, nil}, |
| 60 |
"cd": {(*BufPane).CdCmd, buffer.FileComplete}, |
| 61 |
"pwd": {(*BufPane).PwdCmd, nil}, |
| 62 |
"open": {(*BufPane).OpenCmd, buffer.FileComplete}, |
| 63 |
"tabmove": {(*BufPane).TabMoveCmd, nil}, |
| 64 |
"tabswitch": {(*BufPane).TabSwitchCmd, nil}, |
| 65 |
"term": {(*BufPane).TermCmd, nil}, |
| 66 |
"memusage": {(*BufPane).MemUsageCmd, nil}, |
| 67 |
"retab": {(*BufPane).RetabCmd, nil}, |
| 68 |
"raw": {(*BufPane).RawCmd, nil}, |
| 69 |
"textfilter": {(*BufPane).TextFilterCmd, nil}, |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
// MakeCommand is a function to easily create new commands |
| 74 |
// This can be called by plugins in Lua so that plugins can define their own commands |
| 75 |
func MakeCommand(name string, action func(bp *BufPane, args []string), completer buffer.Completer) { |
| 76 |
if action != nil { |
| 77 |
commands[name] = Command{action, completer} |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
// CommandEditAction returns a bindable function that opens a prompt with |
| 82 |
// the given string and executes the command when the user presses |
| 83 |
// enter |
| 84 |
func CommandEditAction(prompt string) BufKeyAction { |
| 85 |
return func(h *BufPane) bool { |
| 86 |
InfoBar.Prompt("> ", prompt, "Command", nil, func(resp string, canceled bool) { |
| 87 |
if !canceled { |
| 88 |
MainTab().CurPane().HandleCommand(resp) |
| 89 |
} |
| 90 |
}) |
| 91 |
return false |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
// CommandAction returns a bindable function which executes the |
| 96 |
// given command |
| 97 |
func CommandAction(cmd string) BufKeyAction { |
| 98 |
return func(h *BufPane) bool { |
| 99 |
MainTab().CurPane().HandleCommand(cmd) |
| 100 |
return false |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
var PluginCmds = []string{"install", "remove", "update", "available", "list", "search"} |
| 105 |
|
| 106 |
// PluginCmd installs, removes, updates, lists, or searches for given plugins |
| 107 |
func (h *BufPane) PluginCmd(args []string) { |
| 108 |
if len(args) < 1 { |
| 109 |
InfoBar.Error("Not enough arguments") |
| 110 |
return |
| 111 |
} |
| 112 |
|
| 113 |
if h.Buf.Type != buffer.BTLog { |
| 114 |
h.OpenLogBuf() |
| 115 |
} |
| 116 |
|
| 117 |
config.PluginCommand(buffer.LogBuf, args[0], args[1:]) |
| 118 |
} |
| 119 |
|
| 120 |
// RetabCmd changes all spaces to tabs or all tabs to spaces |
| 121 |
// depending on the user's settings |
| 122 |
func (h *BufPane) RetabCmd(args []string) { |
| 123 |
h.Buf.Retab() |
| 124 |
} |
| 125 |
|
| 126 |
// RawCmd opens a new raw view which displays the escape sequences micro |
| 127 |
// is receiving in real-time |
| 128 |
func (h *BufPane) RawCmd(args []string) { |
| 129 |
width, height := screen.Screen.Size() |
| 130 |
iOffset := config.GetInfoBarOffset() |
| 131 |
tp := NewTabFromPane(0, 0, width, height-iOffset, NewRawPane(nil)) |
| 132 |
Tabs.AddTab(tp) |
| 133 |
Tabs.SetActive(len(Tabs.List) - 1) |
| 134 |
} |
| 135 |
|
| 136 |
// TextFilterCmd filters the selection through the command. |
| 137 |
// Selection goes to the command input. |
| 138 |
// On successful run command output replaces the current selection. |
| 139 |
func (h *BufPane) TextFilterCmd(args []string) { |
| 140 |
if len(args) == 0 { |
| 141 |
InfoBar.Error("usage: textfilter arguments") |
| 142 |
return |
| 143 |
} |
| 144 |
for _, c := range h.Buf.GetCursors() { |
| 145 |
sel := c.GetSelection() |
| 146 |
if len(sel) == 0 { |
| 147 |
c.SelectWord() |
| 148 |
sel = c.GetSelection() |
| 149 |
} |
| 150 |
var bout, berr bytes.Buffer |
| 151 |
cmd := exec.Command(args[0], args[1:]...) |
| 152 |
cmd.Stdin = strings.NewReader(string(sel)) |
| 153 |
cmd.Stderr = &berr |
| 154 |
cmd.Stdout = &bout |
| 155 |
err := cmd.Run() |
| 156 |
if err != nil { |
| 157 |
InfoBar.Error(err.Error() + " " + berr.String()) |
| 158 |
return |
| 159 |
} |
| 160 |
c.DeleteSelection() |
| 161 |
h.Buf.Insert(c.Loc, bout.String()) |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
// TabMoveCmd moves the current tab to a given index (starts at 1). The |
| 166 |
// displaced tabs are moved up. |
| 167 |
func (h *BufPane) TabMoveCmd(args []string) { |
| 168 |
if len(args) <= 0 { |
| 169 |
InfoBar.Error("Not enough arguments: provide an index, starting at 1") |
| 170 |
return |
| 171 |
} |
| 172 |
|
| 173 |
if len(args[0]) <= 0 { |
| 174 |
InfoBar.Error("Invalid argument: empty string") |
| 175 |
return |
| 176 |
} |
| 177 |
|
| 178 |
num, err := strconv.Atoi(args[0]) |
| 179 |
if err != nil { |
| 180 |
InfoBar.Error("Invalid argument: ", err) |
| 181 |
return |
| 182 |
} |
| 183 |
|
| 184 |
// Preserve sign for relative move, if one exists |
| 185 |
var shiftDirection byte |
| 186 |
if strings.Contains("-+", string([]byte{args[0][0]})) { |
| 187 |
shiftDirection = args[0][0] |
| 188 |
} |
| 189 |
|
| 190 |
// Relative positions -> absolute positions |
| 191 |
idxFrom := Tabs.Active() |
| 192 |
idxTo := 0 |
| 193 |
offset := util.Abs(num) |
| 194 |
if shiftDirection == '-' { |
| 195 |
idxTo = idxFrom - offset |
| 196 |
} else if shiftDirection == '+' { |
| 197 |
idxTo = idxFrom + offset |
| 198 |
} else { |
| 199 |
idxTo = offset - 1 |
| 200 |
} |
| 201 |
|
| 202 |
// Restrain position to within the valid range |
| 203 |
idxTo = util.Clamp(idxTo, 0, len(Tabs.List)-1) |
| 204 |
|
| 205 |
activeTab := Tabs.List[idxFrom] |
| 206 |
Tabs.RemoveTab(activeTab.Panes[0].ID()) |
| 207 |
Tabs.List = append(Tabs.List, nil) |
| 208 |
copy(Tabs.List[idxTo+1:], Tabs.List[idxTo:]) |
| 209 |
Tabs.List[idxTo] = activeTab |
| 210 |
Tabs.Resize() |
| 211 |
Tabs.UpdateNames() |
| 212 |
Tabs.SetActive(idxTo) |
| 213 |
// InfoBar.Message(fmt.Sprintf("Moved tab from slot %d to %d", idxFrom+1, idxTo+1)) |
| 214 |
} |
| 215 |
|
| 216 |
// TabSwitchCmd switches to a given tab either by name or by number |
| 217 |
func (h *BufPane) TabSwitchCmd(args []string) { |
| 218 |
if len(args) > 0 { |
| 219 |
num, err := strconv.Atoi(args[0]) |
| 220 |
if err != nil { |
| 221 |
// Check for tab with this name |
| 222 |
|
| 223 |
found := false |
| 224 |
for i, t := range Tabs.List { |
| 225 |
if t.Panes[t.active].Name() == args[0] { |
| 226 |
Tabs.SetActive(i) |
| 227 |
found = true |
| 228 |
} |
| 229 |
} |
| 230 |
if !found { |
| 231 |
InfoBar.Error("Could not find tab: ", err) |
| 232 |
} |
| 233 |
} else { |
| 234 |
num-- |
| 235 |
if num >= 0 && num < len(Tabs.List) { |
| 236 |
Tabs.SetActive(num) |
| 237 |
} else { |
| 238 |
InfoBar.Error("Invalid tab index") |
| 239 |
} |
| 240 |
} |
| 241 |
} |
| 242 |
} |
| 243 |
|
| 244 |
// CdCmd changes the current working directory |
| 245 |
func (h *BufPane) CdCmd(args []string) { |
| 246 |
if len(args) > 0 { |
| 247 |
path, err := util.ReplaceHome(args[0]) |
| 248 |
if err != nil { |
| 249 |
InfoBar.Error(err) |
| 250 |
return |
| 251 |
} |
| 252 |
err = os.Chdir(path) |
| 253 |
if err != nil { |
| 254 |
InfoBar.Error(err) |
| 255 |
return |
| 256 |
} |
| 257 |
wd, _ := os.Getwd() |
| 258 |
for _, b := range buffer.OpenBuffers { |
| 259 |
if len(b.Path) > 0 { |
| 260 |
b.Path, _ = util.MakeRelative(b.AbsPath, wd) |
| 261 |
if p, _ := filepath.Abs(b.Path); !strings.Contains(p, wd) { |
| 262 |
b.Path = b.AbsPath |
| 263 |
} |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
// MemUsageCmd prints micro's memory usage |
| 270 |
// Alloc shows how many bytes are currently in use |
| 271 |
// Sys shows how many bytes have been requested from the operating system |
| 272 |
// NumGC shows how many times the GC has been run |
| 273 |
// Note that Go commonly reserves more memory from the OS than is currently in-use/required |
| 274 |
// Additionally, even if Go returns memory to the OS, the OS does not always claim it because |
| 275 |
// there may be plenty of memory to spare |
| 276 |
func (h *BufPane) MemUsageCmd(args []string) { |
| 277 |
InfoBar.Message(util.GetMemStats()) |
| 278 |
} |
| 279 |
|
| 280 |
// PwdCmd prints the current working directory |
| 281 |
func (h *BufPane) PwdCmd(args []string) { |
| 282 |
wd, err := os.Getwd() |
| 283 |
if err != nil { |
| 284 |
InfoBar.Message(err.Error()) |
| 285 |
} else { |
| 286 |
InfoBar.Message(wd) |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
// OpenCmd opens a new buffer with a given filename |
| 291 |
func (h *BufPane) OpenCmd(args []string) { |
| 292 |
if len(args) > 0 { |
| 293 |
filename := args[0] |
| 294 |
// the filename might or might not be quoted, so unquote first then join the strings. |
| 295 |
args, err := shellquote.Split(filename) |
| 296 |
if err != nil { |
| 297 |
InfoBar.Error("Error parsing args ", err) |
| 298 |
return |
| 299 |
} |
| 300 |
if len(args) == 0 { |
| 301 |
return |
| 302 |
} |
| 303 |
filename = strings.Join(args, " ") |
| 304 |
|
| 305 |
open := func() { |
| 306 |
b, err := buffer.NewBufferFromFile(filename, buffer.BTDefault) |
| 307 |
if err != nil { |
| 308 |
InfoBar.Error(err) |
| 309 |
return |
| 310 |
} |
| 311 |
h.OpenBuffer(b) |
| 312 |
} |
| 313 |
if h.Buf.Modified() && !h.Buf.Shared() { |
| 314 |
h.closePrompt("Save", open) |
| 315 |
} else { |
| 316 |
open() |
| 317 |
} |
| 318 |
} else { |
| 319 |
InfoBar.Error("No filename") |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
// ToggleLogCmd toggles the log view |
| 324 |
func (h *BufPane) ToggleLogCmd(args []string) { |
| 325 |
if h.Buf.Type != buffer.BTLog { |
| 326 |
h.OpenLogBuf() |
| 327 |
} else { |
| 328 |
h.Quit() |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
// ReloadCmd reloads all files (syntax files, colorschemes, plugins...) |
| 333 |
func (h *BufPane) ReloadCmd(args []string) { |
| 334 |
reloadRuntime(true) |
| 335 |
} |
| 336 |
|
| 337 |
// ReloadConfig reloads only the configuration |
| 338 |
func ReloadConfig() { |
| 339 |
reloadRuntime(false) |
| 340 |
} |
| 341 |
|
| 342 |
func reloadRuntime(reloadPlugins bool) { |
| 343 |
if reloadPlugins { |
| 344 |
err := config.RunPluginFn("deinit") |
| 345 |
if err != nil { |
| 346 |
screen.TermMessage(err) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
config.InitRuntimeFiles(true) |
| 351 |
|
| 352 |
if reloadPlugins { |
| 353 |
config.InitPlugins() |
| 354 |
} |
| 355 |
|
| 356 |
err := config.ReadSettings() |
| 357 |
if err != nil { |
| 358 |
screen.TermMessage(err) |
| 359 |
} else { |
| 360 |
parsedSettings := config.ParsedSettings() |
| 361 |
defaultSettings := config.DefaultAllSettings() |
| 362 |
for k := range defaultSettings { |
| 363 |
if _, ok := config.VolatileSettings[k]; ok { |
| 364 |
// reload should not override volatile settings |
| 365 |
continue |
| 366 |
} |
| 367 |
|
| 368 |
if _, ok := parsedSettings[k]; ok { |
| 369 |
err = doSetGlobalOptionNative(k, parsedSettings[k]) |
| 370 |
} else { |
| 371 |
err = doSetGlobalOptionNative(k, defaultSettings[k]) |
| 372 |
} |
| 373 |
if err != nil { |
| 374 |
screen.TermMessage(err) |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
if reloadPlugins { |
| 380 |
err = config.LoadAllPlugins() |
| 381 |
if err != nil { |
| 382 |
screen.TermMessage(err) |
| 383 |
} |
| 384 |
} |
| 385 |
|
| 386 |
InitBindings() |
| 387 |
InitCommands() |
| 388 |
|
| 389 |
if reloadPlugins { |
| 390 |
err = config.RunPluginFn("preinit") |
| 391 |
if err != nil { |
| 392 |
screen.TermMessage(err) |
| 393 |
} |
| 394 |
err = config.RunPluginFn("init") |
| 395 |
if err != nil { |
| 396 |
screen.TermMessage(err) |
| 397 |
} |
| 398 |
err = config.RunPluginFn("postinit") |
| 399 |
if err != nil { |
| 400 |
screen.TermMessage(err) |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
err = config.InitColorscheme() |
| 405 |
if err != nil { |
| 406 |
screen.TermMessage(err) |
| 407 |
} |
| 408 |
for _, b := range buffer.OpenBuffers { |
| 409 |
b.ReloadSettings(true) |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
// ReopenCmd reopens the buffer (reload from disk) |
| 414 |
func (h *BufPane) ReopenCmd(args []string) { |
| 415 |
if h.Buf.Modified() { |
| 416 |
InfoBar.YNPrompt("Save file before reopen?", func(yes, canceled bool) { |
| 417 |
if !canceled && yes { |
| 418 |
h.Save() |
| 419 |
h.ReOpen() |
| 420 |
} else if !canceled { |
| 421 |
h.ReOpen() |
| 422 |
} |
| 423 |
}) |
| 424 |
} else { |
| 425 |
h.ReOpen() |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
func (h *BufPane) openHelp(page string, hsplit bool, forceSplit bool) error { |
| 430 |
if data, err := config.FindRuntimeFile(config.RTHelp, page).Data(); err != nil { |
| 431 |
return errors.New(fmt.Sprintf("Unable to load help text for %s: %v", page, err)) |
| 432 |
} else { |
| 433 |
helpBuffer := buffer.NewBufferFromString(string(data), page+".md", buffer.BTHelp) |
| 434 |
helpBuffer.SetName("Help " + page) |
| 435 |
helpBuffer.SetOptionNative("hltaberrors", false) |
| 436 |
helpBuffer.SetOptionNative("hltrailingws", false) |
| 437 |
|
| 438 |
if h.Buf.Type == buffer.BTHelp && !forceSplit { |
| 439 |
h.OpenBuffer(helpBuffer) |
| 440 |
} else if hsplit { |
| 441 |
h.HSplitBuf(helpBuffer) |
| 442 |
} else { |
| 443 |
h.VSplitBuf(helpBuffer) |
| 444 |
} |
| 445 |
} |
| 446 |
return nil |
| 447 |
} |
| 448 |
|
| 449 |
// HelpCmd tries to open the given help page according to the split type |
| 450 |
// configured with the "helpsplit" option. It can be overriden by the optional |
| 451 |
// arguments "-vpslit" or "-hsplit". In case more than one help page is given |
| 452 |
// as argument then it opens all of them with the defined split type. |
| 453 |
func (h *BufPane) HelpCmd(args []string) { |
| 454 |
hsplit := config.GlobalSettings["helpsplit"] == "hsplit" |
| 455 |
if len(args) < 1 { |
| 456 |
// Open the default help if the user just typed "> help" |
| 457 |
h.openHelp("help", hsplit, false) |
| 458 |
} else { |
| 459 |
var topics []string |
| 460 |
forceSplit := false |
| 461 |
const errSplit = "hsplit and vsplit are not allowed at the same time" |
| 462 |
for _, arg := range args { |
| 463 |
switch arg { |
| 464 |
case "-vsplit": |
| 465 |
if forceSplit { |
| 466 |
InfoBar.Error(errSplit) |
| 467 |
return |
| 468 |
} |
| 469 |
hsplit = false |
| 470 |
forceSplit = true |
| 471 |
case "-hsplit": |
| 472 |
if forceSplit { |
| 473 |
InfoBar.Error(errSplit) |
| 474 |
return |
| 475 |
} |
| 476 |
hsplit = true |
| 477 |
forceSplit = true |
| 478 |
default: |
| 479 |
topics = append(topics, arg) |
| 480 |
} |
| 481 |
} |
| 482 |
|
| 483 |
if len(topics) < 1 { |
| 484 |
// Do the same as without arg |
| 485 |
h.openHelp("help", hsplit, forceSplit) |
| 486 |
return |
| 487 |
} |
| 488 |
if len(topics) > 1 { |
| 489 |
forceSplit = true |
| 490 |
} |
| 491 |
|
| 492 |
for _, topic := range topics { |
| 493 |
if config.FindRuntimeFile(config.RTHelp, topic) != nil { |
| 494 |
err := h.openHelp(topic, hsplit, forceSplit) |
| 495 |
if err != nil { |
| 496 |
InfoBar.Error(err) |
| 497 |
} |
| 498 |
} else { |
| 499 |
InfoBar.Error("Sorry, no help for ", topic) |
| 500 |
} |
| 501 |
} |
| 502 |
} |
| 503 |
} |
| 504 |
|
| 505 |
// VSplitCmd opens one or more vertical splits with the files given as arguments |
| 506 |
// If no file is given, it opens an empty buffer in a new split |
| 507 |
func (h *BufPane) VSplitCmd(args []string) { |
| 508 |
if len(args) == 0 { |
| 509 |
// Open an empty vertical split |
| 510 |
h.VSplitAction() |
| 511 |
return |
| 512 |
} |
| 513 |
|
| 514 |
for _, a := range args { |
| 515 |
buf, err := buffer.NewBufferFromFile(a, buffer.BTDefault) |
| 516 |
if err != nil { |
| 517 |
InfoBar.Error(err) |
| 518 |
return |
| 519 |
} |
| 520 |
|
| 521 |
h.VSplitBuf(buf) |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
// HSplitCmd opens one or more horizontal splits with the files given as arguments |
| 526 |
// If no file is given, it opens an empty buffer in a new split |
| 527 |
func (h *BufPane) HSplitCmd(args []string) { |
| 528 |
if len(args) == 0 { |
| 529 |
// Open an empty horizontal split |
| 530 |
h.HSplitAction() |
| 531 |
return |
| 532 |
} |
| 533 |
|
| 534 |
for _, a := range args { |
| 535 |
buf, err := buffer.NewBufferFromFile(a, buffer.BTDefault) |
| 536 |
if err != nil { |
| 537 |
InfoBar.Error(err) |
| 538 |
return |
| 539 |
} |
| 540 |
|
| 541 |
h.HSplitBuf(buf) |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
// EvalCmd evaluates a lua expression |
| 546 |
func (h *BufPane) EvalCmd(args []string) { |
| 547 |
InfoBar.Error("Eval unsupported") |
| 548 |
} |
| 549 |
|
| 550 |
// NewTabCmd opens one or more tabs with the files given as arguments |
| 551 |
// If no file is given, it opens an empty buffer in a new tab |
| 552 |
func (h *BufPane) NewTabCmd(args []string) { |
| 553 |
width, height := screen.Screen.Size() |
| 554 |
iOffset := config.GetInfoBarOffset() |
| 555 |
if len(args) > 0 { |
| 556 |
for _, a := range args { |
| 557 |
b, err := buffer.NewBufferFromFile(a, buffer.BTDefault) |
| 558 |
if err != nil { |
| 559 |
InfoBar.Error(err) |
| 560 |
return |
| 561 |
} |
| 562 |
tp := NewTabFromBuffer(0, 0, width, height-1-iOffset, b) |
| 563 |
Tabs.AddTab(tp) |
| 564 |
Tabs.SetActive(len(Tabs.List) - 1) |
| 565 |
} |
| 566 |
} else { |
| 567 |
b := buffer.NewBufferFromString("", "", buffer.BTDefault) |
| 568 |
tp := NewTabFromBuffer(0, 0, width, height-iOffset, b) |
| 569 |
Tabs.AddTab(tp) |
| 570 |
Tabs.SetActive(len(Tabs.List) - 1) |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
func doSetGlobalOptionNative(option string, nativeValue any) error { |
| 575 |
if reflect.DeepEqual(config.GlobalSettings[option], nativeValue) { |
| 576 |
return nil |
| 577 |
} |
| 578 |
|
| 579 |
config.GlobalSettings[option] = nativeValue |
| 580 |
config.ModifiedSettings[option] = true |
| 581 |
delete(config.VolatileSettings, option) |
| 582 |
|
| 583 |
if option == "colorscheme" { |
| 584 |
// LoadSyntaxFiles() |
| 585 |
config.InitColorscheme() |
| 586 |
for _, b := range buffer.OpenBuffers { |
| 587 |
b.UpdateRules() |
| 588 |
} |
| 589 |
} else if option == "infobar" || option == "keymenu" { |
| 590 |
Tabs.Resize() |
| 591 |
} else if option == "mouse" { |
| 592 |
if !nativeValue.(bool) { |
| 593 |
screen.Screen.DisableMouse() |
| 594 |
} else { |
| 595 |
screen.Screen.EnableMouse() |
| 596 |
} |
| 597 |
} else if option == "autosave" { |
| 598 |
if nativeValue.(float64) > 0 { |
| 599 |
config.SetAutoTime(nativeValue.(float64)) |
| 600 |
} else { |
| 601 |
config.SetAutoTime(0) |
| 602 |
} |
| 603 |
} else if option == "paste" { |
| 604 |
screen.Screen.SetPaste(nativeValue.(bool)) |
| 605 |
} else if option == "clipboard" { |
| 606 |
m := clipboard.SetMethod(nativeValue.(string)) |
| 607 |
err := clipboard.Initialize(m) |
| 608 |
if err != nil { |
| 609 |
return err |
| 610 |
} |
| 611 |
} else { |
| 612 |
for _, pl := range config.Plugins { |
| 613 |
if option == pl.Name { |
| 614 |
if nativeValue.(bool) && !pl.Loaded { |
| 615 |
pl.Load() |
| 616 |
_, err := pl.Call("init") |
| 617 |
if err != nil && err != config.ErrNoSuchFunction { |
| 618 |
screen.TermMessage(err) |
| 619 |
} |
| 620 |
} else if !nativeValue.(bool) && pl.Loaded { |
| 621 |
_, err := pl.Call("deinit") |
| 622 |
if err != nil && err != config.ErrNoSuchFunction { |
| 623 |
screen.TermMessage(err) |
| 624 |
} |
| 625 |
} |
| 626 |
} |
| 627 |
} |
| 628 |
} |
| 629 |
|
| 630 |
return nil |
| 631 |
} |
| 632 |
|
| 633 |
func SetGlobalOptionNative(option string, nativeValue any, writeToFile bool) error { |
| 634 |
if err := config.OptionIsValid(option, nativeValue); err != nil { |
| 635 |
return err |
| 636 |
} |
| 637 |
|
| 638 |
// check for local option first... |
| 639 |
for _, s := range config.LocalSettings { |
| 640 |
if s == option { |
| 641 |
return MainTab().CurPane().Buf.SetOptionNative(option, nativeValue) |
| 642 |
} |
| 643 |
} |
| 644 |
|
| 645 |
// ...if it's not local continue with the globals... |
| 646 |
if err := doSetGlobalOptionNative(option, nativeValue); err != nil { |
| 647 |
return err |
| 648 |
} |
| 649 |
|
| 650 |
// ...at last check the buffer locals |
| 651 |
for _, b := range buffer.OpenBuffers { |
| 652 |
b.DoSetOptionNative(option, nativeValue) |
| 653 |
delete(b.LocalSettings, option) |
| 654 |
} |
| 655 |
|
| 656 |
if !writeToFile { |
| 657 |
return nil |
| 658 |
} |
| 659 |
|
| 660 |
err := config.WriteSettings(filepath.Join(config.ConfigDir, "settings.json")) |
| 661 |
if err != nil { |
| 662 |
if errors.Is(err, util.ErrOverwrite) { |
| 663 |
screen.TermMessage(err) |
| 664 |
err = errors.Unwrap(err) |
| 665 |
} |
| 666 |
return err |
| 667 |
} |
| 668 |
|
| 669 |
return nil |
| 670 |
} |
| 671 |
|
| 672 |
func SetGlobalOption(option, value string, writeToFile bool) error { |
| 673 |
if _, ok := config.GlobalSettings[option]; !ok { |
| 674 |
return config.ErrInvalidOption |
| 675 |
} |
| 676 |
|
| 677 |
nativeValue, err := config.GetNativeValue(option, value) |
| 678 |
if err != nil { |
| 679 |
return err |
| 680 |
} |
| 681 |
|
| 682 |
return SetGlobalOptionNative(option, nativeValue, writeToFile) |
| 683 |
} |
| 684 |
|
| 685 |
func SetGlobalOptionNativePlug(option string, nativeValue any) error { |
| 686 |
return SetGlobalOptionNative(option, nativeValue, false) |
| 687 |
} |
| 688 |
|
| 689 |
func SetGlobalOptionPlug(option, value string) error { |
| 690 |
return SetGlobalOption(option, value, false) |
| 691 |
} |
| 692 |
|
| 693 |
// ResetCmd resets a setting to its default value |
| 694 |
func (h *BufPane) ResetCmd(args []string) { |
| 695 |
if len(args) < 1 { |
| 696 |
InfoBar.Error("Not enough arguments") |
| 697 |
return |
| 698 |
} |
| 699 |
|
| 700 |
option := args[0] |
| 701 |
defaults := config.DefaultAllSettings() |
| 702 |
|
| 703 |
if _, ok := defaults[option]; ok { |
| 704 |
SetGlobalOptionNative(option, defaults[option], true) |
| 705 |
return |
| 706 |
} |
| 707 |
InfoBar.Error(config.ErrInvalidOption) |
| 708 |
} |
| 709 |
|
| 710 |
// SetCmd sets an option |
| 711 |
func (h *BufPane) SetCmd(args []string) { |
| 712 |
if len(args) < 2 { |
| 713 |
InfoBar.Error("Not enough arguments") |
| 714 |
return |
| 715 |
} |
| 716 |
|
| 717 |
option := args[0] |
| 718 |
value := args[1] |
| 719 |
|
| 720 |
err := SetGlobalOption(option, value, true) |
| 721 |
if err == config.ErrInvalidOption { |
| 722 |
err := h.Buf.SetOption(option, value) |
| 723 |
if err != nil { |
| 724 |
InfoBar.Error(err) |
| 725 |
} |
| 726 |
} else if err != nil { |
| 727 |
InfoBar.Error(err) |
| 728 |
} |
| 729 |
} |
| 730 |
|
| 731 |
// SetLocalCmd sets an option local to the buffer |
| 732 |
func (h *BufPane) SetLocalCmd(args []string) { |
| 733 |
if len(args) < 2 { |
| 734 |
InfoBar.Error("Not enough arguments") |
| 735 |
return |
| 736 |
} |
| 737 |
|
| 738 |
option := args[0] |
| 739 |
value := args[1] |
| 740 |
|
| 741 |
err := h.Buf.SetOption(option, value) |
| 742 |
if err != nil { |
| 743 |
InfoBar.Error(err) |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
func (h *BufPane) toggleOption(option string, local bool) error { |
| 748 |
var curVal, newVal any |
| 749 |
|
| 750 |
if local { |
| 751 |
curVal = h.Buf.Settings[option] |
| 752 |
} else { |
| 753 |
curVal = config.GetGlobalOption(option) |
| 754 |
} |
| 755 |
if curVal == nil { |
| 756 |
return config.ErrInvalidOption |
| 757 |
} |
| 758 |
|
| 759 |
if choices, ok := config.OptionChoices[option]; ok && len(choices) == 2 { |
| 760 |
if curVal == choices[0] { |
| 761 |
newVal = choices[1] |
| 762 |
} else { |
| 763 |
newVal = choices[0] |
| 764 |
} |
| 765 |
} else if curValBool, ok := curVal.(bool); ok { |
| 766 |
newVal = !curValBool |
| 767 |
} else { |
| 768 |
return config.ErrOptNotToggleable |
| 769 |
} |
| 770 |
|
| 771 |
if local { |
| 772 |
if err := h.Buf.SetOptionNative(option, newVal); err != nil { |
| 773 |
return err |
| 774 |
} |
| 775 |
} else { |
| 776 |
if err := SetGlobalOptionNative(option, newVal, true); err != nil { |
| 777 |
return err |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
return nil |
| 782 |
} |
| 783 |
|
| 784 |
// ToggleCmd toggles a toggleable option |
| 785 |
func (h *BufPane) ToggleCmd(args []string) { |
| 786 |
if len(args) < 1 { |
| 787 |
InfoBar.Error("Not enough arguments: provide a toggleable option") |
| 788 |
return |
| 789 |
} |
| 790 |
if err := h.toggleOption(args[0], false); err != nil { |
| 791 |
InfoBar.Error(err) |
| 792 |
} |
| 793 |
} |
| 794 |
|
| 795 |
// ToggleCmd toggles a toggleable option local to the buffer |
| 796 |
func (h *BufPane) ToggleLocalCmd(args []string) { |
| 797 |
if len(args) < 1 { |
| 798 |
InfoBar.Error("Not enough arguments: provide a toggleable option") |
| 799 |
return |
| 800 |
} |
| 801 |
if err := h.toggleOption(args[0], true); err != nil { |
| 802 |
InfoBar.Error(err) |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
// ShowCmd shows the value of the given option |
| 807 |
func (h *BufPane) ShowCmd(args []string) { |
| 808 |
if len(args) < 1 { |
| 809 |
InfoBar.Error("Please provide an option to show") |
| 810 |
return |
| 811 |
} |
| 812 |
|
| 813 |
var option any |
| 814 |
if opt, ok := h.Buf.Settings[args[0]]; ok { |
| 815 |
option = opt |
| 816 |
} else if opt, ok := config.GlobalSettings[args[0]]; ok { |
| 817 |
option = opt |
| 818 |
} |
| 819 |
|
| 820 |
if option == nil { |
| 821 |
InfoBar.Error(args[0], " is not a valid option") |
| 822 |
return |
| 823 |
} |
| 824 |
|
| 825 |
InfoBar.Message(option) |
| 826 |
} |
| 827 |
|
| 828 |
func parseKeyArg(arg string) string { |
| 829 |
// If this is a raw escape sequence, convert it to its raw byte form |
| 830 |
return strings.ReplaceAll(arg, "\\x1b", "\x1b") |
| 831 |
} |
| 832 |
|
| 833 |
// ShowKeyCmd displays the action that a key is bound to |
| 834 |
func (h *BufPane) ShowKeyCmd(args []string) { |
| 835 |
if len(args) < 1 { |
| 836 |
InfoBar.Error("Please provide a key to show") |
| 837 |
return |
| 838 |
} |
| 839 |
|
| 840 |
event, err := findEvent(parseKeyArg(args[0])) |
| 841 |
if err != nil { |
| 842 |
InfoBar.Error(err) |
| 843 |
return |
| 844 |
} |
| 845 |
if action, ok := config.Bindings["buffer"][event.Name()]; ok { |
| 846 |
InfoBar.Message(action) |
| 847 |
} else { |
| 848 |
InfoBar.Message(args[0], " has no binding") |
| 849 |
} |
| 850 |
} |
| 851 |
|
| 852 |
// BindCmd creates a new keybinding |
| 853 |
func (h *BufPane) BindCmd(args []string) { |
| 854 |
if len(args) < 2 { |
| 855 |
InfoBar.Error("Not enough arguments") |
| 856 |
return |
| 857 |
} |
| 858 |
|
| 859 |
_, err := TryBindKey(parseKeyArg(args[0]), args[1], true, true) |
| 860 |
if err != nil { |
| 861 |
if errors.Is(err, util.ErrOverwrite) { |
| 862 |
screen.TermMessage(err) |
| 863 |
} else { |
| 864 |
InfoBar.Error(err) |
| 865 |
} |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
// UnbindCmd binds a key to its default action |
| 870 |
func (h *BufPane) UnbindCmd(args []string) { |
| 871 |
if len(args) < 1 { |
| 872 |
InfoBar.Error("Not enough arguments") |
| 873 |
return |
| 874 |
} |
| 875 |
|
| 876 |
err := UnbindKey(parseKeyArg(args[0])) |
| 877 |
if err != nil { |
| 878 |
if errors.Is(err, util.ErrOverwrite) { |
| 879 |
screen.TermMessage(err) |
| 880 |
} else { |
| 881 |
InfoBar.Error(err) |
| 882 |
} |
| 883 |
} |
| 884 |
} |
| 885 |
|
| 886 |
// RunCmd runs a shell command in the background |
| 887 |
func (h *BufPane) RunCmd(args []string) { |
| 888 |
runf, err := shell.RunBackgroundShell(shellquote.Join(args...)) |
| 889 |
if err != nil { |
| 890 |
InfoBar.Error(err) |
| 891 |
} else { |
| 892 |
go func() { |
| 893 |
InfoBar.Message(runf()) |
| 894 |
screen.Redraw() |
| 895 |
}() |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
// QuitCmd closes the main view |
| 900 |
func (h *BufPane) QuitCmd(args []string) { |
| 901 |
h.Quit() |
| 902 |
} |
| 903 |
|
| 904 |
// GotoCmd is a command that will send the cursor to a certain |
| 905 |
// position in the buffer |
| 906 |
// For example: `goto line`, or `goto line:col` |
| 907 |
func (h *BufPane) GotoCmd(args []string) { |
| 908 |
line, col, err := h.parseLineCol(args) |
| 909 |
if err != nil { |
| 910 |
InfoBar.Error(err) |
| 911 |
return |
| 912 |
} |
| 913 |
|
| 914 |
if line < 0 { |
| 915 |
line = h.Buf.LinesNum() + 1 + line |
| 916 |
} |
| 917 |
line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1) |
| 918 |
col = util.Clamp(col-1, 0, util.CharacterCount(h.Buf.LineBytes(line))) |
| 919 |
|
| 920 |
h.RemoveAllMultiCursors() |
| 921 |
h.Cursor.Deselect(true) |
| 922 |
h.GotoLoc(buffer.Loc{col, line}) |
| 923 |
} |
| 924 |
|
| 925 |
// JumpCmd is a command that will send the cursor to a certain relative |
| 926 |
// position in the buffer |
| 927 |
// For example: `jump line`, `jump -line`, or `jump -line:col` |
| 928 |
func (h *BufPane) JumpCmd(args []string) { |
| 929 |
line, col, err := h.parseLineCol(args) |
| 930 |
if err != nil { |
| 931 |
InfoBar.Error(err) |
| 932 |
return |
| 933 |
} |
| 934 |
|
| 935 |
line = h.Buf.GetActiveCursor().Y + 1 + line |
| 936 |
line = util.Clamp(line-1, 0, h.Buf.LinesNum()-1) |
| 937 |
col = util.Clamp(col-1, 0, util.CharacterCount(h.Buf.LineBytes(line))) |
| 938 |
|
| 939 |
h.RemoveAllMultiCursors() |
| 940 |
h.Cursor.Deselect(true) |
| 941 |
h.GotoLoc(buffer.Loc{col, line}) |
| 942 |
} |
| 943 |
|
| 944 |
// parseLineCol is a helper to parse the input of GotoCmd and JumpCmd |
| 945 |
func (h *BufPane) parseLineCol(args []string) (line int, col int, err error) { |
| 946 |
if len(args) <= 0 { |
| 947 |
return 0, 0, errors.New("Not enough arguments") |
| 948 |
} |
| 949 |
|
| 950 |
line, col = 0, 0 |
| 951 |
if strings.Contains(args[0], ":") { |
| 952 |
parts := strings.SplitN(args[0], ":", 2) |
| 953 |
line, err = strconv.Atoi(parts[0]) |
| 954 |
if err != nil { |
| 955 |
return 0, 0, err |
| 956 |
} |
| 957 |
col, err = strconv.Atoi(parts[1]) |
| 958 |
if err != nil { |
| 959 |
return 0, 0, err |
| 960 |
} |
| 961 |
} else { |
| 962 |
line, err = strconv.Atoi(args[0]) |
| 963 |
if err != nil { |
| 964 |
return 0, 0, err |
| 965 |
} |
| 966 |
} |
| 967 |
|
| 968 |
return line, col, nil |
| 969 |
} |
| 970 |
|
| 971 |
// SaveCmd saves the buffer optionally with an argument file name |
| 972 |
func (h *BufPane) SaveCmd(args []string) { |
| 973 |
if len(args) == 0 { |
| 974 |
h.Save() |
| 975 |
} else { |
| 976 |
h.saveBufToFile(args[0], "SaveAs", nil) |
| 977 |
} |
| 978 |
} |
| 979 |
|
| 980 |
// ReplaceCmd runs search and replace |
| 981 |
func (h *BufPane) ReplaceCmd(args []string) { |
| 982 |
if len(args) < 2 || len(args) > 4 { |
| 983 |
// We need to find both a search and replace expression |
| 984 |
InfoBar.Error("Invalid replace statement: " + strings.Join(args, " ")) |
| 985 |
return |
| 986 |
} |
| 987 |
|
| 988 |
all := false |
| 989 |
noRegex := false |
| 990 |
|
| 991 |
foundSearch := false |
| 992 |
foundReplace := false |
| 993 |
var search string |
| 994 |
var replaceStr string |
| 995 |
for _, arg := range args { |
| 996 |
switch arg { |
| 997 |
case "-a": |
| 998 |
all = true |
| 999 |
case "-l": |
| 1000 |
noRegex = true |
| 1001 |
default: |
| 1002 |
if !foundSearch { |
| 1003 |
foundSearch = true |
| 1004 |
search = arg |
| 1005 |
} else if !foundReplace { |
| 1006 |
foundReplace = true |
| 1007 |
replaceStr = arg |
| 1008 |
} else { |
| 1009 |
InfoBar.Error("Invalid flag: " + arg) |
| 1010 |
return |
| 1011 |
} |
| 1012 |
} |
| 1013 |
} |
| 1014 |
|
| 1015 |
if noRegex { |
| 1016 |
search = regexp.QuoteMeta(search) |
| 1017 |
} |
| 1018 |
|
| 1019 |
replace := []byte(replaceStr) |
| 1020 |
|
| 1021 |
var regex *regexp.Regexp |
| 1022 |
var err error |
| 1023 |
if h.Buf.Settings["ignorecase"].(bool) { |
| 1024 |
regex, err = regexp.Compile("(?im)" + search) |
| 1025 |
} else { |
| 1026 |
regex, err = regexp.Compile("(?m)" + search) |
| 1027 |
} |
| 1028 |
if err != nil { |
| 1029 |
// There was an error with the user's regex |
| 1030 |
InfoBar.Error(err) |
| 1031 |
return |
| 1032 |
} |
| 1033 |
|
| 1034 |
nreplaced := 0 |
| 1035 |
start := h.Buf.Start() |
| 1036 |
end := h.Buf.End() |
| 1037 |
searchLoc := h.Cursor.Loc |
| 1038 |
selection := h.Cursor.HasSelection() |
| 1039 |
if selection { |
| 1040 |
start = h.Cursor.CurSelection[0] |
| 1041 |
end = h.Cursor.CurSelection[1] |
| 1042 |
searchLoc = start // otherwise me might start at the end |
| 1043 |
} |
| 1044 |
if all { |
| 1045 |
nreplaced, _ = h.Buf.ReplaceRegex(start, end, regex, replace, !noRegex) |
| 1046 |
} else { |
| 1047 |
inRange := func(l buffer.Loc) bool { |
| 1048 |
return l.GreaterEqual(start) && l.LessEqual(end) |
| 1049 |
} |
| 1050 |
|
| 1051 |
lastMatchEnd := buffer.Loc{-1, -1} |
| 1052 |
var doReplacement func() |
| 1053 |
doReplacement = func() { |
| 1054 |
locs, found, err := h.Buf.FindNext(search, start, end, searchLoc, true, true) |
| 1055 |
if err != nil { |
| 1056 |
InfoBar.Error(err) |
| 1057 |
return |
| 1058 |
} |
| 1059 |
if !found || !inRange(locs[0]) || !inRange(locs[1]) { |
| 1060 |
h.Cursor.ResetSelection() |
| 1061 |
h.Buf.RelocateCursors() |
| 1062 |
|
| 1063 |
return |
| 1064 |
} |
| 1065 |
|
| 1066 |
if lastMatchEnd == locs[1] { |
| 1067 |
// skip empty match right after previous match |
| 1068 |
if searchLoc == end { |
| 1069 |
searchLoc = start |
| 1070 |
lastMatchEnd = buffer.Loc{-1, -1} |
| 1071 |
} else { |
| 1072 |
searchLoc = searchLoc.Move(1, h.Buf) |
| 1073 |
} |
| 1074 |
doReplacement() |
| 1075 |
return |
| 1076 |
} |
| 1077 |
|
| 1078 |
h.Cursor.SetSelectionStart(locs[0]) |
| 1079 |
h.Cursor.SetSelectionEnd(locs[1]) |
| 1080 |
h.GotoLoc(locs[0]) |
| 1081 |
h.Buf.LastSearch = search |
| 1082 |
h.Buf.LastSearchRegex = true |
| 1083 |
h.Buf.HighlightSearch = h.Buf.Settings["hlsearch"].(bool) |
| 1084 |
|
| 1085 |
InfoBar.YNPrompt("Perform replacement (y,n,esc)", func(yes, canceled bool) { |
| 1086 |
if !canceled && yes { |
| 1087 |
_, nrunes := h.Buf.ReplaceRegex(locs[0], locs[1], regex, replace, !noRegex) |
| 1088 |
|
| 1089 |
searchLoc = locs[0] |
| 1090 |
searchLoc.X += nrunes + locs[0].Diff(locs[1], h.Buf) |
| 1091 |
if end.Y == locs[1].Y { |
| 1092 |
end = end.Move(nrunes, h.Buf) |
| 1093 |
} |
| 1094 |
h.Cursor.Loc = searchLoc |
| 1095 |
nreplaced++ |
| 1096 |
} else if !canceled && !yes { |
| 1097 |
searchLoc = locs[1] |
| 1098 |
} else if canceled { |
| 1099 |
h.Cursor.ResetSelection() |
| 1100 |
h.Buf.RelocateCursors() |
| 1101 |
return |
| 1102 |
} |
| 1103 |
lastMatchEnd = searchLoc |
| 1104 |
doReplacement() |
| 1105 |
}) |
| 1106 |
} |
| 1107 |
doReplacement() |
| 1108 |
} |
| 1109 |
|
| 1110 |
h.Buf.RelocateCursors() |
| 1111 |
h.Relocate() |
| 1112 |
|
| 1113 |
var s string |
| 1114 |
if nreplaced > 1 { |
| 1115 |
s = fmt.Sprintf("Replaced %d occurrences of %s", nreplaced, search) |
| 1116 |
} else if nreplaced == 1 { |
| 1117 |
s = fmt.Sprintf("Replaced 1 occurrence of %s", search) |
| 1118 |
} else { |
| 1119 |
s = fmt.Sprintf("Nothing matched %s", search) |
| 1120 |
} |
| 1121 |
|
| 1122 |
if selection { |
| 1123 |
s += " in selection" |
| 1124 |
} |
| 1125 |
|
| 1126 |
InfoBar.Message(s) |
| 1127 |
} |
| 1128 |
|
| 1129 |
// ReplaceAllCmd replaces search term all at once |
| 1130 |
func (h *BufPane) ReplaceAllCmd(args []string) { |
| 1131 |
// aliased to Replace command |
| 1132 |
h.ReplaceCmd(append(args, "-a")) |
| 1133 |
} |
| 1134 |
|
| 1135 |
func (h *BufPane) openTerm(args []string, newtab bool) { |
| 1136 |
t := new(shell.Terminal) |
| 1137 |
err := t.Start(args, false, true, nil, nil) |
| 1138 |
if err != nil { |
| 1139 |
InfoBar.Error(err) |
| 1140 |
return |
| 1141 |
} |
| 1142 |
|
| 1143 |
pane := 0 |
| 1144 |
id := h.ID() |
| 1145 |
if newtab { |
| 1146 |
h.AddTab() |
| 1147 |
id = MainTab().Panes[pane].ID() |
| 1148 |
} else { |
| 1149 |
for i, p := range MainTab().Panes { |
| 1150 |
if p.IsActive() { |
| 1151 |
pane = i |
| 1152 |
id = p.ID() |
| 1153 |
p.Close() |
| 1154 |
break |
| 1155 |
} |
| 1156 |
} |
| 1157 |
} |
| 1158 |
|
| 1159 |
v := h.GetView() |
| 1160 |
tp, err := NewTermPane(v.X, v.Y, v.Width, v.Height, t, id, MainTab()) |
| 1161 |
if err != nil { |
| 1162 |
InfoBar.Error(err) |
| 1163 |
return |
| 1164 |
} |
| 1165 |
MainTab().Panes[pane] = tp |
| 1166 |
MainTab().SetActive(pane) |
| 1167 |
} |
| 1168 |
|
| 1169 |
// TermCmd opens a terminal in the current view |
| 1170 |
func (h *BufPane) TermCmd(args []string) { |
| 1171 |
if !TermEmuSupported { |
| 1172 |
InfoBar.Error("Terminal emulator not supported on this system") |
| 1173 |
return |
| 1174 |
} |
| 1175 |
|
| 1176 |
if len(args) == 0 { |
| 1177 |
sh := os.Getenv("SHELL") |
| 1178 |
if sh == "" { |
| 1179 |
InfoBar.Error("Shell environment not found") |
| 1180 |
return |
| 1181 |
} |
| 1182 |
args = []string{sh} |
| 1183 |
} |
| 1184 |
|
| 1185 |
// If there is only one open file we make a new tab instead of overwriting it |
| 1186 |
newtab := len(MainTab().Panes) == 1 && len(Tabs.List) == 1 |
| 1187 |
if newtab { |
| 1188 |
h.openTerm(args, true) |
| 1189 |
return |
| 1190 |
} |
| 1191 |
|
| 1192 |
if h.Buf.Modified() && !h.Buf.Shared() { |
| 1193 |
h.closePrompt("Save", func() { |
| 1194 |
h.openTerm(args, false) |
| 1195 |
}) |
| 1196 |
} else { |
| 1197 |
h.openTerm(args, false) |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
// HandleCommand handles input from the user |
| 1202 |
func (h *BufPane) HandleCommand(input string) { |
| 1203 |
args, err := shellquote.Split(input) |
| 1204 |
if err != nil { |
| 1205 |
InfoBar.Error("Error parsing args ", err) |
| 1206 |
return |
| 1207 |
} |
| 1208 |
|
| 1209 |
if len(args) == 0 { |
| 1210 |
return |
| 1211 |
} |
| 1212 |
|
| 1213 |
inputCmd := args[0] |
| 1214 |
|
| 1215 |
if _, ok := commands[inputCmd]; !ok { |
| 1216 |
InfoBar.Error("Unknown command ", inputCmd) |
| 1217 |
} else { |
| 1218 |
WriteLog("> " + input + "\n") |
| 1219 |
commands[inputCmd].action(h, args[1:]) |
| 1220 |
WriteLog("\n") |
| 1221 |
} |
| 1222 |
} |