DIRECTORY Atom, Buttons, Menus, Commander, CommandTool, Convert, IO, ProcessExtras USING [CheckForAbort], Rope, TiogaOps, ViewerClasses, ViewerOps, ViewerTools, VFonts ; TiogaRenumber: CEDAR PROGRAM IMPORTS Buttons, Commander, CommandTool, ProcessExtras, IO, Convert, Rope, TiogaOps, ViewerOps, ViewerTools = BEGIN ROPE: TYPE ~ Rope.ROPE; FootNotes: PROCEDURE [name: Rope.ROPE, startingAfter: INT _ 0] RETURNS[last: INT _ 0] = { last _ DocumentRenumbering[documentName: name, startingAfter: startingAfter, patternRope: "!", patternLooks: "m"]; }; References: PROCEDURE [name: Rope.ROPE, startingAfter: INT, delta: INT] = { [] _ DocumentRenumbering[documentName: name, startingAfter: startingAfter, delta: delta, patternRope: "["]; }; Figures: PROCEDURE [name: Rope.ROPE, startingAfter: INT, delta: INT] = { [] _ DocumentRenumbering[documentName: name, startingAfter: startingAfter, delta: delta, patternRope: "Figure "]; }; DocumentRenumbering: PROCEDURE [documentName: Rope.ROPE, startingAfter: INT, delta: INT _ 1, patternRope: Rope.ROPE, patternLooks: Rope.ROPE _ NIL] RETURNS[last: INT _ 0] = { viewer: ViewerClasses.Viewer; IF (viewer _ ViewerOps.FindViewer[documentName]) = NIL THEN viewer _ ViewerOps.CreateViewer[flavor: $Text, info: [name: documentName, file: documentName]]; IF viewer.iconic THEN ViewerOps.OpenIcon[viewer]; IF viewer = NIL THEN RETURN[FIRST[INT]]; last _ GenericRenumbering[viewer~viewer, startSearch~[TiogaOps.ViewerDoc[viewer], 0], startAtNumber~startingAfter-1, becomesNumber~startingAfter+delta, delta~delta, patternRope~patternRope, patternLooks~patternLooks]; }; GenericRenumbering: PROC [viewer: ViewerClasses.Viewer, startSearch: TiogaOps.Location, startAtNumber, becomesNumber: INT, delta: INT _ 1, patternRope: Rope.ROPE, patternLooks: Rope.ROPE _ NIL, singleStep: BOOLEAN _ FALSE] RETURNS [last: INT _ FIRST[INT]] ~ { pattern: TiogaOps.Pattern ~ TiogaOps.CreateSimplePattern[patternRope]; -- creates a pattern for the search. checkLooks: BOOLEAN ~ NOT patternLooks.IsEmpty; SeenNode: TYPE = RECORD[key: Rope.ROPE, val: INT]; seen: LIST OF REF SeenNode _ NIL; current: INT _ becomesNumber; numberToken: ROPE; stream: IO.STREAM; NumberProc: IO.BreakProc ~ { RETURN [SELECT char FROM IN ['0 .. '9] => other, IN [IO.NUL .. IO.SP], ',, ':, '; => sepr, ENDCASE => break]}; TiogaOps.SelectPoint[viewer: viewer, caret: startSearch]; DO begin, end: TiogaOps.Location; looks: ROPE; replacementNumber: INT; found: BOOL _ TiogaOps.SelectionSearch[pattern: pattern]; IF NOT found THEN EXIT; [, begin, end] _ TiogaOps.GetSelection[]; IF checkLooks THEN { looks _ TiogaOps.FetchLooks[node: begin.node, index: begin.where]; IF Rope.Find[s1: looks, s2: patternLooks] = -1 THEN LOOP; -- this information should be specified in the pattern, but currently don't know how to create such a pattern }; stream _ IO.RIS[TiogaOps.GetRope[begin.node], stream]; IO.SetIndex[stream, end.where + 1]; numberToken _ IO.GetTokenRope[stream, NumberProc ! IO.Error => LOOP].token; FOR l: LIST OF REF SeenNode _ seen, l.rest UNTIL l = NIL DO IF Rope.Equal[l.first.key, numberToken] THEN { replacementNumber _ l.first.val; EXIT; }; REPEAT FINISHED => { replacementNumber _ current; current _ current + delta; seen _ CONS[NEW[SeenNode _ [key: numberToken, val: replacementNumber]], seen]; }; ENDLOOP; IF replacementNumber < startAtNumber THEN LOOP; TiogaOps.SetSelection[viewer: viewer, start: [end.node, end.where + 1], end: [end.node, IO.GetIndex[stream] - 1]]; looks _ TiogaOps.FetchLooks[node: end.node, index: end.where + 1]; TiogaOps.Delete[]; TiogaOps.SetLooks[looks, caret]; TiogaOps.InsertRope[Convert.RopeFromInt[replacementNumber]]; last _ replacementNumber; IF singleStep THEN EXIT; ENDLOOP; RETURN[last]; }; RenumberFootnotesCmd: Commander.CommandProc ~ { argv: CommandTool.ArgumentVector; currentNumber: INT _ 0; argv _ CommandTool.Parse[cmd: cmd ! CommandTool.Failed => { msg _ errorMsg; CONTINUE; }]; IF argv = NIL THEN RETURN[$Failure, msg]; FOR i: NAT IN [1..argv.argc) DO IF argv[i].Length[] = 0 THEN LOOP; IF argv[i].Fetch[0] = '- THEN { IF argv[i].Fetch[1] = 'n OR argv[i].Fetch[1] = 'N THEN currentNumber _ Convert.IntFromRope[argv[i].Substr[2]]; LOOP; }; [] _ FootNotes[argv[i], currentNumber]; IF AbortRequested[cmd, TRUE] THEN EXIT; ENDLOOP; }; RenumberFiguresCmd: Commander.CommandProc ~ { argv: CommandTool.ArgumentVector; startAfter: INT _ 0; delta: INT _ 1; argv _ CommandTool.Parse[cmd: cmd ! CommandTool.Failed => { msg _ errorMsg; CONTINUE; }]; IF argv = NIL THEN RETURN[$Failure, msg]; FOR i: NAT IN [1..argv.argc) DO IF argv[i].Length[] = 0 THEN LOOP; IF argv[i].Fetch[0] = '- THEN { IF argv[i].Fetch[1] = 'a OR argv[i].Fetch[1] = 'A THEN startAfter _ Convert.IntFromRope[argv[i].Substr[2]] ELSE IF argv[i].Fetch[1] = 'd OR argv[i].Fetch[1] = 'D THEN delta _ Convert.IntFromRope[argv[i].Substr[2]]; LOOP; }; Figures[argv[i], startAfter, delta]; IF AbortRequested[cmd, TRUE] THEN EXIT; ENDLOOP; }; RenumberReferencesCmd: Commander.CommandProc ~ { argv: CommandTool.ArgumentVector; startAfter: INT _ 0; delta: INT _ 1; argv _ CommandTool.Parse[cmd: cmd ! CommandTool.Failed => { msg _ errorMsg; CONTINUE; }]; IF argv = NIL THEN RETURN[$Failure, msg]; FOR i: NAT IN [1..argv.argc) DO IF argv[i].Length[] = 0 THEN LOOP; IF argv[i].Fetch[0] = '- THEN { IF argv[i].Fetch[1] = 'a OR argv[i].Fetch[1] = 'A THEN startAfter _ Convert.IntFromRope[argv[i].Substr[2]] ELSE IF argv[i].Fetch[1] = 'd OR argv[i].Fetch[1] = 'D THEN delta _ Convert.IntFromRope[argv[i].Substr[2]]; LOOP; }; References[argv[i], startAfter, delta]; IF AbortRequested[cmd, TRUE] THEN EXIT; ENDLOOP; }; RenumberCmd: Commander.CommandProc ~ { argv: CommandTool.ArgumentVector; startAfter: INT _ 0; delta: INT _ 1; patternRope, patternLooks: Rope.ROPE; argv _ CommandTool.Parse[cmd: cmd ! CommandTool.Failed => { msg _ errorMsg; CONTINUE; }]; IF argv = NIL OR argv.argc = 1 THEN { v: ViewerClasses.Viewer ~ ViewerOps.CreateViewer[flavor: $TiogaRenumber, info: [name: "Renumber", iconic: TRUE, column: right, scrollable: FALSE]]; ViewerOps.OpenIcon[v]; ViewerOps.TopViewer[v]; RETURN; }; FOR i: NAT IN [1..argv.argc) DO IF argv[i].Length[] = 0 THEN LOOP; IF argv[i].Fetch[0] = '- THEN { SELECT argv[i].Fetch[1] FROM 'a, 'A => startAfter _ Convert.IntFromRope[argv[i].Substr[2]]; 'd, 'D => delta _ Convert.IntFromRope[argv[i].Substr[2]]; 'p, 'P => patternRope _ argv[i].Substr[2]; 'l, 'L => patternLooks _ argv[i].Substr[2]; ENDCASE => RETURN[$Failure, IO.PutFR["Bad -option syntax in %g\n..Aborted", IO.rope[argv[i]]]]; LOOP; }; IF patternRope.IsEmpty THEN RETURN[$Failure, "-p option to specify pattern missing\n..Aborted"]; [] _ DocumentRenumbering[argv[i], startAfter, delta, patternRope, patternLooks]; IF AbortRequested[cmd, TRUE] THEN EXIT; ENDLOOP; }; AbortRequested: PROC [cmd: Commander.Handle, reset: BOOL _ FALSE] RETURNS [BOOL] = { ProcessExtras.CheckForAbort[]; RETURN [FALSE]; }; InitRenumberViewer: ViewerClasses.InitProc ~ { t: ViewerClasses.Viewer; b: Buttons.Button; line1: INT ~ 0; line2: INT ~ 14; height: INT ~ 16; patternWidth: INT ~ 70; numberWidth: INT ~ 35; openHeight: INT ~ 47; -- experimentally determined... containerInitProc[self]; -- since I know its really a $Container, I need to init it for him b _ Buttons.Create[proc: DoITProc, info: [parent: self, name: "DoIT!", wx: 0, wy: line1, border: FALSE]]; b _ Buttons.Create[proc: StepProc, info: [parent: self, name: "ForAll", wx: b.wx+b.ww, wy: line1, border: FALSE]]; ViewerOps.AddProp[viewer: self, prop: $SingleStep, val: b]; b _ Buttons.Create[proc: FiguresProc, info: [parent: self, name: "Figures", wx: b.wx+b.ww, wy: line1, border: FALSE]]; b _ Buttons.Create[proc: FootnotesProc, info: [parent: self, name: "Footnotes", wx: b.wx+b.ww, wy: line1, border: FALSE]]; b _ Buttons.Create[proc: ReferencesProc, info: [parent: self, name: "References", wx: b.wx+b.ww, wy: line1, border: FALSE]]; b _ Buttons.Create[proc: PatternProc, info: [parent: self, name: "Pattern:", wx: 0, wy: line2, border: FALSE]]; t _ ViewerTools.MakeNewTextViewer[info: [parent: self, wx: b.wx+b.ww, wy: line2+1, ww: patternWidth, wh: height, border: FALSE, scrollable: TRUE]]; ViewerOps.AddProp[viewer: self, prop: $Pattern, val: t]; b _ Buttons.Create[proc: StartAtProc, info: [parent: self, name: "StartAt:", wx: t.wx+t.ww, wy: line2, border: FALSE]]; t _ ViewerTools.MakeNewTextViewer[info: [parent: self, wx: b.wx+b.ww, wy: line2+1, ww: numberWidth, wh: height, border: FALSE, scrollable: TRUE]]; ViewerOps.AddProp[viewer: self, prop: $StartAt, val: t]; ViewerTools.SetContents[t, "1"]; b _ Buttons.Create[proc: BecomesProc, info: [parent: self, name: "Becomes:", wx: t.wx+t.ww, wy: line2, border: FALSE]]; t _ ViewerTools.MakeNewTextViewer[info: [parent: self, wx: b.wx+b.ww, wy: line2+1, ww: numberWidth, wh: height, border: FALSE, scrollable: TRUE]]; ViewerTools.SetContents[t, "1"]; ViewerOps.AddProp[viewer: self, prop: $Becomes, val: t]; b _ Buttons.Create[proc: DeltaProc, info: [parent: self, name: "Delta:", wx: t.wx+t.ww, wy: line2, border: FALSE]]; t _ ViewerTools.MakeNewTextViewer[info: [parent: self, wx: b.wx+b.ww, wy: line2+1, ww: numberWidth, wh: height, border: FALSE, scrollable: TRUE]]; ViewerOps.AddProp[viewer: self, prop: $Delta, val: t]; ViewerTools.SetContents[t, "1"]; self.openHeight _ openHeight; }; DoITProc: Buttons.ButtonProc ~ { patternRope: ROPE; patternLooks: ROPE; startAtNumber: INT; becomesNumber: INT; delta: INT; singleStep: BOOLEAN; startSearch: TiogaOps.Location; viewer: ViewerClasses.Viewer; [viewer, startSearch, ] _ TiogaOps.GetSelection[primary]; singleStep _ ViewerTools.GetContents[ViewerFromParent[parent, $SingleStep]].Equal["Single"]; IF NOT singleStep THEN startSearch _ [TiogaOps.SelectionRoot[], 0]; patternRope _ ViewerTools.GetContents[ViewerFromParent[parent, $Pattern]]; IF patternRope.IsEmpty THEN ERROR; patternLooks _ TiogaOps.FetchLooks[node: TiogaOps.ViewerDoc[ViewerFromParent[parent, $Pattern]], index: 1]; startAtNumber _ Convert.IntFromRope[ViewerTools.GetContents[ViewerFromParent[parent, $StartAt]]]; becomesNumber _ Convert.IntFromRope[ViewerTools.GetContents[ViewerFromParent[parent, $Becomes]]]; delta _ Convert.IntFromRope[ViewerTools.GetContents[ViewerFromParent[parent, $Delta]]]; [] _ GenericRenumbering[viewer, startSearch, startAtNumber, becomesNumber, delta, patternRope, patternLooks, singleStep]; }; ViewerFromParent: PROC [parent: REF ANY, atom: ATOM] RETURNS [v: ViewerClasses.Viewer] ~ { ref: REF ~ ViewerOps.FetchProp[viewer: NARROW[parent, ViewerClasses.Viewer].parent, prop: atom]; IF ref # NIL AND ISTYPE[ref, ViewerClasses.Viewer] THEN v _ NARROW[ref, ViewerClasses.Viewer]; }; StepProc: Buttons.ButtonProc ~ { button: ViewerClasses.Viewer ~ NARROW[parent]; IF button.name.Equal["ForAll"] THEN Buttons.ReLabel[button, "Single"] ELSE Buttons.ReLabel[button, "ForAll"]; }; FiguresProc: Buttons.ButtonProc ~ { SetTextContents[ViewerFromParent[parent, $Pattern], "Figure ", ""]; }; FootnotesProc: Buttons.ButtonProc ~ { SetTextContents[ViewerFromParent[parent, $Pattern], "!", "m"]; }; ReferencesProc: Buttons.ButtonProc ~ { SetTextContents[ViewerFromParent[parent, $Pattern], "[", ""]; }; SetTextContents: PROC [viewer: ViewerClasses.Viewer, rope: ROPE, looks: ROPE] ~ { IF viewer # NIL THEN { ViewerTools.SetContents[viewer, rope]; IF ~looks.IsEmpty THEN { TiogaOps.SaveSelA[]; TiogaOps.SelectDocument[viewer]; TiogaOps.SetLooks[looks]; TiogaOps.RestoreSelA[]; }; }; }; PatternProc: Buttons.ButtonProc ~ { TextButton[viewer~ViewerFromParent[parent, $Pattern], mouseButton~mouseButton]; }; StartAtProc: Buttons.ButtonProc ~ { TextButton[viewer~ViewerFromParent[parent, $StartAt], mouseButton~mouseButton]; }; BecomesProc: Buttons.ButtonProc ~ { TextButton[viewer~ViewerFromParent[parent, $Becomes], mouseButton~mouseButton]; }; DeltaProc: Buttons.ButtonProc ~ { TextButton[viewer~ViewerFromParent[parent, $Delta], mouseButton~mouseButton]; }; TextButton: PROC [viewer: ViewerClasses.Viewer, mouseButton: Menus.MouseButton] ~ { IF viewer # NIL THEN { IF mouseButton # red THEN ViewerTools.SetContents[viewer, ""]; ViewerTools.SetSelection[viewer]; }; }; renumberViewerClass: ViewerClasses.ViewerClass _ NEW[ViewerClasses.ViewerClassRec _ ViewerOps.FetchViewerClass[$Container]^]; containerInitProc: ViewerClasses.InitProc _ renumberViewerClass.init; renumberViewerClass.init _ InitRenumberViewer; ViewerOps.RegisterViewerClass[$TiogaRenumber, renumberViewerClass]; Commander.Register[key: "RenumberFootnotes", proc: RenumberFootnotesCmd, doc: "RenumberFootnotes document-name {-nxx} where xx is the first footnote to begin renumbering"]; Commander.Register[key: "RenumberFigures", proc: RenumberFiguresCmd, doc: "RenumberFigures {-axx} {-dyy} document-name where xx is the first figure after which to begin renumbering and yy is the increment in the figure numbers"]; Commander.Register[key: "RenumberReferences", proc: RenumberReferencesCmd, doc: "RenumberReferences {-axx} {-dyy} document-name where xx is the first reference after which to begin renumbering and yy is the increment in the figure numbers"]; Commander.Register[key: "Renumber", proc: RenumberCmd, doc: "Renumber -pPattern {-lz} {-axx} {-dyy} document-name where Pattern identifies the thing being renumbered, z is a set of looks to match with the pattern, xx is the first thing after which to begin renumbering and yy is the increment in the figure numbers"]; END. JTiogaRenumber.Mesa Created by Teitelman, April 5, 1984 3:58:02 pm PST Last Edited by: Beach, September 27, 1984 3:52:11 pm PDT A hack to assist in renumbering figures, references, or footnotes. Registers the commands RenumberFootnotes, RenumberFigures, RenumberReferences, and the generic command Renumber. The first three commands search a document for a pattern and renumber following tokens, e.g. footnotes like `!13', figures like `Figure 13', references like `[13]'. The generic command Renumber permits a user to specify all the search parameters. For use when a reference is added or dropped, references are of the form [nn]. For all nn > startingAfter, changes nn to nn + delta. For use when a figure is added or dropped, figures are of the form Figure nn. For all nn > startingAfter, changes nn to nn + delta. For use when a reference is added or dropped, references are of the form [nn]. For all nn > startingAfter, changes nn to nn + delta. PROC [self: Viewer] single step or run through all variations get the pattern and looks from $Pattern viewer do the generic renumbering สŒ˜™Jšœ2™2J™8J™—šฯk ˜ J˜J˜J˜Jšœ ˜ Jšœ ˜ Jšœ˜Jšœ˜Jšœœ˜$Jšœ˜Jšœ ˜ Jšœ˜Jšœ ˜ J˜ J˜J˜J˜—šœ œ˜Jšœœ œ œ1˜kJ˜—šœ˜J˜Jšœœœ˜J˜—šœฯtTฯežŸžŸžŸžpฯmžLŸž5™ญJ˜—š ฯn œ œ œœœœ˜ZJšœ[ œ˜rJšœ˜J˜—š ก œ œ œœ œ˜KJ™„Jšœk˜kJšœ˜J˜—š กœ œ œœ œ˜HJ™ƒJšœq˜qJšœ˜J˜—šกœ œœœ œœœœœœ ˜ฎJ™„Jšœ˜Jšœ1œœ`˜›Jšœœ˜1Jš œ œœœœœ˜(Jšœู˜ูJšœ˜J˜—šกœœ^œ œœœœœœœœœœ˜ƒJšœGฯc&˜mJšœ œœ˜/Jš œ œœ œœ˜2Jš œœœœ œ˜!Jšœ œ˜Jšœ œ˜Jšœœ˜šก œœ˜šœœ˜Jšœ˜Jš œœœœœ˜*Jšœ ˜——J˜Jšœ9˜9J˜š˜Jšœ˜Jšœœ˜ Jšœœ˜Jšœœ.˜9Jšœœœœ˜J˜)šœ œ˜JšœB˜BJ•StartOfExpansion>[s1: ROPE, s2: ROPE, pos1: INT _ 0, case: BOOL _ TRUE]šœ-œœขm˜จJšœ˜—Jšœ œœ'˜6Jšœ!˜#Jšœœ#œ œ˜Kš œœœœœœ˜;šœ&œ˜.Jšœ ˜ Jšœ˜J˜—Jš˜šœ˜ Jšœ˜Jšœ˜Jšœœœ?˜NJ˜—Jšœ˜—Jšœ#œœ˜/JšœXœ˜rJšœB˜BJ˜J˜ Jšœ<˜˜>Jšœ9˜9Jšœ*˜*Jšœ+˜+Jšœœ œ.œ˜_—Jšœ˜J˜—šœ˜Jšœ>˜D—JšœP˜PJšœœœœ˜'Jšœ˜—J˜J˜—š กœœ œœœœ˜TJ˜Jšœœ˜J˜J˜—šกœ˜.Jšœ™J˜Jšœ˜Jšœœ˜Jšœœ˜Jšœœ˜Jšœœ˜Jšœ œ˜Jšœ œข˜6J˜JšœขB˜\J˜Jšœaœ˜išœjœ˜rJ˜;—Jšœnœ˜vJšœrœ˜zJšœtœ˜|J˜Jšœgœ˜ošœyœœ˜“J˜8J˜—Jšœoœ˜wšœxœœ˜’J˜8J˜ J˜—Jšœoœ˜wšœxœœ˜’J˜ J˜8—Jšœkœ˜sšœxœœ˜’J˜6J˜ —J˜J˜J˜J˜—šกœ˜ Jšœ œ˜Jšœœ˜Jšœœ˜Jšœœ˜Jšœœ˜ Jšœ œ˜Jšœ˜J˜Jšœ9˜9J˜J™)Jšœ\˜\J˜šœœ ˜Jšœ,˜,J˜—J™.JšœJ˜JJšœœœ˜"Jšœk˜kJ˜Jšœa˜aJšœa˜aJšœW˜WJ˜J™Jšœy˜yJ˜J˜—š กœœ œœœœ˜ZJšœœœ3˜`š œœœœ˜7Jšœœ˜&—J˜J˜—šกœ˜ Jšœœ ˜.šœ˜#Jšœ!˜!—šœ˜Jšœ"˜"—J˜J˜—šก œ˜#JšœC˜CJ˜J˜—šก œ˜%Jšœ5 œ˜>J˜J˜—šกœ˜&Jšœ=˜=J˜J˜—šกœœ&œ œ˜Qšœ œœ˜Jšœ&˜&šœœ˜J˜Jšœ ˜ J˜J˜Jšœ˜—Jšœ˜—J˜J˜—šก œ˜#JšœO˜OJ˜J˜—šก œ˜#JšœO˜OJ˜J˜—šก œ˜#JšœO˜OJ˜J˜—šก œ˜!JšœM˜MJ˜J˜—šก œœD˜Tšœ œœ˜Jšœœ%˜>J˜"Jšœ˜—J˜J˜—Jšœ1œI˜}JšœE˜EJ˜Jšœ.˜.IunitšœC˜CJ–\[key: ROPE, proc: Commander.CommandProc, doc: ROPE _ NIL, clientData: REF ANY _ NIL]šœฎ˜ฎJ–\[key: ROPE, proc: Commander.CommandProc, doc: ROPE _ NIL, clientData: REF ANY _ NIL]šœ็˜็J–\[key: ROPE, proc: Commander.CommandProc, doc: ROPE _ NIL, clientData: REF ANY _ NIL]šœ๓˜๓J–\[key: ROPE, proc: Commander.CommandProc, doc: ROPE _ NIL, clientData: REF ANY _ NIL]šœฟ˜ฟJšœ˜—…—4„GZ