Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For exactly the same purpose I found <code>.dir-locals.el</code> to be <em>extremely</em> helpful, here's how one of mine looks like:</p> <pre><code>((nil . ((tab-width . 8) (indent-tabs-mode . t))) (c++-mode . ((c-basic-offset . 8) (tab-width . 8) (indent-tabs-mode . t) (compile-command . "make -C ../build -j 2 run_tests"))) ((c-mode . ((c-basic-offset . 8) (tab-width . 8) (indent-tabs-mode . t) (compile-command . "make -C ../build -j 2 run_tests"))))) </code></pre> <p>Obviously I can have paths specified in different <code>.dir_locals.el</code> according to their location and such, say some build <code>run_tests</code> for unit test, some build the real target and so on.</p> <p>Then I put a dummy <code>makefile</code> in the build directory, which looks like this:</p> <pre><code>all: run_tests run_tests: @cmake .. @make -j 2 </code></pre> <p>This way I can make a checkout and just run <code>M-x compile</code> in whichever file I like and it will do the right thing. I use git and have that Makefile <em>ignored</em> from monitoring by git like so:</p> <p><code>git update-index --assume-unchanged Makefile</code></p> <p>and if I want to modify and commit it I do </p> <p><code>git update-index --no-assume-unchanged Makefile</code>. </p> <p>This way the newly created by cmake Makefile won't show in <code>git status</code> as modified, and I won't commit it accidentally.</p> <p>Benefits of this approach:</p> <ul> <li><p>since cmake uses absolute paths internally, there's absolutely no problem jumping through compilation errors in compilation buffer by just pressing enter on them.</p></li> <li><p>you can specify whatever indentation rules you want there, and they can be different in different projects or even directories if you so please :)</p></li> <li><p>you can specify any amount of targets you want in different projects, even directories, still use the same old <code>M-x compile</code> (I have it bound to <code>C-c b</code>) and get Emacs to do <em>the right thing</em>.</p></li> </ul> <p>The only drawback there is having <code>.dir-locals.el</code> in your subdirectories, which I hardly find bad.</p> <p><strong>EDIT:</strong> robUK, to answer your comment:</p> <p>Here is <a href="http://www.gnu.org/software/emacs/manual/html_node/emacs/Directory-Variables.html" rel="noreferrer">documentation</a> for <em>Per-Directory Local Variables</em> as they call it in Emacs manual. It is not a package, it's a part of Emacs, although I'm not sure if it was available in pre-23 versions, I'm using 23.1.1.</p> <p><strong>EDIT2:</strong> liwp, to answer your question:</p> <p>As Charles already pointed out, and as documentation says:</p> <blockquote> <p>If you put a file with a special name .dir-locals.el in a directory, Emacs will read it when it visits any file in that directory or any of its subdirectories, and apply the settings it specifies to the file's buffer. Emacs searches for .dir-locals.el starting in the directory of the visited file, and moving up the directory tree. (To avoid slowdown, this search is skipped for remote files.)</p> </blockquote> <p>You don't need <code>.dir-locals.el</code> everywhere in your project tree. Note, however, it probably depends on the complexity of your code base structure.</p> <p>I normally would go for a fairly simple layout like this:</p> <pre><code>/project_root_dir /build /src /test </code></pre> <p>and I would have slightly different targets specified in different <code>.dir-locals.el</code>, say in <code>/test</code> I would only build <code>testrunner</code> and have it executed, I'd spend most of the development time building this target. Then in <code>/src</code> I would first build the same <code>testrunner</code>, have it executed,and if everything goes well there, it would build me my main project target. If you don't need this, you could be fine with just one <code>.dir-locals.el</code> under <code>/project_root_dir</code>. If your source structure and requirements are a lot more sophisticated, it could mean you'd need a bit more path- and target-related wizardry.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload