From 5787ecd1bd29e6a1ad7b23c707389f8188093b9a Mon Sep 17 00:00:00 2001 From: Gokcehan Date: Thu, 28 Jun 2018 21:51:24 +0300 Subject: [PATCH] add default environmental variables --- app.go | 5 +++++ doc.go | 20 ++++++++++++++------ docstring.go | 20 ++++++++++++++------ os.go | 48 +++++++++++++++++++++++++----------------------- os_windows.go | 2 +- 5 files changed, 59 insertions(+), 36 deletions(-) diff --git a/app.go b/app.go index 017a201b..71794a5a 100644 --- a/app.go +++ b/app.go @@ -163,6 +163,11 @@ func (app *app) exportVars() { } os.Setenv("id", strconv.Itoa(gClientID)) + + os.Setenv("OPENER", envOpener) + os.Setenv("EDITOR", envEditor) + os.Setenv("PAGER", envPager) + os.Setenv("SHELL", envShell) } func waitKey() error { diff --git a/doc.go b/doc.go index 0f85b84e..b68bee01 100644 --- a/doc.go +++ b/doc.go @@ -114,6 +114,15 @@ The following variables are exported for shell commands: $fx current file or marked file(s) if any $id id number of the client +The following default values are set to the environmental variables on unix +when they are not set or empty: + + $OPENER open # macos + $OPENER xdg-open # others + $EDITOR vi + $PAGER less + $SHELL sh + The following additional keybindings are provided by default: map zh set hidden! @@ -129,9 +138,9 @@ The following additional keybindings are provided by default: The following keybindings to applications are provided by default on unix: - map e $$EDITOR $f ('vi' if empty) - map i $$PAGER $f ('less' if empty) - map w $$SHELL ('sh' if empty) + map e $$EDITOR "$f" + map i $$PAGER "$f" + map w $$SHELL The following keybindings to applications are provided by default on windows: @@ -525,9 +534,8 @@ You may want to use either file extensions or mime types from 'file' command: Following commands are provided by default: - cmd open &start %f% # windows - cmd open &open "$f" # macos - cmd open &xdg-open "$f" # others + cmd open &$OPENER "$f # unix + cmd open &start %f% # windows You may also use any other existing file openers as you like. Possible options are 'libfile-mimeinfo-perl' (executable name is 'mimeopen'), 'rifle' (ranger's diff --git a/docstring.go b/docstring.go index 96e232ad..cc211eb1 100644 --- a/docstring.go +++ b/docstring.go @@ -118,6 +118,15 @@ The following variables are exported for shell commands: $fx current file or marked file(s) if any $id id number of the client +The following default values are set to the environmental variables on unix +when they are not set or empty: + + $OPENER open # macos + $OPENER xdg-open # others + $EDITOR vi + $PAGER less + $SHELL sh + The following additional keybindings are provided by default: map zh set hidden! @@ -133,9 +142,9 @@ The following additional keybindings are provided by default: The following keybindings to applications are provided by default on unix: - map e $$EDITOR $f ('vi' if empty) - map i $$PAGER $f ('less' if empty) - map w $$SHELL ('sh' if empty) + map e $$EDITOR "$f" + map i $$PAGER "$f" + map w $$SHELL The following keybindings to applications are provided by default on windows: @@ -552,9 +561,8 @@ command: Following commands are provided by default: - cmd open &start %f% # windows - cmd open &open "$f" # macos - cmd open &xdg-open "$f" # others + cmd open &$OPENER "$f # unix + cmd open &start %f% # windows You may also use any other existing file openers as you like. Possible options are 'libfile-mimeinfo-perl' (executable name is 'mimeopen'), 'rifle' diff --git a/os.go b/os.go index 09ffdbff..4299e797 100644 --- a/os.go +++ b/os.go @@ -13,6 +13,7 @@ import ( ) var ( + envOpener = os.Getenv("OPENER") envEditor = os.Getenv("EDITOR") envPager = os.Getenv("PAGER") envShell = os.Getenv("SHELL") @@ -30,6 +31,26 @@ var ( ) func init() { + if envOpener == "" { + if runtime.GOOS == "darwin" { + envOpener = "open" + } else { + envOpener = "xdg-open" + } + } + + if envEditor == "" { + envEditor = "vi" + } + + if envPager == "" { + envPager = "less" + } + + if envShell == "" { + envShell = "sh" + } + u, err := user.Current() if err != nil { log.Printf("user: %s", err) @@ -99,29 +120,10 @@ func pasteCommand(list []string, dir *dir, cp bool) *exec.Cmd { } func setDefaults() { - if envEditor == "" { - gOpts.keys["e"] = &execExpr{"$", `vi "$f"`} - } else { - gOpts.keys["e"] = &execExpr{"$", envEditor + ` "$f"`} - } - - if envPager == "" { - gOpts.keys["i"] = &execExpr{"$", `less "$f"`} - } else { - gOpts.keys["i"] = &execExpr{"$", envPager + ` "$f"`} - } - - if envShell == "" { - gOpts.keys["w"] = &execExpr{"$", "sh"} - } else { - gOpts.keys["w"] = &execExpr{"$", envShell} - } - - if runtime.GOOS == "darwin" { - gOpts.cmds["open"] = &execExpr{"&", `open "$f"`} - } else { - gOpts.cmds["open"] = &execExpr{"&", `xdg-open "$f"`} - } + gOpts.cmds["open"] = &execExpr{"&", `$OPENER "$f"`} + gOpts.keys["e"] = &execExpr{"$", `$EDITOR "$f"`} + gOpts.keys["i"] = &execExpr{"$", `$PAGER "$f"`} + gOpts.keys["w"] = &execExpr{"$", "$SHELL"} } func moveCursor(y, x int) { diff --git a/os_windows.go b/os_windows.go index 4bfaa86d..d3accf48 100644 --- a/os_windows.go +++ b/os_windows.go @@ -76,10 +76,10 @@ func pasteCommand(list []string, dir *dir, cp bool) *exec.Cmd { } func setDefaults() { + gOpts.cmds["open"] = &execExpr{"&", `start %f%`} gOpts.keys["e"] = &execExpr{"$", `notepad %f%`} gOpts.keys["i"] = &execExpr{"$", `more %f%`} gOpts.keys["w"] = &execExpr{"$", "cmd"} - gOpts.cmds["open"] = &execExpr{"&", `start %f%`} } func moveCursor(y, x int) {