Skip to content

mc-wrapper: fish shenanigans #4726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions contrib/.gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mc-wrapper.csh
mc-wrapper.sh
mc-wrapper.*
!mc-wrapper.*.in
mc.csh
mc.sh
mc.fish
31 changes: 18 additions & 13 deletions contrib/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@

noinst_DATA = README.xterm

SCRIPTS_IN = mc.csh.in mc.sh.in mc-wrapper.csh.in mc-wrapper.sh.in
SCRIPTS_OUT = mc.csh mc.sh mc-wrapper.csh mc-wrapper.sh
SCRIPTS_IN = \
mc.csh.in \
mc.fish.in \
mc.sh.in \
mc-wrapper.csh.in \
mc-wrapper.fish.in \
mc-wrapper.sh.in
SCRIPTS_OUT = \
mc.csh \
mc.fish \
mc.sh \
mc-wrapper.csh \
mc-wrapper.fish \
mc-wrapper.sh

pkglibexec_SCRIPTS = $(SCRIPTS_OUT)

Expand All @@ -17,15 +29,8 @@ EXTRA_DIST = \
$(SCRIPTS_IN) \
$(noinst_DATA)

mc.csh: $(top_builddir)/config.status $(srcdir)/mc.csh.in
$(SED) "s%@""pkglibexecdir@%$(pkglibexecdir)%" $(srcdir)/mc.csh.in > mc.csh

mc.sh: $(top_builddir)/config.status $(srcdir)/mc.sh.in
$(SED) "s%@""pkglibexecdir@%$(pkglibexecdir)%" $(srcdir)/mc.sh.in > mc.sh

mc-wrapper.csh: $(top_builddir)/config.status $(srcdir)/mc-wrapper.csh.in
$(SED) "s%@""bindir@%$(bindir)%" $(srcdir)/mc-wrapper.csh.in > mc-wrapper.csh

mc-wrapper.sh: $(top_builddir)/config.status $(srcdir)/mc-wrapper.sh.in
$(SED) "s%@""bindir@%$(bindir)%" $(srcdir)/mc-wrapper.sh.in > mc-wrapper.sh
mc.%: $(top_builddir)/config.status $(srcdir)/mc.%.in
$(SED) "s%@""pkglibexecdir@%$(pkglibexecdir)%" $(srcdir)/mc.$*.in > $@

mc-wrapper.%: $(top_builddir)/config.status $(srcdir)/mc-wrapper.%.in
$(SED) "s%@""bindir@%$(bindir)%" $(srcdir)/mc-wrapper.$*.in > $@
2 changes: 1 addition & 1 deletion contrib/mc-wrapper.csh.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ else
setenv MC_PWD_FILE "`mktemp '/tmp/mc.pwd.XXXXXX'`"
endif

@bindir@/mc -P "$MC_PWD_FILE" $*
@bindir@/mc -P "$MC_PWD_FILE" $* || true

if (-r "$MC_PWD_FILE") then
setenv MC_PWD "`cat '$MC_PWD_FILE'`"
Expand Down
20 changes: 20 additions & 0 deletions contrib/mc-wrapper.fish.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
if set -q MC_TMPDIR
set MC_PWD_FILE (mktemp $MC_TMPDIR/mc.pwd.XXXXXX)
else if set -q TMPDIR
set MC_PWD_FILE (mktemp $TMPDIR/mc.pwd.XXXXXX)
else
set MC_PWD_FILE (mktemp /tmp/mc.pwd.XXXXXX)
end

@bindir@/mc -P "$MC_PWD_FILE" $argv || true

if test -r "$MC_PWD_FILE"
set MC_PWD (cat $MC_PWD_FILE)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't know fish syntax, but the inconsistent use of quotes around variable expansions seems ... fishy.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I used the "only quote when needed" style.
Every variable in fish is an array variable, so we need to quote it for
test -r "$foo", lest it expand to noe argument. But if we're
guaranteed the variable is non-empty, or if the behavior for "empty
array" is fine, there's no need.

Happy to add it though, if it helps to stay closer to other integrations.

