Why you should use #pragma mark

2007-01-20 04:17:06 -08:00

It’s a minute and a half, and it’s 200 K, and you’ll need QuickTime 7 to watch it.


pragma mark at work in Xcode’s editor.

Useful as #pragma mark is, I wouldn’t recommend it for cross-platform code. GCC’s documentation of #pragma mark seems to suggest that it’s Darwin-only.

15 Responses to “Why you should use #pragma mark”

  1. Mark Grimes Says:

    That sure was a lot of typing! Textmate ftw. But no, the pragma compiler directive is a C standard (C99 iirc), not limited to Darwin. Use it, live it, love it! But alas I do recommend checking out Textmate if you enjoying typing less to represent the same amount of code.

  2. Ian Baird Says:

    Add “#pragma mark -” above your “#pragma mark <label>” statements to get a nice little horizontal separator in the menu above the bold rendering of the label.

  3. Peter Hosey Says:

    #pragma is standard, but #pragma mark is not.

    And TextMate is OK, but missing some things:

    • It does not let me color preprocessor directives. I like having them in yellow, so they stand out.
    • It colors printf formatters using “user-defined constant” (constant.other), and doesn’t let me set a different color for it. I, for one, don’t want them colored at all.
    • #pragma marks aren’t emboldened in the function pop-up.

    Regarding “#pragma mark -”: I used to use it, but got Bored of typing it before every #pragma mark. I now survive on a label mark alone, with no separator.

    Trivia: The “#pragma mark -” trick used to be free. When you used the Toolbox procedures AppendMenu and InsertMenuItem (which still exist in Carbon), inserting a menu item titled “-” inserted a separator. (I first found this out in HyperCard as a kid, although at the time I didn’t know anything about the Toolbox.) Sadly, the modern Menu Manager (introduced in 8.5, with AppendMenuItemText and InsertMenuItemText among other things) and NSMenuItem don’t do this; one must write one’s own code to handle “#pragma mark -”.

  4. Erik Says:

    [quote]It does not let me color preprocessor directives.[/quote]

    Sure it does. Just set a color for the scope “meta.preprocessor.c” (and “meta.preprocessor.c keyword”).

    [quote]It colors printf formatters […] and doesn’t let me set a different color […].[/quote]

    Change the color for the “string constant” scope. It worked for me, anyway.

    [quote]#pragma marks aren’t emboldened in the function pop-up.[/quote]

    True, but this becomes less of an issue if you use “#pragma mark -“, which you could insert in TextMate with a single keystroke.

  5. Erik Says:

    Sorry, some of the formatting got lost in my previous comment (even though it looked fine in the preview). I hope it’s readable.

  6. Peter Hosey Says:

    I’ve edited the formatting of your comment. I wonder what happened to the “quote” link that I used to have on each one…

    [quote]Sure it does. Just set a color for the scope “meta.preprocessor.c” (and “meta.preprocessor.c keyword”).[/quote]

    It would be nice if that were documented. (I’m assuming that it’s not documented because Google(textmate C preprocessor syntax coloring) fails to find anything for it.)

    [quote][quote]It colors printf formatters […] and doesn’t let me set a different color […].[/quote]

    Change the color for the “string constant” scope. It worked for me, anyway.[/quote]

    I don’t want to change the color for string constants. I want to change the color for printf formatters to be the same as the color for string constants.

    [quote][quote]#pragma marks aren’t emboldened in the function pop-up.[/quote]

    True, but this becomes less of an issue if you use “#pragma mark -“, which you could insert in TextMate with a single keystroke.[/quote]

    The reason why I don’t do that is that I don’t like having two pragma marks for 99% of my section divisions.

    I, for one, would welcome an editor that inserted a separator before every mark so that specifying one explicitly wouldn’t be necessary. (Extra credit if it suppressed multiple consecutive separators, for the benefit of legacy code that already does have explicit separators.)

  7. Scott Stevenson Says:

    I thought the same thing as Mark when I watched the video. You can get the exact same text on the screen in about 1/4 the time with TextMate, maybe less with some custom bundle items.

    Check out TextMate’s help, chapters 12 and 13 specifically. I believe it answers your questions. The trick is that you can get the scope for any particular bit of text using a shortcut (control-shift-p by default). Not all of the scopes are explicitly documented explicitly because there’s quite a lot of them and you can just make up your own.

    People seem resistant to giving TextMate a real shot because there’s this idea that it can’t be that much better than what’s already out there. But it really is. Google for “textmate objective-c screencast”.

  8. Paul Ferguson Says:

    To use #pragma mark in cross platform code, just enclose it like this:

    #if 0
    #pragma mark –
    #endif

    Xcode and other “#pragma mark” aware apps will still recognize the pragma directive, but other, shall we say, unenlightened compilers (*cough* Visual Studio *cough*) will not generate compile errors over them.

  9. Erik Says:

    I don’t want to change the color for string constants. I want to change the color for printf formatters to be the same as the color for string constants.

    If you wanted to change the color of the strings themselves you’d simply use the scope selector “string”. The scope selector “string constant” is different, and it actually does apply to printf formatters:

    The printf formatters are named constant.other.placeholder.c by the TextMate language grammar for C, i.e. they are defined as descendants of constant. That’s why they normally inherit the color that’s set for constants. The scope selector “string constant” matches any constant (or descendant thereof, like a printf formatter) that lives within a string (which printf formatters do).

    There are other ways to specify a scope selector that will apply to printf formatters. A good choice might be “string constant.other.placeholder” (or just “constant.other.placeholder”). I suggested “string constant” in my first comment because it was shorter.

    While setting a color for printf formatters should work just fine, in this case it’s probably easier to prevent them from inheriting the color from constants to begin with. This is simply a matter of changing the scope selector of the color setting for constants to “constant – string constant” (or “constant – constant.other.placeholder.c”).

  10. Peter Hosey Says:

    Very flexible. Thanks, Erik.

    I especially didn’t know that it shared any behavior with CSS selectors. I only knew that you could specify exactly one scope identifier; I didn’t know that you could specify multiple scopes and it would look for the according nested scope. That’s very cool.

    Maybe someday I’ll give TextMate another go, with this syntax-coloring peeve out of the way.

  11. Wincent Colaiuta Says:

    I have:

    #pragma mark -#pragma mark <#title>

    Set up as a text macro in Xcode. I type pm, the autocomplete suggestion pops up and I hit the tab key; the completion fills in with <#title> already selected so that I can type over it. Admittedly Xcode is not as customizable as TextMate but you can still do a fair bit with text macros. Google for “Xcode text macros” or just go here (Apple docs) for a start.

  12. Colin Barrett Says:

    Yeah, #pragma mark is the shizz. I’m sad I don’t get to use it at work, developing cross platform code.

    Since we’re mentioning TextMate, My main annoyance with it was that there’s no way to open all windows in tabs. You have to make a project. I don’t want my editor to do project management at work (we have a huge make infrastructure set up, it’s worthless to try and edit it in a GUI).

  13. Mark Grimes Says:

    Colin,

    You don’t have to make a project to manage all your files, just drag the directory that houses your xcodeproj, source and resources onto the TextMate icon. It will open a new window that has all the relevant stuff in the window’s drawer (⌃⌥⌘D is of course easy access to that drawer)– as you click on each source it gets a tab in a single window. Likewise you can do things like click on the nib in TM and it will open IB or click on the model and it’ll open the respective window in Xcode. Excellent stuff.

    I do prefer doing project mmgt in TextMate however because once I’ve finished my build configuration, model (if a CD app) and occasionally adding target dependencies, I spend all of my time in source or wiring my interface. Xcode is the setup once and forget and revisit occasionally. I mean once your project is layed out, it’s more convenient to ⌘B through TextMate — and for continuous integration there’s the wonderful BuildFactory.

  14. Stephen McConnell Says:

    Neither this thread or any of the comments or the the video tell me “WHY I SHOULD USE #PRAGMA mark”.

    What is #pragma mark, why is it used, what are the pros and cons in its use?

    Stephen McConnell

  15. Peter Hosey Says:

    Stephen McConnell: I did assume, in my title, that you already know what #pragma mark is. Sorry about that.

    It’s a compiler directive that the compiler ignores. It allows you to mark off sections within a single file.

    The advantage of #pragma mark over a giant section-header comment, besides being smaller, is that most Mac editors (CodeWarrior, Xcode, BBEdit, etc.) display marks in their function pop-up. This makes it easy to move from one section to another just by hitting the pop-up.

Leave a Reply

Do not delete the second sentence.


Warning: Undefined array key "ntt_saved_comment_text" in /home/public/blog/wp-content/plugins/negative-turing-test/negative-turing-test.php on line 143