File: rbgtksourcebuffer.c

package info (click to toggle)
ruby-gnome2 3.1.0-1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 16,072 kB
  • ctags: 17,433
  • sloc: ansic: 93,621; ruby: 62,273; xml: 335; sh: 246; makefile: 25
file content (318 lines) | stat: -rw-r--r-- 10,570 bytes parent folder | download | duplicates (6)
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
/* -*- c-file-style: "ruby"; indent-tabs-mode: nil -*- */
/*
 *  Copyright (C) 2011  Ruby-GNOME2 Project Team
 *  Copyright (C) 2004,2005 Ruby-GNOME2 Project Team
 *  Copyright (C) 2003  Geoff Youngs, based on gtktextview.c by Masao Mutoh
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library 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
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 *  MA  02110-1301  USA
 */

#include "rbgtksourceview3private.h"

/* Class: Gtk::SourceBuffer
 * Text buffer object for Gtk::SourceView.
 */

#define RG_TARGET_NAMESPACE cBuffer
#define _SELF(self) (RVAL2GTKSOURCEBUFFER(self))

#define RVAL2ITER(self, position) rval2iter(self, position)
#define RVAL2STARTITER(self, iter, out) \
        rval2iter_with_default(&(self), &(iter), &(out), gtk_text_buffer_get_start_iter)
#define RVAL2ENDITER(self, iter, out) \
        rval2iter_with_default(&(self), &(iter), &(out), gtk_text_buffer_get_end_iter)

static GtkTextIter *
rval2iter(VALUE self, VALUE position)
{
    if (!g_type_is_a(RVAL2GTYPE(position), GTK_TYPE_TEXT_ITER))
        position = rb_funcall(self, rb_intern("get_iter_at"), 1, position);
    return RVAL2GTKTEXTITER(position);
}

static GtkTextIter *
rval2iter_with_default(VALUE *self, VALUE *iter, GtkTextIter *out,
                       void (*default_func)(GtkTextBuffer *, GtkTextIter *))
{
    if (NIL_P(*iter)) {
        default_func(RVAL2GTKTEXTBUFFER(*self), out);
        return out;
    } else {
        return RVAL2ITER(*self, *iter);
    }
}

/*
 * Class method: new(obj=nil)
 * obj: either a Gtk::TextTagTable, a Gtk::SourceLanguage, or nil.
 *
 * Creates a new source buffer.  If a Gtk::SourceTagTable is provided, the
 * buffer will use it, otherwise it will create a new one.
 *
 * If a Gtk::SourceLanguage object is given, the buffer will be created
 * using highlightings patterns in this language. This is equivalent to
 * creating a new source buffer with the default tag table and then setting
 * the 'language' property.
 *
 * Returns: a newly created Gtk::SourceBuffer object.
 */
static VALUE
rg_initialize(int argc, VALUE *argv, VALUE self)
{
    VALUE val;

    rb_scan_args (argc, argv, "01", &val);
    if (NIL_P (val)) {
        G_INITIALIZE (self, gtk_source_buffer_new (NULL));
    } else
        if (rb_obj_is_kind_of
        (val, GTYPE2CLASS (GTK_TYPE_TEXT_TAG_TABLE))) {
        G_INITIALIZE (self,
                  gtk_source_buffer_new(RVAL2GTKTEXTTAGTABLE(val)));
    } else
        if (rb_obj_is_kind_of
        (val, GTYPE2CLASS (GTK_SOURCE_TYPE_LANGUAGE))) {
        G_INITIALIZE (self,
                  gtk_source_buffer_new_with_language(RVAL2GTKSOURCELANGUAGE(val)));
    } else {
        rb_raise (rb_eArgError,
              "invalid argument %s (expect nil, Gtk::TextTagTable or Gtk::SourceLanguage)",
              rb_class2name (CLASS_OF (val)));
    }
    return Qnil;
}

