summary refs log blame commit diff stats
path: root/doc/cd-after-exit.txt
blob: c67b52415019eaa8347c4bbb75b2d418fa8c608e (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<
The "cd-after-exit" Feature

== Abstract

This document explains the troublesome implementation of the "cd-after-exit"
feature.

This is written for developers who wonder how it's working.


== Specification

When the feature is enabled, ranger will attempt to change the directory of
the parent shell (from which ranger is run) to the last visited directory
when ranger is exited.

This task is, by its nature, shell dependent.  As a bash or zsh user,
I focused on the implementation for those two shells and left the
addition of support for csh, ksh, and other shells to those who actually use
those shells.


== What's the problem?

Shells have several limitations, the implementation could not be done easily
because:

1. It is not possible to use something like system('cd xyz') at the end.
This command would run in a new shell and wouldn't change the directory
of the parent shell at all.

2. Using exec('cd xyz') is not possible either, since 'cd' is a command
which is directly integrated in to the shell and can not be run this way.


== Redirection of streams

The only way I found is using cd `program` from inside the shell to change
the directory to whatever `program` prints to the stdout:
    
    bash$ cd `echo ..`

Since the user interface still has to be printed, we simply redirect it to
the stderr.  It is not sufficient however to change sys.stdout to sys.stderr,
since curses seems not to be aware of sys.stdout and continues to print out
the interface to the actual stdout.

So what I did was swap the stdout and stderr of the whole ranger process on
the shell command line by using:

    bash$ cd `ranger 3>&1 1>&2 2>&3 3>&-`

Since errors are now printed to the stdout, we have do this in ranger:
    sys.stderr = sys.__stdout__

And at the end, write the current directory to the stdout, which is now
reachable via sys.__stderr__ due to the redirections:
    sys.__stderr__.write(last_visited_directory)

To inform the ranger process about these changes, we add a --cd-after-exit
switch which:
    bash$ cd `ranger --cd-after-exit 3>&1 1>&2 2>&3 3>&-`


== Argument passing

This works well enough, but there are two remaining problems:

1. How to pass arguments to ranger?

2. How to memorize that line? Although you can just copy+paste it
into your bashrc and create an alias, the complexity of the line
could lead to errors.

Both problems are solved by putting the command in a file:

run.sh:
    cd "`ranger --cd-after-exit \"$@\" 3>&1 1>&2 2>&3 3>&-`"

The $@ is responsible for argument passing.  By using the source command,
the file will be evaluated without creating a distinct new shell.

    bash$ source run.sh arg1 ... argN

To add flexibility, replace the name "ranger" in the command to the first
argument.  Now it requires you to pass the name of the ranger command to
the script as the first argument:

run.sh:
    RANGER="$1"
    shift
    cd "`$RANGER --cd-after-exit \"$@\" 3>&1 1>&2 2>&3 3>&-`"


== Put it in a nutshell

I didn't want to have 2 files for the main program and wanted just one
file at /usr/bin/ranger.  So I used this trick to merge both files into one:

    #!/usr/bin/python
    """":
    <shell code>
    """
    <python code>

If you run this file with python, or simply by typing ranger, the program will
run normally.  If you, however, run this file by sourcing it into the shell,
like you did with run.sh, the cd-after-exit mode will be activated.

Now the way of running ranger with the cd-after-exit feature is:

    bash$ source /path/to/ranger.py /path/to/ranger.py

or, if properly installed:

    bash$ source ranger ranger

A convenient way of using this feature is adding this line to your bashrc:

    alias rn='source ranger ranger'


== Keyboard interrupt handling

Another thing: If Ctrl+C is pressed anywhere in the program, the execution
of the sourced shell script is stopped and the feature stops working.

To handle that, I use: trap "" INT
This ignores all interrupt signals. However, since ranger needs to know
about ^C presses, I use this approach:

    ranger_exec="$1"
    shift
    trap "" INT
    exec 3< <($ranger_exec --cd-after-exit $@ 3>&1 1>&2 2>&3 3>&-)
    while read ranger_output; do false; done <&3
    cd "$ranger_output"
    #...and some clean ups

(this breaks zsh support!)


== Open issues

Unfortunately there is some redundancy: you have to type the path to ranger
twice.  I know of no way to fix this, because it is not possible to get the
filename of the file currently being sourced.

Example:

    bash$ echo 'source sourced.sh' > main.sh
    bash$ echo 'echo $0 $@' > sourced.sh
    bash$ bash main.sh
    main.sh

If you find a way to make this print out 'sourced.sh', let me know. :)


