View Full Version : Storing info with text file

Nov 17, 2007, 03:19 PM
Hi all,

I am writing a text editor in Cocoa. I plan to implement code folding and line bookmarking. However, I cannot figure out how to store this information so that it will be available the next time the text file is opened. When using TextMate, for instance, your code folding and line bookmarks are restored when you reopen a text file even if it has been moved or renamed. How is this possible?


Nov 17, 2007, 03:28 PM
Without seeing the TextMate source code I couldn't say for sure but I'd suggest they use HFS+ Extended Attributes to store the data...

Nov 17, 2007, 04:32 PM
Wow, kudos to the TextMate developer. That's pretty slick :)

Nov 17, 2007, 08:16 PM
Using HFS+ attributes is a possibility, I suppose, but I have been hearing a lot about ZFS and I would rather use a more portable approach, if possible. Even so, can anyone point me to example code for manipulating HFS+ attributes? I can't seem to find anything.

Someone suggested storing aliases as data in a file. (As I understand it, aliases can only be used with Carbon code; perhaps this has changed in 10.5?) However, this is once again beyond me and I would appreciate any pointers on how to go about implementing this. On the other hand, if I used this approach I assume I would have to iterate through each alias and compare it to the file being opened, which could take a long time.

What is my best option?

Nov 18, 2007, 02:20 AM
There is no built in Cocoa API for this. I you end up in Carbon or worse to do it. I was trying to find the API last night but gave up.

I can, however, confirm that text mates uses Extended Attributes for something:

MacBook-Pro:java robbie$ xattr -l PairMatcher.java
com.macromates.caret: {column = 49; line = 8; }

PairMatcher.java has been edited by TextMate and as you can see has an extended attribute which matches their domain. This only appears to hold the caret position, not the folding status.

I can't find any API other than the BSD level setxattr (http://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/setxattr.2.html) and getxattr (http://developer.apple.com/documentation/Darwin/Reference/ManPages/man2/getxattr.2.html).

Note there is no issue in using these from Cocoa.