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.32 - (hide annotations) (download) (as text)
Thu Nov 16 08:52:02 2006 UTC (17 years, 4 months ago) by aloha
Branch: MAIN
Changes since 1.31: +202 -126 lines
File MIME type: text/x-csrc
add single-level undo/redo (undo-undo)

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

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