File: tea_funx.c

package info (click to toggle)
tea 14.2.4-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,112 kB
  • ctags: 1,746
  • sloc: ansic: 21,260; sh: 3,538; makefile: 204; cs: 8
file content (1351 lines) | stat: -rw-r--r-- 28,531 bytes parent folder | download
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
/***************************************************************************
                          tea_funx.c  -  description
                             -------------------
    begin                : Mon Dec 12 2003
    copyright            : (C) 2003-2006 by Peter 'Roxton' Semiletov
    email                : peter.semiletov@gmail.com
 ***************************************************************************/

/*
Copyright (C) 2002 Paolo Maggi
Copyright (C) 2000  Kh. Naba Kumar Singh
Copyright (C) 2005 BMPx development team.
*/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif

#include <unistd.h>

#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <glib/gi18n.h>
#include <stdlib.h> // Needed for strtol
#include <limits.h> // Needed for strtol
#include <errno.h>
#include <sys/stat.h>
#include <time.h>
#include <stdio.h>
#include <string.h>


#include "tea_defs.h"
#include "tea_text_document.h" // Needs to be above for t_note_page struct
#include "tea_config.h"
#include "image_browser.h"

#include "rox_strings.h"
#include "interface.h"
#include "tea_funx.h"
#include "tea_proj.h"
#include "tea_gtk_utils.h"
#include "callbacks.h"


/* To avoid implicit declaration warning */
void make_stats (t_note_page *doc);


/* from:
 * time.c
 * This file is part of gedit
 *
 * Copyright (C) 2002 Paolo Maggi

  changed by roxton
*/

gchar* get_time (const gchar *format)
{
   if (! format)
      return NULL;

  gchar *out = NULL;
  gchar *out_utf8 = NULL;
  time_t clock;
  struct tm *now;
  size_t out_length = 0;
  gchar *locale_format;

  if (strlen (format) == 0)
     return NULL;

  locale_format = g_locale_from_utf8 (format, -1, NULL, NULL, NULL);

  if (! locale_format)
     return NULL;

  clock = time (NULL);
  now = localtime (&clock);

  do
    {
     out_length += 255;
     out = g_realloc (out, out_length);
    }
  while (strftime (out, out_length, locale_format, now) == 0);

  g_free (locale_format);

  if (g_utf8_validate (out, -1, NULL))
     out_utf8 = out;
  else
      {
       out_utf8 = g_locale_to_utf8 (out, -1, NULL, NULL, NULL);
       g_free (out);
      }

  return out_utf8;
}


//from Anjuta ::
//    utilities.c
//  Copyright (C) 2000  Kh. Naba Kumar Singh
#define FILE_BUFFER_SIZE 65535
gboolean copy_file (const gchar *src, const gchar *dest)
{
  FILE *input_fp, *output_fp;
  gchar buffer[FILE_BUFFER_SIZE];
  gint bytes_read, bytes_written;
  gboolean error = TRUE;

  input_fp = fopen (src, "rb");
  if (! input_fp)
     return FALSE;

  output_fp = fopen (dest, "wb");
  if (! output_fp)
     {
      fclose (input_fp);
      return TRUE;
     }

  for (;;)
      {
       bytes_read = fread (buffer, 1, FILE_BUFFER_SIZE, input_fp);
       if (bytes_read != FILE_BUFFER_SIZE && ferror (input_fp))
          {
           error = FALSE;
           break;
          }

       if (bytes_read)
         {
          bytes_written = fwrite (buffer, 1, bytes_read, output_fp);
          if (bytes_read != bytes_written)
            {
             error = FALSE;
             break;
            }
         }

       if (bytes_read != FILE_BUFFER_SIZE && feof (input_fp))
          break;
       }

  fclose (input_fp);
  fclose (output_fp);

  return error;
}
//


static gint noname_name_counter = -1;

gchar* get_noname_name (void)
{
  ++noname_name_counter;
  if (noname_name_counter >= G_MAXINT)
     noname_name_counter = 0;
  return g_strdup_printf (_("noname_%d"), noname_name_counter);
}