/*
 * Method: redo!
 *
 * Redoes the last undo operation. Use Gtk::SourceBuffer#can_redo? to check
 * whether a call to this function will have any effect.
 *
 * Returns: self.
 */
static VALUE
rg_redo_bang(VALUE self)
{
    gtk_source_buffer_redo (_SELF (self));
    return self;
}

/*
 * Method: undo!
 *
 * Undoes the last user action which modified the buffer.
 * Use Gtk::SourceBuffer#can_undo? to check whether a call to this function
 * will have any effect.
 *
 * Actions are defined as groups of operations between a call to 
 * Gtk::TextBuffer#begin_user_action and Gtk::TextBuffer#end_user_action,
 * or sequences of similar edits (inserts or deletes) on the same line.
 *
 * Returns: self.
 */
static VALUE
rg_undo_bang(VALUE self)
{
    gtk_source_buffer_undo (_SELF (self));
    return self;
}

/*
 * Method: begin_not_undoable_action
 * Method: begin_not_undoable_action { ... }
 *
 * Marks the beginning of a not undoable action on the buffer, disabling the
 * undo manager.
 *
 * If a block is given, the block is called after marking the beginning
 * of a not undoable action on the buffer.
 * At the end of the block, marks the end of a not undoable action on the
 * buffer. When the last not undoable block is finished, the list of undo
 * actions is cleared and the undo manager is re-enabled.
 *
 * Returns: self
 */
static VALUE
rg_begin_not_undoable_action(VALUE self)
{
    gtk_source_buffer_begin_not_undoable_action (_SELF (self));

    if (rb_block_given_p()) {
        VALUE block = rb_block_proc ();
        rb_funcall (block, rb_intern ("call"), 0);
        gtk_source_buffer_end_not_undoable_action (_SELF (self));
    }
    return self;
}

/*
 * Method: end_not_undoable_action
 *
 * Marks the end of a not undoable action on the buffer.
 * When the last not undoable block is finished, the list of undo
 * actions is cleared and the undo manager is re-enabled.
 *
 * Returns: self
 */
static VALUE
rg_end_not_undoable_action(VALUE self)
{
    gtk_source_buffer_end_not_undoable_action (_SELF (self));
    return self;
}

/*
 * Method: create_source_mark(name=nil, category, where)
 * name: the name of the mark.
 * type: a string defining the mark type.
 * where: a location to place the mark, as a Gtk::TreeIter object.
 *
 * Creates a mark in the buffer of the given type. A mark is semantically
 * very similar to a Gtk::TextMark, except it has a type which is used by the
 * Gtk::SourceView object displaying the buffer to show a pixmap on the left
 * margin, at the line the mark is in. Because of this, a mark is generally
 * associated to a line and not a character position. Marks are also
 * accessible through a position or range in the buffer.
 *
 * Marks are implemented using Gtk::TextMark, so all characteristics and
 * restrictions to marks apply to marks too. These includes life cycle issues
 * and "mark-set" and "mark-deleted" signal emissions.
 *
 * Like a Gtk::TextMark, a Gtk::SourceMark can be anonymous if the passed
 * name is nil.
 *
 * Marks always have left gravity and are moved to the beginning of the line
 * when the user deletes the line they were in. Also, if the user deletes a
 * region of text which contained lines with marks, those are deleted.
 *
 * Typical uses for a mark are bookmarks, breakpoints, current executing
 * instruction indication in a source file, etc..
 *
 * Returns: a new Gtk::SourceMark object, owned by the buffer.
 */
static VALUE
rg_create_source_mark(int argc, VALUE *argv, VALUE self)
{
    VALUE name, category, where;

    if (argc == 2)
        rb_scan_args (argc, argv, "21", &where, &category, &name);
    else
        rb_scan_args (argc, argv, "30", &name, &category, &where);

    return GOBJ2RVAL (gtk_source_buffer_create_source_mark (_SELF (self),
                               RVAL2CSTR (name),
                               RVAL2CSTR_ACCEPT_SYMBOL (category),
                               RVAL2ITER (self, where)));
}

