<<>> <> <> <> <> <> <<>> 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 = { <> <<>> ROPE: TYPE = Rope.ROPE; <> CountSelection: PUBLIC PROC [tokenMode: BOOL ¬ FALSE] RETURNS [lines, words, characters: INT ¬ 0] ~ { [lines, words, characters] ¬ WordCount.CountRope[ViewerTools.GetSelectionContents[], tokenMode]; }; WCButton: Buttons.ButtonProc ~ { <> 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."]; }.