int get_value (int total, int perc)
{
  return (int) (total / 100) * perc;
}


int get_percent (int total, int value)
{
  return (int) (value / total) * 100;
}


void handle_file (const gchar *filename, gint mode, gboolean kwas)
{
  if (! filename)
     return;

  gchar *cmd = NULL;

  gint i = get_n_page_by_filename (filename);

  if (i != -1)
     {
      gtk_notebook_set_current_page (notebook1, i);
      return;
     }

  if (! g_file_test (filename, G_FILE_TEST_EXISTS))
     return;

  if (is_image (filename))
     {
      if (mode != 0)
         {
          cmd = g_strdup_printf (confile.ext_pic_editor, filename);
          system (cmd);
          g_free (cmd);
          return;
         }

      if (confile.use_ext_image_viewer)
         {
          cmd = g_strdup_printf (confile.ext_pic_editor, filename);
          system (cmd);
          g_free(cmd);
          return;
         }
      else
          image_viewer_create (filename);

      return;
     }

  if (! kwas)
     {
      if (! get_page_text() )
        cur_settings.selected_enc = ch_str (cur_settings.selected_enc, confile.default_charset);
      else
          cur_settings.selected_enc = ch_str (cur_settings.selected_enc, cur_text_doc->encoding);
     }

  open_file_std (filename);
}


void handle_file_enc (const gchar *filename, const gchar *enc)
{
  if (! filename)
     return;

  gchar *cmd = NULL;

  gint i = get_n_page_by_filename (filename);

  if (i != -1)
     {
      gtk_notebook_set_current_page (notebook1, i);
      return;
     }

  if (! g_file_test (filename, G_FILE_TEST_EXISTS))
     return;

  cur_settings.selected_enc = ch_str (cur_settings.selected_enc, enc);

  open_file_std (filename);
}

/*
gboolean is_image2 (const gchar *filename)
{
  return is_ext (filename, ".png", ".gif", ".jpg", ".jpeg", ".wbmp", ".bmp", ".mng", NULL);
}
*/

gboolean is_tex (const gchar *filename)
{
  return is_ext (filename, ".tex", NULL);
}


gboolean is_po (const gchar *f)
{
  return is_ext (f, ".pot", ".po", NULL);
}


void create_empty_file (const gchar *filename, const gchar *first_line)
{
  FILE *out = fopen (filename, "w");

  if (! out)
     return;

  if (first_line)
     fprintf (out, "%s", first_line);

  fclose (out);
}


gint get_file_size (const gchar *filename)
{
  struct stat s;
  stat (filename, &s);
  return s.st_size;
}


//check extension without dot
gboolean check_ext_wo_dot (const gchar *filename, const gchar *ext)
{
  if (! filename || ! ext)
     return FALSE;

  gboolean r = FALSE;
  gchar *f = g_utf8_strdown (filename, -1);
  gchar *full_ext = g_strconcat (".", ext, NULL);

  r = g_str_has_suffix (f, full_ext);
  g_free (full_ext);
  g_free (f);
  return r;
}


gboolean check_ext (const gchar *filename, const gchar *ext)
{
  if (! filename || ! ext)
     return FALSE;

  gboolean r = FALSE;
  gchar *f = g_utf8_strdown (filename, -1);
  r = g_str_has_suffix (f, ext);
  g_free (f);
  return r;
}


gboolean is_markup (const gchar *f)
{
  return is_ext (f, ".html", ".htm", ".xml", ".xhtml", ".sgm", ".shtml", ".sgml", ".docbook", NULL);
}


gboolean is_c (const gchar *f)
{
  return is_ext (f, ".c", ".h", ".cpp", ".cxx", ".cc", ".cp", ".c++", NULL);
}


gboolean is_pascal (const gchar *f)
{
  return is_ext (f, ".pas", ".pp", ".dpr", NULL);
}


gchar* get_hl_name (const gchar *file_name)
{
  if (! file_name)
     return NULL;

  gchar *b;

  if (is_markup (file_name))
     return g_strdup (HL_MARKUP);

  if (is_c (file_name))
     return g_strdup (HL_C);

  if (is_pascal (file_name))
     return g_strdup (HL_PASCAL);

  if (check_ext (file_name, ".py"))
     return g_strdup (HL_PYTHON);

  if (check_ext (file_name, ".php"))
     return g_strdup (HL_PHP);

  if (check_ext (file_name, ".sh"))
     return g_strdup (HL_BASH);

  if (is_po (file_name))
     return g_strdup (HL_PO);

  if (is_tex (file_name))
     return g_strdup (HL_TEX);

  if (confile.do_det_scripts_by_content)
     {
      b = str_file_read (file_name);
      if (b)
         {
          if ((strstr (b, "#!/bin/bash")) || (strstr (b, "#!/bin/sh")))
             {
              g_free (b);
              return g_strdup (HL_BASH);
             }
          g_free (b);
         }
     }

  return g_strdup (HL_NONE);
}


gchar* get_l_filename (const gchar *filename)
{
  if (! filename)
     return NULL;

  gsize bytes_read;
  gsize bytes_written;
  return g_filename_from_utf8 (filename, -1, &bytes_read, &bytes_written, NULL);
}


gchar* get_8_filename (const gchar *filename)
{
  if (! filename)
     return NULL;

  gsize bytes_read;
  gsize bytes_written;

  return g_filename_to_utf8 (filename, -1, &bytes_read, &bytes_written, NULL);
}

/*
void make_stats2 (t_note_page *doc)
{
  gboolean selected = TRUE;
  gchar *text;
  gint words = 0;
  gint chars = 0;
  gint white_chars = 0;
  gint lines = 0;
  gint bytes = 0;
  GtkTextIter start;
  GtkTextIter end;
  gunichar ch;

  if (doc_is_sel (doc->text_buffer))
     gtk_text_buffer_get_selection_bounds (doc->text_buffer, &start, &end);
  else
      {
       gtk_text_buffer_get_bounds (doc->text_buffer, &start, &end);
       selected = FALSE;
      }

  lines = gtk_text_buffer_get_line_count (doc->text_buffer);
  text =  gtk_text_buffer_get_text (doc->text_buffer, &start, &end, FALSE);
  bytes = strlen (text);

  do
    {
     ch = gtk_text_iter_get_char (&start);
     if (g_unichar_isspace (ch))
        white_chars++;
     if (gtk_text_iter_starts_word (&start))
        words++;

     chars++;
    }
  while ( (gtk_text_iter_forward_char (&start)) && ( ! gtk_text_iter_equal (&start, &end)) );

  gchar *s_bytes = g_strdup_printf(_("bytes: %d\n"), bytes);
  gchar *s_lines = g_strdup_printf(_("lines: %d\n"), lines);
  gchar *s_words = g_strdup_printf(_("words: %d\n"), words);
  gchar *s_chars = g_strdup_printf(_("chars: %d\n"), chars);
  gchar *s_charsnsp = g_strdup_printf(_("chars non-space: %d\n"), chars - white_chars);

  gchar *result;

  if (! selected)
     result = g_strconcat (_("stats for "),
                           doc->file_name_utf8,
                           ":\n",
                           s_charsnsp,
                           s_chars,
                           s_words,
                           s_lines,
                           s_bytes,
                           NULL);
   else
       result = g_strconcat (_("stats for the selection"),
                             ":\n",
                             s_charsnsp,
                             s_chars,
                             s_words,
                             s_bytes,
                             NULL);

  log_to_memo (result, NULL, LM_NORMAL);

  g_free (s_bytes);
  g_free (s_charsnsp);
  g_free (s_chars);
  g_free (s_words);
  g_free (s_lines);
  g_free (result);
  g_free (text);
}
*/


//from Gedit - plugin
/*
 * docinfo.c
 * This file is part of gedit
 *
 * Copyright (C) 2002 Paolo Maggi

modified by roxton
*/

