001: /*
002: * ManMode.java
003: *
004: * Copyright (C) 2000-2004 Peter Graves
005: * $Id: ManMode.java,v 1.4 2004/04/01 18:50:03 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 java.awt.AWTEvent;
025: import java.awt.event.KeyEvent;
026: import java.awt.event.MouseEvent;
027: import javax.swing.JPopupMenu;
028:
029: public final class ManMode extends AbstractMode implements Constants,
030: Mode {
031: private static final ManMode mode = new ManMode();
032:
033: private ManMode() {
034: super (MAN_MODE, MAN_MODE_NAME);
035: setProperty(Property.VERTICAL_RULE, 0);
036: setProperty(Property.SHOW_LINE_NUMBERS, false);
037: setProperty(Property.HIGHLIGHT_MATCHING_BRACKET, false);
038: setProperty(Property.HIGHLIGHT_BRACKETS, false);
039: }
040:
041: public static final ManMode getMode() {
042: return mode;
043: }
044:
045: public JPopupMenu getContextMenu(Editor editor) {
046: return null;
047: }
048:
049: public Formatter getFormatter(Buffer buffer) {
050: if (buffer.getType() != Buffer.TYPE_MAN)
051: return null;
052: return new ManFormatter(buffer, ((Man) buffer).isApropos());
053: }
054:
055: protected void setKeyMapDefaults(KeyMap km) {
056: km.mapKey(KeyEvent.VK_ENTER, 0, "manFollowLink");
057: km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK,
058: "manFollowLink");
059: km.mapKey(VK_DOUBLE_MOUSE_1, 0, "manFollowLink");
060: km.mapKey(VK_MOUSE_2, 0, "manFollowLink");
061: }
062:
063: private static void followLink(Editor editor) {
064: if (editor.getBuffer().getType() == Buffer.TYPE_MAN) {
065: final Man man = (Man) editor.getBuffer();
066: final Line line = editor.getDotLine();
067: if (man.isApropos()) {
068: String topic = null;
069: String section = null;
070: String text = line.getText();
071: int index = text.indexOf(' ');
072: if (index >= 0) {
073: topic = text.substring(0, index);
074: text = text.substring(index);
075: index = text.indexOf('(');
076: if (index >= 0) {
077: int begin = index + 1;
078: int end = text.indexOf(')', begin);
079: if (end >= 0)
080: section = text.substring(begin, end);
081: }
082: }
083: if (topic != null) {
084: if (section != null && section.length() > 0)
085: man(editor, section + " " + topic);
086: else
087: man(editor, topic);
088: }
089: } else {
090: LineSegmentList segmentList = man.getFormatter()
091: .formatLine(line);
092: int col = editor.getDotCol();
093: int startCol = 0;
094: for (int i = 0; i < segmentList.size(); i++) {
095: LineSegment segment = segmentList.getSegment(i);
096: if (startCol <= col
097: && col < startCol + segment.length()) {
098: String s;
099: if (segment.getFormat() != 0) {
100: // Segment is highlighted. Use the whole segment.
101: s = segment.getText();
102: } else {
103: // Not highlighted.
104: s = editor.getFilenameAtDot();
105: }
106: if (s != null) {
107: if (s.startsWith("/")) {
108: // Looks like a Unix filename.
109: Buffer buf = editor.openFile(File
110: .getInstance(s));
111: if (buf != null) {
112: editor.makeNext(buf);
113: editor.activate(buf);
114: }
115: } else
116: man(editor, s);
117: }
118: break;
119: }
120: startCol += segment.length();
121: }
122: }
123: }
124: }
125:
126: public static String getTitle(String topic) {
127: return "man " + topic;
128: }
129:
130: public static void man() {
131: if (!Platform.isPlatformUnix())
132: return;
133: final Editor editor = Editor.currentEditor();
134: ManDialog d = new ManDialog(editor);
135: editor.centerDialog(d);
136: d.show();
137: String topic = d.getInput();
138: if (topic != null && topic.length() != 0)
139: man(topic);
140: }
141:
142: public static void man(String topic) {
143: if (!Platform.isPlatformUnix())
144: return;
145: man(Editor.currentEditor(), topic);
146: }
147:
148: private static void man(Editor editor, String topic) {
149: editor.setWaitCursor();
150: try {
151: final String title = ManMode.getTitle(topic);
152: for (BufferIterator it = new BufferIterator(); it.hasNext();) {
153: Buffer buf = it.nextBuffer();
154: if (buf.getModeId() == MAN_MODE
155: && title.equals(buf.getTitle())) {
156: editor.makeNext(buf);
157: editor.switchToBuffer(buf);
158: return;
159: }
160: }
161: File tempFile = Utilities.getTempFile();
162: String cmd = "man " + topic + " > "
163: + tempFile.canonicalPath();
164: String[] cmdarray = { "/bin/sh", "-c", cmd };
165: try {
166: Process process = Runtime.getRuntime().exec(cmdarray);
167: process.waitFor();
168: } catch (Exception e) {
169: Log.error(e);
170: }
171: if (tempFile.isFile() && tempFile.length() > 0) {
172: Man man = new Man(topic, tempFile);
173: man.load();
174: tempFile.delete();
175: editor.makeNext(man);
176: editor.switchToBuffer(man);
177: } else
178: editor.status("No entry");
179: } finally {
180: editor.setDefaultCursor();
181: }
182: }
183:
184: public static void manFollowLink() {
185: final Editor editor = Editor.currentEditor();
186: final Buffer buffer = editor.getBuffer();
187: if (buffer.getType() != Buffer.TYPE_MAN)
188: return;
189: // If this method is invoked via a mouse event mapping, move dot to
190: // location of mouse click before following link.
191: AWTEvent e = editor.getDispatcher().getLastEvent();
192: if (e instanceof MouseEvent)
193: editor.mouseMoveDotToPoint((MouseEvent) e);
194: followLink(editor);
195: }
196: }
|