001: /*
002: * LispShellFormatter.java
003: *
004: * Copyright (C) 2002-2004 Peter Graves
005: * $Id: LispShellFormatter.java,v 1.13 2004/09/08 19:34:26 piso Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.j;
023:
024: import gnu.regexp.RE;
025: import gnu.regexp.REMatch;
026: import gnu.regexp.UncheckedRE;
027:
028: public final class LispShellFormatter extends Formatter {
029: // Formats.
030: private static final byte FORMAT_TEXT = 0;
031: private static final byte FORMAT_COMMENT = 1;
032: private static final byte FORMAT_PROMPT = 2;
033: private static final byte FORMAT_INPUT = 3;
034:
035: private final RE defaultPromptRE = new UncheckedRE(
036: "^[^>\\*\\]]*[>\\*\\]] *");
037:
038: public LispShellFormatter(Buffer buffer) {
039: this .buffer = buffer;
040: }
041:
042: public LineSegmentList formatLine(Line line) {
043: clearSegmentList();
044: if (line == null) {
045: addSegment("", FORMAT_TEXT);
046: return segmentList;
047: }
048: final String text = getDetabbedText(line);
049: Annotation a = line.getAnnotation();
050: if (a != null) {
051: // Prompt line.
052: int index = a.getIntegerValue();
053: if (index > 0 && index <= text.length()) {
054: addSegment(text, 0, index, FORMAT_PROMPT);
055: addSegment(text, index, FORMAT_INPUT);
056: return segmentList;
057: }
058: }
059: if (line.flags() == 0) {
060: int promptEnd = getPromptEndIndex(text);
061: if (promptEnd > 0) {
062: line.setAnnotation(new Annotation(promptEnd));
063: line.setFlags(STATE_PROMPT);
064: addSegment(text, 0, promptEnd, FORMAT_PROMPT);
065: int commentStart = text.indexOf(';', promptEnd);
066: if (commentStart >= promptEnd) {
067: addSegment(text, promptEnd, commentStart,
068: FORMAT_INPUT);
069: addSegment(text, commentStart, FORMAT_COMMENT);
070: } else
071: addSegment(text, promptEnd, FORMAT_INPUT);
072: return segmentList;
073: }
074: // LispShell.enter() calls setFlags(STATE_INPUT), so this line
075: // must be output.
076: line.setFlags(STATE_OUTPUT);
077: // Fall through...
078: }
079: if (text.indexOf(';') == 0) {
080: addSegment(text, FORMAT_COMMENT);
081: } else {
082: int format = (line.flags() == STATE_INPUT) ? FORMAT_INPUT
083: : FORMAT_TEXT;
084: addSegment(text, format);
085: }
086: return segmentList;
087: }
088:
089: private int getPromptEndIndex(String text) {
090: if (text.length() == 0)
091: return 0;
092: String trim = text.trim();
093: if (trim.length() == 0)
094: return 0;
095: if (trim.charAt(0) == '(')
096: return 0; // Don't mistake "(* " for a prompt!
097: if (text.startsWith("> "))
098: return 2;
099: if (text.startsWith("* "))
100: return 2;
101: final RE promptRE;
102: if (buffer instanceof CommandInterpreter)
103: promptRE = ((CommandInterpreter) buffer).getPromptRE();
104: else
105: promptRE = defaultPromptRE;
106: REMatch match = promptRE.getMatch(text);
107: if (match != null)
108: return match.getEndIndex();
109: return 0;
110: }
111:
112: public FormatTable getFormatTable() {
113: if (formatTable == null) {
114: formatTable = new FormatTable("LispShellMode");
115: formatTable.addEntryFromPrefs(FORMAT_TEXT, "text");
116: formatTable.addEntryFromPrefs(FORMAT_COMMENT, "comment");
117: formatTable.addEntryFromPrefs(FORMAT_PROMPT, "prompt");
118: formatTable.addEntryFromPrefs(FORMAT_INPUT, "input");
119: }
120: return formatTable;
121: }
122: }
|