Dec 25, 2009
lass="n">S, Proto* f) { int n=LoadInt(S); f->code=luaM_newvector(S->L,n,Instruction); f->sizecode=n; LoadVector(S,f->code,n,sizeof(Instruction)); } static Proto* LoadFunction(LoadState* S, TString* p); static void LoadConstants(LoadState* S, Proto* f) { int i,n; n=LoadInt(S); f->k=luaM_newvector(S->L,n,TValue); f->sizek=n; for (i=0; i<n; i++) setnilvalue(&f->k[i]); for (i=0; i<n; i++) { TValue* o=&f->k[i]; int t=LoadChar(S); switch (t) { case LUA_TNIL: setnilvalue(o); break; case LUA_TBOOLEAN: setbvalue(o,LoadChar(S)!=0); break; case LUA_TNUMBER: setnvalue(o,LoadNumber(S)); break; case LUA_TSTRING: setsvalue2n(S->L,o,LoadString(S)); break; default: error(S,"bad constant"); break; } } n=LoadInt(S); f->p=luaM_newvector(S->L,n,Proto*); f->sizep=n; for (i=0; i<n; i++) f->p[i]=NULL; for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); } static void LoadDebug(LoadState* S, Proto* f) { int i,n; n=LoadInt(S); f->lineinfo=luaM_newvector(S->L,n,int); f->sizelineinfo=n; LoadVector(S,f->lineinfo,n,sizeof(int)); n=LoadInt(S); f->locvars=luaM_newvector(S->L,n,LocVar); f->sizelocvars=n; for (i=0; i<n; i++) f->locvars[i].varname=NULL; for (i=0; i<n; i++) { f->locvars[i].varname=LoadString(S); f->locvars[i].startpc=LoadInt(S); f->locvars[i].endpc=LoadInt(S); } n=LoadInt(S); f->upvalues=luaM_newvector(S->L,n,TString*); f->sizeupvalues=n; for (i=0; i<n; i++) f->upvalues[i]=NULL; for (i=0; i<n; i++) f->upvalues[i]=LoadString(S); } static Proto* LoadFunction(LoadState* S, TString* p) { Proto* f; if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep"); f=luaF_newproto(S->L); setptvalue2s(S->L,S->L->top,f); incr_top(S->L); f->source=LoadString(S); if (f->source==NULL) f->source=p; f->linedefined=LoadInt(S); f->lastlinedefined=LoadInt(S); f->nups=LoadByte(S); f->numparams=LoadByte(S); f->is_vararg=LoadByte(S); f->maxstacksize=LoadByte(S); LoadCode(S,f); LoadConstants(S,f); LoadDebug(S,f); IF (!luaG_checkcode(f), "bad code"); S->L->top--; S->L->nCcalls--; return f; } static void LoadHeader(LoadState* S) { char h[LUAC_HEADERSIZE]; char s[LUAC_HEADERSIZE]; luaU_header(h); LoadBlock(S,s,LUAC_HEADERSIZE); IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); } /* ** load precompiled chunk */ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) { LoadState S; if (*name=='@' || *name=='=') S.name=name+1; else if (*name==LUA_SIGNATURE[0]) S.name="binary string"; else S.name=name; S.L=L; S.Z=Z; S.b=buff; LoadHeader(&S); return LoadFunction(&S,luaS_newliteral(L,"=?")); } /* * make header */ void luaU_header (char* h) { int x=1; memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); h+=sizeof(LUA_SIGNATURE)-1; *h++=(char)LUAC_VERSION; *h++=(char)LUAC_FORMAT; *h++=(char)*(char*)&x; /* endianness */ *h++=(char)sizeof(int); *h++=(char)sizeof(size_t); *h++=(char)sizeof(Instruction); *h++=(char)sizeof(lua_Number); *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ }