/*
 * Cut_Lines_Containing.bsh - Cuts lines from current buffer that
 * contain a user-supplied string to the clipboard.
 * 
 * Copyright (C) 2004 Ollie Rutherfurd <oliver@jedit.org>
 *
 * $Id: Cut_Lines_Containing.bsh 21353 2012-03-14 09:46:51Z jojaba_67 $
 */

// Localization
final static String CutLinesContainingLabel = jEdit.getProperty("macro.rs.CutLinesContaining.CutLinesContaining.label", "Cut lines containing:");
final static String LinesCutMessage = jEdit.getProperty("macro.rs.CutLinesContaining.LinesCut.message", "line(s) cut");

// Process 
cutLinesContaining(){
	String text = Macros.input(view, CutLinesContainingLabel);
	if(text == null || "".equals(text))
		return;
	int count = 0;
	int start = 0;
	int end = 0;
	StringBuffer buff = new StringBuffer();
	for(int i = buffer.getLineCount() - 1 ; i >= 0; i--){
		String line = buffer.getLineText(i);
		if(line.indexOf(text) > -1){
			buff.insert(0,'\n').insert(0,line);
			int start = buffer.getLineStartOffset(i);
			int end = buffer.getLineEndOffset(i);
			buffer.remove(start,Math.min(end,buffer.getLength())-start);
			count++;
		}
	}
	Registers.setRegister('$',buff.toString());
	HistoryModel.getModel("clipboard").addItem(buff.toString());
	view.getStatus().setMessageAndClear(count + " " + LinesCutMessage);
}

if(buffer.isReadOnly())
	Toolkit.getDefaultToolkit().beep();
else
	cutLinesContaining();
