KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > gnu > jemacs > lang > SaveExcursion


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

75   public static long savePointMark(Buffer buffer)
76   {
77     return buffer.savePointMark();
78   }
79
80   public static void restoreBufferPointMark(Buffer buffer, long pointMark)
81   {
82     Buffer.setCurrent(buffer);
83     buffer.restorePointMark(pointMark);
84   }
85 }
86
Popular Tags