void make_stats (t_note_page *doc)
{
  if (! doc)
     return;

  if (! doc->text_buffer)
     return;

  gboolean selected = TRUE;

  gchar *text;
  PangoLogAttr *attrs;
  gint words = 0;
  gint chars = 0;
  gint white_chars = 0;
  gint lines = 0;
  gint bytes = 0;
  gint i;
  gchar *tmp_str;

  selected = doc_has_selection (doc);

  if (! selected)
      text = doc_get_buf (doc->text_buffer);
  else
      text = doc_get_sel (doc);

  if (! text)
     return;

  if (! g_utf8_validate (text, -1, NULL))
     return;

  lines = gtk_text_buffer_get_line_count (doc->text_buffer);

  chars = g_utf8_strlen (text, -1);
  attrs = g_new0 (PangoLogAttr, chars + 1);

  pango_get_log_attrs (text,
                       -1,
                       0,
                       pango_language_from_string ("C"),
                       attrs,
                       chars + 1);

  i = 0;

  while (i < chars)
        {
         if (attrs [i].is_white)
            ++white_chars;

         if (attrs [i].is_word_start)
            ++words;

         ++i;
        }

  if (chars == 0)
     lines = 0;

  bytes = strlen (text);

  gchar *s_bytes = g_strdup_printf(_("bytes: %d\n"), bytes);
  gchar *s_lines = g_strdup_printf(_("lines: %d\n"), lines);
  gchar *s_words = g_strdup_printf(_("words: %d\n"), words);
  gchar *s_chars = g_strdup_printf(_("chars: %d\n"), chars);
  gchar *s_charsnsp = g_strdup_printf(_("chars non-space: %d\n"), chars - white_chars);

  gchar *result;

  if (! selected)
     result = g_strconcat (_("stats for "),
                           doc->file_name_utf8,
                           ":\n",
                           s_charsnsp,
                           s_chars,
                           s_words,
                           s_lines,
                           s_bytes,
                           NULL);
   else
       result = g_strconcat (_("stats for selected:\n"),
                             s_charsnsp,
                             s_chars,
                             s_words,
                             s_bytes,
                             NULL);

  log_to_memo (result, NULL, LM_NORMAL);

  g_free (s_bytes);
  g_free (s_charsnsp);
  g_free (s_chars);
  g_free (s_words);
  g_free (s_lines);
  g_free (result);
  g_free (attrs);
  g_free (text);
}


gchar* get_lang_name (void)
{
  gchar *l = g_getenv ("LANG");
  if (! l)
     return g_strdup ("UTF-8");

  if (strcmp (l, "UTF-8") == 0)
     return g_strdup ("UTF-8");

  return g_strndup (g_getenv ("LANG"), 2);
}


gchar* find_good_browser (void)
{
   gchar *t = g_find_program_in_path ("konqueror");
   if (! t)
      t = g_find_program_in_path ("opera");
      if (! t)
         t = g_find_program_in_path ("firefox");
         if (! t)
            t = g_find_program_in_path ("mozilla");
            if (! t)
                t = g_find_program_in_path ("galeon");
                   if (! t)
                      t = g_find_program_in_path ("yelp");

   return t;
}


gchar* compose_browser_cmd (const gchar *filename)
{
  if (! filename)
     return NULL;

  gchar *t;
  gchar *b;

  if (confile.use_def_doc_browser)
     {
      if (confile.cmd_def_doc_browser)
         t = rep_all_s (confile.cmd_def_doc_browser, filename);
      return t;
     }

  b = find_good_browser ();

  if (! b)
     return NULL;

  t = g_strconcat (b, " ", filename, " &", NULL);

  g_free (b);
  return t;
}


gchar* get_tea_doc_compose_name (const gchar *f)
{
  return g_strconcat (TEA_DOC_DIR, f, NULL);
}


gchar* find_doc_index_name (void)
{
  gchar *l = get_lang_name ();
  gchar *f = g_strconcat (TEA_DOC_DIR, l, G_DIR_SEPARATOR_S, "index.html", NULL);

  if (! g_file_test (f, G_FILE_TEST_EXISTS))
     {
      g_free (f);
      f = g_strconcat (TEA_DOC_DIR, "en", G_DIR_SEPARATOR_S, "index.html", NULL);
      g_free (l);
      return f;
     }

  g_free (l);
  return f;
}


