Chapters: 1: Introduction 2: Simple example 3: Invocation 4: Finer Control 5: X-Y Plots 6: Contour Plots 7: Image Plots 8: Examples 9: Gri Commands 10: Programming 11: Environment 12: Emacs Mode 13: History 14: Installation 15: Gri Bugs 16: Test Suite 17: Gri in Press 18: Acknowledgments 19: License Indices: Concepts Commands Variables | ### 10.11.4: Complicated example of a new command The following example from the global ``gri.cmd`' file illustrates how to parse/check the commandline (see Local Synonyms), which is a good practice in any code you expect to re-use. The first ``if`' statement checks that the word ``at`' is in the right place (this would not have been checked by the syntax matcher, the word having followed a string). The presence of the keyword ``cm`' is checked for, and user units or cm units are used accordingly. Local variables are created (``new`') and then destroyed (``delete`') so that this new command cannot affect outside code.
    `draw label whiteunder "\string" at .xleft. .ybottom. [cm]'
    Draw label for plot, located with lower-left corner
    at indicated (x,y) position (specified in user
    units or in cm on the page).  Whiteout is used
    to clean up the area under the label.  BUGS:
    Cannot handle angled text; doesn't check for
    super/subscripts.
    {
        if {rpn "\.word4." "at" !=}
          show "ERROR: 5th word must be `at', not `\.word4.'"
          show traceback
          quit
        end if
        new .x. .y. .oldgray. .space.
        if {rpn \.words. 7 ==}
          .x. = {rpn \.word5. xusertocm}
          .y. = {rpn \.word6. yusertocm}
        else if {rpn \.words. 8 ==}
          if {rpn "\.word7." "cm" !=}
            show "ERROR: Require 8th word to be `cm'"
            show traceback
            quit
          end if
          .x. = \.word5.
          .y. = \.word6.
        else
          show "ERROR: Require 7 or 8 words, not \.words."
          show traceback
          quit
        end if
        # Coordinates now in cm.  Next, white out a box
        # under the text (and .space. centimetres
        # beyond text), then draw label.
        .space. = 0.1               # Space of 1mm
        .oldgray. = ..graylevel..
        set graylevel white
        draw box filled                          \
          {rpn .x. .space. -}                    \
          {rpn .y. .space. -}                    \
          {rpn .x. "\.word3." width + .space. +} \
          {rpn .y. "M" ascent + .space. + } cm
        set graylevel .oldgray.
        draw label "\.word3." at .x. .y. cm
        delete .x. .y. .oldgray. .space.
    }