allow defining a custom rename command

This commit is contained in:
Gokcehan 2019-07-21 20:40:57 +03:00
parent bfd01e5ed8
commit 9f661344ed
3 changed files with 36 additions and 11 deletions

View File

@ -42,9 +42,9 @@ cmd open ${{
esac esac
}} }}
# rename current file without overwrite # define a custom 'rename' command without prompt for overwrite
cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1 # cmd rename %[ -e $1 ] && printf "file exists" || mv $f $1
map r push :rename<space> # map r push :rename<space>
# make sure trash folder exists # make sure trash folder exists
# %mkdir -p ~/.trash # %mkdir -p ~/.trash

33
eval.go
View File

@ -470,7 +470,10 @@ func insert(app *app, arg string) {
app.nav.unselect() app.nav.unselect()
if err := remote("send load"); err != nil { if err := remote("send load"); err != nil {
app.ui.echoerrf("delete: %s", err) app.ui.echoerrf("delete: %s", err)
return
} }
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
} }
case strings.HasPrefix(app.ui.cmdPrefix, "replace") || case strings.HasPrefix(app.ui.cmdPrefix, "replace") ||
strings.HasPrefix(app.ui.cmdPrefix, "create path"): strings.HasPrefix(app.ui.cmdPrefix, "create path"):
@ -481,8 +484,13 @@ func insert(app *app, arg string) {
app.ui.echoerrf("rename: %s", err) app.ui.echoerrf("rename: %s", err)
return return
} }
if err := remote("send load"); err != nil {
app.ui.echoerrf("rename: %s", err)
return
}
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
} }
case app.ui.cmdPrefix == "mark-save: ": case app.ui.cmdPrefix == "mark-save: ":
normal(app) normal(app)
@ -829,14 +837,23 @@ func (e *callExpr) eval(app *app, args []string) {
app.ui.menuBuf = listMarks(app.nav.marks) app.ui.menuBuf = listMarks(app.nav.marks)
app.ui.cmdPrefix = "mark-remove: " app.ui.cmdPrefix = "mark-remove: "
case "rename": case "rename":
if curr, err := app.nav.currFile(); err != nil { if cmd, ok := gOpts.cmds["rename"]; ok {
cmd.eval(app, e.args)
if err := remote("send load"); err != nil {
app.ui.echoerrf("rename: %s", err)
return
}
} else {
curr, err := app.nav.currFile()
if err != nil {
app.ui.echoerrf("rename: %s:", err) app.ui.echoerrf("rename: %s:", err)
return return
} else { }
app.ui.cmdPrefix = "rename: " app.ui.cmdPrefix = "rename: "
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...) app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...)
} }
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
case "sync": case "sync":
if err := app.nav.sync(); err != nil { if err := app.nav.sync(); err != nil {
app.ui.echoerrf("sync: %s", err) app.ui.echoerrf("sync: %s", err)
@ -1076,8 +1093,16 @@ func (e *callExpr) eval(app *app, args []string) {
if err := app.nav.rename(app.ui); err != nil { if err := app.nav.rename(app.ui); err != nil {
app.ui.echoerrf("rename: %s", err) app.ui.echoerrf("rename: %s", err)
return
} }
if err := remote("send load"); err != nil {
app.ui.echoerrf("rename: %s", err)
return
}
app.ui.loadFile(app.nav)
app.ui.loadFileInfo(app.nav)
} }
default: default:
log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix) log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix)

6
nav.go
View File

@ -751,16 +751,16 @@ func (nav *nav) rename(ui *ui) error {
return err return err
} }
} }
if err := os.Rename(oldPath, newPath); err != nil { if err := os.Rename(oldPath, newPath); err != nil {
return err return err
} }
nav.renew()
// TODO: change selection // TODO: change selection
if err := nav.sel(newPath); err != nil { if err := nav.sel(newPath); err != nil {
return err return err
} }
ui.loadFile(nav)
ui.loadFileInfo(nav)
return nil return nil
} }