draft rename (#197)
* basic structure * working rename, without making dir's and selection change * mkdirAll if necessary * updated TODO's * rename with replace prompting * path prompt and moved functionality to nav.rename() * renewing now (no set period is needed) * documentation, keybidning change, load file info after renaming * go generate
This commit is contained in:
parent
27861f06e2
commit
bfd01e5ed8
@ -53,6 +53,7 @@ var (
|
|||||||
"mark-save",
|
"mark-save",
|
||||||
"mark-remove",
|
"mark-remove",
|
||||||
"mark-load",
|
"mark-load",
|
||||||
|
"rename",
|
||||||
"sync",
|
"sync",
|
||||||
"echo",
|
"echo",
|
||||||
"echomsg",
|
"echomsg",
|
||||||
|
1
doc.go
1
doc.go
@ -37,6 +37,7 @@ The following commands are provided by lf with default keybindings:
|
|||||||
redraw (default '<c-l>')
|
redraw (default '<c-l>')
|
||||||
reload (default '<c-r>')
|
reload (default '<c-r>')
|
||||||
read (default ':')
|
read (default ':')
|
||||||
|
rename (default 'r')
|
||||||
shell (default '$')
|
shell (default '$')
|
||||||
shell-pipe (default '%')
|
shell-pipe (default '%')
|
||||||
shell-wait (default '!')
|
shell-wait (default '!')
|
||||||
|
@ -40,6 +40,7 @@ The following commands are provided by lf with default keybindings:
|
|||||||
redraw (default '<c-l>')
|
redraw (default '<c-l>')
|
||||||
reload (default '<c-r>')
|
reload (default '<c-r>')
|
||||||
read (default ':')
|
read (default ':')
|
||||||
|
rename (default 'r')
|
||||||
shell (default '$')
|
shell (default '$')
|
||||||
shell-pipe (default '%')
|
shell-pipe (default '%')
|
||||||
shell-wait (default '!')
|
shell-wait (default '!')
|
||||||
|
52
eval.go
52
eval.go
@ -472,6 +472,17 @@ func insert(app *app, arg string) {
|
|||||||
app.ui.echoerrf("delete: %s", err)
|
app.ui.echoerrf("delete: %s", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
case strings.HasPrefix(app.ui.cmdPrefix, "replace") ||
|
||||||
|
strings.HasPrefix(app.ui.cmdPrefix, "create path"):
|
||||||
|
normal(app)
|
||||||
|
|
||||||
|
if arg == "y" {
|
||||||
|
if err := app.nav.rename(app.ui); err != nil {
|
||||||
|
app.ui.echoerrf("rename: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
case app.ui.cmdPrefix == "mark-save: ":
|
case app.ui.cmdPrefix == "mark-save: ":
|
||||||
normal(app)
|
normal(app)
|
||||||
|
|
||||||
@ -523,7 +534,6 @@ func insert(app *app, arg string) {
|
|||||||
if err := remote("send sync"); err != nil {
|
if err := remote("send sync"); err != nil {
|
||||||
app.ui.echoerrf("mark-remove: %s", err)
|
app.ui.echoerrf("mark-remove: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(arg)...)
|
||||||
}
|
}
|
||||||
@ -818,6 +828,15 @@ func (e *callExpr) eval(app *app, args []string) {
|
|||||||
case "mark-remove":
|
case "mark-remove":
|
||||||
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":
|
||||||
|
if curr, err := app.nav.currFile(); err != nil {
|
||||||
|
app.ui.echoerrf("rename: %s:", err)
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
app.ui.cmdPrefix = "rename: "
|
||||||
|
app.ui.cmdAccLeft = append(app.ui.cmdAccLeft, []rune(curr.Name())...)
|
||||||
|
}
|
||||||
|
|
||||||
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)
|
||||||
@ -1029,6 +1048,37 @@ func (e *callExpr) eval(app *app, args []string) {
|
|||||||
app.ui.loadFile(app.nav)
|
app.ui.loadFile(app.nav)
|
||||||
app.ui.loadFileInfo(app.nav)
|
app.ui.loadFileInfo(app.nav)
|
||||||
}
|
}
|
||||||
|
case "rename: ":
|
||||||
|
app.ui.cmdPrefix = ""
|
||||||
|
if curr, err := app.nav.currFile(); err != nil {
|
||||||
|
app.ui.echoerrf("rename: %s", err)
|
||||||
|
} else {
|
||||||
|
wd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
log.Printf("getting current directory: %s", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
oldPathTo := filepath.Join(wd, curr.Name())
|
||||||
|
newPathTo := filepath.Join(wd, s)
|
||||||
|
app.nav.renameCache = []string{oldPathTo, newPathTo}
|
||||||
|
|
||||||
|
if dir, _ := filepath.Split(s); dir != "" {
|
||||||
|
if _, err := os.Stat(filepath.Join(wd, dir)); err != nil {
|
||||||
|
app.ui.cmdPrefix = "create path " + dir + "?[y/N]"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(newPathTo); err == nil { // file exists
|
||||||
|
app.ui.cmdPrefix = "replace " + s + "?[y/N]"
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := app.nav.rename(app.ui); err != nil {
|
||||||
|
app.ui.echoerrf("rename: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix)
|
log.Printf("entering unknown execution prefix: %q", app.ui.cmdPrefix)
|
||||||
}
|
}
|
||||||
|
1
lf.1
1
lf.1
@ -47,6 +47,7 @@ The following commands are provided by lf with default keybindings:
|
|||||||
redraw (default '<c-l>')
|
redraw (default '<c-l>')
|
||||||
reload (default '<c-r>')
|
reload (default '<c-r>')
|
||||||
read (default ':')
|
read (default ':')
|
||||||
|
rename (default 'r')
|
||||||
shell (default '$')
|
shell (default '$')
|
||||||
shell-pipe (default '%')
|
shell-pipe (default '%')
|
||||||
shell-wait (default '!')
|
shell-wait (default '!')
|
||||||
|
26
nav.go
26
nav.go
@ -207,6 +207,7 @@ type nav struct {
|
|||||||
regCache map[string]*reg
|
regCache map[string]*reg
|
||||||
saves map[string]bool
|
saves map[string]bool
|
||||||
marks map[string]string
|
marks map[string]string
|
||||||
|
renameCache []string
|
||||||
selections map[string]int
|
selections map[string]int
|
||||||
selectionInd int
|
selectionInd int
|
||||||
height int
|
height int
|
||||||
@ -293,6 +294,7 @@ func newNav(height int) *nav {
|
|||||||
regCache: make(map[string]*reg),
|
regCache: make(map[string]*reg),
|
||||||
saves: make(map[string]bool),
|
saves: make(map[string]bool),
|
||||||
marks: make(map[string]string),
|
marks: make(map[string]string),
|
||||||
|
renameCache: make([]string, 2),
|
||||||
selections: make(map[string]int),
|
selections: make(map[string]int),
|
||||||
selectionInd: 0,
|
selectionInd: 0,
|
||||||
height: height,
|
height: height,
|
||||||
@ -738,6 +740,30 @@ func (nav *nav) del() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (nav *nav) rename(ui *ui) error {
|
||||||
|
oldPath := nav.renameCache[0]
|
||||||
|
newPath := nav.renameCache[1]
|
||||||
|
dir, _ := filepath.Split(newPath)
|
||||||
|
os.MkdirAll(dir, os.ModePerm)
|
||||||
|
|
||||||
|
if _, err := os.Stat(newPath); err == nil { // file exists
|
||||||
|
if err := os.Remove(newPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if err := os.Rename(oldPath, newPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
nav.renew()
|
||||||
|
// TODO: change selection
|
||||||
|
if err := nav.sel(newPath); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ui.loadFile(nav)
|
||||||
|
ui.loadFileInfo(nav)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (nav *nav) sync() error {
|
func (nav *nav) sync() error {
|
||||||
list, cp, err := loadFiles()
|
list, cp, err := loadFiles()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
1
opts.go
1
opts.go
@ -140,6 +140,7 @@ func init() {
|
|||||||
gOpts.keys["m"] = &callExpr{"mark-save", nil, 1}
|
gOpts.keys["m"] = &callExpr{"mark-save", nil, 1}
|
||||||
gOpts.keys["'"] = &callExpr{"mark-load", nil, 1}
|
gOpts.keys["'"] = &callExpr{"mark-load", nil, 1}
|
||||||
gOpts.keys[`"`] = &callExpr{"mark-remove", nil, 1}
|
gOpts.keys[`"`] = &callExpr{"mark-remove", nil, 1}
|
||||||
|
gOpts.keys[`r`] = &callExpr{"rename", nil, 1}
|
||||||
gOpts.keys["<c-n>"] = &callExpr{"cmd-history-next", nil, 1}
|
gOpts.keys["<c-n>"] = &callExpr{"cmd-history-next", nil, 1}
|
||||||
gOpts.keys["<c-p>"] = &callExpr{"cmd-history-prev", nil, 1}
|
gOpts.keys["<c-p>"] = &callExpr{"cmd-history-prev", nil, 1}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user