-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtxviewer.c
1779 lines (1627 loc) · 55.4 KB
/
mtxviewer.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
/* vim:set ts=8 sw=4 et: */
/*
MDVIEW MTX
Copyright (C) 2024 step, https://github.com/step-
Licensed under the GNU General Public License Version 2
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.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
2024-08-15 step:
This file was derived from the mdview3 help-viewer.c with substantial changes
and feature additions for MTX. The mdview3 help-viewer.c itself was derived
from the hardinfo "help-viewer" directory.
*/
/*
* HelpViewer - Simple Help file browser
* Copyright (C) 2009 Leandro A. F. Pereira <[email protected]>
* Copyright (C) 2015 James B
* Copyright (C) 2023 step
*
* 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, version 2.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define _GNU_SOURCE /* for strcasestr() */
#define GLIB_VERSION_MIN_REQUIRED GLIB_VERSION_2_66
#include <string.h>
#include <stdlib.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#include <libintl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <glib/gi18n.h> /* xgettext --keyword=_ --keyword=Q_:1g */
#include <locale.h>
#include "mtxcmm.h"
#include "mtxtextview.h"
#include "mtxviewer.h"
#include "mtxversion.h"
#include "mtxdbg.h"
#define STATUSBAR_CTX_MAIN 0
#define STATUSBAR_CTX_LINK 1
#define STATUSBAR_CTX_WARN 2
typedef struct mtx_viewer_nav_unit
{
gchar *file;
guint offset;
} MtxViewerNavUnit;
static gboolean do_file_search (MtxViewer *, const gchar *);
static gboolean do_resource_load (MtxViewer *, const gchar *, const gchar *);
static void file_load_complete (MtxTextView *, const gchar *, gpointer);
static void on_curpos_changed (GtkTextBuffer *, GParamSpec *, gpointer);
#ifdef VIEWER_DEBUG
static void
_nav_trail_print (MtxViewer *mvr)
{
MtxViewerNavUnit *unit;
gint nav_trail_length;
MtxViewerNavUnit *page;
if (mvr->nav_trail_page == NULL)
{
return;
}
page = (MtxViewerNavUnit *) mvr->nav_trail_page;
g_printerr ("#back(%d)", mvr->nav_trail_page_idx);
for (gint i = 0; i < mvr->nav_trail_page_idx; i++)
{
unit = g_queue_peek_nth (mvr->nav_trail, i);
g_printerr(" « (%d)(%s)", unit->offset, unit->file);
}
g_printerr (" « \033[7m (%d)(%s) \033[0m »", page->offset, page->file);
nav_trail_length = g_queue_get_length (mvr->nav_trail);
for (gint i = mvr->nav_trail_page_idx + 1; i < nav_trail_length; i++)
{
unit = g_queue_peek_nth (mvr->nav_trail, i);
g_printerr(" (%d)(%s) »", unit->offset, unit->file);
}
g_printerr (" #fore(%d) changed_curpos(%d)\n", nav_trail_length - mvr->nav_trail_page_idx - 1, mvr->changed_curpos);
}
#endif /* VIEWER_DEBUG */
/**
_nav_trail_print_status_bar:
*/
static void
_nav_trail_print_status_bar (MtxViewer *mvr)
{
MtxViewerNavUnit *unit;
gint nav_trail_length;
MtxViewerNavUnit *page;
gchar *name;
GString *message = g_string_new (NULL);
if (mvr->nav_trail_page == NULL)
{
return;
}
page = (MtxViewerNavUnit *) mvr->nav_trail_page;
for (gint i = 0; i < mvr->nav_trail_page_idx; i++)
{
unit = g_queue_peek_nth (mvr->nav_trail, i);
name = g_path_get_basename (unit->file);
g_string_append_printf (message, " « %s", name);
g_free (name);
}
if (mvr->nav_trail_page_idx > 0)
{
g_string_append (message, " \u25C0");
}
name = g_path_get_basename (page->file);
g_string_append_printf (message, " %s ", name);
g_free (name);
nav_trail_length = g_queue_get_length (mvr->nav_trail);
if (nav_trail_length - mvr->nav_trail_page_idx - 1 > 0)
{
g_string_append (message, "\u25B6 ");
}
for (gint i = mvr->nav_trail_page_idx + 1; i < nav_trail_length; i++)
{
unit = g_queue_peek_nth (mvr->nav_trail, i);
name = g_path_get_basename (unit->file);
g_string_append_printf (message, " %s »", name);
g_free (name);
}
gtk_statusbar_pop (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_LINK);
gtk_statusbar_pop (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_WARN);
gtk_statusbar_push (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_MAIN,
message->str);
g_string_free (message, TRUE);
}
/**
_nav_trail_back:
Go back one step on the navigation trail, and disable the back button if the
trail start is reached.
@mvr: pointer to #MtxViewer instance
*/
static void
_nav_trail_back (MtxViewer *mvr)
{
mvr->nav_trail_page_idx--;
mvr->nav_trail_page =
g_queue_peek_nth (mvr->nav_trail, mvr->nav_trail_page_idx);
if (mvr->nav_trail_page_idx == 0)
{
gtk_widget_set_sensitive (mvr->btn_nav_back, mvr->can_go_back = FALSE);
}
if (!mvr->can_go_fore)
{
gtk_widget_set_sensitive (mvr->btn_nav_fore, mvr->can_go_fore = TRUE);
}
_nav_trail_print_status_bar (mvr);
#ifdef VIEWER_DEBUG
g_printerr ("+++++++++++ %s: ", __FUNCTION__); _nav_trail_print (mvr);
#endif // VIEWER_DEBUG
}
/**
_nav_trail_fore:
Go forward one step on the navigation trail, and disable the fore button if the
trail end is reached.
@mvr: pointer to #MtxViewer instance
*/
static void
_nav_trail_fore (MtxViewer *mvr)
{
mvr->nav_trail_page_idx++;
mvr->nav_trail_page =
g_queue_peek_nth (mvr->nav_trail, mvr->nav_trail_page_idx);
if (mvr->can_go_fore && mvr->nav_trail_page_idx == (gint)
(g_queue_get_length (mvr->nav_trail) - 1))
{
gtk_widget_set_sensitive (mvr->btn_nav_fore, mvr->can_go_fore = FALSE);
}
if (!mvr->can_go_back && mvr->nav_trail_page_idx > 0)
{
gtk_widget_set_sensitive (mvr->btn_nav_back, mvr->can_go_back = TRUE);
}
_nav_trail_print_status_bar (mvr);
#ifdef VIEWER_DEBUG
g_printerr ("+++++++++++ %s: ", __FUNCTION__); _nav_trail_print (mvr);
#endif // VIEWER_DEBUG
}
/**
_nav_trail_insert:
insert file in the navigation trail after the current page and go forward.
@mvr: pointer to #MtxViewer instance.
@file: path.
@offset: cursor offset at the jumping page.
*/
static void
_nav_trail_insert (MtxViewer *mvr,
const gchar *file,
guint offset)
{
MtxViewerNavUnit *page = g_malloc (sizeof (MtxViewerNavUnit));
if (mvr->nav_trail_page != NULL)
{
((MtxViewerNavUnit *) mvr->nav_trail_page)->offset = offset;
}
page->offset = 0;
page->file = g_strdup (file);
g_queue_insert_after (mvr->nav_trail, g_queue_peek_nth_link
(mvr->nav_trail, mvr->nav_trail_page_idx), page);
mvr->nav_trail_page = (gpointer) page;
_nav_trail_fore (mvr); /* increments mvr->nav_trail_page_idx */
}
/**
_nav_trail_fore_clear
Clear the navigation trail forward of the current page.
@mvr: pointer to #MtxViewer instance
*/
static void
_nav_trail_fore_clear (MtxViewer *mvr)
{
gpointer p;
if (!mvr->can_go_fore)
{
return;
}
for (gint i = g_queue_get_length (mvr->nav_trail) - 1;
i > mvr->nav_trail_page_idx; i--)
/* for (gint i = 0; i < mvr->nav_trail_page_idx; i++) */
{
/* p = g_queue_pop_head (mvr->nav_trail); */
p = g_queue_pop_tail (mvr->nav_trail);
g_free (((MtxViewerNavUnit *)p)->file);
}
mvr->nav_trail_page_idx = g_queue_get_length (mvr->nav_trail) - 1;
gtk_widget_set_sensitive (mvr->btn_nav_fore, mvr->can_go_fore = FALSE);
}
/**
_scroll_to_curpos:
@curpos: #GtkTextBuffer offset.
@highlight:
*/
static void
_scroll_to_curpos (MtxViewer *mvr,
const gint curpos,
const MtxTextViewHilightMode highlight)
{
GtkTextIter iter;
GtkTextMark *mark;
MtxTextView *tv = MTX_TEXT_VIEW (mvr->text_view);
#ifdef MTX_DEBUG
#define mtx_dbg_get_curpos(LEVEL, FMTPREFIX) \
do { \
gint cp; \
g_object_get (tv->buffer, "cursor-position", &cp, NULL); \
mtx_dbg_errseq (LEVEL, FMTPREFIX "(%d)", cp); \
} while (0);
#endif
mtx_dbg_errout (-1, "A \"%s\" curpos(%d)", mvr->current_file, curpos);
gtk_text_buffer_get_iter_at_offset (tv->buffer, &iter, curpos);
gtk_text_buffer_place_cursor (tv->buffer, &iter);
mark = gtk_text_buffer_get_insert (tv->buffer);
gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW (tv), mark, 0.0, TRUE, 0.0,
0.5);
if (curpos > 0 && highlight)
{
#ifdef MTX_DEBUG
mtx_dbg_get_curpos (-1, " => B");
#endif
mtx_text_view_highlight_at_cursor (MTX_TEXT_VIEW
(mvr->text_view), highlight);
}
#ifdef MTX_DEBUG
mtx_dbg_get_curpos (-1, " => Z");
mtx_dbg_errseq(0, "%c", '\n');
#undef mtx_dbg_get_curpos
#endif
}
static gboolean
idle_scroll_to_curpos (MtxViewer *mvr)
{
_scroll_to_curpos (mvr, mvr->current_curpos,
MTX_TEXT_VIEW_HILIGHT_NORMAL);
return FALSE;
}
/**
open_url:
Open a URL with a suitable Web browser.
Web browser search order (first match wins):
$BROWSER, xdg-open, Gnome open, KDE open,
a list of graphical Web browsers, links -g.
*/
static void
open_url (MtxViewer *mvr,
const gchar *url)
{
const gchar *browsers[] =
{
"xdg-open", "gnome-open", "kfmclient openURL",
"sensible-browser", "firefox", "epiphany",
"iceweasel", "seamonkey", "galeon", "mozilla",
"opera", "konqueror", "netscape", "vivaldi",
"links -g", NULL
};
gint i = 0;
gchar *browser = (gchar *) g_getenv ("BROWSER");
if (browser == NULL || *browser == '\0')
{
browser = (gchar *) browsers[i++];
}
do {
gchar *cmdline = g_strdup_printf ("%s '%s'", browser, url);
if (g_spawn_command_line_async (cmdline, NULL))
{
g_free (cmdline);
return;
}
g_free (cmdline);
browser = (gchar *) browsers[i++];
} while (browser != NULL);
{
gchar *message =
g_strdup (_("Web browser not found. Set environment variable BROWSER."));
gtk_statusbar_push (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_WARN,
message);
g_free (message);
}
}
/**
edit_text_file:
Edit a file using the default editor.
Default editor search order (first match wins):
$DEFAULTTEXTEDITOR, defaulttexteditor,
a list of graphical text editors.
*/
static void
edit_text_file (MtxViewer *mvr,
const gchar *file)
{
const gchar *editors[] =
{
"defaulttexteditor", "gedit", "kate", "leafpad", "mousepad", "pluma",
NULL
};
gint i = 0;
gchar *editor = (gchar *) g_getenv ("DEFAULTTEXTEDITOR");
if (editor == NULL || *editor == '\0')
{
editor = (gchar *) editors[i++];
}
do {
gchar *cmdline = g_strdup_printf ("%s '%s'", editor, file);
if (g_spawn_command_line_async (cmdline, NULL))
{
g_free (cmdline);
return;
}
g_free (cmdline);
editor = (gchar *) editors[i++];
} while (editor != NULL);
{
gchar *message =
g_strdup (_("Text editor not found. Set environment variable DEFAULTTEXTEDITOR."));
gtk_statusbar_push (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_WARN,
message);
g_free (message);
}
}
/**
dispatch_to_page:
Load a new page from a file path or a URI.
Supported URIs: `resource://...`, `search://...`.
on_link_clicked() handles URI schemes `http:`, `https:`, `ftp:` and `mailto:`.
Returns: TRUE if a new page was loaded otherwise returns FALSE.
*/
/*
A discipline of navigation history:
Viewer navigation history shall be managed only by the callers of this function
and by the `file_load_complete` callback, which is connected to the
"file-load-complete" signal emitted by `mtx_text_view_load_file()`.
*/
static gboolean
dispatch_to_page (MtxViewer *mvr,
const gchar *path)
{
const gchar *scheme = g_uri_peek_scheme (path);
gboolean retval = FALSE; /* => found an unknown scheme */
if (g_strcmp0 (scheme, "search") == 0)
{
retval = do_file_search (mvr, path + sizeof ("search://") - 1);
if (retval)
{
file_load_complete (MTX_TEXT_VIEW (mvr->text_view), path, mvr);
}
}
else if (g_strcmp0 (scheme, "resource") == 0)
{
const gchar *p = path + sizeof "resource://" - 1;
retval = do_resource_load (mvr, p, p);
if (retval)
{
file_load_complete (MTX_TEXT_VIEW (mvr->text_view), path, mvr);
}
}
else if (scheme == NULL)
{
retval =
mtx_text_view_load_file (MTX_TEXT_VIEW (mvr->text_view),
path,
mvr->current_file ==
NULL ? "" : mvr->current_file, TRUE);
}
else
{
/* TODO status bar message telling scheme+path can't load */
}
g_idle_add (G_SOURCE_FUNC (idle_scroll_to_curpos), mvr);
return retval;
}
/**
*/
static void
nav_fore_clicked (GtkWidget *widget __attribute__((unused)),
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
MtxViewerNavUnit *fore =
(MtxViewerNavUnit *) g_queue_peek_nth (mvr->nav_trail,
mvr->nav_trail_page_idx + 1);
guint saved_curpos = mvr->current_curpos;
((MtxViewerNavUnit *) mvr->nav_trail_page)->offset = mvr->changed_curpos;
mvr->current_curpos = fore->offset;
if (dispatch_to_page (mvr, fore->file))
{
_nav_trail_fore (mvr);
}
else
{
mvr->current_curpos = saved_curpos;
}
}
/**
*/
static gboolean
accel_nav_fore (GtkAccelGroup *group __attribute__((unused)),
GObject *obj __attribute__((unused)),
guint *keyval __attribute__((unused)),
GdkModifierType mod __attribute__((unused)),
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
if (mvr->can_go_fore)
{
nav_fore_clicked (NULL, data);
}
return TRUE;
}
/**
*/
static void
nav_back_clicked (GtkWidget *widget __attribute__((unused)),
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
MtxViewerNavUnit *back =
(MtxViewerNavUnit *) g_queue_peek_nth (mvr->nav_trail,
mvr->nav_trail_page_idx - 1);
guint saved_curpos = mvr->current_curpos;
((MtxViewerNavUnit *) mvr->nav_trail_page)->offset = mvr->changed_curpos;
mvr->current_curpos = back->offset;
if (dispatch_to_page (mvr, back->file))
{
_nav_trail_back (mvr);
}
else
{
mvr->current_curpos = saved_curpos;
}
}
/**
*/
static gboolean
accel_nav_back (GtkAccelGroup *group __attribute__((unused)),
GObject * obj __attribute__((unused)),
guint keyval __attribute__((unused)),
GdkModifierType mod __attribute__((unused)),
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
if (mvr->can_go_back)
{
nav_back_clicked (NULL, data);
}
return TRUE;
}
/**
on_link_clicked:
Callback from #MtxTextView class.
@link_dest: format: <uri-encoded>\n<verbatim>
*/
static void
on_link_clicked (MtxTextView *text_view,
const gchar *link_dest,
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
gchar *end = strchr (link_dest, '\n'); /* uri-encoded */
g_assert (end != NULL);
gchar *link = g_strndup (link_dest, end - link_dest);
const gchar *scheme = g_uri_peek_scheme (link);
const gchar *current_scheme = g_uri_peek_scheme (mvr->current_file);
if (scheme == NULL)
{
gtk_statusbar_pop (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_LINK);
const guint offset = mvr->changed_curpos;
if (mtx_text_view_load_file (text_view, link, current_scheme ? "/" :
mvr->current_file, TRUE))
{
_nav_trail_fore_clear (mvr);
_nav_trail_insert (mvr, mvr->current_file, offset);
mtx_text_view_cursor_to_top (MTX_TEXT_VIEW (mvr->text_view));
}
}
else if (strcmp (scheme, "file") == 0)
{
gtk_statusbar_pop (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_LINK);
const guint offset = mvr->changed_curpos;
if (mtx_text_view_load_file (text_view, link + sizeof "file://" - 1,
current_scheme ? "/" :
mvr->current_file, TRUE))
{
_nav_trail_fore_clear (mvr);
_nav_trail_insert (mvr, mvr->current_file, offset);
mtx_text_view_cursor_to_top (MTX_TEXT_VIEW (mvr->text_view));
}
}
else if (strcmp (scheme, "https") == 0 || strcmp (scheme, "http") == 0
|| strcmp (scheme, "ftp") == 0 || strcmp (scheme, "mailto") == 0)
{
open_url (mvr, link);
}
g_free (link);
}
/**
file_load_complete:
Callback from #MtxTextView class and, in some cases, called directly by
#dispatch_to_page.
*/
static void
file_load_complete (MtxTextView *text_view __attribute__((unused)),
const gchar *file,
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
gchar *message = NULL;
const gchar *scheme = g_uri_peek_scheme (file);
if (g_strcmp0 (scheme, "search") == 0)
{
message = g_strdup (_("Search complete."));
}
else if (g_strcmp0 (scheme, "resource") == 0)
{
message = g_strdup (_("Loaded."));
}
else if (scheme == NULL)
{
gchar *p = g_path_get_basename (file);
message = g_strdup_printf (_("%1$s loaded."), p);
g_free (p);
}
/* Set the currently-loaded file. */
g_free (mvr->current_file);
mvr->current_file = g_strdup (file);
gtk_statusbar_pop (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_LINK);
gtk_statusbar_pop (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_WARN);
gtk_statusbar_push (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_MAIN,
message);
g_free (message);
}
/**
on_curpos_changed:
*/
static void
on_curpos_changed (GtkTextBuffer *buffer,
GParamSpec *a2 __attribute__((unused)),
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
g_object_get (buffer, "cursor-position", &mvr->changed_curpos, NULL);
mtx_dbg_errout (-1, "changed_curpos (%d)\n", mvr->changed_curpos);
}
/**
hovering_over_link:
Callback from #MtxTextView class.
*/
static void
hovering_over_link (MtxTextView *text_view __attribute__((unused)),
const gchar *link,
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
gchar *temp;
temp = g_strdup_printf (_("Link to %s"), link);
gtk_statusbar_push (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_LINK,
temp);
g_free (temp);
}
/**
hovering_over_text:
Callback from #MtxTextView class.
*/
static void
hovering_over_text (MtxTextView *text_view __attribute__((unused)),
gpointer data)
{
MtxViewer *mvr = (MtxViewer *) data;
gtk_statusbar_pop (GTK_STATUSBAR (mvr->status_bar), STATUSBAR_CTX_LINK);
}
/**
_file_get_content_type:
Returns: newly-allocated string, NULL on error, "" on content type unknown,
otherwise the content type string.
*/
static gchar *
_file_get_content_type (const gchar *path)
{
const gchar *content_type;
GError *error = NULL;
g_autoptr (GFile) file = g_file_new_for_path (path);
g_autoptr (GFileInfo) info =
g_file_query_info (file, G_FILE_ATTRIBUTE_STANDARD_CONTENT_TYPE, 0, NULL,
&error);
if (error != NULL)
{
g_error_free (error);
return NULL;
}
if (info == NULL)
{
return NULL;
}
content_type = g_file_info_get_content_type (info);
return g_strdup (content_type == NULL ? "" : content_type);
}
/**
_is_text_and_markdown:
@content_type: MIME-type string.
@is_markdown: pointer to #gboolean, NULLABLE.
Returns TRUE if MIME type is text then sets *is_markdown if MIME subtype is
markdown. Otherwise it returns FALSE and *is_boolean is untouched.
*/
static gboolean
_is_text_and_markdown (const gchar *content_type,
gboolean *is_markdown)
{
if (strncmp (content_type, "text/", sizeof ("text/") - 1) == 0)
{
if (is_markdown != NULL)
{
/* https://stackoverflow.com/a/25812177 */
*is_markdown =
strstr (content_type + sizeof ("text/") - 1, "markdown") != NULL;
}
return TRUE;
}
return FALSE;
}
/**
_build_search_lists:
Build two lists of searchable files in the homepage directory. Only text files
are considered. If auto_languages is active, results will prefer File.$LANG.ext
over File.ext.
@markdown: address of a #GSList pointer that will received the list of markdown
files in the directory.
@text: address of a #GSList pointer that will received the list of other text
files in the directory.
Returns: the total number of elements in the two lists or -1 if case of error.
List elements can be NULL. *@markdown and @text are NULL if a list is empty.
The caller owns the returned lists and should free them when done.
*/
static gint
_build_search_lists (MtxViewer *mvr,
GSList **markdown,
GSList **text)
{
GDir *dir;
const gchar *name;
guint counter = 0;
g_autofree gchar *abs_dirpath =
g_canonicalize_filename (mvr->base_directory, NULL);
*markdown = *text = NULL;
if ((dir = g_dir_open (abs_dirpath, 0, NULL)) == NULL)
{
return -1;
}
while ((name = g_dir_read_name (dir)))
{
gboolean is_text_type = FALSE;
gboolean is_text_markdown = FALSE;
gchar *path = g_build_filename (abs_dirpath, name, NULL);
gchar *content_type = _file_get_content_type (path);
if (content_type == NULL)
{
g_free (path);
continue;
}
is_text_type =
_is_text_and_markdown (content_type, &is_text_markdown);
g_free (content_type);
if (is_text_type)
{
GSList **head = is_text_markdown ? markdown : text;
gchar *filename = NULL;
if (mvr->auto_lang)
{
filename =
mtx_text_view_auto_lang_find (MTX_TEXT_VIEW
(mvr->text_view), path);
}
if (filename == NULL)
{
filename = path;
}
*head = g_slist_insert (*head, (gpointer) filename, 0);
++counter;
}
}
g_dir_close (dir);
return counter;
}
/**
_file_search:
*/
static void
_file_search (gpointer path, gpointer pod)
{
typedef struct
{
gboolean is_text_markdown;
GString *retstr;
gchar **terms;
guint *ctr;
GRegex *regex_astx;
GtkEntry *entry;
} POD;
POD *ppod = (POD *) pod;
const gboolean is_text_markdown = ppod->is_text_markdown;
GString *retstr = ppod->retstr;
gchar **terms = ppod->terms;
guint *counter = ppod->ctr;
GRegex *regex_astx = ppod->regex_astx;
GtkEntry *entry = ppod->entry;
gboolean found = FALSE;
gtk_entry_progress_pulse (entry);
errno = 0;
g_autofree gchar *contents = _get_file_contents (path, NULL, TRUE);
if (contents == NULL)
{
if (errno)
{
fprintf (stderr, PROGNAME ": ");
perror (path);
}
return;
}
if (contents[0] == '\0')
{
return;
}
#ifdef _GNU_SOURCE
for (guint term = 0; !found && terms[term]; term++)
{
found = strcasestr (contents, terms[term]) != NULL;
}
#else
gchar *upper1 = g_utf8_strup (contents, -1);
for (guint term = 0; !found && terms[term]; term++)
{
gchar *upper2 = g_utf8_strup (terms[term], -1);
found = strstr (upper1, upper2) != NULL;
g_free (upper2);
}
g_free (upper1);
#endif
if (found)
{
*counter +=1;
/*
Extract the page title from the first level-1 setext heading
*/
GString *title = NULL, *dest = NULL;
g_autoptr (GMatchInfo) minfo = NULL;
if (is_text_markdown
&& g_regex_match (regex_astx, contents, 0, &minfo))
{
g_autofree gchar *p =
g_match_info_fetch_named (minfo, "TITLE");
title = g_string_new (p);
}
/* Sanitize title and destination. */
if (title == NULL)
{
gchar *p;
for (p = strchr (path, '\0'); p >= (gchar *) path; p--)
{
if (G_IS_DIR_SEPARATOR (*p))
{
p++;
break;
}
}
title = g_string_new (p);
}
title->str = g_strstrip (g_strdelimit
(title->str, "\\\n\r", ' '));
g_string_set_size (title, strlen (title->str));
g_string_replace (title, "]", "\\]", -1);
dest = g_string_new (path);
g_string_replace (dest, ")", "\\)", -1);
g_string_append_printf (retstr, "* [%s](%s)\n",
title->str, dest->str);
g_string_free (title, TRUE);
g_string_free (dest, TRUE);
}
}
/**
do_file_search:
Load the results of a search URI into a new viewing page.
Returns: TRUE if the new page was generated otherwise returns FALSE.
*/
/*
Result is a synthetic page.
We must not call mtx_text_view_load_file!
*/
static gboolean
do_file_search (MtxViewer *mvr,
const gchar *text)
{
GString *markdown = g_string_new (NULL);
gchar *stripped, **terms;
gint ctr_subjects, ctr_results = 0;
GSList *mkd = NULL, *txt = NULL;
GtkEntry *entry = GTK_ENTRY (mvr->text_search);
gint argc = 0;
stripped = g_strstrip (g_strdup (text));
if (!g_shell_parse_argv (text, &argc, &terms, NULL))
{
terms = g_strsplit (stripped, " ", 0);
argc = g_strv_length (terms);
}
g_free (stripped);
ctr_subjects = _build_search_lists (mvr, &mkd, &txt);
if (ctr_subjects < 0)
{
return FALSE;
}
gtk_widget_set_sensitive (mvr->window, FALSE);
gtk_entry_set_progress_fraction (entry, 1.0f / (ctr_subjects + 1));
gtk_entry_progress_pulse (entry);
mkd = g_slist_sort (mkd, (GCompareFunc) g_strcmp0);
txt = g_slist_sort (txt, (GCompareFunc) g_strcmp0);
typedef struct
{
gboolean is_text_markdown;
GString *retstr;
gchar **terms;
gint *ctr;
GRegex *regex_astx;
GtkEntry *entry;
} POD;
POD pod = { TRUE, markdown, terms, &ctr_results, mvr->regex_astx, entry };
g_slist_foreach (mkd, (GFunc) _file_search, &pod);
g_slist_free_full (mkd, g_free);
pod.is_text_markdown = FALSE;
g_slist_foreach (txt, (GFunc) _file_search, &pod);
g_slist_free_full (txt, g_free);
/* prepend formatted page heading */
{
guint n = 0;
for (const gchar *p = text; *p; p++)
{
if (*p == '`') ++n;
}
g_autofree gchar *ctr_subjects_str =
g_strdup_printf (ngettext
(_("%d document examined"),
_("%d documents examined"), ctr_subjects),
ctr_subjects);
g_autofree gchar *codespan = g_strnfill (n + 1, '`');
g_autofree gchar *ctr_results_str =
g_strdup_printf (ngettext
(_("%d document found"),
_("%d documents found"), ctr_results),
ctr_results);
g_autofree gchar *terms_str =
g_strdup_printf (ngettext
(Q_ ("search:1=#terms:2=terms:3=```|%1$d search term %3$s %2$s %3$s"),
Q_ ("search:1=#terms:2=terms:3=```|%1$d search terms %3$s %2$s %3$s"),
argc), argc, text, codespan);
g_autofree gchar *heading =
g_strdup_printf (Q_("search:1=ctr_subjects,2=ctr_results:3=terms|### %1$s, %2$s, %3$s\n# \n"),
ctr_subjects_str, ctr_results_str, terms_str);
g_string_prepend (markdown, heading);
}
/* show the results inside the textview */
gboolean retval =
mtx_text_view_set_text (MTX_TEXT_VIEW (mvr->text_view),
&markdown->str, NULL, TRUE);
g_string_free (markdown, FALSE);
gtk_entry_set_progress_fraction (entry, 0.0f);