Set WP-Syntax tab width

Since I’m using the WP-Syntax plug-in on my blog I tried to optimize font size and the tab width. Like a lot of developers I heavily use tabs in my source code. These tabs are really wide within the browser so I was looking for a way to minimize the used space.

I’m not a CSS guy but it seems like there is no way to set a specific tab width. So why not replace the tab with white space?

My change to wp-syntax/wp-syntax.php:

function wp_syntax_highlight($match)
{
    global $wp_syntax_matches;
 
    $i = intval($match[1]);
    $match = $wp_syntax_matches[$i];
 
    $language = strtolower(trim($match[1]));
    $line = trim($match[2]);
    $escaped = trim($match[3]);
    $code = wp_syntax_code_trim($match[4]);
 
    // INSERT JUST THIS LINE OF CODE
    $code = str_replace ( '\t' , '   ' , $code );
    .
    .
    .

4 thoughts on “Set WP-Syntax tab width”

  1. Thank you so much for this! Was driving me mad!
    But after coping your code it just did not work (I have maybe a newer version) so I changed your line to this:

    $code = str_replace ( "\t", ' ' , $code );

    And it works like a charm.

  2. I had the same problem derRaab! Replace \t by fixed spaces is not really correct, tab char represent variables space char. I wrote a quickly solution making this two changes in wp-syntax/wp-syntax.php:

    Change 1:

    if ($this->header_type != GESHI_HEADER_PRE && $this->header_type != GESHI_HEADER_PRE_VALID) {
    	$this->indent($parsed_code);
    } else{
    	$this->indentPRE($parsed_code);
    }
    

    Change 2, insert a new funtion for my pre-blocks:

    function indentPRE(&$result) {
    	/// Replace tabs with the correct number of spaces
    	if (false !== strpos($result, "\t")) {
    		$lines = explode("\n", $result);
    		$result = null;//Save memory while we process the lines individually
    		$tab_width = $this->get_real_tab_width();
    		$tab_string = ' ' . str_repeat(' ', $tab_width);
    
    		for ($key = 0, $n = count($lines); $key < $n; $key++) {
    			$line = $lines[$key];
    			if (false === strpos($line, "\t")) {
    				continue;
    			}
    
    			$pos = 0;
    			$length = strlen($line);
    			$lines[$key] = ''; // reduce memory
    
    			
    			for ($i = 0; $i  ' ',
    					//  2 => '  ',
    					//  3 => '   ' etc etc
    					// to use instead of building a string every time
    					$tab_end_width = $tab_width - ($pos % $tab_width); //Moved out of the look as it doesn't change within the loop
    					if (($pos & 1) || 1 == $tab_end_width) {
    						$str .= substr($tab_string, 6, $tab_end_width);
    					} else {
    						$str .= substr($tab_string, 0, $tab_end_width+5);
    					}
    					$lines[$key] .= $str;
    					$pos += $tab_end_width;
    
    					if (false === strpos($line, "\t", $i + 1)) {
    						$lines[$key] .= substr($line, $i + 1);
    						break;
    					}
    					
    				} else if (0 == $pos && ' ' == $char) {
    					$lines[$key] .= ' ';
    					++$pos;
    				} else {
    					$lines[$key] .= $char;
    					++$pos;
    				}
    				
    			}
    		}
    		$result = implode("\n", $lines);
    		unset($lines);//We don't need the lines separated beyond this --- free them!
    	}
    }
    

    This last function is a copy-paste-modify of ident().

    If I didn’t make a mistake this corrected tab’s size (in my case 8 to 4).

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.