feat(file-manager): open files in the editor from the panel

Files used to open in a new tab, which loses the shell the user was
working in. Hand the editor command to that shell instead, optionally
through sudo for files owned by root.

A docked panel cannot do that: kitty never routes the result of a
launch --type=window-dock back to the shell, so Enter used to close the
panel without opening anything. Add --open-here, which suspends the
panel and runs the editor in its own window. The editor is resolved
from the new `editor` option, because kitty's own editor option is not
visible to a process started with launch.
This commit is contained in:
BlackAngelTV committed 2026-11-05 15:48:01 +01:00
1 parent fb87a7306f
commit 86982b79be
3 files changed
+212 -15

No files matched your search

+69 -2
View File
@@ -3,8 +3,10 @@
package file_manager
import (
"errors"
"fmt"
"os"
"os/exec"
"path/filepath"
"sort"
"strings"
@@ -14,6 +16,7 @@ import (
"github.com/kovidgoyal/kitty/tools/config"
"github.com/kovidgoyal/kitty/tools/tui"
"github.com/kovidgoyal/kitty/tools/tui/loop"
"github.com/kovidgoyal/kitty/tools/utils/shlex"
"github.com/kovidgoyal/kitty/tools/wcswidth"
)
@@ -200,6 +203,7 @@ type Handler struct {
message string
result_action string
result_path string
open_here bool
shortcut_tracker config.ShortcutTracker
keyboard_shortcuts []*config.KeyAction
}
@@ -406,10 +410,62 @@ func (h *Handler) activate() {
h.change_shell_dir_to(path)
return
}
if h.open_here {
h.open_file_in_panel(path)
return
}
h.result_action, h.result_path = "open", path
h.lp.Quit(0)
}
// editor_cmd resolves the editor used by --open-here. The panel's own `editor`
// option comes first because kitty's `editor` option is not visible to a
// process started with launch, then the usual environment variables.
func editor_cmd(conf *Config) []string {
for _, candidate := range []string{conf.Editor, os.Getenv("VISUAL"), os.Getenv("EDITOR")} {
if candidate == "" {
continue
}
if words, err := shlex.Split(candidate); err == nil && len(words) > 0 {
return words
}
}
return []string{"vi"}
}
// open_cmdline builds the command that opens path in the editor.
func open_cmdline(conf *Config, path string) []string {
cmdline := append(editor_cmd(conf), path)
if conf.Open_with_sudo {
// -- stops sudo from eating editor arguments that look like its own options.
cmdline = append([]string{"sudo", "--"}, cmdline...)
}
return cmdline
}
// open_file_in_panel runs the editor in the panel's own window. A docked panel
// has to do this itself: kitty never sends the result of a launch --type=window-dock
// back to the shell, so the panel would otherwise close without opening anything.
func (h *Handler) open_file_in_panel(path string) {
cmdline := open_cmdline(h.conf, path)
err := h.lp.SuspendAndRun(func() error {
cmd := exec.Command(cmdline[0], cmdline[1:]...)
cmd.Stdin, cmd.Stdout, cmd.Stderr = os.Stdin, os.Stdout, os.Stderr
return cmd.Run()
})
var ee *exec.ExitError
if err != nil && !errors.As(err, &ee) {
h.message = err.Error()
}
// The editor may have changed or removed the file, so refresh the listing.
if lerr := h.load(h.cwd); lerr != nil {
h.message = lerr.Error()
} else {
h.select_name(filepath.Base(path))
}
h.draw_screen()
}
// enter_dir browses into the selected directory inside the panel, leaving the
// working directory of the shell untouched.
func (h *Handler) enter_dir() {
@@ -735,6 +791,17 @@ func (h *Handler) draw_screen() {
}
}
// result_payload is the JSON data handed over to the Python result handler. The
// sudo flag travels with the result so that handle_result does not have to read
// file-manager.conf a second time.
func (h *Handler) result_payload() map[string]string {
data := map[string]string{"action": h.result_action, "path": h.result_path}
if h.result_action == "open" && h.conf.Open_with_sudo {
data["sudo"] = "1"
}
return data
}
func load_config(opts *Options) (*Config, error) {
ans := NewConfig()
p := config.ConfigParser{LineHandler: ans.Parse}
@@ -761,7 +828,7 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
if err != nil {
return 1, err
}
h := &Handler{lp: lp, conf: conf, cwd: start_dir, show_hidden: opts.ShowHidden || conf.Show_hidden}
h := &Handler{lp: lp, conf: conf, cwd: start_dir, show_hidden: opts.ShowHidden || conf.Show_hidden, open_here: opts.OpenHere}
h.keyboard_shortcuts = config.ResolveShortcuts(conf.KeyboardShortcuts)
lp.MouseTrackingMode(loop.FULL_MOUSE_TRACKING)
@@ -783,7 +850,7 @@ func main(_ *cli.Command, opts *Options, args []string) (rc int, err error) {
rc = lp.ExitCode()
if h.result_action != "" {
output := tui.KittenOutputSerializer()
if s, serr := output(map[string]string{"action": h.result_action, "path": h.result_path}); serr == nil {
if s, serr := output(h.result_payload()); serr == nil {
fmt.Println(s)
}
}