Develop and Download Open Source Software

Browse CVS Repository

Annotation of /shiki/shiki/shiki.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.33 - (hide annotations) (download) (as text)
Thu Nov 16 16:15:34 2006 UTC (17 years, 4 months ago) by aloha
Branch: MAIN
Changes since 1.32: +65 -44 lines
File MIME type: text/x-csrc
bug fix and add undo icon

1 aloha 1.1 /* vim: set encoding=utf8:
2     *
3     * shiki.c
4     *
5     * Copyright(C)2006 WAKATSUKI toshihiro
6     *
7     * Permission is hereby granted, free of charge, to any person obtaining a
8     * copy of this software and associated documentation files (the "Software"),
9     * to deal in the Software without restriction, including without limitation
10     * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11     * and/or sell copies of the Software, and to permit persons to whom the
12     * Software is furnished to do so, subject to the following conditions:
13     *
14     * The above copyright notice and this permission notice shall be included in
15     * all copies or substantial portions of the Software.
16     *
17     * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18     * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19     * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20     * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21     * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22     * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23     * SOFTWARE.
24     *
25 aloha 1.33 * $Id: shiki.c,v 1.32 2006/11/16 08:52:02 aloha Exp $
26 aloha 1.1 */
27    
28     #include<gauche.h>
29     #include<gtk/gtk.h>
30     #include<gdk/gdkkeysyms.h>
31    
32 aloha 1.6 static gint editor_indent_width = 2;
33 aloha 1.7
34 aloha 1.32 /* Undo/Redo ������������������ */
35     typedef enum {SHIKI_UNDO_INSERT, SHIKI_UNDO_DELETE} ShikiAction;
36    
37     typedef struct {
38     ShikiAction action;
39     gchar *str;
40     gint strlen;
41     gint start;
42     gint end;
43     } ShikiUndoInfo;
44    
45 aloha 1.17 /* ��������������������������������������������� */
46 aloha 1.16 typedef struct {
47 aloha 1.20 GtkScrolledWindow *tabpage; /* ������ */
48     gchar *tabpage_label; /* ��������� (���������������) ������ */
49     GtkTextView *text_view; /* ��������������� */
50     GtkTextBuffer *text_buffer; /* ��������������������������������� */
51     gchar *filename; /* ������������������������������ */
52 aloha 1.32 GList *undoInfoList; /* ��������������������������������������� */
53 aloha 1.20 ScmObj env; /* ��������������������������� Scheme ������ */
54     guint delete_handler_id; /* ��������������������������������������������� ID */
55 aloha 1.16 } ShikiTabInfo;
56    
57 aloha 1.17 /* ������������������������������������������������������������������������������������������������������������������������������ */
58 aloha 1.16 struct {
59 aloha 1.17 GtkWidget *editor_window;
60 aloha 1.31 GtkClipboard *clipboard;
61 aloha 1.26 GtkNotebook *notebook;
62 aloha 1.17 GtkWidget *statusbar;
63     GtkWidget *modeline_label;
64     GList *tabInfoList;
65     gint current_tabpage_num;
66     ShikiTabInfo *current_tabpage_info;
67 aloha 1.16 } shiki_editor;
68    
69 aloha 1.25 /* ��������������������������������������������� */
70 aloha 1.32 #define Shiki_EDITOR_WINDOW shiki_editor.editor_window
71     #define Shiki_EDITOR_CLIPBOARD shiki_editor.clipboard
72     #define Shiki_EDITOR_NOTEBOOK shiki_editor.notebook
73     #define Shiki_EDITOR_STATUSBAR shiki_editor.statusbar
74     #define Shiki_EDITOR_MODELINE_LABEL shiki_editor.modeline_label
75     #define Shiki_EDITOR_TAB_INFO_LIST shiki_editor.tabInfoList
76 aloha 1.25
77     /* ��������������������������������������������������������������������������������������� */
78 aloha 1.32 #define Shiki_CURRENT_TAB_NUM shiki_editor.current_tabpage_num
79     #define Shiki_CURRENT_TAB_INFO shiki_editor.current_tabpage_info
80     #define Shiki_CURRENT_UNDO_INFO_LIST (shiki_editor.current_tabpage_info)->undoInfoList
81     #define Shiki_CURRENT_TAB (shiki_editor.current_tabpage_info)->tabpage
82     #define Shiki_CURRENT_TAB_TITLE (shiki_editor.current_tabpage_info)->tabpage_label
83     #define Shiki_CURRENT_TEXT_VIEW (shiki_editor.current_tabpage_info)->text_view
84     #define Shiki_CURRENT_TEXT_BUFFER (shiki_editor.current_tabpage_info)->text_buffer
85     #define Shiki_CURRENT_FILENAME (shiki_editor.current_tabpage_info)->filename
86     #define Shiki_CURRENT_SCHEME_ENV (shiki_editor.current_tabpage_info)->env
87 aloha 1.10
88 aloha 1.17 /* ������������������ */
89 aloha 1.25
90 aloha 1.27 /* ������������������������ : ���������������ShikiTabInfo ������������������������������������������������������������������ 2 ������������������������������������������ (������������������) */
91     static void append_tabpage(gchar *filename);
92     static void remove_tabpage();
93    
94 aloha 1.33 /* foo_bar_handler() ������������������������������������������������������ */
95     static void append_default_tabpage_handler();
96    
97 aloha 1.25 /* ������������������ */
98 aloha 1.27 static void open_file(gchar *filename);
99     static void open_file_handler();
100     static void save_file();
101     static void save_file_as();
102 aloha 1.25 static gchar *get_filename_from_dialog(const gchar *msg);
103    
104     /* ������������������������������ */
105     static gchar* get_all_buffer_contents(GtkTextBuffer *buffer);
106     static gboolean save_text_buffer(const gchar *filename, GtkTextBuffer *buffer);
107 aloha 1.17 static void clear_current_buffer();
108 aloha 1.33 static void undo();
109 aloha 1.25
110 aloha 1.32 static void insert_text_handler(GtkTextBuffer *buffer, GtkTextIter *p, gchar *str, gint len);
111     static void delete_range_handler(GtkTextBuffer *buffer, GtkTextIter *start, GtkTextIter *end);
112    
113 aloha 1.25 /* ������������������ */
114 aloha 1.17 static gint get_current_line_number(GtkTextBuffer *buffer);
115     static void update_modeline_label();
116     static void text_buffer_cursor_moved_handler();
117 aloha 1.25
118     /* ������ */
119 aloha 1.17 static void really_quit_dialog_yes(GtkWidget *widget, gboolean *flag);
120     static void really_quit_dialog_no(GtkWidget *widget, gint *flag);
121     static gboolean not_yet_save_changes_really_quit(GtkTextBuffer *buffer);
122 aloha 1.27 static gboolean delete_event_handler(GtkWidget *widget, GdkEvent *event, GtkTextBuffer *buffer);
123 aloha 1.25
124     /* Gauche ��� S ��������������������������������� */
125 aloha 1.17 static gchar *eval_cstring_by_gauche(gchar *s);
126 aloha 1.25 static gchar *load_cstring_by_gauche(gchar *s);
127     static void load_buffer_by_gauche();
128 aloha 1.27 static void load_region_by_gauche();
129     static void load_scheme_file_by_gauche();
130 aloha 1.17 static gboolean is_kakko_or_kokka(gunichar ch, gpointer);
131 aloha 1.29 static gboolean is_kakko(gunichar ch, gpointer);
132 aloha 1.17 static gboolean is_kokka(gunichar ch, gpointer);
133 aloha 1.30 static gboolean search_sexp(GtkTextIter *start, GtkTextIter *end);
134     static gboolean search_sexp_kokka(GtkTextIter *end);
135     static gboolean search_last_sexp(GtkTextIter *start, GtkTextIter *end);
136 aloha 1.29 static gboolean search_last_sexp_kakko(GtkTextIter *start);
137 aloha 1.17 static gint get_parent_nest_level_at_cursor(GtkTextBuffer *buffer);
138 aloha 1.25
139     /* ������ */
140     static void select_font();
141     static void font_selection_ok(GtkWidget *button, GtkWidget *font_dialog);
142 aloha 1.18 static void switch_tabpage_handler(GtkNotebook *notebook, GtkNotebookPage *page, guint pagenum) ;
143 aloha 1.17 static void tabsborder_on_off(GtkButton *button, GtkNotebook *notebook);
144 aloha 1.27 static void rotate_tab_position(GtkButton *button, GtkNotebook *notebook);
145 aloha 1.25
146     /* ��������������� ������������ ������ */
147 aloha 1.17 static void forward_current_buffer();
148     static void backward_current_buffer();
149     static void line_forward_current_buffer();
150     static void line_backward_current_buffer();
151 aloha 1.25
152     /* ������������������������ */
153 aloha 1.22 static gboolean signal_key_press_handler(GtkWidget *notebook, GdkEventKey *event, gpointer contextid);
154     static gboolean signal_key_release_handler(GtkWidget *notebook, GdkEventKey *event, gpointer contextid);
155 aloha 1.25
156     /* ������������������ */
157 aloha 1.27 static void open_online_help();
158 aloha 1.25 static void about_this_application();
159    
160 aloha 1.27 /* ������������������������ */
161 aloha 1.21 static void shiki_editor_window_init(int argc, char **argv);
162 aloha 1.14
163 aloha 1.32 /* ��������������������������������������������� */
164     static void insert_text_handler(GtkTextBuffer *buffer, GtkTextIter *iter, gchar *str, gint len) {
165     /* Undo ��������������������������� */
166     ShikiUndoInfo *undoInfo = g_malloc(sizeof(ShikiUndoInfo));
167     g_return_if_fail(undoInfo != NULL);
168     undoInfo->action = SHIKI_UNDO_INSERT;
169     undoInfo->str = g_strdup(str);
170     undoInfo->strlen = len;
171     undoInfo->start = gtk_text_iter_get_offset(iter);
172     undoInfo->end = undoInfo->start + undoInfo->strlen;
173     Shiki_CURRENT_UNDO_INFO_LIST = g_list_prepend(Shiki_CURRENT_UNDO_INFO_LIST, undoInfo);
174     g_print("insert : %s, len : %d, start : %d, end : %d\n", undoInfo->str, undoInfo->strlen, undoInfo->start, undoInfo->end);
175     }
176    
177     /* ������������������������������������������������ */
178     static void delete_range_handler(GtkTextBuffer *buffer, GtkTextIter *start, GtkTextIter *end) {
179     /* Undo ��������������������������� */
180     ShikiUndoInfo *undoInfo = g_malloc(sizeof(ShikiUndoInfo));
181     g_return_if_fail(undoInfo != NULL);
182     undoInfo->action = SHIKI_UNDO_DELETE;
183     undoInfo->str = gtk_text_buffer_get_text(buffer, start, end, FALSE);
184     undoInfo->start = gtk_text_iter_get_offset(start);
185     undoInfo->end = gtk_text_iter_get_offset(end);
186     undoInfo->strlen = end - start;
187     Shiki_CURRENT_UNDO_INFO_LIST = g_list_prepend(Shiki_CURRENT_UNDO_INFO_LIST, undoInfo);
188     g_print("delete : %s %d\n", undoInfo->str, undoInfo->strlen);
189     }
190    
191     /* ������������������������������������������������������������ */
192     static void switch_tabpage_handler(GtkNotebook *notebook, GtkNotebookPage *page, guint pagenum) {
193     /* ��������������������������������������������������������������������� */
194     Shiki_CURRENT_TAB_INFO = (ShikiTabInfo *)g_list_nth_data(Shiki_EDITOR_TAB_INFO_LIST, pagenum);
195    
196     /* ������������������������������������ */
197     Shiki_CURRENT_TAB_NUM = pagenum;
198    
199     /* ������������������������������������������������������ */
200     if(!Shiki_CURRENT_TAB_INFO) return;
201     gtk_window_set_title (GTK_WINDOW(Shiki_EDITOR_WINDOW), Shiki_CURRENT_FILENAME);
202    
203     update_modeline_label();
204     }
205    
206 aloha 1.33 static void undo() {
207     g_print("Undo\n");
208     GtkTextIter start, end;
209     ShikiUndoInfo *undoInfo = g_list_nth_data(Shiki_CURRENT_UNDO_INFO_LIST, 0);
210     if(!undoInfo) {
211     g_print("������������ Undo ���������������\n");
212     return;
213     }
214     gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &start, undoInfo->start);
215     gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &end, undoInfo->end);
216    
217     if(undoInfo->action == SHIKI_UNDO_INSERT) {
218     Shiki_CURRENT_UNDO_INFO_LIST = g_list_delete_link(Shiki_CURRENT_UNDO_INFO_LIST, g_list_first(Shiki_CURRENT_UNDO_INFO_LIST));
219     gtk_text_buffer_delete(Shiki_CURRENT_TEXT_BUFFER, &start, &end);
220     g_free(undoInfo->str);
221     g_free(undoInfo);
222     } else if(undoInfo->action == SHIKI_UNDO_DELETE) {
223     Shiki_CURRENT_UNDO_INFO_LIST = g_list_delete_link(Shiki_CURRENT_UNDO_INFO_LIST, g_list_first(Shiki_CURRENT_UNDO_INFO_LIST));
224     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &start, undoInfo->str, -1);
225     g_free(undoInfo->str);
226     g_free(undoInfo);
227     }
228    
229     }
230    
231 aloha 1.32 /* ��������������������� */
232     static gboolean signal_key_press_handler (GtkWidget *notebook, GdkEventKey *event, gpointer contextid) {
233     GtkTextIter start, end;
234    
235     /* ������������������������������������ */
236     gtk_text_buffer_get_start_iter(Shiki_CURRENT_TEXT_BUFFER, &start);
237     gtk_text_buffer_get_end_iter(Shiki_CURRENT_TEXT_BUFFER, &end);
238     gtk_text_buffer_remove_tag_by_name(Shiki_CURRENT_TEXT_BUFFER, "parent_emphasis_background", &start, &end);
239    
240     if(event->state & GDK_CONTROL_MASK && event->state & GDK_MOD1_MASK) {
241     switch(event->keyval) {
242     case GDK_at : /* C-M-SPC */
243     { GtkTextIter start, end;
244     if(!search_sexp(&start, &end)) return FALSE;
245     gtk_text_buffer_select_range(Shiki_CURRENT_TEXT_BUFFER, &start, &end);
246     }
247     break;
248     case GDK_space : /* C-M-SPC */
249     { GtkTextIter start, end;
250     if(!search_last_sexp(&start, &end)) return FALSE;
251     gtk_text_buffer_select_range(Shiki_CURRENT_TEXT_BUFFER, &start, &end);
252     }
253     break;
254     }
255     } else if(event->state & GDK_CONTROL_MASK) {
256     switch(event->keyval) {
257     case GDK_f : /* Ctrl + f : forward */
258     forward_current_buffer();
259     break;
260     case GDK_b : /* Ctrl + b : backward */
261     backward_current_buffer();
262     break;
263     case GDK_n : /* Ctrl + n : next line */
264     line_forward_current_buffer();
265     break;
266     case GDK_p : /* Ctrl + p : previous line */
267     line_backward_current_buffer();
268     break;
269     case GDK_h :
270     { GtkTextIter p;
271     gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
272     gtk_text_buffer_backspace(Shiki_CURRENT_TEXT_BUFFER, &p, FALSE, TRUE);
273     }
274     break;
275    
276     case GDK_e : /* Ctrl + e : eval-expression */
277     {
278     gchar *code;
279     GtkTextIter start, end;
280    
281     if(!search_sexp(&start, &end)) return FALSE;
282    
283     code = gtk_text_buffer_get_text(Shiki_CURRENT_TEXT_BUFFER, &start, &end, FALSE);
284     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &end, "\n\n", -1);
285     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &end, eval_cstring_by_gauche(code), -1);
286     g_free(code);
287     }
288     break;
289    
290     case GDK_j : /* Ctrl + j : eval-last-sexp */
291     {
292     gchar *code;
293     GtkTextIter start, end;
294    
295     if(!search_last_sexp(&start, &end)) return FALSE;
296    
297     code = gtk_text_buffer_get_text(Shiki_CURRENT_TEXT_BUFFER, &start, &end, FALSE);
298     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &end, "\n\n", -1);
299     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &end, eval_cstring_by_gauche(code), -1);
300     g_free(code);
301     }
302     break;
303    
304     case GDK_underscore : /* Ctrl + _ : Undo */
305 aloha 1.33 undo();
306 aloha 1.32 break;
307    
308     case GDK_t : /* Ctrl + t : ��������������� */
309 aloha 1.33 append_default_tabpage_handler();
310 aloha 1.32 break;
311    
312     case GDK_k : /* Ctrl + k : ������������������ */
313     remove_tabpage();
314     break;
315    
316     case GDK_w : /* Ctrl + w : ��������� */
317     gtk_text_buffer_cut_clipboard(Shiki_CURRENT_TEXT_BUFFER, Shiki_EDITOR_CLIPBOARD, TRUE);
318     break;
319    
320     case GDK_y : /* Ctrl + y : ��������� */
321     {GtkTextIter p;
322     gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
323     gtk_text_buffer_paste_clipboard(Shiki_CURRENT_TEXT_BUFFER, Shiki_EDITOR_CLIPBOARD, &p, TRUE);
324     }
325     break;
326     }
327     }
328     return FALSE;
329     }
330    
331    
332 aloha 1.23 /* ��������������������������������������� (������������) ��������� */
333 aloha 1.27 static void append_tabpage(gchar *filename) {
334 aloha 1.24 /*-------------------- ������������������������ ----------------------------------*/
335     /* ShikiTabInfo ������������������������������������������������������������������ */
336     ShikiTabInfo *tabinfo = g_malloc(sizeof(ShikiTabInfo));
337 aloha 1.32 tabinfo->undoInfoList = NULL;
338 aloha 1.23 tabinfo->filename = filename;
339     tabinfo->tabpage_label = g_path_get_basename(filename);
340 aloha 1.24
341     /* ������������������������������ (������������������������) ��������� */
342     tabinfo->tabpage = GTK_SCROLLED_WINDOW(gtk_scrolled_window_new(NULL, NULL));
343     gtk_scrolled_window_set_policy (tabinfo->tabpage, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
344    
345     /* ��������������������������������������������������������������������������������������� */
346     tabinfo->text_view = GTK_TEXT_VIEW(gtk_text_view_new());
347     tabinfo->text_buffer = gtk_text_view_get_buffer(tabinfo->text_view);
348 aloha 1.33
349 aloha 1.24 gtk_container_add(GTK_CONTAINER(tabinfo->tabpage), GTK_WIDGET(tabinfo->text_view));
350     gtk_widget_set_size_request(GTK_WIDGET(tabinfo->text_view), 500, 500);
351     g_signal_connect(tabinfo->text_buffer, "mark_set", G_CALLBACK(text_buffer_cursor_moved_handler), tabinfo->text_view);
352 aloha 1.32 g_signal_connect(tabinfo->text_buffer, "insert-text", G_CALLBACK(insert_text_handler), NULL);
353     g_signal_connect(tabinfo->text_buffer, "delete-range", G_CALLBACK(delete_range_handler), NULL);
354    
355 aloha 1.24 /* ������������������������������������������������������������������������������������������������������ */
356     tabinfo->delete_handler_id = g_signal_connect(Shiki_EDITOR_WINDOW, "delete_event", G_CALLBACK(delete_event_handler), tabinfo->text_buffer);
357 aloha 1.33
358 aloha 1.24 /* ������������������������ */
359     /* ������������������������������������������������ */
360     gtk_text_buffer_create_tag (tabinfo->text_buffer, "parent_emphasis_background", "background", "green", NULL);
361    
362     /* ������������������������������������������ */
363 aloha 1.27 gtk_notebook_append_page(Shiki_EDITOR_NOTEBOOK, GTK_WIDGET(tabinfo->tabpage), gtk_label_new(tabinfo->tabpage_label));
364 aloha 1.24 /* ������������������������������������������������������������������ */
365 aloha 1.23 Shiki_EDITOR_TAB_INFO_LIST = g_list_append(Shiki_EDITOR_TAB_INFO_LIST, tabinfo);
366 aloha 1.32
367 aloha 1.27 gtk_widget_show_all(GTK_WIDGET(Shiki_EDITOR_NOTEBOOK));
368 aloha 1.24 /* ��������������������������������� */
369 aloha 1.27 gtk_notebook_set_current_page(Shiki_EDITOR_NOTEBOOK, g_list_length(Shiki_EDITOR_TAB_INFO_LIST) - 1);
370 aloha 1.23 }
371    
372 aloha 1.33 static void append_default_tabpage_handler() {
373     append_tabpage(g_strdup("*scratch*"));
374     }
375    
376 aloha 1.23 /* ������������������������������������������ (������������) ��������� */
377 aloha 1.27 static void remove_tabpage() {
378 aloha 1.23 /* ��������� 1 ��������������������������������������������������� */
379     if(g_list_length(Shiki_EDITOR_TAB_INFO_LIST) == 1)
380     return;
381     if(!not_yet_save_changes_really_quit(Shiki_CURRENT_TEXT_BUFFER)) {
382     /* ��������������������������������������������������������������������������������������������� */
383     g_signal_handler_disconnect(Shiki_EDITOR_WINDOW, (Shiki_CURRENT_TAB_INFO)->delete_handler_id);
384     /* ������������������������ */
385     gtk_widget_destroy(GTK_WIDGET(Shiki_CURRENT_TEXT_VIEW));
386     /* ������������������������������������������ */
387     g_free(Shiki_CURRENT_TAB_TITLE);
388     g_free(Shiki_CURRENT_FILENAME);
389     Shiki_EDITOR_TAB_INFO_LIST = g_list_delete_link(Shiki_EDITOR_TAB_INFO_LIST, g_list_nth(Shiki_EDITOR_TAB_INFO_LIST, Shiki_CURRENT_TAB_NUM));
390     g_free(Shiki_CURRENT_TAB_INFO);
391    
392     /* ������������������������������������ */
393     Shiki_CURRENT_TAB_INFO = g_list_nth_data(Shiki_EDITOR_TAB_INFO_LIST, Shiki_CURRENT_TAB_NUM);
394 aloha 1.27 gtk_notebook_remove_page(Shiki_EDITOR_NOTEBOOK, Shiki_CURRENT_TAB_NUM);
395 aloha 1.23 /* ��������������������������������������� */
396 aloha 1.27 gtk_widget_queue_draw(GTK_WIDGET(Shiki_EDITOR_NOTEBOOK));
397 aloha 1.23 }
398     }
399    
400 aloha 1.15 /* ������������������������������ */
401     static void clear_current_buffer() {
402     GtkTextIter start, end;
403 aloha 1.16 gtk_text_buffer_get_start_iter(Shiki_CURRENT_TEXT_BUFFER, &start);
404     gtk_text_buffer_get_end_iter(Shiki_CURRENT_TEXT_BUFFER, &end);
405     gtk_text_buffer_delete(Shiki_CURRENT_TEXT_BUFFER, &start, &end);
406 aloha 1.15 }
407    
408 aloha 1.14 /* ������������������������������������ */
409     static void load_buffer_by_gauche() {
410     GtkTextIter p;
411 aloha 1.16 gtk_text_buffer_get_end_iter(Shiki_CURRENT_TEXT_BUFFER, &p);
412     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &p, "\n\n", -1);
413     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &p, load_cstring_by_gauche(get_all_buffer_contents(Shiki_CURRENT_TEXT_BUFFER)), -1);
414 aloha 1.14 }
415    
416     /* ������������������������ */
417 aloha 1.27 static void load_scheme_file_by_gauche() {
418 aloha 1.14 gchar *contents, *text;
419     gsize br, bw, len;
420     GError *err = NULL;
421     gchar *filename = get_filename_from_dialog("File Selection");
422     GtkTextIter p;
423    
424     if(!filename) return;
425 aloha 1.33
426 aloha 1.16 gtk_text_buffer_get_end_iter(Shiki_CURRENT_TEXT_BUFFER, &p);
427     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &p, "\n\n", -1);
428 aloha 1.14
429     if(g_file_get_contents(filename, &contents, &len, NULL)) {
430     if(!(text = g_locale_to_utf8(contents, -1, &br, &bw, &err)))
431 aloha 1.16 gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &p, load_cstring_by_gauche(text), -1);
432 aloha 1.14 else
433 aloha 1.16 gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &p, load_cstring_by_gauche(contents), -1);
434 aloha 1.14 }
435     g_free(text); g_free(contents); g_free(filename);
436     }
437    
438 aloha 1.13 /* gauche ������������������������������������ */
439     static gchar *load_cstring_by_gauche(gchar *s) {
440     gchar *msg;
441    
442     ScmObj result, error;
443     /* ��������������������������������� */
444     ScmObj is = Scm_MakeInputStringPort(SCM_STRING(SCM_MAKE_STR(s)), TRUE);
445     /* ������������������������������ */
446     ScmObj os = Scm_MakeOutputStringPort(TRUE);
447    
448     Scm_Define(Scm_UserModule(), SCM_SYMBOL(SCM_INTERN("*input*")), is);
449     Scm_Define(Scm_UserModule(), SCM_SYMBOL(SCM_INTERN("*error*")), SCM_FALSE);
450     /* Scheme ��������������������������������������������������������������������������������� S ��������������������������������������������������������������������������� *error* ������������������ */
451     result = Scm_EvalCString("(guard (e (else (set! *error* e) #f)) (eval (load-from-port *input*) (current-module)))", SCM_OBJ(Scm_UserModule()));
452    
453     error = Scm_GlobalVariableRef(Scm_UserModule(), SCM_SYMBOL(SCM_INTERN("*error*")), 0);
454    
455     /* ��������������������������������������������������������� */
456     if (!SCM_FALSEP(error))
457     Scm_Write(error, os, SCM_WRITE_DISPLAY);
458     else
459     Scm_Write(result, os, SCM_WRITE_DISPLAY);
460    
461     msg = Scm_GetString(SCM_STRING(Scm_GetOutputString(SCM_PORT(os))));
462     /* ������������������ */
463     Scm_ClosePort(SCM_PORT(is));
464     Scm_ClosePort(SCM_PORT(os));
465    
466     return msg;
467     }
468    
469 aloha 1.12 static void font_selection_ok(GtkWidget *button, GtkWidget *font_dialog) {
470     gchar *font_name = gtk_font_selection_dialog_get_font_name (GTK_FONT_SELECTION_DIALOG (font_dialog));
471     if(font_name) {
472     GtkRcStyle *style = gtk_rc_style_new ();
473     pango_font_description_free(style->font_desc);
474     style->font_desc = pango_font_description_from_string(font_name);
475 aloha 1.16 gtk_widget_modify_style (GTK_WIDGET(Shiki_CURRENT_TEXT_VIEW), style);
476 aloha 1.12 g_free (font_name);
477     }
478     }
479    
480 aloha 1.14 /* ������������������������������������������������������ */
481 aloha 1.12 static void select_font(){
482     GtkWidget *font_dialog = gtk_font_selection_dialog_new("Font Selection Dialog");
483     g_signal_connect (GTK_FONT_SELECTION_DIALOG (font_dialog)->ok_button, "clicked", G_CALLBACK(font_selection_ok), font_dialog);
484     gtk_dialog_run(GTK_DIALOG(font_dialog));
485     gtk_widget_destroy(font_dialog);
486     }
487    
488 aloha 1.11 /* ������������������������������������������ */
489     static void about_this_application() {
490     GtkAboutDialog *about = GTK_ABOUT_DIALOG(gtk_about_dialog_new());
491     const gchar *authors[] = {
492 aloha 1.23 "������������ (���������) <alohakun@gmail.com>\n",
493     "Contribute : tkng ������",
494     "(http://d.hatena.ne.jp/tkng/20061113)", NULL
495 aloha 1.11 };
496     gtk_about_dialog_set_authors(about, authors);
497     gtk_about_dialog_set_copyright(about, "Copyright(C)2006 WAKATSUKI Toshihiro");
498     gtk_about_dialog_set_name(about, "��� (SHIKI)");
499     gtk_about_dialog_set_website_label(about, "���������30������������������������������������������������������������Blog");
500     gtk_about_dialog_set_website(about, "http://alohakun.blog7.fc2.com/blog-category-29.html");
501     gtk_dialog_run(GTK_DIALOG(about));
502     gtk_widget_destroy(GTK_WIDGET(about));
503     }
504    
505 aloha 1.10 /* ��������������������������������������������������������������� */
506     static gint get_current_line_number(GtkTextBuffer *b) {
507     GtkTextIter p;
508     gtk_text_buffer_get_iter_at_mark(b, &p, gtk_text_buffer_get_insert(b));
509     return gtk_text_iter_get_line(&p) + 1;
510     }
511    
512     /* ��������������������������������������������������������������� */
513     static void update_modeline_label() {
514 aloha 1.16 gchar* basename = g_path_get_basename(Shiki_CURRENT_TAB_TITLE);
515 aloha 1.11 gchar* l = g_strdup_printf("-E:%s %-10s (Gauche Interaction)--L%d--------------------------------------",
516 aloha 1.16 gtk_text_buffer_get_modified(Shiki_CURRENT_TEXT_BUFFER) ? "**" : "--",
517     basename, get_current_line_number(Shiki_CURRENT_TEXT_BUFFER));
518     gtk_label_set_text(GTK_LABEL(Shiki_EDITOR_MODELINE_LABEL), l);
519 aloha 1.10 g_free(l); g_free(basename);
520     }
521    
522     static void text_buffer_cursor_moved_handler(){
523     update_modeline_label();
524     }
525 aloha 1.1
526     /* ��������������������������������������������������������������� */
527     static gchar* get_all_buffer_contents(GtkTextBuffer *buffer) {
528     GtkTextIter start, end;
529     gtk_text_buffer_get_start_iter(buffer, &start);
530     gtk_text_buffer_get_end_iter(buffer, &end);
531     return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
532     }
533    
534     /* buffer ������������������������ filename ��������� */
535     static gboolean save_text_buffer(const gchar *filename, GtkTextBuffer *buffer) {
536     gchar *contents, *text;
537     gsize br, bw;
538     GError *err = NULL;
539    
540     if(!filename) return FALSE;
541     contents = get_all_buffer_contents(buffer);
542     text = g_locale_from_utf8(contents, -1, &br, &bw, &err);
543     /* ��������������������������������� */
544     g_file_set_contents(filename, text, -1, NULL);
545     gtk_text_buffer_set_modified(buffer, FALSE);
546 aloha 1.10 update_modeline_label();
547 aloha 1.1 g_free(contents); g_free(text);
548     return TRUE;
549     }
550    
551     /* ������������������������������������������������������msg ������������������������������������ */
552     static gchar *get_filename_from_dialog(const gchar *msg) {
553    
554     GtkWidget *dialog = gtk_file_selection_new(msg);
555     int resp = gtk_dialog_run(GTK_DIALOG(dialog));
556     gchar *filename = NULL;
557    
558     /* gtk_file_selection_get_filename ������������������������������������������������������������������������������������������������������������������������������ */
559     if(resp == GTK_RESPONSE_OK)
560     filename = g_strdup(gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog)));
561    
562     gtk_widget_destroy(dialog);
563     return filename;
564     }
565    
566     /* ��������������������������������������������������������������������� */
567 aloha 1.27 static void save_file() {
568 aloha 1.1
569 aloha 1.22 /* ��������������������������������������� */
570     if(g_ascii_strcasecmp("*help*", Shiki_CURRENT_TAB_TITLE) == 0) return;
571 aloha 1.33
572 aloha 1.1 /* ������������������������������������ */
573 aloha 1.16 if(!gtk_text_buffer_get_modified(Shiki_CURRENT_TEXT_BUFFER)) return;
574 aloha 1.1
575     /* ������������������������������������������������������������������������������������������������������ */
576 aloha 1.16 if(g_ascii_strcasecmp("*scratch*", Shiki_CURRENT_TAB_TITLE) == 0) {
577 aloha 1.7 gchar *filename = get_filename_from_dialog("Save File As ...");
578     if(!filename) return;
579 aloha 1.16 if(!save_text_buffer(filename, Shiki_CURRENT_TEXT_BUFFER)) return;
580 aloha 1.27 gtk_notebook_set_tab_label_text(Shiki_EDITOR_NOTEBOOK, GTK_WIDGET(Shiki_CURRENT_TAB), filename);
581 aloha 1.16 gtk_window_set_title (GTK_WINDOW(Shiki_EDITOR_WINDOW), filename);
582 aloha 1.1 g_free(filename);
583     } else
584 aloha 1.16 save_text_buffer(Shiki_CURRENT_TAB_TITLE, Shiki_CURRENT_TEXT_BUFFER);
585 aloha 1.1 }
586    
587     /* ��������������������������������������������������������������������������� */
588 aloha 1.27 static void save_file_as() {
589 aloha 1.1 gchar *filename = get_filename_from_dialog("Save File As ...");
590    
591 aloha 1.7 if(!filename) return;
592 aloha 1.16 if(!save_text_buffer(filename, Shiki_CURRENT_TEXT_BUFFER)) return;
593 aloha 1.1
594 aloha 1.27 gtk_notebook_set_tab_label_text(Shiki_EDITOR_NOTEBOOK, GTK_WIDGET(Shiki_CURRENT_TAB), filename);
595 aloha 1.16 gtk_window_set_title (GTK_WINDOW (Shiki_EDITOR_WINDOW), filename);
596 aloha 1.1
597     g_free(filename);
598     }
599    
600     /* YES ������������NO ������������������������������������ callback */
601 aloha 1.17 static void really_quit_dialog_yes(GtkWidget *widget, gboolean *flag){*flag = FALSE;}
602     static void really_quit_dialog_no(GtkWidget *widget, gint *flag){*flag = TRUE;}
603 aloha 1.1
604     /* ��������������������������������������������� ? */
605 aloha 1.17 static gboolean not_yet_save_changes_really_quit(GtkTextBuffer *buffer) {
606 aloha 1.1 GtkWidget *yes_button, *no_button;
607     static GtkWidget *dialog_window = NULL;
608    
609     /* ��������������������������������������� */
610     if(!gtk_text_buffer_get_modified(buffer)) return FALSE;
611    
612     if(dialog_window == NULL) {
613     gboolean flag = TRUE;
614     dialog_window = gtk_dialog_new ();
615    
616     /* ��������������������������������������������� ? ��������������������������� */
617     g_signal_connect(G_OBJECT(dialog_window), "delete_event", G_CALLBACK(gtk_false), NULL);
618     g_signal_connect(G_OBJECT(dialog_window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
619 aloha 1.5 gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog_window)->vbox),
620     gtk_label_new("������������������������������������������\n��������������������������������������� ?"), TRUE, TRUE, 0);
621 aloha 1.1 gtk_window_set_title(GTK_WINDOW (dialog_window), "Really Quit ?");
622     /* YES ������������ */
623 aloha 1.8 yes_button = gtk_button_new_with_mnemonic("������ (_Y)");
624 aloha 1.1 g_signal_connect(GTK_OBJECT(yes_button), "clicked", G_CALLBACK(really_quit_dialog_yes), &flag);
625     g_signal_connect_swapped(GTK_OBJECT(yes_button), "clicked", G_CALLBACK(gtk_widget_destroy), G_OBJECT(dialog_window));
626     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->action_area), yes_button, TRUE, TRUE, 0);
627    
628     /* NO ������������ */
629 aloha 1.8 no_button = gtk_button_new_with_mnemonic("��������� (_N)");
630 aloha 1.1 g_signal_connect(GTK_OBJECT(no_button), "clicked", G_CALLBACK(really_quit_dialog_no), &flag);
631     g_signal_connect_swapped(GTK_OBJECT(no_button), "clicked", G_CALLBACK(gtk_widget_destroy), G_OBJECT(dialog_window));
632     gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialog_window)->action_area), no_button, TRUE, TRUE, 0);
633    
634     gtk_window_set_modal(GTK_WINDOW(dialog_window), TRUE);
635 aloha 1.16 gtk_window_set_transient_for(GTK_WINDOW(dialog_window), GTK_WINDOW (Shiki_EDITOR_WINDOW));
636 aloha 1.1
637     gtk_widget_show_all(dialog_window);
638     gtk_main ();
639     dialog_window = NULL;
640    
641     /* "delete_event" ��������������� FALSE ���������"destory" ������������������window ������������������ */
642     return flag;
643     }
644     return TRUE;
645     }
646    
647     /* ������������������������������������������������������������������������������������������ */
648 aloha 1.27 static gboolean delete_event_handler(GtkWidget *widget, GdkEvent *event, GtkTextBuffer *buffer){
649     return not_yet_save_changes_really_quit(buffer);
650 aloha 1.1 }
651    
652     /* ��������������������� */
653 aloha 1.27 static void open_file(gchar *filename) {
654 aloha 1.1 gchar *contents, *text;
655     gsize br, bw, len;
656     GError *err = NULL;
657 aloha 1.33
658 aloha 1.27 g_return_if_fail(filename != NULL);
659 aloha 1.33
660 aloha 1.1 if(g_file_get_contents(filename, &contents, &len, NULL)) {
661     GtkTextIter p;
662 aloha 1.3
663 aloha 1.1 /* ������������������������������ */
664 aloha 1.27 append_tabpage(g_strdup(filename));
665 aloha 1.1
666     if(!(text = g_locale_to_utf8(contents, -1, &br, &bw, &err)))
667 aloha 1.17 gtk_text_buffer_set_text(Shiki_CURRENT_TEXT_BUFFER, contents, len);
668 aloha 1.1 else
669 aloha 1.17 gtk_text_buffer_set_text(Shiki_CURRENT_TEXT_BUFFER, text, len);
670 aloha 1.1
671     /* ������������������������ */
672 aloha 1.17 gtk_text_buffer_set_modified(Shiki_CURRENT_TEXT_BUFFER, FALSE);
673 aloha 1.1 /* ������������������������������ */
674 aloha 1.17 gtk_text_buffer_get_start_iter(Shiki_CURRENT_TEXT_BUFFER, &p);
675     gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p);
676 aloha 1.10 update_modeline_label();
677 aloha 1.16 gtk_window_set_title (GTK_WINDOW (Shiki_EDITOR_WINDOW), filename);
678 aloha 1.27 gtk_widget_show_all(GTK_WIDGET(Shiki_EDITOR_NOTEBOOK));
679 aloha 1.1 g_free(contents); g_free(text); g_free(filename);
680     } else
681     g_printerr("Get file contents error !\n");
682     }
683    
684 aloha 1.21 /* ��������������������������������������������������� */
685 aloha 1.27 static void open_file_handler() {
686 aloha 1.21 gchar *filename = get_filename_from_dialog("File Selection");
687    
688     if(!filename) return;
689 aloha 1.27 open_file(filename);
690 aloha 1.1 }
691    
692     /* gauche ��������������������������������� */
693     static gchar *eval_cstring_by_gauche(gchar *s) {
694     gchar *msg;
695    
696     ScmObj result, error;
697     /* ������������������������������ */
698     ScmObj os = Scm_MakeOutputStringPort(TRUE);
699    
700     /* Scheme ��������������������������������������� */
701     /* http://alohakun.blog7.fc2.com/blog-entry-517.html */
702     Scm_Define(Scm_UserModule(), SCM_SYMBOL(SCM_INTERN("*input*")), SCM_MAKE_STR(s));
703     Scm_Define(Scm_UserModule(), SCM_SYMBOL(SCM_INTERN("*error*")), SCM_FALSE);
704    
705     result = Scm_EvalCString("(guard (e (else (set! *error* e) #f)) (eval (read-from-string *input*) (current-module)))", SCM_OBJ(Scm_UserModule()));
706    
707     error = Scm_GlobalVariableRef(Scm_UserModule(), SCM_SYMBOL(SCM_INTERN("*error*")), 0);
708    
709     /* ��������������������������������������������������������� */
710     if (!SCM_FALSEP(error))
711     Scm_Write(error, os, SCM_WRITE_DISPLAY);
712     else
713     Scm_Write(result, os, SCM_WRITE_DISPLAY);
714    
715     msg = Scm_GetString(SCM_STRING(Scm_GetOutputString(SCM_PORT(os))));
716     /* ������������������ */
717     Scm_ClosePort(SCM_PORT(os));
718    
719     return msg;
720     }
721    
722 aloha 1.13 /* ������������������������������������������������������������������ S ��������������� (������������) */
723 aloha 1.27 static void load_region_by_gauche() {
724 aloha 1.1
725     GtkTextIter start, end, p;
726     gchar *code;
727 aloha 1.16 gtk_text_buffer_get_end_iter(Shiki_CURRENT_TEXT_BUFFER, &p);
728     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &p, "\n\n", -1);
729 aloha 1.1
730     /* ������������������������������������������������������������ */
731 aloha 1.16 if(gtk_text_buffer_get_selection_bounds(Shiki_CURRENT_TEXT_BUFFER, &start, &end)) {
732     code = gtk_text_buffer_get_text(Shiki_CURRENT_TEXT_BUFFER, &start, &end, FALSE);
733     gtk_text_buffer_insert(Shiki_CURRENT_TEXT_BUFFER, &p, load_cstring_by_gauche(code), -1);
734 aloha 1.1 g_free(code);
735     }
736     }
737    
738     // GtkTextCharPredicate
739     static gboolean is_kakko_or_kokka(gunichar ch, gpointer p) {
740     return ch == '(' || ch == ')';
741     }
742 aloha 1.29 static gboolean is_kakko(gunichar ch, gpointer p) {return ch == '(';}
743 aloha 1.1 static gboolean is_kokka(gunichar ch, gpointer p) {return ch == ')';}
744    
745 aloha 1.30 /* ��������������������� '(' ��������������� ')' ������������������ (S ���) ��������������� */
746     static gboolean search_sexp(GtkTextIter *start, GtkTextIter *end) {
747 aloha 1.33
748 aloha 1.30 /* ������������������������������ */
749     gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER, start, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
750    
751     if(gtk_text_iter_get_char(start) != '(')
752     gtk_text_iter_forward_find_char(start, is_kakko, NULL, NULL);
753    
754     *end = *start;
755    
756     /* ��������������������������������� S ������������������ */
757     if(!search_sexp_kokka(end)) return FALSE;
758     gtk_text_iter_forward_char(end);
759     return TRUE;
760     }
761    
762     static gboolean search_sexp_kokka(GtkTextIter *end) {
763 aloha 1.29 gint nest_level = 0;
764 aloha 1.33
765 aloha 1.30 /* ������������ ')' ��������� */
766 aloha 1.29 while(1) {
767 aloha 1.30 if(!gtk_text_iter_forward_find_char(end, is_kakko_or_kokka, NULL, NULL))
768 aloha 1.29 return FALSE;
769    
770 aloha 1.30 if(gtk_text_iter_get_char(end) == '(')
771 aloha 1.29 nest_level++;
772     else {
773     if(!nest_level)
774     break;
775     else
776     nest_level--;
777     }
778     }
779     return TRUE;
780     }
781 aloha 1.1
782 aloha 1.30 /* ��������������������� ')' ��������������� '(' ������������������ (S ���) ��������������� */
783     static gboolean search_last_sexp(GtkTextIter *start, GtkTextIter *end) {
784    
785     /* ������������������������������ */
786     gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER, end, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
787    
788     gtk_text_iter_backward_char(end);
789 aloha 1.33
790 aloha 1.30 if(gtk_text_iter_get_char(end) != ')')
791     gtk_text_iter_backward_find_char(end, is_kokka, NULL, NULL);
792     *start = *end;
793     gtk_text_iter_forward_char(end);
794    
795     /* ��������������������������������� S ������������������ */
796     if(!search_last_sexp_kakko(start)) return FALSE;
797    
798     return TRUE;
799     }
800    
801 aloha 1.1 /* ')' ��������������� '(' ������������������ (S ���) ��������������� */
802 aloha 1.29 static gboolean search_last_sexp_kakko(GtkTextIter *start) {
803 aloha 1.1 gint nest_level = 0;
804 aloha 1.30 /* ��������������������������������������� ')' ��������� */
805 aloha 1.1 while(1) {
806     if(!gtk_text_iter_backward_find_char(start, is_kakko_or_kokka, NULL, NULL))
807     return FALSE;
808    
809     if(gtk_text_iter_get_char(start) == ')')
810     nest_level++;
811     else {
812     if(!nest_level)
813     break;
814     else
815     nest_level--;
816     }
817     }
818     return TRUE;
819     }
820    
821     /* ������������������������������������������������ */
822     static gint get_parent_nest_level_at_cursor(GtkTextBuffer *buffer) {
823     gint nest_level = 0;
824     GtkTextIter start, end;
825     gtk_text_buffer_get_start_iter(buffer, &start);
826     if(gtk_text_iter_get_char(&start) == '(') nest_level++;
827    
828     /* ��������������������� (= end) ��������� */
829     gtk_text_buffer_get_iter_at_mark(buffer,&end, gtk_text_buffer_get_insert(buffer));
830    
831     while(1) {
832     /* end ������ '(' ��� ')' ��������������������������������������������� */
833     if(!gtk_text_iter_forward_find_char(&start, is_kakko_or_kokka, NULL, &end))
834     return nest_level;
835    
836     if(gtk_text_iter_get_char(&start) == '(')
837     nest_level++;
838     else
839     nest_level--;
840     }
841     }
842    
843     /* ��������������������������������� on/off */
844     static void tabsborder_on_off(GtkButton *button, GtkNotebook *notebook) {
845     gint tval = FALSE;
846     gint bval = FALSE;
847     if(notebook->show_tabs == FALSE)
848     tval = TRUE;
849     if(notebook->show_border == FALSE)
850     bval = TRUE;
851    
852     gtk_notebook_set_show_tabs(notebook, tval);
853     gtk_notebook_set_show_border(notebook, bval);
854     }
855    
856     /* ������������������������ */
857 aloha 1.17 static void rotate_tab_position(GtkButton *button, GtkNotebook *notebook ) {
858 aloha 1.1 gtk_notebook_set_tab_pos(notebook, (notebook->tab_pos + 1) % 4);
859     }
860    
861     /* ������������������������������������������ */
862    
863 aloha 1.3 /* ��������������������� ^npfb */
864 aloha 1.7 static void forward_current_buffer() {
865 aloha 1.3 GtkTextIter p;
866 aloha 1.16 gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
867 aloha 1.3 gtk_text_iter_forward_char(&p);
868 aloha 1.16 gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p);
869 aloha 1.3 }
870 aloha 1.7 static void backward_current_buffer() {
871 aloha 1.3 GtkTextIter p;
872 aloha 1.16 gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
873 aloha 1.3 gtk_text_iter_backward_char(&p);
874 aloha 1.16 gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p);
875 aloha 1.3 }
876 aloha 1.7 static void line_forward_current_buffer() {
877 aloha 1.3 GtkTextIter p;
878 aloha 1.16 gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER, &p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
879     gtk_text_view_forward_display_line(Shiki_CURRENT_TEXT_VIEW, &p);
880     gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p);
881 aloha 1.7 }
882     static void line_backward_current_buffer() {
883 aloha 1.3 GtkTextIter p;
884 aloha 1.16 gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
885     gtk_text_view_backward_display_line(Shiki_CURRENT_TEXT_VIEW, &p);
886     gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p);
887 aloha 1.3 }
888    
889 aloha 1.1 /* ��������������������� */
890 aloha 1.8 static gboolean signal_key_release_handler (GtkWidget *notebook, GdkEventKey *event, gpointer contextid) {
891     static gint metakey_pressed = 0;
892 aloha 1.6 static gint controlx_pressed = 0;
893 aloha 1.1
894     if(event->keyval == GDK_parenright && event->state & GDK_SHIFT_MASK) {
895     GtkTextIter start, end;
896    
897     /* ������������������������������ */
898 aloha 1.16 gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER, &end, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER));
899 aloha 1.1
900     start = end;
901     gtk_text_iter_backward_char(&start);
902    
903     /* ��������������������������������� S ������������������ */
904 aloha 1.29 if(!search_last_sexp_kakko(&start)) return FALSE;
905 aloha 1.1
906 aloha 1.16 gtk_text_buffer_apply_tag_by_name(Shiki_CURRENT_TEXT_BUFFER, "parent_emphasis_background", &start, &end);
907 aloha 1.1 }
908    
909     /* ������������������������������������������������������������������������������������������������ (���������������) ������������������ */
910     if(event->keyval == GDK_Return) {
911 aloha 1.16 gint indentWidth = get_parent_nest_level_at_cursor(Shiki_CURRENT_TEXT_BUFFER) * editor_indent_width;
912 aloha 1.1 gchar *indent = g_strnfill(indentWidth, ' ');
913 aloha 1.16 gtk_text_buffer_insert_at_cursor(Shiki_CURRENT_TEXT_BUFFER, indent, -1);
914 aloha 1.1 g_free(indent);
915     }
916    
917 aloha 1.6 /* C-x */
918     if(event->keyval == GDK_x && event->state & GDK_CONTROL_MASK) {
919     controlx_pressed++;
920 aloha 1.16 gtk_statusbar_push(GTK_STATUSBAR(Shiki_EDITOR_STATUSBAR), GPOINTER_TO_INT(contextid), "C-x -");
921 aloha 1.6 } else if(event->state & GDK_CONTROL_MASK) {
922 aloha 1.8
923 aloha 1.6 if(controlx_pressed > 0) {
924     switch(event->keyval) {
925     case GDK_c :/* C-x C-c : ������ */
926 aloha 1.16 gtk_statusbar_push(GTK_STATUSBAR(Shiki_EDITOR_STATUSBAR), GPOINTER_TO_INT(contextid), "C-c");
927 aloha 1.6 {/* "delete-event" ��������������������������������������� �� ������������������������������������ */
928     GdkEvent ev;
929    
930     ev.any.type = GDK_DELETE;
931 aloha 1.16 ev.any.window = Shiki_EDITOR_WINDOW->window;
932 aloha 1.6 ev.any.send_event = FALSE;
933     gdk_event_put (&ev);
934     }
935     break;
936    
937     case GDK_f : /* C-x C-f : ������������������ */
938 aloha 1.16 gtk_statusbar_push(GTK_STATUSBAR(Shiki_EDITOR_STATUSBAR), GPOINTER_TO_INT(contextid), "C-f");
939 aloha 1.27 open_file_handler();
940 aloha 1.6 break;
941    
942     case GDK_s : /* C-x C-s : ������������������ */
943 aloha 1.16 gtk_statusbar_push(GTK_STATUSBAR(Shiki_EDITOR_STATUSBAR), GPOINTER_TO_INT(contextid), "C-s");
944 aloha 1.27 save_file();
945 aloha 1.6 break;
946    
947     case GDK_w : /* C-x C-w : ������������������������ */
948 aloha 1.16 gtk_statusbar_push(GTK_STATUSBAR(Shiki_EDITOR_STATUSBAR), GPOINTER_TO_INT(contextid), "C-w");
949 aloha 1.27 save_file_as();
950 aloha 1.6 break;
951     }
952     controlx_pressed = 0;
953     }
954 aloha 1.8
955     switch(event->keyval) {
956     case GDK_g :/* C-g : ��������������� */
957     metakey_pressed = 0;
958     controlx_pressed = 0;
959    
960 aloha 1.16 gtk_statusbar_push(GTK_STATUSBAR(Shiki_EDITOR_STATUSBAR), GPOINTER_TO_INT(contextid), "Quit");
961 aloha 1.8 break;
962     }
963    
964 aloha 1.6 }
965 aloha 1.1 return FALSE;
966     }
967 aloha 1.27 static void open_online_help() {
968 aloha 1.22 GtkTextIter p;
969 aloha 1.27 append_tabpage(g_strdup("*help*"));
970 aloha 1.22 gtk_text_buffer_set_text(Shiki_CURRENT_TEXT_BUFFER,
971 aloha 1.30 "������\n"
972     "$ ./shiki [file1 file2 ....]\n\n"
973     "[���������������������������] ��������������������� (C-x C-f)\n"
974     "[������������������������������������������] ��������������������� (C-x C-s)\n"
975     "[���������������������������������������������] ��������������������������� (C-x C-w)\n"
976     "[���������������������] ��������������� gauche ������������ (region-compile)\n"
977     "[��������������� (���������) ���������������] ��������� on/off\n"
978     "[������ (���������) ���������������] ������������������������\n"
979 aloha 1.33 "[������ + ������������] ��������������������������� (C-t) (tab)\n"
980     "[���������������������������] ������������ (C-_)\n"
981 aloha 1.30 "[���������������������] ������������������������\n"
982 aloha 1.33 "[�� ������������] ������������������������ (C-k) (kill buffer)\n"
983 aloha 1.30 "[A ������������] ���������������������\n"
984     "[���������������������������] Scheme ������������������������\n"
985     "[���������������������������������] ���������������������������������������\n"
986     "[��������� (?) ������������] ���������������������������������������\n"
987 aloha 1.33 "[info ������������] ���������������������������������������������������\n"
988     "\n"
989 aloha 1.30 "C-f : ��� ��������� (forward)\n"
990     "C-b : ��� ��������� (backward)\n"
991     "C-n : ��� ��������� (next line)\n"
992 aloha 1.33 "C-p : ��� ��������� (previous line)\n"
993     "\n"
994     "C-h : ���������������������\n"
995 aloha 1.31 "C-w : ���������\n"
996 aloha 1.33 "C-y : ��������� (������������)\n"
997     "\n"
998 aloha 1.30 "C-e : ��������������������� S ������������ (eval-expression)\n"
999     "C-j : ��������������������� S ������������ (eval-last-sexp)\n"
1000 aloha 1.33 "(emacs/xyzzy ��� *scratch* ���������������������)\n"
1001     "\n"
1002 aloha 1.31 "C-M-@ : ��������������������� S ������������ (mark-sexp)\n"
1003     "C-M-SPC : ��������������������� S ������������ (mark-last-sexp)\n"
1004 aloha 1.33 "C-x C-c : ��������������������������� �� ���������������������������������\n"
1005     "\n"
1006 aloha 1.30 "������������������������������������������������������������������Really Quit ?���������������������������������\n", -1);
1007     gtk_text_buffer_set_modified(Shiki_CURRENT_TEXT_BUFFER, FALSE);
1008     /* ������������������������������ */
1009     gtk_text_buffer_get_start_iter(Shiki_CURRENT_TEXT_BUFFER, &p);
1010     gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p);
1011 aloha 1.22 }
1012    
1013 aloha 1.1 /* ��������������������������������������� */
1014 aloha 1.21 static void shiki_editor_window_init(int argc, char **argv) {
1015 aloha 1.26 GtkWidget *vbox, *toolbar;
1016 aloha 1.1 GtkToolItem *icon;
1017     GtkIconSize iconsize;
1018 aloha 1.2 GtkTooltips *toolbar_tips = gtk_tooltips_new();
1019 aloha 1.1 /* ��������������������������������������������������������������������������������� */
1020     GtkToolItem *oicon, *sicon, *saicon, *eicon;
1021    
1022 aloha 1.8 gint contextid;
1023    
1024 aloha 1.1 /* ������������ */
1025 aloha 1.16 Shiki_EDITOR_WINDOW = gtk_window_new(GTK_WINDOW_TOPLEVEL);
1026     g_signal_connect(G_OBJECT(Shiki_EDITOR_WINDOW), "destroy", G_CALLBACK(gtk_main_quit), NULL);
1027 aloha 1.1
1028 aloha 1.31 /* ������������������������������������������������������������ */
1029     Shiki_EDITOR_CLIPBOARD = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
1030    
1031 aloha 1.1 /* ������������������������������������ */
1032     vbox = gtk_vbox_new(FALSE, 0);
1033     /* ��������������������� */
1034     toolbar = gtk_toolbar_new();
1035     gtk_box_pack_start(GTK_BOX(vbox), toolbar, FALSE, FALSE, 0);
1036    
1037 aloha 1.26 Shiki_EDITOR_NOTEBOOK = GTK_NOTEBOOK(gtk_notebook_new());
1038     g_signal_connect(G_OBJECT(Shiki_EDITOR_NOTEBOOK), "switch-page", GTK_SIGNAL_FUNC(switch_tabpage_handler), NULL);
1039 aloha 1.1
1040     /* ������������������������������������������������ */
1041     gtk_toolbar_set_style(GTK_TOOLBAR (toolbar), GTK_TOOLBAR_ICONS);
1042     iconsize = gtk_toolbar_get_icon_size (GTK_TOOLBAR (toolbar));
1043    
1044     /* ������������������ */
1045    
1046     /* ������������������ */
1047     oicon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-open", iconsize), "");
1048     /* ������������������������������������������������������������������������������������ */
1049 aloha 1.27 g_signal_connect(G_OBJECT(oicon), "clicked", G_CALLBACK(open_file_handler), NULL);
1050 aloha 1.1 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(oicon));
1051 aloha 1.2 gtk_tool_item_set_tooltip(oicon, toolbar_tips, "���������������������������",
1052     "���������������������������������������������������������������������������������������");
1053 aloha 1.1
1054     /* ������������������ */
1055     sicon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-save", iconsize), "");
1056     /* ������������������������������������������������������������������������������������ */
1057 aloha 1.27 g_signal_connect(G_OBJECT(sicon), "clicked", G_CALLBACK(save_file), NULL);
1058 aloha 1.1 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET(sicon));
1059 aloha 1.2 gtk_tool_item_set_tooltip(sicon, toolbar_tips, "������������������������������",
1060     "������������������������������������������������������������������������������������������������������������������������������������");
1061 aloha 1.1
1062     /* ��������������������������� */
1063     saicon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-save-as", iconsize), "");
1064     /* ������������������������������������������������������������������������������������������������������������������ */
1065 aloha 1.27 g_signal_connect(G_OBJECT(saicon), "clicked", G_CALLBACK(save_file_as), NULL);
1066 aloha 1.2 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET(saicon));
1067     gtk_tool_item_set_tooltip(saicon, toolbar_tips, "������������������������������������",
1068     "");
1069 aloha 1.1
1070     /* ������������������ */
1071     eicon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-execute", iconsize), "");
1072 aloha 1.14 /* ������������������������������������������ libgauche ������������������ */
1073 aloha 1.27 g_signal_connect(G_OBJECT(eicon), "clicked", G_CALLBACK(load_region_by_gauche), NULL);
1074 aloha 1.1 gtk_container_add (GTK_CONTAINER (toolbar), GTK_WIDGET(eicon));
1075 aloha 1.14 gtk_tool_item_set_tooltip(eicon, toolbar_tips, "��������������� S ������������������������ (load-region-lisp)",
1076 aloha 1.2 "Scheme (gauche) ������������������ S ������������������������");
1077 aloha 1.1
1078 aloha 1.16 gtk_container_add(GTK_CONTAINER(Shiki_EDITOR_WINDOW), vbox);
1079 aloha 1.26 gtk_container_add(GTK_CONTAINER(vbox), GTK_WIDGET(Shiki_EDITOR_NOTEBOOK));
1080 aloha 1.1
1081 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-apply", iconsize), "");
1082 aloha 1.26 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(tabsborder_on_off), G_OBJECT(Shiki_EDITOR_NOTEBOOK));
1083 aloha 1.1 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1084 aloha 1.6 gtk_tool_item_set_tooltip(icon, toolbar_tips, "��������� on/off", "");
1085 aloha 1.1
1086 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-preferences", iconsize), "");
1087 aloha 1.26 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(rotate_tab_position), G_OBJECT(Shiki_EDITOR_NOTEBOOK));
1088 aloha 1.1 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1089 aloha 1.14 gtk_tool_item_set_tooltip(icon, toolbar_tips, "���������������������", "");
1090 aloha 1.1
1091 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-add", iconsize), "");
1092 aloha 1.33 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(append_default_tabpage_handler), NULL);
1093 aloha 1.1 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1094 aloha 1.6 gtk_tool_item_set_tooltip(icon, toolbar_tips, "���������������������������������", "");
1095 aloha 1.1
1096 aloha 1.33 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-undo", iconsize), "");
1097     g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(undo), G_OBJECT(Shiki_EDITOR_NOTEBOOK));
1098     gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1099     gtk_tool_item_set_tooltip(icon, toolbar_tips, "Undo","");
1100    
1101 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-delete", iconsize), "");
1102 aloha 1.26 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(clear_current_buffer), G_OBJECT(Shiki_EDITOR_NOTEBOOK));
1103 aloha 1.15 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1104     gtk_tool_item_set_tooltip(icon, toolbar_tips, "���������������������������������",
1105     "���������������������������������������������������������������������");
1106    
1107 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-close", iconsize), "");
1108 aloha 1.27 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(remove_tabpage), NULL);
1109 aloha 1.1 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1110 aloha 1.2 gtk_tool_item_set_tooltip(icon, toolbar_tips, "���������������������������",
1111     "���������������������������������������������������������������");
1112 aloha 1.15
1113 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-bold", iconsize), "");
1114 aloha 1.12 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(select_font), NULL);
1115     gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1116     gtk_tool_item_set_tooltip(icon, toolbar_tips, "���������������������", "");
1117 aloha 1.1
1118 aloha 1.18 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-connect", iconsize), "");
1119 aloha 1.27 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(load_scheme_file_by_gauche), NULL);
1120 aloha 1.14 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1121     gtk_tool_item_set_tooltip(icon, toolbar_tips, "Scheme ������������������������", "");
1122    
1123 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-convert", iconsize), "");
1124 aloha 1.26 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(load_buffer_by_gauche), G_OBJECT(Shiki_EDITOR_NOTEBOOK));
1125 aloha 1.14 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1126     gtk_tool_item_set_tooltip(icon, toolbar_tips, "������������������������", "");
1127    
1128 aloha 1.22 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-dialog-question", iconsize), "");
1129 aloha 1.27 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(open_online_help), NULL);
1130 aloha 1.22 gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1131     gtk_tool_item_set_tooltip(icon, toolbar_tips, "���������", "");
1132    
1133 aloha 1.17 icon = gtk_tool_button_new(gtk_image_new_from_stock ("gtk-dialog-info", iconsize), "");
1134 aloha 1.11 g_signal_connect(G_OBJECT(icon), "clicked", G_CALLBACK(about_this_application), NULL);
1135     gtk_container_add(GTK_CONTAINER (toolbar), GTK_WIDGET(icon));
1136     gtk_tool_item_set_tooltip(icon, toolbar_tips, "������������������������������������������", "");
1137    
1138 aloha 1.16 gtk_box_pack_start(GTK_BOX(vbox), Shiki_EDITOR_MODELINE_LABEL = gtk_label_new("-E:** *scratch* (Gauche Interaction)--L1--All---------------------------------"), TRUE, TRUE, 0);
1139 aloha 1.12
1140 aloha 1.8 /* C-x C-s ��������������������������������������������������������������������������������������� */
1141 aloha 1.16 Shiki_EDITOR_STATUSBAR = gtk_statusbar_new();
1142     gtk_box_pack_start(GTK_BOX(vbox), Shiki_EDITOR_STATUSBAR, TRUE, TRUE, 0);
1143     contextid = gtk_statusbar_get_context_id(GTK_STATUSBAR(Shiki_EDITOR_STATUSBAR), "");
1144 aloha 1.8
1145     /* ������������������������������������������������ */
1146 aloha 1.26 g_signal_connect(G_OBJECT(Shiki_EDITOR_NOTEBOOK), "key-press-event", G_CALLBACK (signal_key_press_handler), GINT_TO_POINTER(contextid));
1147     g_signal_connect(G_OBJECT(Shiki_EDITOR_NOTEBOOK), "key-release-event", G_CALLBACK (signal_key_release_handler), GINT_TO_POINTER(contextid));
1148 aloha 1.12
1149 aloha 1.21 /* ��������������������������������������������� */
1150     if(argc >= 2) {
1151     int i;
1152     for(i = 1; i < argc; i++)
1153 aloha 1.27 open_file(g_strdup(argv[i]));
1154 aloha 1.21 } else /* ������������������������������������������������������������������ */
1155 aloha 1.26 open_online_help(Shiki_EDITOR_NOTEBOOK);
1156 aloha 1.8
1157 aloha 1.26 gtk_widget_grab_focus(GTK_WIDGET(Shiki_EDITOR_NOTEBOOK));
1158 aloha 1.16 gtk_widget_show_all(Shiki_EDITOR_WINDOW);
1159 aloha 1.1 }
1160    
1161     int main(int argc, char *argv[]) {
1162     /* ������������������������������������ */
1163     gtk_set_locale();
1164     gtk_init(&argc, &argv);
1165     GC_INIT(); Scm_Init(GAUCHE_SIGNATURE);
1166 aloha 1.21 shiki_editor_window_init(argc, argv);
1167 aloha 1.1 gtk_main();
1168     Scm_Exit(0);
1169     return 0;
1170     }

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26