From ceea7cdcdbe974c3c0ad8bf0b4249a1071b98400 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Sat, 27 Jul 2013 11:12:25 +0200 Subject: [PATCH] Zshbrowser script When start, it will spawn ranger from zsh browse() function. This way you can quit ranger, return to the shell, do some shell stuff, and go back to ranger with the browse() function. The big advantage of using ranger this way is that the browse() function syncs the pwd between ranger and Zsh. --- .config/awesome/rc.lua | 2 +- .config/ranger/rc.conf | 3 ++- .scripts/zshbrowser | 28 ++++++++++++++++++++++++++++ .scripts/zshcd | 41 ++++++++++++++++------------------------- .shell.d/funs_rc | 2 +- .zshrc | 5 ++++- 6 files changed, 52 insertions(+), 29 deletions(-) create mode 100755 .scripts/zshbrowser diff --git a/.config/awesome/rc.lua b/.config/awesome/rc.lua index 7d4df00b..f4b5c686 100644 --- a/.config/awesome/rc.lua +++ b/.config/awesome/rc.lua @@ -333,7 +333,7 @@ globalkeys = awful.util.table.join( awful.key({ modkey, }, "e", function () awful.util.spawn(os.getenv("EDITOR")) end), -- Directory browser - awful.key({ modkey, }, "d", function () awful.util.spawn(termcmd .. "ranger") end), + awful.key({ modkey, }, "d", function () awful.util.spawn("zshbrowser") end), awful.key({ }, "XF86Explorer", function () awful.util.spawn(termcmd .. "ranger") end), awful.key({ }, "XF86MyComputer", function () awful.util.spawn(termcmd .. "ranger") end), diff --git a/.config/ranger/rc.conf b/.config/ranger/rc.conf index 8e581e77..82924631 100644 --- a/.config/ranger/rc.conf +++ b/.config/ranger/rc.conf @@ -152,5 +152,6 @@ map J move down=5 ## New tab in current folder. Use 'gn' for home folder. map eval fm.tab_new('%d') -## Start shell in current folder. Needs zshcd script. +## Start shell in current folder. Needs zshcd script. This is useful if you have +## multiple tabs and you do not want to lose them just to run a shell. map S shell zshcd %d \ No newline at end of file diff --git a/.scripts/zshbrowser b/.scripts/zshbrowser new file mode 100755 index 00000000..4b658854 --- /dev/null +++ b/.scripts/zshbrowser @@ -0,0 +1,28 @@ +#!/bin/sh + +## Start terminal, invoke Zsh and launch browser automatically. + +## It is important to invoke the terminal from here, and not run the terminal +## over this script, otherwise cleaning line will not run when window is killed. + +if [ ! -f ~/.zshrc ]; then + echo '~/.zshrc not found. Exiting.' + exit +fi + +HOOK_FILE_GREP="$(grep "HOOK_FILE=" ~/.zshrc | cut -f2 -d=)" +[ -n "$HOOK_FILE_GREP" ] && HOOK_FILE="$(eval echo "$HOOK_FILE_GREP")" +if [ -z "$HOOK_FILE" ]; then + echo 'HOOK_FILE variable could not be set. Exiting.' + exit +fi +if [ -z "$TERMCMD" ]; then + echo 'TERMCMD not set. Exiting.' + exit +fi + +[ -f "$HOOK_FILE" ] && sed -ni '/###ZSHBROWSER/!p' "$HOOK_FILE" +echo 'browse ###ZSHBROWSER' >> "$HOOK_FILE" +$TERMCMD -e zsh +[ -f "$HOOK_FILE" ] && sed -ni '/###ZSHBROWSER/!p' "$HOOK_FILE" +[ $(du "$HOOK_FILE" | cut -f1) -eq 0 ] && rm "$HOOK_FILE" diff --git a/.scripts/zshcd b/.scripts/zshcd index fd878108..a291dfb1 100755 --- a/.scripts/zshcd +++ b/.scripts/zshcd @@ -1,5 +1,5 @@ #!/bin/sh -## Start zsh in directory specified as first argument. +## Start zsh in the directory specified as first argument. if [ ! -d "$1" ]; then zsh @@ -10,36 +10,27 @@ fi ZDIRS="$HOME/.cache/zsh/dirs" ## Fetch file location dynamically. -if [ ! -f "$ZDIRS" ]; then - if [ ! -f ~/.zshrc ]; then - echo '~/.zshrc not found. Exiting.' - exit - fi - - SHELL_DIR="$HOME/.shell.d" - if [ ! -d "$SHELL_DIR" ]; then - SHELL_DIR_GREP="$(grep "SHELL_DIR=" ~/.zshrc | cut -f2 -d=)" - [ -n "$SHELL_DIR_GREP" ] && SHELL_DIR="$(eval echo "$SHELL_DIR_GREP")" - fi - if [ ! -d "$SHELL_DIR" ]; then - echo 'DIRSTACKFILE and SHELL_DIR not found. Exiting.' - exit - fi - ZDIRS_GREP="$(grep -rm1 'DIRSTACKFILE=' "$SHELL_DIR" | cut -f2 -d=)" - [ -n "$ZDIRS_GREP" ] && ZDIRS="$(eval echo "$ZDIRS_GREP")" +if [ ! -f ~/.zshrc ]; then + echo '~/.zshrc not found. Exiting.' + exit fi +SHELL_DIR_GREP="$(grep "SHELL_DIR=" ~/.zshrc | cut -f2 -d=)" +[ -n "$SHELL_DIR_GREP" ] && SHELL_DIR="$(eval echo "$SHELL_DIR_GREP")" +if [ ! -d "$SHELL_DIR" ]; then + echo 'SHELL_DIR not found. Could not search for DIRSTACKFILE. Exiting.' + exit +fi + +ZDIRS_GREP="$(grep -rm1 'DIRSTACKFILE=' "$SHELL_DIR" | cut -f2 -d=)" +[ -n "$ZDIRS_GREP" ] && ZDIRS="$(eval echo "$ZDIRS_GREP")" if [ ! -f "$ZDIRS" ]; then echo 'DIRSTACKFILE not found. Exiting.' exit fi -if [ -n "$(grep "$1" "$ZDIRS")" ]; then - BUF="$(grep -v "$1" "$ZDIRS")" - echo "$BUF" - echo "$BUF" > "$ZDIRS" -fi +BUF="$(realpath "$1") +$(grep -v "$1" "$ZDIRS")" +echo "$BUF" > "$ZDIRS" -prepend "$1" "$ZDIRS" zsh - diff --git a/.shell.d/funs_rc b/.shell.d/funs_rc index 08799360..fc3c993a 100644 --- a/.shell.d/funs_rc +++ b/.shell.d/funs_rc @@ -411,7 +411,7 @@ browse () { # Compatible with ranger 1.4.2 through 1.6.* # - # Automatically change the directory in bash after closing ranger + # Automatically change the directory in bash after closing ranger. # # This is a shell wrapper script to automatically change the directory to the # last visited one after ranger quits. To undo the effect of this function, you diff --git a/.zshrc b/.zshrc index 907730b9..f5e36356 100644 --- a/.zshrc +++ b/.zshrc @@ -10,6 +10,9 @@ SHELL_CURRENT="$(ps -o command="" $$)" SHELL_DIR="$HOME/.shell.d" +## Need to be expanded for external scripts to fetch zsh hook file location. +HOOK_FILE="$HOME/.shell.d/hook" + ## .profile is sourced automatically when X is started, but we need to source it ## manually to TTY. [ -z "$DISPLAY" ] && [ -f "$HOME/.profile" ] && . "$HOME/.profile" @@ -26,4 +29,4 @@ SHELL_DIR="$HOME/.shell.d" [ -f "${SHELL_DIR}/keys_zsh" ] && . "${SHELL_DIR}/keys_zsh" ## Should be sourced last -[ -f "${SHELL_DIR}/hook" ] && . "${SHELL_DIR}/hook" +[ -f "$HOOK_FILE" ] && . "$HOOK_FILE"