App-Codit

 view release on metacpan or  search on metacpan

lib/App/Codit/Plugins/Snippets.pm  view on Meta::CPAN

use warnings;
use vars qw( $VERSION );
$VERSION = '0.19';

use Carp;

use base qw( Tk::AppWindow::BaseClasses::Plugin );

require Tk::HList;
require Tk::XText;
use File::Path qw(make_path);

=head1 DESCRIPTION

Quick and easy code samples.

=head1 DETAILS

Snippets are shorts pieces of code that you find yourself writing over and over again.
The top side of the panel allows you to manage your collection of snippets.
The bottom side allows you to insert a selected snippet into your document,
copy the snippet to the clipboard or create a new document based on the snippet.
A file dialog is launched and the snippet is saved to the selected file name.
Then it is opened in a new tab.

=cut

sub new {
	my $class = shift;
	my $self = $class->SUPER::new(@_);
	return undef unless defined $self;
	
	$self->{CURRENT} = undef;
#	my $tp = $self->extGet('ToolPanel');
#	my $page = $tp->addPage('Snippets', 'insert-text', undef, 'Snippets');
	my $page = $self->ToolRightPageAdd('Snippets', 'insert-text', undef, 'Snippets', 350);

	my @padding = (-padx => 2, -pady => 2);

	my $lf = $page->Frame(
		-relief => 'groove',
		-borderwidth => 2,
	)->pack(@padding, -fill => 'x');
	
	$lf->Button(
		-text => 'New',
		-command => ['snippetNew', $self],
	)->pack(@padding, -fill => 'x');
	$lf->Button(
		-text => 'Copy to',
		-command => ['snippetCopy', $self],
	)->pack(@padding, -fill=> 'x');
	$lf->Button(
		-text => 'Delete',
		-command => ['snippetDelete', $self],
	)->pack(@padding, -fill => 'x');
	my $hlist = $lf->Scrolled('HList',
		-browsecmd => ['listSelect', $self],
		-height => 4,
		-scrollbars => 'osoe',
	)->pack(@padding, -expand => 1, -fill => 'both');
	$self->{LIST} = $hlist;

	$page->Adjuster(
		-side => 'top',
		-widget => $lf,
	)->pack(-fill => 'x');
	
	my $sf = $page->Frame(
		-relief => 'groove',
		-borderwidth => 2,
	)->pack(@padding, -expand => 1, -fill => 'both');
	$sf->Button(
		-text => 'Insert',
		-command => ['snippetInsert', $self],
	)->pack(@padding, -fill => 'x');
	$sf->Button(
		-text => 'Clipboard',
		-command => ['snippetClipboard', $self],
	)->pack(@padding, -fill => 'x');
	$sf->Button(
		-text => 'Create',
		-command => ['snippetCreate', $self],
	)->pack(@padding, -fill => 'x');
	my @to = ();
	my $text = $sf->Scrolled('XText', @to,
		-scrollbars => 'osoe',
		-tabs => '8m',
		-wrap => 'none',
		-height => 4,
		-width => 20,
	)->pack(@padding, -expand => 1, -fill => 'both');
	$self->after(200, sub {
		$text->configure('-font', $self->mdi->docWidget->cget('-font'))
	});
	$self->{TEXT} = $text;

	$self->listRefresh;
	return $self;
}

sub _list {
	return $_[0]->{LIST}
}

sub _text {
	return $_[0]->{TEXT}
}

sub current {
	my $self = shift;
	$self->{CURRENT} = shift if @_;
	return $self->{CURRENT}
}

sub itemName {
	my ($self, $item) = @_;
	$item =~ s/\./`/g;
	return $item
}

sub itemText {
	my ($self, $item) = @_;
	return $self->_list->entrycget($item, '-text');
}

sub listSelect {
	my ($self, $item) = @_;
	croak "Item not defined" unless defined $item;
	my $cur = $self->current;
	$self->snippetSave if defined $cur;
	$self->current($item);
	$self->snippetLoad;
}

sub listRefresh {
	my $self = shift;
	my $folder = $self->snippetsFolder;
	my $dh;
	unless (opendir($dh, $folder)) {
		croak "cannot open folder $folder";
		return
	}
	$self->snippetSave;
	my $l = $self->_list;
	$l->deleteAll;
	$self->current(undef);
	$self->_text->clear;
	while (my $i = readdir($dh)) {
		next if $i eq '.';
		next if $i eq '..';
		$self->snippetAdd($i);



( run in 0.472 second using v1.01-cache-2.11-cpan-5b529ec07f3 )