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
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from
// from gir-files (https://github.com/gtk-rs/gir-files)
// DO NOT EDIT

use crate::{Buffer, SearchSettings, Style};
use glib::{
    prelude::*,
    signal::{connect_raw, SignalHandlerId},
    translate::*,
};
use std::{boxed::Box as Box_, fmt, mem, mem::transmute, pin::Pin, ptr};

glib::wrapper! {
    ///
    ///
    /// ## Properties
    ///
    ///
    /// #### `buffer`
    ///  The [`Buffer`][crate::Buffer] associated to the search context.
    ///
    /// Readable | Writeable | Construct Only
    ///
    ///
    /// #### `highlight`
    ///  Highlight the search occurrences.
    ///
    /// Readable | Writeable | Construct
    ///
    ///
    /// #### `match-style`
    ///  A [`Style`][crate::Style], or [`None`] for theme's scheme default style.
    ///
    /// Readable | Writeable | Construct
    ///
    ///
    /// #### `occurrences-count`
    ///  The total number of search occurrences. If the search is disabled,
    /// the value is 0. If the buffer is not already fully scanned, the value
    /// is -1.
    ///
    /// Readable
    ///
    ///
    /// #### `regex-error`
    ///  If the regex search pattern doesn't follow all the rules, this
    /// [`glib::Error`][crate::glib::Error] property will be set. If the pattern is valid, the value is
    /// [`None`].
    ///
    /// Free with `g_error_free()`.
    ///
    /// Readable
    ///
    ///
    /// #### `settings`
    ///  The [`SearchSettings`][crate::SearchSettings] associated to the search context.
    ///
    /// This property is construct-only since version 4.0.
    ///
    /// Readable | Writeable | Construct Only
    ///
    /// # Implements
    ///
    /// [`SearchContextExt`][trait@crate::prelude::SearchContextExt], [`SearchContextExtManual`][trait@crate::prelude::SearchContextExtManual]
    #[doc(alias = "GtkSourceSearchContext")]
    pub struct SearchContext(Object<ffi::GtkSourceSearchContext, ffi::GtkSourceSearchContextClass>);

    match fn {
        type_ => || ffi::gtk_source_search_context_get_type(),
    }
}

impl SearchContext {
    pub const NONE: Option<&'static SearchContext> = None;

    /// Creates a new search context, associated with `buffer`, and customized with
    /// `settings`. If `settings` is [`None`], a new [`SearchSettings`][crate::SearchSettings] object will
    /// be created, that you can retrieve with
    /// [`SearchContextExt::settings()`][crate::prelude::SearchContextExt::settings()].
    /// ## `buffer`
    /// a [`Buffer`][crate::Buffer].
    /// ## `settings`
    /// a [`SearchSettings`][crate::SearchSettings], or [`None`].
    ///
    /// # Returns
    ///
    /// a new search context.
    #[doc(alias = "gtk_source_search_context_new")]
    pub fn new(
        buffer: &impl IsA<Buffer>,
        settings: Option<&impl IsA<SearchSettings>>,
    ) -> SearchContext {
        skip_assert_initialized!();
        unsafe {
            from_glib_full(ffi::gtk_source_search_context_new(
                buffer.as_ref().to_glib_none().0,
                settings.map(|p| p.as_ref()).to_glib_none().0,
            ))
        }
    }

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`SearchContext`] objects.
    ///
    /// This method returns an instance of [`SearchContextBuilder`](crate::builders::SearchContextBuilder) which can be used to create [`SearchContext`] objects.
    pub fn builder() -> SearchContextBuilder {
        SearchContextBuilder::new()
    }
}

impl Default for SearchContext {
    fn default() -> Self {
        glib::object::Object::new::<Self>()
    }
}

// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`SearchContext`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct SearchContextBuilder {
    builder: glib::object::ObjectBuilder<'static, SearchContext>,
}

