home *** CD-ROM | disk | FTP | other *** search
- ############################################################################
- #
- # File: snapshot.icn
- #
- # Subject: Procedures to show snapshot of Icon string scanning
- #
- # Author: Ralph E. Griswold and Randal L. Schwartz, modified by
- # Cheyenne Wills and Richard Goerwitz
- #
- # Date: Mar 23, 1991
- #
- ###########################################################################
- #
- # The procedure snapshot(title,len) writes a snapshot of the state
- # of string scanning, showing the value of &subject and &pos, an
- # optional title (arg 1), and (again optionally) wrapping the display
- # for a terminal of len (arg 2) columns.
- #
- # For example,
- #
- # "((a+b)-delta)/(c*d))" ? {
- # tab(bal('+-/*'))
- # snapshot("example")
- # }
- #
- # produces
- #
- # ---example---------------------------
- # | |
- # | |
- # | &subject = "((a+b)-delta)/(c*d))" |
- # | | |
- # | |
- # -------------------------------------
- #
- # Note that the bar showing the &pos is positioned under the &posth
- # character (actual positions are between characters). If &pos is
- # at the end of &subject, the bar is positioned under the quotation
- # mark delimiting the subject. For example,
- #
- # "abcdefgh" ? (tab(0) & snapshot())
- #
- # produces
- #
- # -------------------------
- # | |
- # | |
- # | &subject = "abcdefgh" |
- # | | |
- # | |
- # -------------------------
- #
- # Escape sequences are handled properly. For example,
- #
- # "abc\tdef\nghi" ? (tab(upto('\n')) & snapshot())
- #
- # produces
- #
- # ------------------------------
- # | |
- # | |
- # | &subject = "abc\tdef\nghi" |
- # | | |
- # | |
- # ------------------------------
- #
- # The title argument places a title into the top bar, as in
- #
- # "abc\tdef\nghi" ? (tab(upto('\n')) & snapshot("upto('\n')")
- #
- # which produces
- #
- # --upto('\n')-------------------
- # | |
- # | |
- # | &subject = "abc\tdef\nghi" |
- # | | |
- # | |
- # -------------------------------
- #
- # The len argument rewraps the display for a screen of len width.
- #
- ############################################################################
-
-
- procedure snapshot(title,len)
-
- local bar1, bar2, bar3, is, is0, prefix, titlel, placement, POS
-
- /title := "" # no meaningful default
- \len <:= 20 # any less is really not useful
- prefix := "&subject = "
- is := image(&subject)
- is0 := *image(&subject[1:&pos]) | fail
-
- #
- # Set up top and bottom bars (not exceeding len width, if
- # len is nonnull). Fit title into top bar (bar1).
- #
- bar1 := bar3 := repl("-", *is + *prefix + 4)[1:\len-4|0]
- # in *is + *prefix + 4, the 4 is for two vbars/two spaces
- titlel := (*title > *bar3-4) | *title[1:\len-4|0]
- bar1 ?:= move(3) || (tab(4+titlel), title) || tab(0)
-
- #
- # Write bar1, then spacers (bar2). Then write out len-size chunks
- # of &subject, with the | pointer-line, where appropriate. Finally,
- # write out bar3 (like bar1, but with no title).
- #
- write(bar1)
- bar2 := "|" || repl(" ", *bar3 - 2) || "|"
- write(bar2, "\n", bar2)
- placement := *prefix + is0
- (prefix || is) ? {
- until pos(0) do {
- POS := &pos - 1
- write("| ", move(*bar3-4) | left(tab(0), *bar3-4), " |")
- if POS < placement < &pos then {
- writes("| ")
- writes(left(repl(" ", placement - POS - 1) || "|", *bar3-4))
- write(" |\n", bar2)
- }
- else write(bar2, "\n", bar2)
- }
- }
- write(bar3)
- return # nothing useful to return
-
- end
-