Author Topic: Formatting code in your posts  (Read 2973 times)

0 Members and 1 Guest are viewing this topic.

John Kaul (Se7en)

  • Administrator
  • Needs a day job
  • Posts: 8734
Formatting code in your posts
« on: November 26, 2014, 08:31:05 am »
1.0 Formatting your code for best results at the TheSwamp.org.

1.1 Basic CODE tag
When posting code to the swamp please use CODE tags to automatically format the code, otherwise it will be formatted for standard text, i.e. it will be posted in a proportional font, will loose indentation etc.

TheSwamp.org offers two types of CODE formatting; a generic CODE tag, e.g. [code]...your code...[/code], which honors indentation and uses a fixed width font.

It displays your code like this:

Code: [Select]
(defun c:MyProgram ( / input )
    ;;  get input from user
    (setq input (getstring "nEnter input: "))
    ;;  echo user's input
    (princ (strcat "You entered <" input ">.\n"))
    ;;  shhh
    (princ)
)

To use this CODE tag, use the [code]...your code...[/code] format as noted above, for example:

[code]
      ...
      your code
      ...
[/code]


1.2 TheSwamp's custom CODE tag
The other type of CODE formatting employs a custom tagging mechanism, e.g. [code=cadlisp-7]...your code...[/code]. It offers syntax highlighting (in addition to honoring indentation and the use of a fixed width font), and in some cases adds URL links to function names for quick help.

It displays your code like this:

Code - Auto/Visual Lisp: [Select]
  1. (defun c:MyProgram ( / input )
  2.    ;;  get input from user
  3.    (setq input (getstring "nEnter input: "))
  4.    ;;  echo user's input
  5.    (princ (strcat "You entered <" input ">.\n"))
  6.    ;;  shhh
  7.    (princ)
  8. )

Note that some items above are URL linked to quick help, try it by clicking defun or setq above.

To use this CODE tag, use the [code=cadlisp-7]...your code...[/code] format mentioned above, for example:

[code=cadlisp-7]
      ...
      your code
      ...
[/code]


TheSwamp.org has added this second CODE formatting ability by using a program called GeSHi -- a generic syntax highlighter which is capable of highlighting over 200 different programming languages.

Some of the most popular languages supported on this forum are:
    [code=cadlisp-7]
    [code=csharp]
    [code=cpp]
    [code=cpp-qt]
    [code=html]
    [code=python]
    [code=vb]
    [code=vbnet]


1.3 Examples
1.3.1 Simple Examples
Simple Autolisp example
[code=cadlisp-7]
Code - Auto/Visual Lisp: [Select]
  1. ;;      testfile.lsp
  2. ;;;------------------------------------------------------------------
  3. ;;;------------------------------------------------------------------
  4. ;;
  5. ;; LIBRARY STUFF
  6. ;;
  7. ;;;------------------------------------------------------------------
  8. ;;;------------------------------------------------------------------
  9. (defun kdub:on-error (msg / tmp)
  10.  ;;----- Cancel any Active Commands -----------------------------
  11.  (while (< 0 (getvar "cmdactive")) (command))
  12.  (setvar "menuecho" 1)
  13.  (vla-endundomark kglobal:activedoc)
  14.  ;;----- Display error message if applicable _-------------------
  15.  (cond
  16.    ((not msg))                              ; no error, do nothing.
  17.    ((member (strcase msg t)                 ; User cancelled.
  18.             '("console break" "function cancelled" "quit / exit abort")
  19.     )
  20.    )
  21.    ((princ (strcat "\nApplication Error: " (itoa (getvar "errno")) " :- " msg))
  22.     ;;----- Display backtrace if in debug mode ---------------------
  23.     (if kglobal:debug_on
  24.       (vl-bt)
  25.     )
  26.    )
  27.  )
  28.  (setvar "errno" 0)
  29.  ;;----- Reset System Variables from global list ----------------
  30.  (kdub:restoresysvar)
  31.  (princ)
  32. )
  33. ;;;------------------------------------------------------------------
  34. ;;;------------------------------------------------------------------
  35.  
Simple C++ example
[code=cpp]
Code - C++: [Select]
  1. //-------------------------------------------------------------------
  2. // SkipWhiteSpace
  3. // Repeatedly fetch characters from the text input as long as they're
  4. // whitespace.
  5. //-------------------------------------------------------------------
  6. void TTextScanner::SkipWhiteSpace( void ) {
  7.    char ch = pTextInBuffer->Char();
  8.  
  9.    do {
  10.        if (charCodeMap[ch] == ccWhiteSpace) {
  11.            // --Saw a whitespace character: fetch the next char.
  12.            ch = pTextInBuffer->GetChar();
  13.        } else {
  14.            if (ch == '{') {
  15.                // --Skip over a comment, then fetch the next char.
  16.                do {
  17.                    ch = pTextInBuffer->GetChar();
  18.                } while ((ch != '}') && (ch != eofChar));
  19.                if (ch != eofChar) ch = pTextInBuffer->GetChar();
  20.                else               Error(errUnexpectedEndOfFile);
  21.            }
  22.        }
  23.    } while ((charCodeMap[ch] == ccWhiteSpace) || (ch == '{'));
  24. }