void run_doc_in_browser (void)
{
  gchar *f = find_doc_index_name ();
  gchar *t = compose_browser_cmd (f);
  if (t)
     system (t);

  g_free (f);
  g_free (t);
}


gchar* get_clipboard_text (void)
{
  GtkClipboard *c = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  if (! c)
     return NULL;

  if (! gtk_clipboard_wait_is_text_available (c))
     return NULL;

  return gtk_clipboard_wait_for_text (c);
}


void insert_image (const gchar *a_filename)
{
  if (! get_page_text ())
     return;

  if (! cur_text_doc->b_saved)
     return;

  if (! a_filename)
     return;

  if (! g_file_test (a_filename, G_FILE_TEST_EXISTS))
     return;

  gint width = 0;
  gint height = 0;
  gdk_pixbuf_get_file_info (a_filename, &width, &height);

  gchar *filename = create_relative_link (cur_text_doc->file_name_local, a_filename);

  if (! filename)
      return;

  gint mm = get_markup_mode ();
  gchar *t;

  gchar *temp = g_malloc (7777);

  if (mm == MM_HTML)
      g_snprintf (temp,
                  7777,
                  "<img src=\"%s\" border=\"0\" alt=\"\" width=\"%d\" height=\"%d\">",
                  filename, width, height);
  else
  if (mm == MM_XHTML)
     g_snprintf (temp,
                 7777,
                 "<img src=\"%s\" border=\"0\" alt=\"\" width=\"%d\" height=\"%d\" />",
                 filename, width, height);
  else
      if (mm == MM_DOCBOOK)
          g_snprintf (temp,
                      7777,
                      "<mediaobject><imageobject>\n<imagedata fileref=\"%s\"/>\n</imageobject></mediaobject>",
                      filename);
  else
  if (mm == MM_WIKI)
     g_snprintf (temp,
                 7777,
                 "[[Image:%s|thumb|alternative text]]",
                 filename);
  else
  if (mm == MM_TEX)
     g_snprintf (temp,
                 7777,
                 "\\includegraphics{%s}",
                 filename);

  doc_insert_at_cursor (cur_text_doc, temp);

  g_free (temp);
  g_free (filename);
}


void insert_link (const gchar *a_filename)
{
  if (! a_filename || ! cur_text_doc)
     return;

  if (! g_file_test (a_filename, G_FILE_TEST_EXISTS))
     return;

  gchar *filename = create_relative_link (cur_text_doc->file_name_local, a_filename);

  if (! filename)
      return;

  gchar *z =  g_strdup_printf ("<a href=\"%s\"></a>", filename);

  doc_insert_at_cursor (cur_text_doc, z);
  doc_move_cursor_backw_middle_tags (cur_text_doc);

  g_free (filename);
  g_free (z);
}


void handle_file_ide (const gchar *filename, gint line)
{
  if (! filename || ! cur_tea_project)
     return;

  gchar *cmd = file_combine_path (cur_tea_project->dir_source, filename);
  gint i = get_n_page_by_filename (cmd);

  if (i != -1)
     {
      gtk_notebook_set_current_page (notebook1, i);
      if (get_page_text ())
          doc_select_line (cur_text_doc, line);
      g_free (cmd);
      return;
     }

  if (! g_file_test (cmd, G_FILE_TEST_EXISTS))
     {
      g_free (cmd);
      return;
     }
  else
      {
       if (! get_page_text ())
          cur_settings.selected_enc = ch_str (cur_settings.selected_enc, "UTF-8");
       else
           cur_settings.selected_enc = ch_str (cur_settings.selected_enc, cur_text_doc->encoding);

       cur_text_doc = open_file_std (cmd);
       doc_select_line (cur_text_doc, line);
       g_free (cmd);
      }
}


