<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: indent code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 21:34:27 GMT</pubDate>
    <description>DZone Snippets: indent code</description>
    <item>
      <title>scala extensions for emacs</title>
      <link>http://snippets.dzone.com/posts/show/5013</link>
      <description>// description of your code here&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;(require 'scala-mode)&lt;br /&gt;(require 'compile)&lt;br /&gt;(require 'flymake)&lt;br /&gt;(require 'font-lock)&lt;br /&gt;&lt;br /&gt;(defvar scala-build-commad nil)&lt;br /&gt;(make-variable-buffer-local 'scala-build-command)&lt;br /&gt;&lt;br /&gt;(add-hook 'scala-mode-hook&lt;br /&gt;          (lambda ()&lt;br /&gt;	    (flymake-mode-on)&lt;br /&gt;	    ))&lt;br /&gt;&lt;br /&gt;(defun flymake-scala-init ()&lt;br /&gt;  (let* ((text-of-first-line (buffer-substring-no-properties (point-min) (min 20 (point-max)))))&lt;br /&gt;    (progn&lt;br /&gt;      (remove-hook 'after-save-hook 'flymake-after-save-hook t)&lt;br /&gt;      (save-buffer)&lt;br /&gt;      (add-hook 'after-save-hook 'flymake-after-save-hook nil t)&lt;br /&gt;      (if (string-match "^//script" text-of-first-line)&lt;br /&gt;	  (list "fsc" (list "-Xscript" "MainScript" "-d" "c:/tmp" buffer-file-name))&lt;br /&gt;	(or scala-build-command (list "fsc" (list "-d" "c:/tmp" buffer-file-name))))&lt;br /&gt;      )))&lt;br /&gt;&lt;br /&gt;(push '(".+\\.scala$" flymake-scala-init) flymake-allowed-file-name-masks)&lt;br /&gt;(push '("^\\(.*\\):\\([0-9]+\\): error: \\(.*\\)$" 1 2 nil 3) flymake-err-line-patterns)&lt;br /&gt;&lt;br /&gt;(set (make-local-variable 'indent-line-function) 'scala-indent-line)&lt;br /&gt;&lt;br /&gt;(defun scala-indent-line ()&lt;br /&gt;  "Indent current line of Scala code."&lt;br /&gt;  (interactive)&lt;br /&gt;  (indent-line-to (max 0 (scala-calculate-indentation))))&lt;br /&gt;&lt;br /&gt;(defun scala-calculate-indentation ()&lt;br /&gt;  "Return the column to which the current line should be indented."&lt;br /&gt;  (save-excursion&lt;br /&gt;    (scala-maybe-skip-leading-close-delim)&lt;br /&gt;    (let ((pos (point)))&lt;br /&gt;      (beginning-of-line)&lt;br /&gt;      (if (not (search-backward-regexp "[^\n\t\r ]" 1 0))&lt;br /&gt;	  0&lt;br /&gt;	(progn&lt;br /&gt;	  (scala-maybe-skip-leading-close-delim)&lt;br /&gt;	  (+ (current-indentation) (* 2 (scala-count-scope-depth (point) pos))))))))&lt;br /&gt;&lt;br /&gt;(defun scala-maybe-skip-leading-close-delim ()&lt;br /&gt;  (beginning-of-line)&lt;br /&gt;  (forward-to-indentation 0)&lt;br /&gt;  (if (looking-at "\\s)")&lt;br /&gt;      (forward-char)&lt;br /&gt;    (beginning-of-line)))&lt;br /&gt;&lt;br /&gt;(defun scala-face-at-point (pos)&lt;br /&gt;  "Return face descriptor for char at point."&lt;br /&gt;  (plist-get (text-properties-at pos) 'face))&lt;br /&gt;&lt;br /&gt;(defun scala-count-scope-depth (rstart rend)&lt;br /&gt;  "Return difference between open and close scope delimeters."&lt;br /&gt;  (save-excursion&lt;br /&gt;    (goto-char rstart)&lt;br /&gt;    (let ((open-count 0)&lt;br /&gt;	  (close-count 0)&lt;br /&gt;	  opoint)&lt;br /&gt;      (while (and (&lt; (point) rend)&lt;br /&gt;		  (progn (setq opoint (point))&lt;br /&gt;			 (re-search-forward "\\s)\\|\\s(" rend t)))&lt;br /&gt;	(if (= opoint (point))&lt;br /&gt;	    (forward-char 1)&lt;br /&gt;	  (cond&lt;br /&gt;&lt;br /&gt;            ;; Use font-lock-mode to ignore strings and comments&lt;br /&gt;	   ((scala-face-at-point (- (point) 1))) &lt;br /&gt;&lt;br /&gt;	   ((looking-back "\\s)")&lt;br /&gt;	    (incf close-count))&lt;br /&gt;	   ((looking-back "\\s(")&lt;br /&gt;	    (incf open-count))&lt;br /&gt;	   )))&lt;br /&gt;      (- open-count close-count))))&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;(provide 'scala-extensions)&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 20 Jan 2008 02:26:36 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/5013</guid>
      <author>aemon (Aemon Cannon)</author>
    </item>
    <item>
      <title>PHP function for cleaning up HTML and JavaSctipt code</title>
      <link>http://snippets.dzone.com/posts/show/1964</link>
      <description>// This is a function for PHP scripts to clean up HTML code before outputting it.&lt;br /&gt;// The function applies correct indentation to HTML/XHTML 1.0 and JavaScript&lt;br /&gt;// And makes the output much more readable.&lt;br /&gt;// You can specify the wanted indentation through the variable $indent&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;?php&lt;br /&gt;&lt;br /&gt;//Function to seperate multiple tags one line&lt;br /&gt;function fix_newlines_for_clean_html($fixthistext)&lt;br /&gt;{&lt;br /&gt;	$fixthistext_array = explode("\n", $fixthistext);&lt;br /&gt;	foreach ($fixthistext_array as $unfixedtextkey =&gt; $unfixedtextvalue)&lt;br /&gt;	{&lt;br /&gt;		//Makes sure empty lines are ignores&lt;br /&gt;		if (!preg_match("/^(\s)*$/", $unfixedtextvalue))&lt;br /&gt;		{&lt;br /&gt;			$fixedtextvalue = preg_replace("/&gt;(\s|\t)*&lt;/U", "&gt;\n&lt;", $unfixedtextvalue);&lt;br /&gt;			$fixedtext_array[$unfixedtextkey] = $fixedtextvalue;&lt;br /&gt;		}&lt;br /&gt;	}&lt;br /&gt;	return implode("\n", $fixedtext_array);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;function clean_html_code($uncleanhtml)&lt;br /&gt;{&lt;br /&gt;	//Set wanted indentation&lt;br /&gt;	$indent = "    ";&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;	//Uses previous function to seperate tags&lt;br /&gt;	$fixed_uncleanhtml = fix_newlines_for_clean_html($uncleanhtml);&lt;br /&gt;	$uncleanhtml_array = explode("\n", $fixed_uncleanhtml);&lt;br /&gt;	//Sets no indentation&lt;br /&gt;	$indentlevel = 0;&lt;br /&gt;	foreach ($uncleanhtml_array as $uncleanhtml_key =&gt; $currentuncleanhtml)&lt;br /&gt;	{&lt;br /&gt;		//Removes all indentation&lt;br /&gt;		$currentuncleanhtml = preg_replace("/\t+/", "", $currentuncleanhtml);&lt;br /&gt;		$currentuncleanhtml = preg_replace("/^\s+/", "", $currentuncleanhtml);&lt;br /&gt;		&lt;br /&gt;		$replaceindent = "";&lt;br /&gt;		&lt;br /&gt;		//Sets the indentation from current indentlevel&lt;br /&gt;		for ($o = 0; $o &lt; $indentlevel; $o++)&lt;br /&gt;		{&lt;br /&gt;			$replaceindent .= $indent;&lt;br /&gt;		}&lt;br /&gt;		&lt;br /&gt;		//If self-closing tag, simply apply indent&lt;br /&gt;		if (preg_match("/&lt;(.+)\/&gt;/", $currentuncleanhtml))&lt;br /&gt;		{ &lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If doctype declaration, simply apply indent&lt;br /&gt;		else if (preg_match("/&lt;!(.*)&gt;/", $currentuncleanhtml))&lt;br /&gt;		{ &lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If opening AND closing tag on same line, simply apply indent&lt;br /&gt;		else if (preg_match("/&lt;[^\/](.*)&gt;/", $currentuncleanhtml) &amp;&amp; preg_match("/&lt;\/(.*)&gt;/", $currentuncleanhtml))&lt;br /&gt;		{ &lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If closing HTML tag or closing JavaScript clams, decrease indentation and then apply the new level&lt;br /&gt;		else if (preg_match("/&lt;\/(.*)&gt;/", $currentuncleanhtml) || preg_match("/^(\s|\t)*\}{1}(\s|\t)*$/", $currentuncleanhtml))&lt;br /&gt;		{&lt;br /&gt;			$indentlevel--;&lt;br /&gt;			$replaceindent = "";&lt;br /&gt;			for ($o = 0; $o &lt; $indentlevel; $o++)&lt;br /&gt;			{&lt;br /&gt;				$replaceindent .= $indent;&lt;br /&gt;			}&lt;br /&gt;			&lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;		}&lt;br /&gt;		//If opening HTML tag AND not a stand-alone tag, or opening JavaScript clams, increase indentation and then apply new level&lt;br /&gt;		else if ((preg_match("/&lt;[^\/](.*)&gt;/", $currentuncleanhtml) &amp;&amp; !preg_match("/&lt;(link|meta|base|br|img|hr)(.*)&gt;/", $currentuncleanhtml)) || preg_match("/^(\s|\t)*\{{1}(\s|\t)*$/", $currentuncleanhtml))&lt;br /&gt;		{&lt;br /&gt;			$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;&lt;br /&gt;			&lt;br /&gt;			$indentlevel++;&lt;br /&gt;			$replaceindent = "";&lt;br /&gt;			for ($o = 0; $o &lt; $indentlevel; $o++)&lt;br /&gt;			{&lt;br /&gt;				$replaceindent .= $indent;&lt;br /&gt;			}&lt;br /&gt;		}&lt;br /&gt;		else&lt;br /&gt;		//Else, only apply indentation&lt;br /&gt;		{$cleanhtml_array[$uncleanhtml_key] = $replaceindent.$currentuncleanhtml;}&lt;br /&gt;	}&lt;br /&gt;	//Return single string seperated by newline&lt;br /&gt;	return implode("\n", $cleanhtml_array);	&lt;br /&gt;}&lt;br /&gt;?&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 27 Apr 2006 13:53:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1964</guid>
      <author>Jonhoo ()</author>
    </item>
    <item>
      <title>indent function</title>
      <link>http://snippets.dzone.com/posts/show/1109</link>
      <description>&lt;code&gt;&lt;br /&gt;    indent: func [&lt;br /&gt;        "Prepend a leader to all lines in a string."&lt;br /&gt;        string [string!]&lt;br /&gt;        /by&lt;br /&gt;            amount [integer!]&lt;br /&gt;        /with&lt;br /&gt;            leader [string! char!]&lt;br /&gt;        /local&lt;br /&gt;            lines dent&lt;br /&gt;    ] [&lt;br /&gt;        if not by   [amount: 1]&lt;br /&gt;        if not with [leader: " "]&lt;br /&gt;        dent: mk-string amount leader&lt;br /&gt;        lines: parse/all string form newline&lt;br /&gt;        foreach line lines [insert line dent]&lt;br /&gt;        build-dlm-str lines newline&lt;br /&gt;    ]&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Tue, 10 Jan 2006 05:56:28 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1109</guid>
      <author>gregg.irwin (Gregg Irwin)</author>
    </item>
    <item>
      <title>XMLPrettyPrint: simple xml pretty print in perl</title>
      <link>http://snippets.dzone.com/posts/show/929</link>
      <description>&lt;code&gt;&lt;br /&gt;### begin_: file metadata&lt;br /&gt;    ### &lt;region-file_info&gt;&lt;br /&gt;    ### main:&lt;br /&gt;    ###   - name    : XMLPrettyPrint: simple xml pretty print in perl&lt;br /&gt;    ###     desc    : use perl with XML::Twig library to print indented xml&lt;br /&gt;    ###     date    : created="Thu 2005-12-01 11:08:15"&lt;br /&gt;    ###     last    : lastmod="Thu 2005-12-01 11:22:34"&lt;br /&gt;    ###     lang    : perl&lt;br /&gt;    ###     tags    : perl xml indent formatted pretty string cfPrettyPrint&lt;br /&gt;    ### &lt;/region-file_info&gt;&lt;br /&gt;&lt;br /&gt;### begin_: init perl&lt;br /&gt;    use strict;&lt;br /&gt;    use warnings;&lt;br /&gt;    use XML::Twig;&lt;br /&gt;&lt;br /&gt;### begin_: init vars&lt;br /&gt;    my  $sXML  = join "", (&lt;DATA&gt;);&lt;br /&gt;&lt;br /&gt;    ### init params&lt;br /&gt;    my  $params = [qw(none nsgmls nice indented record record_c)];&lt;br /&gt;    my  $sPrettyFormat  = $params-&gt;[3] || 'none';&lt;br /&gt;&lt;br /&gt;### begin_: process&lt;br /&gt;    my  $twig= new XML::Twig;&lt;br /&gt;    $twig-&gt;set_indent(" "x4);&lt;br /&gt;    $twig-&gt;parse( $sXML );&lt;br /&gt;    $twig-&gt;set_pretty_print( $sPrettyFormat );&lt;br /&gt;    $sXML      = $twig-&gt;sprint;&lt;br /&gt;&lt;br /&gt;### begin_: output&lt;br /&gt;    print $sXML;&lt;br /&gt;&lt;br /&gt;### begin_: sample data&lt;br /&gt;    1;&lt;br /&gt;    __END__&lt;br /&gt;&lt;table&gt;&lt;tr age="35" &gt;&lt;br /&gt;&lt;fname&gt;Homer&lt;/fname&gt;&lt;br /&gt;&lt;lname&gt;Simpson&lt;/lname&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr age="33" &gt;&lt;br /&gt;&lt;fname&gt;Barney&lt;/fname&gt;&lt;br /&gt;&lt;lname&gt;Rubble&lt;/lname&gt;&lt;/tr&gt;&lt;br /&gt;&lt;tr age="29" &gt;&lt;br /&gt;&lt;fname&gt;Betty&lt;/fname&gt;&lt;br /&gt;&lt;lname&gt;Rubble&lt;/lname&gt;&lt;/tr&gt;&lt;/table&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 02 Dec 2005 06:23:55 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/929</guid>
      <author>drefty (drefty)</author>
    </item>
  </channel>
</rss>