static VALUE
rg_get_source_marks_at_line(int argc, VALUE *argv, VALUE self)
{
    VALUE line, category;

    rb_scan_args (argc, argv, "11", &line, &category);

    /* TODO: need free? */
    return GOBJGSLIST2RVAL_FREE(gtk_source_buffer_get_source_marks_at_line(_SELF(self),
                                        NUM2INT(line),
                                        RVAL2CSTR_ACCEPT_SYMBOL_ACCEPT_NIL(category)),
                                g_slist_free, NULL);
}

static VALUE
rg_get_source_marks_at_iter(int argc, VALUE *argv, VALUE self)
{
    VALUE iter, category;

    rb_scan_args (argc, argv, "11", &iter, &category);

    /* TODO: need free? */
    return GOBJGSLIST2RVAL_FREE(gtk_source_buffer_get_source_marks_at_iter(_SELF(self),
                                        RVAL2ITER(self, iter),
                                        RVAL2CSTR_ACCEPT_SYMBOL_ACCEPT_NIL(category)),
                                g_slist_free, NULL);
}

static VALUE
rg_remove_source_marks(int argc, VALUE *argv, VALUE self)
{
    VALUE start, end, category;
    GtkTextIter start_iter, end_iter;

    rb_scan_args (argc, argv, "03", &start, &end, &category);

    gtk_source_buffer_remove_source_marks (_SELF (self),
                                           RVAL2STARTITER(self, start, start_iter),
                                           RVAL2ENDITER(self, end, end_iter),
                                           RVAL2CSTR_ACCEPT_SYMBOL_ACCEPT_NIL (category));

    return self;
}

static VALUE
rg_forward_iter_to_source_mark(int argc, VALUE *argv, VALUE self)
{
    VALUE iter, category;

    rb_scan_args (argc, argv, "11", &iter, &category);

    return
        CBOOL2RVAL (gtk_source_buffer_forward_iter_to_source_mark
                                   (_SELF (self), RVAL2ITER (self, iter),
                                    RVAL2CSTR_ACCEPT_SYMBOL_ACCEPT_NIL (category)));
}

static VALUE
rg_backward_iter_to_source_mark(int argc, VALUE *argv, VALUE self)
{
    VALUE iter, category;

    rb_scan_args (argc, argv, "11", &iter, &category);

    return
        CBOOL2RVAL (gtk_source_buffer_backward_iter_to_source_mark
                                   (_SELF (self), RVAL2ITER (self, iter),
                                    RVAL2CSTR_ACCEPT_SYMBOL_ACCEPT_NIL (category)));
}

static VALUE
rg_ensure_highlight(VALUE self, VALUE start, VALUE end)
{
    gtk_source_buffer_ensure_highlight (_SELF (self), RVAL2ITER (self, start), RVAL2ITER (self, end));

    return self;
}

void
Init_gtksource_buffer (VALUE mGtkSource)
{
    VALUE RG_TARGET_NAMESPACE =
        G_DEF_CLASS (GTK_SOURCE_TYPE_BUFFER, "Buffer", mGtkSource);

    RG_DEF_METHOD(initialize, -1);
    RG_DEF_METHOD_BANG(redo, 0);
    RG_DEF_METHOD_BANG(undo, 0);
    RG_DEF_METHOD(begin_not_undoable_action, 0);
    RG_DEF_METHOD(end_not_undoable_action, 0);
    RG_DEF_METHOD(create_source_mark, -1);
    RG_DEF_METHOD(get_source_marks_at_line, -1);
    RG_DEF_METHOD(get_source_marks_at_iter, -1);
    RG_DEF_METHOD(remove_source_marks, -1);
    RG_DEF_METHOD(forward_iter_to_source_mark, -1);
    RG_DEF_METHOD(backward_iter_to_source_mark, -1);
    RG_DEF_METHOD(ensure_highlight, 2);
}