3 * @author Shinichiro Nakamura
4 * @brief NT-Shell用テキストエディタモジュールの実装。
6 * 文字列の編集を論理的に扱うためのモジュール。
7 * このモジュールはビューに関して一切感知しない。
11 * ===============================================================
12 * Natural Tiny Shell (NT-Shell)
14 * ===============================================================
15 * Copyright (c) 2010-2011 Shinichiro Nakamura
17 * Permission is hereby granted, free of charge, to any person
18 * obtaining a copy of this software and associated documentation
19 * files (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use,
21 * copy, modify, merge, publish, distribute, sublicense, and/or
22 * sell copies of the Software, and to permit persons to whom the
23 * Software is furnished to do so, subject to the following
26 * The above copyright notice and this permission notice shall be
27 * included in all copies or substantial portions of the Software.
29 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
31 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
33 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
34 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
35 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
36 * OTHER DEALINGS IN THE SOFTWARE.
37 * ===============================================================
40 #include "text_editor.h"
43 * @brief テキストエディタを初期化する。
45 * @param p テキストエディタ構造体。
47 void text_editor_init(text_editor_t *p)
51 p->buffer[p->len] = '\0';
57 * @param p テキストエディタ構造体。
60 int text_editor_insert(text_editor_t *p, char c)
62 if (p->len < sizeof(p->buffer) - 1) {
63 int n = p->len - p->pos + 1;
65 char *src = p->buffer + p->len + 0;
66 char *des = p->buffer + p->len + 1;
67 for (i = 0; i < n; i++) {
73 p->buffer[p->pos] = c;
76 p->buffer[p->len] = '\0';
85 * @param p テキストエディタ構造体。
87 int text_editor_backspace(text_editor_t *p)
90 int n = p->len - p->pos;
92 char *src = p->buffer + p->pos - 0;
93 char *des = p->buffer + p->pos - 1;
96 for (i = 0; i < n; i++) {
101 *(p->buffer + p->len) = '\0';
108 * @brief カーソル位置を取得する。
110 * @param p テキストエディタ構造体。
112 int text_editor_cursor_get_position(text_editor_t *p)
118 * @brief カーソルを先頭に移動させる。
120 * @param p テキストエディタ構造体。
122 int text_editor_cursor_head(text_editor_t *p)
132 * @brief カーソルを最後尾に移動させる。
134 * @param p テキストエディタ構造体。
136 int text_editor_cursor_tail(text_editor_t *p)
138 if (p->pos < p->len) {
146 * @brief カーソルを左へ移動させる。
148 * @param p テキストエディタ構造体。
150 int text_editor_cursor_left(text_editor_t *p)
160 * @brief カーソルを右へ移動させる。
162 * @param p テキストエディタ構造体。
164 int text_editor_cursor_right(text_editor_t *p)
166 if (p->pos < p->len) {
176 * @param p テキストエディタ構造体。
177 * @param buf 文字列が格納されたバッファ。
179 int text_editor_set_text(text_editor_t *p, char *buf)
182 char *des = p->buffer;
189 if (sizeof(p->buffer) <= n - 1) {
202 * @param p テキストエディタ構造体。
203 * @param buf 文字列を格納するバッファ。
204 * @param siz バッファサイズ。
206 int text_editor_get_text(text_editor_t *p, char *buf, int siz)
208 char *src = p->buffer;
225 * @param p テキストエディタ構造体。
227 void text_editor_clear(text_editor_t *p)
231 p->buffer[p->len] = '\0';