2012/03/15: "Somewhere on your filesystem" -- the emacs way

On Unix systems, it is quite common to put temporary files for feature xyz into a new directory called

${XYZPREFIX}`pwd`/xyz
For example the working directory for a port on FreeBSD is
${WRKDIRPREFIX}`pwd`/work
In that way, for the default value of WRKDIRPREFIX, which is the empty string, the build process will just create a directory work in the current directory. In some cases, however, you might want to have the work directory at some other place in your file hierarchy. For example, you want the temporary files to reside on a memory file system. Or you have several builds sharing a common ports tree (maybe mounted via nfs). Or you simply do not want to clutter the versioned ports tree with non-versioned files. In those cases, simply setting WRKDIRPREFIX does the job. And since `pwd` is included in the path, a parallel shadow of the original file hierarchy is built under the prefix -- and in that way, no conflicts arise.

So far, so good. A useful general concept. Now let's have a look at the text editor emacs. It also creates work-like files: backups and autosaves. By default they're generated in the current directory. And you can get them out of the way, if desired. But emacs wouldn't be emacs if it were as simple and clean as setting a prefix.

For backup files (those foo~ left all over the place), the variable backup-directory-alist contains an association list telling which files should go in which directory. The first component of each pair in that list is a regular expression; if it matches, the backup goes to the directory named in the second component -- literally as no directory hierarchy is created on the fly. Hence you only have a fixed number of directories (but they needn't have a non-trival common prefix) were backups are put into. So, how to avoid clashes between files with the same name residing in different directories? emacs has a solution (well, kind of) for that. It encode the full path in the file name (which would be perfectly fine) by simply replacing the file-hierarchy separator by an exclamation point; at that point, we simply hope that exclamation points are not used in file names too often...

For autosave files a different concept is used (would be too easy otherwise, wouldn't it?). Here the variable to look for is auto-save-file-name-transforms. It contains a list of lists, where the first entry is a regular expression matched against the file name and the second entry is the replacement string, if that expression matches. Subexpression matches can be accessed using \1, \2 etc. An optional third argument, if non-nil, advises emacs to "encode" the path in the file name with the already mentioned exclamation point method to avoid name clashes.