forked from geany/geany-plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
geanymacro.c
2304 lines (1883 loc) · 70.3 KB
/
geanymacro.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* This code is supplied as is, and is used at your own risk.
* The GNU GPL version 2 rules apply to this code (see http://fsf.org/>
* You can alter it, and pass it on as you want.
* If you alter it, or pass it on, the only restriction is that this disclamour and licence be
* left intact
*
* Features of Macro implementation adapted from gPHPedit (currently maintained by Anoop John)
*
* william.fraser@virgin.net
* 2010-11-01
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <geanyplugin.h>
#include "utils.h"
#include "Scintilla.h"
#include <stdlib.h>
#include <sys/stat.h>
#include <string.h>
#include <gdk/gdkkeysyms.h>
#include <gtk/gtk.h>
/* structure to hold details of Macro event */
typedef struct
{
gint message;
gulong wparam;
glong lparam;
} MacroEvent;
/* structure to hold details of a macro */
typedef struct
{
gchar *name;
/* trigger codes */
guint keyval;
guint state;
GSList *MacroEvents;
} Macro;
/* structure to hold details of Macro for macro editor */
typedef struct
{
gint message;
const gchar *description;
} MacroDetailEntry;
/* list of editor messages this plugin can handle & a description */
const MacroDetailEntry MacroDetails[]={
{SCI_CUT,N_("Cut to Clipboard")},
{SCI_COPY,N_("Copy to Clipboard")},
{SCI_PASTE,N_("Paste from Clipboard")},
{SCI_LINECUT,N_("Cut current line to Clipboard")},
{SCI_LINECOPY,N_("Copy current line to Clipboard")},
{SCI_DELETEBACK,N_("Delete character to the left")},
{SCI_CLEAR,N_("Delete character to the right")},
{SCI_DELETEBACKNOTLINE,N_("Delete character to the left (but not newline)")},
{SCI_DELWORDLEFT,N_("Delete up to start of word to the Left")},
{SCI_DELWORDRIGHT,N_("Delete up to start of word to the Right")},
{SCI_DELWORDRIGHTEND,N_("Delete up to end of word to the Right")},
{SCI_DELLINELEFT,N_("Delete to beginning of line")},
{SCI_DELLINERIGHT,N_("Delete to end of line")},
{SCI_LINEDELETE,N_("Delete current line")},
{SCI_BACKTAB,N_("Backwards Tab (deletes tab if nothing after it)")},
{SCI_LINESCROLLDOWN,N_("Scroll Display down a line")},
{SCI_LINESCROLLUP,N_("Scroll Display up a line")},
{SCI_ZOOMIN,N_("Zoom view in")},
{SCI_ZOOMOUT,N_("Zoom view out")},
{SCI_LINEDOWN,N_("Move Cursor Down")},
{SCI_LINEUP,N_("Move Cursor Up")},
{SCI_CHARLEFT,N_("Move Cursor Left")},
{SCI_CHARRIGHT,N_("Move Cursor Right")},
{SCI_WORDLEFT,N_("Move Cursor to start of Word to the Left")},
{SCI_WORDRIGHT,N_("Move Cursor to start of Word to the Right")},
{SCI_WORDPARTLEFT,N_("Move Cursor to start of Part of Word to the Left")},
{SCI_WORDPARTRIGHT,N_("Move Cursor to start of Part of Word to the Right")},
{SCI_HOME,N_("Move Cursor to start of line")},
{SCI_LINEEND,N_("Move Cursor to end of line")},
{SCI_DOCUMENTSTART,N_("Move Cursor to 1st line of Document")},
{SCI_DOCUMENTEND,N_("Move Cursor to last line of document")},
{SCI_PAGEUP,N_("Move Cursor up one Page")},
{SCI_PAGEDOWN,N_("Move Cursor down one Page")},
{SCI_HOMEDISPLAY,N_("Move Cursor to first visible character")},
{SCI_LINEENDDISPLAY,N_("Move Cursor to last visible character")},
{SCI_VCHOME,N_("Move Cursor to 1st non-whitespace character of line, or 1st character of line if\
already at 1st non-whitespace character")},
{SCI_PARADOWN,N_("Move Cursor to beginning of next paragraph")},
{SCI_PARAUP,N_("Move Cursor up to beginning of current/previous paragraph")},
{SCI_WORDLEFTEND,N_("Move Cursor to end of Word to the Left")},
{SCI_WORDRIGHTEND,N_("Move Cursor to end of Word to the Right")},
{SCI_LINEDOWNEXTEND,N_("Extend Selection down a line")},
{SCI_LINEUPEXTEND,N_("Extend Selection up a line")},
{SCI_CHARLEFTEXTEND,N_("Extend Selection Left a line")},
{SCI_CHARRIGHTEXTEND,N_("Extend Selection Right a line")},
{SCI_WORDLEFTEXTEND,N_("Extend Selection to start of Word to the Left")},
{SCI_WORDRIGHTEXTEND,N_("Extend Selection to start of Word to the Right")},
{SCI_WORDPARTLEFTEXTEND,N_("Extend Selection to start of Part of Word to the Left")},
{SCI_WORDPARTRIGHTEXTEND,N_("Extend Selection to start of Part of Word to the Right")},
{SCI_HOMEEXTEND,N_("Extend Selection to start of line")},
{SCI_LINEENDEXTEND,N_("Extend Selection to end of line")},
{SCI_DOCUMENTSTARTEXTEND,N_("Extend Selection to start of document")},
{SCI_DOCUMENTENDEXTEND,N_("Extend Selection to end of document")},
{SCI_PAGEUPEXTEND,N_("Extend Selection up one Page")},
{SCI_PAGEDOWNEXTEND,N_("Extend Selection down one Page")},
{SCI_HOMEDISPLAYEXTEND,N_("Extend Selection to fist visible character")},
{SCI_LINEENDDISPLAYEXTEND,N_("Extend Selection to last visible character")},
{SCI_VCHOMEEXTEND,N_("Extend Selection to 1st non-whitespace character of line, or 1st character of\
line if already at 1st non-whitespace character")},
{SCI_PARADOWNEXTEND,N_("Extend Selection to beginning of next paragraph")},
{SCI_PARAUPEXTEND,N_("Extend Selection up to beginning of current/previous paragraph")},
{SCI_WORDLEFTENDEXTEND,N_("Extend Selection to end of Word to the Left")},
{SCI_WORDRIGHTENDEXTEND,N_("Extend Selection to end of Word to the Right")},
{SCI_LINEDOWNRECTEXTEND,N_("Extend Rectangular Selection down a line")},
{SCI_LINEUPRECTEXTEND,N_("Extend Rectangular Selection up a line")},
{SCI_CHARLEFTRECTEXTEND,N_("Extend Rectangular Selection Left a line")},
{SCI_CHARRIGHTRECTEXTEND,N_("Extend Rectangular Selection Right a line")},
{SCI_HOMERECTEXTEND,N_("Extend Rectangular Selection to start of line")},
{SCI_LINEENDRECTEXTEND,N_("Extend Rectangular Selection to end of line")},
{SCI_PAGEUPRECTEXTEND,N_("Extend Rectangular Selection up one Page")},
{SCI_PAGEDOWNRECTEXTEND,N_("Extend Rectangular Selection down one Page")},
{SCI_VCHOMERECTEXTEND,N_("Extend Rectangular Selection to 1st non-whitespace character of line, or\
1st character of line if already at 1st non-whitespace character")},
{SCI_CANCEL,N_("Cancel Selection")},
{SCI_EDITTOGGLEOVERTYPE,N_("Toggle Insert/Overwrite mode")},
{SCI_TAB,N_("Tab")},
{SCI_NEWLINE,N_("Newline")},
{SCI_REPLACESEL,N_("Insert/replace with \"\"")},
{SCI_LINETRANSPOSE,N_("Swap current line with one above")},
{SCI_LOWERCASE,N_("Change selected text to lowercase")},
{SCI_UPPERCASE,N_("Change selected text to uppercase")},
{SCI_LINEDUPLICATE,N_("Insert duplicate of current line below")},
{SCI_SELECTIONDUPLICATE,N_("Insert duplicate of selected text after selection. If nothing selected,\
duplicate line")},
{SCI_SEARCHNEXT,N_("Search for next \"\"")},
{SCI_SEARCHPREV,N_("Search for previous \"\"")},
{SCI_SEARCHANCHOR,N_("Set start of search to beginning of selection")},
/* editor commands that don't seem to work well in editing
* {SCI_FORMFEED,N_("FormFeed")},
*
* other commands ommited as they don't appear to do anything different to existing commands
*/
{0,NULL}
};
/* define IDs for dialog buttons */
enum GEANY_MACRO_BUTTON {
GEANY_MACRO_BUTTON_CANCEL,
GEANY_MACRO_BUTTON_DELETE,
GEANY_MACRO_BUTTON_EDIT,
GEANY_MACRO_BUTTON_RERECORD,
GEANY_MACRO_BUTTON_UP,
GEANY_MACRO_BUTTON_DOWN,
GEANY_MACRO_BUTTON_ABOVE,
GEANY_MACRO_BUTTON_BELOW,
GEANY_MACRO_BUTTON_APPLY
};
GeanyPlugin *geany_plugin;
GeanyData *geany_data;
PLUGIN_VERSION_CHECK(224)
PLUGIN_SET_TRANSLATABLE_INFO(LOCALEDIR, GETTEXT_PACKAGE,
_("Macros"),_("Macros for Geany"),
"1.1","William Fraser <william.fraser@virgin.net>")
/* Plugin user alterable settings */
static gboolean bSaveMacros=TRUE;
static gboolean bQueryOverwriteMacros=TRUE;
/* internal variables */
static gint iShiftNumbers[]={41,33,34,163,36,37,94,38,42,40};
static gulong key_release_signal_id;
static GtkWidget *Record_Macro_menu_item=NULL;
static GtkWidget *Stop_Record_Macro_menu_item=NULL;
static GtkWidget *Edit_Macro_menu_item=NULL;
static Macro *RecordingMacro=NULL;
static GSList *mList=NULL;
static gboolean bMacrosHaveChanged=FALSE;
/* default config file */
const gchar default_config[] =
"[Settings]\n"
"Save_Macros = true\n"
"Question_Macro_Overwrite = true\n"
"[Macros]";
/* clear macro events list and free up any memory they are using */
static GSList * ClearMacroList(GSList *gsl)
{
MacroEvent *me;
GSList * gslTemp=gsl;
/* free data held in GSLIST structure */
while(gslTemp!=NULL)
{
me=gslTemp->data;
/* check to see if it's a message that has string attached, and free it if so
* lparam might be NULL for SCI_SEARCHNEXT or SCI_SEARCHPREV but g_free is ok
* with this
*/
if(me->message==SCI_REPLACESEL ||
me->message==SCI_SEARCHNEXT ||
me->message==SCI_SEARCHPREV)
g_free((void*)(me->lparam));
g_free((void*)(gslTemp->data));
gslTemp=g_slist_next(gslTemp);
}
/* free SLIST structure */
g_slist_free(gsl);
return NULL;
}
/* create new Macro */
static Macro * CreateMacro(void)
{
Macro *m;
if((m=(Macro*)(g_malloc(sizeof *m)))!=NULL)
{
m->name=NULL;
m->MacroEvents=NULL;
return m;
}
return NULL;
}
/* delete macro */
static Macro * FreeMacro(Macro *m)
{
if(m==NULL)
return NULL;
g_free(m->name);
ClearMacroList(m->MacroEvents);
g_free(m);
return NULL;
}
/* add a macro to the list of defined macros */
static void AddMacroToList(Macro *m)
{
mList=g_slist_append(mList,m);
}
/* remove macro from list of defined macros */
static void RemoveMacroFromList(Macro *m)
{
mList=g_slist_remove(mList,m);
}
/* returns a macro in the defined list by name, or NULL if no macro exists with the specified name
*/
static Macro * FindMacroByName(gchar *name)
{
GSList *gsl=mList;
if(name==NULL)
return NULL;
while(gsl!=NULL)
{
if(strcmp(name,((Macro*)(gsl->data))->name)==0)
return gsl->data;
gsl=g_slist_next(gsl);
}
return NULL;
}
/* returns a macro in the defined list by key press combination, or NULL if no macro exists
* with the specified key combination */
static Macro * FindMacroByKey(guint keyval,guint state)
{
GSList *gsl=mList;
while(gsl!=NULL)
{
if(((Macro*)(gsl->data))->keyval==keyval && ((Macro*)(gsl->data))->state==state)
return gsl->data;
gsl=g_slist_next(gsl);
}
return NULL;
}
/* completely wipe all saved macros and ascosiated memory */
static void ClearAllMacros(void)
{
GSList *gsl=mList;
while(gsl!=NULL)
{
FreeMacro((Macro*)(gsl->data));
gsl=g_slist_next(gsl);
}
g_slist_free(mList);
mList=NULL;
}
/* Repeat a macro to the editor */
static void ReplayMacro(Macro *m)
{
MacroEvent *me;
GSList *gsl=m->MacroEvents;
ScintillaObject* sci=document_get_current()->editor->sci;
gchar *clipboardcontents;
gboolean bFoundAnchor=FALSE;
scintilla_send_message(sci,SCI_BEGINUNDOACTION,0,0);
while(gsl!=NULL)
{
me=gsl->data;
/* make not if anchor has been found */
if(me->message==SCI_SEARCHANCHOR)
bFoundAnchor=TRUE;
/* possibility that user edited macros might not have anchor before search */
if((me->message==SCI_SEARCHNEXT || me->message==SCI_SEARCHPREV) &&
bFoundAnchor==FALSE)
{
scintilla_send_message(sci,SCI_SEARCHANCHOR,0,0);
bFoundAnchor=TRUE;
}
/* search might use clipboard to look for: check & hanndle */
if((me->message==SCI_SEARCHNEXT || me->message==SCI_SEARCHPREV) &&
((gchar*)me->lparam)==NULL)
{
clipboardcontents=gtk_clipboard_wait_for_text(gtk_clipboard_get(
GDK_SELECTION_CLIPBOARD));
/* ensure there is something in the clipboard */
if(clipboardcontents==NULL)
{
dialogs_show_msgbox(GTK_MESSAGE_INFO,_("No text in clipboard!"));
break;
}
scintilla_send_message(sci,me->message,me->wparam,(glong)clipboardcontents);
}
else
scintilla_send_message(sci,me->message,me->wparam,me->lparam);
/* move to next macro event */
gsl=g_slist_next(gsl);
}
scintilla_send_message(sci,SCI_ENDUNDOACTION,0,0);
}
/* convert string so that it can be saved as text in a comma separated text entry in an ini file
* resultant string needs to be freed after use
*/
static gchar * MakeStringSaveable(gchar *s)
{
gchar *cTemp;
gchar **pszBits;
/* use GLib routine to do most of the work
* (sort out all special characters, and non-ASCII characters)
*/
cTemp=g_strescape(s,"");
/* now make sure that all instances of ',' are replaced by octal version so that can use ',' as
* a separator
* first break the string up at the commas
*/
pszBits=g_strsplit(cTemp,",",0);
/* can now free up cTemp */
g_free(cTemp);
/* now combine bits together with octal verion of comma in-between */
cTemp=g_strjoinv("\\054",pszBits);
/* free up memory used by bits */
g_strfreev(pszBits);
return cTemp;
}
/* create a macro event from an array of stings. This command may move past more than one array
* entry if the macro event details require it
*/
static MacroEvent * GetMacroEventFromString(gchar **s,gint *k)
{
MacroEvent *me;
me=g_new0(MacroEvent,1);
/* get event number */
me->message=strtoll(s[(*k)++],NULL,10);
/* default to 0 */
me->wparam=0;
/* now handle lparam if required */
switch(me->message)
{
case SCI_SEARCHNEXT:
case SCI_SEARCHPREV:
/* get text */
me->lparam=(glong)(g_strcompress(s[(*k)++]));
/* if text is empty string replace with NULL to signify use clipboard */
if((*((gchar*)(me->lparam)))==0)
{
g_free((gchar*)me->lparam);
me->lparam=(glong)NULL;
}
/* get search flags */
me->wparam=strtoll(s[(*k)++],NULL,10);
break;
case SCI_REPLACESEL:
/* get text */
me->lparam=(glong)(g_strcompress(s[(*k)++]));
break;
/* default handler for messages without extra data */
default:
me->lparam=0;
break;
}
/* return MacroEvent object */
return me;
}
/* convert a Macro event to a string that can be savable in a comma separated ini file entry
* resultant string needs to be freed afterwards
*/
static gchar *MacroEventToString(MacroEvent *me)
{
gchar *szMacroNumber;
gchar *szNumberAndData;
gchar *pTemp;
/* save off macro code */
szMacroNumber=g_strdup_printf("%i",me->message);
/* now handle lparam if required */
switch(me->message)
{
case SCI_SEARCHNEXT:
case SCI_SEARCHPREV:
/* check if string is NULL */
if(((gchar*)(me->lparam))==NULL)
{
/* now merge code and data */
szNumberAndData=g_strdup_printf("%s,,%lu",szMacroNumber,me->wparam);
/* free memory */
g_free(szMacroNumber);
return szNumberAndData;
}
/* first get string reprisentation of data */
pTemp=MakeStringSaveable((gchar*)(me->lparam));
/* now merge code and data */
szNumberAndData=g_strdup_printf("%s,%s,%lu",szMacroNumber,pTemp,me->wparam);
/* free memory */
g_free(szMacroNumber);
g_free(pTemp);
return szNumberAndData;
case SCI_REPLACESEL:
/* first get string reprisentation of data */
pTemp=MakeStringSaveable((gchar*)(me->lparam));
/* now merge code and data */
szNumberAndData=g_strdup_printf("%s,%s",szMacroNumber,pTemp);
/* free memory */
g_free(szMacroNumber);
g_free(pTemp);
return szNumberAndData;
/* default handler for messages without extra data */
default:
return szMacroNumber;
}
}
/* Is there a document open in the editor */
static gboolean DocumentPresent(void)
{
return (document_get_current()!=NULL);
}
/* check editor notifications and remember editor events */
static gboolean Notification_Handler(GObject *obj,GeanyEditor *ed,SCNotification *nt,gpointer ud)
{
MacroEvent *me;
gint i;
/* ignore non macro recording messages */
if(nt->nmhdr.code!=SCN_MACRORECORD)
return FALSE;
/* probably overkill as should not recieve SCN_MACRORECORD messages unless recording
* macros
*/
if(RecordingMacro==NULL)
return FALSE;
/* check to see if it's a code we're happy to deal with */
for(i=0;;i++)
{
/* break if code we can deal with */
if(nt->message==MacroDetails[i].message)
break;
/* got to end of codes without match: unrecognised */
if(MacroDetails[i].description==NULL)
{
dialogs_show_msgbox(GTK_MESSAGE_INFO,_("Unrecognised message\n%i %i %i"),nt->message,
(gint)(nt->wParam),(gint)(nt->lParam));
return FALSE;
}
}
me=g_new0(MacroEvent,1);
me->message=nt->message;
me->wparam=nt->wParam;
/* Special handling for text in lparam */
me->lparam=(me->message==SCI_SEARCHNEXT ||
me->message==SCI_SEARCHPREV ||
me->message==SCI_REPLACESEL)
?((glong) g_strdup((gchar *)(nt->lParam))) : nt->lParam;
/* more efficient to create reverse list and reverse it at the end */
RecordingMacro->MacroEvents=g_slist_prepend(RecordingMacro->MacroEvents,me);
return FALSE;
}
/* convert GTK <Alt><Control><Shift>lowercase char to standard Alt+Ctrl+Shift+uppercase char
* format resultant string needs to be freed
*/
static gchar *GetPretyKeyName(guint keyval,guint state)
{
gboolean bAlt,bCtrl,bShift;
const gchar *cTemp;
gchar *cName;
gchar *cPretyName;
/* get basic keypress description */
cName=gtk_accelerator_name(keyval,state);
/* cName now holds the name but in the <Control><Shift>h format.
* Pretify to Ctrl+Shift+H like in menus
*/
bAlt=((gchar*)g_strrstr(cName,"<Alt>")!=NULL);
bCtrl=((gchar*)g_strrstr(cName,"<Control>")!=NULL);
bShift=((gchar*)g_strrstr(cName,"<Shift>")!=NULL);
/* find out where the modifyer key description ends. */
cTemp=g_strrstr(cName,">");
/* if there are no modifyers then point to start of string */
if(cTemp==NULL)
cTemp=cName;
else
cTemp++;
/* put string back together but pretier */
cPretyName=g_strdup_printf("%s%s%s%c%s",(bShift?"Shift+":""),(bCtrl?"Ctrl+":""),
(bAlt?"Alt+":""),g_ascii_toupper(cTemp[0]),
&(g_ascii_strdown(cTemp,-1)[1]));
/* tidy up */
g_free(cName);
/* return pretified name */
return cPretyName;
}
/* save settings (preferences, and macro data) */
static void SaveSettings(void)
{
GKeyFile *config = NULL;
gchar *config_file = NULL;
gchar *data;
gchar *cKey;
gchar *pcTemp;
gint i,k;
GSList *gsl=mList;
GSList *gslTemp;
gchar **pszMacroStrings;
Macro *m;
/* create new config from default settings */
config=g_key_file_new();
/* now set settings */
g_key_file_set_boolean(config,"Settings","Save_Macros",bSaveMacros);
g_key_file_set_boolean(config,"Settings","Question_Macro_Overwrite",bQueryOverwriteMacros);
/* now save macros */
if(bSaveMacros==TRUE)
{
i=0;
/* iterate through macros and save them */
while(gsl!=NULL)
{
m=(Macro*)(gsl->data);
cKey=g_strdup_printf("A%d",i);
/* save macro name */
pcTemp=MakeStringSaveable(m->name);
g_key_file_set_string(config,"Macros",cKey,pcTemp);
g_free(pcTemp);
/* save trigger data */
cKey[0]='B';
g_key_file_set_integer(config,"Macros",cKey,m->keyval);
cKey[0]='C';
g_key_file_set_integer(config,"Macros",cKey,m->state);
/* convert macros to saveable format
* first generate list of all macrodetails
*/
pszMacroStrings=(gchar **)
(g_malloc(sizeof(gchar *)*(g_slist_length(m->MacroEvents)+1)));
gslTemp=m->MacroEvents;
k=0;
while(gslTemp!=NULL)
{
pszMacroStrings[k++]=MacroEventToString((MacroEvent*)(gslTemp->data));
gslTemp=g_slist_next(gslTemp);
}
/* null terminate array for g_strfreev to work */
pszMacroStrings[k]=NULL;
/* now transfer to single string */
pcTemp=g_strjoinv(",",pszMacroStrings);
/* save data */
cKey[0]='D';
g_key_file_set_string(config,"Macros",cKey,pcTemp);
/* free up memory */
g_free(pcTemp);
g_strfreev(pszMacroStrings);
g_free(cKey);
/* move to next macro */
i++;
gsl=g_slist_next(gsl);
}
}
/* turn config into data */
data=g_key_file_to_data(config,NULL,NULL);
/* calculate setting directory name */
config_file=g_build_filename(geany->app->configdir,"plugins","Geany_Macros",NULL);
/* ensure directory exists */
g_mkdir_with_parents(config_file,0755);
/* make config_file hold name of settings file */
setptr(config_file,g_build_filename(config_file,"settings.conf",NULL));
/* write data */
utils_write_file(config_file, data);
/* free memory */
g_free(config_file);
g_key_file_free(config);
g_free(data);
/* Macros have now been saved */
bMacrosHaveChanged=FALSE;
}
/* load settings (preferences, file data, and macro data) */
static void LoadSettings(void)
{
gchar *pcTemp;
gchar *pcKey;
gint i,k;
gchar *config_file=NULL;
GKeyFile *config=NULL;
Macro *m;
gchar **pcMacroCommands;
/* Make config_file hold directory name of settings file */
config_file=g_build_filename(geany->app->configdir,"plugins","Geany_Macros",NULL);
/* ensure directory exists */
g_mkdir_with_parents(config_file,0755);
/* make config_file hold name of settings file */
setptr(config_file,g_build_filename(config_file,"settings.conf",NULL));
/* either load settings file, or create one from default */
config=g_key_file_new();
if(!g_key_file_load_from_file(config,config_file, G_KEY_FILE_KEEP_COMMENTS,NULL))
g_key_file_load_from_data(config,default_config,sizeof(default_config),
G_KEY_FILE_KEEP_COMMENTS,NULL);
/* extract settings */
bQueryOverwriteMacros=utils_get_setting_boolean(config,"Settings",
"Question_Macro_Overwrite",FALSE);
bSaveMacros=utils_get_setting_boolean(config,"Settings","Save_Macros",FALSE);
/* extract macros */
i=0;
while(TRUE)
{
pcKey=g_strdup_printf("A%d",i);
i++;
/* get macro name */
pcTemp=(gchar*)(utils_get_setting_string(config,"Macros",pcKey,NULL));
/* if null then have reached end of macros */
if(pcTemp==NULL)
{
g_free(pcKey);
break;
}
m=CreateMacro();
m->name=pcTemp;
/* load triggers */
pcKey[0]='B';
m->keyval=utils_get_setting_integer(config,"Macros",pcKey,0);
pcKey[0]='C';
m->state=utils_get_setting_integer(config,"Macros",pcKey,0);
/* Load macro list */
pcKey[0]='D';
pcTemp=(gchar*)(utils_get_setting_string(config,"Macros",pcKey,NULL));
g_free(pcKey);
/* break into individual macro data */
pcMacroCommands=g_strsplit(pcTemp,",",0);
/* can now free up pcTemp */
g_free(pcTemp);
/* now go through macro data generating macros */
for(k=0,m->MacroEvents=NULL;pcMacroCommands[k]!=NULL;)
m->MacroEvents=g_slist_prepend(m->MacroEvents,
GetMacroEventFromString(pcMacroCommands,
&k));
/* list created in reverse as more efficient, now turn it around */
m->MacroEvents=g_slist_reverse(m->MacroEvents);
/* macro now complete, add it to the list */
AddMacroToList(m);
/* free up memory used by pcMacroCommands */
g_strfreev(pcMacroCommands);
}
/* free memory */
g_free(config_file);
g_key_file_free(config);
}
/* handle button presses in the preferences dialog box */
static void on_configure_response(GtkDialog *dialog, gint response, gpointer user_data)
{
gboolean bSettingsChanged;
GtkCheckButton *cb1,*cb2;
if(response!=GTK_RESPONSE_OK && response!=GTK_RESPONSE_APPLY)
return;
/* retreive pointers to check boxes */
cb1=(GtkCheckButton*)(g_object_get_data(G_OBJECT(dialog),"GeanyMacros_cb1"));
cb2=(GtkCheckButton*)(g_object_get_data(G_OBJECT(dialog),"GeanyMacros_cb2"));
/* first see if settings are going to change */
bSettingsChanged=(bSaveMacros!=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb1)));
bSettingsChanged|=(bQueryOverwriteMacros!=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb2)));
/* set new settings settings */
bSaveMacros=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb1));
bQueryOverwriteMacros=gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb2));
/* now save new settings if they have changed */
if(bSettingsChanged)
SaveSettings();
}
/* return a widget containing settings for plugin that can be changed */
GtkWidget *plugin_configure(GtkDialog *dialog)
{
GtkWidget *vbox;
GtkWidget *cb1,*cb2;
vbox=gtk_vbox_new(FALSE, 6);
cb1=gtk_check_button_new_with_label(_("Save Macros when close Geany"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cb1),bSaveMacros);
gtk_box_pack_start(GTK_BOX(vbox),cb1,FALSE,FALSE,2);
/* save pointer to check_button */
g_object_set_data(G_OBJECT(dialog),"GeanyMacros_cb1",cb1);
cb2=gtk_check_button_new_with_label(_("Ask before replacing existing Macros"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cb2),bQueryOverwriteMacros);
gtk_box_pack_start(GTK_BOX(vbox),cb2,FALSE,FALSE,2);
/* save pointer to check_button */
g_object_set_data(G_OBJECT(dialog),"GeanyMacros_cb2",cb2);
gtk_widget_show_all(vbox);
g_signal_connect(dialog,"response",G_CALLBACK(on_configure_response),NULL);
return vbox;
}
/* display help box */
void plugin_help(void)
{
GtkWidget *dialog,*label,*scroll;
gchar *cText;
/* create dialog box */
dialog=gtk_dialog_new_with_buttons(_("Geany Macros help"),
GTK_WINDOW(geany->main_widgets->window),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_STOCK_OK,GTK_RESPONSE_ACCEPT,
NULL);
/* setup help text */
cText=g_strconcat(
_("This Plugin implements Macros in Geany.\n\n"),
_("This plugin allows you to record and use your own macros. "),
_("These are sequences of actions that can then be repeated with a single key combination. "),
_("So if you had dozens of lines where you wanted to delete the last 2 characters, you could simpl\
y start recording, press End, Backspace, Backspace, down line and then stop recording. "),
_("Then simply trigger the macro and it would automatically edit the line and move to the next. "),
_("Select Record Macro from the Tools menu and you will be prompted with a dialog box. "),
_("You need to specify a key combination that isn't being used, and a name for the macro to help y\
ou identify it. "),
_("Then press Record. "),
_("What you do in the editor is then recorded until you select Stop Recording Macro from the Tools\
menu. "),
_("Simply pressing the specified key combination will re-run the macro. "),
_("To edit the macros you have, select Edit Macro from the Tools menu. "),
_("You can select a macro and delete it, or re-record it. "),
_("You can also click on a macro's name and change it, or the key combination and re-define that a\
ssuming that it's not already in use. "),
_("Selecting the edit option allows you to view all the individual elements that make up the macro\
. "),
_("You can select a different command for each element, move them, add new elements, delete element\
s, or if it's replace/insert, you can edit the text that replaces the selected text, or is inserte\
d.\n\n"),
_("The only thing to bear in mind is that undo and redo actions are not recorded, and won't be rep\
layed when the macro is re-run.\n\n"),
_("You can alter the default behaviour of this plugin by selecting Plugin Manager under the Tools \
menu, selecting this plugin, and clicking Preferences. "),
_("You can change:\n"),
_("Save Macros when close Geany - If this is selected then Geany will save any recorded macros and\
reload them for use the next time you open Geany, if not they will be lost when Geany is closed.\
\n"),
_("Ask before replacing existing Macros - If this is selected then if you try recording a macro o\
ver an existing one it will check before over-writing it, giving you the option of trying a differ\
ent name or key trigger combination, otherwise it will simply erase any existing macros with the s\
ame name, or the same key trigger combination."),
NULL);
/* create label */
label=gtk_label_new(cText);
gtk_label_set_line_wrap(GTK_LABEL(label),TRUE);
gtk_widget_show(label);
/* create scrolled window to display label */
scroll=gtk_scrolled_window_new(NULL,NULL);
gtk_scrolled_window_set_policy((GtkScrolledWindow*)scroll,GTK_POLICY_NEVER,
GTK_POLICY_AUTOMATIC);
gtk_scrolled_window_add_with_viewport((GtkScrolledWindow*)scroll,label);
gtk_container_add(GTK_CONTAINER(gtk_dialog_get_content_area(GTK_DIALOG(dialog))),scroll);
gtk_widget_show(scroll);
/* set dialog size (leave width default) */
gtk_widget_set_size_request(dialog,-1,300);
/* display the dialog */
gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
/* free memory */
g_free(cText);
}
/* handle key press
* used to see if macro is being triggered and to control numbered bookmarks
*/
static gboolean Key_Released_CallBack(GtkWidget *widget, GdkEventKey *ev, gpointer data)
{
Macro *m;
m=FindMacroByKey(ev->keyval,ev->state);
/* if it's a macro trigger then run macro */
if(m!=NULL)
{
ReplayMacro(m);
/* ?is this needed */
/* g_signal_stop_emission_by_name((GObject *)widget,"key-release-event"); */
return TRUE;
}
return FALSE;
}
/* return TRUE if this key combination is not bing used by anything else (and so can be used by
* this plugin as a macro trigger)
*/
static gboolean UseableAccel(guint key,guint mod)
{
gint i;
guint u,t;
GSList *gsl;
/* check if in use by accelerator groups */
gsl=gtk_accel_groups_from_object((GObject*)geany->main_widgets->window);
/* loop through all the accelerator groups until we either find one that matches the key (k!=0)
* or we don't (k==0)
*/
for(u=0;u<g_slist_length(gsl);u++)
{
gtk_accel_group_query((GtkAccelGroup*)((g_slist_nth(gsl,u))->data),key,mod,&t);
if(t!=0)
return FALSE; /* combination in use so don't accept as macro trigger */
}
/* now check to see if numbered bookmark key is atempted
*
* control and number pressed
*/
if(mod==4)
{
i=((gint)key)-'0';
if(i>=0 && i<=9)
return FALSE;
}
/* control+shift+number */
if(mod==5)
{
/* could use hardware keycode instead of keyvals but if unable to get keyode then don't
* have a logical default to fall back on
*/
for(i=0;i<10;i++) if((gint)key==iShiftNumbers[i])
return FALSE;
}
/* there are a lot of unmodified keys, and shift+key combinations in use by the editor.
* Rather than list them all individually that the editor uses, ban all except the
* shift+function keys
*/
if(mod==1 || mod==0)
{
if(key<GDK_F1 || key>GDK_F35)
return FALSE;
}
/* there are a several control keys that could be pressed but should not be used on their own
* check for them
*/
if(key==GDK_Shift_L || key==GDK_Shift_R || key==GDK_Control_L || key==GDK_Control_R ||
key==GDK_Caps_Lock || key==GDK_Shift_Lock || key==GDK_Meta_L || key==GDK_Meta_R ||
key==GDK_Alt_L || key==GDK_Alt_R || key==GDK_Super_L || key==GDK_Super_R ||
key==GDK_Hyper_L || key==GDK_Hyper_R)
return FALSE;