/* * Toggle_Line_Comment.bsh - a BeanShell macro script for the * jEdit text editor - toggles comment at beginning of each * selected line (jEdit version 3.2 or greater required) * Copyright (C) 2001 John Gellene * jgellene at nyc . rr . com * http://community.jedit.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with the jEdit application; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * This macro is based upon similar code in the TextTools plugin for * jEdit written by Valery Kondakof. The code presented here is updated to * use the selection API first included in jEdit 3.2. It also uses the * buffer's lineComment token instead of blockComment * * This macro requires version 3.2 (or greater) of jEdit. * * $Id: Toggle_Line_Comment.bsh,v 1.5 2002/02/26 01:27:36 spestov Exp $ * * Checked for jEdit 4.0 API * */ void toggleLineComment() { /* * Guard for readonly files becuase Buffer.insert() * ignores the flag * */ if(buffer.isReadOnly()) { Macros.error(view, "This file is read only."); return; } String getRE(String str) { commentArray = str.toCharArray(); escapedChars = ".?*()|[]\\"; sbComment = new StringBuffer("^\\s*("); for (i = 0; i < commentArray.length; ++i) { ch = commentArray[i]; if(-1 != escapedChars.indexOf(ch)) sbComment.append('\\'); sbComment.append(ch); } sbComment.append(")"); return sbComment.toString(); } void doComment(int line) { offset = textArea.getLineStartOffset(line); text = textArea.getLineText(line); try { if(text != null) { match = super.regExp.getMatch(text); if (match != null) { matchEnd = match.getEndIndex(); replace = match.toString().substring(0, matchEnd - super.comment.length()); buffer.remove(offset, matchEnd); buffer.insertString(offset, replace, null); return; } } buffer.insert(offset, super.comment); } catch(javax.swing.text.BadLocationException bl) { Log.log(Log.ERROR, this, bl); } } // main routine of method starts here comment = buffer.getContextSensitiveProperty(textArea.getCaretPosition(), "lineComment"); if (comment == null) { Macros.error(view, "No line comment token to insert"); return; } regExpString = getRE(comment); regExp = null; try { regExp = new gnu.regexp.RE(regExpString); } catch (gnu.regexp.REException e) { Log.log(Log.ERROR, this, e); Macros.error(view, "Could not create regexp for locating comments"); return; } selections = textArea.getSelection(); if(selections.length == 0) { doComment(textArea.getCaretLine()); } else { for( i = 0; i < selections.length; ++i) { selItem = selections[i]; startLine = selItem.getStartLine(); endLine = selItem.getEndLine(); for (j = startLine; j <= endLine; ++j) { doComment(j); } } } textArea.selectNone(); } toggleLineComment(); /* Macro index data (in DocBook format) Toggle_Line_Comment.bsh Toggles line comments, alternately inserting and deleting them at the beginning of each selected line. If there is no selection, the macro operates on the current line. */ // end Toggle_Line_Comment.bsh