How to detect a tab character when processing a literate in lstlistings?

In the solution to Making more easy the itemized of item with tabulation system, I am counting up the number of spaces in order to determine the type of leading character to insert. However, if the I replace the leading spaces with a tab character, the solution does not work.

If I could detect the tab character in the literate, then I could have \ProcessSpace increment the counter NumOfContigousSpaces appropriately, but I don't know how to test for it?

I thought adding tabsize=4, keepspaces=true would do the job but this is not quite enough. So I attempted to use lstag@tabulator from How to automatically skip leading white spaces in listings, but was not able to get that to work.

The code below has a 4 leading spaces before the W in the first line and a tab as the leading character before the W in the second line. This produces no bullet for the line with a tab:

The correct output can be seen by using 4 spaces before the W in both lines:

Note:

• It appears the posting a code snippet here replaces a tab with 4 spaces. So to use the MWE below you will need to replace the four leading spaces before the Wxxx with a tab character.

Code:

\documentclass{article}
\usepackage{pgf}
\usepackage{xstring}
\usepackage{listings}

\newcounter{NumOfContigousSpaces}%
\setcounter{NumOfContigousSpaces}{0}%

\newcommand{\Width}{1}%
\pgfmathtruncatemacro{\BulletType}{\arabic{NumOfContigousSpaces}/4}%
\IfEqCase{\BulletType}{%
{0}{\gdef\Width{1}}
{1}{\gdef\Width{3}$\bullet$ }
{2}{\gdef\Width{3}$\circ$ }
{3}{\gdef\Width{3}$\times$ }
{4}{\gdef\Width{3}$\star$ }
{5}{\gdef\Width{3}$-$ }
}[\gdef\Width{3}$\bullet$ ]%
#1%
\setcounter{NumOfContigousSpaces}{0}%
}%
\newcommand*{\ProcessSpace}{%
\space%
}%
\newcommand*{\ProcessTab}{%
\space\space\space\space%
}%

\makeatletter
\lstdefinestyle{MyItemize}{%
basicstyle=\ttfamily,
columns=flexible,
tabsize=4, keepspaces=true,
literate=%
{\ }{{{\ProcessSpace}}}1% Count contigous spaces
{lstag@tabulator}{{{\ProcessTab}}}4% ??? how detect a tab?
%
%--- much code removed here (See http://tex.stackexchange.com/questions/57939/making-more-easy-the-itemized-of-item-with-tabulation-system for full code)
}%
\makeatother

\begin{document}
\begin{lstlisting}[style=MyItemize]
Wxxx xxx
Wxxx xx xx
\end{lstlisting}
\end{document}

-
Using \^^I is the 'safe' way to type in a tab, and will let listings do the conversion. Unfortunately, there seems to be something up with the width of characters used when columns = flexible is set, as the space used for the 'tab' bullet is different from that for the 'four space' bullet. –  Joseph Wright Jun 1 '12 at 6:32
@JosephWright: Please make that the answer as I was able to get it to work just fine with that. Required replacing the call to \ProcessSpace{4} with a call to \ProcessTab which is defined as \newcommand*{\ProcessTab}{\addtocounter{NumOfContigousSpaces}{4}\space\space\sp‌​ace\space} –  Peter Grill Jun 1 '12 at 19:34

Using \^^I is the 'safe' way to type in a tab, and will let listings do the conversion.

Replacing the line using lstag@tabulator with the following:

    {\^^I}{{{\ProcessTab}}}4%


produces:

(There seems to be something up with the width of characters used when columns = flexible is set, as the space used for the 'tab' bullet is different from that for the 'four space' bullet.)

-

you can try something like:

\lstset{
...
tab=$\bullet$\space,
showtabs=true,
...
}

-
I don't want listings to replace the tab with a \bullet (as that results in two \bullets for the case where there are two leading tabs. I want the \literate to be able to detect it and treat it as 4 spaces. –  Peter Grill Jun 1 '12 at 19:26