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"
13 * Check if this line is expression or not.
14 * And update it if it is.
16 void check_expression_line(text_body_type *tb, int y)
18 concptr s = tb->lines_list[y];
20 if ((s[0] == '?' && s[1] == ':') ||
21 (tb->states[y] & LSTAT_BYPASS))
23 tb->dirty_flags |= DIRTY_EXPRESSION;
28 * Insert return code and split the line
30 bool insert_return_code(text_body_type *tb)
32 char buf[MAX_LINELEN];
35 for (num_lines = 0; tb->lines_list[num_lines]; num_lines++);
37 if (num_lines >= MAX_LINES - 2) return FALSE;
40 for (; tb->cy < num_lines; num_lines--)
42 tb->lines_list[num_lines + 1] = tb->lines_list[num_lines];
43 tb->states[num_lines + 1] = tb->states[num_lines];
46 for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
49 if (iskanji(tb->lines_list[tb->cy][i]))
50 buf[j++] = tb->lines_list[tb->cy][i++];
52 buf[j++] = tb->lines_list[tb->cy][i];
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;
66 * Get a trigger key and insert ASCII string for the trigger
68 bool insert_macro_line(text_body_type *tb)
87 ascii_to_text(tmp, buf);
88 if (!tmp[0]) return FALSE;
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));
95 i = macro_find_exact(buf);
102 ascii_to_text(tmp, macro__act[i]);
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));
114 * Get a command key and insert ASCII string for the key
116 bool insert_keymap_line(text_body_type *tb)
119 if (rogue_like_commands)
121 mode = KEYMAP_MODE_ROGUE;
125 mode = KEYMAP_MODE_ORIG;
135 ascii_to_text(tmp, buf);
136 if (!tmp[0]) return FALSE;
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));
143 concptr act = keymap_act[mode][(byte)(buf[0])];
146 ascii_to_text(tmp, act);
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));
158 * Insert single letter at cursor position.
160 void insert_single_letter(text_body_type *tb, int key)
163 char buf[MAX_LINELEN];
165 for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
167 buf[j++] = tb->lines_list[tb->cy][i];
177 if (j + 2 < MAX_LINELEN)
179 buf[j++] = (char)key;
180 buf[j++] = (char)next;
189 if (j + 1 < MAX_LINELEN)
190 buf[j++] = (char)key;
194 for (; tb->lines_list[tb->cy][i] && j + 1 < MAX_LINELEN; i++)
195 buf[j++] = tb->lines_list[tb->cy][i];
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;
203 tb->dirty_line = tb->cy;
204 check_expression_line(tb, tb->cy);
210 * Kill segment of a line
212 void kill_line_segment(text_body_type *tb, int y, int x0, int x1, bool whole)
214 concptr s = tb->lines_list[y];
215 if (whole && x0 == 0 && s[x1] == '\0' && tb->lines_list[y + 1])
217 string_free(tb->lines_list[y]);
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;
224 tb->dirty_flags |= DIRTY_EXPRESSION;
229 if (x0 == x1) return;
231 char buf[MAX_LINELEN];
233 for (int x = 0; x < x0; x++)
236 for (int x = x1; s[x]; x++)
240 string_free(tb->lines_list[y]);
241 tb->lines_list[y] = string_make(buf);
242 check_expression_line(tb, y);