KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > LispShellFormatter


1 /*
2  * LispShellFormatter.java
3  *
4  * Copyright (C) 2002-2004 Peter Graves
5  * $Id: LispShellFormatter.java,v 1.13 2004/09/08 19:34:26 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 import gnu.regexp.RE;
25 import gnu.regexp.REMatch;
26 import gnu.regexp.UncheckedRE;
27
28 public final class LispShellFormatter extends Formatter
29 {
30     // Formats.
31
private static final byte FORMAT_TEXT = 0;
32     private static final byte FORMAT_COMMENT = 1;
33     private static final byte FORMAT_PROMPT = 2;
34     private static final byte FORMAT_INPUT = 3;
35
36     private final RE defaultPromptRE =
37         new UncheckedRE("^[^>\\*\\]]*[>\\*\\]] *");
38
39     public LispShellFormatter(Buffer buffer)
40     {
41         this.buffer = buffer;
42     }
43
44     public LineSegmentList formatLine(Line line)
45     {
46         clearSegmentList();
47         if (line == null) {
48             addSegment("", FORMAT_TEXT);
49             return segmentList;
50         }
51         final String JavaDoc text = getDetabbedText(line);
52         Annotation a = line.getAnnotation();
53         if (a != null) {
54             // Prompt line.
55
int index = a.getIntegerValue();
56             if (index > 0 && index <= text.length()) {
57                 addSegment(text, 0, index, FORMAT_PROMPT);
58                 addSegment(text, index, FORMAT_INPUT);
59                 return segmentList;
60             }
61         }
62         if (line.flags() == 0) {
63             int promptEnd = getPromptEndIndex(text);
64             if (promptEnd > 0) {
65                 line.setAnnotation(new Annotation(promptEnd));
66                 line.setFlags(STATE_PROMPT);
67                 addSegment(text, 0, promptEnd, FORMAT_PROMPT);
68                 int commentStart = text.indexOf(';', promptEnd);
69                 if (commentStart >= promptEnd) {
70                     addSegment(text, promptEnd, commentStart, FORMAT_INPUT);
71                     addSegment(text, commentStart, FORMAT_COMMENT);
72                 } else
73                     addSegment(text, promptEnd, FORMAT_INPUT);
74                 return segmentList;
75             }
76             // LispShell.enter() calls setFlags(STATE_INPUT), so this line
77
// must be output.
78
line.setFlags(STATE_OUTPUT);
79             // Fall through...
80
}
81         if (text.indexOf(';') == 0) {
82             addSegment(text, FORMAT_COMMENT);
83         } else {
84             int format =
85                 (line.flags() == STATE_INPUT) ? FORMAT_INPUT : FORMAT_TEXT;
86             addSegment(text, format);
87         }
88         return segmentList;
89     }
90
91     private int getPromptEndIndex(String JavaDoc text)
92     {
93         if (text.length() == 0)
94             return 0;
95         String JavaDoc trim = text.trim();
96         if (trim.length() == 0)
97             return 0;
98         if (trim.charAt(0) == '(')
99             return 0; // Don't mistake "(* " for a prompt!
100
if (text.startsWith("> "))
101             return 2;
102         if (text.startsWith("* "))
103             return 2;
104         final RE promptRE;
105         if (buffer instanceof CommandInterpreter)
106             promptRE = ((CommandInterpreter)buffer).getPromptRE();
107         else
108             promptRE = defaultPromptRE;
109         REMatch match = promptRE.getMatch(text);
110         if (match != null)
111             return match.getEndIndex();
112         return 0;
113     }
114
115     public FormatTable getFormatTable()
116     {
117         if (formatTable == null) {
118             formatTable = new FormatTable("LispShellMode");
119             formatTable.addEntryFromPrefs(FORMAT_TEXT, "text");
120             formatTable.addEntryFromPrefs(FORMAT_COMMENT, "comment");
121             formatTable.addEntryFromPrefs(FORMAT_PROMPT, "prompt");
122             formatTable.addEntryFromPrefs(FORMAT_INPUT, "input" );
123         }
124         return formatTable;
125     }
126 }
127
Popular Tags