gboolean parse_error_line (const gchar *line, gchar **filename, gint *lineno)
{
  if (! line)
      return FALSE;

  if (! strstr (line, ":"))
      return FALSE;

  gchar **a = g_strsplit (line, ":", -1);

  if (a[0])
     if (a[1])
        if (a[2])
           if (strstr (a[2], "error") || strstr (a[2], "warning"))
              {
               *filename = g_strstrip (g_strdup (a[0]));
               *lineno = strtol (a[1], NULL, 10);
               g_strfreev (a);
               return TRUE;
              }

   g_strfreev (a);
   return FALSE;
}


void clipboard_put_text (const gchar *s)
{
  GtkClipboard *c = gtk_clipboard_get (GDK_SELECTION_CLIPBOARD);
  if (c && s)
     gtk_clipboard_set_text (c, s, -1);
}


GList* read_dir_to_glist (const gchar *path)
{
  if (! path)
     return NULL;

  GDir *dir = g_dir_open (path, 0, NULL);
  if (! dir)
     return NULL;

  GList *l = NULL;
  gchar *f = NULL;

  while (f = g_dir_read_name (dir))
        l = g_list_prepend (l, g_strconcat (path, G_DIR_SEPARATOR_S, f, NULL));

  g_dir_close (dir);
  return g_list_reverse (l);
}


GList* read_dir_files (const gchar *path)
{
  if (! path)
      return NULL;

  GList *l = NULL;

  GDir *d = g_dir_open (path, 0, NULL);
  gchar *t;

  while (t = g_dir_read_name (d))
         if (! g_file_test (t, G_FILE_TEST_IS_DIR))
             l = g_list_prepend (l, g_strdup (t));

  g_dir_close (d);
  return g_list_reverse (l);
}

/*
gint get_markup_mode (void)
returns an ID of the markup mode
*/
gint get_markup_mode (void)
{
  if (g_utf8_collate (confile.default_markup_mode, "HTML") == 0)
     return MM_HTML;
  if (g_utf8_collate (confile.default_markup_mode, "XHTML") == 0)
     return MM_XHTML;
  if (g_utf8_collate (confile.default_markup_mode, "Wikipedia") == 0)
     return MM_WIKI;
  if (g_utf8_collate (confile.default_markup_mode, "LaTeX") == 0)
     return MM_TEX;
  if (g_utf8_collate (confile.default_markup_mode, "Docbook") == 0)
     return MM_DOCBOOK;

  return MM_HTML;
}


gchar* get_amarok_now_playing (void)
{
  gchar *standard_output = NULL;
  gchar *standard_error = NULL;
  gint exit_status;
  GError *error = NULL;

  if (g_spawn_command_line_sync ("dcop amarok player nowPlaying",
                                 &standard_output, &standard_error,
                                 &exit_status, &error))
     g_free (standard_error);

  gchar *x = locale_to_utf8 (standard_output);
  g_free (standard_output);
  if (x)
     return g_strstrip (x);
  else
      return NULL;
}


gboolean is_ext (const gchar *filename, gchar *ext1, ...)
{
  if (! filename)
     return FALSE;

  va_list args;
  gchar *t;
  gboolean r = FALSE;

  va_start (args, ext1);
  t = ext1;

  while (t)
        {
         r = check_ext (filename, t);
         if (r)
            break;

         t = va_arg (args, gchar*);
        }

  va_end (args);

  return r;
}


gchar* run_process_and_wait (const gchar *command)
{
  if (! command)
     return NULL;

  gchar *standard_output = NULL;
  gchar *standard_error = NULL;
  gint exit_status;
  gchar *x = NULL;

  if (command)
     if (g_spawn_command_line_sync (command, &standard_output,
                                    &standard_error, &exit_status, NULL))
       {
        g_free (standard_error);
        x = locale_to_utf8 (standard_output);
        g_free (standard_output);
       }

  return x;
}


void delete_dir (const gchar *path)
{
  if (! path)
     return;

  if (g_utf8_collate (path, G_DIR_SEPARATOR_S) == 0)
     return;

  gchar *cm = g_strconcat ("rm -r -f ", path, NULL);
  system (cm);
  g_free (cm);
}


