root/trunk/Padre/lib/Padre/Action/Edit.pm @ 11105

Revision 11105, 17.9 KB (checked in by karl.forner, 6 months ago)

Development related to the braces highlighting and matching.

Added some methods to enforce a uniform definition of what is a brace (so currently it is {}[](). All features using braces share this same definition :

  • highlighting
  • goto matching brace
  • select to matching brace

Moved all related code into Editor.pm.

Modified goto matching brace (CTRL+1) and select to matching brace in order to:

  • use the defined braces
  • respect the inside/outsideness of starting point, meaning that is the cursor is inside the braces; its detination point is also inside
  • the select to matching brace should also now work properly if there is an existing selection to the right of a closing brace
Line 
1package Padre::Action::Edit;
2
3# Fully encapsulated Edit menu
4
5use 5.008;
6use strict;
7use warnings;
8use Padre::Current qw{_CURRENT};
9use Padre::Wx       ();
10use Padre::Wx::Menu ();
11
12our $VERSION = '0.58';
13
14
15#####################################################################
16# Padre::Wx::Menu Methods
17
18sub new {
19        my $class = shift;
20        my $main  = shift;
21
22        my $self = bless {}, $class;
23
24        # Add additional properties
25        $self->{main} = $main;
26
27        # Undo/Redo
28        Padre::Action->new(
29                name        => 'edit.undo',
30                id          => Wx::wxID_UNDO,
31                need_editor => 1,
32
33                #               need        => sub {
34                #                       my %objects = @_;
35                #                       return 0 if !defined( $objects{editor} );
36                #                       return $objects{editor}->CanUndo;
37                #               },
38                label      => Wx::gettext('&Undo'),
39                comment    => Wx::gettext('Undo last change in current file'),
40                shortcut   => 'Ctrl-Z',
41                toolbar    => 'actions/edit-undo',
42                menu_event => sub {
43                        my $editor = Padre::Current->editor or return;
44                        $editor->Undo;
45                },
46        );
47
48        Padre::Action->new(
49                name        => 'edit.redo',
50                id          => Wx::wxID_REDO,
51                need_editor => 1,
52
53                #               need        => sub {
54                #                       my %objects = @_;
55                #                       return 0 if !defined( $objects{editor} );
56                #                       return $objects{editor}->CanRedo;
57                #               },
58                label      => Wx::gettext('&Redo'),
59                comment    => Wx::gettext('Redo last undo'),
60                shortcut   => 'Ctrl-Y',
61                toolbar    => 'actions/edit-redo',
62                menu_event => sub {
63                        my $editor = Padre::Current->editor or return;
64                        $editor->Redo;
65                },
66        );
67
68        Padre::Action->new(
69                name        => 'edit.select_all',
70                id          => Wx::wxID_SELECTALL,
71                need_editor => 1,
72                label       => Wx::gettext('Select all'),
73                comment     => Wx::gettext('Select all the text in the current document'),
74                shortcut    => 'Ctrl-A',
75                toolbar     => 'actions/edit-select-all',
76                menu_event  => sub {
77                        require Padre::Wx::Editor;
78                        Padre::Wx::Editor::text_select_all(@_);
79                },
80        );
81
82        Padre::Action->new(
83                name        => 'edit.mark_selection_start',
84                need_editor => 1,
85                label       => Wx::gettext('Mark selection start'),
86                comment     => Wx::gettext('Mark the place where the selection should start'),
87                shortcut    => 'Ctrl-[',
88                menu_event  => sub {
89                        my $editor = Padre::Current->editor or return;
90                        $editor->text_selection_mark_start;
91                },
92        );
93
94        Padre::Action->new(
95                name        => 'edit.mark_selection_end',
96                need_editor => 1,
97                label       => Wx::gettext('Mark selection end'),
98                comment     => Wx::gettext('Mark the place where the selection should end'),
99                shortcut    => 'Ctrl-]',
100                menu_event  => sub {
101                        my $editor = Padre::Current->editor or return;
102                        $editor->text_selection_mark_end;
103                },
104        );
105
106        Padre::Action->new(
107                name        => 'edit.clear_selection_marks',
108                need_editor => 1,
109                label       => Wx::gettext('Clear selection marks'),
110                comment     => Wx::gettext('Remove all the selection marks'),
111                menu_event  => sub {
112                        require Padre::Wx::Editor;
113                        Padre::Wx::Editor::text_selection_clear_marks(@_);
114                },
115        );
116
117        # Cut and Paste
118        Padre::Action->new(
119                name           => 'edit.cut',
120                id             => Wx::wxID_CUT,
121                need_editor    => 1,
122                need_selection => 1,
123                label          => Wx::gettext('Cu&t'),
124                comment        => Wx::gettext('Remove the current selection and put it in the clipboard'),
125                shortcut       => 'Ctrl-X',
126                toolbar        => 'actions/edit-cut',
127                menu_event     => sub {
128                        my $editor = Padre::Current->editor or return;
129                        $editor->Cut;
130                },
131        );
132
133        Padre::Action->new(
134                name           => 'edit.copy',
135                id             => Wx::wxID_COPY,
136                need_editor    => 1,
137                need_selection => 1,
138                label          => Wx::gettext('&Copy'),
139                comment        => Wx::gettext('Put the current selection in the clipboard'),
140                shortcut       => 'Ctrl-C',
141                toolbar        => 'actions/edit-copy',
142                menu_event     => sub {
143                        my $editor = Padre::Current->editor or return;
144                        $editor->Copy;
145                },
146        );
147
148        # Special copy
149
150        Padre::Action->new(
151                name        => 'edit.copy_filename',
152                need_editor => 1,
153                need_file   => 1,
154                label       => Wx::gettext('Copy full filename'),
155                comment     => Wx::gettext('Put the full path of the current file in the clipboard'),
156                menu_event  => sub {
157                        my $document = Padre::Current->document;
158                        return if !defined( $document->{file} );
159                        my $editor = Padre::Current->editor;
160                        $editor->put_text_to_clipboard( $document->{file}->{filename} );
161                },
162        );
163
164        Padre::Action->new(
165                name        => 'edit.copy_basename',
166                need_editor => 1,
167                need_file   => 1,
168                label       => Wx::gettext('Copy filename'),
169                comment     => Wx::gettext('Put the name of the current file in the clipboard'),
170                menu_event  => sub {
171                        my $document = Padre::Current->document;
172                        return if !defined( $document->{file} );
173                        my $editor = Padre::Current->editor;
174                        $editor->put_text_to_clipboard( $document->{file}->basename );
175                },
176        );
177
178        Padre::Action->new(
179                name        => 'edit.copy_dirname',
180                need_file   => 1,
181                need_editor => 1,
182                label       => Wx::gettext('Copy directory name'),
183                comment     => Wx::gettext('Put the full path of the directory of the current file in the clipboard'),
184                menu_event  => sub {
185                        my $document = Padre::Current->document;
186                        return if !defined( $document->{file} );
187                        my $editor = Padre::Current->editor;
188                        $editor->put_text_to_clipboard( $document->{file}->dirname );
189                },
190        );
191
192        Padre::Action->new(
193                name        => 'edit.copy_content',
194                need_editor => 1,
195                label       => Wx::gettext('Copy editor content'),
196                comment     => Wx::gettext('Put the content of the current document in the clipboard'),
197                menu_event  => sub {
198                        my $document = Padre::Current->document;
199                        return if !defined( $document->{file} );
200                        my $editor = Padre::Current->editor;
201                        $editor->put_text_to_clipboard( $document->text_get );
202                },
203        );
204
205        # Paste
206        Padre::Action->new(
207                name        => 'edit.paste',
208                need_editor => 1,
209                id          => Wx::wxID_PASTE,
210                label       => Wx::gettext('&Paste'),
211                comment     => Wx::gettext('Paste the clipboard to the current location'),
212                shortcut    => 'Ctrl-V',
213                toolbar     => 'actions/edit-paste',
214                menu_event  => sub {
215                        my $editor = Padre::Current->editor or return;
216                        $editor->Paste;
217                },
218        );
219
220        # Miscellaneous Actions
221        Padre::Action->new(
222                name       => 'edit.goto',
223                label      => Wx::gettext('&Goto'),
224                comment    => Wx::gettext('Ask the user for a line number or a character position and jump there'),
225                shortcut   => 'Ctrl-G',
226                menu_event => sub {
227                        Padre::Wx::Main::on_goto(@_);
228                },
229        );
230
231        Padre::Action->new(
232                name        => 'edit.next_problem',
233                need_editor => 1,
234                label       => Wx::gettext('&Next Problem'),
235                comment     => Wx::gettext('Jump to the code that triggered the next error'),
236                shortcut    => 'Ctrl-.',
237                menu_event  => sub {
238                        $main->{syntax}->select_next_problem if $main->{syntax};
239                },
240        );
241
242        Padre::Action->new(
243                name        => 'edit.quick_fix',
244                need_editor => 1,
245                label       => Wx::gettext('&Quick Fix'),
246                comment     => Wx::gettext('Apply one of the quick fixes for the current document'),
247                shortcut    => 'Ctrl-2',
248                menu_event  => sub {
249
250                        my $doc = Padre::Current->document;
251                        return if not $doc;
252                        my $editor = $doc->editor;
253                        $editor->AutoCompSetSeparator( ord '|' );
254                        my @list  = ();
255                        my @items = ();
256                        eval {
257
258                                # Find available quick fixes from provider
259                                my $provider = $doc->get_quick_fix_provider;
260                                @items = $provider->quick_fix_list( $doc, $editor );
261
262                                # Add quick list items from document's quick fix provider
263                                foreach my $item (@items) {
264                                        push @list, $item->{text};
265                                }
266                        };
267                        if ($@) {
268                                warn "Error while calling get_quick_fix_provider: $@\n";
269                        }
270                        my $empty_list = ( scalar @list == 0 );
271                        if ($empty_list) {
272                                @list = ( Wx::gettext('No suggestions') );
273                        }
274                        my $words = join( '|', @list );
275                        Wx::Event::EVT_STC_USERLISTSELECTION(
276                                $main, $editor,
277                                sub {
278                                        my ( $self, $event ) = @_;
279                                        return if $empty_list;
280                                        my $text = $event->GetText;
281                                        my $selection;
282                                        foreach my $item (@items) {
283                                                if ( $item->{text} eq $text ) {
284                                                        $selection = $item;
285                                                        last;
286                                                }
287                                        }
288                                        if ($selection) {
289                                                eval { &{ $selection->{listener} }(); };
290                                                if ($@) {
291                                                        warn "Failed while calling Quick fix " . $selection->{text} . "\n";
292                                                }
293                                        }
294                                },
295                        );
296                        $editor->UserListShow( 1, $words );
297                },
298        );
299
300        Padre::Action->new(
301                name        => 'edit.autocomp',
302                need_editor => 1,
303                label       => Wx::gettext('&AutoComplete'),
304                comment     => Wx::gettext('Offer completions to the current string. See Preferences'),
305                shortcut    => 'Ctrl-Space',
306                menu_event  => sub {
307                        Padre::Wx::Main::on_autocompletion(@_);
308                },
309        );
310
311        Padre::Action->new(
312                name        => 'edit.brace_match',
313                need_editor => 1,
314                label       => Wx::gettext('&Brace matching'),
315                comment     => Wx::gettext('Jump to the matching opening or closing brace: { }, ( ), [ ], < >'),
316                shortcut    => 'Ctrl-1',
317                menu_event  => sub {
318                        Padre::Wx::Main::on_brace_matching(@_);
319                },
320        );
321       
322        Padre::Action->new(
323                name        => 'edit.brace_match_select',
324                need_editor => 1,
325                label       => Wx::gettext('&Select to matching brace'),
326                comment     => Wx::gettext('Select to the matching opening or closing brace'),
327                shortcut    => 'Ctrl-4',
328                menu_event  => sub { shift->current->editor->select_to_matching_brace }
329        );
330
331        Padre::Action->new(
332                name           => 'edit.join_lines',
333                need_editor    => 1,
334                need_selection => 1,
335                label          => Wx::gettext('&Join lines'),
336                comment        => Wx::gettext('Join the next line to the end of the current line.'),
337                shortcut       => 'Ctrl-J',
338                menu_event     => sub {
339                        Padre::Wx::Main::on_join_lines(@_);
340                },
341        );
342
343        Padre::Action->new(
344                name        => 'edit.insert.insert_special',
345                need_editor => 1,
346                label       => Wx::gettext('Insert Special Value'),
347                comment     => Wx::gettext('Select a Date, Filename or other value and insert at the current location'),
348                shortcut    => 'Ctrl-Shift-I',
349                menu_event  => sub {
350                        require Padre::Wx::Dialog::SpecialValues;
351                        Padre::Wx::Dialog::SpecialValues->insert_special(@_);
352                },
353
354        );
355
356        Padre::Action->new(
357                name        => 'edit.insert.snippets',
358                need_editor => 1,
359                label       => Wx::gettext('Snippets'),
360                comment     => Wx::gettext('Select and insert a snippet at the current location'),
361                shortcut    => 'Ctrl-Shift-A',
362                menu_event  => sub {
363                        require Padre::Wx::Dialog::Snippets;
364                        Padre::Wx::Dialog::Snippets->snippets(@_);
365                },
366        );
367
368        Padre::Action->new(
369                name        => 'edit.insert.from_file',
370                need_editor => 1,
371                label       => Wx::gettext('Insert From File...'),
372                comment     => Wx::gettext('Select a file and insert its content at the current location'),
373                menu_event  => sub {
374                        Padre::Wx::Main::on_insert_from_file(@_);
375                },
376        );
377
378        # Commenting
379        Padre::Action->new(
380                name           => 'edit.comment_toggle',
381                need_editor    => 1,
382                need_selection => 1,
383                label          => Wx::gettext('&Toggle Comment'),
384                comment        => Wx::gettext('Comment out or remove comment out of selected lines in the document'),
385                shortcut       => 'Ctrl-Shift-C',
386                toolbar        => 'actions/toggle-comments',
387                menu_event     => sub {
388                        Padre::Wx::Main::on_comment_block( $_[0], 'TOGGLE' );
389                },
390        );
391
392        Padre::Action->new(
393                name           => 'edit.comment',
394                need_editor    => 1,
395                need_selection => 1,
396                label          => Wx::gettext('&Comment Selected Lines'),
397                comment        => Wx::gettext('Comment out selected lines in the document'),
398                shortcut       => 'Ctrl-M',
399                menu_event     => sub {
400                        Padre::Wx::Main::on_comment_block( $_[0], 'COMMENT' );
401                },
402        );
403
404        Padre::Action->new(
405                name           => 'edit.uncomment',
406                need_editor    => 1,
407                need_selection => 1,
408                label          => Wx::gettext('&Uncomment Selected Lines'),
409                comment        => Wx::gettext('Remove comment out of selected lines in the document'),
410                shortcut       => 'Ctrl-Shift-M',
411                menu_event     => sub {
412                        Padre::Wx::Main::on_comment_block( $_[0], 'UNCOMMENT' );
413                },
414        );
415
416        # Conversions and Transforms
417        Padre::Action->new(
418                name        => 'edit.convert_encoding_system',
419                need_editor => 1,
420                label       => Wx::gettext('Encode document to System Default'),
421                comment    => Wx::gettext('Change the encoding of the current document to the default of the operating system'),
422                menu_event => sub {
423                        require Padre::Wx::Dialog::Encode;
424                        Padre::Wx::Dialog::Encode::encode_document_to_system_default(@_);
425                },
426        );
427
428        Padre::Action->new(
429                name        => 'edit.convert_encoding_utf8',
430                need_editor => 1,
431                label       => Wx::gettext('Encode document to utf-8'),
432                comment     => Wx::gettext('Change the encoding of the current document to utf-8'),
433                menu_event  => sub {
434                        require Padre::Wx::Dialog::Encode;
435                        Padre::Wx::Dialog::Encode::encode_document_to_utf8(@_);
436                },
437        );
438
439        Padre::Action->new(
440                name        => 'edit.convert_encoding_to',
441                need_editor => 1,
442                label       => Wx::gettext('Encode document to...'),
443                comment     => Wx::gettext('Select an encoding and encode the document to that'),
444                menu_event  => sub {
445                        require Padre::Wx::Dialog::Encode;
446                        Padre::Wx::Dialog::Encode::encode_document_to(@_);
447                },
448        );
449
450        Padre::Action->new(
451                name        => 'edit.convert_nl_windows',
452                need_editor => 1,
453                label       => Wx::gettext('EOL to Windows'),
454                comment     => Wx::gettext(
455                        'Change the end of line character of the current document to those used in files on MS Windows'),
456                menu_event => sub {
457                        $_[0]->convert_to('WIN');
458                },
459        );
460
461        Padre::Action->new(
462                name        => 'edit.convert_nl_unix',
463                need_editor => 1,
464                label       => Wx::gettext('EOL to Unix'),
465                comment     => Wx::gettext(
466                        'Change the end of line character of the current document to that used on Unix, Linux, Mac OSX'),
467                menu_event => sub {
468                        $_[0]->convert_to('UNIX');
469                },
470        );
471
472        Padre::Action->new(
473                name        => 'edit.convert_nl_mac',
474                need_editor => 1,
475                label       => Wx::gettext('EOL to Mac Classic'),
476                comment => Wx::gettext('Change the end of line character of the current document to that used on Mac Classic'),
477                menu_event => sub {
478                        $_[0]->convert_to('MAC');
479                },
480        );
481
482        # Tabs And Spaces
483        Padre::Action->new(
484                name        => 'edit.tabs_to_spaces',
485                need_editor => 1,
486                label       => Wx::gettext('Tabs to Spaces...'),
487                comment     => Wx::gettext('Convert all tabs to spaces in the current document'),
488                menu_event  => sub {
489                        $_[0]->on_tab_and_space('Tab_to_Space');
490                },
491        );
492
493        Padre::Action->new(
494                name        => 'edit.spaces_to_tabs',
495                need_editor => 1,
496                label       => Wx::gettext('Spaces to Tabs...'),
497                comment     => Wx::gettext('Convert all the spaces to tabs in the current document'),
498                menu_event  => sub {
499                        $_[0]->on_tab_and_space('Space_to_Tab');
500                },
501        );
502
503        Padre::Action->new(
504                name        => 'edit.delete_trailing',
505                need_editor => 1,
506                label       => Wx::gettext('Delete Trailing Spaces'),
507                comment     => Wx::gettext('Remove the spaces from the end of the selected lines'),
508                menu_event  => sub {
509                        $_[0]->on_delete_ending_space;
510                },
511        );
512
513        Padre::Action->new(
514                name        => 'edit.delete_leading',
515                need_editor => 1,
516                label       => Wx::gettext('Delete Leading Spaces'),
517                comment     => Wx::gettext('Remove the spaces from the beginning of the selected lines'),
518                menu_event  => sub {
519                        $_[0]->on_delete_leading_space;
520                },
521        );
522
523        # Upper and Lower Case
524        Padre::Action->new(
525                name        => 'edit.case_upper',
526                need_editor => 1,
527                label       => Wx::gettext('Upper All'),
528                comment     => Wx::gettext('Change the current selection to upper case'),
529                shortcut    => 'Ctrl-Shift-U',
530                menu_event  => sub {
531                        $_[0]->current->editor->UpperCase;
532                },
533        );
534
535        Padre::Action->new(
536                name        => 'edit.case_lower',
537                need_editor => 1,
538                label       => Wx::gettext('Lower All'),
539                comment     => Wx::gettext('Change the current selection to lower case'),
540                shortcut    => 'Ctrl-U',
541                menu_event  => sub {
542                        $_[0]->current->editor->LowerCase;
543                },
544        );
545
546        Padre::Action->new(
547                name        => 'edit.diff2saved',
548                need_editor => 1,
549                label       => Wx::gettext('Diff to Saved Version'),
550                comment =>
551                        Wx::gettext('Compare the file in the editor to that on the disk and show the diff in the output window'),
552                menu_event => sub {
553                        Padre::Wx::Main::on_diff(@_);
554                },
555        );
556        Padre::Action->new(
557                name        => 'edit.applydiff2file',
558                need_editor => 1,
559                label       => Wx::gettext('Apply Diff to File'),
560                comment     => Wx::gettext('Apply a patch file to the current document'),
561                menu_event  => sub {
562                        Padre::Wx::Main::on_diff(@_);
563                },
564        );
565        Padre::Action->new(
566                name        => 'edit.applydiff2project',
567                need_editor => 1,
568                label       => Wx::gettext('Apply Diff to Project'),
569                comment     => Wx::gettext('Apply a patch file to the current project'),
570                menu_event  => sub {
571                        Padre::Wx::Main::on_diff(@_);
572                },
573        );
574
575        # End diff tools
576
577        Padre::Action->new(
578                name        => 'edit.filter_tool',
579                need_editor => 1,
580                label       => Wx::gettext('Filter through external tool'),
581                comment     => Wx::gettext('Filters the selection (or the whole document) through any external command.'),
582                menu_event  => sub {
583                        Padre::Wx::Main::on_filter_tool(@_);
584                },
585        );
586
587        Padre::Action->new(
588                name    => 'edit.regex',
589                label   => Wx::gettext('Regex Editor'),
590                comment => Wx::gettext('Open the regular expression editing window'),
591
592                menu_event => sub {
593                        Padre::Wx::Main::open_regex_editor(@_);
594                },
595        );
596
597        Padre::Action->new(
598                name        => 'edit.show_as_hex',
599                need_editor => 1,
600                label       => Wx::gettext('Show as hexa'),
601                comment     => Wx::gettext('Show the ASCII values of the selected text in hexa in the output window'),
602                menu_event  => sub {
603                        Padre::Wx::Main::show_as_numbers( @_, 'hex' );
604                },
605        );
606
607        Padre::Action->new(
608                name        => 'edit.show_as_decimal',
609                need_editor => 1,
610                label       => Wx::gettext('Show as decimal'),
611                comment    => Wx::gettext('Show the ASCII values of the selected text in decimal numbers in the output window'),
612                menu_event => sub {
613                        Padre::Wx::Main::show_as_numbers( @_, 'decimal' );
614                },
615        );
616
617        # User Preferences
618        Padre::Action->new(
619                name       => 'edit.preferences',
620                label      => Wx::gettext('Preferences'),
621                comment    => Wx::gettext('Edit the user preferences'),
622                menu_event => sub {
623                        Padre::Wx::Main::on_preferences(@_);
624                },
625        );
626
627        return $self;
628}
629
6301;
631
632# Copyright 2008-2010 The Padre development team as listed in Padre.pm.
633# LICENSE
634# This program is free software; you can redistribute it and/or
635# modify it under the same terms as Perl 5 itself.
Note: See TracBrowser for help on using the browser.