How on earth can zsh know whether it's starting at the beginning of a line or not?

Hi everyone,
I'm currently developing my own shell and I've been wondering how this awesome "printing a reverse-video percent character and a newline when the prompt doesn't start at the beginning of a line" feature is implemented in zsh.

Although it is obvious what is needed to implement this (a way to figure out whether you are at the beginning of a line or not), how to get it is much more complicated. Very weirdly, there seems to exist no way to know the cursor position in a terminal without clearing it and keeping track of the cursor, which a shell obviously cannot do since it can't control the output of the programs it launches (that is why my theory that zsh uses ncurses doesn't seem to hold, although it is very possible that it uses some ncurses functions).

I checked the zsh source code but I'm not used to doing such things, and besides, it's quite like looking for a needle in a haystack (even though I'm fairly confident the code is situated in the zle part)...

I although figured there might be a way to check for the beginning of the line indirectly, I didn't find any yet, so would anyone be able to help me find how zsh does this, or a way to get similar results?

Thanks a lot in advance.

Really? I thought this is what the [^[6n escape sequence was for? Besides:

$ which zsh
/usr/bin/zsh
$ ldd -r $( which zsh )
        linux-gate.so.1 =>  (0xffffe000)
        libcap.so.1 => /lib/libcap.so.1 (0xb7f6b000)
        libdl.so.2 => /lib/libdl.so.2 (0xb7f67000)
        libnsl.so.1 => /lib/libnsl.so.1 (0xb7f50000)
        libncurses.so.5 => /lib/libncurses.so.5 (0xb7f1b000)
        libm.so.6 => /lib/libm.so.6 (0xb7ef6000)
        libc.so.6 => /lib/libc.so.6 (0xb7dc3000)
        /lib/ld-linux.so.2 (0xb7f89000)
$ zsh --version
zsh 4.3.4 (i686-suse-linux-gnu)

That makes it sound like you just started programming, and are thinking that writing your own shell can't be that hard, since there are so many around. Well, the source code for KSH93 (which has far less features that zsh) comes in at roughly 2.5 MiB of code, and that's not counting any supporting libraries from the same archive.

Thanks, that does the trick (in a very ugly way though). No need to be ironical about it though, it's not as if hardcoded escape sequences were that much of an obvious solution (although it is true I could have found it myself, which I did, actually, a few hours after creating this thread).

I did say zsh possibly used ncurses functions ("although it is very possible that it uses some ncurses functions"). It has, in fact, a ncurses module. But that doesn't mean it uses ncurses to get the cursor position. It can't do this because ncurses knows it by clearing the screen an keeping track of it, which is incompatible with launching external programs easily.

Well I'm sorry if it sounds like that but that's just not true.

As a matter of fact, I found a better solution to this problem than ^[[6n. The end is not to find the cursor position, this is the mean. The real purpose here is to be able to stay on the current line if we're at the beginning of it or print a % and get to the next line otherwise. To achieve this I thought the cursor position was needed but it's not.
You can get the same result by always printing a %, and number_of_columns_in_the_terminal - 1 spaces, then a carriage return and the 'ce' termcap. This is in fact what my version of zsh seems to do (I tried selecting the end of the last line to check for spaces).