<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>insomnia bytes &#187; Emacs</title>
	<atom:link href="http://bytes.inso.cc/wp/category/emacs/feed/" rel="self" type="application/rss+xml" />
	<link>http://bytes.inso.cc/wp</link>
	<description>Imagination is a nightbird's dream</description>
	<lastBuildDate>Sat, 07 Nov 2009 17:13:07 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>dot-emacs: smarter indentation with tabs and spaces</title>
		<link>http://bytes.inso.cc/wp/2009/01/07/dot-emacs-smarter-indentation-with-tabs-and-spaces/</link>
		<comments>http://bytes.inso.cc/wp/2009/01/07/dot-emacs-smarter-indentation-with-tabs-and-spaces/#comments</comments>
		<pubDate>Wed, 07 Jan 2009 10:50:07 +0000</pubDate>
		<dc:creator>theefer</dc:creator>
				<category><![CDATA[Emacs]]></category>
		<category><![CDATA[configuration]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[elisp]]></category>
		<category><![CDATA[indentation]]></category>

		<guid isPermaLink="false">http://bytes.inso.cc/wp/?p=836</guid>
		<description><![CDATA[In XMMS2, the coding guidelines require you to use a mix of tabs and spaces to indent code: tabs indent blocks, while spaces are used to align multi-line statements. It&#8217;s all very natural really:

[tab][tab][tab]foo_function (arg1,
[tab][tab][tab]              arg2,
[tab][tab][tab]       [...]]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://xmms2.sf.net/">XMMS2</a>, the <a href="http://wiki.xmms2.xmms.se/wiki/XMMS2_Coding_Style#Indentation">coding guidelines</a> require you to use a mix of tabs and spaces to indent code: <em>tabs</em> indent blocks, while <em>spaces</em> are used to align multi-line statements. It&#8217;s all very natural really:</p>
<p><code>
<pre>[tab][tab][tab]foo_function (arg1,
[tab][tab][tab]              arg2,
[tab][tab][tab]              arg3)</pre>
<p></code></p>
<p>Unfortunately, if you use the default C indentation mode in emacs, what you get (when breaking like with <kbd>C-j</kbd> or RET TAB) is the following:</p>
<p><code>
<pre>[tab][tab][tab]foo_function (arg1,
[tab][tab][tab][tab][tab]    arg2,
[tab][tab][tab][tab][tab]    arg3)</pre>
<p></code></p>
<p>So you mean to tweak the code yourself to fix the tabs. Tedious, annoying. We know better!</p>
<p>I wrote a hook to fix this. The idea is to fix tabs when we&#8217;re in an argument list context. It can probably be further improved (please send patches!), but it&#8217;s quite useful already.</p>
<p>Just add the following to your .emacs file.</p>
<p>[code lang="lisp"]<br />
(defun xmms2-c-mode ()<br />
  "C mode with adjusted defaults for use with the xmms2."<br />
  (interactive)<br />
  (c-mode)<br />
  (c-set-style "K&#038;R")<br />
  (setq tab-width 4)<br />
  (setq indent-tabs-mode t)<br />
  (setq c-basic-offset 4)</p>
<p>  ; Align closing paren with opening paren<br />
  (c-set-offset 'arglist-close 'c-lineup-arglist-intro-after-paren)</p>
<p>  (add-hook 'c-special-indent-hook 'smart-tab-indent-hook))</p>
<p>(defun get-nonempty-context ()<br />
  (let ((curr-context (car (c-guess-basic-syntax))))<br />
    (if (or (eq (car curr-context) 'arglist-intro)<br />
            (eq (car curr-context) 'arglist-cont)<br />
            (eq (car curr-context) 'arglist-cont-nonempty)<br />
            (eq (car curr-context) 'arglist-close))<br />
        curr-context<br />
      nil)))</p>
<p>(defun smart-tab-indent-hook ()<br />
  "Fixes indentation to pad with spaces in arglists."<br />
  (let ((nonempty-ctx (get-nonempty-context)))<br />
    (if nonempty-ctx<br />
        (let ((tabbed-columns (+ (point-at-bol)<br />
                                 (/ (c-langelem-col nonempty-ctx t)<br />
                                    tab-width)))<br />
              (orig-column (current-column)))<br />
          (tabify (point-at-bol) tabbed-columns)<br />
          (untabify tabbed-columns (point-at-eol))<br />
          ; editing tabs screws the pointer position<br />
          (move-to-column orig-column)))))<br />
[/code]</p>
<p>Note: don&#8217;t forget the <code>c-set-offset</code> bit, else it won&#8217;t work properly for the last argument before the closing parenthesis. And avoid using <code>c-indent-new-comment-line</code> to break lines, it can fail with this hook; use <code>c-context-line-break</code> instead, it&#8217;s smarter anyway!</p>
]]></content:encoded>
			<wfw:commentRss>http://bytes.inso.cc/wp/2009/01/07/dot-emacs-smarter-indentation-with-tabs-and-spaces/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
