WordCountButtonImpl.mesa
Copyright Ó 1988, 1992 by Xerox Corporation. All rights reserved.
Wes Irish, January 12, 1989 4:26:53 pm PST
Jules Bloomenthal July 7, 1992 12:30 pm PDT
Willie-s, November 13, 1991 10:47 am PST
DIRECTORY
Buttons, Commander, Convert, IO, MessageWindow, Rope, ViewerClasses, ViewerOps, ViewerTools, WordCount;
WordCountButtonImpl: CEDAR PROGRAM
IMPORTS Buttons, Commander, Convert, IO, MessageWindow, Rope, ViewerOps, ViewerTools, WordCount
EXPORTS WordCount = {
TYPEs...
ROPE: TYPE = Rope.ROPE;
PROCs...
CountSelection: PUBLIC PROC [tokenMode: BOOL ¬ FALSE] RETURNS [lines, words, characters: INT ¬ 0] ~ {
[lines, words, characters] ¬ WordCount.CountRope[ViewerTools.GetSelectionContents[], tokenMode];
};
WCButton: Buttons.ButtonProc ~ {
PROC [parent: Viewer, clientData: REF ANYNIL,
mouseButton: MouseButton ← red, shift, control: BOOLFALSE];
selection: BOOL ¬ mouseButton IN [red..yellow];
tokenMode: BOOL ¬ shift;
msg: ROPE ¬ NIL;
lines, words, characters: INT ¬ 0;
[lines, words, characters] ¬ IF selection THEN CountSelection[tokenMode]
ELSE WordCount.CountRope[NARROW[ViewerOps.GetViewer[ViewerTools.GetSelectedViewer[]]], tokenMode];
msg ¬ IO.PutFR1["%g: ", [rope[IF selection THEN "Selection" ELSE "Viewer"]]];
msg ¬ Rope.Cat[msg, "lines: ", Convert.RopeFromInt[from: lines, showRadix: FALSE]];
msg ¬ Rope.Cat[msg, ", words: ", Convert.RopeFromInt[from: words, showRadix: FALSE]];
msg ¬ Rope.Cat[msg, ", chars: ", Convert.RopeFromInt[from: characters, showRadix: FALSE]];
msg ¬ Rope.Concat[msg, IO.PutFR1[" (%g mode)", [rope[IF tokenMode THEN "token" ELSE "white-space"]]]];
MessageWindow.Append[message: msg, clearFirst: TRUE];
};
CreateWCButton: Commander.CommandProc ~ {
[] ¬ Buttons.Create[
info: [name: "WC", parent: NIL],
proc: WCButton,
documentation: "Counts the lines, words, and chars in the current selection (or viewer)."];
};
Commander.Register[key: "WordCountButton", proc: CreateWCButton, doc: "Creates a WC (Word Count) button in the top button space."];
}.