[ Direct Download: Latest TeXShop | Latest TeXShop Source ] [ Contact ]

Automatic Saving

Saving source files to disk is an essential feature of TeXShop, and a bug which broke this operation would be fatal to the future of the program. Luckily, most steps in this crucial chain are done automatically by Cocoa; the only spot where TeXShop code is involves comes when Cocoa asks what data to save and we point to the string containing the full source. I would not dare modify the TeXShop "Save" or "Save As" code.

However, in Lion this system was completely rewritten by Apple. The new code automatically saves files every few minutes, so it is essentially impossible to lose files you are writing. If your power goes out on a desktop without a battery, you might lose a sentence or two, but nothing more. You do not need to save files before quitting the program, or before closing windows, because that is done automatically. In a sense, you can forget the Save operation. The system handles everything automatically.

A University of Oregon faculty member once called me at midnight. He was writing the final exam for a graduate analysis course to be given at 8 AM the next morning, he was almost done, but he accidentally closed the source file without saving or typesetting. He asked what he could do. The only answer I could give was "start over." This problem cannot happen on Lion or higher.

Saving every few minutes is not noticeable because the system does not save the complete file. It only saves the changes. So if your entire book is one gigantic source file, you will still not notice the save operation. I'm sure this holds because automatic saving has been the norm on TeXShop for ten years and I have never received a complaint.

Since the Macintosh is only saving changes, it has a complete record of all the versions of the document. Theoretically, you could go back to earlier versions and reconstruct the document as it was yesterday, or last month. As we will see in a moment, this isn't just theoretically possible --- it is actually easy.

You might wonder if the file containing a document has all the old versions hidden away somewhere. Could I send someone the TeX source of a letter of recommendation which currently says ``Paul is a creative student'', but whose initial version said ``Paul hasn't had a new idea in years"? Happily, no. Files on disk only contain the latest version.

What happens if I decide to experiment , get confused, lose my way, and end up with a source document that makes no sense, and then discover that my crazy changes have been saved out from under me? You can use undo to get back to the original document because TeXShop's undo works even across file saves.

However, Apple has a much nicer solution to this problem. The TeXShop File menu has an item named "Revert To". Selecting this item gives a choice between two items, and one reads "Browse All Versions".

The Macintosh screen then switches to a Time Machine view showing the current document on the left and a receding stack of earlier views on the right. You can leaf through these earlier versions, noting the date and time that the version was created. All these views are alive, so you can copy and paste between versions, or restore an earlier version. This works even if you have not activated Time Machine. So it is a personal Time Machine just for TeXShop source documents.

Additional details about Saving are in the TeXShop Manual. But perhaps this initial description is still alarming. A TeXShop preference item allows you to turn Automatic Saving off. The preference item is hidden away, but described in the manual. I really, really do not recommend using that item, and I don't know any users who now use it.

Automatic saving is a courageous and enormous change by Apple, made possible by the solution of the fragile base class problem. You might wonder how much TeXShop code we had to change to activate this change. It is easy to answer that question. We had to add the following lines. The top line reads the hidden preference to obtain YES if we want automatic saving and NO otherwise. The bottom lines return this answer to a Cocoa procedure which asks if we should "autosaveInPlace".

   doAutoSave = [SUD boolForKey:AutoSaveEnabledKey];
   
   + (BOOL)autosavesInPlace
       {
         return doAutoSave;
       }

There is one other surprise. The TeXShop source code contains a file named MainMenu.nib which displays the various menus in the program. If you compare the File menu described by this file with the actual File menu displayed by TeXShop when running, you will discover that the two menus do not agree. The running program contains several items related to Automatic Saving, including that "Revert To:" menu item mentioned earlier. That item is not mentioned in the TeXShop source code because the TeXShop menus were created long before automatic saving was even a dream in an Apple programmer's eye. But fixing the fragile base class problem allows Apple to reach into those menu objects and rewrite them on the fly whenever TeXShop starts up. That is possible because TeXShop used the default File menu items recommended by Apple long ago. Apple knows about those items, so it knows how to change them.