| 1 |
/* vim: set encoding=utf8: |
| 2 |
* |
| 3 |
* buffer.c |
| 4 |
* |
| 5 |
* This file is part of Shiki. |
| 6 |
* |
| 7 |
* Copyright(C)2006 WAKATSUKI toshihiro |
| 8 |
* |
| 9 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 10 |
* copy of this software and associated documentation files (the "Software"), |
| 11 |
* to deal in the Software without restriction, including without limitation |
| 12 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 13 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 14 |
* Software is furnished to do so, subject to the following conditions: |
| 15 |
* |
| 16 |
* The above copyright notice and this permission notice shall be included in |
| 17 |
* all copies or substantial portions of the Software. |
| 18 |
* |
| 19 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 25 |
* SOFTWARE. |
| 26 |
* |
| 27 |
* $Id: buffer.c,v 1.6 2006/11/26 18:41:43 aloha Exp $ |
| 28 |
*/ |
| 29 |
#include"shiki.h" |
| 30 |
|
| 31 |
/* GtkTextBuffer ��������������������� ShikiBuffer ��������������������� */ |
| 32 |
static gint compBuffer(gconstpointer a, gconstpointer b) { |
| 33 |
return ((ShikiBuffer *)a)->text_buffer == b ? 0 : b - a; |
| 34 |
} |
| 35 |
|
| 36 |
static GList *get_ShikiBufferListElement_By_GtkTextBuffer(GtkTextBuffer *b) { |
| 37 |
return g_list_find_custom(Shiki_EDITOR_BUFFER_LIST, b, compBuffer); |
| 38 |
} |
| 39 |
|
| 40 |
void Shiki_delete_buffer(GtkTextBuffer *buffer) { |
| 41 |
/* ��������������������������������������������������������������������������������������������������� */ |
| 42 |
/* ���������������������Scheme ������������������������ Gtk ������������������������������������������������ */ |
| 43 |
GList *bufListElem = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 44 |
ShikiBuffer *tabInfo = bufListElem->data; |
| 45 |
gint bufNum = g_list_position(Shiki_EDITOR_BUFFER_LIST, bufListElem); |
| 46 |
|
| 47 |
/* ��������� 1 ��������������������������������������������������� */ |
| 48 |
if(g_list_length(Shiki_EDITOR_BUFFER_LIST) == 1) |
| 49 |
return; |
| 50 |
/* ��������������������������������������������������������������������������������������������� */ |
| 51 |
g_signal_handler_disconnect(Shiki_EDITOR_WINDOW, tabInfo->delete_handler_id); |
| 52 |
Shiki_EDITOR_BUFFER_LIST = g_list_delete_link(Shiki_EDITOR_BUFFER_LIST, bufListElem); |
| 53 |
gtk_widget_destroy(GTK_WIDGET(tabInfo->tabpage)); |
| 54 |
g_free(tabInfo->tabpage_label); |
| 55 |
g_free(tabInfo->name); |
| 56 |
g_free(tabInfo->filename); |
| 57 |
g_free(tabInfo); |
| 58 |
gtk_notebook_remove_page(Shiki_EDITOR_NOTEBOOK, bufNum); |
| 59 |
/* ��������������� */ |
| 60 |
gtk_widget_queue_draw(GTK_WIDGET(Shiki_EDITOR_NOTEBOOK)); |
| 61 |
} |
| 62 |
|
| 63 |
GtkTextBuffer *Shiki_find_buffer(const gchar *name) { |
| 64 |
GList *l; |
| 65 |
for(l = Shiki_EDITOR_BUFFER_LIST; l != NULL; l = l->next) |
| 66 |
if(strcmp(((ShikiBuffer *)l->data)->name, name) == 0) |
| 67 |
return ((ShikiBuffer *)l->data)->text_buffer; |
| 68 |
return NULL; |
| 69 |
} |
| 70 |
|
| 71 |
gchar *Shiki_buffer_substring(gint start, gint end) { |
| 72 |
if(start >= end) |
| 73 |
return NULL; |
| 74 |
else { |
| 75 |
GtkTextIter s, e; |
| 76 |
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &s, start); |
| 77 |
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &e, end); |
| 78 |
|
| 79 |
return gtk_text_buffer_get_text(Shiki_CURRENT_TEXT_BUFFER, &s, &e, FALSE); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
void Shiki_delete_region(gint start, gint end) { |
| 84 |
if(start >= end) |
| 85 |
return; |
| 86 |
else { |
| 87 |
GtkTextIter s, e; |
| 88 |
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &s, start); |
| 89 |
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &e, end); |
| 90 |
|
| 91 |
return gtk_text_buffer_delete(Shiki_CURRENT_TEXT_BUFFER, &s, &e); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
gint Shiki_point() { |
| 96 |
GtkTextIter p; |
| 97 |
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 98 |
return gtk_text_iter_get_offset(&p); |
| 99 |
} |
| 100 |
|
| 101 |
gint Shiki_point_max() { |
| 102 |
GtkTextIter p; |
| 103 |
gtk_text_buffer_get_end_iter(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 104 |
return gtk_text_iter_get_offset(&p); |
| 105 |
} |
| 106 |
|
| 107 |
gint Shiki_point_min() { |
| 108 |
return 0; |
| 109 |
} |
| 110 |
|
| 111 |
void Shiki_goto_char(gint offset) { |
| 112 |
GtkTextIter p; |
| 113 |
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &p, offset); |
| 114 |
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 115 |
} |
| 116 |
|
| 117 |
void Shiki_forward_char() { |
| 118 |
GtkTextIter p; |
| 119 |
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 120 |
gtk_text_iter_forward_char(&p); |
| 121 |
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 122 |
} |
| 123 |
|
| 124 |
void Shiki_backward_char() { |
| 125 |
GtkTextIter p; |
| 126 |
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 127 |
gtk_text_iter_backward_char(&p); |
| 128 |
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 129 |
} |
| 130 |
|
| 131 |
void Shiki_goto_line(gint line) { |
| 132 |
GtkTextIter p; |
| 133 |
gtk_text_buffer_get_iter_at_line(Shiki_CURRENT_TEXT_BUFFER, &p, line); |
| 134 |
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 135 |
} |
| 136 |
|
| 137 |
void Shiki_goto_bol() { |
| 138 |
GtkTextIter p; |
| 139 |
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 140 |
gtk_text_buffer_get_iter_at_line_offset(Shiki_CURRENT_TEXT_BUFFER, &p, gtk_text_iter_get_line(&p), 0); |
| 141 |
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 142 |
} |
| 143 |
|
| 144 |
void Shiki_goto_eol() { |
| 145 |
GtkTextIter p; |
| 146 |
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 147 |
gtk_text_iter_forward_to_line_end(&p); |
| 148 |
gtk_text_iter_backward_char(&p); |
| 149 |
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 150 |
} |
| 151 |
|
| 152 |
void Shiki_forward_line(gint count) { |
| 153 |
GtkTextIter p; |
| 154 |
gint i; |
| 155 |
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 156 |
|
| 157 |
if(count >= 0) { |
| 158 |
for(i = count; i != 0; i--) |
| 159 |
gtk_text_view_forward_display_line(Shiki_CURRENT_TEXT_VIEW, &p); |
| 160 |
} else { |
| 161 |
for(i = count; i != 0; i++) |
| 162 |
gtk_text_view_backward_display_line(Shiki_CURRENT_TEXT_VIEW, &p); |
| 163 |
} |
| 164 |
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 165 |
} |
| 166 |
|
| 167 |
const char *Shiki_buffer_name(GtkTextBuffer *buffer) { |
| 168 |
GList *l = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 169 |
if(l) |
| 170 |
return ((ShikiBuffer *)(l->data))->name; |
| 171 |
else |
| 172 |
return NULL; |
| 173 |
} |
| 174 |
|
| 175 |
gboolean Shiki_deleted_buffer_p(GtkTextBuffer *buffer) { |
| 176 |
GList *l = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 177 |
if(l) |
| 178 |
return FALSE; |
| 179 |
else |
| 180 |
return TRUE; |
| 181 |
} |
| 182 |
|
| 183 |
GtkTextBuffer *Shiki_get_next_buffer(GtkTextBuffer *buffer) { |
| 184 |
GList *l = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 185 |
if(l && l->next) |
| 186 |
return ((ShikiBuffer *)(l->next->data))->text_buffer; |
| 187 |
else |
| 188 |
return NULL; |
| 189 |
} |
| 190 |
|
| 191 |
GtkTextBuffer *Shiki_get_previous_buffer(GtkTextBuffer *buffer) { |
| 192 |
GList *l = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 193 |
if(l && l->prev) |
| 194 |
return ((ShikiBuffer *)(l->prev->data))->text_buffer; |
| 195 |
else |
| 196 |
return NULL; |
| 197 |
} |
| 198 |
|
| 199 |
ScmObj Shiki_buffer_list() { |
| 200 |
GList *l; |
| 201 |
GtkTextBuffer *b; |
| 202 |
ScmObj bl = SCM_NIL; |
| 203 |
|
| 204 |
for(l = Shiki_EDITOR_BUFFER_LIST; l != NULL; l = l->next) { |
| 205 |
b= ((ShikiBuffer *)(l->data))->text_buffer; |
| 206 |
bl = Scm_Cons(SHIKI_BUFFER_BOX(g_object_ref(b)), bl); |
| 207 |
} |
| 208 |
return bl; |
| 209 |
} |
| 210 |
|
| 211 |
void Shiki_erase_buffer(GtkTextBuffer *buffer) { |
| 212 |
GtkTextIter start, end; |
| 213 |
gtk_text_buffer_get_start_iter(buffer, &start); |
| 214 |
gtk_text_buffer_get_end_iter(buffer, &end); |
| 215 |
gtk_text_buffer_delete(buffer, &start, &end); |
| 216 |
} |
| 217 |
|
| 218 |
const gchar *Shiki_file_name_dialog(const gchar *msg) { |
| 219 |
|
| 220 |
GtkWidget *dialog = gtk_file_selection_new(msg); |
| 221 |
gint resp = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 222 |
const gchar *filename = NULL; |
| 223 |
|
| 224 |
if(resp == GTK_RESPONSE_OK) |
| 225 |
filename = gtk_file_selection_get_filename(GTK_FILE_SELECTION(dialog)); |
| 226 |
|
| 227 |
gtk_widget_destroy(dialog); |
| 228 |
return filename; |
| 229 |
} |
| 230 |
|
| 231 |
gboolean Shiki_yes_or_no_p(const gchar *msg) { |
| 232 |
GtkWidget *dialog = gtk_message_dialog_new(GTK_WINDOW(Shiki_EDITOR_WINDOW), |
| 233 |
GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_QUESTION, |
| 234 |
GTK_BUTTONS_YES_NO, msg); |
| 235 |
|
| 236 |
gint resp = gtk_dialog_run(GTK_DIALOG(dialog)); |
| 237 |
gtk_widget_destroy(dialog); |
| 238 |
if(GTK_RESPONSE_YES == resp) |
| 239 |
return TRUE; |
| 240 |
return FALSE; |
| 241 |
} |