<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="../assets/xml/rss.xsl" media="all"?><rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/"><channel><title>Lex Technica (dummy-headers)</title><link>http://code.lexarcana.com/</link><description></description><atom:link href="http://code.lexarcana.com/categories/dummy-headers.xml" type="application/rss+xml" rel="self"></atom:link><language>en</language><lastBuildDate>Sun, 14 Jun 2015 21:18:30 GMT</lastBuildDate><generator>http://getnikola.com/</generator><docs>http://blogs.law.harvard.edu/tech/rss</docs><item><title>Emacs dummy headers mode without Objective C</title><link>http://code.lexarcana.com/posts/emacs-dummy-headers-mode-without-objective-c.html</link><dc:creator>Ravi R Kiran</dc:creator><description>&lt;div&gt;&lt;p&gt;Choosing between C mode and C++ mode in emacs for header files can be a bit of a chore if both types of header files have a &lt;tt class="docutils literal"&gt;.h&lt;/tt&gt; extension. As usual, a package for solving this problem already exists: &lt;a class="reference external" href="http://www.emacswiki.org/emacs/dummy-h-mode.el"&gt;dummy-h-mode&lt;/a&gt;. One of the heuristics that it uses is to search for an implementation file and then to check the extension of that implementation file for a hint on the mode. This heuristic works very well, but with a minor annoyance that it tries to handle Objective C headers as well. In my use case, I sometimes have purely template-based C++ code and a test implementation in Matlab or &lt;a class="reference external" href="http://www.gnu.org/software/octave/"&gt;octave&lt;/a&gt;:&lt;/p&gt;
&lt;pre class="literal-block"&gt;
mycode.h
mycode.m
&lt;/pre&gt;
&lt;p&gt;&lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dummy-h-mode&lt;/span&gt;&lt;/tt&gt; unfortunately parses the Matlab/octave file as an Objective C file. Since I do not use Objective C, I used a quick and dirty hack to avoid it:&lt;/p&gt;
&lt;pre class="code elisp"&gt;&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-1"&gt;&lt;/a&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;defun&lt;/span&gt; &lt;span class="nv"&gt;ravi/do-not-allow-objc&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;mode-val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-2"&gt;&lt;/a&gt;  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;eq&lt;/span&gt; &lt;span class="nv"&gt;mode-val&lt;/span&gt; &lt;span class="ss"&gt;'objc-mode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-3"&gt;&lt;/a&gt;      &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-4"&gt;&lt;/a&gt;    &lt;span class="nv"&gt;mode-val&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-5"&gt;&lt;/a&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;mapc&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;lambda&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;x&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;advice-add&lt;/span&gt; &lt;span class="nv"&gt;x&lt;/span&gt; &lt;span class="ss"&gt;:filter-return&lt;/span&gt; &lt;span class="nf"&gt;#'&lt;/span&gt;&lt;span class="nv"&gt;ravi/do-not-allow-objc&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-6"&gt;&lt;/a&gt;      &lt;span class="o"&gt;'&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;dummy-h-mode-get-major-mode-by-source-file&lt;/span&gt;
&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-7"&gt;&lt;/a&gt;        &lt;span class="c1"&gt;;dummy-h-mode-get-major-mode-by-keywords&lt;/span&gt;
&lt;a name="rest_code_dce0971a19144c37be59e0241a43d0ac-8"&gt;&lt;/a&gt;        &lt;span class="nv"&gt;dummy-h-mode-get-major-mode-by-files-directory&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;/pre&gt;&lt;p&gt;We could simply avoid checking for &lt;tt class="docutils literal"&gt;.m&lt;/tt&gt; files, but that would involve intrusive changes to &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dummy-h-mode&lt;/span&gt;&lt;/tt&gt;. Instead, in the code above, we look at the result of obtaining the major mode by one of the file-based methods (checking the implementation, or checking the number of files), and set it to &lt;tt class="docutils literal"&gt;nil&lt;/tt&gt; if the result is Objective C. When one method returns nil, &lt;tt class="docutils literal"&gt;&lt;span class="pre"&gt;dummy-h-mode&lt;/span&gt;&lt;/tt&gt; goes on to the next heuristic. In my case, looking at keywords is almost always sufficient since most of my C++ code is in some &lt;tt class="docutils literal"&gt;namespace&lt;/tt&gt; which clearly indicates the correct mode.&lt;/p&gt;&lt;/div&gt;</description><category>C/C++</category><category>dummy-headers</category><category>emacs</category><guid>http://code.lexarcana.com/posts/emacs-dummy-headers-mode-without-objective-c.html</guid><pubDate>Sun, 14 Jun 2015 19:59:03 GMT</pubDate></item></channel></rss>