| 1 |
package main |
| 2 |
|
| 3 |
import ( |
| 4 |
"errors" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"log" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"slices" |
| 11 |
"strconv" |
| 12 |
"strings" |
| 13 |
"time" |
| 14 |
"unicode" |
| 15 |
"unicode/utf8" |
| 16 |
|
| 17 |
"github.com/clipperhouse/displaywidth" |
| 18 |
"github.com/gdamore/tcell/v3" |
| 19 |
) |
| 20 |
|
| 21 |
func applyBoolOpt(opt *bool, e *setExpr) error { |
| 22 |
switch { |
| 23 |
case strings.HasPrefix(e.opt, "no"): |
| 24 |
if e.val != "" { |
| 25 |
return fmt.Errorf("%s: unexpected value: %s", e.opt, e.val) |
| 26 |
} |
| 27 |
*opt = false |
| 28 |
case strings.HasSuffix(e.opt, "!"): |
| 29 |
if e.val != "" { |
| 30 |
return fmt.Errorf("%s: unexpected value: %s", e.opt, e.val) |
| 31 |
} |
| 32 |
*opt = !*opt |
| 33 |
default: |
| 34 |
switch e.val { |
| 35 |
case "", "true": |
| 36 |
*opt = true |
| 37 |
case "false": |
| 38 |
*opt = false |
| 39 |
default: |
| 40 |
return fmt.Errorf("%s: value should be empty, 'true', or 'false'", e.opt) |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
return nil |
| 45 |
} |
| 46 |
|
| 47 |
func applyLocalBoolOpt(localOpt map[string]bool, globalOpt bool, e *setLocalExpr) error { |
| 48 |
opt, ok := localOpt[e.path] |
| 49 |
if !ok { |
| 50 |
opt = globalOpt |
| 51 |
} |
| 52 |
|
| 53 |
if err := applyBoolOpt(&opt, &setExpr{e.opt, e.val}); err != nil { |
| 54 |
return err |
| 55 |
} |
| 56 |
|
| 57 |
localOpt[e.path] = opt |
| 58 |
return nil |
| 59 |
} |
| 60 |
|
| 61 |
func (e *setExpr) eval(app *app, _ []string) { |
| 62 |
var err error |
| 63 |
switch e.opt { |
| 64 |
case "anchorfind", "noanchorfind", "anchorfind!": |
| 65 |
err = applyBoolOpt(&gOpts.anchorfind, e) |
| 66 |
case "autoquit", "noautoquit", "autoquit!": |
| 67 |
err = applyBoolOpt(&gOpts.autoquit, e) |
| 68 |
case "dircounts", "nodircounts", "dircounts!": |
| 69 |
err = applyBoolOpt(&gOpts.dircounts, e) |
| 70 |
if err == nil { |
| 71 |
app.nav.renew() |
| 72 |
app.ui.loadFile(app, false) |
| 73 |
} |
| 74 |
case "dirfirst", "nodirfirst", "dirfirst!": |
| 75 |
err = applyBoolOpt(&gOpts.dirfirst, e) |
| 76 |
if err == nil { |
| 77 |
app.nav.sort() |
| 78 |
} |
| 79 |
case "dironly", "nodironly", "dironly!": |
| 80 |
err = applyBoolOpt(&gOpts.dironly, e) |
| 81 |
if err == nil { |
| 82 |
app.nav.sort() |
| 83 |
app.nav.position() |
| 84 |
app.ui.loadFile(app, true) |
| 85 |
} |
| 86 |
case "dirpreviews", "nodirpreviews", "dirpreviews!": |
| 87 |
err = applyBoolOpt(&gOpts.dirpreviews, e) |
| 88 |
case "drawbox", "nodrawbox", "drawbox!": |
| 89 |
err = applyBoolOpt(&gOpts.drawbox, e) |
| 90 |
if err == nil { |
| 91 |
app.ui.renew() |
| 92 |
app.nav.resize(app.ui) |
| 93 |
app.ui.loadFile(app, true) |
| 94 |
} |
| 95 |
case "hidden", "nohidden", "hidden!": |
| 96 |
err = applyBoolOpt(&gOpts.hidden, e) |
| 97 |
if err == nil { |
| 98 |
app.nav.sort() |
| 99 |
app.nav.position() |
| 100 |
app.ui.loadFile(app, true) |
| 101 |
} |
| 102 |
case "history", "nohistory", "history!": |
| 103 |
err = applyBoolOpt(&gOpts.history, e) |
| 104 |
case "icons", "noicons", "icons!": |
| 105 |
err = applyBoolOpt(&gOpts.icons, e) |
| 106 |
case "ignorecase", "noignorecase", "ignorecase!": |
| 107 |
err = applyBoolOpt(&gOpts.ignorecase, e) |
| 108 |
if err == nil { |
| 109 |
app.nav.sort() |
| 110 |
app.nav.position() |
| 111 |
app.ui.loadFile(app, true) |
| 112 |
} |
| 113 |
case "ignoredia", "noignoredia", "ignoredia!": |
| 114 |
err = applyBoolOpt(&gOpts.ignoredia, e) |
| 115 |
if err == nil { |
| 116 |
app.nav.sort() |
| 117 |
app.nav.position() |
| 118 |
app.ui.loadFile(app, true) |
| 119 |
} |
| 120 |
case "incfilter", "noincfilter", "incfilter!": |
| 121 |
err = applyBoolOpt(&gOpts.incfilter, e) |
| 122 |
case "incsearch", "noincsearch", "incsearch!": |
| 123 |
err = applyBoolOpt(&gOpts.incsearch, e) |
| 124 |
case "mergeindicators", "nomergeindicators", "mergeindicators!": |
| 125 |
err = applyBoolOpt(&gOpts.mergeindicators, e) |
| 126 |
case "mouse", "nomouse", "mouse!": |
| 127 |
err = applyBoolOpt(&gOpts.mouse, e) |
| 128 |
if err == nil { |
| 129 |
if gOpts.mouse { |
| 130 |
app.ui.screen.EnableMouse(tcell.MouseButtonEvents) |
| 131 |
} else { |
| 132 |
app.ui.screen.DisableMouse() |
| 133 |
} |
| 134 |
} |
| 135 |
case "number", "nonumber", "number!": |
| 136 |
err = applyBoolOpt(&gOpts.number, e) |
| 137 |
case "preload", "nopreload", "preload!": |
| 138 |
err = applyBoolOpt(&gOpts.preload, e) |
| 139 |
case "preview", "nopreview", "preview!": |
| 140 |
preview := gOpts.preview |
| 141 |
err = applyBoolOpt(&preview, e) |
| 142 |
if preview && len(gOpts.ratios) < 2 { |
| 143 |
err = errors.New("preview: 'ratios' should consist of at least two numbers before enabling 'preview'") |
| 144 |
} |
| 145 |
if err == nil { |
| 146 |
gOpts.preview = preview |
| 147 |
app.ui.sxScreen.forceClear = true |
| 148 |
app.ui.loadFile(app, true) |
| 149 |
} |
| 150 |
case "relativenumber", "norelativenumber", "relativenumber!": |
| 151 |
err = applyBoolOpt(&gOpts.relativenumber, e) |
| 152 |
case "reverse", "noreverse", "reverse!": |
| 153 |
err = applyBoolOpt(&gOpts.reverse, e) |
| 154 |
if err == nil { |
| 155 |
app.nav.sort() |
| 156 |
} |
| 157 |
// DEPRECATED: remove after r42 is released |
| 158 |
case "roundbox", "noroundbox", "roundbox!": |
| 159 |
app.ui.echoerr("option 'roundbox' is deprecated, use 'borderstyle' instead") |
| 160 |
case "showbinds", "noshowbinds", "showbinds!": |
| 161 |
err = applyBoolOpt(&gOpts.showbinds, e) |
| 162 |
case "smartcase", "nosmartcase", "smartcase!": |
| 163 |
err = applyBoolOpt(&gOpts.smartcase, e) |
| 164 |
if err == nil { |
| 165 |
app.nav.sort() |
| 166 |
app.nav.position() |
| 167 |
app.ui.loadFile(app, true) |
| 168 |
} |
| 169 |
case "smartdia", "nosmartdia", "smartdia!": |
| 170 |
err = applyBoolOpt(&gOpts.smartdia, e) |
| 171 |
if err == nil { |
| 172 |
app.nav.sort() |
| 173 |
app.nav.position() |
| 174 |
app.ui.loadFile(app, true) |
| 175 |
} |
| 176 |
case "sortignorecase", "nosortignorecase", "sortignorecase!": |
| 177 |
err = applyBoolOpt(&gOpts.sortignorecase, e) |
| 178 |
if err == nil { |
| 179 |
app.nav.sort() |
| 180 |
} |
| 181 |
case "sortignoredia", "nosortignoredia", "sortignoredia!": |
| 182 |
err = applyBoolOpt(&gOpts.sortignoredia, e) |
| 183 |
if err == nil { |
| 184 |
app.nav.sort() |
| 185 |
} |
| 186 |
case "watch", "nowatch", "watch!": |
| 187 |
err = applyBoolOpt(&gOpts.watch, e) |
| 188 |
if err == nil { |
| 189 |
if gOpts.watch { |
| 190 |
app.watch.start() |
| 191 |
for _, dir := range app.nav.dirCache { |
| 192 |
app.watchDir(dir) |
| 193 |
} |
| 194 |
} else { |
| 195 |
app.watch.stop() |
| 196 |
} |
| 197 |
} |
| 198 |
case "wrapscan", "nowrapscan", "wrapscan!": |
| 199 |
err = applyBoolOpt(&gOpts.wrapscan, e) |
| 200 |
case "wrapscroll", "nowrapscroll", "wrapscroll!": |
| 201 |
err = applyBoolOpt(&gOpts.wrapscroll, e) |
| 202 |
case "borderfmt": |
| 203 |
gOpts.borderfmt = e.val |
| 204 |
case "cleaner": |
| 205 |
gOpts.cleaner = replaceTilde(e.val) |
| 206 |
case "copyfmt": |
| 207 |
gOpts.copyfmt = e.val |
| 208 |
case "cursoractivefmt": |
| 209 |
gOpts.cursoractivefmt = e.val |
| 210 |
case "cursorparentfmt": |
| 211 |
gOpts.cursorparentfmt = e.val |
| 212 |
case "cursorpreviewfmt": |
| 213 |
gOpts.cursorpreviewfmt = e.val |
| 214 |
case "cutfmt": |
| 215 |
gOpts.cutfmt = e.val |
| 216 |
case "borderstyle": |
| 217 |
switch e.val { |
| 218 |
case "box": |
| 219 |
gOpts.borderstyle = borderBox |
| 220 |
case "roundbox": |
| 221 |
gOpts.borderstyle = borderRoundBox |
| 222 |
case "outline": |
| 223 |
gOpts.borderstyle = borderOutline |
| 224 |
case "roundoutline": |
| 225 |
gOpts.borderstyle = borderRoundOutline |
| 226 |
case "separators": |
| 227 |
gOpts.borderstyle = borderSeparators |
| 228 |
default: |
| 229 |
app.ui.echoerr("borderstyle: value should either be 'box', 'roundbox', 'outline', 'roundoutline' or 'separators'") |
| 230 |
return |
| 231 |
} |
| 232 |
app.ui.renew() |
| 233 |
app.nav.resize(app.ui) |
| 234 |
app.ui.loadFile(app, true) |
| 235 |
case "dupfilefmt": |
| 236 |
gOpts.dupfilefmt = e.val |
| 237 |
case "errorfmt": |
| 238 |
gOpts.errorfmt = e.val |
| 239 |
case "filesep": |
| 240 |
gOpts.filesep = e.val |
| 241 |
case "filtermethod": |
| 242 |
switch e.val { |
| 243 |
case "text", "glob", "regex": |
| 244 |
gOpts.filtermethod = searchMethod(e.val) |
| 245 |
default: |
| 246 |
app.ui.echoerr("filtermethod: value should either be 'text', 'glob' or 'regex") |
| 247 |
return |
| 248 |
} |
| 249 |
app.nav.sort() |
| 250 |
app.nav.position() |
| 251 |
app.ui.loadFile(app, true) |
| 252 |
case "findlen": |
| 253 |
n, err := strconv.Atoi(e.val) |
| 254 |
if err != nil { |
| 255 |
app.ui.echoerrf("findlen: %s", err) |
| 256 |
return |
| 257 |
} |
| 258 |
if n < 0 { |
| 259 |
app.ui.echoerr("findlen: value should be a non-negative number") |
| 260 |
return |
| 261 |
} |
| 262 |
gOpts.findlen = n |
| 263 |
case "hiddenfiles": |
| 264 |
toks := strings.Split(e.val, ":") |
| 265 |
for _, s := range toks { |
| 266 |
if s == "" { |
| 267 |
app.ui.echoerr("hiddenfiles: glob should be non-empty") |
| 268 |
return |
| 269 |
} |
| 270 |
_, err := filepath.Match(s, "a") |
| 271 |
if err != nil { |
| 272 |
app.ui.echoerrf("hiddenfiles: %s", err) |
| 273 |
return |
| 274 |
} |
| 275 |
} |
| 276 |
gOpts.hiddenfiles = toks |
| 277 |
app.nav.sort() |
| 278 |
app.nav.position() |
| 279 |
app.ui.loadFile(app, true) |
| 280 |
case "ifs": |
| 281 |
gOpts.ifs = e.val |
| 282 |
case "info": |
| 283 |
if e.val == "" { |
| 284 |
gOpts.info = nil |
| 285 |
return |
| 286 |
} |
| 287 |
toks := strings.Split(e.val, ":") |
| 288 |
for _, s := range toks { |
| 289 |
switch s { |
| 290 |
case "size", "time", "atime", "btime", "ctime", "perm", "user", "group", "custom": |
| 291 |
default: |
| 292 |
app.ui.echoerr("info: should consist of 'size', 'time', 'atime', 'btime', 'ctime', 'perm', 'user', 'group' or 'custom' separated with colon") |
| 293 |
return |
| 294 |
} |
| 295 |
} |
| 296 |
gOpts.info = toks |
| 297 |
case "infotimefmtnew": |
| 298 |
gOpts.infotimefmtnew = e.val |
| 299 |
case "infotimefmtold": |
| 300 |
gOpts.infotimefmtold = e.val |
| 301 |
case "menufmt": |
| 302 |
gOpts.menufmt = e.val |
| 303 |
case "menuheaderfmt": |
| 304 |
gOpts.menuheaderfmt = e.val |
| 305 |
case "menuselectfmt": |
| 306 |
gOpts.menuselectfmt = e.val |
| 307 |
case "numbercursorfmt": |
| 308 |
gOpts.numbercursorfmt = e.val |
| 309 |
case "numberfmt": |
| 310 |
gOpts.numberfmt = e.val |
| 311 |
case "period": |
| 312 |
n, err := strconv.Atoi(e.val) |
| 313 |
if err != nil { |
| 314 |
app.ui.echoerrf("period: %s", err) |
| 315 |
return |
| 316 |
} |
| 317 |
if n < 0 { |
| 318 |
app.ui.echoerr("period: value should be a non-negative number") |
| 319 |
return |
| 320 |
} |
| 321 |
gOpts.period = n |
| 322 |
if n == 0 { |
| 323 |
app.ticker.Stop() |
| 324 |
} else { |
| 325 |
app.ticker.Stop() |
| 326 |
app.ticker = time.NewTicker(time.Duration(gOpts.period) * time.Second) |
| 327 |
} |
| 328 |
case "preserve": |
| 329 |
if e.val == "" { |
| 330 |
gOpts.preserve = nil |
| 331 |
return |
| 332 |
} |
| 333 |
toks := strings.Split(e.val, ":") |
| 334 |
for _, s := range toks { |
| 335 |
switch s { |
| 336 |
case "mode", "timestamps": |
| 337 |
default: |
| 338 |
app.ui.echoerr("preserve: should consist of 'mode' or 'timestamps' separated with colon") |
| 339 |
return |
| 340 |
} |
| 341 |
} |
| 342 |
gOpts.preserve = toks |
| 343 |
case "previewer": |
| 344 |
gOpts.previewer = replaceTilde(e.val) |
| 345 |
case "promptfmt": |
| 346 |
gOpts.promptfmt = e.val |
| 347 |
case "ratios": |
| 348 |
toks := strings.Split(e.val, ":") |
| 349 |
rats := make([]int, 0, len(toks)) |
| 350 |
for _, s := range toks { |
| 351 |
n, err := strconv.Atoi(s) |
| 352 |
if err != nil { |
| 353 |
app.ui.echoerrf("ratios: %s", err) |
| 354 |
return |
| 355 |
} |
| 356 |
if n <= 0 { |
| 357 |
app.ui.echoerr("ratios: value should be a positive number") |
| 358 |
return |
| 359 |
} |
| 360 |
rats = append(rats, n) |
| 361 |
} |
| 362 |
if gOpts.preview && len(rats) < 2 { |
| 363 |
app.ui.echoerr("ratios: should consist of at least two numbers when 'preview' is enabled") |
| 364 |
return |
| 365 |
} |
| 366 |
gOpts.ratios = rats |
| 367 |
app.ui.wins = getWins(app.ui.screen) |
| 368 |
app.nav.resize(app.ui) |
| 369 |
app.ui.loadFile(app, true) |
| 370 |
case "rulerfile", "norulerfile", "rulerfile!": |
| 371 |
gOpts.rulerfile = replaceTilde(e.val) |
| 372 |
app.ui.ruler, app.ui.rulerErr = parseRuler(gOpts.rulerfile) |
| 373 |
case "rulerfmt": |
| 374 |
app.ui.echoerr("option 'rulerfmt' is deprecated and will be replaced by the 'rulerfile' option") |
| 375 |
gOpts.rulerfmt = e.val |
| 376 |
case "scrolloff": |
| 377 |
n, err := strconv.Atoi(e.val) |
| 378 |
if err != nil { |
| 379 |
app.ui.echoerrf("scrolloff: %s", err) |
| 380 |
return |
| 381 |
} |
| 382 |
if n < 0 { |
| 383 |
app.ui.echoerr("scrolloff: value should be a non-negative number") |
| 384 |
return |
| 385 |
} |
| 386 |
gOpts.scrolloff = n |
| 387 |
case "searchmethod": |
| 388 |
switch e.val { |
| 389 |
case "text", "glob", "regex": |
| 390 |
gOpts.searchmethod = searchMethod(e.val) |
| 391 |
default: |
| 392 |
app.ui.echoerr("searchmethod: value should either be 'text', 'glob' or 'regex'") |
| 393 |
return |
| 394 |
} |
| 395 |
case "selectfmt": |
| 396 |
gOpts.selectfmt = e.val |
| 397 |
case "selmode": |
| 398 |
switch e.val { |
| 399 |
case "all", "dir": |
| 400 |
gOpts.selmode = e.val |
| 401 |
default: |
| 402 |
app.ui.echoerr("selmode: value should either be 'all' or 'dir'") |
| 403 |
return |
| 404 |
} |
| 405 |
case "shell": |
| 406 |
gOpts.shell = e.val |
| 407 |
case "shellflag": |
| 408 |
gOpts.shellflag = e.val |
| 409 |
case "shellopts": |
| 410 |
if e.val == "" { |
| 411 |
gOpts.shellopts = nil |
| 412 |
return |
| 413 |
} |
| 414 |
gOpts.shellopts = strings.Split(e.val, ":") |
| 415 |
case "sizeunits": |
| 416 |
switch e.val { |
| 417 |
case "binary", "decimal": |
| 418 |
gOpts.sizeunits = e.val |
| 419 |
default: |
| 420 |
app.ui.echoerr("sizeunits: value should either be 'binary' or 'decimal'") |
| 421 |
return |
| 422 |
} |
| 423 |
case "sortby": |
| 424 |
method := sortMethod(e.val) |
| 425 |
if !isValidSortMethod(method) { |
| 426 |
app.ui.echoerr(invalidSortErrorMessage) |
| 427 |
return |
| 428 |
} |
| 429 |
gOpts.sortby = method |
| 430 |
app.nav.sort() |
| 431 |
case "statfmt": |
| 432 |
app.ui.echoerr("option 'statfmt' is deprecated and will be replaced by the 'rulerfile' option") |
| 433 |
gOpts.statfmt = e.val |
| 434 |
case "tabstop": |
| 435 |
n, err := strconv.Atoi(e.val) |
| 436 |
if err != nil { |
| 437 |
app.ui.echoerrf("tabstop: %s", err) |
| 438 |
return |
| 439 |
} |
| 440 |
if n <= 0 { |
| 441 |
app.ui.echoerr("tabstop: value should be a positive number") |
| 442 |
return |
| 443 |
} |
| 444 |
gOpts.tabstop = n |
| 445 |
case "tagfmt": |
| 446 |
gOpts.tagfmt = e.val |
| 447 |
case "tempmarks": |
| 448 |
gOpts.tempmarks = "'" + e.val |
| 449 |
case "terminalcursor": |
| 450 |
style := cursorStyle(e.val) |
| 451 |
switch style { |
| 452 |
case defaultCursor: |
| 453 |
app.ui.screen.SetCursorStyle(tcell.CursorStyleDefault) |
| 454 |
case blockCursor: |
| 455 |
app.ui.screen.SetCursorStyle(tcell.CursorStyleSteadyBlock) |
| 456 |
case underlineCursor: |
| 457 |
app.ui.screen.SetCursorStyle(tcell.CursorStyleSteadyUnderline) |
| 458 |
case barCursor: |
| 459 |
app.ui.screen.SetCursorStyle(tcell.CursorStyleSteadyBar) |
| 460 |
case blinkBlockCursor: |
| 461 |
app.ui.screen.SetCursorStyle(tcell.CursorStyleBlinkingBlock) |
| 462 |
case blinkUnderlineCursor: |
| 463 |
app.ui.screen.SetCursorStyle(tcell.CursorStyleBlinkingUnderline) |
| 464 |
case blinkBarCursor: |
| 465 |
app.ui.screen.SetCursorStyle(tcell.CursorStyleBlinkingBar) |
| 466 |
default: |
| 467 |
app.ui.echoerr("terminalcursor: value should either be 'default', 'block', 'underline', 'bar', 'blinkblock', 'blinkunderline' or 'blinkbar'") |
| 468 |
return |
| 469 |
} |
| 470 |
gOpts.terminalcursor = style |
| 471 |
case "timefmt": |
| 472 |
gOpts.timefmt = e.val |
| 473 |
case "truncatechar": |
| 474 |
if displaywidth.String(e.val) != 1 { |
| 475 |
app.ui.echoerr("truncatechar: value should be a single character") |
| 476 |
return |
| 477 |
} |
| 478 |
gOpts.truncatechar = e.val |
| 479 |
case "truncatepct": |
| 480 |
n, err := strconv.Atoi(e.val) |
| 481 |
if err != nil { |
| 482 |
app.ui.echoerrf("truncatepct: %s", err) |
| 483 |
return |
| 484 |
} |
| 485 |
if n < 0 || n > 100 { |
| 486 |
app.ui.echoerrf("truncatepct: must be between 0 and 100 (both inclusive), got %d", n) |
| 487 |
return |
| 488 |
} |
| 489 |
gOpts.truncatepct = n |
| 490 |
case "visualfmt": |
| 491 |
gOpts.visualfmt = e.val |
| 492 |
case "waitmsg": |
| 493 |
gOpts.waitmsg = e.val |
| 494 |
default: |
| 495 |
// any key with the prefix user_ is accepted as a user defined option |
| 496 |
if strings.HasPrefix(e.opt, "user_") { |
| 497 |
gOpts.user[e.opt[5:]] = e.val |
| 498 |
// Export user defined options immediately, so that the current values |
| 499 |
// are available for some external previewer, which is started in a |
| 500 |
// different thread and thus cannot export (as `setenv` is not thread-safe). |
| 501 |
os.Setenv("lf_"+e.opt, e.val) |
| 502 |
} else { |
| 503 |
err = fmt.Errorf("unknown option: %s", e.opt) |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
if err != nil { |
| 508 |
app.ui.echoerr(err.Error()) |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
func (e *setLocalExpr) eval(app *app, _ []string) { |
| 513 |
var err error |
| 514 |
e.path, err = filepath.Abs(replaceTilde(e.path)) |
| 515 |
if err != nil { |
| 516 |
app.ui.echoerrf("setlocal: %s", err) |
| 517 |
return |
| 518 |
} |
| 519 |
|
| 520 |
switch e.opt { |
| 521 |
case "dircounts", "nodircounts", "dircounts!": |
| 522 |
err = applyLocalBoolOpt(gLocalOpts.dircounts, gOpts.dircounts, e) |
| 523 |
case "dirfirst", "nodirfirst", "dirfirst!": |
| 524 |
err = applyLocalBoolOpt(gLocalOpts.dirfirst, gOpts.dirfirst, e) |
| 525 |
if err == nil { |
| 526 |
app.nav.sort() |
| 527 |
} |
| 528 |
case "dironly", "nodironly", "dironly!": |
| 529 |
err = applyLocalBoolOpt(gLocalOpts.dironly, gOpts.dironly, e) |
| 530 |
if err == nil { |
| 531 |
app.nav.sort() |
| 532 |
app.nav.position() |
| 533 |
app.ui.loadFile(app, true) |
| 534 |
} |
| 535 |
case "hidden", "nohidden", "hidden!": |
| 536 |
err = applyLocalBoolOpt(gLocalOpts.hidden, gOpts.hidden, e) |
| 537 |
if err == nil { |
| 538 |
app.nav.sort() |
| 539 |
app.nav.position() |
| 540 |
app.ui.loadFile(app, true) |
| 541 |
} |
| 542 |
case "reverse", "noreverse", "reverse!": |
| 543 |
err = applyLocalBoolOpt(gLocalOpts.reverse, gOpts.reverse, e) |
| 544 |
if err == nil { |
| 545 |
app.nav.sort() |
| 546 |
} |
| 547 |
case "sortignorecase", "nosortignorecase", "sortignorecase!": |
| 548 |
err = applyLocalBoolOpt(gLocalOpts.sortignorecase, gOpts.sortignorecase, e) |
| 549 |
if err == nil { |
| 550 |
app.nav.sort() |
| 551 |
} |
| 552 |
case "sortignoredia", "nosortignoredia", "sortignoredia!": |
| 553 |
err = applyLocalBoolOpt(gLocalOpts.sortignoredia, gOpts.sortignoredia, e) |
| 554 |
if err == nil { |
| 555 |
app.nav.sort() |
| 556 |
} |
| 557 |
case "info": |
| 558 |
if e.val == "" { |
| 559 |
gLocalOpts.info[e.path] = nil |
| 560 |
return |
| 561 |
} |
| 562 |
toks := strings.Split(e.val, ":") |
| 563 |
for _, s := range toks { |
| 564 |
switch s { |
| 565 |
case "size", "time", "atime", "btime", "ctime", "perm", "user", "group", "custom": |
| 566 |
default: |
| 567 |
app.ui.echoerr("info: should consist of 'size', 'time', 'atime', 'btime', 'ctime', 'perm', 'user', 'group' or 'custom' separated with colon") |
| 568 |
return |
| 569 |
} |
| 570 |
} |
| 571 |
gLocalOpts.info[e.path] = toks |
| 572 |
case "sortby": |
| 573 |
method := sortMethod(e.val) |
| 574 |
if !isValidSortMethod(method) { |
| 575 |
app.ui.echoerr(invalidSortErrorMessage) |
| 576 |
return |
| 577 |
} |
| 578 |
gLocalOpts.sortby[e.path] = method |
| 579 |
app.nav.sort() |
| 580 |
default: |
| 581 |
err = fmt.Errorf("unknown option: %s", e.opt) |
| 582 |
} |
| 583 |
|
| 584 |
if err != nil { |
| 585 |
app.ui.echoerr(err.Error()) |
| 586 |
} |
| 587 |
} |
| 588 |
|
| 589 |
func (e *mapExpr) eval(app *app, _ []string) { |
| 590 |
if e.expr == nil { |
| 591 |
delete(gOpts.nkeys, e.keys) |
| 592 |
delete(gOpts.vkeys, e.keys) |
| 593 |
} else { |
| 594 |
gOpts.nkeys[e.keys] = e.expr |
| 595 |
gOpts.vkeys[e.keys] = e.expr |
| 596 |
} |
| 597 |
} |
| 598 |
|
| 599 |
func (e *nmapExpr) eval(app *app, _ []string) { |
| 600 |
if e.expr == nil { |
| 601 |
delete(gOpts.nkeys, e.keys) |
| 602 |
} else { |
| 603 |
gOpts.nkeys[e.keys] = e.expr |
| 604 |
} |
| 605 |
} |
| 606 |
|
| 607 |
func (e *vmapExpr) eval(app *app, _ []string) { |
| 608 |
if e.expr == nil { |
| 609 |
delete(gOpts.vkeys, e.keys) |
| 610 |
} else { |
| 611 |
gOpts.vkeys[e.keys] = e.expr |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
func (e *cmapExpr) eval(app *app, _ []string) { |
| 616 |
if e.expr == nil { |
| 617 |
delete(gOpts.cmdkeys, e.key) |
| 618 |
} else { |
| 619 |
gOpts.cmdkeys[e.key] = e.expr |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
func (e *cmdExpr) eval(app *app, _ []string) { |
| 624 |
if e.expr == nil { |
| 625 |
delete(gOpts.cmds, e.name) |
| 626 |
} else { |
| 627 |
gOpts.cmds[e.name] = e.expr |
| 628 |
} |
| 629 |
|
| 630 |
// only enable focus reporting if required by the user |
| 631 |
if e.name == "on-focus-gained" || e.name == "on-focus-lost" { |
| 632 |
_, onFocusGainedExists := gOpts.cmds["on-focus-gained"] |
| 633 |
_, onFocusLostExists := gOpts.cmds["on-focus-lost"] |
| 634 |
if onFocusGainedExists || onFocusLostExists { |
| 635 |
app.ui.screen.EnableFocus() |
| 636 |
} else { |
| 637 |
app.ui.screen.DisableFocus() |
| 638 |
} |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
func preChdir(app *app) { |
| 643 |
if cmd, ok := gOpts.cmds["pre-cd"]; ok { |
| 644 |
cmd.eval(app, nil) |
| 645 |
} |
| 646 |
} |
| 647 |
|
| 648 |
func onChdir(app *app) { |
| 649 |
app.nav.addJumpList() |
| 650 |
if cmd, ok := gOpts.cmds["on-cd"]; ok { |
| 651 |
cmd.eval(app, nil) |
| 652 |
} |
| 653 |
} |
| 654 |
|
| 655 |
func onLoad(app *app, files []string) { |
| 656 |
if cmd, ok := gOpts.cmds["on-load"]; ok { |
| 657 |
cmd.eval(app, files) |
| 658 |
} |
| 659 |
} |
| 660 |
|
| 661 |
func onFocusGained(app *app) { |
| 662 |
if cmd, ok := gOpts.cmds["on-focus-gained"]; ok { |
| 663 |
cmd.eval(app, nil) |
| 664 |
} |
| 665 |
} |
| 666 |
|
| 667 |
func onFocusLost(app *app) { |
| 668 |
if cmd, ok := gOpts.cmds["on-focus-lost"]; ok { |
| 669 |
cmd.eval(app, nil) |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
func onInit(app *app) { |
| 674 |
if cmd, ok := gOpts.cmds["on-init"]; ok { |
| 675 |
cmd.eval(app, nil) |
| 676 |
} |
| 677 |
} |
| 678 |
|
| 679 |
func onRedraw(app *app) { |
| 680 |
if cmd, ok := gOpts.cmds["on-redraw"]; ok { |
| 681 |
cmd.eval(app, nil) |
| 682 |
} |
| 683 |
} |
| 684 |
|
| 685 |
func onSelect(app *app) { |
| 686 |
app.nav.preload() |
| 687 |
if cmd, ok := gOpts.cmds["on-select"]; ok { |
| 688 |
cmd.eval(app, nil) |
| 689 |
} |
| 690 |
} |
| 691 |
|
| 692 |
func onQuit(app *app) { |
| 693 |
if cmd, ok := gOpts.cmds["on-quit"]; ok { |
| 694 |
cmd.eval(app, nil) |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
func splitKeys(s string) (keys []string) { |
| 699 |
for i := 0; i < len(s); { |
| 700 |
r, w := utf8.DecodeRuneInString(s[i:]) |
| 701 |
if r != '<' { |
| 702 |
keys = append(keys, s[i:i+w]) |
| 703 |
i += w |
| 704 |
} else { |
| 705 |
j := i + w |
| 706 |
for r != '>' && j < len(s) { |
| 707 |
r, w = utf8.DecodeRuneInString(s[j:]) |
| 708 |
j += w |
| 709 |
} |
| 710 |
keys = append(keys, s[i:j]) |
| 711 |
i = j |
| 712 |
} |
| 713 |
} |
| 714 |
return |
| 715 |
} |
| 716 |
|
| 717 |
func update(app *app) { |
| 718 |
exitCompMenu(app) |
| 719 |
app.cmdHistoryInput = nil |
| 720 |
|
| 721 |
switch { |
| 722 |
case gOpts.incsearch && app.ui.cmdPrefix == "/": |
| 723 |
app.nav.search = app.ui.cmdAccLeft + app.ui.cmdAccRight |
| 724 |
if app.nav.search == "" { |
| 725 |
return |
| 726 |
} |
| 727 |
|
| 728 |
dir := app.nav.currDir() |
| 729 |
old := dir.ind |
| 730 |
dir.ind = app.nav.searchInd |
| 731 |
dir.pos = app.nav.searchPos |
| 732 |
|
| 733 |
if _, err := app.nav.searchNext(); err != nil { |
| 734 |
app.ui.echoerrf("search: %s: %s", err, app.nav.search) |
| 735 |
} else if old != dir.ind { |
| 736 |
app.ui.loadFile(app, true) |
| 737 |
} |
| 738 |
case gOpts.incsearch && app.ui.cmdPrefix == "?": |
| 739 |
app.nav.search = app.ui.cmdAccLeft + app.ui.cmdAccRight |
| 740 |
if app.nav.search == "" { |
| 741 |
return |
| 742 |
} |
| 743 |
|
| 744 |
dir := app.nav.currDir() |
| 745 |
old := dir.ind |
| 746 |
dir.ind = app.nav.searchInd |
| 747 |
dir.pos = app.nav.searchPos |
| 748 |
|
| 749 |
if _, err := app.nav.searchPrev(); err != nil { |
| 750 |
app.ui.echoerrf("search: %s: %s", err, app.nav.search) |
| 751 |
} else if old != dir.ind { |
| 752 |
app.ui.loadFile(app, true) |
| 753 |
} |
| 754 |
case gOpts.incfilter && app.ui.cmdPrefix == "filter: ": |
| 755 |
filter := app.ui.cmdAccLeft + app.ui.cmdAccRight |
| 756 |
dir := app.nav.currDir() |
| 757 |
old := dir.ind |
| 758 |
|
| 759 |
if err := app.nav.setFilter(strings.Split(filter, " ")); err != nil { |
| 760 |
app.ui.echoerrf("filter: %s", err) |
| 761 |
} else if old != dir.ind { |
| 762 |
app.ui.loadFile(app, true) |
| 763 |
} |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
func restartIncCmd(app *app) { |
| 768 |
if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") { |
| 769 |
dir := app.nav.currDir() |
| 770 |
app.nav.searchInd = dir.ind |
| 771 |
app.nav.searchPos = dir.pos |
| 772 |
update(app) |
| 773 |
} else if gOpts.incfilter && app.ui.cmdPrefix == "filter: " { |
| 774 |
dir := app.nav.currDir() |
| 775 |
app.nav.prevFilter = dir.filter |
| 776 |
update(app) |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
func resetIncCmd(app *app) { |
| 781 |
if gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?") { |
| 782 |
dir := app.nav.currDir() |
| 783 |
dir.pos = app.nav.searchPos |
| 784 |
if dir.ind != app.nav.searchInd { |
| 785 |
dir.ind = app.nav.searchInd |
| 786 |
app.ui.loadFile(app, true) |
| 787 |
} |
| 788 |
} else if gOpts.incfilter && app.ui.cmdPrefix == "filter: " { |
| 789 |
dir := app.nav.currDir() |
| 790 |
old := dir.ind |
| 791 |
if err := app.nav.setFilter(app.nav.prevFilter); err != nil { |
| 792 |
log.Printf("reset filter: %s", err) |
| 793 |
} else if old != dir.ind { |
| 794 |
app.ui.loadFile(app, true) |
| 795 |
} |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
func normal(app *app) { |
| 800 |
resetIncCmd(app) |
| 801 |
exitCompMenu(app) |
| 802 |
|
| 803 |
app.cmdHistoryInd = 0 |
| 804 |
app.cmdHistoryInput = nil |
| 805 |
|
| 806 |
app.ui.cmdAccLeft = "" |
| 807 |
app.ui.cmdAccRight = "" |
| 808 |
app.ui.cmdPrefix = "" |
| 809 |
} |
| 810 |
|
| 811 |
func insert(app *app, arg string) { |
| 812 |
switch { |
| 813 |
case gOpts.incsearch && (app.ui.cmdPrefix == "/" || app.ui.cmdPrefix == "?"): |
| 814 |
app.ui.cmdAccLeft += arg |
| 815 |
update(app) |
| 816 |
case gOpts.incfilter && app.ui.cmdPrefix == "filter: ": |
| 817 |
app.ui.cmdAccLeft += arg |
| 818 |
update(app) |
| 819 |
case app.ui.cmdPrefix == "find: ": |
| 820 |
app.nav.find = app.ui.cmdAccLeft + arg + app.ui.cmdAccRight |
| 821 |
|
| 822 |
if gOpts.findlen == 0 { |
| 823 |
switch app.nav.findSingle() { |
| 824 |
case 0: |
| 825 |
app.ui.echoerrf("find: pattern not found: %s", app.nav.find) |
| 826 |
case 1: |
| 827 |
app.ui.loadFile(app, true) |
| 828 |
default: |
| 829 |
app.ui.cmdAccLeft += arg |
| 830 |
return |
| 831 |
} |
| 832 |
} else { |
| 833 |
if len(app.nav.find) < gOpts.findlen { |
| 834 |
app.ui.cmdAccLeft += arg |
| 835 |
return |
| 836 |
} |
| 837 |
|
| 838 |
if moved, found := app.nav.findNext(); !found { |
| 839 |
app.ui.echoerrf("find: pattern not found: %s", app.nav.find) |
| 840 |
} else if moved { |
| 841 |
app.ui.loadFile(app, true) |
| 842 |
} |
| 843 |
} |
| 844 |
|
| 845 |
normal(app) |
| 846 |
case app.ui.cmdPrefix == "find-back: ": |
| 847 |
app.nav.find = app.ui.cmdAccLeft + arg + app.ui.cmdAccRight |
| 848 |
|
| 849 |
if gOpts.findlen == 0 { |
| 850 |
switch app.nav.findSingle() { |
| 851 |
case 0: |
| 852 |
app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find) |
| 853 |
case 1: |
| 854 |
app.ui.loadFile(app, true) |
| 855 |
default: |
| 856 |
app.ui.cmdAccLeft += arg |
| 857 |
return |
| 858 |
} |
| 859 |
} else { |
| 860 |
if len(app.nav.find) < gOpts.findlen { |
| 861 |
app.ui.cmdAccLeft += arg |
| 862 |
return |
| 863 |
} |
| 864 |
|
| 865 |
if moved, found := app.nav.findPrev(); !found { |
| 866 |
app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find) |
| 867 |
} else if moved { |
| 868 |
app.ui.loadFile(app, true) |
| 869 |
} |
| 870 |
} |
| 871 |
|
| 872 |
normal(app) |
| 873 |
case strings.HasPrefix(app.ui.cmdPrefix, "delete"): |
| 874 |
normal(app) |
| 875 |
|
| 876 |
if arg == "y" { |
| 877 |
if err := app.nav.del(app); err != nil { |
| 878 |
app.ui.echoerrf("delete: %s", err) |
| 879 |
return |
| 880 |
} |
| 881 |
app.nav.unselect() |
| 882 |
app.ui.loadFile(app, true) |
| 883 |
} |
| 884 |
case strings.HasPrefix(app.ui.cmdPrefix, "replace"): |
| 885 |
normal(app) |
| 886 |
|
| 887 |
if arg == "y" { |
| 888 |
if err := app.nav.rename(); err != nil { |
| 889 |
app.ui.echoerrf("rename: %s", err) |
| 890 |
return |
| 891 |
} |
| 892 |
if gSingleMode { |
| 893 |
app.nav.renew() |
| 894 |
app.ui.loadFile(app, true) |
| 895 |
} else { |
| 896 |
if _, err := remote("send load"); err != nil { |
| 897 |
app.ui.echoerrf("rename: %s", err) |
| 898 |
return |
| 899 |
} |
| 900 |
} |
| 901 |
app.ui.loadFile(app, true) |
| 902 |
} |
| 903 |
case strings.HasPrefix(app.ui.cmdPrefix, "create"): |
| 904 |
normal(app) |
| 905 |
|
| 906 |
if arg == "y" { |
| 907 |
if err := os.MkdirAll(filepath.Dir(app.nav.renameNewPath), os.ModePerm); err != nil { |
| 908 |
app.ui.echoerrf("rename: %s", err) |
| 909 |
return |
| 910 |
} |
| 911 |
if err := app.nav.rename(); err != nil { |
| 912 |
app.ui.echoerrf("rename: %s", err) |
| 913 |
return |
| 914 |
} |
| 915 |
if gSingleMode { |
| 916 |
app.nav.renew() |
| 917 |
app.ui.loadFile(app, true) |
| 918 |
} else { |
| 919 |
if _, err := remote("send load"); err != nil { |
| 920 |
app.ui.echoerrf("rename: %s", err) |
| 921 |
return |
| 922 |
} |
| 923 |
} |
| 924 |
app.ui.loadFile(app, true) |
| 925 |
} |
| 926 |
case app.ui.cmdPrefix == "mark-save: ": |
| 927 |
normal(app) |
| 928 |
|
| 929 |
app.nav.marks[arg] = app.nav.currDir().path |
| 930 |
if err := app.nav.writeMarks(); err != nil { |
| 931 |
app.ui.echoerrf("mark-save: %s", err) |
| 932 |
return |
| 933 |
} |
| 934 |
if gSingleMode { |
| 935 |
if err := app.nav.sync(); err != nil { |
| 936 |
app.ui.echoerrf("mark-save: %s", err) |
| 937 |
return |
| 938 |
} |
| 939 |
} else { |
| 940 |
if _, err := remote("send sync"); err != nil { |
| 941 |
app.ui.echoerrf("mark-save: %s", err) |
| 942 |
return |
| 943 |
} |
| 944 |
} |
| 945 |
case app.ui.cmdPrefix == "mark-load: ": |
| 946 |
normal(app) |
| 947 |
|
| 948 |
path, ok := app.nav.marks[arg] |
| 949 |
if !ok { |
| 950 |
app.ui.echoerr("mark-load: no such mark") |
| 951 |
return |
| 952 |
} |
| 953 |
|
| 954 |
if err := cd(app, path); err != nil { |
| 955 |
app.ui.echoerrf("mark-load: %s", err) |
| 956 |
} |
| 957 |
case app.ui.cmdPrefix == "mark-remove: ": |
| 958 |
normal(app) |
| 959 |
if err := app.nav.removeMark(arg); err != nil { |
| 960 |
app.ui.echoerrf("mark-remove: %s", err) |
| 961 |
return |
| 962 |
} |
| 963 |
if err := app.nav.writeMarks(); err != nil { |
| 964 |
app.ui.echoerrf("mark-remove: %s", err) |
| 965 |
return |
| 966 |
} |
| 967 |
if gSingleMode { |
| 968 |
if err := app.nav.sync(); err != nil { |
| 969 |
app.ui.echoerrf("mark-remove: %s", err) |
| 970 |
return |
| 971 |
} |
| 972 |
} else { |
| 973 |
if _, err := remote("send sync"); err != nil { |
| 974 |
app.ui.echoerrf("mark-remove: %s", err) |
| 975 |
return |
| 976 |
} |
| 977 |
} |
| 978 |
case app.ui.cmdPrefix == ":" && app.ui.cmdAccLeft == "": |
| 979 |
switch arg { |
| 980 |
case "!", "$", "%", "&": |
| 981 |
app.ui.cmdPrefix = arg |
| 982 |
app.cmdHistoryInd = 0 |
| 983 |
app.cmdHistoryInput = nil |
| 984 |
return |
| 985 |
} |
| 986 |
fallthrough |
| 987 |
default: |
| 988 |
exitCompMenu(app) |
| 989 |
app.cmdHistoryInput = nil |
| 990 |
app.ui.cmdAccLeft += arg |
| 991 |
} |
| 992 |
} |
| 993 |
|
| 994 |
func cd(app *app, path string) error { |
| 995 |
wd := app.nav.currDir().path |
| 996 |
|
| 997 |
path, err := filepath.Abs(replaceTilde(path)) |
| 998 |
if err != nil { |
| 999 |
return fmt.Errorf("getting absolute path: %w", err) |
| 1000 |
} |
| 1001 |
|
| 1002 |
if path == wd { |
| 1003 |
return nil |
| 1004 |
} |
| 1005 |
|
| 1006 |
resetIncCmd(app) |
| 1007 |
preChdir(app) |
| 1008 |
|
| 1009 |
if err := app.nav.cd(path); err != nil { |
| 1010 |
return fmt.Errorf("changing directory: %w", err) |
| 1011 |
} |
| 1012 |
|
| 1013 |
app.ui.loadFile(app, true) |
| 1014 |
|
| 1015 |
app.nav.marks["'"] = wd |
| 1016 |
restartIncCmd(app) |
| 1017 |
onChdir(app) |
| 1018 |
|
| 1019 |
return nil |
| 1020 |
} |
| 1021 |
|
| 1022 |
func exitCompMenu(app *app) { |
| 1023 |
app.ui.menu = "" |
| 1024 |
app.ui.menuSelect = nil |
| 1025 |
app.menuCompActive = false |
| 1026 |
} |
| 1027 |
|
| 1028 |
func (e *callExpr) eval(app *app, _ []string) { |
| 1029 |
os.Setenv("lf_count", strconv.Itoa(e.count)) |
| 1030 |
|
| 1031 |
// commands that shouldn't clear the message line |
| 1032 |
silentCmds := []string{ |
| 1033 |
"addcustominfo", |
| 1034 |
"clearmaps", |
| 1035 |
"draw", |
| 1036 |
"load", |
| 1037 |
"push", |
| 1038 |
"redraw", |
| 1039 |
"source", |
| 1040 |
"sync", |
| 1041 |
"tty-write", |
| 1042 |
"on-focus-gained", |
| 1043 |
"on-focus-lost", |
| 1044 |
"on-init", |
| 1045 |
} |
| 1046 |
if !slices.Contains(silentCmds, e.name) && app.ui.cmdPrefix != ">" { |
| 1047 |
app.ui.echo("") |
| 1048 |
} |
| 1049 |
|
| 1050 |
switch e.name { |
| 1051 |
case "quit": |
| 1052 |
app.quitChan <- struct{}{} |
| 1053 |
case "up": |
| 1054 |
if app.nav.up(e.count) { |
| 1055 |
app.ui.loadFile(app, true) |
| 1056 |
} |
| 1057 |
case "half-up": |
| 1058 |
if app.nav.up(e.count * app.nav.height / 2) { |
| 1059 |
app.ui.loadFile(app, true) |
| 1060 |
} |
| 1061 |
case "page-up": |
| 1062 |
if app.nav.up(e.count * app.nav.height) { |
| 1063 |
app.ui.loadFile(app, true) |
| 1064 |
} |
| 1065 |
case "scroll-up": |
| 1066 |
if app.nav.scrollUp(e.count) { |
| 1067 |
app.ui.loadFile(app, true) |
| 1068 |
} |
| 1069 |
case "down": |
| 1070 |
if app.nav.down(e.count) { |
| 1071 |
app.ui.loadFile(app, true) |
| 1072 |
} |
| 1073 |
case "half-down": |
| 1074 |
if app.nav.down(e.count * app.nav.height / 2) { |
| 1075 |
app.ui.loadFile(app, true) |
| 1076 |
} |
| 1077 |
case "page-down": |
| 1078 |
if app.nav.down(e.count * app.nav.height) { |
| 1079 |
app.ui.loadFile(app, true) |
| 1080 |
} |
| 1081 |
case "scroll-down": |
| 1082 |
if app.nav.scrollDown(e.count) { |
| 1083 |
app.ui.loadFile(app, true) |
| 1084 |
} |
| 1085 |
case "updir": |
| 1086 |
resetIncCmd(app) |
| 1087 |
preChdir(app) |
| 1088 |
for range e.count { |
| 1089 |
if err := app.nav.updir(); err != nil { |
| 1090 |
app.ui.echoerrf("%s", err) |
| 1091 |
return |
| 1092 |
} |
| 1093 |
} |
| 1094 |
app.ui.loadFile(app, true) |
| 1095 |
restartIncCmd(app) |
| 1096 |
onChdir(app) |
| 1097 |
case "open": |
| 1098 |
curr := app.nav.currFile() |
| 1099 |
if curr == nil { |
| 1100 |
return |
| 1101 |
} |
| 1102 |
|
| 1103 |
if curr.IsDir() { |
| 1104 |
resetIncCmd(app) |
| 1105 |
preChdir(app) |
| 1106 |
err := app.nav.open() |
| 1107 |
if err != nil { |
| 1108 |
app.ui.echoerrf("opening directory: %s", err) |
| 1109 |
return |
| 1110 |
} |
| 1111 |
app.ui.loadFile(app, true) |
| 1112 |
restartIncCmd(app) |
| 1113 |
onChdir(app) |
| 1114 |
} else { |
| 1115 |
if gSelectionPath != "" || gPrintSelection { |
| 1116 |
app.selectionOut, _ = app.nav.currFileOrSelections() |
| 1117 |
app.quitChan <- struct{}{} |
| 1118 |
return |
| 1119 |
} |
| 1120 |
|
| 1121 |
if cmd, ok := gOpts.cmds["open"]; ok { |
| 1122 |
cmd.eval(app, e.args) |
| 1123 |
} |
| 1124 |
} |
| 1125 |
case "jump-next": |
| 1126 |
resetIncCmd(app) |
| 1127 |
preChdir(app) |
| 1128 |
for range e.count { |
| 1129 |
app.nav.cdJumpListNext() |
| 1130 |
} |
| 1131 |
app.ui.loadFile(app, true) |
| 1132 |
restartIncCmd(app) |
| 1133 |
onChdir(app) |
| 1134 |
case "jump-prev": |
| 1135 |
resetIncCmd(app) |
| 1136 |
preChdir(app) |
| 1137 |
for range e.count { |
| 1138 |
app.nav.cdJumpListPrev() |
| 1139 |
} |
| 1140 |
app.ui.loadFile(app, true) |
| 1141 |
restartIncCmd(app) |
| 1142 |
onChdir(app) |
| 1143 |
case "top": |
| 1144 |
var moved bool |
| 1145 |
if e.count == 1 { |
| 1146 |
moved = app.nav.top() |
| 1147 |
} else { |
| 1148 |
moved = app.nav.move(e.count - 1) |
| 1149 |
} |
| 1150 |
if moved { |
| 1151 |
app.ui.loadFile(app, true) |
| 1152 |
} |
| 1153 |
case "bottom": |
| 1154 |
var moved bool |
| 1155 |
if e.count == 1 { |
| 1156 |
// Different from Vim, which would treat a count of 1 as meaning to |
| 1157 |
// move to the first line (i.e. the top) |
| 1158 |
moved = app.nav.bottom() |
| 1159 |
} else { |
| 1160 |
moved = app.nav.move(e.count - 1) |
| 1161 |
} |
| 1162 |
if moved { |
| 1163 |
app.ui.loadFile(app, true) |
| 1164 |
} |
| 1165 |
case "high": |
| 1166 |
if app.nav.high() { |
| 1167 |
app.ui.loadFile(app, true) |
| 1168 |
} |
| 1169 |
case "middle": |
| 1170 |
if app.nav.middle() { |
| 1171 |
app.ui.loadFile(app, true) |
| 1172 |
} |
| 1173 |
case "low": |
| 1174 |
if app.nav.low() { |
| 1175 |
app.ui.loadFile(app, true) |
| 1176 |
} |
| 1177 |
case "toggle": |
| 1178 |
if len(e.args) == 0 { |
| 1179 |
app.nav.toggle() |
| 1180 |
} else { |
| 1181 |
for _, path := range e.args { |
| 1182 |
path, err := filepath.Abs(replaceTilde(path)) |
| 1183 |
if err != nil { |
| 1184 |
app.ui.echoerrf("toggle: %s", err) |
| 1185 |
continue |
| 1186 |
} |
| 1187 |
|
| 1188 |
if _, err := os.Lstat(path); os.IsNotExist(err) { |
| 1189 |
app.ui.echoerrf("toggle: %s", err) |
| 1190 |
continue |
| 1191 |
} |
| 1192 |
|
| 1193 |
app.nav.toggleSelection(path) |
| 1194 |
} |
| 1195 |
} |
| 1196 |
case "invert": |
| 1197 |
app.nav.invert() |
| 1198 |
case "unselect": |
| 1199 |
app.nav.unselect() |
| 1200 |
case "glob-select": |
| 1201 |
if len(e.args) != 1 { |
| 1202 |
app.ui.echoerr("glob-select: requires a pattern to match") |
| 1203 |
return |
| 1204 |
} |
| 1205 |
if err := app.nav.globSel(e.args[0], false); err != nil { |
| 1206 |
app.ui.echoerrf("%s", err) |
| 1207 |
return |
| 1208 |
} |
| 1209 |
case "glob-unselect": |
| 1210 |
if len(e.args) != 1 { |
| 1211 |
app.ui.echoerr("glob-unselect: requires a pattern to match") |
| 1212 |
return |
| 1213 |
} |
| 1214 |
if err := app.nav.globSel(e.args[0], true); err != nil { |
| 1215 |
app.ui.echoerrf("%s", err) |
| 1216 |
return |
| 1217 |
} |
| 1218 |
case "copy": |
| 1219 |
if err := app.nav.save(clipboardCopy); err != nil { |
| 1220 |
app.ui.echoerrf("copy: %s", err) |
| 1221 |
return |
| 1222 |
} |
| 1223 |
app.nav.unselect() |
| 1224 |
if gSingleMode { |
| 1225 |
if err := app.nav.sync(); err != nil { |
| 1226 |
app.ui.echoerrf("copy: %s", err) |
| 1227 |
return |
| 1228 |
} |
| 1229 |
} else { |
| 1230 |
if _, err := remote("send sync"); err != nil { |
| 1231 |
app.ui.echoerrf("copy: %s", err) |
| 1232 |
return |
| 1233 |
} |
| 1234 |
} |
| 1235 |
case "cut": |
| 1236 |
if err := app.nav.save(clipboardCut); err != nil { |
| 1237 |
app.ui.echoerrf("cut: %s", err) |
| 1238 |
return |
| 1239 |
} |
| 1240 |
app.nav.unselect() |
| 1241 |
if gSingleMode { |
| 1242 |
if err := app.nav.sync(); err != nil { |
| 1243 |
app.ui.echoerrf("cut: %s", err) |
| 1244 |
return |
| 1245 |
} |
| 1246 |
} else { |
| 1247 |
if _, err := remote("send sync"); err != nil { |
| 1248 |
app.ui.echoerrf("cut: %s", err) |
| 1249 |
return |
| 1250 |
} |
| 1251 |
} |
| 1252 |
case "paste": |
| 1253 |
if cmd, ok := gOpts.cmds["paste"]; ok { |
| 1254 |
cmd.eval(app, e.args) |
| 1255 |
} else if err := app.nav.paste(app); err != nil { |
| 1256 |
app.ui.echoerrf("paste: %s", err) |
| 1257 |
return |
| 1258 |
} |
| 1259 |
app.ui.loadFile(app, true) |
| 1260 |
case "clear": |
| 1261 |
if err := saveFiles(clipboard{nil, clipboardCut}); err != nil { |
| 1262 |
app.ui.echoerrf("clear: %s", err) |
| 1263 |
return |
| 1264 |
} |
| 1265 |
if gSingleMode { |
| 1266 |
if err := app.nav.sync(); err != nil { |
| 1267 |
app.ui.echoerrf("clear: %s", err) |
| 1268 |
return |
| 1269 |
} |
| 1270 |
} else { |
| 1271 |
if _, err := remote("send sync"); err != nil { |
| 1272 |
app.ui.echoerrf("clear: %s", err) |
| 1273 |
return |
| 1274 |
} |
| 1275 |
} |
| 1276 |
case "sync": |
| 1277 |
if err := app.nav.sync(); err != nil { |
| 1278 |
app.ui.echoerrf("sync: %s", err) |
| 1279 |
} |
| 1280 |
case "draw": |
| 1281 |
case "redraw": |
| 1282 |
app.ui.screen.Sync() |
| 1283 |
app.ui.renew() |
| 1284 |
app.nav.resize(app.ui) |
| 1285 |
app.ui.sxScreen.forceClear = true |
| 1286 |
app.ui.loadFile(app, true) |
| 1287 |
onRedraw(app) |
| 1288 |
case "load": |
| 1289 |
app.nav.renew() |
| 1290 |
app.ui.loadFile(app, false) |
| 1291 |
case "reload": |
| 1292 |
app.nav.reload() |
| 1293 |
app.ui.loadFile(app, true) |
| 1294 |
case "delete": |
| 1295 |
if cmd, ok := gOpts.cmds["delete"]; ok { |
| 1296 |
cmd.eval(app, e.args) |
| 1297 |
app.nav.unselect() |
| 1298 |
if gSingleMode { |
| 1299 |
app.nav.renew() |
| 1300 |
app.ui.loadFile(app, true) |
| 1301 |
} else { |
| 1302 |
if _, err := remote("send load"); err != nil { |
| 1303 |
app.ui.echoerrf("delete: %s", err) |
| 1304 |
return |
| 1305 |
} |
| 1306 |
} |
| 1307 |
} else { |
| 1308 |
list, err := app.nav.currFileOrSelections() |
| 1309 |
if err != nil { |
| 1310 |
app.ui.echoerrf("delete: %s", err) |
| 1311 |
return |
| 1312 |
} |
| 1313 |
|
| 1314 |
if app.ui.cmdPrefix == ">" { |
| 1315 |
return |
| 1316 |
} |
| 1317 |
normal(app) |
| 1318 |
if len(list) == 1 { |
| 1319 |
app.ui.cmdPrefix = "delete '" + list[0] + "'? [y/N] " |
| 1320 |
} else { |
| 1321 |
app.ui.cmdPrefix = "delete " + strconv.Itoa(len(list)) + " items? [y/N] " |
| 1322 |
} |
| 1323 |
} |
| 1324 |
case "rename": |
| 1325 |
if cmd, ok := gOpts.cmds["rename"]; ok { |
| 1326 |
cmd.eval(app, e.args) |
| 1327 |
if gSingleMode { |
| 1328 |
app.nav.renew() |
| 1329 |
app.ui.loadFile(app, true) |
| 1330 |
} else { |
| 1331 |
if _, err := remote("send load"); err != nil { |
| 1332 |
app.ui.echoerrf("rename: %s", err) |
| 1333 |
return |
| 1334 |
} |
| 1335 |
} |
| 1336 |
} else { |
| 1337 |
curr := app.nav.currFile() |
| 1338 |
if curr == nil { |
| 1339 |
app.ui.echoerr("rename: empty directory") |
| 1340 |
return |
| 1341 |
} |
| 1342 |
if app.ui.cmdPrefix == ">" { |
| 1343 |
return |
| 1344 |
} |
| 1345 |
normal(app) |
| 1346 |
app.ui.cmdPrefix = "rename: " |
| 1347 |
extension := getFileExtension(curr) |
| 1348 |
if len(extension) == 0 { |
| 1349 |
// no extension or .hidden or is directory |
| 1350 |
app.ui.cmdAccLeft = curr.Name() |
| 1351 |
} else { |
| 1352 |
app.ui.cmdAccLeft = strings.TrimSuffix(curr.Name(), extension) |
| 1353 |
app.ui.cmdAccRight = extension |
| 1354 |
} |
| 1355 |
} |
| 1356 |
case "read": |
| 1357 |
if app.ui.cmdPrefix == ">" { |
| 1358 |
return |
| 1359 |
} |
| 1360 |
normal(app) |
| 1361 |
app.ui.cmdPrefix = ":" |
| 1362 |
case "shell": |
| 1363 |
if app.ui.cmdPrefix == ">" { |
| 1364 |
return |
| 1365 |
} |
| 1366 |
normal(app) |
| 1367 |
app.ui.cmdPrefix = "$" |
| 1368 |
case "shell-pipe": |
| 1369 |
if app.ui.cmdPrefix == ">" { |
| 1370 |
return |
| 1371 |
} |
| 1372 |
normal(app) |
| 1373 |
app.ui.cmdPrefix = "%" |
| 1374 |
case "shell-wait": |
| 1375 |
if app.ui.cmdPrefix == ">" { |
| 1376 |
return |
| 1377 |
} |
| 1378 |
normal(app) |
| 1379 |
app.ui.cmdPrefix = "!" |
| 1380 |
case "shell-async": |
| 1381 |
if app.ui.cmdPrefix == ">" { |
| 1382 |
return |
| 1383 |
} |
| 1384 |
normal(app) |
| 1385 |
app.ui.cmdPrefix = "&" |
| 1386 |
case "find": |
| 1387 |
if app.ui.cmdPrefix == ">" { |
| 1388 |
return |
| 1389 |
} |
| 1390 |
normal(app) |
| 1391 |
app.ui.cmdPrefix = "find: " |
| 1392 |
app.nav.findBack = false |
| 1393 |
case "find-back": |
| 1394 |
if app.ui.cmdPrefix == ">" { |
| 1395 |
return |
| 1396 |
} |
| 1397 |
normal(app) |
| 1398 |
app.ui.cmdPrefix = "find-back: " |
| 1399 |
app.nav.findBack = true |
| 1400 |
case "find-next": |
| 1401 |
dir := app.nav.currDir() |
| 1402 |
old := dir.ind |
| 1403 |
for range e.count { |
| 1404 |
if app.nav.findBack { |
| 1405 |
app.nav.findPrev() |
| 1406 |
} else { |
| 1407 |
app.nav.findNext() |
| 1408 |
} |
| 1409 |
} |
| 1410 |
if old != dir.ind { |
| 1411 |
app.ui.loadFile(app, true) |
| 1412 |
} |
| 1413 |
case "find-prev": |
| 1414 |
dir := app.nav.currDir() |
| 1415 |
old := dir.ind |
| 1416 |
for range e.count { |
| 1417 |
if app.nav.findBack { |
| 1418 |
app.nav.findNext() |
| 1419 |
} else { |
| 1420 |
app.nav.findPrev() |
| 1421 |
} |
| 1422 |
} |
| 1423 |
if old != dir.ind { |
| 1424 |
app.ui.loadFile(app, true) |
| 1425 |
} |
| 1426 |
case "search": |
| 1427 |
if app.ui.cmdPrefix == ">" { |
| 1428 |
return |
| 1429 |
} |
| 1430 |
normal(app) |
| 1431 |
app.ui.cmdPrefix = "/" |
| 1432 |
dir := app.nav.currDir() |
| 1433 |
app.nav.searchInd = dir.ind |
| 1434 |
app.nav.searchPos = dir.pos |
| 1435 |
app.nav.searchBack = false |
| 1436 |
case "search-back": |
| 1437 |
if app.ui.cmdPrefix == ">" { |
| 1438 |
return |
| 1439 |
} |
| 1440 |
normal(app) |
| 1441 |
app.ui.cmdPrefix = "?" |
| 1442 |
dir := app.nav.currDir() |
| 1443 |
app.nav.searchInd = dir.ind |
| 1444 |
app.nav.searchPos = dir.pos |
| 1445 |
app.nav.searchBack = true |
| 1446 |
case "search-next": |
| 1447 |
for range e.count { |
| 1448 |
if app.nav.searchBack { |
| 1449 |
if moved, err := app.nav.searchPrev(); err != nil { |
| 1450 |
app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) |
| 1451 |
} else if moved { |
| 1452 |
app.ui.loadFile(app, true) |
| 1453 |
} |
| 1454 |
} else { |
| 1455 |
if moved, err := app.nav.searchNext(); err != nil { |
| 1456 |
app.ui.echoerrf("search: %s: %s", err, app.nav.search) |
| 1457 |
} else if moved { |
| 1458 |
app.ui.loadFile(app, true) |
| 1459 |
} |
| 1460 |
} |
| 1461 |
} |
| 1462 |
case "search-prev": |
| 1463 |
for range e.count { |
| 1464 |
if app.nav.searchBack { |
| 1465 |
if moved, err := app.nav.searchNext(); err != nil { |
| 1466 |
app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) |
| 1467 |
} else if moved { |
| 1468 |
app.ui.loadFile(app, true) |
| 1469 |
} |
| 1470 |
} else { |
| 1471 |
if moved, err := app.nav.searchPrev(); err != nil { |
| 1472 |
app.ui.echoerrf("search: %s: %s", err, app.nav.search) |
| 1473 |
} else if moved { |
| 1474 |
app.ui.loadFile(app, true) |
| 1475 |
} |
| 1476 |
} |
| 1477 |
} |
| 1478 |
case "filter": |
| 1479 |
if app.ui.cmdPrefix == ">" { |
| 1480 |
return |
| 1481 |
} |
| 1482 |
normal(app) |
| 1483 |
app.ui.cmdPrefix = "filter: " |
| 1484 |
dir := app.nav.currDir() |
| 1485 |
app.nav.prevFilter = dir.filter |
| 1486 |
if len(e.args) == 0 { |
| 1487 |
app.ui.cmdAccLeft = strings.Join(dir.filter, " ") |
| 1488 |
} else { |
| 1489 |
app.ui.cmdAccLeft = strings.Join(e.args, " ") |
| 1490 |
} |
| 1491 |
case "setfilter": |
| 1492 |
log.Printf("filter: %s", e.args) |
| 1493 |
if err := app.nav.setFilter(e.args); err != nil { |
| 1494 |
app.ui.echoerrf("filter: %s", err) |
| 1495 |
} |
| 1496 |
app.ui.loadFile(app, true) |
| 1497 |
case "mark-save": |
| 1498 |
if app.ui.cmdPrefix == ">" { |
| 1499 |
return |
| 1500 |
} |
| 1501 |
normal(app) |
| 1502 |
app.ui.cmdPrefix = "mark-save: " |
| 1503 |
case "mark-load": |
| 1504 |
if app.ui.cmdPrefix == ">" { |
| 1505 |
return |
| 1506 |
} |
| 1507 |
normal(app) |
| 1508 |
app.ui.menu = listMarks(app.nav.marks) |
| 1509 |
app.ui.cmdPrefix = "mark-load: " |
| 1510 |
case "mark-remove": |
| 1511 |
if app.ui.cmdPrefix == ">" { |
| 1512 |
return |
| 1513 |
} |
| 1514 |
normal(app) |
| 1515 |
app.ui.menu = listMarks(app.nav.marks) |
| 1516 |
app.ui.cmdPrefix = "mark-remove: " |
| 1517 |
case "tag": |
| 1518 |
tag := "*" |
| 1519 |
if len(e.args) != 0 { |
| 1520 |
tag = e.args[0] |
| 1521 |
} |
| 1522 |
|
| 1523 |
if err := app.nav.tag(tag); err != nil { |
| 1524 |
app.ui.echoerrf("tag: %s", err) |
| 1525 |
} else if err := app.nav.writeTags(); err != nil { |
| 1526 |
app.ui.echoerrf("tag: %s", err) |
| 1527 |
} |
| 1528 |
|
| 1529 |
if gSingleMode { |
| 1530 |
if err := app.nav.sync(); err != nil { |
| 1531 |
app.ui.echoerrf("tag: %s", err) |
| 1532 |
} |
| 1533 |
} else { |
| 1534 |
if _, err := remote("send sync"); err != nil { |
| 1535 |
app.ui.echoerrf("tag: %s", err) |
| 1536 |
} |
| 1537 |
} |
| 1538 |
case "tag-toggle": |
| 1539 |
tag := "*" |
| 1540 |
if len(e.args) != 0 { |
| 1541 |
tag = e.args[0] |
| 1542 |
} |
| 1543 |
|
| 1544 |
if err := app.nav.tagToggle(tag); err != nil { |
| 1545 |
app.ui.echoerrf("tag-toggle: %s", err) |
| 1546 |
} else if err := app.nav.writeTags(); err != nil { |
| 1547 |
app.ui.echoerrf("tag-toggle: %s", err) |
| 1548 |
} |
| 1549 |
|
| 1550 |
if gSingleMode { |
| 1551 |
if err := app.nav.sync(); err != nil { |
| 1552 |
app.ui.echoerrf("tag-toggle: %s", err) |
| 1553 |
} |
| 1554 |
} else { |
| 1555 |
if _, err := remote("send sync"); err != nil { |
| 1556 |
app.ui.echoerrf("tag-toggle: %s", err) |
| 1557 |
} |
| 1558 |
} |
| 1559 |
case "echo": |
| 1560 |
app.ui.echo(sanitizeMessage(strings.Join(e.args, " "))) |
| 1561 |
case "echomsg": |
| 1562 |
app.ui.echomsg(strings.Join(e.args, " ")) |
| 1563 |
case "echoerr": |
| 1564 |
app.ui.echoerr(strings.Join(e.args, " ")) |
| 1565 |
case "cd": |
| 1566 |
path := "~" |
| 1567 |
if len(e.args) > 0 { |
| 1568 |
path = e.args[0] |
| 1569 |
} |
| 1570 |
|
| 1571 |
if err := cd(app, path); err != nil { |
| 1572 |
app.ui.echoerrf("cd: %s", err) |
| 1573 |
} |
| 1574 |
case "select": |
| 1575 |
if len(e.args) != 1 { |
| 1576 |
app.ui.echoerr("select: requires an argument") |
| 1577 |
return |
| 1578 |
} |
| 1579 |
|
| 1580 |
path := replaceTilde(e.args[0]) |
| 1581 |
lstat, err := os.Lstat(path) |
| 1582 |
if err != nil { |
| 1583 |
app.ui.echoerrf("select: %s", err) |
| 1584 |
return |
| 1585 |
} |
| 1586 |
|
| 1587 |
path, err = filepath.Abs(replaceTilde(e.args[0])) |
| 1588 |
if err != nil { |
| 1589 |
app.ui.echoerrf("select: %s", err) |
| 1590 |
return |
| 1591 |
} |
| 1592 |
|
| 1593 |
if err := cd(app, filepath.Dir(path)); err != nil { |
| 1594 |
app.ui.echoerrf("select: %s", err) |
| 1595 |
return |
| 1596 |
} |
| 1597 |
|
| 1598 |
dir := app.nav.currDir() |
| 1599 |
app.nav.checkDir(dir) |
| 1600 |
if dir.loading { |
| 1601 |
dir.files = append(dir.files, &file{FileInfo: lstat}) |
| 1602 |
} |
| 1603 |
dir.sel(filepath.Base(path), app.nav.height) |
| 1604 |
app.ui.loadFile(app, true) |
| 1605 |
case "source": |
| 1606 |
if len(e.args) != 1 { |
| 1607 |
app.ui.echoerr("source: requires an argument") |
| 1608 |
return |
| 1609 |
} |
| 1610 |
app.readFile(replaceTilde(e.args[0])) |
| 1611 |
case "push": |
| 1612 |
if len(e.args) != 1 { |
| 1613 |
app.ui.echoerr("push: requires an argument") |
| 1614 |
return |
| 1615 |
} |
| 1616 |
log.Println("pushing keys", e.args[0]) |
| 1617 |
for _, val := range splitKeys(e.args[0]) { |
| 1618 |
app.ui.evChan <- parseKey(val) |
| 1619 |
} |
| 1620 |
case "addcustominfo": |
| 1621 |
var k, v string |
| 1622 |
switch len(e.args) { |
| 1623 |
case 1: |
| 1624 |
k, v = e.args[0], "" |
| 1625 |
case 2: |
| 1626 |
k, v = e.args[0], e.args[1] // don't trim to allow for custom alignment |
| 1627 |
default: |
| 1628 |
app.ui.echoerr("addcustominfo: requires either 1 or 2 arguments") |
| 1629 |
return |
| 1630 |
} |
| 1631 |
|
| 1632 |
path, err := filepath.Abs(replaceTilde(k)) |
| 1633 |
if err != nil { |
| 1634 |
app.ui.echoerrf("addcustominfo: %s", err) |
| 1635 |
return |
| 1636 |
} |
| 1637 |
|
| 1638 |
d := app.nav.getDir(filepath.Dir(path)) |
| 1639 |
|
| 1640 |
var f *file |
| 1641 |
for _, file := range d.allFiles { |
| 1642 |
if file.path == path { |
| 1643 |
f = file |
| 1644 |
break |
| 1645 |
} |
| 1646 |
} |
| 1647 |
if f == nil { |
| 1648 |
app.ui.echoerrf("addcustominfo: file not found: %s", path) |
| 1649 |
return |
| 1650 |
} |
| 1651 |
|
| 1652 |
if f.customInfo != v { |
| 1653 |
f.customInfo = v |
| 1654 |
// only sort when order changes |
| 1655 |
if getSortBy(d.path) == customSort { |
| 1656 |
d.sort() |
| 1657 |
} |
| 1658 |
} |
| 1659 |
case "calcdirsize": |
| 1660 |
err := app.nav.calcDirSize() |
| 1661 |
if err != nil { |
| 1662 |
app.ui.echoerrf("calcdirsize: %s", err) |
| 1663 |
return |
| 1664 |
} |
| 1665 |
app.nav.sort() |
| 1666 |
case "clearmaps": |
| 1667 |
// leave `:` and cmaps bound so the user can still exit using `:quit` |
| 1668 |
clear(gOpts.nkeys) |
| 1669 |
clear(gOpts.vkeys) |
| 1670 |
gOpts.nkeys[":"] = &callExpr{"read", nil, 1} |
| 1671 |
gOpts.vkeys[":"] = &callExpr{"read", nil, 1} |
| 1672 |
case "tty-write": |
| 1673 |
if len(e.args) != 1 { |
| 1674 |
app.ui.echoerr("tty-write: requires an argument") |
| 1675 |
return |
| 1676 |
} |
| 1677 |
|
| 1678 |
tty, ok := app.ui.screen.Tty() |
| 1679 |
if !ok { |
| 1680 |
log.Print("tty-write: failed to get tty") |
| 1681 |
return |
| 1682 |
} |
| 1683 |
|
| 1684 |
tty.Write([]byte(e.args[0])) |
| 1685 |
case "visual": |
| 1686 |
dir := app.nav.currDir() |
| 1687 |
dir.visualAnchor = dir.ind |
| 1688 |
dir.visualWrap = 0 |
| 1689 |
case "visual-accept": |
| 1690 |
dir := app.nav.currDir() |
| 1691 |
for _, path := range dir.visualSelections() { |
| 1692 |
if _, ok := app.nav.selections[path]; !ok { |
| 1693 |
app.nav.selections[path] = app.nav.selectionInd |
| 1694 |
app.nav.selectionInd++ |
| 1695 |
} |
| 1696 |
} |
| 1697 |
// resetting Visual mode here instead of inside `normal()` |
| 1698 |
// allows us to use Visual mode inside search, find etc. |
| 1699 |
dir.visualAnchor = -1 |
| 1700 |
normal(app) |
| 1701 |
case "visual-unselect": |
| 1702 |
dir := app.nav.currDir() |
| 1703 |
for _, path := range dir.visualSelections() { |
| 1704 |
delete(app.nav.selections, path) |
| 1705 |
} |
| 1706 |
if len(app.nav.selections) == 0 { |
| 1707 |
app.nav.selectionInd = 0 |
| 1708 |
} |
| 1709 |
dir.visualAnchor = -1 |
| 1710 |
normal(app) |
| 1711 |
case "visual-discard": |
| 1712 |
dir := app.nav.currDir() |
| 1713 |
dir.visualAnchor = -1 |
| 1714 |
normal(app) |
| 1715 |
case "visual-change": |
| 1716 |
if !app.nav.isVisualMode() { |
| 1717 |
return |
| 1718 |
} |
| 1719 |
dir := app.nav.currDir() |
| 1720 |
old := dir.ind |
| 1721 |
beg := max(dir.ind-dir.pos, 0) |
| 1722 |
dir.ind, dir.visualAnchor = dir.visualAnchor, dir.ind |
| 1723 |
dir.pos = dir.ind - beg |
| 1724 |
dir.visualWrap = -dir.visualWrap |
| 1725 |
dir.boundPos(app.nav.height) |
| 1726 |
if old != dir.ind { |
| 1727 |
app.ui.loadFile(app, true) |
| 1728 |
} |
| 1729 |
case "cmd-insert": |
| 1730 |
if len(e.args) == 0 { |
| 1731 |
return |
| 1732 |
} |
| 1733 |
insert(app, e.args[0]) |
| 1734 |
case "cmd-escape": |
| 1735 |
if app.ui.cmdPrefix == ">" { |
| 1736 |
return |
| 1737 |
} |
| 1738 |
normal(app) |
| 1739 |
case "cmd-complete": |
| 1740 |
app.doComplete() |
| 1741 |
case "cmd-menu-complete": |
| 1742 |
app.menuComplete(1) |
| 1743 |
case "cmd-menu-complete-back": |
| 1744 |
app.menuComplete(-1) |
| 1745 |
case "cmd-menu-accept": |
| 1746 |
exitCompMenu(app) |
| 1747 |
case "cmd-menu-discard": |
| 1748 |
if app.menuCompActive { |
| 1749 |
app.ui.cmdAccLeft = strings.Join(app.menuCompTmp, " ") |
| 1750 |
} |
| 1751 |
exitCompMenu(app) |
| 1752 |
case "cmd-enter": |
| 1753 |
s := app.ui.cmdAccLeft + app.ui.cmdAccRight |
| 1754 |
if len(s) == 0 && app.ui.cmdPrefix != "filter: " && app.ui.cmdPrefix != ">" { |
| 1755 |
return |
| 1756 |
} |
| 1757 |
|
| 1758 |
exitCompMenu(app) |
| 1759 |
|
| 1760 |
app.ui.cmdAccLeft = "" |
| 1761 |
app.ui.cmdAccRight = "" |
| 1762 |
|
| 1763 |
switch app.ui.cmdPrefix { |
| 1764 |
case ":": |
| 1765 |
log.Printf("command: %s", s) |
| 1766 |
app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) |
| 1767 |
app.ui.cmdPrefix = "" |
| 1768 |
p := newParser(strings.NewReader(s)) |
| 1769 |
for p.parse() { |
| 1770 |
p.expr.eval(app, nil) |
| 1771 |
} |
| 1772 |
if p.err != nil { |
| 1773 |
app.ui.echoerrf("%s", p.err) |
| 1774 |
} |
| 1775 |
case "$": |
| 1776 |
log.Printf("shell: %s", s) |
| 1777 |
app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) |
| 1778 |
app.ui.cmdPrefix = "" |
| 1779 |
app.runShell(s, nil, "$") |
| 1780 |
case "%": |
| 1781 |
log.Printf("shell-pipe: %s", s) |
| 1782 |
app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) |
| 1783 |
app.runShell(s, nil, "%") |
| 1784 |
case ">": |
| 1785 |
io.WriteString(app.cmdIn, s+"\n") |
| 1786 |
app.cmdOutBuf = nil |
| 1787 |
case "!": |
| 1788 |
log.Printf("shell-wait: %s", s) |
| 1789 |
app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) |
| 1790 |
app.ui.cmdPrefix = "" |
| 1791 |
app.runShell(s, nil, "!") |
| 1792 |
case "&": |
| 1793 |
log.Printf("shell-async: %s", s) |
| 1794 |
app.cmdHistory = append(app.cmdHistory, app.ui.cmdPrefix+s) |
| 1795 |
app.ui.cmdPrefix = "" |
| 1796 |
app.runShell(s, nil, "&") |
| 1797 |
case "/": |
| 1798 |
dir := app.nav.currDir() |
| 1799 |
old := dir.ind |
| 1800 |
if gOpts.incsearch { |
| 1801 |
dir.ind = app.nav.searchInd |
| 1802 |
dir.pos = app.nav.searchPos |
| 1803 |
} |
| 1804 |
log.Printf("search: %s", s) |
| 1805 |
app.ui.cmdPrefix = "" |
| 1806 |
app.nav.search = s |
| 1807 |
if _, err := app.nav.searchNext(); err != nil { |
| 1808 |
app.ui.echoerrf("search: %s: %s", err, app.nav.search) |
| 1809 |
} else if old != dir.ind { |
| 1810 |
app.ui.loadFile(app, true) |
| 1811 |
} |
| 1812 |
case "?": |
| 1813 |
dir := app.nav.currDir() |
| 1814 |
old := dir.ind |
| 1815 |
if gOpts.incsearch { |
| 1816 |
dir.ind = app.nav.searchInd |
| 1817 |
dir.pos = app.nav.searchPos |
| 1818 |
} |
| 1819 |
log.Printf("search-back: %s", s) |
| 1820 |
app.ui.cmdPrefix = "" |
| 1821 |
app.nav.search = s |
| 1822 |
if _, err := app.nav.searchPrev(); err != nil { |
| 1823 |
app.ui.echoerrf("search-back: %s: %s", err, app.nav.search) |
| 1824 |
} else if old != dir.ind { |
| 1825 |
app.ui.loadFile(app, true) |
| 1826 |
} |
| 1827 |
case "filter: ": |
| 1828 |
log.Printf("filter: %s", s) |
| 1829 |
app.ui.cmdPrefix = "" |
| 1830 |
if err := app.nav.setFilter(strings.Split(s, " ")); err != nil { |
| 1831 |
app.ui.echoerrf("filter: %s", err) |
| 1832 |
} |
| 1833 |
app.ui.loadFile(app, true) |
| 1834 |
case "find: ": |
| 1835 |
app.ui.cmdPrefix = "" |
| 1836 |
if moved, found := app.nav.findNext(); !found { |
| 1837 |
app.ui.echoerrf("find: pattern not found: %s", app.nav.find) |
| 1838 |
} else if moved { |
| 1839 |
app.ui.loadFile(app, true) |
| 1840 |
} |
| 1841 |
case "find-back: ": |
| 1842 |
app.ui.cmdPrefix = "" |
| 1843 |
if moved, found := app.nav.findPrev(); !found { |
| 1844 |
app.ui.echoerrf("find-back: pattern not found: %s", app.nav.find) |
| 1845 |
} else if moved { |
| 1846 |
app.ui.loadFile(app, true) |
| 1847 |
} |
| 1848 |
case "rename: ": |
| 1849 |
app.ui.cmdPrefix = "" |
| 1850 |
|
| 1851 |
curr := app.nav.currFile() |
| 1852 |
if curr == nil { |
| 1853 |
app.ui.echoerr("rename: empty directory") |
| 1854 |
return |
| 1855 |
} |
| 1856 |
wd, err := os.Getwd() |
| 1857 |
if err != nil { |
| 1858 |
log.Printf("getting current directory: %s", err) |
| 1859 |
return |
| 1860 |
} |
| 1861 |
|
| 1862 |
oldPath := filepath.Join(wd, curr.Name()) |
| 1863 |
newPath := filepath.Clean(replaceTilde(s)) |
| 1864 |
if !filepath.IsAbs(newPath) { |
| 1865 |
newPath = filepath.Join(wd, newPath) |
| 1866 |
} |
| 1867 |
if oldPath == newPath { |
| 1868 |
return |
| 1869 |
} |
| 1870 |
app.nav.renameOldPath = oldPath |
| 1871 |
app.nav.renameNewPath = newPath |
| 1872 |
|
| 1873 |
newDir := filepath.Dir(newPath) |
| 1874 |
if _, err := os.Stat(newDir); os.IsNotExist(err) { |
| 1875 |
app.ui.cmdPrefix = "create '" + newDir + "'? [y/N] " |
| 1876 |
return |
| 1877 |
} |
| 1878 |
|
| 1879 |
oldStat, err := os.Lstat(oldPath) |
| 1880 |
if err != nil { |
| 1881 |
app.ui.echoerrf("rename: %s", err) |
| 1882 |
return |
| 1883 |
} |
| 1884 |
if newStat, err := os.Lstat(newPath); !os.IsNotExist(err) && !os.SameFile(oldStat, newStat) { |
| 1885 |
app.ui.cmdPrefix = "replace '" + newPath + "'? [y/N] " |
| 1886 |
return |
| 1887 |
} |
| 1888 |
|
| 1889 |
if err := app.nav.rename(); err != nil { |
| 1890 |
app.ui.echoerrf("rename: %s", err) |
| 1891 |
return |
| 1892 |
} |
| 1893 |
|
| 1894 |
if gSingleMode { |
| 1895 |
app.nav.renew() |
| 1896 |
} else { |
| 1897 |
if _, err := remote("send load"); err != nil { |
| 1898 |
app.ui.echoerrf("rename: %s", err) |
| 1899 |
return |
| 1900 |
} |
| 1901 |
} |
| 1902 |
app.ui.loadFile(app, true) |
| 1903 |
default: |
| 1904 |
log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix) |
| 1905 |
} |
| 1906 |
case "cmd-interrupt": |
| 1907 |
if app.cmd != nil { |
| 1908 |
err := shellKill(app.cmd) |
| 1909 |
if err != nil { |
| 1910 |
app.ui.echoerrf("kill: %s", err) |
| 1911 |
} else { |
| 1912 |
app.ui.echoerr("process interrupt") |
| 1913 |
} |
| 1914 |
} |
| 1915 |
normal(app) |
| 1916 |
case "cmd-history-next": |
| 1917 |
if !slices.Contains([]string{":", "$", "!", "%", "&"}, app.ui.cmdPrefix) { |
| 1918 |
return |
| 1919 |
} |
| 1920 |
input := app.ui.cmdPrefix + app.ui.cmdAccLeft |
| 1921 |
if app.cmdHistoryInput == nil { |
| 1922 |
app.cmdHistoryInput = &input |
| 1923 |
} |
| 1924 |
for i := app.cmdHistoryInd - 1; i >= 0; i-- { |
| 1925 |
if i == 0 { |
| 1926 |
if *app.cmdHistoryInput == "" { |
| 1927 |
normal(app) |
| 1928 |
} else { |
| 1929 |
exitCompMenu(app) |
| 1930 |
app.ui.cmdAccLeft = "" |
| 1931 |
app.cmdHistoryInd = 0 |
| 1932 |
} |
| 1933 |
break |
| 1934 |
} |
| 1935 |
cmd := app.cmdHistory[len(app.cmdHistory)-i] |
| 1936 |
if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input { |
| 1937 |
exitCompMenu(app) |
| 1938 |
app.ui.cmdPrefix = cmd[:1] |
| 1939 |
app.ui.cmdAccLeft = cmd[1:] |
| 1940 |
app.cmdHistoryInd = i |
| 1941 |
break |
| 1942 |
} |
| 1943 |
} |
| 1944 |
case "cmd-history-prev": |
| 1945 |
if !slices.Contains([]string{":", "$", "!", "%", "&", ""}, app.ui.cmdPrefix) { |
| 1946 |
return |
| 1947 |
} |
| 1948 |
input := app.ui.cmdPrefix + app.ui.cmdAccLeft |
| 1949 |
if app.cmdHistoryInput == nil { |
| 1950 |
app.cmdHistoryInput = &input |
| 1951 |
} |
| 1952 |
for i := app.cmdHistoryInd + 1; i <= len(app.cmdHistory); i++ { |
| 1953 |
cmd := app.cmdHistory[len(app.cmdHistory)-i] |
| 1954 |
if strings.HasPrefix(cmd, *app.cmdHistoryInput) && cmd != input { |
| 1955 |
exitCompMenu(app) |
| 1956 |
app.ui.cmdPrefix = cmd[:1] |
| 1957 |
app.ui.cmdAccLeft = cmd[1:] |
| 1958 |
app.cmdHistoryInd = i |
| 1959 |
break |
| 1960 |
} |
| 1961 |
} |
| 1962 |
case "cmd-left": |
| 1963 |
if app.ui.cmdAccLeft == "" { |
| 1964 |
return |
| 1965 |
} |
| 1966 |
last := lastGraphemeCluster(app.ui.cmdAccLeft) |
| 1967 |
app.ui.cmdAccLeft = strings.TrimSuffix(app.ui.cmdAccLeft, last) |
| 1968 |
app.ui.cmdAccRight = last + app.ui.cmdAccRight |
| 1969 |
case "cmd-right": |
| 1970 |
if app.ui.cmdAccRight == "" { |
| 1971 |
return |
| 1972 |
} |
| 1973 |
first := firstGraphemeCluster(app.ui.cmdAccRight) |
| 1974 |
app.ui.cmdAccLeft += first |
| 1975 |
app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first) |
| 1976 |
case "cmd-home": |
| 1977 |
app.ui.cmdAccRight = app.ui.cmdAccLeft + app.ui.cmdAccRight |
| 1978 |
app.ui.cmdAccLeft = "" |
| 1979 |
case "cmd-end": |
| 1980 |
app.ui.cmdAccLeft += app.ui.cmdAccRight |
| 1981 |
app.ui.cmdAccRight = "" |
| 1982 |
case "cmd-delete": |
| 1983 |
if app.ui.cmdAccRight == "" { |
| 1984 |
return |
| 1985 |
} |
| 1986 |
first := firstGraphemeCluster(app.ui.cmdAccRight) |
| 1987 |
app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first) |
| 1988 |
update(app) |
| 1989 |
case "cmd-delete-back": |
| 1990 |
if app.ui.cmdAccLeft == "" { |
| 1991 |
switch app.ui.cmdPrefix { |
| 1992 |
case "!", "$", "%", "&": |
| 1993 |
app.ui.cmdPrefix = ":" |
| 1994 |
app.cmdHistoryInd = 0 |
| 1995 |
app.cmdHistoryInput = nil |
| 1996 |
case ">", "rename: ", "filter: ": |
| 1997 |
// Don't mess with programs waiting for input. |
| 1998 |
// Exiting on backspace is also inconvenient for 'rename' and 'filter', |
| 1999 |
// since the text field can start out nonempty. |
| 2000 |
default: |
| 2001 |
normal(app) |
| 2002 |
} |
| 2003 |
return |
| 2004 |
} |
| 2005 |
last := lastGraphemeCluster(app.ui.cmdAccLeft) |
| 2006 |
app.ui.cmdAccLeft = strings.TrimSuffix(app.ui.cmdAccLeft, last) |
| 2007 |
update(app) |
| 2008 |
case "cmd-delete-home": |
| 2009 |
if app.ui.cmdAccLeft == "" { |
| 2010 |
return |
| 2011 |
} |
| 2012 |
app.ui.cmdYankBuf = app.ui.cmdAccLeft |
| 2013 |
app.ui.cmdAccLeft = "" |
| 2014 |
update(app) |
| 2015 |
case "cmd-delete-end": |
| 2016 |
if app.ui.cmdAccRight == "" { |
| 2017 |
return |
| 2018 |
} |
| 2019 |
app.ui.cmdYankBuf = app.ui.cmdAccRight |
| 2020 |
app.ui.cmdAccRight = "" |
| 2021 |
update(app) |
| 2022 |
case "cmd-delete-unix-word": |
| 2023 |
ind := strings.LastIndex(strings.TrimRight(app.ui.cmdAccLeft, " "), " ") + 1 |
| 2024 |
app.ui.cmdYankBuf = app.ui.cmdAccLeft[ind:] |
| 2025 |
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] |
| 2026 |
update(app) |
| 2027 |
case "cmd-yank": |
| 2028 |
app.ui.cmdAccLeft += app.ui.cmdYankBuf |
| 2029 |
update(app) |
| 2030 |
case "cmd-transpose": |
| 2031 |
var c []string |
| 2032 |
gr := displaywidth.StringGraphemes(app.ui.cmdAccLeft) |
| 2033 |
for gr.Next() { |
| 2034 |
c = append(c, gr.Value()) |
| 2035 |
} |
| 2036 |
|
| 2037 |
first := firstGraphemeCluster(app.ui.cmdAccRight) |
| 2038 |
if first != "" { |
| 2039 |
c = append(c, first) |
| 2040 |
} |
| 2041 |
|
| 2042 |
if len(c) < 2 { |
| 2043 |
return |
| 2044 |
} |
| 2045 |
|
| 2046 |
app.ui.cmdAccRight = strings.TrimPrefix(app.ui.cmdAccRight, first) |
| 2047 |
|
| 2048 |
c[len(c)-1], c[len(c)-2] = c[len(c)-2], c[len(c)-1] |
| 2049 |
app.ui.cmdAccLeft = strings.Join(c, "") |
| 2050 |
update(app) |
| 2051 |
case "cmd-transpose-word": |
| 2052 |
if app.ui.cmdAccLeft == "" { |
| 2053 |
return |
| 2054 |
} |
| 2055 |
|
| 2056 |
locs := reWord.FindAllStringIndex(app.ui.cmdAccLeft, -1) |
| 2057 |
if len(locs) < 2 { |
| 2058 |
return |
| 2059 |
} |
| 2060 |
|
| 2061 |
if app.ui.cmdAccRight != "" { |
| 2062 |
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) |
| 2063 |
if loc != nil { |
| 2064 |
ind := loc[3] |
| 2065 |
app.ui.cmdAccLeft += app.ui.cmdAccRight[:ind] |
| 2066 |
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] |
| 2067 |
} |
| 2068 |
} |
| 2069 |
|
| 2070 |
locs = reWord.FindAllStringIndex(app.ui.cmdAccLeft, -1) |
| 2071 |
|
| 2072 |
beg1, end1 := locs[len(locs)-2][0], locs[len(locs)-2][1] |
| 2073 |
beg2, end2 := locs[len(locs)-1][0], locs[len(locs)-1][1] |
| 2074 |
|
| 2075 |
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:beg1] + |
| 2076 |
app.ui.cmdAccLeft[beg2:end2] + |
| 2077 |
app.ui.cmdAccLeft[end1:beg2] + |
| 2078 |
app.ui.cmdAccLeft[beg1:end1] + |
| 2079 |
app.ui.cmdAccLeft[end2:] |
| 2080 |
update(app) |
| 2081 |
case "cmd-word": |
| 2082 |
if app.ui.cmdAccRight == "" { |
| 2083 |
return |
| 2084 |
} |
| 2085 |
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) |
| 2086 |
if loc == nil { |
| 2087 |
return |
| 2088 |
} |
| 2089 |
ind := loc[3] |
| 2090 |
app.ui.cmdAccLeft += app.ui.cmdAccRight[:ind] |
| 2091 |
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] |
| 2092 |
case "cmd-word-back": |
| 2093 |
if app.ui.cmdAccLeft == "" { |
| 2094 |
return |
| 2095 |
} |
| 2096 |
locs := reWordBeg.FindAllStringSubmatchIndex(app.ui.cmdAccLeft, -1) |
| 2097 |
if locs == nil { |
| 2098 |
return |
| 2099 |
} |
| 2100 |
ind := locs[len(locs)-1][3] |
| 2101 |
app.ui.cmdAccRight = app.ui.cmdAccLeft[ind:] + app.ui.cmdAccRight |
| 2102 |
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] |
| 2103 |
case "cmd-delete-word": |
| 2104 |
if app.ui.cmdAccRight == "" { |
| 2105 |
return |
| 2106 |
} |
| 2107 |
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) |
| 2108 |
if loc == nil { |
| 2109 |
return |
| 2110 |
} |
| 2111 |
ind := loc[3] |
| 2112 |
app.ui.cmdYankBuf = app.ui.cmdAccRight[:ind] |
| 2113 |
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] |
| 2114 |
update(app) |
| 2115 |
case "cmd-delete-word-back": |
| 2116 |
if app.ui.cmdAccLeft == "" { |
| 2117 |
return |
| 2118 |
} |
| 2119 |
locs := reWordBeg.FindAllStringSubmatchIndex(app.ui.cmdAccLeft, -1) |
| 2120 |
if locs == nil { |
| 2121 |
return |
| 2122 |
} |
| 2123 |
ind := locs[len(locs)-1][3] |
| 2124 |
app.ui.cmdYankBuf = app.ui.cmdAccLeft[ind:] |
| 2125 |
app.ui.cmdAccLeft = app.ui.cmdAccLeft[:ind] |
| 2126 |
update(app) |
| 2127 |
case "cmd-capitalize-word": |
| 2128 |
if app.ui.cmdAccRight == "" { |
| 2129 |
return |
| 2130 |
} |
| 2131 |
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) |
| 2132 |
if loc == nil { |
| 2133 |
return |
| 2134 |
} |
| 2135 |
ind := loc[3] |
| 2136 |
capitalize := func(s string) string { |
| 2137 |
runes := []rune(s) |
| 2138 |
for i, r := range runes { |
| 2139 |
if !unicode.IsSpace(r) { |
| 2140 |
runes[i] = unicode.ToUpper(r) |
| 2141 |
break |
| 2142 |
} |
| 2143 |
} |
| 2144 |
return string(runes) |
| 2145 |
} |
| 2146 |
app.ui.cmdAccLeft += capitalize(app.ui.cmdAccRight[:ind]) |
| 2147 |
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] |
| 2148 |
update(app) |
| 2149 |
case "cmd-uppercase-word": |
| 2150 |
if app.ui.cmdAccRight == "" { |
| 2151 |
return |
| 2152 |
} |
| 2153 |
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) |
| 2154 |
if loc == nil { |
| 2155 |
return |
| 2156 |
} |
| 2157 |
ind := loc[3] |
| 2158 |
app.ui.cmdAccLeft += strings.ToUpper(app.ui.cmdAccRight[:ind]) |
| 2159 |
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] |
| 2160 |
update(app) |
| 2161 |
case "cmd-lowercase-word": |
| 2162 |
if app.ui.cmdAccRight == "" { |
| 2163 |
return |
| 2164 |
} |
| 2165 |
loc := reWordEnd.FindStringSubmatchIndex(app.ui.cmdAccRight) |
| 2166 |
if loc == nil { |
| 2167 |
return |
| 2168 |
} |
| 2169 |
ind := loc[3] |
| 2170 |
app.ui.cmdAccLeft += strings.ToLower(app.ui.cmdAccRight[:ind]) |
| 2171 |
app.ui.cmdAccRight = app.ui.cmdAccRight[ind:] |
| 2172 |
update(app) |
| 2173 |
case "on-focus-gained": |
| 2174 |
onFocusGained(app) |
| 2175 |
case "on-focus-lost": |
| 2176 |
onFocusLost(app) |
| 2177 |
case "on-init": |
| 2178 |
onInit(app) |
| 2179 |
default: |
| 2180 |
cmd, ok := gOpts.cmds[e.name] |
| 2181 |
if !ok { |
| 2182 |
app.ui.echoerrf("command not found: %s", e.name) |
| 2183 |
return |
| 2184 |
} |
| 2185 |
cmd.eval(app, e.args) |
| 2186 |
} |
| 2187 |
} |
| 2188 |
|
| 2189 |
func (e *execExpr) eval(app *app, args []string) { |
| 2190 |
switch e.prefix { |
| 2191 |
case "$": |
| 2192 |
log.Printf("shell: %s -- %s", e, args) |
| 2193 |
app.runShell(e.value, args, e.prefix) |
| 2194 |
case "%": |
| 2195 |
log.Printf("shell-pipe: %s -- %s", e, args) |
| 2196 |
app.runShell(e.value, args, e.prefix) |
| 2197 |
case "!": |
| 2198 |
log.Printf("shell-wait: %s -- %s", e, args) |
| 2199 |
app.runShell(e.value, args, e.prefix) |
| 2200 |
case "&": |
| 2201 |
log.Printf("shell-async: %s -- %s", e, args) |
| 2202 |
app.runShell(e.value, args, e.prefix) |
| 2203 |
default: |
| 2204 |
log.Printf("evaluating unknown execution prefix: %q", e.prefix) |
| 2205 |
} |
| 2206 |
} |
| 2207 |
|
| 2208 |
func (e *listExpr) eval(app *app, _ []string) { |
| 2209 |
for range e.count { |
| 2210 |
for _, expr := range e.exprs { |
| 2211 |
expr.eval(app, nil) |
| 2212 |
} |
| 2213 |
} |
| 2214 |
} |