impl SearchContextBuilder {
    fn new() -> Self {
        Self {
            builder: glib::object::Object::builder(),
        }
    }

    /// The [`Buffer`][crate::Buffer] associated to the search context.
    pub fn buffer(self, buffer: &impl IsA<Buffer>) -> Self {
        Self {
            builder: self.builder.property("buffer", buffer.clone().upcast()),
        }
    }

    /// Highlight the search occurrences.
    pub fn highlight(self, highlight: bool) -> Self {
        Self {
            builder: self.builder.property("highlight", highlight),
        }
    }

    /// A [`Style`][crate::Style], or [`None`] for theme's scheme default style.
    pub fn match_style(self, match_style: &Style) -> Self {
        Self {
            builder: self.builder.property("match-style", match_style.clone()),
        }
    }

    /// The [`SearchSettings`][crate::SearchSettings] associated to the search context.
    ///
    /// This property is construct-only since version 4.0.
    pub fn settings(self, settings: &impl IsA<SearchSettings>) -> Self {
        Self {
            builder: self.builder.property("settings", settings.clone().upcast()),
        }
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`SearchContext`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> SearchContext {
        self.builder.build()
    }
}

mod sealed {
    pub trait Sealed {}
    impl<T: super::IsA<super::SearchContext>> Sealed for T {}
}

/// Trait containing all [`struct@SearchContext`] methods.
///
/// # Implementors
///
/// [`SearchContext`][struct@crate::SearchContext]
pub trait SearchContextExt: IsA<SearchContext> + sealed::Sealed + 'static {
    /// Synchronous backward search. It is recommended to use the asynchronous
    /// functions instead, to not block the user interface. However, if you are sure
    /// that the `buffer` is small, this function is more convenient to use.
    ///
    /// If the [`wrap-around`][struct@crate::SearchSettings#wrap-around] property is [`false`], this function
    /// doesn't try to wrap around.
    ///
    /// The `has_wrapped_around` out parameter is set independently of whether a match
    /// is found. So if this function returns [`false`], `has_wrapped_around` will have
    /// the same value as the [`wrap-around`][struct@crate::SearchSettings#wrap-around] property.
    /// ## `iter`
    /// start of search.
    ///
    /// # Returns
    ///
    /// whether a match was found.
    ///
    /// ## `match_start`
    /// return location for start of match, or [`None`].
    ///
    /// ## `match_end`
    /// return location for end of match, or [`None`].
    ///
    /// ## `has_wrapped_around`
    /// return location to know whether the
    ///  search has wrapped around, or [`None`].
    #[doc(alias = "gtk_source_search_context_backward")]
    fn backward(&self, iter: &gtk::TextIter) -> Option<(gtk::TextIter, gtk::TextIter, bool)> {
        unsafe {
            let mut match_start = gtk::TextIter::uninitialized();
            let mut match_end = gtk::TextIter::uninitialized();
            let mut has_wrapped_around = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gtk_source_search_context_backward(
                self.as_ref().to_glib_none().0,
                iter.to_glib_none().0,
                match_start.to_glib_none_mut().0,
                match_end.to_glib_none_mut().0,
                has_wrapped_around.as_mut_ptr(),
            ));
            if ret {
                Some((
                    match_start,
                    match_end,
                    from_glib(has_wrapped_around.assume_init()),
                ))
            } else {
                None
            }
        }
    }

    /// The asynchronous version of [`backward()`][Self::backward()].
    ///
    /// See the documentation of [`backward()`][Self::backward()] for more
    /// details.
    ///
    /// See the `GAsyncResult` documentation to know how to use this function.
    ///
    /// If the operation is cancelled, the `callback` will only be called if
    /// `cancellable` was not [`None`]. [`backward_async()`][Self::backward_async()] takes
    /// ownership of `cancellable`, so you can unref it after calling this function.
    /// ## `iter`
    /// start of search.
    /// ## `cancellable`
    /// a [`gio::Cancellable`][crate::gio::Cancellable], or [`None`].
    /// ## `callback`
    /// a `GAsyncReadyCallback` to call when the operation is finished.
    #[doc(alias = "gtk_source_search_context_backward_async")]
    fn backward_async<
        P: FnOnce(Result<(gtk::TextIter, gtk::TextIter, bool), glib::Error>) + 'static,
    >(
        &self,
        iter: &gtk::TextIter,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
        callback: P,
    ) {
        let main_context = glib::MainContext::ref_thread_default();
        let is_main_context_owner = main_context.is_owner();
        let has_acquired_main_context = (!is_main_context_owner)
            .then(|| main_context.acquire().ok())
            .flatten();
        assert!(
            is_main_context_owner || has_acquired_main_context.is_some(),
            "Async operations only allowed if the thread is owning the MainContext"
        );

        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
        unsafe extern "C" fn backward_async_trampoline<
            P: FnOnce(Result<(gtk::TextIter, gtk::TextIter, bool), glib::Error>) + 'static,
        >(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut gio::ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            let mut error = ptr::null_mut();
            let mut match_start = gtk::TextIter::uninitialized();
            let mut match_end = gtk::TextIter::uninitialized();
            let mut has_wrapped_around = mem::MaybeUninit::uninit();
            let _ = ffi::gtk_source_search_context_backward_finish(
                _source_object as *mut _,
                res,
                match_start.to_glib_none_mut().0,
                match_end.to_glib_none_mut().0,
                has_wrapped_around.as_mut_ptr(),
                &mut error,
            );
            let result = if error.is_null() {
                Ok((
                    match_start,
                    match_end,
                    from_glib(has_wrapped_around.assume_init()),
                ))
            } else {
                Err(from_glib_full(error))
            };
            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
                Box_::from_raw(user_data as *mut _);
            let callback: P = callback.into_inner();
            callback(result);
        }
        let callback = backward_async_trampoline::<P>;
        unsafe {
            ffi::gtk_source_search_context_backward_async(
                self.as_ref().to_glib_none().0,
                iter.to_glib_none().0,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                Some(callback),
                Box_::into_raw(user_data) as *mut _,
            );
        }
    }

    fn backward_future(
        &self,
        iter: &gtk::TextIter,
    ) -> Pin<
        Box_<
            dyn std::future::Future<
                    Output = Result<(gtk::TextIter, gtk::TextIter, bool), glib::Error>,
                > + 'static,
        >,
    > {
        let iter = iter.clone();
        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
            obj.backward_async(&iter, Some(cancellable), move |res| {
                send.resolve(res);
            });
        }))
    }

    /// Synchronous forward search. It is recommended to use the asynchronous
    /// functions instead, to not block the user interface. However, if you are sure
    /// that the `buffer` is small, this function is more convenient to use.
    ///
    /// If the [`wrap-around`][struct@crate::SearchSettings#wrap-around] property is [`false`], this function
    /// doesn't try to wrap around.
    ///
    /// The `has_wrapped_around` out parameter is set independently of whether a match
    /// is found. So if this function returns [`false`], `has_wrapped_around` will have
    /// the same value as the [`wrap-around`][struct@crate::SearchSettings#wrap-around] property.
    /// ## `iter`
    /// start of search.
    ///
    /// # Returns
    ///
    /// whether a match was found.
    ///
    /// ## `match_start`
    /// return location for start of match, or [`None`].
    ///
    /// ## `match_end`
    /// return location for end of match, or [`None`].
    ///
    /// ## `has_wrapped_around`
    /// return location to know whether the
    ///  search has wrapped around, or [`None`].
    #[doc(alias = "gtk_source_search_context_forward")]
    fn forward(&self, iter: &gtk::TextIter) -> Option<(gtk::TextIter, gtk::TextIter, bool)> {
        unsafe {
            let mut match_start = gtk::TextIter::uninitialized();
            let mut match_end = gtk::TextIter::uninitialized();
            let mut has_wrapped_around = mem::MaybeUninit::uninit();
            let ret = from_glib(ffi::gtk_source_search_context_forward(
                self.as_ref().to_glib_none().0,
                iter.to_glib_none().0,
                match_start.to_glib_none_mut().0,
                match_end.to_glib_none_mut().0,
                has_wrapped_around.as_mut_ptr(),
            ));
            if ret {
                Some((
                    match_start,
                    match_end,
                    from_glib(has_wrapped_around.assume_init()),
                ))
            } else {
                None
            }
        }
    }

    /// The asynchronous version of [`forward()`][Self::forward()].
    ///
    /// See the documentation of [`forward()`][Self::forward()] for more
    /// details.
    ///
    /// See the `GAsyncResult` documentation to know how to use this function.
    ///
    /// If the operation is cancelled, the `callback` will only be called if
    /// `cancellable` was not [`None`]. [`forward_async()`][Self::forward_async()] takes
    /// ownership of `cancellable`, so you can unref it after calling this function.
    /// ## `iter`
    /// start of search.
    /// ## `cancellable`
    /// a [`gio::Cancellable`][crate::gio::Cancellable], or [`None`].
    /// ## `callback`
    /// a `GAsyncReadyCallback` to call when the operation is finished.
    #[doc(alias = "gtk_source_search_context_forward_async")]
    fn forward_async<
        P: FnOnce(Result<(gtk::TextIter, gtk::TextIter, bool), glib::Error>) + 'static,
    >(
        &self,
        iter: &gtk::TextIter,
        cancellable: Option<&impl IsA<gio::Cancellable>>,
        callback: P,
    ) {
        let main_context = glib::MainContext::ref_thread_default();
        let is_main_context_owner = main_context.is_owner();
        let has_acquired_main_context = (!is_main_context_owner)
            .then(|| main_context.acquire().ok())
            .flatten();
        assert!(
            is_main_context_owner || has_acquired_main_context.is_some(),
            "Async operations only allowed if the thread is owning the MainContext"
        );

        let user_data: Box_<glib::thread_guard::ThreadGuard<P>> =
            Box_::new(glib::thread_guard::ThreadGuard::new(callback));
        unsafe extern "C" fn forward_async_trampoline<
            P: FnOnce(Result<(gtk::TextIter, gtk::TextIter, bool), glib::Error>) + 'static,
        >(
            _source_object: *mut glib::gobject_ffi::GObject,
            res: *mut gio::ffi::GAsyncResult,
            user_data: glib::ffi::gpointer,
        ) {
            let mut error = ptr::null_mut();
            let mut match_start = gtk::TextIter::uninitialized();
            let mut match_end = gtk::TextIter::uninitialized();
            let mut has_wrapped_around = mem::MaybeUninit::uninit();
            let _ = ffi::gtk_source_search_context_forward_finish(
                _source_object as *mut _,
                res,
                match_start.to_glib_none_mut().0,
                match_end.to_glib_none_mut().0,
                has_wrapped_around.as_mut_ptr(),
                &mut error,
            );
            let result = if error.is_null() {
                Ok((
                    match_start,
                    match_end,
                    from_glib(has_wrapped_around.assume_init()),
                ))
            } else {
                Err(from_glib_full(error))
            };
            let callback: Box_<glib::thread_guard::ThreadGuard<P>> =
                Box_::from_raw(user_data as *mut _);
            let callback: P = callback.into_inner();
            callback(result);
        }
        let callback = forward_async_trampoline::<P>;
        unsafe {
            ffi::gtk_source_search_context_forward_async(
                self.as_ref().to_glib_none().0,
                iter.to_glib_none().0,
                cancellable.map(|p| p.as_ref()).to_glib_none().0,
                Some(callback),
                Box_::into_raw(user_data) as *mut _,
            );
        }
    }

    fn forward_future(
        &self,
        iter: &gtk::TextIter,
    ) -> Pin<
        Box_<
            dyn std::future::Future<
                    Output = Result<(gtk::TextIter, gtk::TextIter, bool), glib::Error>,
                > + 'static,
        >,
    > {
        let iter = iter.clone();
        Box_::pin(gio::GioFuture::new(self, move |obj, cancellable, send| {
            obj.forward_async(&iter, Some(cancellable), move |res| {
                send.resolve(res);
            });
        }))
    }

    ///
    /// # Returns
    ///
    /// the associated buffer.
    #[doc(alias = "gtk_source_search_context_get_buffer")]
    #[doc(alias = "get_buffer")]
    fn buffer(&self) -> Option<Buffer> {
        unsafe {
            from_glib_none(ffi::gtk_source_search_context_get_buffer(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    ///
    /// # Returns
    ///
    /// whether to highlight the search occurrences.
    #[doc(alias = "gtk_source_search_context_get_highlight")]
    #[doc(alias = "get_highlight")]
    fn is_highlight(&self) -> bool {
        unsafe {
            from_glib(ffi::gtk_source_search_context_get_highlight(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    ///
    /// # Returns
    ///
    /// the [`Style`][crate::Style] to apply on search matches.
    #[doc(alias = "gtk_source_search_context_get_match_style")]
    #[doc(alias = "get_match_style")]
    fn match_style(&self) -> Option<Style> {
        unsafe {
            from_glib_none(ffi::gtk_source_search_context_get_match_style(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Gets the position of a search occurrence. If the buffer is not already fully
    /// scanned, the position may be unknown, and -1 is returned. If 0 is returned,
    /// it means that this part of the buffer has already been scanned, and that
    /// `match_start` and `match_end` don't delimit an occurrence.
    /// ## `match_start`
    /// the start of the occurrence.
    /// ## `match_end`
    /// the end of the occurrence.
    ///
    /// # Returns
    ///
    /// the position of the search occurrence. The first occurrence has the
    /// position 1 (not 0). Returns 0 if `match_start` and `match_end` don't delimit
    /// an occurrence. Returns -1 if the position is not yet known.
    #[doc(alias = "gtk_source_search_context_get_occurrence_position")]
    #[doc(alias = "get_occurrence_position")]
    fn occurrence_position(&self, match_start: &gtk::TextIter, match_end: &gtk::TextIter) -> i32 {
        unsafe {
            ffi::gtk_source_search_context_get_occurrence_position(
                self.as_ref().to_glib_none().0,
                match_start.to_glib_none().0,
                match_end.to_glib_none().0,
            )
        }
    }

    /// Gets the total number of search occurrences. If the buffer is not already
    /// fully scanned, the total number of occurrences is unknown, and -1 is
    /// returned.
    ///
    /// # Returns
    ///
    /// the total number of search occurrences, or -1 if unknown.
    #[doc(alias = "gtk_source_search_context_get_occurrences_count")]
    #[doc(alias = "get_occurrences_count")]
    fn occurrences_count(&self) -> i32 {
        unsafe {
            ffi::gtk_source_search_context_get_occurrences_count(self.as_ref().to_glib_none().0)
        }
    }

    /// Regular expression patterns must follow certain rules. If
    /// [`search-text`][struct@crate::SearchSettings#search-text] breaks a rule, the error can be retrieved
    /// with this function. The error domain is `G_REGEX_ERROR`.
    ///
    /// Free the return value with `g_error_free()`.
    ///
    /// # Returns
    ///
    /// the [`glib::Error`][crate::glib::Error], or [`None`] if the pattern is valid.
    #[doc(alias = "gtk_source_search_context_get_regex_error")]
    #[doc(alias = "get_regex_error")]
    fn regex_error(&self) -> Option<glib::Error> {
        unsafe {
            from_glib_full(ffi::gtk_source_search_context_get_regex_error(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    ///
    /// # Returns
    ///
    /// the search settings.
    #[doc(alias = "gtk_source_search_context_get_settings")]
    #[doc(alias = "get_settings")]
    fn settings(&self) -> Option<SearchSettings> {
        unsafe {
            from_glib_none(ffi::gtk_source_search_context_get_settings(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    /// Replaces a search match by another text. If `match_start` and `match_end`
    /// doesn't correspond to a search match, [`false`] is returned.
    ///
    /// `match_start` and `match_end` iters are revalidated to point to the replacement
    /// text boundaries.
    ///
    /// For a regular expression replacement, you can check if `replace` is valid by
    /// calling `g_regex_check_replacement()`. The `replace` text can contain
    /// backreferences; read the `g_regex_replace()` documentation for more details.
    /// ## `match_start`
    /// the start of the match to replace.
    /// ## `match_end`
    /// the end of the match to replace.
    /// ## `replace`
    /// the replacement text.
    /// ## `replace_length`
    /// the length of `replace` in bytes, or -1.
    ///
    /// # Returns
    ///
    /// whether the match has been replaced.
    #[doc(alias = "gtk_source_search_context_replace")]
    fn replace(
        &self,
        match_start: &mut gtk::TextIter,
        match_end: &mut gtk::TextIter,
        replace: &str,
    ) -> Result<(), glib::Error> {
        let replace_length = replace.len() as _;
        unsafe {
            let mut error = ptr::null_mut();
            let is_ok = ffi::gtk_source_search_context_replace(
                self.as_ref().to_glib_none().0,
                match_start.to_glib_none_mut().0,
                match_end.to_glib_none_mut().0,
                replace.to_glib_none().0,
                replace_length,
                &mut error,
            );
            debug_assert_eq!(is_ok == glib::ffi::GFALSE, !error.is_null());
            if error.is_null() {
                Ok(())
            } else {
                Err(from_glib_full(error))
            }
        }
    }

    /// Enables or disables the search occurrences highlighting.
    /// ## `highlight`
    /// the setting.
    #[doc(alias = "gtk_source_search_context_set_highlight")]
    fn set_highlight(&self, highlight: bool) {
        unsafe {
            ffi::gtk_source_search_context_set_highlight(
                self.as_ref().to_glib_none().0,
                highlight.into_glib(),
            );
        }
    }

    /// Set the style to apply on search matches. If `match_style` is [`None`], default
    /// theme's scheme 'match-style' will be used.
    /// To enable or disable the search highlighting, use
    /// [`set_highlight()`][Self::set_highlight()].
    /// ## `match_style`
    /// a [`Style`][crate::Style], or [`None`].
    #[doc(alias = "gtk_source_search_context_set_match_style")]
    fn set_match_style(&self, match_style: Option<&Style>) {
        unsafe {
            ffi::gtk_source_search_context_set_match_style(
                self.as_ref().to_glib_none().0,
                match_style.to_glib_none().0,
            );
        }
    }

    #[doc(alias = "highlight")]
    fn connect_highlight_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_highlight_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkSourceSearchContext,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::highlight\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_highlight_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "match-style")]
    fn connect_match_style_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_match_style_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkSourceSearchContext,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::match-style\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_match_style_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "occurrences-count")]
    fn connect_occurrences_count_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_occurrences_count_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkSourceSearchContext,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::occurrences-count\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_occurrences_count_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    #[doc(alias = "regex-error")]
    fn connect_regex_error_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_regex_error_trampoline<
            P: IsA<SearchContext>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::GtkSourceSearchContext,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(SearchContext::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::regex-error\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_regex_error_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl<O: IsA<SearchContext>> SearchContextExt for O {}

impl fmt::Display for SearchContext {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("SearchContext")
    }
}