gchar* correct_relative_filename (const gchar *path, const gchar *filename)
{
  if (! filename)
     return NULL;

  gchar *x = filename_from_xuri (filename);
  gchar *r;

  if (g_utf8_get_char (x) == '/')
     r = g_strdup (x);
  else
      r = g_strconcat (path, G_DIR_SEPARATOR_S, filename, NULL);

  g_free (x);
  return r;
}


gchar* filename_from_xuri (const gchar *uri)
{
  gchar *p = NULL;
  gint mode = 0;

  /*
  0 - no uri
  1 - file:/
  2 - file:///
  */

  p = strstr (uri, "file:///");
  if (p)
     mode = 2;
  else
      {
       p = strstr (uri, "file:/");
       if (p)
          mode = 1;
      }

  if (mode == 0)
     return g_strdup (uri);

  if (mode == 1)
     p+=5;
  else
      p+=7;

  return g_strdup (p);
}


gchar *create_relative_link (const gchar *doc_filename, const gchar *img_filename)
{
  if (! doc_filename || ! img_filename)
     return NULL;

  gchar *dir_doc_filename = g_path_get_dirname (doc_filename);
  gchar *dir_img_filename = g_path_get_dirname (img_filename);

  gint dir_doc_filename_len = strlen (dir_doc_filename);
  gint dir_img_filename_len = strlen (dir_img_filename);

  //if the directories are the same, return the pure filename

  if (strcmp (dir_doc_filename, dir_img_filename) == 0)
     {
      g_free (dir_doc_filename);
      g_free (dir_img_filename);
      return g_path_get_basename (img_filename);
     }

  //if img_filename is in the upper dir than doc_filename, we return the pure filename
  if (strcmp (dir_img_filename, dir_doc_filename) < 0)
     {
      g_free (dir_doc_filename);
      g_free (dir_img_filename);
      return g_path_get_basename (img_filename);
     }

  //if img_filename is in the lower dir than doc_filename, we return the relative filename
  if (strcmp (dir_doc_filename, dir_img_filename) < 0)
  if (strstr (dir_img_filename, dir_doc_filename))
     {
      gchar *rel_path = dir_img_filename + dir_doc_filename_len;
      gchar *pure_filename = g_path_get_basename (img_filename);
      gchar *x = g_strconcat (".", rel_path, G_DIR_SEPARATOR_S, pure_filename, NULL);
      g_free (pure_filename);
      g_free (dir_doc_filename);
      g_free (dir_img_filename);

      return g_strdup (x);
     }

   return g_path_get_basename (img_filename);
}


gchar* create_full_path (const gchar *filename, const gchar *basedir)
{
  if (! filename && ! basedir)
     return NULL;

  if (g_path_is_absolute (filename))
     return g_strdup (filename);

  gchar *x = filename;
  if (x[0] == '.')
     x++;

  if (x[0] == G_DIR_SEPARATOR)
     x++;

  gchar *p = g_strconcat (basedir, G_DIR_SEPARATOR_S, x, NULL);
  return p;
}


gboolean is_ext_arr (const gchar *filename, const gchar *exts)
{
  if (! filename || ! exts)
     return FALSE;

  gboolean r = FALSE;

  gchar **arr = g_strsplit (exts, ";", -1);

  if (! arr)
     return FALSE;

  gint c = -1;

  while (arr[c++])
      {
       r = check_ext_wo_dot (filename, arr [c]);
       if (r)
          break;
      }

  g_strfreev (arr);

  return r;
}


void add_image_format (GdkPixbufFormat *data, GList **list)
{
  if (! data)
     return;

  gchar** arr = gdk_pixbuf_format_get_extensions (data);
  if (! arr)
     return;

  gchar *extensions = g_strjoinv (";", arr);
  *list = g_list_prepend (*list, extensions);
  g_strfreev (arr);
}