FWIW there are only three things missing from fish that prevent it from using contrib/mc-wrapper.sh.in as-is:

  1. foo=bar for global assignment (foo=bar somecommand already works)
  2. $@
  3. unset (simple wrapper around fish's set -e)
    The first two I have implemented in my fork for now.

It's possible to make the wrapper scripts a bit smaller, by farming
out the management of $MC_PWD_FILE to an external program (written
in any language):

# mc-and-echo-pwd.sh
MC_PWD_FILE=$(mktemp)
mc -P "$MC_PWD_FILE"
wd=$(cat "$MC_PWD_FILE")
rm -f "$MC_PWD_FILE"
if [ -z "$wd" ]; then
	wd=.
fi
printf %s "$wd"

# mc.sh
alias mc='__mcwrapper'
__mcwrapper() {
	cd "$(mc-and-echo-pwd.sh "$@")"
}

# mc.fish
function mc
	cd (mc-and-echo-pwd.sh $argv)
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nevermind, that shared logic won't work because stdout is captured i.e redirected

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right sorry about that. It is toally unintentional glitch. I can fix that up.

if test -n "$MC_PWD" && test $MC_PWD != $PWD && test -d $MC_PWD
cd $MC_PWD || true
end
set -e MC_PWD
end

rm -f $MC_PWD_FILE
set -e MC_PWD_FILE
2 changes: 1 addition & 1 deletion contrib/mc-wrapper.sh.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ else
MC_PWD_FILE="`mktemp "/tmp/mc.pwd.XXXXXX"`"
fi

@bindir@/mc -P "$MC_PWD_FILE" "$@"
@bindir@/mc -P "$MC_PWD_FILE" "$@" || true

if test -r "$MC_PWD_FILE"; then
MC_PWD="`cat "$MC_PWD_FILE"`"
Expand Down
3 changes: 3 additions & 0 deletions contrib/mc.fish.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function mc --description 'Visual shell for Unix-like systems - fish wrapper'
source @pkglibexecdir@/mc-wrapper.fish $argv
end
4 changes: 3 additions & 1 deletion doc/man/es/mc.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ Midnight Commander. Consúltese en los archivos
.B %pkglibexecdir%/mc.sh
(usuarios de bash y zsh) y
.B %pkglibexecdir%/mc.csh
(usuarios de tcsh) la manera de definir
(usuarios de tcsh)
.B %pkglibexecdir%/mc.fish
(usuarios de fish) la manera de definir
.B mc
como un alias para el correspondiente guión de shell.
.TP
Expand Down
6 changes: 4 additions & 2 deletions doc/man/hu/mc.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ helyett a Midnight Commander által utoljára meglátogatott könyvtárra való
Fjerdingstad\-nek és Sergey\-nek közreműködésükért). Kérlek, ne csinálj
szó szerinti másolatot a funkció beállításairól. A fájlok forrása a
.I %pkglibexecdir%/mc.sh
(bash és zsh felhasználóknak), illetőleg a
(bash és zsh felhasználóknak),
.I %pkglibexecdir%/mc.csh
(tcsh felhasználóknak) fájl. Ilyenkor, amikor a funkció beállításokat
(tcsh felhasználóknak) illetőleg a
.I %pkglibexecdir%/mc.fish
(fish felhasználóknak) fájl. Ilyenkor, amikor a funkció beállításokat
változtatod, a profil értékeket nem szükséges megváltoztatnod, csak
arról gondoskodj, hogy az MC\-t ne fordítsd eltérő beállításokkal.
.PP
Expand Down
6 changes: 4 additions & 2 deletions doc/man/it/mc.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,11 @@ utilizzata da una speciale funzione shell che imposti automaticamente
l'ultima directory corrente della shell come l'ultima directory in cui
stava il Midnight Commander. Prelevate i file
.B %pkglibexecdir%/mc.sh
(utenti bash e zsh) o rispettivamente
(utenti bash e zsh),
.B %pkglibexecdir%/mc.csh
(utenti tcsh) per definire
(utenti tcsh) o rispettivamente
.B %pkglibexecdir%/mc.fish
(utenti fish) per definire
.B mc
come un alias allo script di shell appropriato.
.TP
Expand Down
6 changes: 4 additions & 2 deletions doc/man/mc.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,10 @@ script that automatically changes the current directory of the shell to
the last directory Midnight Commander was in. Source the file
.B %pkglibexecdir%/mc.sh
(bash and zsh users) or
.B %libexecdir%/mc.csh
(tcsh users) respectively to define
.B %pkglibexecdir%/mc.csh
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually there's another issue: pkglibexecdir is not defined on my system, only libexecdir is.
After installing, man mc renders like:

              shell to the last directory Midnight Commander was in. Source the file @pkglibexecdir@/mc.sh (bash and zsh
              users) or /home/johannes/.local/libexec/mc.csh (tcsh users) respectively to define mc as an alias  to  the

I'm building with

./autogen.sh &&
    ./configure --prefix=$HOME/.local &&
    make -j3 &&
    make install

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

found it, will attempt a fix

(tcsh users) or
.B %pkglibexecdir%/mc.fish
(fish users) respectively to define
.B mc
as an alias to the appropriate shell script.
.TP
Expand Down
4 changes: 3 additions & 1 deletion doc/man/ru/mc.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,10 @@ Commander.
.IP
Для того чтобы эта функция была определена, используйте файл
.B %pkglibexecdir%/mc.sh
для оболочек bash и zsh, а для оболочки tcsh соответственно файл
для оболочек bash и zsh, или для оболочки tcsh файл
.B %pkglibexecdir%/mc.csh
а для оболочки fish соответственно файл
.B %pkglibexecdir%/mc.fish
.TP
.I \-s, \-\-slow
Включает медленный режим терминала, в котором программа выводит меньше
Expand Down
4 changes: 3 additions & 1 deletion doc/man/sr/mc.1.in
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,9 @@ termcap/terminfo. Корисно је само на ХП\-овим термин
.B %pkglibexecdir%/mc.sh
(за кориснике љуски bash и zsh) или
.B %pkglibexecdir%/mc.csh
(за кориснике љуске tcsh), тим редом, да бисте задали
(за кориснике љуске tcsh) или
.B %pkglibexecdir%/mc.fish
(за кориснике љуске fish), тим редом, да бисте задали
.B mc
као надимак за одговарајући спис љуске.
.TP
Expand Down
Loading