PageRenderTime 45ms CodeModel.GetById 40ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1/*
  2 * Toggle_Line_Comment.bsh - a BeanShell macro script for the
  3 * jEdit text editor - toggles comment at beginning of each
  4 * selected line (jEdit version 3.2 or greater required)
  5 * Copyright (C) 2001 John Gellene
  6 * jgellene@nyc.rr.com
  7 * http://community.jedit.org
  8 *
  9 * This program is free software; you can redistribute it and/or
 10 * modify it under the terms of the GNU General Public License
 11 * as published by the Free Software Foundation; either version 2
 12 * of the License, or any later version.
 13 *
 14 * This program is distributed in the hope that it will be useful,
 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 17 * GNU General Public License for more details.
 18 *
 19 * You should have received a copy of the GNU General Public License
 20 * along with the jEdit application; if not, write to the Free Software
 21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 22 *
 23 * This macro is based upon similar code in the TextTools plugin for
 24 * jEdit written by Valery Kondakof.  The code presented here is updated to
 25 * use the selection API first included in jEdit 3.2. It also uses the
 26 * buffer's lineComment token instead of blockComment
 27 *
 28 * This macro requires version 3.2 (or greater) of jEdit.
 29 *
 30 * $Id: Toggle_Line_Comment.bsh 3936 2001-12-21 07:02:14Z spestov $
 31 *
 32 * Checked for jEdit 4.0 API
 33 *
 34 */
 35
 36void toggleLineComment()
 37{
 38	/*
 39	 * Guard for readonly files becuase Buffer.insert()
 40	 * ignores the flag
 41	 *
 42	 */
 43	if(buffer.isReadOnly())
 44	{
 45		Macros.error(view, "This file is read only.");
 46		return;
 47	}
 48	String getRE(String str)
 49	{
 50		commentArray = str.toCharArray();
 51		escapedChars = ".?*()|[]\\";
 52		sbComment = new StringBuffer("^\\s*(");
 53		for (i = 0; i < commentArray.length; ++i)
 54		{
 55			ch = commentArray[i];
 56			if(-1 != escapedChars.indexOf(ch))
 57				sbComment.append('\\');
 58			sbComment.append(ch);
 59		}
 60		sbComment.append(")");
 61		return sbComment.toString();
 62	}
 63
 64	void doComment(int line)
 65	{
 66		offset = textArea.getLineStartOffset(line);
 67		text = textArea.getLineText(line);
 68		try
 69		{
 70			if(text != null)
 71			{
 72				match = super.regExp.getMatch(text);
 73				if (match != null)
 74				{
 75					matchEnd = match.getEndIndex();
 76					replace = match.toString().substring(0,
 77						matchEnd - super.comment.length());
 78					buffer.remove(offset, matchEnd);
 79					buffer.insertString(offset,	replace, null);
 80					return;
 81				}
 82			}
 83			buffer.insert(offset, super.comment);
 84		}
 85		catch(javax.swing.text.BadLocationException bl)
 86		{
 87			Log.log(Log.ERROR, this, bl);
 88		}
 89	}
 90
 91	// main routine of method starts here
 92	comment = buffer.getContextSensitiveProperty(textArea.getCaretPosition(),
 93		"lineComment");
 94
 95	if (comment == null)
 96	{
 97		Macros.error(view, "No line comment token to insert");
 98		return;
 99	}
100	regExpString = getRE(comment);
101	regExp = null;
102	try
103	{
104		regExp = new gnu.regexp.RE(regExpString);
105	}
106	catch (gnu.regexp.REException e)
107	{
108		Log.log(Log.ERROR, this, e);
109		Macros.error(view, "Could not create regexp for locating comments");
110		return;
111	}
112	buffer.beginCompoundEdit();
113	selections = textArea.getSelection();
114	if(selections.length == 0)
115	{
116		doComment(textArea.getCaretLine());
117	}
118	else
119	{
120		for( i = 0; i < selections.length; ++i)
121		{
122			selItem = selections[i];
123			startLine = selItem.getStartLine();
124			endLine = selItem.getEndLine();
125			for (j = startLine; j <= endLine; ++j)
126			{
127				doComment(j);
128			}
129		}
130	}
131	textArea.selectNone();
132}
133
134toggleLineComment();
135
136/*
137	Macro index data (in DocBook format)
138
139<listitem>
140  <para><filename>Toggle_Line_Comment.bsh</filename></para>
141  <abstract><para>
142	  Toggles line comments, alternately inserting and deleting them
143    at the beginning of each selected line.
144  </para></abstract>
145	<para>
146		If there is no selection, the macro operates on the current line.
147	</para>
148</listitem>
149
150*/
151
152// end Toggle_Line_Comment.bsh
153