GList* image_formats_fill (void)
{
  GSList *sl_formats = gdk_pixbuf_get_formats ();
  GList *result = NULL;
  g_slist_foreach (sl_formats, add_image_format, &result);
  g_slist_free (sl_formats);
  return result;
}


gboolean is_image (const gchar *filename)
{
  if (! gl_image_formats || ! filename)
     return FALSE;

  gboolean r = FALSE;

  GList *t = g_list_first (gl_image_formats);
  while (t)
        {
         r = is_ext_arr (filename, t->data);
         if (r)
             break;

         t = g_list_next (t);
        }

  return r;
}


gboolean is_writable (const gchar *filename)
{
  if (access (filename, W_OK) != 0)
     return FALSE;
  else
      return TRUE;
}


gboolean is_readable (const gchar *filename)
{
  if (access (filename, R_OK) != 0)
     return FALSE;
  else
      return TRUE;
}


gboolean has_ext (const gchar *filename)
{
  return (strstr (++filename, "."));
}

/*
from:
  BMPx - The Dumb Music Player
  Copyright (C) 2005 BMPx development team.
*/


gboolean dir_foreach (const gchar *path, DirForeachFunc function,
                      gpointer user_data, GError **error)
{
  GDir *dir;
  const gchar *entry;
  gchar *entry_fullpath;

  if (! (dir = g_dir_open (path, 0, error)))
    return FALSE;

  while ((entry = g_dir_read_name (dir)))
       {
        entry_fullpath = g_build_filename (path, entry, NULL);

        if ((*function) (entry_fullpath, entry, user_data))
           {
            g_free (entry_fullpath);
            break;
           }

         g_free (entry_fullpath);
       }

  g_dir_close (dir);
  return TRUE;
}


gboolean cbf_listdir (const gchar *path, const gchar *basename, gpointer user_data)
{
  GError *error = NULL;

  if (g_file_test (path, G_FILE_TEST_IS_DIR))
     {
      dir_foreach (path, cbf_listdir, user_data, &error);
      return FALSE;
     }

  if (g_pattern_match_simple (user_data, path))
      gl_found_files = g_list_prepend (gl_found_files, g_strdup (path));


  return FALSE;
}


void read_dir_files_recurse (const gchar *path, const gchar *pattern)
{
  if (! path || ! pattern)
     return;

  glist_strings_free (gl_found_files);
  gl_found_files = NULL;
  GError *error = NULL;
  dir_foreach (path, cbf_listdir, pattern, &error);
}


gchar* get_build_info (void)
{
   return g_strdup_printf (_("(built on %s with GTK %d.%d.%d and GLib %d.%d.%d)"),
                          __DATE__, GTK_MAJOR_VERSION, GTK_MINOR_VERSION, GTK_MICRO_VERSION,
                          GLIB_MAJOR_VERSION, GLIB_MINOR_VERSION, GLIB_MICRO_VERSION);
}


gchar* file_replace_path (const gchar *filename, const gchar *new_dir)
{
  if (! filename || ! new_dir)
     return NULL;

  gchar *name = g_strrstr (filename, G_DIR_SEPARATOR_S);
  if (! name)
     return NULL;

  if (g_str_has_suffix  (new_dir, G_DIR_SEPARATOR_S))
     return g_strconcat (new_dir, name, NULL);
  else
     return g_strconcat (new_dir, name, G_DIR_SEPARATOR_S, NULL);

}


gchar* file_combine_path (const gchar *dir, const gchar *filename)
{
  if (! filename || ! dir)
     return NULL;

  if (g_str_has_suffix  (dir, G_DIR_SEPARATOR_S))
     return g_strconcat (dir, filename, NULL);
  else
     return g_strconcat (dir, filename, G_DIR_SEPARATOR_S, NULL);
}


gchar* add_slash_if_need (const gchar *dir)
{
  if (! dir)
     return NULL;

  if (g_str_has_suffix  (dir, G_DIR_SEPARATOR_S))
     return g_strdup (dir);
  else
     return g_strconcat (dir, G_DIR_SEPARATOR_S, NULL);
}