01: package gnu.jemacs.lang;
02:
03: import kawa.lang.*;
04: import gnu.expr.*;
05: import gnu.bytecode.*;
06: import gnu.jemacs.buffer.*;
07: import gnu.kawa.reflect.Invoke;
08:
09: public class SaveExcursion extends Syntax {
10: boolean bufferOnly;
11:
12: public static ClassType typeSaveExcursion = ClassType
13: .make("gnu.jemacs.lang.SaveExcursion");
14: public static ClassType typeBuffer = ClassType
15: .make("gnu.jemacs.buffer.Buffer");
16:
17: public SaveExcursion(boolean bufferOnly) {
18: this .bufferOnly = bufferOnly;
19: }
20:
21: public Expression rewrite(Object obj, Translator tr) {
22: Expression[] inits1 = new Expression[1];
23: inits1[0] = Invoke.makeInvokeStatic(typeBuffer, "getCurrent",
24: Expression.noExpressions);
25: LetExp let1 = new LetExp(inits1);
26: Declaration savedBuffer = let1.addDeclaration(null, typeBuffer);
27: savedBuffer.noteValue(inits1[0]);
28: Declaration savedPointMark;
29: LetExp let2;
30: tr.push(let1);
31: if (bufferOnly) {
32: savedPointMark = null;
33: let2 = let1;
34: } else {
35: Expression[] inits2 = new Expression[1];
36: let2 = new LetExp(inits2);
37: savedPointMark = let2.addDeclaration(null, Type.long_type);
38: Expression[] args = new Expression[1];
39: args[0] = new ReferenceExp(savedBuffer);
40: inits2[0] = Invoke.makeInvokeStatic(typeSaveExcursion,
41: "savePointMark", args);
42: savedBuffer.noteValue(inits2[0]);
43: tr.push(let2);
44: }
45: Expression body = tr.rewrite_body(obj);
46: Expression finalizer;
47: if (bufferOnly) {
48: Expression[] args = new Expression[1];
49: args[0] = new ReferenceExp(savedBuffer);
50: finalizer = Invoke.makeInvokeStatic(typeBuffer,
51: "setBuffer", args);
52: } else {
53: tr.pop(let2);
54: let1.body = let2;
55: Expression[] args = new Expression[2];
56: args[0] = new ReferenceExp(savedBuffer);
57: args[1] = new ReferenceExp(savedPointMark);
58: finalizer = Invoke.makeInvokeStatic(typeSaveExcursion,
59: "restoreBufferPointMark", args);
60: }
61: tr.pop(let1);
62: let2.body = new TryExp(body, finalizer);
63: return let1;
64: }
65:
66: /** Save point and (in the future) mark of a buffer.
67: * Returns a pair (packed in a long) of buffer posistions. */
68: public static long savePointMark(Buffer buffer) {
69: return buffer.savePointMark();
70: }
71:
72: public static void restoreBufferPointMark(Buffer buffer,
73: long pointMark) {
74: Buffer.setCurrent(buffer);
75: buffer.restorePointMark(pointMark);
76: }
77: }
|