1.3.2 Line Highlighting Examples
Autolisp example highlighting line 9
[code=cadlisp-7,9]
Code - Auto/Visual Lisp: [Select]
  1. ;;      testfile.lsp
  2. ;;;------------------------------------------------------------------
  3. ;;;------------------------------------------------------------------
  4. ;;
  5. ;; LIBRARY STUFF
  6. ;;
  7. ;;;------------------------------------------------------------------
  8. ;;;------------------------------------------------------------------
  9. (defun kdub:on-error (msg / tmp)
  10.  ;;----- Cancel any Active Commands -----------------------------
  11.  (while (< 0 (getvar "cmdactive")) (command))
  12.  (setvar "menuecho" 1)
  13.  (vla-endundomark kglobal:activedoc)
  14.  ;;----- Display error message if applicable _-------------------
  15.  (cond
  16.    ((not msg))                              ; no error, do nothing.
  17.    ((member (strcase msg t)                 ; User cancelled.
  18.             '("console break" "function cancelled" "quit / exit abort")
  19.     )
  20.    )
  21.    ((princ (strcat "\nApplication Error: " (itoa (getvar "errno")) " :- " msg))
  22.     ;;----- Display backtrace if in debug mode ---------------------
  23.     (if kglobal:debug_on
  24.       (vl-bt)
  25.     )
  26.    )
  27.  )
  28.  (setvar "errno" 0)
  29.  ;;----- Reset System Variables from global list ----------------
  30.  (kdub:restoresysvar)
  31.  (princ)
  32. )
  33. ;;;------------------------------------------------------------------
  34. ;;;------------------------------------------------------------------
  35.  
C++ example highlighting line 6
[code=cpp,6]
Code - C++: [Select]
  1. //-------------------------------------------------------------------
  2. // SkipWhiteSpace
  3. // Repeatedly fetch characters from the text input as long as they're
  4. // whitespace.
  5. //-------------------------------------------------------------------
  6. void TTextScanner::SkipWhiteSpace( void ) {
  7.    char ch = pTextInBuffer->Char();
  8.  
  9.    do {
  10.        if (charCodeMap[ch] == ccWhiteSpace) {
  11.            // --Saw a whitespace character: fetch the next char.
  12.            ch = pTextInBuffer->GetChar();
  13.        } else {
  14.            if (ch == '{') {
  15.                // --Skip over a comment, then fetch the next char.
  16.                do {
  17.                    ch = pTextInBuffer->GetChar();
  18.                } while ((ch != '}') && (ch != eofChar));
  19.                if (ch != eofChar) ch = pTextInBuffer->GetChar();
  20.                else               Error(errUnexpectedEndOfFile);
  21.            }
  22.        }
  23.    } while ((charCodeMap[ch] == ccWhiteSpace) || (ch == '{'));
  24. }

1.3.3 Line Numbering and Highlighting Examples
Autolisp example with line numbering starting at 9 and highlighting lines 9 and 12
[code=cadlisp-7.9,9,12]
Code - Auto/Visual Lisp: [Select]
  1. ;;      testfile.lsp
  2. ;;;------------------------------------------------------------------
  3. ;;;------------------------------------------------------------------
  4. ;;
  5. ;; LIBRARY STUFF
  6. ;;
  7. ;;;------------------------------------------------------------------
  8. ;;;------------------------------------------------------------------
  9. (defun kdub:on-error (msg / tmp)
  10.  ;;----- Cancel any Active Commands -----------------------------
  11.  (while (< 0 (getvar "cmdactive")) (command))
  12.  (setvar "menuecho" 1)
  13.  (vla-endundomark kglobal:activedoc)
  14.  ;;----- Display error message if applicable _-------------------
  15.  (cond
  16.    ((not msg))                              ; no error, do nothing.
  17.    ((member (strcase msg t)                 ; User cancelled.
  18.             '("console break" "function cancelled" "quit / exit abort")
  19.     )
  20.    )
  21.    ((princ (strcat "\nApplication Error: " (itoa (getvar "errno")) " :- " msg))
  22.     ;;----- Display backtrace if in debug mode ---------------------
  23.     (if kglobal:debug_on
  24.       (vl-bt)
  25.     )
  26.    )
  27.  )
  28.  (setvar "errno" 0)
  29.  ;;----- Reset System Variables from global list ----------------
  30.  (kdub:restoresysvar)
  31.  (princ)
  32. )
  33. ;;;------------------------------------------------------------------
  34. ;;;------------------------------------------------------------------
  35.  
C++ example with line numbering starting at 9 and highlighting lines 2 and 6
[code=cpp.9,2,6]
Code - C++: [Select]
  1. //-------------------------------------------------------------------
  2. // SkipWhiteSpace
  3. // Repeatedly fetch characters from the text input as long as they're
  4. // whitespace.
  5. //-------------------------------------------------------------------
  6. void TTextScanner::SkipWhiteSpace( void ) {
  7.    char ch = pTextInBuffer->Char();
  8.  
  9.    do {
  10.        if (charCodeMap[ch] == ccWhiteSpace) {
  11.            // --Saw a whitespace character: fetch the next char.
  12.            ch = pTextInBuffer->GetChar();
  13.        } else {
  14.            if (ch == '{') {
  15.                // --Skip over a comment, then fetch the next char.
  16.                do {
  17.                    ch = pTextInBuffer->GetChar();
  18.                } while ((ch != '}') && (ch != eofChar));
  19.                if (ch != eofChar) ch = pTextInBuffer->GetChar();
  20.                else               Error(errUnexpectedEndOfFile);
  21.            }
  22.        }
  23.    } while ((charCodeMap[ch] == ccWhiteSpace) || (ch == '{'));
  24. }
“Common sense is not so common.” ~Voltaire

--> Donate to TheSwamp.org <--