Changeset 129
- Timestamp:
- Mar 2, 2004, 10:45:16 AM (22 years ago)
- File:
-
- 1 edited
-
trunk/doc/misc/codeConventions.tex (modified) (73 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/doc/misc/codeConventions.tex
r128 r129 1 1 \documentclass[panstarrs]{panstarrs} 2 \newcommand\note[1]{{\bf #1}}3 \newcommand\tbd[1]{{\bf [#1]}}4 2 5 3 \title{\PS{} Code Conventions} … … 19 17 20 18 \newcommand{\matchBracket}[1]{} % help emacs match properly 21 \newcommand{\XXX} {{\color{red} XXX}}19 \newcommand{\XXX}[1]{{\color{red} XXX #1\hfil\break}} 22 20 23 21 \pagenumbering{roman} \thispagestyle{empty} \maketitle 24 22 25 23 \pagebreak \pagestyle{fancy} 26 { \Large \bfRevision History}24 \textbf{\Large Revision History} 27 25 \begin{center} 28 26 \begin{tabular}{|p{1in}|p{1in}|p{4.5in}|} 29 27 \hline 30 {\bf Revision Number} & {\bf Release Date} & {\bfDescription} \\28 \textbf{Revision Number} & \textbf{Release Date} & \textbf{Description} \\ 31 29 \hline 32 %0.1 & 2003/12/08& First draft \\30 0.1 & 2004/03/01 & First draft \\ 33 31 \hline 34 32 \end{tabular} 35 33 \end{center} 36 34 37 { \Large \bfReferenced Documents}35 \textbf{\Large Referenced Documents} 38 36 \begin{center} 39 37 \begin{tabular}{|p{1in}|p{2.75in}|p{2.75in}|} 40 38 \hline 41 {\bf \PS{} ID} & {\bf Title} & {\bfAuthor} \\39 \textbf{\PS{} ID} & \textbf{Title} & \textbf{Author} \\ 42 40 \hline 43 41 %%% References here. … … 76 74 77 75 78 \label{L16729}79 76 \subsection{Acknowledgments} 80 77 … … 96 93 copyright notice (Copyright.doc.html). 97 94 98 \subsection{ \label{L16730}To whom should I Complain?}95 \subsection{To whom should I Complain?} 99 96 100 97 While we're working on this document complain to 101 98 \code{rhl@astro.princeton.edu}. 102 99 103 104 100 %------------------------------------------------------------------------------ 105 101 106 \label{L16732}107 102 \section{File Names} 108 103 109 \emph{\XXX{} I don't discuss doxygen comments here.}110 111 104 This section lists commonly used file suffixes and names. 112 \label{L647} 105 113 106 \subsection{File Suffixes} 114 107 … … 125 118 \end{center} 126 119 127 \label{L253}128 120 \subsection{Common File Names} 129 121 … … 133 125 \textbf{File Name} & \textbf{Use} \\ 134 126 \file{Makefile} & 135 The preferred name for makefiles. We use \code{gnumake} to build our 136 software. \emph{Do we? \XXX{}} \\ 127 The preferred name for makefiles.\\ 137 128 138 129 \file{README} & … … 140 131 particular directory. \\ 141 132 142 \ code{TAGS} &133 \file{TAGS} & 143 134 The index file (built by etags) used by emacs to follow cross references. \\ 144 135 \end{tabular} … … 148 139 %------------------------------------------------------------------------------ 149 140 150 \label{L3043}151 141 \section{File Organization} 152 142 … … 157 147 158 148 For an example of a properly formatted program, see 159 Source File Example (section \ref{L182}). 160 161 \label{L11684} 149 Source File Example (sec. \ref{SourceExample}). 150 162 151 \subsection{Source Files} 163 152 164 \label{Lfile_h}165 153 \subsubsection{Include Files} 166 154 167 \emph{\XXX{} I added this}\hfil\break168 155 Header files should have names starting \code{ps} or 169 156 \code{p_ps} for private interface definitions. The latter … … 189 176 \item 190 177 Any typedefs. 191 \XXX{} describe use of typedefs.192 178 193 179 \item … … 205 191 206 192 typedef struct { 207 int nthing; /* number of subthings */208 int sthing[PS_NMAX]; /* the subthings */193 int nthing; /* number of subthings */ 194 int sthing[PS_NMAX]; /* the subthings */ 209 195 } psThing; 210 196 … … 215 201 \end{verbatim} 216 202 217 \ label{Lfile_c}\subsubsection{C Source Files}203 \subsubsection{C Source Files} 218 204 219 205 C Source files have the following ordering: … … 238 224 \end{itemize} 239 225 240 \ label{Lfile_i}\subsubsection{SWIG Interface Files}241 242 \XXX{ } Write me226 \subsubsection{SWIG Interface Files} 227 228 \XXX{Write me} 243 229 244 230 %------------------------------------------------------------------------------ 245 231 246 \label{L262}247 232 \section{Indentation} 248 233 249 Twospaces should be used as the unit of indentation. The exact234 Four spaces should be used as the unit of indentation. The exact 250 235 construction of the indentation (spaces vs. tabs) is unspecified. Tabs 251 must be set exactly every 8 spaces (not 2). 252 253 \label{L313} 236 must be set exactly every 8 spaces (not 4). 237 254 238 \subsection{Line Length} 255 239 … … 258 242 Note: Examples for use in documentation should have a shorter line length-generally no more than 70 characters. 259 243 260 \label{L248}261 244 \subsection{Wrapping Lines} 262 245 … … 265 248 266 249 \begin{itemize} 267 \item Break after a comma. 268 269 \item Break before an operator. 270 271 \emph{ \XXX{} RHL prefers to break \textit{after} an operator}, e.g. 250 \item Break after a comma or operator. 251 272 252 \begin{verbatim} 273 253 if(a < b || 274 254 a > 2*b) { 275 x = (a + b + c) +255 x = (a + b + c) + 276 256 sin(z); 277 257 } 278 258 \end{verbatim} 279 not280 \begin{verbatim}281 if(a < b282 || a > 2*b) {283 x = (a + b + c)284 + sin(z);285 }286 \end{verbatim}287 as the former tells the reader more directly that it's a partial288 statement. Do we want to modify these rules?289 259 290 260 \item Prefer higher-level breaks to lower-level breaks. … … 296 266 be more formal about what this rule means). 297 267 298 \item If the above rules lead to confusing code or to code that's squished up against the right margin, just indent 8 spaces instead. 299 \hfil\break\emph{\XXX{} emacs actually just uses a regular 2 space indent; 300 should we do the same? } 268 \item If the above rules lead to confusing code or to code that's 269 squished up against the right margin, just indent 8 spaces instead. 270 This should be \emph{very} rare! Consider whether you should be 271 e.g. factoring code into a function. 301 272 \end{itemize} 302 273 … … 312 283 \end{verbatim} 313 284 314 Following are two examples of breaking an arithmetic expression. The first is preferred, since the break occurs outside the parenthesized expression, which is at a higher level. 315 316 \begin{verbatim} 317 longName1 = longName2 * (longName3 + longName4 - longName5) 318 + 4 * longname6; // PREFER 319 320 longName1 = longName2 * (longName3 + longName4 321 - longName5) + 4 * longname6; // AVOID 322 \end{verbatim} 323 324 Following are two examples of indenting function declarations. The first is the conventional case. The second would shift the second and third lines to the far right if it used conventional indentation, so instead it indents only 8 spaces. 285 Following are two examples of breaking an arithmetic expression. The 286 first is preferred, since the break occurs outside the parenthesized 287 expression, which is at a higher level. 288 289 \begin{verbatim} 290 longName1 = longName2*(longName3 + longName4 - longName5) + 291 4*longname6; // PREFER 292 293 longName1 = longName2*(longName3 + longName4 - 294 longName5) + 4*longname6; // AVOID 295 \end{verbatim} 296 297 Following are two examples of indenting function declarations. The 298 first is the conventional case. The second would shift the second and 299 third lines to the far right if it used conventional indentation, so 300 instead it indents only 4 spaces (note that putting the \code{anArg} 301 argument on a line of its own makes this the `natural' emacs indentation). 302 325 303 \begin{verbatim} 326 304 //CONVENTIONAL INDENTATION … … 331 309 } 332 310 333 //INDENT 8 SPACES TO AVOID VERY DEEP INDENTS 334 static type honkingLongFunctionName(int anArg, 335 Object anotherArg, String yetAnotherArg, 336 Object andStillAnother) 311 //INDENT 4 SPACES TO AVOID VERY DEEP INDENTS 312 static type honkingLongFunctionName( 313 int anArg, 314 Object anotherArg, String yetAnotherArg, 315 Object andStillAnother) 337 316 { 338 317 ... … … 340 319 \end{verbatim} 341 320 342 Line wrapping for \code{if} statements should generally use the343 8-space rule, since conventional (2 space) indentation makes seeing344 the body difficult. For example:345 \begin{verbatim}346 //DON'T USE THIS INDENTATION347 if ((condition1 && condition2)348 || (condition3 && condition4)349 ||!(condition5 && condition6)) { //BAD WRAPS350 doSomethingAboutIt(); //MAKE THIS LINE EASY TO MISS351 }352 353 //USE THIS INDENTATION INSTEAD354 if ((condition1 && condition2)355 || (condition3 && condition4)356 ||!(condition5 && condition6)) {357 doSomethingAboutIt();358 }359 360 //OR USE THIS361 if ((condition1 && condition2) || (condition3 && condition4)362 ||!(condition5 && condition6)) {363 doSomethingAboutIt();364 }365 \end{verbatim}366 321 Here are three acceptable ways to format ternary expressions: 367 322 \begin{verbatim} … … 378 333 %------------------------------------------------------------------------------ 379 334 380 \label{L385}381 335 \section{Comments} 382 336 … … 387 341 \begin{verbatim} 388 342 #if 0 389 code that is not wanted just at present343 code that is not wanted just at present 390 344 #endif 391 345 \end{verbatim} … … 397 351 #define IGNORE_RHL 1 398 352 #if !IGNORE_RHL 399 ...353 ... 400 354 #endif 401 355 ... 402 356 #if !IGNORE_RHL 403 ...357 ... 404 358 #endif 405 359 \end{verbatim} 406 360 This type of \code{#define} may appear within the main body of the file. 407 361 \end{itemize} 408 409 \emph{\XXX{} RHL added this. He stands by it. }410 362 411 363 Comments should be used to give overviews of code and provide … … 422 374 date as the code evolves. 423 375 424 Note: The frequency of comments sometimes reflects poor quality of376 Note: The frequency of comments sometimes reflects poor quality of 425 377 code. When you feel compelled to add a comment, consider rewriting the 426 378 code to make it clearer. … … 432 384 backspace. 433 385 434 \label{L216}435 386 \subsection{Implementation Comment Formats} 436 387 437 Programs can have four styles of implementation comments: block, single-line, trailing, and end-of-line. 438 \label{L680} 388 Programs can have four styles of implementation comments: block, 389 single-line, trailing, and end-of-line. 390 439 391 \subsubsection{Block Comments } 392 \label{ImplBlockComments} 440 393 441 394 Block comments are used to provide descriptions of files, functions, … … 446 399 describe. 447 400 448 \emph{\XXX{} I don't see any reason for this blank line}\hfil\break 449 A block comment should be preceded by a blank line to set it apart from the rest of the code. 450 \begin{verbatim} 451 /* 452 * Here is a block comment. 453 */ 454 \end{verbatim} 455 456 See also Documentation Comments (section \ref{L16838}). 457 458 \label{L341} 401 See also Documentation Comments (sec. \ref{DocComments}). 402 459 403 \subsubsection{Single-Line Comments} 404 \label{SingleLineComment} 460 405 461 406 Short comments can appear on a single line indented to the level of 462 407 the code that follows. If a comment can't be written in a single line, 463 it should follow the \ref{L680}block comment format. A 464 single-line comment should be preceded by a blank line. 465 \emph{\XXX{} I don't see any reason for this blank line}\hfil\break 466 Here's an 467 example of a single-line comment in C code (also see \ref{L16838}''Documentation Comments''): 468 469 \begin{verbatim} 470 if (condition) { 471 472 /* Handle the condition. */ 473 ... 474 } 475 \end{verbatim} 476 477 \label{L342} 408 it should follow the \ref{ImplBlockComments}block comment format. 409 478 410 \subsubsection{Trailing Comments} 479 411 … … 487 419 \begin{verbatim} 488 420 if (a == 2) { 489 return TRUE; /* special case */421 return TRUE; /* special case */ 490 422 } else { 491 return isPrime(a); /* works only for odd a */ 492 } 493 \end{verbatim} 494 495 \label{L286} 423 return isPrime(a); /* works only for odd a */ 424 } 425 \end{verbatim} 426 496 427 \subsubsection{End-Of-Line Comments} 497 428 … … 502 433 503 434 \begin{verbatim} 504 if (foo >1) {505 506 // Do a double-flip.507 ...435 if (foo > 1) { 436 437 // Do a double-flip. 438 ... 508 439 } else { 509 return false; // Explain why here. 510 } 511 \end{verbatim} 512 513 \label{L16838} 440 return false; // Explain why here. 441 } 442 \end{verbatim} 443 514 444 \subsection{Documentation Comments} 515 516 \emph{\XXX{} Need references to Doxygen here.} 445 \label{DocComments} 446 447 \XXX{Need references to Doxygen here.} 517 448 518 449 If you need to give information about a type, interface, or variable 519 450 that isn't appropriate for doxygenation, use an implementation block 520 comment (sec. \ref{ L680}) or single-line comment (sec. \ref{L341})451 comment (sec. \ref{ImplBlockComments}) or single-line comment (sec. \ref{SingleLineComment}) 521 452 immediately \textit{before} the code. For example, details about the 522 453 implementation of a type should go in in such an implementation block … … 526 457 %------------------------------------------------------------------------------ 527 458 528 \label{L2991}529 459 \section{Declarations} 530 460 531 \emph{\XXX{} I added this}\hfil\break 532 \textit{All globally-visible symbols must be declared in 533 a suitable header (.h) file.}. Global private symbols (i.e. 534 those named \code{p_ps}) should not appear in public header 535 files, but rather in files whose names begin \code{p_ps}. 536 537 \label{L2992} 461 All globally-visible symbols must be declared in a suitable header 462 (.h) file. Global private symbols (i.e. those named \code{p_ps}) 463 should not appear in public header files, but rather in files whose 464 names begin \code{p_ps}. 465 538 466 \subsection{Number Per Line} 539 467 540 One declaration per line is recommended since it encourages commenting. In other words, 468 One declaration per line is recommended since it encourages 469 commenting. In other words, 541 470 \begin{verbatim} 542 471 int level; // indentation level … … 549 478 Do not put different types on the same line. Example: 550 479 \begin{verbatim} 551 int foo, fooarray[]; // WRONG!480 int foo, fooarray[]; // WRONG! 552 481 \end{verbatim} 553 482 … … 561 490 (Note that the comments are indented to column 40). 562 491 563 \label{L18761}564 492 \subsection{Initialization} 565 493 566 Try to initialize local variables where they're declared. 567 \hfil\break\emph{\XXX{} the following is invalidated by the rewrite of section 6.3} 568 The only 494 Try to initialize local variables where they're declared. The only 569 495 reason not to initialize a variable where it's declared is if the 570 496 initial value depends on some computation occurring first. 571 497 572 \hfil\break\emph{\XXX{} added by RHL}573 498 In some cases it may be necessary to initialize a variable to suppress 574 499 a compiler warning; in this case a comment should explain the circumstances. 575 500 576 \label{L16817}577 501 \subsection{Placement} 578 502 579 \hfil\break\emph{\XXX{} I've changed this} 580 581 Variables should ordinarily be declared as soon as they 582 appear in the logical flow of control through a function. This 583 allows them to be initialised as they are created, and naturally 503 Variables should ordinarily be declared at the top of the block in 504 which they appear, unless there is some reason to declare them later. 505 This allows them to be initialised as they are created, and naturally 584 506 associates their declaration with their use. 585 507 586 In particular, indexes of \code{for} loops should usually 508 For example, declarations are naturally mixed with assertions: 509 \begin{verbatim} 510 void function(REGION *reg) // image of the sky 511 { 512 assert (reg != NULL); 513 const int nrow = reg->nrow; 514 assert (reg->type == psType_U16); 515 const psTypeU16 *rows = reg->rows; 516 ... 517 } 518 \end{verbatim} 519 520 Indexes of \code{for} loops should usually 587 521 be declared in the \code{for} statement (the declarations appear 588 522 in the scope of the braces): 589 523 \begin{verbatim} 590 for (int i = 0; i < maxLoops; i++) { 591 ... 592 } 593 \end{verbatim} 594 595 \hfil\break\emph{\XXX{} OLD VERSION} 596 597 Put declarations at the beginning of blocks. (A block is any code 598 surrounded by curly braces \CODE.{. and \CODE.}..) Don't wait to declare 599 variables until their first use; it can confuse the unwary programmer 600 and hamper code portability within the scope. 601 602 \begin{verbatim} 603 void myFunction(void) 604 { 605 int int1 = 0; // beginning of function block 606 607 if (condition) { 608 int int2 = 0; // beginning of "if" block 524 for (int i = 0; i < maxLoops; i++) { 609 525 ... 610 } 611 } 612 \end{verbatim} 613 614 One exception to the rule is indexes of \code{for} loops, which 615 in C99 can be declared in the \code{for} statement (and which appear 616 in the scope of the braces): 617 \begin{verbatim} 618 for (int i = 0; i < maxLoops; i++) { 619 ... 620 } 621 \end{verbatim} 622 623 p> 624 625 \emph{\XXX{} Added by RHL: }\hfil\break 626 Another exception is that assertions may preceed or be mixed with declarations: 627 \begin{verbatim} 628 void function(REGION *reg) // image of the sky 629 { 630 assert (reg != NULL); 631 const int nrow = reg->nrow; 632 assert (reg->type == psType_U16); 633 const psTypeU16 *rows = reg->rows; 634 ... 635 } 636 \end{verbatim} 637 Note that this allows us to declare variables that have been unpacked 638 to e.g. allow dealiasing to be declared \code{const}. 639 640 \hfil\break\emph{\XXX{} End old version } 526 } 527 \end{verbatim} 641 528 642 529 Avoid local declarations that hide declarations at higher levels. For … … 647 534 void myFunction(void) 648 535 { 649 if (condition) {536 if (condition) { 650 537 int count = 0; // AVOID! 651 538 ... 652 } 653 ... 654 } 655 \end{verbatim} 656 657 \label{L381} 539 } 540 ... 541 } 542 \end{verbatim} 543 658 544 \subsection{Function Declarations} 659 545 … … 664 550 \item 665 551 Function declarations should be preceeded by a short comment 666 describing what the function does. \hfil\break\emph{\XXX{} Doxygen? } 552 describing what the function does. 553 554 \XXX{Discuss Doxygen} 667 555 668 556 \item The function's type should appear on the same line as the 669 557 function declaration. 670 \hfil\break\emph{\XXX{} I don't like this (I put the type671 on the previous line), but MHPCC seems to use it}672 558 673 559 \item All arguments should be declared with their types; i.e. no … … 686 572 void function(void) 687 573 { 688 ...574 ... 689 575 } 690 576 \end{verbatim} … … 699 585 ) 700 586 { 701 struct {702 int x, y;703 } work[10];704 int ivar;705 706 while(isspace(*descrip++)) {} // skip white space707 descrip--; // one too far. Not great code...708 709 ...587 struct { 588 int x, y; 589 } work[10]; 590 int ivar; 591 592 while(isspace(*descrip++)) {} // skip white space 593 descrip--; // one too far. Not great code... 594 595 ... 710 596 } 711 597 \end{verbatim} … … 714 600 %------------------------------------------------------------------------------ 715 601 716 \label{L430}717 602 \section{Statements} 718 \label{L431}719 603 \subsection{Simple Statements} 720 604 721 Each line should contain at most one statement. Example: 722 \begin{verbatim} 723 argv++; // Correct 724 argc--; // Correct 725 argv++; argc--; // AVOID! 726 \end{verbatim} 727 728 \label{L15395} 605 Each line should usually contain only one statement. Example: 606 \begin{verbatim} 607 x = sqrt(x2); // Correct 608 i++; // Correct 609 x = sqrt(x2); i++; // Avoid! 610 \end{verbatim} 611 612 An example of a reasonably two-statement line is: 613 \begin{verbatim} 614 argv++; argc--; 615 \end{verbatim} 616 where the two actions are intimately related. 617 729 618 \subsection{Compound Statements} 730 619 … … 746 635 \end{itemize} 747 636 748 \label{L438}749 637 \subsection{return Statements} 750 638 … … 759 647 \end{verbatim} 760 648 761 \label{L449}762 649 \subsection{if, if-else, if else-if else Statements} 763 650 … … 765 652 \begin{verbatim} 766 653 if (condition) { 767 statements;654 statements; 768 655 } 769 656 770 657 if (condition) { 771 statements;658 statements; 772 659 } else { 773 statements;660 statements; 774 661 } 775 662 776 663 if (condition) { 777 statements;664 statements; 778 665 } else if (condition) { 779 statements;666 statements; 780 667 } else { 781 statements; 782 } 783 784 \end{verbatim} 785 786 Note: \code{if} statements always use braces {}. Avoid the following error-prone form: 787 \begin{verbatim} 788 if (condition) //AVOID! THIS OMITS THE BRACES {}! 668 statements; 669 } 670 671 \end{verbatim} 672 673 Note: \code{if} statements always use braces {}. Avoid the following 674 error-prone form: 675 \begin{verbatim} 676 if (condition) // AVOID! THIS OMITS THE BRACES {}! 789 677 statement; 790 678 \end{verbatim} 791 679 792 \label{L454}793 680 \subsection{for Statements} 794 681 … … 796 683 \begin{verbatim} 797 684 for (initialization; condition; update) { 798 statements;685 statements; 799 686 } 800 687 \end{verbatim} 801 688 802 689 An empty \code{for} statement (one in which all the work is done 803 in the initialization, condition, and update clauses) should have the804 following form:690 in the initialization, condition, and update clauses) should have one of 691 the following forms: 805 692 \begin{verbatim} 806 693 for (initialization; condition; update); 694 for (initialization; condition; update) {} 807 695 \end{verbatim} 808 696 … … 813 701 the loop (for the update clause). 814 702 815 \label{L460}816 703 \subsection{while Statements} 817 704 … … 819 706 \begin{verbatim} 820 707 while (condition) { 821 statements;822 } 823 \end{verbatim} 824 An empty \code{while} statement should have the following form:708 statements; 709 } 710 \end{verbatim} 711 An empty \code{while} statement should have one of the following forms: 825 712 \begin{verbatim} 826 713 while (condition); 827 \end{verbatim}828 829 \label{L465} 714 while (condition) {} 715 \end{verbatim} 716 830 717 \subsection{do-while Statements} 831 718 … … 833 720 \begin{verbatim} 834 721 do { 835 statements;722 statements; 836 723 } while (condition); 837 724 \end{verbatim} 838 725 839 840 \emph{\XXX{} RHL would prefer that we mandate one of the forms}:841 \begin{verbatim}842 for (initialization; condition; update) {}843 for (initialization; condition; update) continue;844 \end{verbatim}845 Ditto for \code{while} statements. The closing ; is subtle,846 especially bearing in mind:847 \begin{verbatim}848 do {849 ....850 while (condition);851 \end{verbatim}852 where the while really does have a closing semicolon.853 854 \label{L468}855 726 \subsection{switch Statements} 856 727 857 A \code{switch} statement should have the following form: 728 A \code{switch} statement should have the following form (note the 729 indentation; the \code{case/default} is indented 2 spaces, the 730 other statements are at the usual 4 space indent): 858 731 \begin{verbatim} 859 732 switch (condition) { 860 case ABC: 861 statements; 862 /* falls through */ 863 864 case DEF: 865 case GHI: 866 statements; 867 break; 868 869 case XYZ: 870 statements; 871 break; 872 873 default: 874 statements; 875 break; 876 } 877 \end{verbatim} 878 879 \emph{\XXX{} I'd prefer to indent the ``case'' 1 more spaces RHL}: 880 \begin{verbatim} 881 switch (condition) { 882 case ABC: 883 statements; 884 /* falls through */ 885 886 case DEF: 887 case GHI: 888 statements; 889 break; 890 891 case XYZ: 892 statements; 893 break; 894 895 default: 896 statements; 897 break; 733 case ABC: 734 statements; 735 /* falls through */ 736 737 case DEF: 738 case GHI: 739 statements; 740 break; 741 742 case XYZ: 743 statements; 744 break; 745 746 default: 747 statements; 748 break; 898 749 } 899 750 \end{verbatim} … … 902 753 statement), add a comment where the \code{break} statement would 903 754 normally be. This is shown in the preceding code example with the 904 \code{/* falls through */} comment. A comment is not 905 required (or expected) when the fall-through is between multiple case 906 statements. 755 \code{/* falls through */} comment. A comment is not required (or 756 expected) when the fall-through is between multiple case statements. 907 757 908 758 Every \code{switch} statement should include a default case, … … 911 761 \code{case} is later (and illegally) added after the default clause. 912 762 913 \emph{ \XXX{} Added by RHL}\hfil\break914 763 When switching on an enumerated type, if all the elements of the 915 764 type are included in the switch a default clause should still be … … 920 769 %------------------------------------------------------------------------------ 921 770 922 \label{L475}923 771 \section{White Space} 924 \label{L487}925 772 \subsection{Blank Lines} 926 773 … … 939 786 \item Between the local variables in a function and its first statement 940 787 941 \item Before a \ref{L680}block or \ref{L341}single-line comment 788 \item Before a block (sec. \ref{ImplBlockComments}) or 789 single-line (sec. \ref{SingleLineComment}) comment 942 790 943 791 \item Between logical sections inside a function to improve readability 944 792 \end{itemize} 945 793 946 \label{L682}947 794 \subsection{Blank Spaces} 948 795 … … 953 800 \begin{verbatim} 954 801 while (true) { 955 ...802 ... 956 803 } 957 804 \end{verbatim} … … 963 810 \item A blank space should appear after commas in argument lists. 964 811 965 \item All binary operators except \code{.} and \code{->} 966 should be separated from their operands by spaces. Blank spaces should 967 never separate unary operators such as unary minus, increment (\code{++}), 968 and decrement (\code{--}) from their operands. Example: 969 \begin{verbatim} 970 a += c + d; 971 a = (a + b) / (c * d); 812 \item 813 Low priority binary operators (\code{+} and \code{-} and below) 814 should be separated from their operands by spaces; higher 815 precedence operators (e.g. \code{*}, \code{/}, \code{%}, 816 \code{.} and \code{->}) should not be separated by spaces. 817 Blank spaces should never separate 818 unary operators such as a type cast, unary minus, increment 819 (\code{++}), and decrement (\code{--}) from their operands. Examples: 820 \begin{verbatim} 821 a += c + d; 822 a = (a + b)/(c*d); 972 823 973 while (*d++ = *s++) { // Tricky way of copying to '\0' 974 n++; 975 } 976 printf("size is %d\n", foo); 977 \end{verbatim} 978 \hfil\break\emph{ \XXX{} I had to fix the code to make it even make sense, but it's 979 still lousy code (I admit that you'll see it in K\&R, but it's a very 980 1970s style)} 981 \hfil\break\emph{\XXX{} I don't like this either. I'd prefer to see multiplicative 982 operators (i.e. * / \%) without whitespace. E.g.} 983 \begin{verbatim} 984 a%x + y; 985 a*(x + y); 986 (a + b)/(x + y); 824 while (*d++ = *s++) { // Tricky way of copying until '\0' 825 n++; 826 } 827 printf("size is %d\n", foo); 987 828 \end{verbatim} 988 829 … … 993 834 for (expr1; expr2; expr3) 994 835 \end{verbatim} 995 \item Casts should be followed by a blank space. Examples:996 \begin{verbatim}997 myFunc((byte) aNum, (Object) x);998 myFunc((int) (cp + 5), ((int) (i + 3))999 + 1);1000 \end{verbatim}1001 1002 \hfil\break\emph{\XXX{} I really don't like this one. A cast is a unary operator,1003 and binds harder than e.g. ++. }1004 836 1005 837 \item 1006 \emph{\XXX{} I added this }\hfil\break1007 838 The word \code{assert} should be treated as a keyword, and 1008 839 separated by a space from its logical expression. … … 1011 842 %------------------------------------------------------------------------------ 1012 843 1013 \label{L367}1014 844 \section{Naming Conventions} 1015 845 … … 1044 874 1045 875 The names of all externally visible functions (i.e. all those that are 1046 not declared \code{static}) should be verb s, in mixed case. The1047 first two characters should be \code{ps}, with the first letter 1048 of each internal word capitalized.876 not declared \code{static}) should be verbal phrases, in mixed 877 case. The first two characters should be \code{ps}, with the first 878 letter of each internal word capitalized. 1049 879 1050 880 Functions that are visible at global scope but are not considered part … … 1067 897 1068 898 Except for variables, all instance, class, and class constants are in 1069 mixed case with a lowercase first letter. Internal words start with 1070 capital letters. Variable names should not start with underscore \code{_} 1071 as these names are, under some circumstances, reserved by the 1072 C standard. 899 mixed case with 900 901 Variable names should not start with underscore \code{_} as these 902 names are, under some circumstances, reserved by the C 903 standard. Non-globally visible words should start with a lowercase 904 first letter; Internal words start with capital letters. 1073 905 1074 906 Variables visible at global scope should begin with \code{ps}, 1075 907 or \code{p_ps} if not considered part of the public interface. 1076 908 Variables that are local to a file should \textit{not} start 1077 with \code{ps} (or \code{p_ps}). 909 with \code{ps} (or \code{p_ps}). These variables should be Capitalised 910 at the beginning of each word. 1078 911 1079 912 Variable names should be short yet meaningful. The choice of a … … 1097 930 & 1098 931 1099 \emph{\XXX{} const is different in C from Java (and C++) so I've rewritten 1100 this.}\hfil\break 1101 1102 Constants used to e.g. dimension arrays should be set using 1103 the \code{#define}; \code{const} variables should not be used, especially 932 Constants used to e.g. dimension arrays should be set using the 933 \code{#define}; \code{const} variables should not be used, especially 1104 934 in header files. Symbolic values should usually be declared using 1105 enumerated types (although this requires more care with namespaces).935 enumerated types. 1106 936 1107 937 The names of all enumerated types and C-preprocessor symbols (but not … … 1120 950 %------------------------------------------------------------------------------ 1121 951 1122 \label{L529}1123 952 \section{Programming Practices} 1124 953 1125 \label{L177}1126 954 \subsection{When to Make Symbols Global} 1127 955 … … 1130 958 with its usage to make auto-variables within a function persistent). 1131 959 1132 \label{L1255}1133 960 \subsection{Constants} 1134 961 … … 1137 964 in a \code{for} loop as counter values. 1138 965 1139 \emph{ \XXX{} RHL relaxed this a little}1140 1141 \label{L547}1142 966 \subsection{Variable Assignments} 1143 967 … … 1149 973 row0 = col0 = 0; // OK 1150 974 \end{verbatim} 1151 \emph{\XXX{} Relaxed by RHL}1152 975 1153 976 Do not use the assignment operator in a place where it can be easily confused with the equality operator. Example: 1154 977 \begin{verbatim} 1155 978 if (c++ = d++) { // AVOID! 1156 ...979 ... 1157 980 } 1158 981 \end{verbatim} … … 1160 983 \begin{verbatim} 1161 984 if ((c++ = d++) != 0) { 1162 ... 1163 } 1164 \end{verbatim} 1165 If \code{c} and \code{d} are char variables, use \code{'\0'} 1166 instead of \code{0}. 1167 \hfil\break\emph{\XXX{} \code{'$\backslash$0'} added by RHL} 1168 1169 Do not use embedded assignments in an attempt to improve run-time performance. This is the job of the compiler. Example: 985 ... 986 } 987 \end{verbatim} 988 989 Do not use embedded assignments in an attempt to improve run-time 990 performance. This is the job of the compiler. Example: 1170 991 \begin{verbatim} 1171 992 d = (a = b + c) + r; // AVOID! … … 1178 999 \end{verbatim} 1179 1000 1180 \label{L554}1181 1001 \subsection{Miscellaneous Practices} 1182 \label{L331}1183 1002 \subsubsection{Parentheses} 1184 1003 … … 1193 1012 if ((a == b) && (c == d)) // RIGHT 1194 1013 \end{verbatim} 1195 \hfil\break\emph{\XXX{} I don't agree with this; you end up with1196 too many parens. Next section is added by RHL}1197 1014 1198 1015 There are some famous problems with precedence in C. In particular, 1199 1016 expressions involving the combinations of \code{||} and 1200 \code{ \&\&} should be parenthesisedas should all1017 \code{&&} should be fully parenthesised, as should all 1201 1018 expressions containing bitwise operators: 1202 1019 \begin{verbatim} 1203 if ((a == b \&\& c == d) || e == f)1020 if ((a == b && c == d) || e == f) 1204 1021 l << (j + k) 1205 1022 (l << j) + k … … 1207 1024 \end{verbatim} 1208 1025 1209 \label{L333}1210 1026 \subsubsection{Returning Values} 1211 1027 … … 1213 1029 \begin{verbatim} 1214 1030 if (booleanExpression) { 1215 return true;1031 return true; 1216 1032 } else { 1217 return false;1033 return false; 1218 1034 } 1219 1035 \end{verbatim} … … 1231 1047 \begin{verbatim} 1232 1048 if (condition) { 1233 return x;1049 return x; 1234 1050 } 1235 1051 return y; … … 1240 1056 \end{verbatim} 1241 1057 1242 \label{L353}1243 1058 \subsubsection{Expressions before `?' in the Conditional Operator } 1244 1059 1245 1060 If an expression containing a binary operator appears before the \code{?} in the ternary \code{?: }operator, it should be parenthesized. Example: 1246 1061 \begin{verbatim} 1247 (x >= 0) ? x : -x; 1248 \end{verbatim} 1249 1250 \label{L396} 1062 (x >= 0) ? x : -x; 1063 \end{verbatim} 1064 1251 1065 \subsubsection{Functions that Take No Parameters} 1252 1253 \emph{\XXX{} added by RHL}1254 1066 1255 1067 When a function takes no arguments, it should be explicitly declared … … 1258 1070 static type myFunction(void) 1259 1071 { 1260 ... 1261 } 1262 \end{verbatim} 1263 1264 1265 \label{L395} 1072 ... 1073 } 1074 \end{verbatim} 1075 1076 1266 1077 \subsubsection{Special Comments} 1267 1078 \begin{itemize} 1268 1079 \item 1269 Use \code{ \XXX{}} in a comment to flag something that is bogus but works.1080 Use \code{XXX} in a comment to flag something that is bogus but works. 1270 1081 1271 1082 \item 1272 1083 Use \code{FIXME} to flag something that is bogus and broken. 1273 1084 1274 \ emph{\XXX{} next items added by RHL}1275 1085 \XXX{Check Doxygen versions of these special comments. I could not 1086 find any occurrence of \code{@notused@} in the Doxygen documentation} 1276 1087 \item 1277 1088 Use \code{NOTREACHED} to indicate a line of code that cannot be reached, … … 1279 1090 \begin{verbatim} 1280 1091 if (sqrt(x) < 0) { 1281 psAbort(); // never returns1282 exit(1); // NOTREACHED1092 psAbort(); // never returns 1093 exit(1); // NOTREACHED 1283 1094 } 1284 1095 \end{verbatim} … … 1296 1107 } 1297 1108 \end{verbatim} 1298 1299 \emph{\XXX{} I see some @notused@ comments in the MHPCC code. Is this1300 usage documented? Should we adopt it?}1301 1109 \end{itemize} 1302 1110 1303 1111 \subsubsection{\label{LNewDel}10.4.6 Constructors/Destructors} 1304 1305 \emph{\XXX{} Added by RHL }1306 1112 1307 1113 Type definitions should always be accompanied by prototypes for … … 1335 1141 \subsection{\label{LConvOps}Naming Conversion Operators} 1336 1142 1337 \emph{\XXX{} Added by RHL }1338 1339 1143 When defining a function to convert from one type to another, the 1340 1144 name should be of the form \code{psOldToNew}, e.g. 1341 \code{psEquatorialToEcliptic} .1145 \code{psEquatorialToEcliptic} (\emph{not} \code{psEquatorial2Ecliptic}). 1342 1146 1343 1147 \subsection{\label{LArgumentOrder}Order of I/O Arguments} 1344 1345 \emph{\XXX{} Added by RHL }1346 1148 1347 1149 Functions that assign to a variable should list that argument … … 1350 1152 \subsection{\label{LConstRestrict}Type Qualifiers (const and restrict)} 1351 1153 1352 \emph{\XXX{} Added by RHL }1353 1354 1154 All interfaces and type definitions should use \code{const} and 1355 1155 \code{restrict} wherever appropriate. For example, 1356 1156 \begin{verbatim} 1357 1157 typedef struct { 1358 int n;1359 float *restrict vec;1158 int n; 1159 float *restrict vec; 1360 1160 } psVec; 1361 1161 … … 1366 1166 so all code should be written to explicitly handle aliasing}. 1367 1167 1368 \emph{\XXX{} Sadly, it looks to me as if gcc 3.3 doesn't make1369 serious use of \code{restrict}. I believe that we should1370 still encourage its use.}1371 1372 1168 %------------------------------------------------------------------------------ 1373 1374 \label{L186} 1169 \appendix %Begin Appendices 1170 %------------------------------------------------------------------------------ 1171 1375 1172 \section{Code Examples} 1376 \label{L182}1377 1173 \subsection{Source File Example} 1174 \label{SourceExample} 1175 1176 \XXX{Need to include more of the rules, and shorten the example} 1177 Here's \file{psBuffer.h}: 1178 \begin{verbatim} 1179 #if !defined(PS_BUFFER_H) /* here's the insides of psBuffer.h */ 1180 #define PS_BUFFER_H 1181 #define PS_BUFSIZE 128 // Size for I/O buffers 1182 1183 typedef struct { 1184 char buf[PS_BUFSIZE]; // buffer 1185 int n; // number of bytes in buffer 1186 } psBuffer; 1187 1188 psBuffer *psBufferNew(void); 1189 void psBufferDel(psBuffer *buf, // buffer to delete 1190 int deep); // NOTUSED. Do a deep delete 1191 1192 void psBufferAppend(psBuffer *restrict buf, // psBuffer to append to 1193 const char *restrict str); // string to add 1194 1195 #endif 1196 \end{verbatim} 1197 1198 And here's the C source; \file{psUtils.h} provides \code{psAlloc/psFree}. 1378 1199 1379 1200 \begin{verbatim} 1380 1201 /* 1381 * This file implements an example of formatting a C1202 * This file implements an example of formatting a file of C code 1382 1203 * 1383 1204 * It isn't a very good piece of code … … 1389 1210 #include <time.h> 1390 1211 #include "psUtils.h" 1391 1392 #define BUFSIZE 128 // Size for I/O buffers 1393 1394 typedef struct { 1395 char buf[BUFSIZE]; // buffer 1396 int n; // number of bytes in buffer 1397 } Buffer; 1398 1399 typedef enum { NL, CRNL, CR } NLType; 1400 1401 static void addNewline(NLType type); // add a newline to the buffer 1402 1403 static Buffer *buf = NULL; // I/O buffer 1212 #include "psBuffer.h" 1213 1214 typedef enum { CRNL, NL } NLType; 1215 1216 static char *getNewline(NLType type); // return a suitable newline 1217 1218 static psBuffer *buf = NULL; // I/O buffer 1404 1219 1405 1220 /*****************************************************************************/ 1406 1221 /* 1407 * Create/destroy Buffers1222 * Create/destroy psBuffers 1408 1223 */ 1409 static Buffer *bufferNew(void)1224 psBuffer *psBufferNew(void) 1410 1225 { 1411 Buffer *buf = psAlloc(sizeof(Buffer));1412 buf->n = 0;1413 1414 return buf;1415 } 1416 1417 void bufferDel(Buffer *buf,// buffer to delete1418 int deep)// NOTUSED. Do a deep delete1226 psBuffer *buf = psAlloc(sizeof(psBuffer)); 1227 buf->n = 0; 1228 1229 return buf; 1230 } 1231 1232 void psBufferDel(psBuffer *buf, // buffer to delete 1233 int deep) // NOTUSED. Do a deep delete 1419 1234 { 1420 if (buf == NULL) {1421 return;1422 }1235 if (buf == NULL) { 1236 return; 1237 } 1423 1238 1424 if (buf->n > 0) {1425 (void)fputs(buf->buf, stdout);1426 }1239 if (buf->n > 0) { 1240 (void)fputs(buf->buf, stdout); 1241 } 1427 1242 1428 psFree(buf);1243 psFree(buf); 1429 1244 } 1430 1245 1431 1246 /*****************************************************************************/ 1432 1247 /* 1433 * Append to a string to a Buffer1248 * Append to a string to a psBuffer 1434 1249 */ 1435 static void bufferAppend(Buffer *restrict buf, //Buffer to append to1436 const char *restrict str) // string to add1250 void psBufferAppend(psBuffer *restrict buf, // psBuffer to append to 1251 const char *restrict str) // string to add 1437 1252 { 1438 assert(str != NULL);1439 const int len = strlen(str);1440 1441 if (buf->n + len >= BUFSIZE) {// XXX Handle this better1442 fprintf(stderr,"Sorry; too many bytes. Bye bye\n");1443 abort();1444 exit(1);// NOTREACHED1445 }1446 1447 (void)strcat(&buf->buf[buf->n], str);1448 buf->n += len;1253 assert(str != NULL); 1254 const int len = strlen(str); 1255 1256 if (buf->n + len >= PS_BUFSIZE) { // XXX Handle this better 1257 fprintf(stderr, "Sorry; too many bytes. Bye bye\n"); 1258 abort(); 1259 exit(1); // NOTREACHED 1260 } 1261 1262 (void)strcat(&buf->buf[buf->n], str); 1263 buf->n += len; 1449 1264 } 1450 1265 … … 1455 1270 int main(void) 1456 1271 { 1457 buf = bufferNew();1458 long t; // current time1459 1460 while(t = (long)time(NULL), t % 4 != 2) {}1272 buf = psBufferNew(); 1273 long t; // current time 1274 1275 while(t = (long)time(NULL), t%3 == 2) {} 1461 1276 1462 if (t % 4 == 0) { 1463 bufferAppend(buf, "Hello"); 1464 } else if (t % 4 == 1) { 1465 bufferAppend(buf, "Bonjour"); 1466 } else { 1467 bufferAppend(buf, "Aloha"); 1468 } 1469 bufferAppend(buf, " World!"); 1470 1471 addNewline(NL); 1472 1473 bufferDel(buf, 0); 1474 1475 return 0; 1277 if (t%3 == 0) { 1278 psBufferAppend(buf, "Hello"); 1279 } else if (t%3 == 1) { 1280 psBufferAppend(buf, "Aloha"); 1281 } 1282 psBufferAppend(buf, " World!"); 1283 1284 psBufferAppend(buf, getNewline(NL)); 1285 1286 psBufferDel(buf, 0); 1287 1288 return 0; 1476 1289 } 1477 1290 1478 1291 /*****************************************************************************/ 1479 1292 /* 1480 * Add a newline to the buffer1293 * Return a desired line terminator 1481 1294 */ 1482 static void addNewline(NLType type)// what sort of newline?1295 static char *getNewline(NLType type) // what sort of newline? 1483 1296 { 1484 char *newline; // newline to add 1485 switch (type) { 1486 case NL: 1487 newline = "\n"; 1488 break; 1489 1490 case CRNL: 1491 newline = "\r\n"; 1492 break; 1493 1494 case CR: 1495 newline = "\r"; 1496 break; 1497 1498 default: 1499 newline = "NL"; // FIXME 1500 break; 1501 } 1502 1503 bufferAppend(buf, newline); 1504 } 1505 \end{verbatim} 1297 char *newline; // newline to add 1298 switch (type) { 1299 case CRNL: 1300 newline = "\r\n"; 1301 break; 1302 1303 case NL: 1304 newline = "\n"; 1305 break; 1306 1307 default: 1308 newline = "NL"; // FIXME 1309 break; 1310 } 1311 1312 return newline; 1313 } 1314 \end{verbatim} 1315 1316 %------------------------------------------------------------------------------ 1317 1318 \section{Departures from Sun Java Coding Standards} 1319 1320 Apart from changes required by our use of C99 rather than Java, 1321 this document differs from the original Sun-Java standard in 1322 two sorts of ways; additions and changes. 1323 1324 \subsection{ Additions to the Sun-Java standards} 1325 1326 \begin{itemize} 1327 \item 1328 Naming convention for include files 1329 1330 \item 1331 Added naming conventions for constructors, destructors, and conversion 1332 functions. 1333 1334 \item 1335 Specified that the `output' argument should come \emph{first}. 1336 1337 \item 1338 Added conventional comments for unused arguments and unreachable 1339 statements (conforming to Doxygen's conventions). 1340 1341 \item 1342 Specified that functions taking no arguments should be explicitly 1343 specified as \code{(void)} (avoiding complaints on some compilers, 1344 e.g. on SGIs). 1345 1346 \end{itemize} 1347 1348 \subsection{Departures from the Sun-Java standards} 1349 1350 \begin{itemize} 1351 1352 \item 1353 We break lines \emph{after} (not before) an operator. 1354 1355 \item 1356 We don't specify any special indentation for continued logical 1357 expressions within an \code{if} clause. 1358 1359 \item 1360 Specify that \code{case/break} statements should be indented 1361 by half an indent (2 spaces) 1362 1363 \item 1364 Comments need not be preceeded by a blank line 1365 1366 \item 1367 The restriction on only declaring variables at the top of blocks (including 1368 \code{for} loops has been somewhat relaxed. 1369 1370 \item 1371 Relaxed the `only one statement per line' rule a little. 1372 1373 \item 1374 High-precedence binary operators (\code{*}, \code{/}, \code{%} and above) 1375 should \emph{not} be surrounded by whitespace. 1376 \end{itemize} 1506 1377 1507 1378 %------------------------------------------------------------------------------ … … 1513 1384 not part of the coding standards.} 1514 1385 1515 If you want to simply set these variables you can, see the code in 1516 the section on hooks (section \ref{Lhooks}) below. 1517 1518 \subsection{\label{Lusing customize}Using Customize to Control Formatting} 1519 1520 Add this to your \file{.emacs} file: 1521 \begin{verbatim} 1522 (custom-set-variables 1523 ;; custom-set-variables was added by Custom -- don't edit or cut/paste it! 1524 ;; Your init file should contain only one such instance. 1525 '(c-basic-offset 2) 1526 '(c-default-style (quote ((other . "k\&r")))) 1527 '(c-offsets-alist (quote ((statement-case-intro . 1) (statement-case-open . 1) (case-label . 1)))) 1528 '(comment-column 40) 1529 ) 1530 \end{verbatim} 1531 1532 If you already have a 1533 \code{custom-set-variables} command be careful! Or set the same values 1534 using the \code{Customize Emacs} entry in the \code{Options} menu, 1535 then follow down: 1536 \begin{verbatim} 1537 Browse Customization Groups --> 1538 Programming --> 1539 Languages --> 1540 C 1541 \end{verbatim} 1542 (an alternative way to get to the same place is 1543 \begin{verbatim} 1544 ESCx 1545 customize-group 1546 C 1547 \end{verbatim} 1548 ) 1549 Then set the values of 1550 \begin{verbatim} 1551 C Basic Offset ==> 2 1552 C Default Style ==> k\&r 1553 C Offsets Alist ==> X statement-case-intro 1 1554 X statement-case-open 1 1555 X case-label 1 1556 \end{verbatim} 1557 1558 Then in the same \code{Customize Emacs} menu, choose 1559 \begin{verbatim} 1560 Settings Matching Regexp... 1561 \end{verbatim} 1562 then set the value of 1563 \begin{verbatim} 1564 Comment Column ==> 40 1565 \end{verbatim} 1566 1567 If you want to use \code{//} style comments, see the top 1568 of the next section. 1569 1570 \subsection{Setting variables directly via lisp hooks} 1571 \label{Lhooks} 1572 1573 If you want to use \code{//} style comments, you may want to 1574 make \code{ESC;} insert them rather than the traditional 1575 \code{/* .. */} version. If so, add something like this to 1576 your \file{.emacs} file: 1577 \begin{verbatim} 1578 (add-hook 'c-mode-hook 1386 The easiest way to use these conventions while writing code using 1387 emacs is to add these lines to your \file{.emacs} file: 1388 1389 \begin{verbatim} 1390 (define-derived-mode panstarrs-c-mode 1391 c-mode "Pan-STARRS C" 1392 "Major mode for editing Pan-STARRS C source. 1393 \\{panstarrs-c-mode-map}" 1394 1395 (setq c-default-style '((other . "k&r"))) 1396 (setq c-basic-offset 4) 1397 (setq comment-column 40) 1398 (add-to-list 'c-offsets-alist (cons 'statement-case-intro 2)) 1399 (add-to-list 'c-offsets-alist (cons 'statement-case-open 2)) 1400 (add-to-list 'c-offsets-alist (cons 'case-label 2)) 1401 ) 1402 \end{verbatim} 1403 1404 You then need to ensure that this mode is set when editing code; 1405 you could always type \code{ESCx panstarrs-c-mode} when you 1406 opened a file, but the simplest way is to add 1407 \begin{verbatim} 1408 (add-to-list 'auto-mode-alist (cons "\\.[ch]$" 'panstarrs-c-mode)) 1409 \end{verbatim} % $ % match that $ for emacs 1410 to your \file{.emacs} file; this will use \code{panstarrs-c-mode} 1411 for all \file{.c} and \file{.h} files. 1412 1413 If you want to use C99-style comments (i.e. \code{//} to end of line), 1414 add this to your \file{.emacs} file too: 1415 \begin{verbatim} 1416 (add-hook 'panstarrs-c-mode-hook 1579 1417 '(lambda () 1580 1418 (setq comment-start "// ") 1581 1419 (setq comment-end ""))) 1582 1420 \end{verbatim} 1583 1584 You can get fancier, of course. For example, 1585 \begin{verbatim} 1586 (setq ps-code t) ;this is Pan-STARRS code 1587 (add-hook 'c-mode-hook 1588 '(lambda () 1589 (if (and (boundp 'ps-code) ps-code) 1590 (progn 1591 (setq comment-start "// ") 1592 (setq comment-end ""))))) 1593 \end{verbatim} 1594 will make emacs use \code{//} if and only if the lisp variable 1595 \code{ps-code} is set and non-\code{nil}. 1596 1597 To entirely bypass emacs' customize menus, also add this to your 1598 \textit{.emacs} file (I left the \code{ps-code} test in): 1599 \begin{verbatim} 1600 (add-hook 'c-mode-hook 1601 '(lambda () 1602 (if (and (boundp 'ps-code) ps-code) 1603 (progn 1604 (setq c-basic-offset 2) 1605 (setq c-default-style '((other . "k&r"))) 1606 (setq comment-column 40) 1607 (let ( el (vals 1608 '((statement-case-intro . 1) 1609 (statement-case-open . 1) 1610 (case-label . 1))) ) 1611 (while vals 1612 (setq el (car vals)) 1613 (setq vals (cdr vals)) 1614 (setq c-offsets-alist 1615 (cons el 1616 (assoc-delete-all 1617 (car el) c-offsets-alist)))) 1618 ) 1619 )))) 1620 \end{verbatim} 1621 (and yes, there must be an easier way to set that alist. But this works). 1622 1421 1623 1422 \bibliographystyle{plain} 1624 1423 \bibliography{panstarrs}
Note:
See TracChangeset
for help on using the changeset viewer.
