diff --git a/app.go b/app.go index 725962b0..1ee75766 100644 --- a/app.go +++ b/app.go @@ -160,13 +160,6 @@ func loadFiles() (clipboard clipboard, err error) { } func saveFiles(clipboard clipboard) error { - for _, path := range clipboard.paths { - // the clipboard file stores one path per line so a newline cannot be saved - if strings.ContainsAny(path, "\n\r") { - return fmt.Errorf("cannot copy %s because the name contains a newline", path) - } - } - if err := os.MkdirAll(filepath.Dir(gFilesPath), 0o700); err != nil { return fmt.Errorf("creating data directory: %w", err) } @@ -190,6 +183,10 @@ func saveFiles(clipboard clipboard) error { } for _, path := range clipboard.paths { + if strings.ContainsAny(path, "\n\r") { + log.Printf("clipboard: skipping path with newline: %q", path) + continue + } if _, err := fmt.Fprintln(files, path); err != nil { return fmt.Errorf("write path to file: %w", err) } diff --git a/eval.go b/eval.go index 07b15709..dd8931f0 100644 --- a/eval.go +++ b/eval.go @@ -1220,6 +1220,12 @@ func (e *callExpr) eval(app *app, _ []string) { app.ui.echoerrf("copy: %s", err) return } + for _, path := range app.nav.clipboard.paths { + if strings.ContainsAny(path, "\n\r") { + app.ui.echoerrf("copy: cannot copy %s because the name contains a newline", path) + break + } + } app.nav.unselect() if gSingleMode { if err := app.nav.sync(); err != nil { @@ -1237,6 +1243,12 @@ func (e *callExpr) eval(app *app, _ []string) { app.ui.echoerrf("cut: %s", err) return } + for _, path := range app.nav.clipboard.paths { + if strings.ContainsAny(path, "\n\r") { + app.ui.echoerrf("cut: cannot move %s because the name contains a newline", path) + break + } + } app.nav.unselect() if gSingleMode { if err := app.nav.sync(); err != nil {