| 1 |
aloha |
1.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 |
aloha |
1.5 |
* $Id: buffer.c,v 1.4 2006/11/25 14:28:17 aloha Exp $ |
| 28 |
aloha |
1.1 |
*/ |
| 29 |
|
|
#include"shiki.h" |
| 30 |
aloha |
1.2 |
|
| 31 |
aloha |
1.3 |
gchar *Shiki_buffer_substring(gint start, gint end) { |
| 32 |
|
|
if(start >= end) |
| 33 |
|
|
return NULL; |
| 34 |
|
|
else { |
| 35 |
|
|
GtkTextIter s, e; |
| 36 |
|
|
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &s, start); |
| 37 |
|
|
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &e, end); |
| 38 |
|
|
|
| 39 |
|
|
return gtk_text_buffer_get_text(Shiki_CURRENT_TEXT_BUFFER, &s, &e, FALSE); |
| 40 |
|
|
} |
| 41 |
|
|
} |
| 42 |
aloha |
1.4 |
|
| 43 |
|
|
void Shiki_delete_region(gint start, gint end) { |
| 44 |
|
|
if(start >= end) |
| 45 |
|
|
return; |
| 46 |
|
|
else { |
| 47 |
|
|
GtkTextIter s, e; |
| 48 |
|
|
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &s, start); |
| 49 |
|
|
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &e, end); |
| 50 |
|
|
|
| 51 |
|
|
return gtk_text_buffer_delete(Shiki_CURRENT_TEXT_BUFFER, &s, &e); |
| 52 |
|
|
} |
| 53 |
|
|
} |
| 54 |
|
|
|
| 55 |
|
|
gint Shiki_point() { |
| 56 |
|
|
GtkTextIter p; |
| 57 |
|
|
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 58 |
|
|
return gtk_text_iter_get_offset(&p); |
| 59 |
|
|
} |
| 60 |
|
|
|
| 61 |
|
|
gint Shiki_point_max() { |
| 62 |
|
|
GtkTextIter p; |
| 63 |
|
|
gtk_text_buffer_get_end_iter(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 64 |
|
|
return gtk_text_iter_get_offset(&p); |
| 65 |
|
|
} |
| 66 |
|
|
|
| 67 |
|
|
gint Shiki_point_min() { |
| 68 |
|
|
return 0; |
| 69 |
|
|
} |
| 70 |
|
|
|
| 71 |
|
|
void Shiki_goto_char(gint offset) { |
| 72 |
|
|
GtkTextIter p; |
| 73 |
|
|
gtk_text_buffer_get_iter_at_offset(Shiki_CURRENT_TEXT_BUFFER, &p, offset); |
| 74 |
|
|
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 75 |
|
|
} |
| 76 |
|
|
|
| 77 |
|
|
void Shiki_forward_char() { |
| 78 |
|
|
GtkTextIter p; |
| 79 |
|
|
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 80 |
|
|
gtk_text_iter_forward_char(&p); |
| 81 |
|
|
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 82 |
|
|
} |
| 83 |
|
|
|
| 84 |
|
|
void Shiki_backward_char() { |
| 85 |
|
|
GtkTextIter p; |
| 86 |
|
|
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 87 |
|
|
gtk_text_iter_backward_char(&p); |
| 88 |
|
|
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
void Shiki_goto_line(gint line) { |
| 92 |
|
|
GtkTextIter p; |
| 93 |
|
|
gtk_text_buffer_get_iter_at_line(Shiki_CURRENT_TEXT_BUFFER, &p, line); |
| 94 |
|
|
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 95 |
|
|
} |
| 96 |
|
|
|
| 97 |
|
|
void Shiki_goto_bol() { |
| 98 |
|
|
GtkTextIter p; |
| 99 |
|
|
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 100 |
|
|
gtk_text_buffer_get_iter_at_line_offset(Shiki_CURRENT_TEXT_BUFFER, &p, gtk_text_iter_get_line(&p), 0); |
| 101 |
|
|
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 102 |
|
|
} |
| 103 |
|
|
|
| 104 |
|
|
void Shiki_goto_eol() { |
| 105 |
|
|
GtkTextIter p; |
| 106 |
|
|
gtk_text_buffer_get_iter_at_mark(Shiki_CURRENT_TEXT_BUFFER,&p, gtk_text_buffer_get_insert(Shiki_CURRENT_TEXT_BUFFER)); |
| 107 |
|
|
gtk_text_iter_forward_to_line_end(&p); |
| 108 |
|
|
gtk_text_iter_backward_char(&p); |
| 109 |
|
|
gtk_text_buffer_place_cursor(Shiki_CURRENT_TEXT_BUFFER, &p); |
| 110 |
|
|
} |
| 111 |
aloha |
1.5 |
|
| 112 |
|
|
static gint compBuffer(gconstpointer a, gconstpointer b) { |
| 113 |
|
|
return ((ShikiBuffer *)a)->text_buffer == b ? 0 : b - a; |
| 114 |
|
|
} |
| 115 |
|
|
|
| 116 |
|
|
static GList *get_ShikiBufferListElement_By_GtkTextBuffer(GtkTextBuffer *b) { |
| 117 |
|
|
return g_list_find_custom(Shiki_EDITOR_BUFFER_LIST, b, compBuffer); |
| 118 |
|
|
} |
| 119 |
|
|
|
| 120 |
|
|
const char *Shiki_buffer_name(GtkTextBuffer *buffer) { |
| 121 |
|
|
GList *l = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 122 |
|
|
if(l) |
| 123 |
|
|
return ((ShikiBuffer *)(l->data))->name; |
| 124 |
|
|
else |
| 125 |
|
|
return NULL; |
| 126 |
|
|
} |
| 127 |
|
|
|
| 128 |
|
|
GtkTextBuffer *Shiki_get_next_buffer(GtkTextBuffer *buffer) { |
| 129 |
|
|
GList *l = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 130 |
|
|
if(l && l->next) |
| 131 |
|
|
return ((ShikiBuffer *)(l->next->data))->text_buffer; |
| 132 |
|
|
else |
| 133 |
|
|
return NULL; |
| 134 |
|
|
} |
| 135 |
|
|
|
| 136 |
|
|
GtkTextBuffer *Shiki_get_previous_buffer(GtkTextBuffer *buffer) { |
| 137 |
|
|
GList *l = get_ShikiBufferListElement_By_GtkTextBuffer(buffer); |
| 138 |
|
|
if(l && l->prev) |
| 139 |
|
|
return ((ShikiBuffer *)(l->prev->data))->text_buffer; |
| 140 |
|
|
else |
| 141 |
|
|
return NULL; |
| 142 |
|
|
} |
| 143 |
|
|
|
| 144 |
|
|
ScmObj Shiki_buffer_list() { |
| 145 |
|
|
GList *l; |
| 146 |
|
|
GtkTextBuffer *b; |
| 147 |
|
|
ScmObj bl = SCM_NIL; |
| 148 |
|
|
|
| 149 |
|
|
for(l = Shiki_EDITOR_BUFFER_LIST; l != NULL; l = l->next) { |
| 150 |
|
|
b= ((ShikiBuffer *)(l->data))->text_buffer; |
| 151 |
|
|
bl = Scm_Cons(SHIKI_BUFFER_BOX(g_object_ref(b)), bl); |
| 152 |
|
|
} |
| 153 |
|
|
return bl; |
| 154 |
|
|
} |