OSDN Git Service

Corrected English spelling mistake.
[hengband/hengband.git] / src / autopick / autopick-inserter-killer.c
1 #include "autopick/autopick-inserter-killer.h"
2 #include "autopick/autopick-dirty-flags.h"
3 #include "cmd-io/macro-util.h"
4 #include "game-option/input-options.h"
5 #include "game-option/keymap-directory-getter.h"
6 #include "io/input-key-acceptor.h"
7 #include "io/input-key-requester.h"
8 #include "main/sound-of-music.h"
9 #include "term/screen-processor.h"
10 #include "util/string-processor.h"
11
12 /*
13  * Check if this line is expression or not.
14  * And update it if it is.
15  */
16 void check_expression_line(text_body_type *tb, int y)
17 {
18         concptr s = tb->lines_list[y];
19
20         if ((s[0] == '?' && s[1] == ':') ||
21                 (tb->states[y] & LSTAT_BYPASS))
22         {
23                 tb->dirty_flags |= DIRTY_EXPRESSION;
24         }
25 }
26
27 /*
28  * Insert return code and split the line
29  */
30 bool insert_return_code(text_body_type *tb)
31 {
32         char buf[MAX_LINELEN];
33         int i, j, num_lines;
34
35         for (num_lines = 0; tb->lines_list[num_lines]; num_lines++);
36
37         if (num_lines >= MAX_LINES - 2) return FALSE;
38         num_lines--;
39
40         for (; tb->cy < num_lines; num_lines--)
41         {
42                 tb->lines_list[num_lines + 1] = tb->lines_list[num_lines];
43                 tb->states[num_lines + 1] = tb->states[num_lines];
44         }
45
46         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
47         {
48 #ifdef JP
49                 if (iskanji(tb->lines_list[tb->cy][i]))
50                         buf[j++] = tb->lines_list[tb->cy][i++];
51 #endif
52                 buf[j++] = tb->lines_list[tb->cy][i];
53         }
54
55         buf[j] = '\0';
56         tb->lines_list[tb->cy + 1] = string_make(&tb->lines_list[tb->cy][i]);
57         string_free(tb->lines_list[tb->cy]);
58         tb->lines_list[tb->cy] = string_make(buf);
59         tb->dirty_flags |= DIRTY_EXPRESSION;
60         tb->changed = TRUE;
61         return TRUE;
62 }
63
64
65 /*
66  * Get a trigger key and insert ASCII string for the trigger
67  */
68 bool insert_macro_line(text_body_type *tb)
69 {
70         int i, n = 0;
71         flush();
72         inkey_base = TRUE;
73         i = inkey();
74         char buf[1024];
75         while (i)
76         {
77                 buf[n++] = (char)i;
78                 inkey_base = TRUE;
79                 inkey_scan = TRUE;
80                 i = inkey();
81         }
82
83         buf[n] = '\0';
84         flush();
85
86         char tmp[1024];
87         ascii_to_text(tmp, buf);
88         if (!tmp[0]) return FALSE;
89
90         tb->cx = 0;
91         insert_return_code(tb);
92         string_free(tb->lines_list[tb->cy]);
93         tb->lines_list[tb->cy] = string_make(format("P:%s", tmp));
94
95         i = macro_find_exact(buf);
96         if (i == -1)
97         {
98                 tmp[0] = '\0';
99         }
100         else
101         {
102                 ascii_to_text(tmp, macro__act[i]);
103         }
104
105         insert_return_code(tb);
106         string_free(tb->lines_list[tb->cy]);
107         tb->lines_list[tb->cy] = string_make(format("A:%s", tmp));
108
109         return TRUE;
110 }
111
112
113 /*
114  * Get a command key and insert ASCII string for the key
115  */
116 bool insert_keymap_line(text_body_type *tb)
117 {
118         BIT_FLAGS mode;
119         if (rogue_like_commands)
120         {
121                 mode = KEYMAP_MODE_ROGUE;
122         }
123         else
124         {
125                 mode = KEYMAP_MODE_ORIG;
126         }
127
128         flush();
129         char buf[2];
130         buf[0] = inkey();
131         buf[1] = '\0';
132
133         flush();
134         char tmp[1024];
135         ascii_to_text(tmp, buf);
136         if (!tmp[0]) return FALSE;
137
138         tb->cx = 0;
139         insert_return_code(tb);
140         string_free(tb->lines_list[tb->cy]);
141         tb->lines_list[tb->cy] = string_make(format("C:%d:%s", mode, tmp));
142
143         concptr act = keymap_act[mode][(byte)(buf[0])];
144         if (act)
145         {
146                 ascii_to_text(tmp, act);
147         }
148
149         insert_return_code(tb);
150         string_free(tb->lines_list[tb->cy]);
151         tb->lines_list[tb->cy] = string_make(format("A:%s", tmp));
152
153         return TRUE;
154 }
155
156
157 /*
158  * Insert single letter at cursor position.
159  */
160 void insert_single_letter(text_body_type *tb, int key)
161 {
162         int i, j, len;
163         char buf[MAX_LINELEN];
164
165         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
166         {
167                 buf[j++] = tb->lines_list[tb->cy][i];
168         }
169
170 #ifdef JP
171         if (iskanji(key))
172         {
173                 int next;
174
175                 inkey_base = TRUE;
176                 next = inkey();
177                 if (j + 2 < MAX_LINELEN)
178                 {
179                         buf[j++] = (char)key;
180                         buf[j++] = (char)next;
181                         tb->cx += 2;
182                 }
183                 else
184                         bell();
185         }
186         else
187 #endif
188         {
189                 if (j + 1 < MAX_LINELEN)
190                         buf[j++] = (char)key;
191                 tb->cx++;
192         }
193
194         for (; tb->lines_list[tb->cy][i] && j + 1 < MAX_LINELEN; i++)
195                 buf[j++] = tb->lines_list[tb->cy][i];
196         buf[j] = '\0';
197
198         string_free(tb->lines_list[tb->cy]);
199         tb->lines_list[tb->cy] = string_make(buf);
200         len = strlen(tb->lines_list[tb->cy]);
201         if (len < tb->cx) tb->cx = len;
202
203         tb->dirty_line = tb->cy;
204         check_expression_line(tb, tb->cy);
205         tb->changed = TRUE;
206 }
207
208
209 /*
210  * Kill segment of a line
211  */
212 void kill_line_segment(text_body_type *tb, int y, int x0, int x1, bool whole)
213 {
214         concptr s = tb->lines_list[y];
215         if (whole && x0 == 0 && s[x1] == '\0' && tb->lines_list[y + 1])
216         {
217                 string_free(tb->lines_list[y]);
218
219                 int i;
220                 for (i = y; tb->lines_list[i + 1]; i++)
221                         tb->lines_list[i] = tb->lines_list[i + 1];
222                 tb->lines_list[i] = NULL;
223
224                 tb->dirty_flags |= DIRTY_EXPRESSION;
225
226                 return;
227         }
228
229         if (x0 == x1) return;
230
231         char buf[MAX_LINELEN];
232         char *d = buf;
233         for (int x = 0; x < x0; x++)
234                 *(d++) = s[x];
235
236         for (int x = x1; s[x]; x++)
237                 *(d++) = s[x];
238
239         *d = '\0';
240         string_free(tb->lines_list[y]);
241         tb->lines_list[y] = string_make(buf);
242         check_expression_line(tb, y);
243         tb->changed = TRUE;
244 }