1 21 22 package org.armedbear.j; 23 24 import java.awt.AWTEvent ; 25 import java.awt.event.KeyEvent ; 26 import java.awt.event.MouseEvent ; 27 import javax.swing.JPopupMenu ; 28 29 public final class ManMode extends AbstractMode implements Constants, Mode 30 { 31 private static final ManMode mode = new ManMode(); 32 33 private ManMode() 34 { 35 super(MAN_MODE, MAN_MODE_NAME); 36 setProperty(Property.VERTICAL_RULE, 0); 37 setProperty(Property.SHOW_LINE_NUMBERS, false); 38 setProperty(Property.HIGHLIGHT_MATCHING_BRACKET, false); 39 setProperty(Property.HIGHLIGHT_BRACKETS, false); 40 } 41 42 public static final ManMode getMode() 43 { 44 return mode; 45 } 46 47 public JPopupMenu getContextMenu(Editor editor) 48 { 49 return null; 50 } 51 52 public Formatter getFormatter(Buffer buffer) 53 { 54 if (buffer.getType() != Buffer.TYPE_MAN) 55 return null; 56 return new ManFormatter(buffer, ((Man)buffer).isApropos()); 57 } 58 59 protected void setKeyMapDefaults(KeyMap km) 60 { 61 km.mapKey(KeyEvent.VK_ENTER, 0, "manFollowLink"); 62 km.mapKey(KeyEvent.VK_G, CTRL_MASK | SHIFT_MASK, "manFollowLink"); 63 km.mapKey(VK_DOUBLE_MOUSE_1, 0, "manFollowLink"); 64 km.mapKey(VK_MOUSE_2, 0, "manFollowLink"); 65 } 66 67 private static void followLink(Editor editor) 68 { 69 if (editor.getBuffer().getType() == Buffer.TYPE_MAN) { 70 final Man man = (Man) editor.getBuffer(); 71 final Line line = editor.getDotLine(); 72 if (man.isApropos()) { 73 String topic = null; 74 String section = null; 75 String text = line.getText(); 76 int index = text.indexOf(' '); 77 if (index >= 0) { 78 topic = text.substring(0, index); 79 text = text.substring(index); 80 index = text.indexOf('('); 81 if (index >= 0) { 82 int begin = index + 1; 83 int end = text.indexOf(')', begin); 84 if (end >= 0) 85 section = text.substring(begin, end); 86 } 87 } 88 if (topic != null) { 89 if (section != null && section.length() > 0) 90 man(editor, section + " " + topic); 91 else 92 man(editor, topic); 93 } 94 } else { 95 LineSegmentList segmentList = man.getFormatter().formatLine(line); 96 int col = editor.getDotCol(); 97 int startCol = 0; 98 for (int i = 0; i < segmentList.size(); i++) { 99 LineSegment segment = segmentList.getSegment(i); 100 if (startCol <= col && col < startCol + segment.length()) { 101 String s; 102 if (segment.getFormat() != 0) { 103 s = segment.getText(); 105 } else { 106 s = editor.getFilenameAtDot(); 108 } 109 if (s != null) { 110 if (s.startsWith("/")) { 111 Buffer buf = editor.openFile(File.getInstance(s)); 113 if (buf != null) { 114 editor.makeNext(buf); 115 editor.activate(buf); 116 } 117 } else 118 man(editor, s); 119 } 120 break; 121 } 122 startCol += segment.length(); 123 } 124 } 125 } 126 } 127 128 public static String getTitle(String topic) 129 { 130 return "man " + topic; 131 } 132 133 public static void man() 134 { 135 if (!Platform.isPlatformUnix()) 136 return; 137 final Editor editor = Editor.currentEditor(); 138 ManDialog d = new ManDialog(editor); 139 editor.centerDialog(d); 140 d.show(); 141 String topic = d.getInput(); 142 if (topic != null && topic.length() != 0) 143 man(topic); 144 } 145 146 public static void man(String topic) 147 { 148 if (!Platform.isPlatformUnix()) 149 return; 150 man(Editor.currentEditor(), topic); 151 } 152 153 private static void man(Editor editor, String topic) 154 { 155 editor.setWaitCursor(); 156 try { 157 final String title = ManMode.getTitle(topic); 158 for (BufferIterator it = new BufferIterator(); it.hasNext();) { 159 Buffer buf = it.nextBuffer(); 160 if (buf.getModeId() == MAN_MODE && title.equals(buf.getTitle())) { 161 editor.makeNext(buf); 162 editor.switchToBuffer(buf); 163 return; 164 } 165 } 166 File tempFile = Utilities.getTempFile(); 167 String cmd = "man " + topic + " > " + tempFile.canonicalPath(); 168 String [] cmdarray = {"/bin/sh", "-c", cmd}; 169 try { 170 Process process = Runtime.getRuntime().exec(cmdarray); 171 process.waitFor(); 172 } 173 catch (Exception e) { 174 Log.error(e); 175 } 176 if (tempFile.isFile() && tempFile.length() > 0) { 177 Man man = new Man(topic, tempFile); 178 man.load(); 179 tempFile.delete(); 180 editor.makeNext(man); 181 editor.switchToBuffer(man); 182 } else 183 editor.status("No entry"); 184 } 185 finally { 186 editor.setDefaultCursor(); 187 } 188 } 189 190 public static void manFollowLink() 191 { 192 final Editor editor = Editor.currentEditor(); 193 final Buffer buffer = editor.getBuffer(); 194 if (buffer.getType() != Buffer.TYPE_MAN) 195 return; 196 AWTEvent e = editor.getDispatcher().getLastEvent(); 199 if (e instanceof MouseEvent ) 200 editor.mouseMoveDotToPoint((MouseEvent )e); 201 followLink(editor); 202 } 203 } 204 | Popular Tags |