/jEdit/tags/jedit-4-0-pre3/macros/Text/Toggle_Line_Comment.bsh
Unknown | 153 lines | 143 code | 10 blank | 0 comment | 0 complexity | f12102af6f705c3f7d6646626929dc6f MD5 | raw file
Possible License(s): BSD-3-Clause, AGPL-1.0, Apache-2.0, LGPL-2.0, LGPL-3.0, GPL-2.0, CC-BY-SA-3.0, LGPL-2.1, GPL-3.0, MPL-2.0-no-copyleft-exception, IPL-1.0
- /*
- * 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@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 3936 2001-12-21 07:02:14Z spestov $
- *
- * 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;
- }
- buffer.beginCompoundEdit();
- 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)
- <listitem>
- <para><filename>Toggle_Line_Comment.bsh</filename></para>
- <abstract><para>
- Toggles line comments, alternately inserting and deleting them
- at the beginning of each selected line.
- </para></abstract>
- <para>
- If there is no selection, the macro operates on the current line.
- </para>
- </listitem>
- */
- // end Toggle_Line_Comment.bsh