OSDN Git Service

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