# notes.pl by arza : Note buffer # This program is free software: you can modify/redistribute it under the terms of # GNU General Public License by Free Software Foundation, either version 3 or later # which you can get from . # This program is distributed in the hope that it will be useful, but without any warranty. weechat::register("notes", "arza ", "0.1", "GPL3", "A buffer for notes", "save", ""); weechat::hook_command("notes", "A buffer for notes", "[-clear] [-del] [-kill] [-reload] [-restore] [-save] [file]", " - Open a note buffer / switch to it if it's open / close it if it's current\n". " - Do action in note buffer\n". " - Load a file", "", "notes_cmd", ""); # This script creates a note buffer and syncs it with a file at $weechat_dir/notes # Functions: # # - load file->array # - save array->file # # - restore array->buffer # # - delete last line in buffer and array # - clear buffer and array my $file; my $filename=weechat::info_get("weechat_dir", "")."/notes"; my $b=weechat::color("bold"); my $r=weechat::color("reset"); my $title="Note buffer | Ctrl keys: ${b}c${r}lear, ${b}d${r}elete, ${b}k${r}ill, ${b}o${r}pen, ${b}r${r}eload, ${b}s${r}ave |"; my @notes; my $pos; open $file, ">>", $filename; close $file; load(); sub load { if(!$filename){ weechat::print("", weechat::prefix("error")."Notes: No file."); return weechat::WEECHAT_RC_ERROR; }elsif(-e $filename && !-r $filename){ weechat::print("", weechat::prefix("error")."Notes: $filename isn't readable."); return 0; }elsif(-e $filename && !-w $filename){ weechat::print("", weechat::prefix("error")."Notes: $filename isn't writable."); } weechat::command("", "/notes -clear"); open $file, "<", $filename; @notes = <$file>; close $file; $pos=$#notes+1; weechat::command("", "/notes -restore"); } sub save { if(!$filename){ weechat::print("", weechat::prefix("error")."Notes: No file."); return weechat::WEECHAT_RC_ERROR; } if(!-w $filename){ weechat::print("", weechat::prefix("error")."Notes: $filename isn't writable."); return weechat::WEECHAT_RC_ERROR; } open $file, ">", $filename; print $file "$_" for @notes; close $file; } sub notes_cmd { # /notes command my $buffer=weechat::buffer_search("perl", "notes"); # find the buffer if($_[2] eq "-clear"){ # functions weechat::buffer_clear($buffer); @notes=(); $pos=0; }elsif($_[2] eq "-del"){ pop @notes; weechat::buffer_clear($buffer); weechat::command("", "/notes -restore"); }elsif($_[2] eq "-restore"){ if($buffer){ my $i=0; foreach(@notes){ weechat::print_y($buffer, $i, $_); $i++; }; $pos=$#notes+1; } }elsif($_[2] eq "-reload"){ load; weechat::buffer_clear($buffer); weechat::command("", "/notes -restore"); }elsif($_[2] eq "-save"){ save; }elsif($_[2] eq "-kill"){ weechat::buffer_clear($buffer); @notes=(); $pos=0; weechat::buffer_set($buffer, "title", "$title"); $filename=undef; }elsif($_[2]){ #if(!$_[2]){ # weechat::print("", weechat::prefix("error")."Notes: No file."); # return weechat::WEECHAT_RC_ERROR; #}els $filename=$_[2]; if(-e $filename && !-r $filename){ weechat::print("", weechat::prefix("error")."Notes: $filename isn't readable."); return weechat::WEECHAT_RC_ERROR; }elsif(-e $filename && !-w $filename){ weechat::print("", weechat::prefix("error")."Notes: $filename isn't writable."); } open $file, ">>", $filename; close $file; load; weechat::buffer_set($buffer, "title", "$title $filename"); }else{ if(!$buffer){ # if not found $buffer=weechat::buffer_new("notes", "notes_in", "", "", ""); # create it weechat::buffer_set($buffer, "title", "$title $filename"); # set title weechat::buffer_set($buffer, "time_for_each_line", "0"); # no timestamps weechat::buffer_set($buffer, "display", 1); # switch to it weechat::buffer_set($buffer, "type", "free"); weechat::buffer_set($buffer, "key_bind_ctrl-D", "/notes -del"); # keys weechat::buffer_set($buffer, "key_bind_ctrl-C", "/notes -clear"); weechat::buffer_set($buffer, "key_bind_ctrl-R", "/notes -reload"); weechat::buffer_set($buffer, "key_bind_ctrl-S", "/notes -save"); weechat::buffer_set($buffer, "key_bind_ctrl-K", "/notes -kill"); weechat::buffer_set($buffer, "key_bind_ctrl-O", "/input insert /notes\\x20"); weechat::command("", "/notes -restore"); }elsif(weechat::current_buffer() eq $buffer){ weechat::buffer_close($buffer); # if it's current, close it }else{ weechat::buffer_set($buffer, "display", 1); # if it isn't, switch to it } } return weechat::WEECHAT_RC_OK; } sub notes_in { # text written in the buffer push(@notes, $_[2]."\n"); weechat::print_y($_[1], $#notes, $_[2]); return weechat::WEECHAT_RC_OK; }