OSDN Git Service

自動破壊を簡単に設定する機能を破壊コマンド(k/^D)に追加した。
[hengband/hengband.git] / src / autopick.c
1 /* File: autopick.c */
2
3 /* Purpose: Object Auto-picker/Destroyer */
4
5 /*
6  * Copyright (c) 2002  Mogami
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14
15
16 #define MAX_LINELEN 1024
17
18 /*
19  * Macros for Keywords
20  */
21 #define FLG_ALL             0
22 #define FLG_COLLECTING      1
23 #define FLG_UNAWARE         2 
24 #define FLG_UNIDENTIFIED    3 
25 #define FLG_IDENTIFIED      4 
26 #define FLG_STAR_IDENTIFIED 5 
27 #define FLG_BOOSTED         6 
28 #define FLG_MORE_THAN       7 
29 #define FLG_DICE            8 
30 #define FLG_MORE_BONUS      9 
31 #define FLG_MORE_BONUS2     10
32 #define FLG_WORTHLESS       11
33 #define FLG_ARTIFACT        12
34 #define FLG_EGO             13
35 #define FLG_NAMELESS        14
36 #define FLG_WANTED          15
37 #define FLG_UNIQUE          16
38 #define FLG_HUMAN           17
39 #define FLG_UNREADABLE      18
40 #define FLG_REALM1          19
41 #define FLG_REALM2          20
42 #define FLG_FIRST           21
43 #define FLG_SECOND          22
44 #define FLG_THIRD           23
45 #define FLG_FOURTH          24
46
47 #define FLG_ITEMS           30
48 #define FLG_WEAPONS         31
49 #define FLG_ARMORS          32
50 #define FLG_MISSILES        33
51 #define FLG_DEVICES         34
52 #define FLG_LIGHTS          35
53 #define FLG_JUNKS           36
54 #define FLG_SPELLBOOKS      37
55 #define FLG_HAFTED          38
56 #define FLG_SHIELDS         39
57 #define FLG_BOWS            40
58 #define FLG_RINGS           41
59 #define FLG_AMULETS         42
60 #define FLG_SUITS           43
61 #define FLG_CLOAKS          44
62 #define FLG_HELMS           45
63 #define FLG_GLOVES          46
64 #define FLG_BOOTS           47
65 #define FLG_FAVORITE        48
66
67 #define FLG_NOUN_BEGIN      FLG_ITEMS
68 #define FLG_NOUN_END        FLG_FAVORITE
69
70 #ifdef JP
71
72 #define KEY_ALL "¤¹¤Ù¤Æ¤Î"
73
74 #ifdef MAC_MPW
75 /*
76  * MEGA HACK -- MPW¤Î¥Ð¥°½ü¤±¡£
77  * pre-processÃæ¤Ë¡Ö¼ý¡×¤Î»ú¤Î2¥Ð¥¤¥ÈÌܤ¬¾¡¼ê¤Ë¾Ã¤¨¤Æ¤·¤Þ¤¦¡£
78  */
79 #define KEY_COLLECTING "\x8e\xfb½¸Ãæ¤Î"
80 #else
81 #define KEY_COLLECTING "¼ý½¸Ãæ¤Î"
82 #endif
83
84 #define KEY_UNIDENTIFIED "̤´ÕÄê¤Î"
85 #define KEY_IDENTIFIED "´ÕÄêºÑ¤ß¤Î"
86 #define KEY_STAR_IDENTIFIED "*´ÕÄê*ºÑ¤ß¤Î"
87 #define KEY_BOOSTED "¥À¥¤¥¹Ìܤΰ㤦"
88 #define KEY_MORE_THAN  "¥À¥¤¥¹ÌÜ"
89 #define KEY_DICE  "°Ê¾å¤Î"
90 #define KEY_MORE_BONUS  "½¤ÀµÃÍ"
91 #define KEY_MORE_BONUS2  "°Ê¾å¤Î"
92 #define KEY_WORTHLESS "̵²ÁÃͤÎ"
93 #define KEY_ARTIFACT "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È"
94 #define KEY_EGO "¥¨¥´"
95 #define KEY_NAMELESS "̵ÌäÎ"
96 #define KEY_UNAWARE "̤ȽÌÀ¤Î"
97 #define KEY_WANTED "¾Þ¶â¼ó¤Î"
98 #define KEY_UNIQUE "¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼¤Î"
99 #define KEY_HUMAN "¿Í´Ö¤Î"
100 #define KEY_UNREADABLE "Æɤá¤Ê¤¤"
101 #define KEY_REALM1 "Âè°ìÎΰè¤Î"
102 #define KEY_REALM2 "ÂèÆóÎΰè¤Î"
103 #define KEY_FIRST "1ºýÌܤÎ"
104 #define KEY_SECOND "2ºýÌܤÎ"
105 #define KEY_THIRD "3ºýÌܤÎ"
106 #define KEY_FOURTH "4ºýÌܤÎ"
107 #define KEY_ITEMS "¥¢¥¤¥Æ¥à"
108 #define KEY_WEAPONS "Éð´ï"
109 #define KEY_ARMORS "Ëɶñ"
110 #define KEY_MISSILES "Ìð"
111 #define KEY_DEVICES "ËâË¡¥¢¥¤¥Æ¥à"
112 #define KEY_LIGHTS "¸÷¸»"
113 #define KEY_JUNKS "¤¬¤é¤¯¤¿"
114 #define KEY_SPELLBOOKS "ËâË¡½ñ"
115 #define KEY_HAFTED "Æß´ï"
116 #define KEY_SHIELDS "½â"
117 #define KEY_BOWS "µÝ"
118 #define KEY_RINGS "»ØÎØ"
119 #define KEY_AMULETS "¥¢¥ß¥å¥ì¥Ã¥È"
120 #define KEY_SUITS "³»"
121 #define KEY_CLOAKS "¥¯¥í¡¼¥¯"
122 #define KEY_HELMS "³õ"
123 #define KEY_GLOVES "äƼê"
124 #define KEY_BOOTS "·¤"
125 #define KEY_FAVORITE "ÆÀ°ÕÉð´ï"
126
127 #else 
128
129 #define KEY_ALL "all"
130 #define KEY_COLLECTING "collecting"
131 #define KEY_UNIDENTIFIED "unidentified"
132 #define KEY_IDENTIFIED "identified"
133 #define KEY_STAR_IDENTIFIED "*identified*"
134 #define KEY_BOOSTED "dice boosted"
135 #define KEY_MORE_THAN  "more than"
136 #define KEY_DICE  " dice"
137 #define KEY_MORE_BONUS  "more bonus than"
138 #define KEY_MORE_BONUS2  ""
139 #define KEY_WORTHLESS "worthless"
140 #define KEY_ARTIFACT "artifact"
141 #define KEY_EGO "ego"
142 #define KEY_NAMELESS "nameless"
143 #define KEY_UNAWARE "unaware"
144 #define KEY_WANTED "wanted"
145 #define KEY_UNIQUE "unique monster's"
146 #define KEY_HUMAN "human"
147 #define KEY_UNREADABLE "unreadable"
148 #define KEY_REALM1 "first realm's"
149 #define KEY_REALM2 "second realm's"
150 #define KEY_FIRST "first"
151 #define KEY_SECOND "second"
152 #define KEY_THIRD "third"
153 #define KEY_FOURTH "fourth"
154 #define KEY_ITEMS "items"
155 #define KEY_WEAPONS "weapons"
156 #define KEY_ARMORS "armors"
157 #define KEY_MISSILES "missiles"
158 #define KEY_DEVICES "magical devices"
159 #define KEY_LIGHTS "lights"
160 #define KEY_JUNKS "junks"
161 #define KEY_SPELLBOOKS "spellbooks"
162 #define KEY_HAFTED "hafted weapons"
163 #define KEY_SHIELDS "shields"
164 #define KEY_BOWS "bows"
165 #define KEY_RINGS "rings"
166 #define KEY_AMULETS "amulets"
167 #define KEY_SUITS "suits"
168 #define KEY_CLOAKS "cloaks"
169 #define KEY_HELMS "helms"
170 #define KEY_GLOVES "gloves"
171 #define KEY_BOOTS "boots"
172 #define KEY_FAVORITE "favorite weapons"
173
174 #endif /* JP */
175
176 #define MATCH_KEY(KEY) (!strncmp(ptr, KEY, sizeof(KEY)-1)\
177      ? (ptr += sizeof(KEY)-1, (' '==*ptr) ? ptr++ : 0, TRUE) : FALSE)
178 #define MATCH_KEY2(KEY) (!strncmp(ptr, KEY, sizeof(KEY)-1)\
179      ? (prev_ptr = ptr, ptr += sizeof(KEY)-1, (' '==*ptr) ? ptr++ : 0, TRUE) : FALSE)
180
181 #ifdef JP
182 #define ADD_KEY(KEY) strcat(ptr, KEY)
183 #else
184 #define ADD_KEY(KEY) (strcat(ptr, KEY), strcat(ptr, " "))
185 #endif
186 #define ADD_KEY2(KEY) strcat(ptr, KEY)
187
188 #define ADD_FLG(FLG) (entry->flag[FLG / 32] |= (1L << (FLG % 32)))
189 #define REM_FLG(FLG) (entry->flag[FLG / 32] &= ~(1L << (FLG % 32)))
190 #define ADD_FLG_NOUN(FLG) (ADD_FLG(FLG), prev_flg = FLG)
191 #define IS_FLG(FLG) (entry->flag[FLG / 32] & (1L << (FLG % 32)))
192
193 #ifdef JP
194         static char kanji_colon[] = "¡§";
195 #endif
196
197
198 /*
199  * A function to create new entry
200  */
201 static bool autopick_new_entry(autopick_type *entry, cptr str, bool allow_default)
202 {
203         cptr insc;
204         int i;
205         byte act = 0;
206         char buf[MAX_LINELEN];
207         cptr prev_ptr, ptr, old_ptr;
208         int prev_flg;
209
210         if (str[1] == ':') switch (str[0])
211         {
212         case '?': case '%':
213         case 'A': case 'P': case 'C':
214                 return FALSE;
215         }
216
217         entry->flag[0] = entry->flag[1] = 0L;
218         entry->dice = 0;
219
220         act = DO_AUTOPICK | DO_DISPLAY;
221         while (1)
222         {
223                 if ((act & DO_AUTOPICK) && *str == '!')
224                 {
225                         act &= ~DO_AUTOPICK;
226                         act |= DO_AUTODESTROY;
227                         str++;
228                 }
229                 else if ((act & DO_AUTOPICK) && *str == '~')
230                 {
231                         act &= ~DO_AUTOPICK;
232                         act |= DONT_AUTOPICK;
233                         str++;
234                 }
235                 else if ((act & DO_AUTOPICK) && *str == ';')
236                 {
237                         act &= ~DO_AUTOPICK;
238                         act |= DO_QUERY_AUTOPICK;
239                         str++;
240                 }
241                 else if ((act & DO_DISPLAY) && *str == '(')
242                 {
243                         act &= ~DO_DISPLAY;
244                         str++;
245                 }
246                 else
247                         break;
248         }
249
250         /* don't mind upper or lower case */
251         insc = NULL;
252         for (i = 0; *str; i++)
253         {
254                 char c = *str++;
255 #ifdef JP
256                 if (iskanji(c))
257                 {
258                         buf[i++] = c;
259                         buf[i] = *str++;
260                         continue;
261                 }
262 #endif
263                 /* Auto-inscription? */
264                 if (c == '#')
265                 {
266                         buf[i] = '\0';
267                         insc = str;
268                         break;
269                 }
270
271                 if (isupper(c)) c = tolower(c);
272
273                 buf[i] = c;
274         }
275         buf[i] = '\0';
276
277         /* Skip empty line unless allow_default */
278         if (!allow_default && *buf == 0) return FALSE;
279
280         /* Skip comment line */
281         if (*buf == 0 && insc) return FALSE;
282
283         ptr = prev_ptr = buf;
284         old_ptr = NULL;
285
286         while (old_ptr != ptr)
287         {
288                 /* Save current location */
289                 old_ptr = ptr;
290
291                 if (MATCH_KEY(KEY_ALL)) ADD_FLG(FLG_ALL);
292                 if (MATCH_KEY(KEY_COLLECTING)) ADD_FLG(FLG_COLLECTING);
293                 if (MATCH_KEY(KEY_UNIDENTIFIED)) ADD_FLG(FLG_UNIDENTIFIED);
294                 if (MATCH_KEY(KEY_IDENTIFIED)) ADD_FLG(FLG_IDENTIFIED);
295                 if (MATCH_KEY(KEY_STAR_IDENTIFIED)) ADD_FLG(FLG_STAR_IDENTIFIED);
296                 if (MATCH_KEY(KEY_BOOSTED)) ADD_FLG(FLG_BOOSTED);
297
298                 /*** Weapons whose dd*ds is more than nn ***/
299                 if (MATCH_KEY2(KEY_MORE_THAN))
300                 {
301                         int k = 0;
302                         entry->dice = 0;
303
304                         /* Drop leading spaces */
305                         while (' ' == *ptr) ptr++;
306
307                         /* Read number */
308                         while ('0' <= *ptr && *ptr <= '9')
309                         {
310                                 entry->dice = 10 * entry->dice + (*ptr - '0');
311                                 ptr++;
312                                 k++;
313                         }
314
315                         if (k > 0 && k <= 2)
316                         {
317                                 (void)MATCH_KEY(KEY_DICE);
318                                 ADD_FLG(FLG_MORE_THAN);
319                         }
320                         else
321                                 ptr = prev_ptr;
322                 }
323
324                 /*** Items whose magical bonus is more than n ***/
325                 if (MATCH_KEY2(KEY_MORE_BONUS))
326                 {
327                         int k = 0;
328                         entry->bonus = 0;
329
330                         /* Drop leading spaces */
331                         while (' ' == *ptr) ptr++;
332
333                         /* Read number */
334                         while ('0' <= *ptr && *ptr <= '9')
335                         {
336                                 entry->bonus = 10 * entry->bonus + (*ptr - '0');
337                                 ptr++;
338                                 k++;
339                         }
340
341                         if (k > 0 && k <= 2)
342                         {
343                                 (void)MATCH_KEY(KEY_MORE_BONUS2);
344                                 ADD_FLG(FLG_MORE_BONUS);
345                         }
346                         else
347                                 ptr = prev_ptr;
348                 }
349
350                 if (MATCH_KEY(KEY_WORTHLESS)) ADD_FLG(FLG_WORTHLESS);
351                 if (MATCH_KEY(KEY_EGO)) ADD_FLG(FLG_EGO);
352                 if (MATCH_KEY(KEY_NAMELESS)) ADD_FLG(FLG_NAMELESS);
353                 if (MATCH_KEY(KEY_UNAWARE)) ADD_FLG(FLG_UNAWARE);
354                 if (MATCH_KEY(KEY_WANTED)) ADD_FLG(FLG_WANTED);
355                 if (MATCH_KEY(KEY_UNIQUE)) ADD_FLG(FLG_UNIQUE);
356                 if (MATCH_KEY(KEY_HUMAN)) ADD_FLG(FLG_HUMAN);
357                 if (MATCH_KEY(KEY_UNREADABLE)) ADD_FLG(FLG_UNREADABLE);
358                 if (MATCH_KEY(KEY_REALM1)) ADD_FLG(FLG_REALM1);
359                 if (MATCH_KEY(KEY_REALM2)) ADD_FLG(FLG_REALM2);
360                 if (MATCH_KEY(KEY_FIRST)) ADD_FLG(FLG_FIRST);
361                 if (MATCH_KEY(KEY_SECOND)) ADD_FLG(FLG_SECOND);
362                 if (MATCH_KEY(KEY_THIRD)) ADD_FLG(FLG_THIRD);
363                 if (MATCH_KEY(KEY_FOURTH)) ADD_FLG(FLG_FOURTH);
364         }
365
366         /* Not yet found any noun */
367         prev_flg = -1;
368
369         if (MATCH_KEY2(KEY_ARTIFACT)) ADD_FLG_NOUN(FLG_ARTIFACT);
370
371         if (MATCH_KEY2(KEY_ITEMS)) ADD_FLG_NOUN(FLG_ITEMS);
372         else if (MATCH_KEY2(KEY_WEAPONS)) ADD_FLG_NOUN(FLG_WEAPONS);
373         else if (MATCH_KEY2(KEY_ARMORS)) ADD_FLG_NOUN(FLG_ARMORS);
374         else if (MATCH_KEY2(KEY_MISSILES)) ADD_FLG_NOUN(FLG_MISSILES);
375         else if (MATCH_KEY2(KEY_DEVICES)) ADD_FLG_NOUN(FLG_DEVICES);
376         else if (MATCH_KEY2(KEY_LIGHTS)) ADD_FLG_NOUN(FLG_LIGHTS);
377         else if (MATCH_KEY2(KEY_JUNKS)) ADD_FLG_NOUN(FLG_JUNKS);
378         else if (MATCH_KEY2(KEY_SPELLBOOKS)) ADD_FLG_NOUN(FLG_SPELLBOOKS);
379         else if (MATCH_KEY2(KEY_HAFTED)) ADD_FLG_NOUN(FLG_HAFTED);
380         else if (MATCH_KEY2(KEY_SHIELDS)) ADD_FLG_NOUN(FLG_SHIELDS);
381         else if (MATCH_KEY2(KEY_BOWS)) ADD_FLG_NOUN(FLG_BOWS);
382         else if (MATCH_KEY2(KEY_RINGS)) ADD_FLG_NOUN(FLG_RINGS);
383         else if (MATCH_KEY2(KEY_AMULETS)) ADD_FLG_NOUN(FLG_AMULETS);
384         else if (MATCH_KEY2(KEY_SUITS)) ADD_FLG_NOUN(FLG_SUITS);
385         else if (MATCH_KEY2(KEY_CLOAKS)) ADD_FLG_NOUN(FLG_CLOAKS);
386         else if (MATCH_KEY2(KEY_HELMS)) ADD_FLG_NOUN(FLG_HELMS);
387         else if (MATCH_KEY2(KEY_GLOVES)) ADD_FLG_NOUN(FLG_GLOVES);
388         else if (MATCH_KEY2(KEY_BOOTS)) ADD_FLG_NOUN(FLG_BOOTS);
389         else if (MATCH_KEY2(KEY_FAVORITE)) ADD_FLG_NOUN(FLG_FAVORITE);
390
391         /* Last 'keyword' must be at the correct location */
392         if (*ptr == ':')
393                 ptr++;
394 #ifdef JP
395         else if (ptr[0] == kanji_colon[0] && ptr[1] == kanji_colon[1])
396                 ptr += 2;
397 #endif
398         else if (*ptr == '\0')
399         {
400                 /* There was no noun */
401                 if (prev_flg == -1)
402
403                 /* Add extra word "items" */
404                 ADD_FLG_NOUN(FLG_ITEMS);
405         }
406         else
407         {
408                 /* Noun type? */
409                 if (prev_flg != -1)
410                 {
411                         /* A noun type keyword didn't end correctly */
412                         entry->flag[prev_flg/32] &= ~(1L<< (prev_flg%32));
413                         ptr = prev_ptr;
414                 }
415         }
416
417         /* Save this auto-picker entry line */
418         entry->name = string_make(ptr);
419         entry->action = act;
420         entry->insc = string_make(insc);
421
422         return TRUE;
423 }
424
425
426 /*
427  * Favorite weapons
428  */
429 static bool is_favorite(object_type *o_ptr, bool others_ok)
430 {
431         /* Only weapons match */
432         switch(o_ptr->tval)
433         {
434         case TV_BOW: case TV_HAFTED: case TV_POLEARM:
435         case TV_SWORD: case TV_DIGGING:
436                 break;
437         default: return FALSE;
438         }
439
440         /* Favorite weapons are varied depend on the class */
441         switch (p_ptr->pclass)
442         {
443         case CLASS_PRIEST:
444         {
445                 u32b flgs[TR_FLAG_SIZE];
446                 object_flags_known(o_ptr, flgs);
447
448                 if (!have_flag(flgs, TR_BLESSED) && 
449                     !(o_ptr->tval == TV_HAFTED))
450                         return FALSE;
451                 break;
452         }
453
454         case CLASS_MONK:
455         case CLASS_FORCETRAINER:
456                 /* Icky to wield? */
457                 if (!(s_info[p_ptr->pclass].w_max[o_ptr->tval-TV_BOW][o_ptr->sval]))
458                         return FALSE;
459                 break;
460
461         case CLASS_BEASTMASTER:
462         case CLASS_CAVALRY:
463         {
464                 u32b flgs[TR_FLAG_SIZE];
465                 object_flags_known(o_ptr, flgs);
466
467                 /* Is it known to be suitable to using while riding? */
468                 if (!(have_flag(flgs, TR_RIDING)))
469                         return FALSE;
470
471                 break;
472         }
473
474         case CLASS_NINJA:
475                 /* Icky to wield? */
476                 if (s_info[p_ptr->pclass].w_max[o_ptr->tval-TV_BOW][o_ptr->sval] <= WEAPON_EXP_BEGINNER)
477                         return FALSE;
478                 break;
479
480         default:
481                 /* Non-special class */
482                 if (others_ok) return TRUE;
483                 else return FALSE;
484         }
485
486         return TRUE;
487 }
488
489
490 /*
491  * Get auto-picker entry from o_ptr.
492  */
493 static void autopick_entry_from_object(autopick_type *entry, object_type *o_ptr)
494 {
495         /* Assume that object name is to be added */
496         bool name = TRUE;
497
498         entry->name = NULL;
499         entry->insc = string_make(quark_str(o_ptr->inscription));
500         entry->action = DO_AUTOPICK | DO_DISPLAY;
501         entry->flag[0] = entry->flag[1] = 0L;
502         entry->dice = 0;
503
504         if (!object_aware_p(o_ptr))
505         {
506                 ADD_FLG(FLG_UNAWARE);
507         }
508         else if (!object_known_p(o_ptr))
509         {
510                 ADD_FLG(FLG_UNIDENTIFIED);
511         }
512         else
513         {
514                 if (o_ptr->name2)
515                 {
516                         ego_item_type *e_ptr = &e_info[o_ptr->name2];
517                         entry->name = string_make(e_name + e_ptr->name);
518                         name = FALSE;
519                         ADD_FLG(FLG_EGO);
520                 }
521                 else if (o_ptr->name1 || o_ptr->art_name)
522                         ADD_FLG(FLG_ARTIFACT);
523                 else
524                 {
525                         /* Wearable nameless object */
526                         switch (o_ptr->tval)
527                         {
528                         case TV_WHISTLE:
529                         case TV_SHOT: case TV_ARROW: case TV_BOLT: case TV_BOW:
530                         case TV_DIGGING: case TV_HAFTED: case TV_POLEARM: case TV_SWORD: 
531                         case TV_BOOTS: case TV_GLOVES: case TV_HELM: case TV_CROWN:
532                         case TV_SHIELD: case TV_CLOAK:
533                         case TV_SOFT_ARMOR: case TV_HARD_ARMOR: case TV_DRAG_ARMOR:
534                         case TV_LITE: case TV_AMULET: case TV_RING: case TV_CARD:
535
536                                 ADD_FLG(FLG_NAMELESS);
537                                 break;
538                         }
539                 }
540
541         }
542
543
544         switch(o_ptr->tval)
545         {
546                 object_kind *k_ptr; 
547         case TV_HAFTED: case TV_POLEARM: case TV_SWORD: case TV_DIGGING:
548                 k_ptr = &k_info[o_ptr->k_idx];
549                 if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
550                         ADD_FLG(FLG_BOOSTED);
551         }
552
553         if (o_ptr->tval == TV_CORPSE && object_is_shoukinkubi(o_ptr))
554         {
555                 REM_FLG(FLG_WORTHLESS);
556                 ADD_FLG(FLG_WANTED);
557         }
558
559         if ((o_ptr->tval == TV_CORPSE || o_ptr->tval == TV_STATUE)
560             && (r_info[o_ptr->pval].flags1 & RF1_UNIQUE))
561         {
562                 ADD_FLG(FLG_UNIQUE);
563         }
564
565         if (o_ptr->tval == TV_CORPSE && strchr("pht", r_info[o_ptr->pval].d_char))
566         {
567                 ADD_FLG(FLG_HUMAN);
568         }
569
570         if (o_ptr->tval >= TV_LIFE_BOOK &&
571             !check_book_realm(o_ptr->tval, o_ptr->sval))
572         {
573                 ADD_FLG(FLG_UNREADABLE);
574                 if (o_ptr->tval != TV_ARCANE_BOOK) name = FALSE;
575         }
576
577         if (REALM1_BOOK == o_ptr->tval &&
578             p_ptr->pclass != CLASS_SORCERER &&
579             p_ptr->pclass != CLASS_RED_MAGE)
580         {
581                 ADD_FLG(FLG_REALM1);
582                 name = FALSE;
583         }
584
585         if (REALM2_BOOK == o_ptr->tval &&
586             p_ptr->pclass != CLASS_SORCERER &&
587             p_ptr->pclass != CLASS_RED_MAGE)
588         {
589                 ADD_FLG(FLG_REALM2);
590                 name = FALSE;
591         }
592
593         if (o_ptr->tval >= TV_LIFE_BOOK && 0 == o_ptr->sval)
594                 ADD_FLG(FLG_FIRST);
595         if (o_ptr->tval >= TV_LIFE_BOOK && 1 == o_ptr->sval)
596                 ADD_FLG(FLG_SECOND);
597         if (o_ptr->tval >= TV_LIFE_BOOK && 2 == o_ptr->sval)
598                 ADD_FLG(FLG_THIRD);
599         if (o_ptr->tval >= TV_LIFE_BOOK && 3 == o_ptr->sval)
600                 ADD_FLG(FLG_FOURTH);
601
602         if (o_ptr->tval == TV_SHOT || o_ptr->tval == TV_BOLT
603                  || o_ptr->tval == TV_ARROW)
604                 ADD_FLG(FLG_MISSILES);
605         else if (o_ptr->tval == TV_SCROLL || o_ptr->tval == TV_STAFF
606                  || o_ptr->tval == TV_WAND || o_ptr->tval == TV_ROD)
607                 ADD_FLG(FLG_DEVICES);
608         else if (o_ptr->tval == TV_LITE)
609                 ADD_FLG(FLG_LIGHTS);
610         else if (o_ptr->tval == TV_SKELETON || o_ptr->tval == TV_BOTTLE
611                  || o_ptr->tval == TV_JUNK || o_ptr->tval == TV_STATUE)
612                 ADD_FLG(FLG_JUNKS);
613         else if (o_ptr->tval >= TV_LIFE_BOOK)
614                 ADD_FLG(FLG_SPELLBOOKS);
615         else if (is_favorite(o_ptr, FALSE))
616                 ADD_FLG(FLG_FAVORITE);
617         else if (o_ptr->tval == TV_POLEARM || o_ptr->tval == TV_SWORD
618                  || o_ptr->tval == TV_DIGGING || o_ptr->tval == TV_HAFTED)
619                 ADD_FLG(FLG_WEAPONS);
620         else if (o_ptr->tval == TV_SHIELD)
621                 ADD_FLG(FLG_SHIELDS);
622         else if (o_ptr->tval == TV_BOW)
623                 ADD_FLG(FLG_BOWS);
624         else if (o_ptr->tval == TV_RING)
625                 ADD_FLG(FLG_RINGS);
626         else if (o_ptr->tval == TV_AMULET)
627                 ADD_FLG(FLG_AMULETS);
628         else if (o_ptr->tval == TV_DRAG_ARMOR || o_ptr->tval == TV_HARD_ARMOR ||
629                  o_ptr->tval == TV_SOFT_ARMOR)
630                 ADD_FLG(FLG_SUITS);
631         else if (o_ptr->tval == TV_CLOAK)
632                 ADD_FLG(FLG_CLOAKS);
633         else if (o_ptr->tval == TV_HELM)
634                 ADD_FLG(FLG_HELMS);
635         else if (o_ptr->tval == TV_GLOVES)
636                 ADD_FLG(FLG_GLOVES);
637         else if (o_ptr->tval == TV_BOOTS)
638                 ADD_FLG(FLG_BOOTS);
639
640
641         if (name)
642         {
643                 char o_name[MAX_NLEN];
644                 object_desc(o_name, o_ptr, FALSE, 0);
645
646                 entry->name = string_make(o_name);
647         }
648
649         else if (!entry->name)
650         {
651                 entry->name = string_make("");
652         }
653
654         return;
655 }
656
657
658 /*
659  * A function to delete entry
660  */
661 static void autopick_free_entry(autopick_type *entry)
662 {
663         string_free(entry->name);
664         string_free(entry->insc);
665 }
666
667
668 /*
669  * Initialize auto-picker preference
670  */
671 #define MAX_AUTOPICK_DEFAULT 200
672
673 void init_autopicker(void)
674 {
675         static const char easy_autopick_inscription[] = "(:=g";
676         autopick_type entry;
677         int i;
678
679         if (!autopick_list)
680         {
681                 max_max_autopick = MAX_AUTOPICK_DEFAULT;
682                 C_MAKE(autopick_list, max_max_autopick, autopick_type);
683                 max_autopick = 0;
684         }
685
686         /* Clear old entries */
687         for( i = 0; i < max_autopick; i++)
688                 autopick_free_entry(&autopick_list[i]);
689
690         max_autopick = 0;
691
692         /* There is always one entry "=g" */
693         autopick_new_entry(&entry, easy_autopick_inscription, TRUE);
694         autopick_list[max_autopick++] = entry;
695 }
696
697
698 /*
699  * Add one line to autopick_list[]
700  */
701 static void add_autopick_list(autopick_type *entry)
702 {
703         /* There is no enough space to add one line */
704         if (max_autopick >= max_max_autopick)
705         {
706                 int old_max_max_autopick = max_max_autopick;
707                 autopick_type *old_autopick_list = autopick_list;
708
709                 /* Increase size of list */
710                 max_max_autopick += MAX_AUTOPICK_DEFAULT;
711
712                 /* Allocate */
713                 C_MAKE(autopick_list, max_max_autopick, autopick_type);
714
715                 /* Copy from old list to new list */
716                 C_COPY(autopick_list, old_autopick_list, old_max_max_autopick, autopick_type);
717
718                 /* Kill old list */
719                 C_FREE(old_autopick_list, old_max_max_autopick, autopick_type);
720         }
721
722         /* Add one line */
723         autopick_list[max_autopick] = *entry;
724
725         max_autopick++;
726 }
727
728
729 /*
730  *  Process line for auto picker/destroyer.
731  */
732 errr process_pickpref_file_line(char *buf)
733 {
734         autopick_type an_entry, *entry = &an_entry;
735         int i;
736
737         /* Nuke illegal char */
738         for(i = 0; buf[i]; i++)
739         {
740 #ifdef JP
741                 if (iskanji(buf[i]))
742                 {
743                         i++;
744                         continue;
745                 }
746 #endif
747                 if (isspace(buf[i]) && buf[i] != ' ')
748                         break;
749         }
750         buf[i] = 0;
751         
752         if (!autopick_new_entry(entry, buf, FALSE)) return 0;
753
754         /* Already has the same entry? */ 
755         for(i = 0; i < max_autopick; i++)
756                 if(!strcmp(entry->name, autopick_list[i].name)
757                    && entry->flag[0] == autopick_list[i].flag[0]
758                    && entry->flag[1] == autopick_list[i].flag[1]
759                    && entry->dice == autopick_list[i].dice
760                    && entry->bonus == autopick_list[i].bonus) return 0;
761
762         add_autopick_list(entry);
763         return 0;
764 }
765
766
767 /*
768  * Reconstruct preference line from entry
769  */
770 cptr autopick_line_from_entry(autopick_type *entry)
771 {
772         char buf[MAX_LINELEN];
773         char *ptr;
774         bool sepa_flag = TRUE;
775
776         *buf = '\0';
777         if (!(entry->action & DO_DISPLAY)) strcat(buf, "(");
778         if (entry->action & DO_QUERY_AUTOPICK) strcat(buf, ";");
779         if (entry->action & DO_AUTODESTROY) strcat(buf, "!");
780         if (entry->action & DONT_AUTOPICK) strcat(buf, "~");
781
782         ptr = buf;
783
784         if (IS_FLG(FLG_ALL)) ADD_KEY(KEY_ALL);
785         if (IS_FLG(FLG_COLLECTING)) ADD_KEY(KEY_COLLECTING);
786         if (IS_FLG(FLG_UNIDENTIFIED)) ADD_KEY(KEY_UNIDENTIFIED);
787         if (IS_FLG(FLG_IDENTIFIED)) ADD_KEY(KEY_IDENTIFIED);
788         if (IS_FLG(FLG_STAR_IDENTIFIED)) ADD_KEY(KEY_STAR_IDENTIFIED);
789         if (IS_FLG(FLG_UNAWARE)) ADD_KEY(KEY_UNAWARE);
790         if (IS_FLG(FLG_BOOSTED)) ADD_KEY(KEY_BOOSTED);
791
792         if (IS_FLG(FLG_MORE_THAN))
793         {
794                 ADD_KEY(KEY_MORE_THAN);
795                 strcat(ptr, format("%d", entry->dice));
796                 ADD_KEY(KEY_DICE);
797         }
798
799         if (IS_FLG(FLG_MORE_BONUS))
800         {
801                 ADD_KEY(KEY_MORE_BONUS);
802                 strcat(ptr, format("%d", entry->bonus));
803                 ADD_KEY(KEY_MORE_BONUS2);
804         }
805
806         if (IS_FLG(FLG_UNREADABLE)) ADD_KEY(KEY_UNREADABLE);
807         if (IS_FLG(FLG_REALM1)) ADD_KEY(KEY_REALM1);
808         if (IS_FLG(FLG_REALM2)) ADD_KEY(KEY_REALM2);
809         if (IS_FLG(FLG_FIRST)) ADD_KEY(KEY_FIRST);
810         if (IS_FLG(FLG_SECOND)) ADD_KEY(KEY_SECOND);
811         if (IS_FLG(FLG_THIRD)) ADD_KEY(KEY_THIRD);
812         if (IS_FLG(FLG_FOURTH)) ADD_KEY(KEY_FOURTH);
813         if (IS_FLG(FLG_WANTED)) ADD_KEY(KEY_WANTED);
814         if (IS_FLG(FLG_UNIQUE)) ADD_KEY(KEY_UNIQUE);
815         if (IS_FLG(FLG_HUMAN)) ADD_KEY(KEY_HUMAN);
816         if (IS_FLG(FLG_WORTHLESS)) ADD_KEY(KEY_WORTHLESS);
817         if (IS_FLG(FLG_NAMELESS)) ADD_KEY(KEY_NAMELESS);
818         if (IS_FLG(FLG_EGO)) ADD_KEY(KEY_EGO);
819
820         if (IS_FLG(FLG_ARTIFACT)) ADD_KEY(KEY_ARTIFACT);
821
822         if (IS_FLG(FLG_ITEMS)) ADD_KEY2(KEY_ITEMS);
823         else if (IS_FLG(FLG_WEAPONS)) ADD_KEY2(KEY_WEAPONS);
824         else if (IS_FLG(FLG_ARMORS)) ADD_KEY2(KEY_ARMORS);
825         else if (IS_FLG(FLG_MISSILES)) ADD_KEY2(KEY_MISSILES);
826         else if (IS_FLG(FLG_DEVICES)) ADD_KEY2(KEY_DEVICES);
827         else if (IS_FLG(FLG_LIGHTS)) ADD_KEY2(KEY_LIGHTS);
828         else if (IS_FLG(FLG_JUNKS)) ADD_KEY2(KEY_JUNKS);
829         else if (IS_FLG(FLG_SPELLBOOKS)) ADD_KEY2(KEY_SPELLBOOKS);
830         else if (IS_FLG(FLG_HAFTED)) ADD_KEY2(KEY_HAFTED);
831         else if (IS_FLG(FLG_SHIELDS)) ADD_KEY2(KEY_SHIELDS);
832         else if (IS_FLG(FLG_BOWS)) ADD_KEY2(KEY_BOWS);
833         else if (IS_FLG(FLG_RINGS)) ADD_KEY2(KEY_RINGS);
834         else if (IS_FLG(FLG_AMULETS)) ADD_KEY2(KEY_AMULETS);
835         else if (IS_FLG(FLG_SUITS)) ADD_KEY2(KEY_SUITS);
836         else if (IS_FLG(FLG_CLOAKS)) ADD_KEY2(KEY_CLOAKS);
837         else if (IS_FLG(FLG_HELMS)) ADD_KEY2(KEY_HELMS);
838         else if (IS_FLG(FLG_GLOVES)) ADD_KEY2(KEY_GLOVES);
839         else if (IS_FLG(FLG_BOOTS)) ADD_KEY2(KEY_BOOTS);
840         else if (IS_FLG(FLG_FAVORITE)) ADD_KEY2(KEY_FAVORITE);
841
842         /* You don't need sepalator after adjective */
843         /* 'artifact' is not true adjective */
844         else if (!IS_FLG(FLG_ARTIFACT))
845                 sepa_flag = FALSE;
846
847         if (entry->name && entry->name[0])
848         {
849                 int i, j = 0;
850
851                 if (sepa_flag) strcat(buf, ":");
852
853                 i = strlen(buf);
854                 while (entry->name[j] && i < MAX_LINELEN - 2 - 1)
855                 {
856 #ifdef JP
857                         if (iskanji(entry->name[j]))
858                                 buf[i++] = entry->name[j++];
859 #endif
860                         buf[i++] = entry->name[j++];
861                 }
862                 buf[i] = '\0';
863         }
864
865         if (entry->insc)
866         {
867                 int i, j = 0;
868                 strcat(buf, "#");
869                 i = strlen(buf);
870
871                 while (entry->insc[j] && i < MAX_LINELEN - 2)
872                 {
873 #ifdef JP
874                         if (iskanji(entry->insc[j]))
875                                 buf[i++] = entry->insc[j++];
876 #endif
877                         buf[i++] = entry->insc[j++];
878                 }
879                 buf[i] = '\0';
880         }
881
882         return string_make(buf);
883 }
884
885
886 /*
887  * Reconstruct preference line from entry and kill entry
888  */
889 static cptr autopick_line_from_entry_kill(autopick_type *entry)
890 {
891         cptr ptr = autopick_line_from_entry(entry);
892
893         /* Free memory for original entry */
894         autopick_free_entry(entry);
895
896         return ptr;
897 }
898
899
900 /*
901  * A function for Auto-picker/destroyer
902  * Examine whether the object matches to the entry
903  */
904 static bool is_autopick_aux(object_type *o_ptr, autopick_type *entry, cptr o_name)
905 {
906         int j;
907         cptr ptr = entry->name;
908
909         /*** Unidentified ***/
910         if (IS_FLG(FLG_UNIDENTIFIED)
911             && (object_known_p(o_ptr) || (o_ptr->ident & IDENT_SENSE)))
912                 return FALSE;
913
914         /*** Identified ***/
915         if (IS_FLG(FLG_IDENTIFIED) && !object_known_p(o_ptr))
916                 return FALSE;
917
918         /*** *Identified* ***/
919         if (IS_FLG(FLG_STAR_IDENTIFIED) &&
920             (!object_known_p(o_ptr) || !(o_ptr->ident & IDENT_MENTAL)))
921                 return FALSE;
922
923         /*** Dice boosted (weapon of slaying) ***/
924         if (IS_FLG(FLG_BOOSTED))
925         {
926                 object_kind *k_ptr = &k_info[o_ptr->k_idx];
927                         
928                 switch( o_ptr->tval )
929                 {
930                 case TV_HAFTED:
931                 case TV_POLEARM:
932                 case TV_SWORD:
933                 case TV_DIGGING:
934                         if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
935                                 break;
936                         else
937                                 return FALSE;
938                 default:
939                         return FALSE;
940                 }
941         }
942
943         /*** Weapons which dd*ds is more than nn ***/
944         if (IS_FLG(FLG_MORE_THAN))
945         {
946                 if (o_ptr->dd * o_ptr->ds < entry->dice)
947                         return FALSE;
948         }
949                                 
950         /*** Weapons whic dd*ds is more than nn ***/
951         if (IS_FLG(FLG_MORE_BONUS))
952         {
953                 if (!object_known_p(o_ptr)) return FALSE;
954
955                 if (o_ptr->pval)
956                 {
957                         if (o_ptr->pval < entry->bonus) return FALSE;
958                 }
959                 else
960                 {
961                         if (o_ptr->to_h < entry->bonus &&
962                             o_ptr->to_d < entry->bonus &&
963                             o_ptr->to_a < entry->bonus &&
964                             o_ptr->pval < entry->bonus)
965                                 return FALSE;
966                 }
967         }
968                                 
969         /*** Worthless items ***/
970         if (IS_FLG(FLG_WORTHLESS) && object_value(o_ptr) > 0)
971                 return FALSE;
972
973         /*** Artifact object ***/
974         if (IS_FLG(FLG_ARTIFACT))
975         {
976                 if (!object_known_p(o_ptr) || (!o_ptr->name1 && !o_ptr->art_name))
977                         return FALSE;
978         }
979
980         /*** Ego object ***/
981         if (IS_FLG(FLG_EGO))
982         {
983                 if (!object_known_p(o_ptr) || !o_ptr->name2)
984                         return FALSE;
985         }
986
987         /*** Nameless ***/
988         if (IS_FLG(FLG_NAMELESS))
989         {
990                 switch (o_ptr->tval)
991                 {
992                 case TV_WHISTLE:
993                 case TV_SHOT: case TV_ARROW: case TV_BOLT: case TV_BOW:
994                 case TV_DIGGING: case TV_HAFTED: case TV_POLEARM: case TV_SWORD: 
995                 case TV_BOOTS: case TV_GLOVES: case TV_HELM: case TV_CROWN:
996                 case TV_SHIELD: case TV_CLOAK:
997                 case TV_SOFT_ARMOR: case TV_HARD_ARMOR: case TV_DRAG_ARMOR:
998                 case TV_LITE: case TV_AMULET: case TV_RING: case TV_CARD:
999                         if ((!object_known_p(o_ptr) || o_ptr->inscription
1000                              || o_ptr->name1 || o_ptr->name2 || o_ptr->art_name))
1001                                 return FALSE;
1002                         break;
1003                 default:
1004                         /* don't match */
1005                         return FALSE;
1006                 }
1007         }
1008
1009         /*** Unaware items ***/
1010         if (IS_FLG(FLG_UNAWARE) && object_aware_p(o_ptr))
1011                 return FALSE;
1012
1013         /*** Wanted monster's corpse/skeletons ***/
1014         if (IS_FLG(FLG_WANTED) &&
1015             (o_ptr->tval != TV_CORPSE || !object_is_shoukinkubi(o_ptr)))
1016                 return FALSE;
1017
1018         /*** Unique monster's corpse/skeletons/statues ***/
1019         if (IS_FLG(FLG_UNIQUE) &&
1020             ((o_ptr->tval != TV_CORPSE && o_ptr->tval != TV_STATUE) ||
1021              !(r_info[o_ptr->pval].flags1 & RF1_UNIQUE)))
1022                 return FALSE;
1023
1024         /*** Human corpse/skeletons (for Daemon magic) ***/
1025         if (IS_FLG(FLG_HUMAN) &&
1026             (o_ptr->tval != TV_CORPSE ||
1027              !strchr("pht", r_info[o_ptr->pval].d_char)))
1028                 return FALSE;
1029
1030         /*** Unreadable spellbooks ***/
1031         if (IS_FLG(FLG_UNREADABLE) &&
1032             (o_ptr->tval < TV_LIFE_BOOK ||
1033              check_book_realm(o_ptr->tval, o_ptr->sval)))
1034                 return FALSE;
1035
1036         /*** First realm spellbooks ***/
1037         if (IS_FLG(FLG_REALM1) && 
1038             (REALM1_BOOK != o_ptr->tval ||
1039              p_ptr->pclass == CLASS_SORCERER ||
1040              p_ptr->pclass == CLASS_RED_MAGE))
1041                 return FALSE;
1042
1043         /*** Second realm spellbooks ***/
1044         if (IS_FLG(FLG_REALM2) &&
1045             (REALM2_BOOK != o_ptr->tval ||
1046              p_ptr->pclass == CLASS_SORCERER ||
1047              p_ptr->pclass == CLASS_RED_MAGE))
1048                 return FALSE;
1049
1050         /*** First rank spellbooks ***/
1051         if (IS_FLG(FLG_FIRST) &&
1052             (o_ptr->tval < TV_LIFE_BOOK || 0 != o_ptr->sval))
1053                 return FALSE;
1054
1055         /*** Second rank spellbooks ***/
1056         if (IS_FLG(FLG_SECOND) &&
1057             (o_ptr->tval < TV_LIFE_BOOK || 1 != o_ptr->sval))
1058                 return FALSE;
1059
1060         /*** Third rank spellbooks ***/
1061         if (IS_FLG(FLG_THIRD) && 
1062             (o_ptr->tval < TV_LIFE_BOOK || 2 != o_ptr->sval))
1063                 return FALSE;
1064
1065         /*** Fourth rank spellbooks ***/
1066         if (IS_FLG(FLG_FOURTH) &&
1067             (o_ptr->tval < TV_LIFE_BOOK || 3 != o_ptr->sval))
1068                 return FALSE;
1069
1070         /*** Items ***/
1071         if (IS_FLG(FLG_WEAPONS))
1072         {
1073                 switch(o_ptr->tval)
1074                 {
1075                 case TV_BOW: case TV_HAFTED: case TV_POLEARM:
1076                 case TV_SWORD: case TV_DIGGING:
1077                         break;
1078                 default: return FALSE;
1079                 }
1080         }
1081         else if (IS_FLG(FLG_ARMORS))
1082         {
1083                 switch(o_ptr->tval)
1084                 {
1085                 case TV_BOOTS: case TV_GLOVES: case TV_CLOAK: case TV_CROWN:
1086                 case TV_HELM: case TV_SHIELD: case TV_SOFT_ARMOR:
1087                 case TV_HARD_ARMOR: case TV_DRAG_ARMOR:
1088                         break;
1089                 default: return FALSE;
1090                 }
1091         }
1092         else if (IS_FLG(FLG_MISSILES))
1093         {
1094                 switch(o_ptr->tval)
1095                 {
1096                 case TV_SHOT: case TV_BOLT: case TV_ARROW:
1097                         break;
1098                 default: return FALSE;
1099                 }
1100         }
1101         else if (IS_FLG(FLG_DEVICES))
1102         {
1103                 switch(o_ptr->tval)
1104                 {
1105                 case TV_SCROLL: case TV_STAFF: case TV_WAND: case TV_ROD:
1106                         break;
1107                 default: return FALSE;
1108                 }
1109         }
1110         else if (IS_FLG(FLG_LIGHTS))
1111         {
1112                 if (!(o_ptr->tval == TV_LITE))
1113                         return FALSE;
1114         }
1115         else if (IS_FLG(FLG_JUNKS))
1116         {
1117                 switch(o_ptr->tval)
1118                 {
1119                 case TV_SKELETON: case TV_BOTTLE:
1120                 case TV_JUNK: case TV_STATUE:
1121                         break;
1122                 default: return FALSE;
1123                 }
1124         }
1125         else if (IS_FLG(FLG_SPELLBOOKS))
1126         {
1127                 if (!(o_ptr->tval >= TV_LIFE_BOOK))
1128                         return FALSE;
1129         }
1130         else if (IS_FLG(FLG_HAFTED))
1131         {
1132                 if (!(o_ptr->tval == TV_HAFTED))
1133                         return FALSE;
1134         }
1135         else if (IS_FLG(FLG_SHIELDS))
1136         {
1137                 if (!(o_ptr->tval == TV_SHIELD))
1138                         return FALSE;
1139         }
1140         else if (IS_FLG(FLG_BOWS))
1141         {
1142                 if (!(o_ptr->tval == TV_BOW))
1143                         return FALSE;
1144         }
1145         else if (IS_FLG(FLG_RINGS))
1146         {
1147                 if (!(o_ptr->tval == TV_RING))
1148                         return FALSE;
1149         }
1150         else if (IS_FLG(FLG_AMULETS))
1151         {
1152                 if (!(o_ptr->tval == TV_AMULET))
1153                         return FALSE;
1154         }
1155         else if (IS_FLG(FLG_SUITS))
1156         {
1157                 if (!(o_ptr->tval == TV_DRAG_ARMOR ||
1158                       o_ptr->tval == TV_HARD_ARMOR ||
1159                       o_ptr->tval == TV_SOFT_ARMOR))
1160                         return FALSE;
1161         }
1162         else if (IS_FLG(FLG_CLOAKS))
1163         {
1164                 if (!(o_ptr->tval == TV_CLOAK))
1165                         return FALSE;
1166         }
1167         else if (IS_FLG(FLG_HELMS))
1168         {
1169                 if (!(o_ptr->tval == TV_CROWN || o_ptr->tval == TV_HELM))
1170                         return FALSE;
1171         }
1172         else if (IS_FLG(FLG_GLOVES))
1173         {
1174                 if (!(o_ptr->tval == TV_GLOVES))
1175                         return FALSE;
1176         }
1177         else if (IS_FLG(FLG_BOOTS))
1178         {
1179                 if (!(o_ptr->tval == TV_BOOTS))
1180                         return FALSE;
1181         }
1182         else if (IS_FLG(FLG_FAVORITE))
1183         {
1184                 if (!is_favorite(o_ptr, TRUE))
1185                         return FALSE;
1186         }
1187
1188         /* Keyword don't match */
1189         if (*ptr == '^')
1190         {
1191                 ptr++;
1192                 if (strncmp(o_name, ptr, strlen(ptr))) return FALSE;
1193         }
1194         else
1195         {
1196 #ifdef JP
1197                 if (!strstr_j(o_name, ptr)) return FALSE;
1198 #else
1199                 if (!strstr(o_name, ptr)) return FALSE;
1200 #endif
1201         }
1202
1203         /* TRUE when it need not to be 'collecting' */
1204         if (!IS_FLG(FLG_COLLECTING)) return TRUE;
1205
1206         /* Check if there is a same item */
1207         for (j = 0; j < INVEN_PACK; j++)
1208         {
1209                 /*
1210                  * 'Collecting' means the item must be absorbed 
1211                  * into an inventory slot.
1212                  * But an item can not be absorbed into itself!
1213                  */
1214                 if ((&inventory[j] != o_ptr) &&
1215                     object_similar(&inventory[j], o_ptr))
1216                         return TRUE;
1217         }
1218
1219         /* Not collecting */
1220         return FALSE;
1221 }
1222
1223
1224 /*
1225  * A function for Auto-picker/destroyer
1226  * Examine whether the object matches to the list of keywords or not.
1227  */
1228 int is_autopick(object_type *o_ptr)
1229 {
1230         int i;
1231         char o_name[MAX_NLEN];
1232
1233         if (o_ptr->tval == TV_GOLD) return -1;
1234         
1235         object_desc(o_name, o_ptr, FALSE, 3);
1236
1237         /* Force to be lower case string */
1238         for (i = 0; o_name[i]; i++)
1239         {
1240 #ifdef JP
1241                 if (iskanji(o_name[i]))
1242                         i++;
1243                 else
1244 #endif
1245                 if (isupper(o_name[i]))
1246                         o_name[i] = tolower(o_name[i]);
1247         }
1248         
1249         for (i=0; i < max_autopick; i++)
1250         {
1251                 autopick_type *entry = &autopick_list[i];
1252
1253                 if (is_autopick_aux(o_ptr, entry, o_name)) return i;
1254         }
1255
1256         /* No matching entry */
1257         return -1;
1258 }
1259
1260
1261 /*
1262  *  Auto inscription
1263  */
1264 void auto_inscribe_item(int item, int idx)
1265 {
1266         object_type *o_ptr;
1267
1268         /* Get the item (in the pack) */
1269         if (item >= 0) o_ptr = &inventory[item];
1270
1271         /* Get the item (on the floor) */
1272         else o_ptr = &o_list[0 - item];
1273
1274         /* Auto-inscription or Re-inscribe for resistances {%} */
1275         if ((idx < 0 || !autopick_list[idx].insc) && !o_ptr->inscription)
1276                 return;
1277
1278         if (!o_ptr->inscription)
1279                 o_ptr->inscription = quark_add(autopick_list[idx].insc);
1280
1281         if (item > INVEN_PACK)
1282         {
1283                 /* Redraw inscription */
1284                 p_ptr->window |= (PW_EQUIP);
1285
1286                 /* {.} and {$} effect p_ptr->warning and TRC_TELEPORT_SELF */
1287                 p_ptr->update |= (PU_BONUS);
1288         }
1289         else if (item >= 0)
1290         {
1291                 /* Redraw inscription */
1292                 p_ptr->window |= (PW_INVEN);
1293         }
1294 }
1295
1296
1297 /*
1298  * Automatically destroy items in this grid.
1299  */
1300 static bool is_opt_confirm_destroy(object_type *o_ptr)
1301 {
1302         if (!destroy_items) return FALSE;
1303
1304         /* Known to be worthless? */
1305         if (leave_worth)
1306                 if (object_value(o_ptr) > 0) return FALSE;
1307         
1308         if (leave_equip)
1309                 if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_DRAG_ARMOR)) return FALSE;
1310         
1311         if (leave_chest)
1312                 if ((o_ptr->tval == TV_CHEST) && o_ptr->pval) return FALSE;
1313         
1314         if (leave_wanted)
1315         {
1316                 if (o_ptr->tval == TV_CORPSE
1317                     && object_is_shoukinkubi(o_ptr)) return FALSE;
1318         }
1319         
1320         if (leave_corpse)
1321                 if (o_ptr->tval == TV_CORPSE) return FALSE;
1322         
1323         if (leave_junk)
1324                 if ((o_ptr->tval == TV_SKELETON) || (o_ptr->tval == TV_BOTTLE) || (o_ptr->tval == TV_JUNK) || (o_ptr->tval == TV_STATUE)) return FALSE;
1325
1326         if (leave_special)
1327         {
1328                 if (p_ptr->prace == RACE_DEMON)
1329                 {
1330                         if (o_ptr->tval == TV_CORPSE &&
1331                             o_ptr->sval == SV_CORPSE &&
1332                             strchr("pht", r_info[o_ptr->pval].d_char))
1333                                 return FALSE;
1334                 }
1335
1336                 if (p_ptr->pclass == CLASS_ARCHER)
1337                 {
1338                         if (o_ptr->tval == TV_SKELETON ||
1339                             (o_ptr->tval == TV_CORPSE && o_ptr->sval == SV_SKELETON))
1340                                 return FALSE;
1341                 }
1342                 else if (p_ptr->pclass == CLASS_NINJA)
1343                 {
1344                         if (o_ptr->tval == TV_LITE &&
1345                             o_ptr->name2 == EGO_LITE_DARKNESS)
1346                                 return FALSE;
1347                 }
1348                 else if (p_ptr->pclass == CLASS_BEASTMASTER ||
1349                          p_ptr->pclass == CLASS_CAVALRY)
1350                 {
1351                         if (o_ptr->tval == TV_WAND &&
1352                             o_ptr->sval == SV_WAND_HEAL_MONSTER)
1353                                 return FALSE;
1354                 }
1355         }
1356         
1357         if (o_ptr->tval == TV_GOLD) return FALSE;
1358         
1359         return TRUE;
1360 }
1361
1362
1363 /*
1364  * Automatically destroy an item if it is to be destroyed
1365  */
1366 static object_type autopick_last_destroyed_object;
1367
1368 bool auto_destroy_item(int item, int autopick_idx)
1369 {
1370         bool destroy = FALSE;
1371         object_type *o_ptr;
1372
1373         /* Don't destroy equipped items */
1374         if (item > INVEN_PACK) return FALSE;
1375
1376         /* Get the item (in the pack) */
1377         if (item >= 0) o_ptr = &inventory[item];
1378
1379         /* Get the item (on the floor) */
1380         else o_ptr = &o_list[0 - item];
1381
1382         /* Easy-Auto-Destroyer */
1383         if (is_opt_confirm_destroy(o_ptr)) destroy = TRUE;
1384
1385         /* Protected by auto-picker */
1386         if (autopick_idx >= 0 &&
1387             !(autopick_list[autopick_idx].action & DO_AUTODESTROY))
1388                 destroy = FALSE;
1389
1390         if (!always_pickup)
1391         {
1392                 /* Auto-picker/destroyer */
1393                 if (autopick_idx >= 0 &&
1394                     (autopick_list[autopick_idx].action & DO_AUTODESTROY))
1395                         destroy = TRUE;
1396         }
1397
1398         /* Not to be destroyed */
1399         if (!destroy) return FALSE;
1400
1401         /* Now decided to destroy */
1402
1403         disturb(0,0);
1404
1405         /* Artifact? */
1406         if (!can_player_destroy_object(o_ptr))
1407         {
1408                 char o_name[MAX_NLEN];
1409
1410                 /* Describe the object (with {terrible/special}) */
1411                 object_desc(o_name, o_ptr, TRUE, 3);
1412
1413                 /* Message */
1414 #ifdef JP
1415                 msg_format("%s¤ÏÇ˲õÉÔǽ¤À¡£", o_name);
1416 #else
1417                 msg_format("You cannot auto-destroy %s.", o_name);
1418 #endif
1419
1420                 /* Done */
1421                 return TRUE;
1422         }
1423
1424         /* Record name of destroyed item */
1425         COPY(&autopick_last_destroyed_object, o_ptr, object_type);
1426
1427         /* Destroy Later */
1428         o_ptr->marked |= OM_AUTODESTROY;
1429         p_ptr->notice |= PN_AUTODESTROY;
1430
1431         return TRUE;
1432 }
1433
1434
1435 /*
1436  *  Auto-destroy marked item
1437  */
1438 static void delayed_auto_destroy_aux(int item)
1439 {
1440         object_type *o_ptr;
1441
1442         /* Get the item (in the pack) */
1443         if (item >= 0) o_ptr = &inventory[item];
1444
1445         /* Get the item (on the floor) */
1446         else o_ptr = &o_list[0 - item];
1447
1448         if (o_ptr->k_idx && o_ptr->marked & OM_AUTODESTROY)
1449         {
1450                 char o_name[MAX_NLEN];
1451
1452                 /* Describe the object (with {terrible/special}) */
1453                 object_desc(o_name, o_ptr, TRUE, 3);
1454
1455                 /* Eliminate the item (from the pack) */
1456                 if (item >= 0)
1457                 {
1458                         inven_item_increase(item, -(o_ptr->number));
1459                         inven_item_optimize(item);
1460                 }
1461
1462                 /* Eliminate the item (from the floor) */
1463                 else
1464                 {
1465                         delete_object_idx(0 - item);
1466                 }
1467
1468                 /* Print a message */
1469 #ifdef JP
1470                 msg_format("%s¤ò¼«Æ°Ç˲õ¤·¤Þ¤¹¡£", o_name);
1471 #else
1472                 msg_format("Auto-destroying %s.", o_name);
1473 #endif
1474         }
1475 }
1476
1477
1478 /*
1479  *  Auto-destroy marked item in inventry and on floor
1480  */
1481 void delayed_auto_destroy(void)
1482 {
1483         int item;
1484
1485         /* 
1486          * Scan inventry in reverse order to prevent
1487          * skipping after inven_item_optimize()
1488          */
1489         for (item = INVEN_TOTAL - 1; item >= 0 ; item--)
1490                 delayed_auto_destroy_aux(item);
1491
1492         /* Scan the pile of objects */
1493         item = cave[py][px].o_idx;
1494         while (item)
1495         {
1496                 int next = o_list[item].next_o_idx;
1497                 delayed_auto_destroy_aux(-item);
1498                 item = next;
1499         }
1500 }
1501
1502
1503 /*
1504  * Automatically pickup/destroy items in this grid.
1505  */
1506 void auto_pickup_items(cave_type *c_ptr)
1507 {
1508         s16b this_o_idx, next_o_idx = 0;
1509         
1510         /* Scan the pile of objects */
1511         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1512         {
1513                 int idx;
1514         
1515                 /* Acquire object */
1516                 object_type *o_ptr = &o_list[this_o_idx];
1517                 
1518                 /* Acquire next object */
1519                 next_o_idx = o_ptr->next_o_idx;
1520
1521                 idx = is_autopick(o_ptr);
1522
1523                 /* Item index for floor -1,-2,-3,...  */
1524                 auto_inscribe_item((-this_o_idx), idx);
1525
1526                 if (idx >= 0 &&
1527                         (autopick_list[idx].action & (DO_AUTOPICK | DO_QUERY_AUTOPICK)))
1528                 {
1529                         disturb(0,0);
1530
1531                         if (!inven_carry_okay(o_ptr))
1532                         {
1533                                 char o_name[MAX_NLEN];
1534
1535                                 /* Describe the object */
1536                                 object_desc(o_name, o_ptr, TRUE, 3);
1537
1538                                 /* Message */
1539 #ifdef JP
1540                                 msg_format("¥¶¥Ã¥¯¤Ë¤Ï%s¤òÆþ¤ì¤ë·ä´Ö¤¬¤Ê¤¤¡£", o_name);
1541 #else
1542                                 msg_format("You have no room for %s.", o_name);
1543 #endif
1544                                 /* Hack - remember that the item has given a message here. */
1545                                 o_ptr->marked |= OM_NOMSG;
1546
1547                                 continue;
1548                         }
1549                         else if (autopick_list[idx].action & DO_QUERY_AUTOPICK)
1550                         {
1551                                 char out_val[MAX_NLEN+20];
1552                                 char o_name[MAX_NLEN];
1553
1554                                 if (o_ptr->marked & OM_NO_QUERY)
1555                                 {
1556                                         /* Already answered as 'No' */
1557                                         continue;
1558                                 }
1559
1560                                 /* Describe the object */
1561                                 object_desc(o_name, o_ptr, TRUE, 3);
1562
1563 #ifdef JP
1564                                 sprintf(out_val, "%s¤ò½¦¤¤¤Þ¤¹¤«? ", o_name);
1565 #else
1566                                 sprintf(out_val, "Pick up %s? ", o_name);
1567 #endif
1568
1569                                 if (!get_check(out_val))
1570                                 {
1571                                         /* Hack - remember that the item has given a message here. */
1572                                         o_ptr->marked |= (OM_NOMSG | OM_NO_QUERY);
1573                                         continue;
1574                                 }
1575
1576                         }
1577                         py_pickup_aux(this_o_idx);
1578
1579                         continue;
1580                 }
1581                 
1582                 /*
1583                  * Do auto-destroy;
1584                  * When always_pickup is 'yes', we disable
1585                  * auto-destroyer from autopick function, and do only
1586                  * easy-auto-destroyer.
1587                  */
1588                 else
1589                 {
1590                         if (auto_destroy_item((-this_o_idx), idx))
1591                                 continue;
1592                 }
1593         }
1594 }
1595
1596
1597 #define PT_DEFAULT 0
1598 #define PT_WITH_PNAME 1
1599
1600 /*
1601  *  Get file name for autopick preference
1602  */
1603 static cptr pickpref_filename(int filename_mode)
1604 {
1605 #ifdef JP
1606         static const char namebase[] = "picktype";
1607 #else
1608         static const char namebase[] = "pickpref";
1609 #endif
1610
1611         switch (filename_mode)
1612         {
1613         case PT_DEFAULT:
1614                 return format("%s.prf", namebase);
1615
1616         case PT_WITH_PNAME:
1617                 return format("%s-%s.prf", namebase, player_name);
1618
1619         default:
1620                 return NULL;
1621         }
1622 }
1623
1624
1625 static const char autoregister_header[] = "?:$AUTOREGISTER";
1626
1627 /*
1628  *  Clear auto registered lines in the picktype.prf .
1629  */
1630 static bool clear_auto_register(void)
1631 {
1632         char tmp_file[1024];
1633         char pref_file[1024];
1634         char buf[1024];
1635         FILE *pref_fff;
1636         FILE *tmp_fff;
1637         int num = 0;
1638         bool autoregister = FALSE;
1639         bool okay = TRUE;
1640
1641         path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_WITH_PNAME));
1642         pref_fff = my_fopen(pref_file, "r");
1643
1644         if (!pref_fff)
1645         {
1646                 path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_DEFAULT));
1647                 pref_fff = my_fopen(pref_file, "r");
1648         }
1649
1650         if (!pref_fff)
1651         {
1652                 /* No file yet */
1653                 return TRUE;
1654         }
1655
1656         /* Open a new (temporary) file */
1657         tmp_fff = my_fopen_temp(tmp_file, sizeof(tmp_file));
1658
1659         if (!tmp_fff)
1660         {
1661                 /* Close the preference file */
1662                 fclose(pref_fff);
1663
1664 #ifdef JP
1665                 msg_format("°ì»þ¥Õ¥¡¥¤¥ë %s ¤òºîÀ®¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", tmp_file);
1666 #else
1667                 msg_format("Failed to create temporary file %s.", tmp_file);
1668 #endif
1669                 msg_print(NULL);
1670                 return FALSE;
1671         }
1672
1673         
1674         /* Loop for every line */
1675         while (TRUE)
1676         {
1677                 /* Read a line */
1678                 if (my_fgets(pref_fff, buf, sizeof(buf))) break;
1679
1680                 if (autoregister)
1681                 {
1682                         /* Delete auto-registered line */
1683
1684                         /* Count auto-destroy preference lines */
1685                         if (buf[0] != '#' && buf[0] != '?') num++;
1686                 }
1687
1688                 /* We are looking for auto-registered line */
1689                 else
1690                 {
1691                         if (streq(buf, autoregister_header))
1692                         {
1693                                 /* Delete all further lines */
1694                                 autoregister = TRUE;
1695                         }
1696                         else
1697                         {
1698                                 /* Copy orginally lines */
1699                                 fprintf(tmp_fff, "%s\n", buf);
1700                         }
1701                 }
1702         }
1703
1704         /* Close files */
1705         my_fclose(pref_fff);
1706         my_fclose(tmp_fff);
1707
1708         if (num)
1709         {
1710 #ifdef JP
1711                 msg_format("°ÊÁ°¤Î¥­¥ã¥é¥¯¥¿¡¼ÍѤμ«Æ°ÀßÄê(%d¹Ô)¤¬»Ä¤Ã¤Æ¤¤¤Þ¤¹¡£", num);
1712                 strcpy(buf, "¸Å¤¤ÀßÄê¹Ô¤Ïºï½ü¤·¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡©");
1713 #else
1714                 msg_format("Auto registered lines (%d lines) for previous character are remaining.", num);
1715                 strcpy(buf, "These lines will be deleted.  Are you sure? ");
1716 #endif
1717
1718                 /* You can cancel it */
1719                 if (!get_check(buf))
1720                 {
1721                         okay = FALSE;
1722                         autoregister = FALSE;
1723
1724 #ifdef JP
1725                         msg_print("¥¨¥Ç¥£¥¿¤Î¥«¥Ã¥È&¥Ú¡¼¥¹¥ÈÅù¤ò»È¤Ã¤ÆɬÍפʹԤòÈòÆñ¤·¤Æ¤¯¤À¤µ¤¤¡£");
1726 #else
1727                         msg_print("Use cut & paste of auto picker editor (_) to keep old prefs.");
1728 #endif
1729                 }
1730         }
1731
1732
1733         /* If there are some changes, overwrite the original file with new one */
1734         if (autoregister)
1735         {
1736                 /* Copy contents of temporary file */
1737
1738                 tmp_fff = my_fopen(tmp_file, "r");
1739                 pref_fff = my_fopen(pref_file, "w");
1740
1741                 while (!my_fgets(tmp_fff, buf, sizeof(buf)))
1742                         fprintf(pref_fff, "%s\n", buf);
1743
1744                 my_fclose(pref_fff);
1745                 my_fclose(tmp_fff);
1746         }
1747
1748         /* Kill the temporary file */
1749         fd_kill(tmp_file);
1750
1751         return okay;
1752 }
1753
1754
1755 /*
1756  *  Automatically register an auto-destroy preference line
1757  */
1758 bool add_auto_register(object_type *o_ptr)
1759 {
1760         char buf[1024];
1761         char pref_file[1024];
1762         FILE *pref_fff;
1763         autopick_type an_entry, *entry = &an_entry;
1764
1765         int match_autopick = is_autopick(o_ptr);
1766
1767         /* Already registered */
1768         if (match_autopick != -1)
1769         {
1770                 cptr what;
1771                 byte act = autopick_list[match_autopick].action;
1772
1773 #ifdef JP
1774                 if (act & DO_AUTOPICK) what = "¼«Æ°¤Ç½¦¤¦";
1775                 else if (act & DO_AUTODESTROY) what = "¼«Æ°Ç˲õ¤¹¤ë";
1776                 else if (act & DONT_AUTOPICK) what = "ÊüÃÖ¤¹¤ë";
1777                 else /* if (act & DO_QUERY_AUTOPICK) */ what = "³Îǧ¤·¤Æ½¦¤¦";
1778
1779                 msg_format("¤½¤Î¥¢¥¤¥Æ¥à¤Ï´û¤Ë%s¤è¤¦¤ËÀßÄꤵ¤ì¤Æ¤¤¤Þ¤¹¡£", what);
1780 #else
1781                 if (act & DO_AUTOPICK) what = "auto-pickup";
1782                 else if (act & DO_AUTODESTROY) what = "auto-destroy";
1783                 else if (act & DONT_AUTOPICK) what = "leave on floor";
1784                 else /* if (act & DO_QUERY_AUTOPICK) */ what = "query auto-pickup";
1785
1786                 msg_format("The object is already registered to %s.", what);
1787 #endif
1788                 
1789                 return FALSE;
1790         }
1791
1792
1793         if (!p_ptr->autopick_autoregister)
1794         {
1795                 /* Clear old auto registered lines */
1796                 if (!clear_auto_register()) return FALSE;
1797         }
1798
1799         /* Try a filename with player name */
1800         path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_WITH_PNAME));
1801         pref_fff = my_fopen(pref_file, "r");
1802
1803         if (!pref_fff)
1804         {
1805                 /* Use default name */
1806                 path_build(pref_file, sizeof(pref_file), ANGBAND_DIR_USER, pickpref_filename(PT_DEFAULT));
1807                 pref_fff = my_fopen(pref_file, "r");
1808         }
1809
1810         /* Check the header */
1811         while (TRUE)
1812         {
1813                 /* Read a line */
1814                 if (my_fgets(pref_fff, buf, sizeof(buf)))
1815                 {
1816                         /* No header found */
1817                         p_ptr->autopick_autoregister = FALSE;
1818
1819                         break;
1820                 }
1821
1822                 if (streq(buf, autoregister_header))
1823                 {
1824                         /* Found the header */
1825                         p_ptr->autopick_autoregister = TRUE;
1826
1827                         break;
1828                 }
1829         }
1830
1831         /* Close read only FILE* */
1832         fclose(pref_fff);
1833
1834         /* Open for append */
1835         pref_fff = my_fopen(pref_file, "a");
1836
1837         /* Failure */
1838         if (!pref_fff) {
1839 #ifdef JP
1840                 msg_format("%s ¤ò³«¤¯¤³¤È¤¬¤Ç¤­¤Þ¤»¤ó¤Ç¤·¤¿¡£", pref_file);
1841 #else
1842                 msg_format("Failed to open %s.", pref_file);
1843 #endif
1844                 msg_print(NULL);
1845
1846                 /* Failed */
1847                 return FALSE;
1848         }
1849
1850         if (!p_ptr->autopick_autoregister)
1851         {
1852                 /* Add the header */
1853                 fprintf(pref_fff, "%s\n", autoregister_header);
1854
1855 #ifdef JP
1856                 fprintf(pref_fff, "%s\n", "# *·Ù¹ð!!* °Ê¹ß¤Î¹Ô¤Ï¼«Æ°ÅÐÏ¿¤µ¤ì¤¿¤â¤Î¤Ç¤¹¡£");
1857                 fprintf(pref_fff, "%s\n", "# ¸å¤Ç¼«Æ°Åª¤Ëºï½ü¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢É¬ÍפʹԤϾå¤ÎÊý¤Ø°ÜÆ°¤·¤Æ¤ª¤¤¤Æ¤¯¤À¤µ¤¤¡£");
1858 #else
1859                 fprintf(pref_fff, "%s\n", "# *Waring!* The lines below will be deleated later.");
1860                 fprintf(pref_fff, "%s\n", "# Keep it by cut & paste if you need these lines for future characters.");
1861 #endif
1862
1863                 /* Now auto register is in-use */
1864                 p_ptr->autopick_autoregister = TRUE;
1865         }
1866
1867         /* Get a preference entry */
1868         autopick_entry_from_object(entry, o_ptr);
1869
1870         /* Set to auto-destroy (with no-display) */
1871         entry->action = DO_AUTODESTROY;
1872
1873         /* Load the new line as preference */
1874         add_autopick_list(entry);
1875
1876         /* Add a line to the file */
1877         /* Don't kill "entry" */
1878         fprintf(pref_fff, "%s\n", autopick_line_from_entry(entry));
1879
1880         /* Close the file */
1881         fclose(pref_fff);
1882
1883         return TRUE;
1884 }
1885
1886
1887 /********  Auto-picker/destroyer editor  **********/
1888
1889 #define MAX_YANK MAX_LINELEN
1890 #define MAX_LINES 3000
1891
1892 #define MARK_MARK     0x01
1893 #define MARK_BY_SHIFT 0x02
1894
1895 #define LSTAT_BYPASS        0x01
1896 #define LSTAT_EXPRESSION    0x02
1897 #define LSTAT_AUTOREGISTER  0x04
1898
1899 #define QUIT_WITHOUT_SAVE 1
1900 #define QUIT_AND_SAVE     2
1901
1902 /* 
1903  * Struct for yank buffer
1904  */
1905 typedef struct chain_str {
1906         struct chain_str *next;
1907         char s[1];
1908 } chain_str_type;
1909
1910
1911 /*
1912  * Data struct for text editor
1913  */
1914 typedef struct {
1915         int wid, hgt;
1916         int cx, cy;
1917         int upper, left;
1918         int old_wid, old_hgt;
1919         int old_cy;
1920         int old_upper, old_left;
1921         int mx, my;
1922         byte mark;
1923
1924         object_type *search_o_ptr;
1925         cptr search_str;
1926         cptr last_destroyed;
1927
1928         chain_str_type *yank;
1929         bool yank_eol;
1930
1931         cptr *lines_list;
1932         byte states[MAX_LINES];
1933
1934         byte dirty_flags;
1935         int dirty_line;
1936         int filename_mode;
1937         int old_com_id;
1938
1939         bool changed;
1940 } text_body_type;
1941
1942
1943 /*
1944  * Dirty flag for text editor
1945  */
1946 #define DIRTY_ALL        0x01
1947 #define DIRTY_MODE       0x04
1948 #define DIRTY_SCREEN     0x08
1949 #define DIRTY_NOT_FOUND  0x10
1950 #define DIRTY_NO_SEARCH  0x20
1951 #define DIRTY_EXPRESSION 0x40
1952
1953
1954 /*
1955  * Describe which kind of object is Auto-picked/destroyed
1956  */
1957 static void describe_autopick(char *buff, autopick_type *entry)
1958 {
1959         cptr str = entry->name;
1960         byte act = entry->action;
1961         cptr insc = entry->insc;
1962         int i;
1963
1964         bool top = FALSE;
1965
1966 #ifdef JP
1967         cptr before_str[100], body_str;
1968         int before_n = 0;
1969
1970         body_str = "¥¢¥¤¥Æ¥à";
1971
1972         /*** Collecting items ***/
1973         /*** Which can be absorbed into a slot as a bundle ***/
1974         if (IS_FLG(FLG_COLLECTING))
1975                 before_str[before_n++] = "¼ý½¸Ãæ¤Ç´û¤Ë»ý¤Ã¤Æ¤¤¤ë¥¹¥í¥Ã¥È¤Ë¤Þ¤È¤á¤é¤ì¤ë";
1976         
1977         /*** Unidentified ***/
1978         if (IS_FLG(FLG_UNIDENTIFIED))
1979                 before_str[before_n++] = "̤´ÕÄê¤Î";
1980
1981         /*** Identified ***/
1982         if (IS_FLG(FLG_IDENTIFIED))
1983                 before_str[before_n++] = "´ÕÄêºÑ¤ß¤Î";
1984
1985         /*** *Identified* ***/
1986         if (IS_FLG(FLG_STAR_IDENTIFIED))
1987                 before_str[before_n++] = "´°Á´¤Ë´ÕÄêºÑ¤ß¤Î";
1988
1989         /*** Dice boosted (weapon of slaying) ***/
1990         if (IS_FLG(FLG_BOOSTED))
1991         {
1992                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤¬Ä̾ï¤è¤êÂ礭¤¤";
1993                 body_str = "Éð´ï";
1994         }
1995
1996         /*** Weapons whose dd*ds is more than nn ***/
1997         if (IS_FLG(FLG_MORE_THAN))
1998         {
1999                 static char more_than_desc_str[] = "___";
2000                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤ÎºÇÂçÃͤ¬";
2001                 body_str = "Éð´ï";
2002                         
2003                 sprintf(more_than_desc_str,"%d", entry->dice);
2004                 before_str[before_n++] = more_than_desc_str;
2005                 before_str[before_n++] = "°Ê¾å¤Î";
2006         }
2007
2008         /*** Items whose magical bonus is more than nn ***/
2009         if (IS_FLG(FLG_MORE_BONUS))
2010         {
2011                 static char more_bonus_desc_str[] = "___";
2012                 before_str[before_n++] = "½¤ÀµÃͤ¬(+";
2013                         
2014                 sprintf(more_bonus_desc_str,"%d", entry->bonus);
2015                 before_str[before_n++] = more_bonus_desc_str;
2016                 before_str[before_n++] = ")°Ê¾å¤Î";
2017         }
2018
2019         /*** Worthless items ***/
2020         if (IS_FLG(FLG_WORTHLESS))
2021                 before_str[before_n++] = "Ź¤Ç̵²ÁÃͤÈȽÄꤵ¤ì¤ë";
2022
2023         /*** Artifact ***/
2024         if (IS_FLG(FLG_ARTIFACT))
2025         {
2026                 before_str[before_n++] = "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Î";
2027                 body_str = "ÁõÈ÷";
2028         }
2029
2030         /*** Ego ***/
2031         if (IS_FLG(FLG_EGO))
2032         {
2033                 before_str[before_n++] = "¥¨¥´¥¢¥¤¥Æ¥à¤Î";
2034                 body_str = "ÁõÈ÷";
2035         }
2036
2037         /*** Nameless ***/
2038         if (IS_FLG(FLG_NAMELESS))
2039         {
2040                 before_str[before_n++] = "¥¨¥´¤Ç¤â¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Ç¤â¤Ê¤¤";
2041                 body_str = "ÁõÈ÷";
2042         }
2043
2044         /*** Unaware items ***/
2045         if (IS_FLG(FLG_UNAWARE))
2046                 before_str[before_n++] = "̤´ÕÄê¤Ç¤½¤Î¸ú²Ì¤âȽÌÀ¤·¤Æ¤¤¤Ê¤¤";
2047
2048         /*** Wanted monster's corpse/skeletons ***/
2049         if (IS_FLG(FLG_WANTED))
2050         {
2051                 before_str[before_n++] = "¥Ï¥ó¥¿¡¼»ö̳½ê¤Ç¾Þ¶â¼ó¤È¤µ¤ì¤Æ¤¤¤ë";
2052                 body_str = "»àÂΤä¹ü";
2053         }
2054
2055         /*** Human corpse/skeletons (for Daemon magic) ***/
2056         if (IS_FLG(FLG_HUMAN))
2057         {
2058                 before_str[before_n++] = "°­ËâËâË¡¤Ç»È¤¦¤¿¤á¤Î¿Í´Ö¤ä¥Ò¥å¡¼¥Þ¥Î¥¤¥É¤Î";
2059                 body_str = "»àÂΤä¹ü";
2060         }
2061
2062         /*** Unique monster's corpse/skeletons/statues ***/
2063         if (IS_FLG(FLG_UNIQUE))
2064         {
2065                 before_str[before_n++] = "¥æ¥Ë¡¼¥¯¥â¥ó¥¹¥¿¡¼¤Î";
2066                 body_str = "»àÂΤä¹ü";
2067         }
2068
2069         /*** Unreadable spellbooks ***/
2070         if (IS_FLG(FLG_UNREADABLE))
2071         {
2072                 before_str[before_n++] = "¤¢¤Ê¤¿¤¬Æɤá¤Ê¤¤Îΰè¤Î";
2073                 body_str = "ËâË¡½ñ";
2074         }
2075
2076         /*** First realm spellbooks ***/
2077         if (IS_FLG(FLG_REALM1))
2078         {
2079                 before_str[before_n++] = "Âè°ìÎΰè¤Î";
2080                 body_str = "ËâË¡½ñ";
2081         }
2082
2083         /*** Second realm spellbooks ***/
2084         if (IS_FLG(FLG_REALM2))
2085         {
2086                 before_str[before_n++] = "ÂèÆóÎΰè¤Î";
2087                 body_str = "ËâË¡½ñ";
2088         }
2089
2090         /*** First rank spellbooks ***/
2091         if (IS_FLG(FLG_FIRST))
2092         {
2093                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î1ºýÌܤÎ";
2094                 body_str = "ËâË¡½ñ";
2095         }
2096
2097         /*** Second rank spellbooks ***/
2098         if (IS_FLG(FLG_SECOND))
2099         {
2100                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î2ºýÌܤÎ";
2101                 body_str = "ËâË¡½ñ";
2102         }
2103
2104         /*** Third rank spellbooks ***/
2105         if (IS_FLG(FLG_THIRD))
2106         {
2107                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î3ºýÌܤÎ";
2108                 body_str = "ËâË¡½ñ";
2109         }
2110
2111         /*** Fourth rank spellbooks ***/
2112         if (IS_FLG(FLG_FOURTH))
2113         {
2114                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î4ºýÌܤÎ";
2115                 body_str = "ËâË¡½ñ";
2116         }
2117
2118         /*** Items ***/
2119         if (IS_FLG(FLG_ITEMS))
2120                 ; /* Nothing to do */
2121         else if (IS_FLG(FLG_WEAPONS))
2122                 body_str = "Éð´ï";
2123         else if (IS_FLG(FLG_ARMORS))
2124                 body_str = "Ëɶñ";
2125         else if (IS_FLG(FLG_MISSILES))
2126                 body_str = "ÃƤäÌð¤ä¥¯¥í¥¹¥Ü¥¦¤ÎÌð";
2127         else if (IS_FLG(FLG_DEVICES))
2128                 body_str = "´¬Êª¤äËâË¡ËÀ¤ä¾ó¤ä¥í¥Ã¥É";
2129         else if (IS_FLG(FLG_LIGHTS))
2130                 body_str = "¸÷¸»ÍѤΥ¢¥¤¥Æ¥à";
2131         else if (IS_FLG(FLG_JUNKS))
2132                 body_str = "Àޤ줿ËÀÅù¤Î¥¬¥é¥¯¥¿";
2133         else if (IS_FLG(FLG_SPELLBOOKS))
2134                 body_str = "ËâË¡½ñ";
2135         else if (IS_FLG(FLG_HAFTED))
2136                 body_str = "Æß´ï";
2137         else if (IS_FLG(FLG_SHIELDS))
2138                 body_str = "½â";
2139         else if (IS_FLG(FLG_BOWS))
2140                 body_str = "¥¹¥ê¥ó¥°¤äµÝ¤ä¥¯¥í¥¹¥Ü¥¦";
2141         else if (IS_FLG(FLG_RINGS))
2142                 body_str = "»ØÎØ";
2143         else if (IS_FLG(FLG_AMULETS))
2144                 body_str = "¥¢¥ß¥å¥ì¥Ã¥È";
2145         else if (IS_FLG(FLG_SUITS))
2146                 body_str = "³»";
2147         else if (IS_FLG(FLG_CLOAKS))
2148                 body_str = "¥¯¥í¡¼¥¯";
2149         else if (IS_FLG(FLG_HELMS))
2150                 body_str = "¥Ø¥ë¥á¥Ã¥È¤ä´§";
2151         else if (IS_FLG(FLG_GLOVES))
2152                 body_str = "äƼê";
2153         else if (IS_FLG(FLG_BOOTS))
2154                 body_str = "¥Ö¡¼¥Ä";
2155         else if (IS_FLG(FLG_FAVORITE))
2156                 body_str = "ÆÀ°ÕÉð´ï";
2157
2158         *buff = '\0';
2159         if (!before_n) 
2160                 strcat(buff, "Á´¤Æ¤Î");
2161         else for (i = 0; i < before_n && before_str[i]; i++)
2162                 strcat(buff, before_str[i]);
2163
2164         strcat(buff, body_str);
2165
2166         if (*str)
2167         {
2168                 if (*str == '^')
2169                 {
2170                         str++;
2171                         top = TRUE;
2172                 }
2173
2174                 strcat(buff, "¤Ç¡¢Ì¾Á°¤¬¡Ö");
2175                 strncat(buff, str, 80);
2176                 if (top)
2177                         strcat(buff, "¡×¤Ç»Ï¤Þ¤ë¤â¤Î");
2178                 else
2179                         strcat(buff, "¡×¤ò´Þ¤à¤â¤Î");
2180         }
2181
2182         if (insc)
2183         {
2184                 strncat(buff, format("¤Ë¡Ö%s¡×", insc), 80);
2185
2186                 if (strstr(insc, "%%all"))
2187                         strcat(buff, "(%%all¤ÏÁ´Ç½ÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
2188                 else if (strstr(insc, "%all"))
2189                         strcat(buff, "(%all¤ÏÁ´Ç½ÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
2190                 else if (strstr(insc, "%%"))
2191                         strcat(buff, "(%%¤ÏÄɲÃǽÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
2192                 else if (strstr(insc, "%"))
2193                         strcat(buff, "(%¤ÏÄɲÃǽÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
2194
2195                 strcat(buff, "¤È¹ï¤ó¤Ç");
2196         }
2197         else
2198                 strcat(buff, "¤ò");
2199
2200         if (act & DONT_AUTOPICK)
2201                 strcat(buff, "ÊüÃÖ¤¹¤ë¡£");
2202         else if (act & DO_AUTODESTROY)
2203                 strcat(buff, "Ç˲õ¤¹¤ë¡£");
2204         else if (act & DO_QUERY_AUTOPICK)
2205                 strcat(buff, "³Îǧ¤Î¸å¤Ë½¦¤¦¡£");
2206         else
2207                 strcat(buff, "½¦¤¦¡£");
2208
2209         if (act & DO_DISPLAY)
2210         {
2211                 if (act & DONT_AUTOPICK)
2212                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'N'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2213                 else if (act & DO_AUTODESTROY)
2214                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'K'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2215                 else
2216                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'M'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
2217         }
2218         else
2219                 strcat(buff, "Á´ÂΥޥåפˤÏɽ¼¨¤·¤Ê¤¤");
2220
2221 #else /* JP */
2222
2223         cptr before_str[20], after_str[20], which_str[20], whose_str[20], body_str;
2224         int before_n = 0, after_n = 0, which_n = 0, whose_n = 0;
2225
2226         body_str = "items";
2227
2228         /*** Collecting items ***/
2229         /*** Which can be absorbed into a slot as a bundle ***/
2230         if (IS_FLG(FLG_COLLECTING))
2231                 which_str[which_n++] = "can be absorbed into an existing inventory slot";
2232         
2233         /*** Unidentified ***/
2234         if (IS_FLG(FLG_UNIDENTIFIED))
2235                 before_str[before_n++] = "unidentified";
2236
2237         /*** Identified ***/
2238         if (IS_FLG(FLG_IDENTIFIED))
2239                 before_str[before_n++] = "identified";
2240
2241         /*** *Identified* ***/
2242         if (IS_FLG(FLG_STAR_IDENTIFIED))
2243                 before_str[before_n++] = "fully identified";
2244
2245         /*** Worthless items ***/
2246         if (IS_FLG(FLG_WORTHLESS))
2247         {
2248                 before_str[before_n++] = "worthless";
2249                 which_str[which_n++] = "can not be sold at stores";
2250         }
2251
2252         /*** Artifacto ***/
2253         if (IS_FLG(FLG_ARTIFACT))
2254         {
2255                 before_str[before_n++] = "artifact";
2256         }
2257
2258         /*** Ego ***/
2259         if (IS_FLG(FLG_EGO))
2260         {
2261                 before_str[before_n++] = "ego";
2262         }
2263
2264         /*** Nameless ***/
2265         if (IS_FLG(FLG_NAMELESS))
2266         {
2267                 body_str = "equipment";
2268                 which_str[which_n++] = "is neither ego-item nor artifact";
2269         }
2270
2271         /*** Unaware items ***/
2272         if (IS_FLG(FLG_UNAWARE))
2273         {
2274                 before_str[before_n++] = "unidentified";
2275                 whose_str[whose_n++] = "basic abilities are not known";
2276         }
2277
2278         /*** Dice boosted (weapon of slaying) ***/
2279         if (IS_FLG(FLG_BOOSTED))
2280         {
2281                 body_str = "weapons";
2282                 whose_str[whose_n++] = "damage dice is bigger than normal";
2283         }
2284
2285         /*** Weapons whose dd*ds is more than nn ***/
2286         if (IS_FLG(FLG_MORE_THAN))
2287         {
2288                 static char more_than_desc_str[] =
2289                         "maximum damage from dice is bigger than __";
2290                 body_str = "weapons";
2291                         
2292                 sprintf(more_than_desc_str + sizeof(more_than_desc_str) - 3,
2293                         "%d", entry->dice);
2294                 whose_str[whose_n++] = more_than_desc_str;
2295         }
2296
2297         /*** Items whose magical bonus is more than nn ***/
2298         if (IS_FLG(FLG_MORE_BONUS))
2299         {
2300                 static char more_bonus_desc_str[] =
2301                         "magical bonus is bigger than (+__)";
2302                         
2303                 sprintf(more_bonus_desc_str + sizeof(more_bonus_desc_str) - 4,
2304                         "%d)", entry->bonus);
2305                 whose_str[whose_n++] = more_bonus_desc_str;
2306         }
2307
2308         /*** Wanted monster's corpse/skeletons ***/
2309         if (IS_FLG(FLG_WANTED))
2310         {
2311                 body_str = "corpse or skeletons";
2312                 which_str[which_n++] = "is wanted at the Hunter's Office";
2313         }
2314
2315         /*** Human corpse/skeletons (for Daemon magic) ***/
2316         if (IS_FLG(FLG_HUMAN))
2317         {
2318                 before_str[before_n++] = "humanoid";
2319                 body_str = "corpse or skeletons";
2320                 which_str[which_n++] = "can be used for Daemon magic";
2321         }
2322
2323         /*** Unique monster's corpse/skeletons/statues ***/
2324         if (IS_FLG(FLG_UNIQUE))
2325         {
2326                 before_str[before_n++] = "unique monster's";
2327                 body_str = "corpse or skeletons";
2328         }
2329
2330         /*** Unreadable spellbooks ***/
2331         if (IS_FLG(FLG_UNREADABLE))
2332         {
2333                 body_str = "spellbooks";
2334                 after_str[after_n++] = "of different realms from yours";
2335         }
2336
2337         /*** First realm spellbooks ***/
2338         if (IS_FLG(FLG_REALM1))
2339         {
2340                 body_str = "spellbooks";
2341                 after_str[after_n++] = "of your first realm";
2342         }
2343
2344         /*** Second realm spellbooks ***/
2345         if (IS_FLG(FLG_REALM2))
2346         {
2347                 body_str = "spellbooks";
2348                 after_str[after_n++] = "of your second realm";
2349         }
2350
2351         /*** First rank spellbooks ***/
2352         if (IS_FLG(FLG_FIRST))
2353         {
2354                 before_str[before_n++] = "first one of four";
2355                 body_str = "spellbooks";
2356         }
2357
2358         /*** Second rank spellbooks ***/
2359         if (IS_FLG(FLG_SECOND))
2360         {
2361                 before_str[before_n++] = "second one of four";
2362                 body_str = "spellbooks";
2363         }
2364
2365         /*** Third rank spellbooks ***/
2366         if (IS_FLG(FLG_THIRD))
2367         {
2368                 before_str[before_n++] = "third one of four";
2369                 body_str = "spellbooks";
2370         }
2371
2372         /*** Fourth rank spellbooks ***/
2373         if (IS_FLG(FLG_FOURTH))
2374         {
2375                 before_str[before_n++] = "fourth one of four";
2376                 body_str = "spellbooks";
2377         }
2378
2379         /*** Items ***/
2380         if (IS_FLG(FLG_ITEMS))
2381                 ; /* Nothing to do */
2382         else if (IS_FLG(FLG_WEAPONS))
2383                 body_str = "weapons";
2384         else if (IS_FLG(FLG_ARMORS))
2385                 body_str = "armors";
2386         else if (IS_FLG(FLG_MISSILES))
2387                 body_str = "shots, arrows or crossbow bolts";
2388         else if (IS_FLG(FLG_DEVICES))
2389                 body_str = "scrolls, wands, staves or rods";
2390         else if (IS_FLG(FLG_LIGHTS))
2391                 body_str = "light sources";
2392         else if (IS_FLG(FLG_JUNKS))
2393                 body_str = "junk such as broken sticks";
2394         else if (IS_FLG(FLG_SPELLBOOKS))
2395                 body_str = "spellbooks";
2396         else if (IS_FLG(FLG_HAFTED))
2397                 body_str = "hafted weapons";
2398         else if (IS_FLG(FLG_SHIELDS))
2399                 body_str = "shields";
2400         else if (IS_FLG(FLG_BOWS))
2401                 body_str = "slings, bows or crossbows";
2402         else if (IS_FLG(FLG_RINGS))
2403                 body_str = "rings";
2404         else if (IS_FLG(FLG_AMULETS))
2405                 body_str = "amulets";
2406         else if (IS_FLG(FLG_SUITS))
2407                 body_str = "body armors";
2408         else if (IS_FLG(FLG_CLOAKS))
2409                 body_str = "cloaks";
2410         else if (IS_FLG(FLG_HELMS))
2411                 body_str = "helms or crowns";
2412         else if (IS_FLG(FLG_GLOVES))
2413                 body_str = "gloves";
2414         else if (IS_FLG(FLG_BOOTS))
2415                 body_str = "boots";
2416         else if (IS_FLG(FLG_FAVORITE))
2417                 body_str = "favorite weapons";
2418
2419         /* Prepare a string for item name */
2420         if (*str)
2421         {
2422                 if (*str == '^')
2423                 {
2424                         str++;
2425                         top = TRUE;
2426                         whose_str[whose_n++] = "name is beginning with \"";
2427                 }
2428                 else
2429                         which_str[which_n++] = "have \"";
2430         }
2431
2432
2433         /* Describe action flag */
2434         if (act & DONT_AUTOPICK)
2435                 strcpy(buff, "Leave on floor ");
2436         else if (act & DO_AUTODESTROY)
2437                 strcpy(buff, "Destroy ");
2438         else if (act & DO_QUERY_AUTOPICK)
2439                 strcpy(buff, "Ask to pick up ");
2440         else
2441                 strcpy(buff, "Pickup ");
2442
2443         /* Auto-insctiption */
2444         if (insc)
2445         {
2446                 strncat(buff, format("and inscribe \"%s\"", insc), 80);
2447
2448                 if (strstr(insc, "%all"))
2449                         strcat(buff, ", replacing %all with code string representing all abilities,");
2450                 else if (strstr(insc, "%"))
2451                         strcat(buff, ", replacing % with code string representing extra random abilities,");
2452
2453                 strcat(buff, " on ");
2454         }
2455
2456         /* Adjective */
2457         if (!before_n) 
2458                 strcat(buff, "all ");
2459         else for (i = 0; i < before_n && before_str[i]; i++)
2460         {
2461                 strcat(buff, before_str[i]);
2462                 strcat(buff, " ");
2463         }
2464
2465         /* Item class */
2466         strcat(buff, body_str);
2467
2468         /* of ... */
2469         for (i = 0; i < after_n && after_str[i]; i++)
2470         {
2471                 strcat(buff, " ");
2472                 strcat(buff, after_str[i]);
2473         }
2474
2475         /* which ... */
2476         for (i = 0; i < whose_n && whose_str[i]; i++)
2477         {
2478                 if (i == 0)
2479                         strcat(buff, " whose ");
2480                 else
2481                         strcat(buff, ", and ");
2482
2483                 strcat(buff, whose_str[i]);
2484         }
2485
2486         /* Item name ; whose name is beginning with "str" */
2487         if (*str && top)
2488         {
2489                 strcat(buff, str);
2490                 strcat(buff, "\"");
2491         }
2492
2493         /* whose ..., and which .... */
2494         if (whose_n && which_n)
2495                 strcat(buff, ", and ");
2496
2497         /* which ... */
2498         for (i = 0; i < which_n && which_str[i]; i++)
2499         {
2500                 if (i == 0)
2501                         strcat(buff, " which ");
2502                 else
2503                         strcat(buff, ", and ");
2504
2505                 strcat(buff, which_str[i]);
2506         }
2507
2508         /* Item name ; which have "str" as part of its name */
2509         if (*str && !top)
2510         {
2511                 strncat(buff, str, 80);
2512                 strcat(buff, "\" as part of its name");
2513         }
2514         strcat(buff, ".");
2515
2516         /* Describe whether it will be displayed on the full map or not */
2517         if (act & DO_DISPLAY)
2518         {
2519                 if (act & DONT_AUTOPICK)
2520                         strcat(buff, "  Display these items when you press the N key in the full 'M'ap.");
2521                 else if (act & DO_AUTODESTROY)
2522                         strcat(buff, "  Display these items when you press the K key in the full 'M'ap.");
2523                 else
2524                         strcat(buff, "  Display these items when you press the M key in the full 'M'ap.");
2525         }
2526         else
2527                 strcat(buff, " Not displayed in the full map.");
2528 #endif /* JP */
2529
2530 }
2531
2532
2533 /*
2534  * Read whole lines of a file to memory
2535  */
2536 static cptr *read_text_lines(cptr filename, bool user)
2537 {
2538         cptr *lines_list = NULL;
2539         FILE *fff;
2540
2541         int lines = 0;
2542         char buf[1024];
2543
2544         if (user)
2545         {
2546                 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
2547         }
2548         else
2549         {
2550                 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
2551         }
2552         
2553         /* Open the file */
2554         fff = my_fopen(buf, "r");
2555
2556         if (fff)
2557         {
2558                 /* Allocate list of pointers */
2559                 C_MAKE(lines_list, MAX_LINES, cptr);
2560
2561                 /* Parse it */
2562                 while (0 == my_fgets(fff, buf, sizeof(buf)))
2563                 {
2564                         lines_list[lines++] = string_make(buf);
2565                         if (lines >= MAX_LINES - 1) break;
2566                 }
2567                 if (lines == 0)
2568                         lines_list[0] = string_make("");
2569
2570                 my_fclose(fff);
2571         }
2572
2573         if (!fff) return NULL;
2574         return lines_list;
2575 }
2576
2577
2578 static cptr *read_pickpref_text_lines(int *filename_mode_p)
2579 {
2580         char buf[1024];
2581         cptr *lines_list;
2582
2583         *filename_mode_p = PT_WITH_PNAME;
2584         strcpy(buf, pickpref_filename(*filename_mode_p));
2585         lines_list = read_text_lines(buf, TRUE);
2586
2587         if (!lines_list)
2588         {
2589                 *filename_mode_p = PT_DEFAULT;
2590                 strcpy(buf, pickpref_filename(*filename_mode_p));
2591                 lines_list = read_text_lines(buf, TRUE);
2592         }
2593
2594         if (!lines_list)
2595         {
2596                 strcpy(buf, pickpref_filename(*filename_mode_p));
2597                 lines_list = read_text_lines(buf, FALSE);
2598         }
2599
2600         if (!lines_list)
2601         {
2602                 /* Allocate list of pointers */
2603                 C_MAKE(lines_list, MAX_LINES, cptr);
2604                 lines_list[0] = string_make("");
2605         }
2606         return lines_list;
2607 }
2608
2609
2610 /*
2611  * Write whole lines of memory to a file.
2612  */
2613 static bool write_text_lines(cptr filename, cptr *lines_list)
2614 {
2615         FILE *fff;
2616
2617         int lines = 0;
2618         char buf[1024];
2619
2620         /* Build the filename */
2621         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
2622         
2623         /* Open the file */
2624         fff = my_fopen(buf, "w");
2625         if (fff)
2626         {
2627                 for (lines = 0; lines_list[lines]; lines++)
2628                         my_fputs(fff, lines_list[lines], 1024);
2629
2630                 my_fclose(fff);
2631         }
2632
2633         if (!fff) return FALSE;
2634         return TRUE;
2635 }
2636
2637
2638 /*
2639  * Free memory of lines_list.
2640  */
2641 static void free_text_lines(cptr *lines_list)
2642 {
2643         int lines;
2644
2645         for (lines = 0; lines_list[lines]; lines++)
2646                 string_free(lines_list[lines]);
2647
2648         /* free list of pointers */
2649         C_FREE((char **)lines_list, MAX_LINES, char *);
2650 }
2651
2652
2653 /*
2654  * Delete or insert string
2655  */
2656 static void toggle_keyword(text_body_type *tb, int flg)
2657 {
2658         int by1, by2, bx1, bx2, y;
2659         bool add = TRUE;
2660         bool fixed = FALSE;
2661
2662         /* Select this line */
2663         if (!tb->mark)
2664         {
2665                 tb->my = tb->cy;
2666                 tb->mx = 0;
2667         }
2668
2669         if (tb->my < tb->cy)
2670         {
2671                 by1 = tb->my;
2672                 by2 = tb->cy;
2673                 bx1 = tb->mx;
2674                 bx2 = tb->cx;
2675         }
2676         else
2677         {
2678                 by2 = tb->my;
2679                 by1 = tb->cy;
2680                 bx2 = tb->mx;
2681                 bx1 = tb->cx;
2682         }
2683
2684         /* String on these lines are not really selected */  
2685         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2686         if (bx2 == 0 && by1 < by2) by2--;
2687
2688
2689         /* Set/Reset flag of each line */
2690         for (y = by1; y <= by2; y++)
2691         {
2692                 autopick_type an_entry, *entry = &an_entry;
2693
2694                 if (!autopick_new_entry(entry, tb->lines_list[y], !fixed)) continue;
2695
2696                 string_free(tb->lines_list[y]);
2697
2698                 if (!fixed)
2699                 {
2700                         /* Add? or Remove? */
2701                         if (!IS_FLG(flg)) add = TRUE;
2702                         else add = FALSE;
2703
2704                         /* No more change */
2705                         fixed = TRUE;
2706                 }
2707
2708                 /* You can use only one noun flag */
2709                 if (FLG_NOUN_BEGIN <= flg && flg <= FLG_NOUN_END)
2710                 {
2711                         int i;
2712                         for (i = FLG_NOUN_BEGIN; i <= FLG_NOUN_END; i++)
2713                                 REM_FLG(i);
2714                 }
2715                 
2716                 /* You can use only one identify state flag */
2717                 else if (FLG_UNAWARE <= flg && flg <= FLG_STAR_IDENTIFIED)
2718                 {
2719                         int i;
2720                         for (i = FLG_UNAWARE; i <= FLG_STAR_IDENTIFIED; i++)
2721                                 REM_FLG(i);
2722                 }
2723                 
2724                 /* You can use only one flag in artifact/ego/nameless */
2725                 else if (FLG_ARTIFACT <= flg && flg <= FLG_NAMELESS)
2726                 {
2727                         int i;
2728                         for (i = FLG_ARTIFACT; i <= FLG_NAMELESS; i++)
2729                                 REM_FLG(i);
2730                 }
2731                 
2732                 if (add) ADD_FLG(flg);
2733                 else REM_FLG(flg);
2734                 
2735                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2736                 
2737                 /* Now dirty */
2738                 tb->dirty_flags |= DIRTY_ALL;
2739
2740                 /* Text is changed */
2741                 tb->changed = TRUE;
2742         }
2743 }
2744
2745
2746 /*
2747  * Change command letter
2748  */
2749 static void toggle_command_letter(text_body_type *tb, byte flg)
2750 {
2751         autopick_type an_entry, *entry = &an_entry;
2752         int by1, by2, bx1, bx2, y;
2753         bool add = TRUE;
2754         bool fixed = FALSE;
2755
2756         /* Select this line */
2757         if (!tb->mark)
2758         {
2759                 tb->my = tb->cy;
2760                 tb->mx = 0;
2761         }
2762
2763         if (tb->my < tb->cy)
2764         {
2765                 by1 = tb->my;
2766                 by2 = tb->cy;
2767                 bx1 = tb->mx;
2768                 bx2 = tb->cx;
2769         }
2770         else
2771         {
2772                 by2 = tb->my;
2773                 by1 = tb->cy;
2774                 bx2 = tb->mx;
2775                 bx1 = tb->cx;
2776         }
2777
2778         /* String on these lines are not really selected */  
2779         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2780         if (bx2 == 0 && by1 < by2) by2--;
2781
2782         /* Set/Reset flag of each line */
2783         for (y = by1; y <= by2; y++)
2784         {
2785                 int wid = 0;
2786
2787                 if (!autopick_new_entry(entry, tb->lines_list[y], FALSE)) continue;
2788
2789                 string_free(tb->lines_list[y]);
2790
2791                 if (!fixed)
2792                 {
2793                         /* Add? or Remove? */
2794                         if (!(entry->action & flg)) add = TRUE;
2795                         else add = FALSE;
2796
2797                         /* No more change */
2798                         fixed = TRUE;
2799                 }
2800
2801                 /* Count number of letter (by negative number) */
2802                 if (entry->action & DONT_AUTOPICK) wid--;
2803                 else if (entry->action & DO_AUTODESTROY) wid--;
2804                 else if (entry->action & DO_QUERY_AUTOPICK) wid--;
2805                 if (!(entry->action & DO_DISPLAY)) wid--;
2806
2807                 /* Set/Reset the flag */
2808                 if (flg != DO_DISPLAY)
2809                 {
2810                         entry->action &= ~(DO_AUTOPICK | DONT_AUTOPICK | DO_AUTODESTROY | DO_QUERY_AUTOPICK);
2811                         if (add) entry->action |= flg;
2812                         else entry->action |= DO_AUTOPICK;
2813                 }
2814                 else
2815                 {
2816                         entry->action &= ~(DO_DISPLAY);
2817                         if (add) entry->action |= flg;
2818                 }
2819
2820                 /* Correct cursor location */
2821                 if (tb->cy == y)
2822                 {
2823                         if (entry->action & DONT_AUTOPICK) wid++;
2824                         else if (entry->action & DO_AUTODESTROY) wid++;
2825                         else if (entry->action & DO_QUERY_AUTOPICK) wid++;
2826                         if (!(entry->action & DO_DISPLAY)) wid++;
2827
2828                         if (wid > 0) tb->cx++;
2829                         if (wid < 0 && tb->cx > 0) tb->cx--;
2830                 }
2831                         
2832                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2833                         
2834                 /* Now dirty */
2835                 tb->dirty_flags |= DIRTY_ALL;
2836
2837                 /* Text is changed */
2838                 tb->changed = TRUE;
2839         }
2840 }
2841
2842 /*
2843  * Delete or insert string
2844  */
2845 static void add_keyword(text_body_type *tb, int flg)
2846 {
2847         int by1, by2, bx1, bx2, y;
2848
2849         /* Select this line */
2850         if (!tb->mark)
2851         {
2852                 tb->my = tb->cy;
2853                 tb->mx = 0;
2854         }
2855
2856         if (tb->my < tb->cy)
2857         {
2858                 by1 = tb->my;
2859                 by2 = tb->cy;
2860                 bx1 = tb->mx;
2861                 bx2 = tb->cx;
2862         }
2863         else
2864         {
2865                 by2 = tb->my;
2866                 by1 = tb->cy;
2867                 bx2 = tb->mx;
2868                 bx1 = tb->cx;
2869         }
2870
2871         /* String on these lines are not really selected */  
2872         if (tb->lines_list[by1][bx1] == '\0' && by1 < tb->cy) by1++;
2873         if (bx2 == 0 && by1 < by2) by2--;
2874
2875         /* Set/Reset flag of each line */
2876         for (y = by1; y <= by2; y++)
2877         {
2878                 autopick_type an_entry, *entry = &an_entry;
2879
2880                 if (!autopick_new_entry(entry, tb->lines_list[y], FALSE)) continue;
2881
2882                 /* There is the flag already */
2883                 if (IS_FLG(flg))
2884                 {
2885                         /* Free memory for the entry */
2886                         autopick_free_entry(entry);
2887                         
2888                         continue;
2889                 }
2890                 
2891                 string_free(tb->lines_list[y]);
2892                 
2893                 /* Remove all noun flag */
2894                 if (FLG_NOUN_BEGIN <= flg && flg <= FLG_NOUN_END)
2895                 {
2896                         int i;
2897                         for (i = FLG_NOUN_BEGIN; i <= FLG_NOUN_END; i++)
2898                                 REM_FLG(i);
2899                 }
2900                 
2901                 ADD_FLG(flg);
2902                 
2903                 tb->lines_list[y] = autopick_line_from_entry_kill(entry);
2904
2905                 /* Now dirty */
2906                 tb->dirty_flags |= DIRTY_ALL;
2907
2908                 /* Text is changed */
2909                 tb->changed = TRUE;
2910         }
2911 }
2912
2913
2914 /*
2915  * Check if this line is expression or not.
2916  * And update it if it is.
2917  */
2918 static void check_expression_line(text_body_type *tb, int y)
2919 {
2920         cptr s = tb->lines_list[y];
2921
2922         if ((s[0] == '?' && s[1] == ':') ||
2923             (tb->states[y] & LSTAT_BYPASS))
2924         {
2925                 /* Expressions need re-evaluation */
2926                 tb->dirty_flags |= DIRTY_EXPRESSION;
2927         }
2928 }
2929
2930
2931 /*
2932  * Insert return code and split the line
2933  */
2934 static bool insert_return_code(text_body_type *tb)
2935 {
2936         char buf[MAX_LINELEN];
2937         int i, j, k;
2938
2939         for (k = 0; tb->lines_list[k]; k++)
2940                 /* count number of lines */ ;
2941
2942         if (k >= MAX_LINES - 2) return FALSE;
2943         k--;
2944
2945         /* Move down lines */
2946         for (; tb->cy < k; k--)
2947         {
2948                 tb->lines_list[k+1] = tb->lines_list[k];
2949                 tb->states[k+1] = tb->states[k];
2950         }
2951
2952         /* Split current line */
2953         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
2954         {
2955 #ifdef JP
2956                 if (iskanji(tb->lines_list[tb->cy][i]))
2957                         buf[j++] = tb->lines_list[tb->cy][i++];
2958 #endif
2959                 buf[j++] = tb->lines_list[tb->cy][i];
2960         }
2961         buf[j] = '\0';
2962         tb->lines_list[tb->cy+1] = string_make(&tb->lines_list[tb->cy][i]);
2963         string_free(tb->lines_list[tb->cy]);
2964         tb->lines_list[tb->cy] = string_make(buf);
2965
2966         /* Expressions need re-evaluation */
2967         tb->dirty_flags |= DIRTY_EXPRESSION;
2968
2969         /* Text is changed */
2970         tb->changed = TRUE;
2971
2972         return TRUE;
2973 }
2974
2975
2976 /*
2977  * Choose an item and get auto-picker entry from it.
2978  */
2979 static object_type *choose_object(cptr q, cptr s)
2980 {
2981         int item;
2982
2983         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR | USE_EQUIP))) return NULL;
2984
2985         /* Get the item (in the pack) */
2986         if (item >= 0) return &inventory[item];
2987
2988         /* Get the item (on the floor) */
2989         else return &o_list[0 - item];
2990 }
2991
2992
2993 /*
2994  * Choose an item and get auto-picker entry from it.
2995  */
2996 static bool entry_from_choosed_object(autopick_type *entry)
2997 {
2998         object_type *o_ptr;
2999         cptr q, s;
3000
3001         /* Get an item */
3002 #ifdef JP
3003         q = "¤É¤Î¥¢¥¤¥Æ¥à¤òÅÐÏ¿¤·¤Þ¤¹¤«? ";
3004         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
3005 #else
3006         q = "Enter which item? ";
3007         s = "You have nothing to enter.";
3008 #endif
3009         o_ptr = choose_object(q, s);
3010         if (!o_ptr) return FALSE;
3011
3012         autopick_entry_from_object(entry, o_ptr);
3013         return TRUE;
3014 }
3015
3016
3017 /*
3018  * Choose an item for search
3019  */
3020 static byte get_object_for_search(object_type **o_handle, cptr *search_strp)
3021 {
3022         char buf[MAX_NLEN+20];
3023         object_type *o_ptr;
3024         cptr q, s;
3025
3026         /* Get an item */
3027 #ifdef JP
3028         q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò¸¡º÷¤·¤Þ¤¹¤«? ";
3029         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
3030 #else
3031         q = "Enter which item? ";
3032         s = "You have nothing to enter.";
3033 #endif
3034         o_ptr = choose_object(q, s);
3035         if (!o_ptr) return 0;
3036
3037         *o_handle = o_ptr;
3038
3039         string_free(*search_strp);
3040         object_desc(buf, *o_handle, FALSE, 3);
3041         *search_strp = string_make(format("<%s>", buf));
3042         return 1;
3043 }
3044
3045
3046 /*
3047  * Prepare for search by destroyed object
3048  */
3049 static byte get_destroyed_object_for_search(object_type **o_handle, cptr *search_strp)
3050 {
3051         char buf[MAX_NLEN+20];
3052
3053         if (!autopick_last_destroyed_object.k_idx) return 0;
3054
3055         *o_handle = &autopick_last_destroyed_object;
3056
3057         string_free(*search_strp);
3058         object_desc(buf, *o_handle, FALSE, 3);
3059         *search_strp = string_make(format("<%s>", buf));
3060         return 1;
3061 }
3062
3063
3064 /*
3065  * Choose an item or string for search
3066  */
3067 static byte get_string_for_search(object_type **o_handle, cptr *search_strp)
3068 {
3069         int pos = 0;
3070         char buf[MAX_NLEN+20];
3071
3072 #ifdef JP
3073         int k_flag[MAX_NLEN+20];
3074         char prompt[] = "¸¡º÷(^I:»ý¤Áʪ ^L:Ç˲õ¤µ¤ì¤¿Êª): ";
3075 #else
3076         char prompt[] = "Search key(^I:inven ^L:destroyed): ";
3077 #endif
3078         int col = sizeof(prompt) - 1;
3079
3080         if (*search_strp) strcpy(buf, *search_strp);
3081         else buf[0] = '\0';
3082
3083         /* Display prompt */
3084         prt(prompt, 0, 0);
3085
3086         /* Display the default answer */
3087         Term_erase(col, 0, 255);
3088         Term_putstr(col, 0, -1, TERM_YELLOW, buf);
3089
3090         /* Process input */
3091         while (1)
3092         {
3093                 bool back = FALSE;
3094                 int key;
3095                 size_t trig_len;
3096
3097                 /* Place cursor */
3098                 Term_gotoxy(col + pos, 0);
3099
3100                 /* Get a key */
3101                 key = inkey();
3102
3103                 /* Count length of macro trigger which induced this key */
3104                 trig_len = strlen(inkey_macro_trigger_string);
3105
3106                 /* HACK -- ignore macro defined on ASCII keys */
3107                 if (trig_len == 1)
3108                 {
3109                         /* Get original key */
3110                         key = inkey_macro_trigger_string[0];
3111
3112                         /* Kill further macro expansion */
3113                         flush();
3114                 }
3115
3116                 /* Delete key */
3117                 if (key == 0x7F) key = KTRL('d');
3118
3119
3120                 /* Analyze the key */
3121                 switch (key)
3122                 {
3123                 case ESCAPE:
3124                         return 0;
3125
3126                 case KTRL('r'):
3127                         back = TRUE;
3128                         /* Fall through */
3129
3130                 case '\n':
3131                 case '\r':
3132                 case KTRL('s'):
3133                         if (!pos && *o_handle) return (back ? -1 : 1);
3134                         string_free(*search_strp);
3135                         *search_strp = string_make(buf);
3136                         *o_handle = NULL;
3137                         return (back ? -1 : 1);
3138
3139                 case KTRL('i'):
3140                         /* Get an item */
3141                         return get_object_for_search(o_handle, search_strp);
3142
3143                 case KTRL('l'):
3144                         /* Prepare string for destroyed object if there is one. */
3145                         if (get_destroyed_object_for_search(o_handle, search_strp))
3146                                 return 1;
3147                         break;
3148
3149                 case 0x7F:
3150                 case '\010':
3151 #ifdef JP
3152                         if (pos > 0)
3153                         {
3154                                 pos--;
3155                                 if (k_flag[pos]) pos--;
3156                         }
3157 #else
3158                         if (pos > 0) pos--;
3159 #endif
3160                         break;
3161
3162                 default:
3163 #ifdef JP
3164                         if (iskanji(key))
3165                         {
3166                                 int next;
3167
3168                                 inkey_base = TRUE;
3169                                 next = inkey ();
3170                                 if (pos + 1 < MAX_NLEN)
3171                                 {
3172                                         buf[pos++] = key;
3173                                         buf[pos] = next;
3174                                         k_flag[pos++] = 1;
3175                                 }
3176                                 else bell();
3177                         }
3178                         else if (pos < MAX_NLEN && isprint(key))
3179                         {
3180                                 buf[pos] = key;
3181                                 k_flag[pos++] = 0;
3182                         }
3183                         else bell();
3184 #else
3185                         if (pos < MAX_NLEN && isprint(key)) buf[pos++] = key;
3186                         else bell();
3187 #endif
3188                         break;
3189                 }
3190
3191                 /* Terminate */
3192                 buf[pos] = '\0';
3193
3194                 /* Update the entry */
3195                 Term_erase(col, 0, 255);
3196                 Term_putstr(col, 0, -1, TERM_WHITE, buf);
3197         }
3198
3199         /* Never reached */
3200 }
3201
3202
3203 /*
3204  * Search next line matches for o_ptr
3205  */
3206 static bool search_for_object(text_body_type *tb, object_type *o_ptr, bool forward)
3207 {
3208         int i;
3209         autopick_type an_entry, *entry = &an_entry;
3210         char o_name[MAX_NLEN];
3211
3212         object_desc(o_name, o_ptr, FALSE, 3);
3213
3214         /* Force to be lower case string */
3215         for (i = 0; o_name[i]; i++)
3216         {
3217 #ifdef JP
3218                 if (iskanji(o_name[i]))
3219                         i++;
3220                 else
3221 #endif
3222                 if (isupper(o_name[i]))
3223                         o_name[i] = tolower(o_name[i]);
3224         }
3225         
3226         i = tb->cy;
3227
3228         while (1)
3229         {
3230                 if (forward)
3231                 {
3232                         if (!tb->lines_list[++i]) break;
3233                 }
3234                 else
3235                 {
3236                         if (--i < 0) break;
3237                 }
3238
3239                 /* Ignore bypassed lines */
3240                 if (tb->states[i] & LSTAT_BYPASS) continue;
3241
3242                 if (!autopick_new_entry(entry, tb->lines_list[i], FALSE)) continue;
3243
3244                 if (is_autopick_aux(o_ptr, entry, o_name))
3245                 {
3246                         tb->cx = 0;
3247                         tb->cy = i;
3248                         return TRUE;
3249                 }
3250         }
3251
3252         return FALSE;
3253 }
3254
3255
3256 /*
3257  * Search next line matches to the string
3258  */
3259 static bool search_for_string(text_body_type *tb, cptr search_str, bool forward)
3260 {
3261         int i = tb->cy;
3262
3263         while (1)
3264         {
3265                 cptr pos;
3266
3267                 if (forward)
3268                 {
3269                         if (!tb->lines_list[++i]) break;
3270                 }
3271                 else
3272                 {
3273                         if (--i < 0) break;
3274                 }
3275
3276                 /* Ignore bypassed lines */
3277                 if (tb->states[i] & LSTAT_BYPASS) continue;
3278
3279 #ifdef JP
3280                 pos = strstr_j(tb->lines_list[i], search_str);
3281 #else
3282                 pos = strstr(tb->lines_list[i], search_str);
3283 #endif
3284                 if (pos)
3285                 {
3286                         tb->cx = (int)(pos - tb->lines_list[i]);
3287                         tb->cy = i;
3288                         return TRUE;
3289                 }
3290         }
3291
3292         return FALSE;
3293 }
3294
3295
3296
3297
3298 /*
3299  * Editor command id's
3300  */
3301 #define EC_QUIT                1 
3302 #define EC_SAVEQUIT            2     
3303 #define EC_REVERT              3 
3304 #define EC_HELP                4 
3305 #define EC_RETURN              5        
3306 #define EC_LEFT                6 
3307 #define EC_DOWN                7 
3308 #define EC_UP                  8 
3309 #define EC_RIGHT               9 
3310 #define EC_BOL                 10
3311 #define EC_EOL                 11
3312 #define EC_PGUP                12
3313 #define EC_PGDOWN              13
3314 #define EC_TOP                 14
3315 #define EC_BOTTOM              15
3316 #define EC_CUT                 16
3317 #define EC_COPY                17
3318 #define EC_PASTE               18
3319 #define EC_BLOCK               19
3320 #define EC_KILL_LINE           20
3321 #define EC_DELETE_CHAR         21
3322 #define EC_BACKSPACE           22
3323 #define EC_SEARCH_STR          23
3324 #define EC_SEARCH_FORW         24
3325 #define EC_SEARCH_BACK         25
3326 #define EC_SEARCH_OBJ          26
3327 #define EC_SEARCH_DESTROYED    27
3328 #define EC_INSERT_OBJECT       28
3329 #define EC_INSERT_DESTROYED    29
3330 #define EC_INSERT_BLOCK        30
3331 #define EC_INSERT_MACRO        31
3332 #define EC_INSERT_KEYMAP       32
3333 #define EC_CL_AUTOPICK         33
3334 #define EC_CL_DESTROY          34
3335 #define EC_CL_LEAVE            35
3336 #define EC_CL_QUERY            36
3337 #define EC_CL_NO_DISP          37
3338 #define EC_OK_COLLECTING       38
3339 #define EC_IK_UNAWARE          39
3340 #define EC_IK_UNIDENTIFIED     40
3341 #define EC_IK_IDENTIFIED       41
3342 #define EC_IK_STAR_IDENTIFIED  42
3343 #define EC_OK_BOOSTED          43
3344 #define EC_OK_MORE_THAN        44
3345 #define EC_OK_MORE_BONUS       45
3346 #define EC_OK_WORTHLESS        46
3347 #define EC_OK_ARTIFACT         47
3348 #define EC_OK_EGO              48
3349 #define EC_OK_NAMELESS         49
3350 #define EC_OK_WANTED           50
3351 #define EC_OK_UNIQUE           51
3352 #define EC_OK_HUMAN            52
3353 #define EC_OK_UNREADABLE       53
3354 #define EC_OK_REALM1           54
3355 #define EC_OK_REALM2           55
3356 #define EC_OK_FIRST            56
3357 #define EC_OK_SECOND           57
3358 #define EC_OK_THIRD            58
3359 #define EC_OK_FOURTH           59
3360 #define EC_KK_WEAPONS          60
3361 #define EC_KK_FAVORITE         61
3362 #define EC_KK_ARMORS           62
3363 #define EC_KK_MISSILES         63
3364 #define EC_KK_DEVICES          64
3365 #define EC_KK_LIGHTS           65
3366 #define EC_KK_JUNKS            66
3367 #define EC_KK_SPELLBOOKS       67
3368 #define EC_KK_SHIELDS          68
3369 #define EC_KK_BOWS             69
3370 #define EC_KK_RINGS            70
3371 #define EC_KK_AMULETS          71
3372 #define EC_KK_SUITS            72
3373 #define EC_KK_CLOAKS           73
3374 #define EC_KK_HELMS            74
3375 #define EC_KK_GLOVES           75
3376 #define EC_KK_BOOTS            76
3377
3378
3379 /* Manu names */
3380 #ifdef JP
3381
3382 #define MN_QUIT "¥»¡¼¥Ö̵¤·¤Ç½ªÎ»" 
3383 #define MN_SAVEQUIT "¥»¡¼¥Ö¤·¤Æ½ªÎ»" 
3384 #define MN_REVERT "Á´¤Æ¤ÎÊѹ¹¤òÇË´þ" 
3385 #define MN_HELP "¥Ø¥ë¥×" 
3386
3387 #define MN_MOVE "¥«¡¼¥½¥ë°ÜÆ°" 
3388 #define MN_LEFT "º¸" 
3389 #define MN_DOWN "²¼" 
3390 #define MN_UP "¾å" 
3391 #define MN_RIGHT "±¦" 
3392 #define MN_BOL "¹Ô¤ÎÀèƬ" 
3393 #define MN_EOL "¹Ô¤Î½ªÃ¼" 
3394 #define MN_PGUP "¾å¤Î¥Ú¡¼¥¸" 
3395 #define MN_PGDOWN "²¼¤Î¥Ú¡¼¥¸" 
3396 #define MN_TOP "1¹ÔÌܤذÜÆ°" 
3397 #define MN_BOTTOM "ºÇ²¼¹Ô¤Ø°ÜÆ°" 
3398
3399 #define MN_EDIT "ÊÔ½¸" 
3400 #define MN_CUT "¥«¥Ã¥È" 
3401 #define MN_COPY "¥³¥Ô¡¼" 
3402 #define MN_PASTE "¥Ú¡¼¥¹¥È" 
3403 #define MN_BLOCK "ÁªÂòÈϰϤλØÄê" 
3404 #define MN_KILL_LINE "¹Ô¤Î»Ä¤ê¤òºï½ü" 
3405 #define MN_DELETE_CHAR "1ʸ»úºï½ü" 
3406 #define MN_BACKSPACE "¥Ð¥Ã¥¯¥¹¥Ú¡¼¥¹" 
3407 #define MN_RETURN "²þ¹Ô" 
3408 #define MN_RETURN "²þ¹Ô" 
3409
3410 #define MN_SEARCH "¸¡º÷" 
3411 #define MN_SEARCH_STR "ʸ»úÎó¤Ç¸¡º÷" 
3412 #define MN_SEARCH_FORW "Á°Êý¤ØºÆ¸¡º÷" 
3413 #define MN_SEARCH_BACK "¸åÊý¤ØºÆ¸¡º÷" 
3414 #define MN_SEARCH_OBJ "¥¢¥¤¥Æ¥à¤òÁªÂò¤·¤Æ¸¡º÷" 
3415 #define MN_SEARCH_DESTROYED "¼«Æ°Ç˲õ¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤Ç¸¡º÷" 
3416
3417 #define MN_INSERT "¿§¡¹ÁÞÆþ" 
3418 #define MN_INSERT_OBJECT "ÁªÂò¤·¤¿¥¢¥¤¥Æ¥à¤Î̾Á°¤òÁÞÆþ" 
3419 #define MN_INSERT_DESTROYED "¼«Æ°Ç˲õ¤µ¤ì¤¿¥¢¥¤¥Æ¥à¤Î̾Á°¤òÁÞÆþ" 
3420 #define MN_INSERT_BLOCK "¾ò·ïʬ´ô¥Ö¥í¥Ã¥¯¤ÎÎã¤òÁÞÆþ" 
3421 #define MN_INSERT_MACRO "¥Þ¥¯¥íÄêµÁ¤òÁÞÆþ" 
3422 #define MN_INSERT_KEYMAP "¥­¡¼¥Þ¥Ã¥×ÄêµÁ¤òÁÞÆþ" 
3423
3424 #define MN_COMMAND_LETTER "½¦¤¤/Ç˲õ/ÊüÃÖ¤ÎÁªÂò" 
3425 #define MN_CL_AUTOPICK "¡Ö ¡× (¼«Æ°½¦¤¤)" 
3426 #define MN_CL_DESTROY "¡Ö!¡× (¼«Æ°Ç˲õ)" 
3427 #define MN_CL_LEAVE "¡Ö~¡× (ÊüÃÖ)" 
3428 #define MN_CL_QUERY "¡Ö;¡× (³Îǧ¤·¤Æ½¦¤¦)" 
3429 #define MN_CL_NO_DISP "¡Ö(¡× (¥Þ¥Ã¥×¥³¥Þ¥ó¥É¤Çɽ¼¨¤·¤Ê¤¤)" 
3430
3431 #define MN_ADJECTIVE_GEN "·ÁÍÆ»ì(°ìÈÌ)¤ÎÁªÂò" 
3432
3433 #define MN_ADJECTIVE_SPECIAL "·ÁÍÆ»ì(Æüì)¤ÎÁªÂò" 
3434 #define MN_BOOSTED "¥À¥¤¥¹Ìܤΰ㤦 (Éð´ï)" 
3435 #define MN_MORE_THAN "¥À¥¤¥¹ÌÜ # °Ê¾å¤Î (Éð´ï)" 
3436 #define MN_MORE_BONUS "½¤ÀµÃÍ # °Ê¾å¤Î (»ØÎØÅù)" 
3437 #define MN_ARTIFACT "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È (ÁõÈ÷)" 
3438 #define MN_EGO "¥¨¥´ (ÁõÈ÷)" 
3439 #define MN_NAMELESS "̵ÌäΠ(ÁõÈ÷)" 
3440 #define MN_WANTED "¾Þ¶â¼ó¤Î (»àÂÎ)" 
3441 #define MN_UNIQUE "¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼¤Î (»àÂÎ)" 
3442 #define MN_HUMAN "¿Í´Ö¤Î (»àÂÎ)" 
3443 #define MN_UNREADABLE "Æɤá¤Ê¤¤ (ËâË¡½ñ)" 
3444 #define MN_REALM1 "Âè°ìÎΰè¤Î (ËâË¡½ñ)" 
3445 #define MN_REALM2 "ÂèÆóÎΰè¤Î (ËâË¡½ñ)" 
3446 #define MN_FIRST "1ºýÌܤΠ(ËâË¡½ñ)" 
3447 #define MN_SECOND "2ºýÌܤΠ(ËâË¡½ñ)" 
3448 #define MN_THIRD "3ºýÌܤΠ(ËâË¡½ñ)" 
3449 #define MN_FOURTH "4ºýÌܤΠ(ËâË¡½ñ)" 
3450
3451 #define MN_NOUN "̾»ì¤ÎÁªÂò" 
3452
3453 #else
3454
3455 #define MN_QUIT "Quit without save" 
3456 #define MN_SAVEQUIT "Save & Quit" 
3457 #define MN_REVERT "Revert all changes" 
3458 #define MN_HELP "Help" 
3459
3460 #define MN_MOVE "Move cursor" 
3461 #define MN_LEFT "Left" 
3462 #define MN_DOWN "Down" 
3463 #define MN_UP "Up" 
3464 #define MN_RIGHT "Right" 
3465 #define MN_BOL "Beggining of line" 
3466 #define MN_EOL "End of line" 
3467 #define MN_PGUP "Page up" 
3468 #define MN_PGDOWN "Page down" 
3469 #define MN_TOP "Top" 
3470 #define MN_BOTTOM "Bottom" 
3471
3472 #define MN_EDIT "Edit" 
3473 #define MN_CUT "Cut" 
3474 #define MN_COPY "Copy" 
3475 #define MN_PASTE "Paste" 
3476 #define MN_BLOCK "Select block" 
3477 #define MN_KILL_LINE "Kill rest of line" 
3478 #define MN_DELETE_CHAR "Delete character" 
3479 #define MN_BACKSPACE "Backspace" 
3480 #define MN_RETURN "Return" 
3481 #define MN_RETURN "Return" 
3482
3483 #define MN_SEARCH "Search" 
3484 #define MN_SEARCH_STR "Search by string" 
3485 #define MN_SEARCH_FORW "Search forward" 
3486 #define MN_SEARCH_BACK "Search backward" 
3487 #define MN_SEARCH_OBJ "Search by inventory object" 
3488 #define MN_SEARCH_DESTROYED "Search by destroyed object" 
3489
3490 #define MN_INSERT "Insert..." 
3491 #define MN_INSERT_OBJECT "Insert name of choosen object" 
3492 #define MN_INSERT_DESTROYED "Insert name of destroyed object" 
3493 #define MN_INSERT_BLOCK "Insert conditional block" 
3494 #define MN_INSERT_MACRO "Insert a macro definition" 
3495 #define MN_INSERT_KEYMAP "Insert a keymap definition" 
3496
3497 #define MN_COMMAND_LETTER "Command letter" 
3498 #define MN_CL_AUTOPICK "' ' (Auto pick)" 
3499 #define MN_CL_DESTROY "'!' (Auto destroy)" 
3500 #define MN_CL_LEAVE "'~' (Leave it on the floor)" 
3501 #define MN_CL_QUERY "';' (Query to pick up)" 
3502 #define MN_CL_NO_DISP "'(' (No display on the large map)" 
3503
3504 #define MN_ADJECTIVE_GEN "Adjective (general)" 
3505
3506 #define MN_ADJECTIVE_SPECIAL "Adjective (special)" 
3507 #define MN_BOOSTED "dice boosted (weapons)" 
3508 #define MN_MORE_THAN "more than # dice (weapons)" 
3509 #define MN_MORE_BONUS "more bonus than # (rings etc.)" 
3510 #define MN_ARTIFACT "artifact (equipments)" 
3511 #define MN_EGO "ego (equipments)" 
3512 #define MN_NAMELESS "nameless (equipments)" 
3513 #define MN_WANTED "wanted (corpse)" 
3514 #define MN_UNIQUE "unique (corpse)" 
3515 #define MN_HUMAN "human (corpse)" 
3516 #define MN_UNREADABLE "unreadable (spellbooks)" 
3517 #define MN_REALM1 "realm1 (spellbooks)" 
3518 #define MN_REALM2 "realm2 (spellbooks)" 
3519 #define MN_FIRST "first (spellbooks)" 
3520 #define MN_SECOND "second (spellbooks)" 
3521 #define MN_THIRD "third (spellbooks)" 
3522 #define MN_FOURTH "fourth (spellbooks)" 
3523
3524 #define MN_NOUN "Keywords (noun)" 
3525
3526 #endif
3527
3528
3529 typedef struct {
3530         cptr name;
3531         int level;
3532         int key;
3533         int com_id;
3534 } command_menu_type;
3535
3536
3537 command_menu_type menu_data[] =
3538 {
3539         {MN_HELP, 0, -1, EC_HELP},
3540         {MN_QUIT, 0, KTRL('q'), EC_QUIT}, 
3541         {MN_SAVEQUIT, 0, KTRL('w'), EC_SAVEQUIT}, 
3542         {MN_REVERT, 0, KTRL('z'), EC_REVERT},
3543
3544         {MN_EDIT, 0, -1, -1},
3545         {MN_CUT, 1, KTRL('x'), EC_CUT},
3546         {MN_COPY, 1, KTRL('c'), EC_COPY},
3547         {MN_PASTE, 1, KTRL('v'), EC_PASTE},
3548         {MN_BLOCK, 1, KTRL('g'), EC_BLOCK},
3549         {MN_KILL_LINE, 1, KTRL('k'), EC_KILL_LINE},
3550         {MN_DELETE_CHAR, 1, KTRL('d'), EC_DELETE_CHAR},
3551         {MN_BACKSPACE, 1, KTRL('h'), EC_BACKSPACE},
3552         {MN_RETURN, 1, KTRL('j'), EC_RETURN},
3553         {MN_RETURN, 1, KTRL('m'), EC_RETURN},
3554
3555         {MN_SEARCH, 0, -1, -1},
3556         {MN_SEARCH_STR, 1, KTRL('s'), EC_SEARCH_STR},
3557         {MN_SEARCH_FORW, 1, -1, EC_SEARCH_FORW},
3558         {MN_SEARCH_BACK, 1, KTRL('r'), EC_SEARCH_BACK},
3559         {MN_SEARCH_OBJ, 1, KTRL('t'), EC_SEARCH_OBJ},
3560         {MN_SEARCH_DESTROYED, 1, -1, EC_SEARCH_DESTROYED},
3561
3562         {MN_MOVE, 0, -1, -1},
3563         {MN_LEFT, 1, KTRL('b'), EC_LEFT},
3564         {MN_DOWN, 1, KTRL('n'), EC_DOWN},
3565         {MN_UP, 1, KTRL('p'), EC_UP},
3566         {MN_RIGHT, 1, KTRL('f'), EC_RIGHT},
3567         {MN_BOL, 1, KTRL('a'), EC_BOL},
3568         {MN_EOL, 1, KTRL('e'), EC_EOL},
3569         {MN_PGUP, 1, KTRL('o'), EC_PGUP},
3570         {MN_PGDOWN, 1, KTRL('l'), EC_PGDOWN},
3571         {MN_TOP, 1, KTRL('y'), EC_TOP},
3572         {MN_BOTTOM, 1, KTRL('u'), EC_BOTTOM},
3573
3574         {MN_INSERT, 0, -1, -1},
3575         {MN_INSERT_OBJECT, 1, KTRL('i'), EC_INSERT_OBJECT},
3576         {MN_INSERT_DESTROYED, 1, -1, EC_INSERT_DESTROYED},
3577         {MN_INSERT_BLOCK, 1, -1, EC_INSERT_BLOCK},
3578         {MN_INSERT_MACRO, 1, -1, EC_INSERT_MACRO},
3579         {MN_INSERT_KEYMAP, 1, -1, EC_INSERT_KEYMAP},
3580
3581         {MN_ADJECTIVE_GEN, 0, -1, -1},
3582         {KEY_UNAWARE, 1, -1, EC_IK_UNAWARE},
3583         {KEY_UNIDENTIFIED, 1, -1, EC_IK_UNIDENTIFIED},
3584         {KEY_IDENTIFIED, 1, -1, EC_IK_IDENTIFIED},
3585         {KEY_STAR_IDENTIFIED, 1, -1, EC_IK_STAR_IDENTIFIED},
3586         {KEY_COLLECTING, 1, -1, EC_OK_COLLECTING},
3587         {KEY_WORTHLESS, 1, -1, EC_OK_WORTHLESS},
3588
3589         {MN_ADJECTIVE_SPECIAL, 0, -1, -1},
3590         {MN_BOOSTED, 1, -1, EC_OK_BOOSTED},
3591         {MN_MORE_THAN, 1, -1, EC_OK_MORE_THAN},
3592         {MN_MORE_BONUS, 1, -1, EC_OK_MORE_BONUS},
3593         {MN_ARTIFACT, 1, -1, EC_OK_ARTIFACT},
3594         {MN_EGO, 1, -1, EC_OK_EGO},
3595         {MN_NAMELESS, 1, -1, EC_OK_NAMELESS},
3596         {MN_WANTED, 1, -1, EC_OK_WANTED},
3597         {MN_UNIQUE, 1, -1, EC_OK_UNIQUE},
3598         {MN_HUMAN, 1, -1, EC_OK_HUMAN},
3599         {MN_UNREADABLE, 1, -1, EC_OK_UNREADABLE},
3600         {MN_REALM1, 1, -1, EC_OK_REALM1},
3601         {MN_REALM2, 1, -1, EC_OK_REALM2},
3602         {MN_FIRST, 1, -1, EC_OK_FIRST},
3603         {MN_SECOND, 1, -1, EC_OK_SECOND},
3604         {MN_THIRD, 1, -1, EC_OK_THIRD},
3605         {MN_FOURTH, 1, -1, EC_OK_FOURTH},
3606
3607         {MN_NOUN, 0, -1, -1},
3608         {KEY_WEAPONS, 1, -1, EC_KK_WEAPONS},
3609         {KEY_FAVORITE, 1, -1, EC_KK_FAVORITE},
3610         {KEY_ARMORS, 1, -1, EC_KK_ARMORS},
3611         {KEY_MISSILES, 1, -1, EC_KK_MISSILES},
3612         {KEY_DEVICES, 1, -1, EC_KK_DEVICES},
3613         {KEY_LIGHTS, 1, -1, EC_KK_LIGHTS},
3614         {KEY_JUNKS, 1, -1, EC_KK_JUNKS},
3615         {KEY_SPELLBOOKS, 1, -1, EC_KK_SPELLBOOKS},
3616         {KEY_SHIELDS, 1, -1, EC_KK_SHIELDS},
3617         {KEY_BOWS, 1, -1, EC_KK_BOWS},
3618         {KEY_RINGS, 1, -1, EC_KK_RINGS},
3619         {KEY_AMULETS, 1, -1, EC_KK_AMULETS},
3620         {KEY_SUITS, 1, -1, EC_KK_SUITS},
3621         {KEY_CLOAKS, 1, -1, EC_KK_CLOAKS},
3622         {KEY_HELMS, 1, -1, EC_KK_HELMS},
3623         {KEY_GLOVES, 1, -1, EC_KK_GLOVES},
3624         {KEY_BOOTS, 1, -1, EC_KK_BOOTS},
3625
3626         {MN_COMMAND_LETTER, 0, -1, -1},
3627         {MN_CL_AUTOPICK, 1, -1, EC_CL_AUTOPICK},
3628         {MN_CL_DESTROY, 1, -1, EC_CL_DESTROY},
3629         {MN_CL_LEAVE, 1, -1, EC_CL_LEAVE},
3630         {MN_CL_QUERY, 1, -1, EC_CL_QUERY},
3631         {MN_CL_NO_DISP, 1, -1, EC_CL_NO_DISP},
3632
3633         {NULL, -1, -1, 0}
3634 };
3635
3636
3637 /*
3638  * Find a command by 'key'.
3639  */
3640 static int get_com_id(char key)
3641 {
3642         int i;
3643
3644         for (i = 0; menu_data[i].name; i++)
3645         {
3646                 if (menu_data[i].key == key)
3647                 {
3648                         return menu_data[i].com_id;
3649                 }
3650         }
3651
3652         return 0;
3653 }
3654
3655
3656 /*
3657  * Display the menu, and get a command 
3658  */
3659 static int do_command_menu(int level, int start)
3660 {
3661         int i;
3662         int max_len = 0;
3663         int max_menu_wid;
3664         int col0 = 5 + level*7;
3665         int row0 = 1 + level*3;
3666         byte menu_key = 0;
3667         int menu_id_list[26];
3668         bool redraw = TRUE;
3669         char linestr[MAX_LINELEN];
3670
3671         /* Get max length */
3672         menu_key = 0;
3673         for (i = start; menu_data[i].level >= level; i++)
3674         {
3675                 int len;
3676
3677                 /* Ignore lower level sub menus */
3678                 if (menu_data[i].level > level) continue;
3679
3680                 len = strlen(menu_data[i].name);
3681                 if (len > max_len) max_len = len;
3682
3683                 menu_id_list[menu_key] = i;
3684                 menu_key++;
3685         }
3686
3687         while (menu_key < 26)
3688         {
3689                 menu_id_list[menu_key] = -1;
3690                 menu_key++;
3691         }
3692
3693         /* Extra space for displaying menu key and command key */
3694         max_menu_wid = max_len + 3 + 3;
3695
3696         /* Prepare box line */
3697         linestr[0] = '\0';
3698         strcat(linestr, "+");
3699         for (i = 0; i < max_menu_wid + 2; i++)
3700         {
3701                 strcat(linestr, "-");
3702         }
3703         strcat(linestr, "+");
3704
3705         while (1)
3706         {
3707                 int com_id;
3708                 char key;
3709                 int menu_id;
3710
3711                 if (redraw)
3712                 {
3713                         int row1 = row0 + 1;
3714
3715                         /* Draw top line */
3716                         Term_putstr(col0, row0, -1, TERM_WHITE, linestr);
3717
3718                         /* Draw menu items */
3719                         menu_key = 0;
3720                         for (i = start; menu_data[i].level >= level; i++)
3721                         {
3722                                 char com_key_str[3];
3723                                 cptr str;
3724
3725                                 /* Ignore lower level sub menus */
3726                                 if (menu_data[i].level > level) continue;
3727
3728                                 if (menu_data[i].com_id == -1)
3729                                 {
3730 #ifdef JP
3731                                         strcpy(com_key_str, "¢§");
3732 #else
3733                                         strcpy(com_key_str, ">");
3734 #endif
3735                                 }
3736                                 else if (menu_data[i].key != -1)
3737                                 {
3738                                         com_key_str[0] = '^';
3739                                         com_key_str[1] = menu_data[i].key + '@';
3740                                         com_key_str[2] = '\0';
3741                                 }
3742                                 else
3743                                 {
3744                                         com_key_str[0] = '\0';
3745                                 }
3746
3747                                 str = format("| %c) %-*s %2s | ", menu_key + 'a', max_len, menu_data[i].name, com_key_str);
3748
3749                                 Term_putstr(col0, row1++, -1, TERM_WHITE, str);
3750
3751                                 menu_key++;
3752                         }
3753
3754                         /* Draw bottom line */
3755                         Term_putstr(col0, row1, -1, TERM_WHITE, linestr);
3756
3757                         /* The menu was shown */
3758                         redraw = FALSE;
3759                 }
3760 #ifdef JP
3761                 prt(format("(a-%c) ¥³¥Þ¥ó¥É:", menu_key + 'a' - 1), 0, 0);
3762 #else
3763                 prt(format("(a-%c) Command:", menu_key + 'a' - 1), 0, 0);
3764 #endif
3765                 key = inkey();
3766
3767                 if (key == ESCAPE) return 0;
3768
3769                 if ('a' <= key && key <= 'z')
3770                 {
3771                         menu_id = menu_id_list[key - 'a'];
3772
3773                         if (menu_id >= 0)
3774                         {
3775                                 com_id = menu_data[menu_id].com_id;
3776
3777                                 if (com_id == -1)
3778                                 {
3779                                         com_id = do_command_menu(level + 1, menu_id + 1);
3780
3781                                         if (com_id) return com_id;
3782                                         else redraw = TRUE;
3783                                 }
3784                                 else if (com_id)
3785                                 {
3786                                         return com_id;
3787                                 }
3788                         }
3789                 }
3790
3791                 else
3792                 {
3793                         com_id = get_com_id(key);
3794                         if (com_id) return com_id;
3795                         else continue;
3796                 }
3797         }
3798 }
3799
3800
3801 static chain_str_type *new_chain_str(cptr str)
3802 {
3803         chain_str_type *chain;
3804
3805         size_t len = strlen(str);
3806
3807         chain = (chain_str_type *)ralloc(sizeof(chain_str_type) + len * sizeof(char));
3808
3809         strcpy(chain->s, str);
3810         chain->next = NULL;
3811
3812         return chain;
3813 }
3814
3815
3816 static void kill_yank_chain(text_body_type *tb)
3817 {
3818         chain_str_type *chain = tb->yank;
3819         tb->yank = NULL;
3820         tb->yank_eol = TRUE;
3821
3822         while (chain)
3823         {
3824                 chain_str_type *next = chain->next;
3825                 size_t len = strlen(chain->s);
3826
3827                 rnfree(chain, sizeof(chain_str_type) + len * sizeof(char));
3828
3829                 chain = next;
3830         }
3831 }
3832
3833
3834 static void add_str_to_yank(text_body_type *tb, cptr str)
3835 {
3836         chain_str_type *chain;
3837
3838         tb->yank_eol = FALSE;
3839
3840         if (NULL == tb->yank)
3841         {
3842                 tb->yank = new_chain_str(str);
3843                 return;
3844         }
3845
3846         chain = tb->yank;
3847
3848         while (1)
3849         {
3850                 if (!chain->next)
3851                 {
3852                         chain->next = new_chain_str(str);
3853                         return;
3854                 }
3855
3856                 /* Go to next */
3857                 chain = chain->next;
3858         }
3859 }
3860
3861
3862 #define DESCRIPT_HGT 3
3863
3864 /*
3865  * Draw text
3866  */
3867 static void draw_text_editor(text_body_type *tb)
3868 {
3869         int i;
3870         int by1 = -1, bx1 = -1, by2 = -1, bx2 = -1;
3871
3872         /* Get size */
3873         Term_get_size(&tb->wid, &tb->hgt);
3874
3875         /*
3876          * Top line (-1), description line (-3), separator (-1)
3877          *  == -5
3878          */
3879         tb->hgt -= 2 + DESCRIPT_HGT;
3880
3881 #ifdef JP
3882         /* Don't let cursor at second byte of kanji */
3883         for (i = 0; tb->lines_list[tb->cy][i]; i++)
3884                 if (iskanji(tb->lines_list[tb->cy][i]))
3885                 {
3886                         i++;
3887                         if (i == tb->cx)
3888                         {
3889                                 tb->cx--;
3890                                 break;
3891                         }
3892                 }
3893 #endif
3894
3895         /* Scroll if necessary */
3896         if (tb->cy < tb->upper || tb->upper + tb->hgt <= tb->cy)
3897                 tb->upper = tb->cy - (tb->hgt)/2;
3898         if (tb->upper < 0)
3899                 tb->upper = 0;
3900         if ((tb->cx < tb->left + 10 && tb->left > 0) || tb->left + tb->wid - 5 <= tb->cx)
3901                 tb->left = tb->cx - (tb->wid)*2/3;
3902         if (tb->left < 0)
3903                 tb->left = 0;
3904
3905         /* Redraw whole window after resize */
3906         if (tb->old_wid != tb->wid || tb->old_hgt != tb->hgt)
3907                 tb->dirty_flags |= DIRTY_SCREEN;
3908
3909         /* Redraw all text after scroll */
3910         else if (tb->old_upper != tb->upper || tb->old_left != tb->left)
3911                 tb->dirty_flags |= DIRTY_ALL;
3912
3913
3914         if (tb->dirty_flags & DIRTY_SCREEN)
3915         {
3916                 tb->dirty_flags |= (DIRTY_ALL | DIRTY_MODE);
3917
3918                 /* Clear screen */
3919                 Term_clear();
3920         }
3921
3922         /* Redraw mode line */
3923         if (tb->dirty_flags & DIRTY_MODE)
3924         {
3925                 char buf[MAX_LINELEN];
3926
3927                 int sepa_length = tb->wid;
3928
3929                 /* Separator */
3930                 for (i = 0; i < sepa_length; i++)
3931                         buf[i] = '-';
3932                 buf[i] = '\0';
3933
3934                 Term_putstr(0, tb->hgt + 1, sepa_length, TERM_WHITE, buf);
3935         }
3936
3937         if (tb->dirty_flags & DIRTY_EXPRESSION)
3938         {
3939                 int y;
3940                 byte state = 0;
3941
3942                 for (y = 0; tb->lines_list[y]; y++)
3943                 {
3944                         char f;
3945                         cptr v;
3946                         cptr s = tb->lines_list[y];
3947
3948                         /* Update this line's state */
3949                         tb->states[y] = state;
3950
3951                         if (*s++ != '?') continue;
3952                         if (*s++ != ':') continue;
3953
3954                         /* Lines below this line are auto-registered */
3955                         if (streq(s, "$AUTOREGISTER"))
3956                                 state |= LSTAT_AUTOREGISTER;
3957
3958                         /* Parse the expr */
3959                         v = process_pref_file_expr(&s, &f);
3960
3961                         /* Set flag */
3962                         if (streq(v, "0")) state |= LSTAT_BYPASS;
3963                         else state &= ~LSTAT_BYPASS;
3964
3965                         /* Re-update this line's state */
3966                         tb->states[y] = state | LSTAT_EXPRESSION;
3967                 }
3968
3969                 tb->dirty_flags |= DIRTY_ALL;
3970         }
3971
3972         if (tb->mark)
3973         {
3974                 int tmp_cx = tb->cx;
3975                 int len = strlen(tb->lines_list[tb->cy]);
3976
3977                 /* Correct cursor location */
3978                 if (tb->cx > len) tmp_cx = len;
3979
3980                 tb->dirty_flags |= DIRTY_ALL;
3981
3982                 if (tb->my < tb->cy ||
3983                     (tb->my == tb->cy && tb->mx < tmp_cx))
3984                 {
3985                         by1 = tb->my;
3986                         bx1 = tb->mx;
3987                         by2 = tb->cy;
3988                         bx2 = tmp_cx;
3989                 }
3990                 else
3991                 {
3992                         by2 = tb->my;
3993                         bx2 = tb->mx;
3994                         by1 = tb->cy;
3995                         bx1 = tmp_cx;
3996                 }
3997         }
3998
3999         /* Dump up to tb->hgt lines of messages */
4000         for (i = 0; i < tb->hgt; i++)
4001         {
4002                 int j;
4003                 int leftcol = 0;
4004                 cptr msg;
4005                 byte color;
4006                 int y = tb->upper+i;
4007
4008                 /* clean or dirty? */
4009                 if (!(tb->dirty_flags & DIRTY_ALL) && (tb->dirty_line != y))
4010                         continue;
4011
4012                 msg = tb->lines_list[y];
4013                 if (!msg) break;
4014
4015                 /* Apply horizontal scroll */
4016                 for (j = 0; *msg; msg++, j++)
4017                 {
4018                         if (j == tb->left) break;
4019 #ifdef JP
4020                         if (j > tb->left)
4021                         {
4022                                 leftcol = 1;
4023                                 break;
4024                         }
4025                         if (iskanji(*msg))
4026                         {
4027                                 msg++;
4028                                 j++;
4029                         }
4030 #endif
4031                 }
4032
4033                 /* Erase line */
4034                 Term_erase(0, i + 1, tb->wid);
4035
4036                 if (tb->states[y] & LSTAT_AUTOREGISTER)
4037                 {
4038                         /* Warning color -- These lines will be deleted later */
4039                         color = TERM_L_RED;
4040                 }
4041                 else
4042                 {
4043                         /* Bypassed line will be displayed by darker color */
4044                         if (tb->states[y] & LSTAT_BYPASS) color = TERM_SLATE;
4045                         else color = TERM_WHITE;
4046                 }
4047
4048                 if (!tb->mark)
4049                 {
4050                         /* Dump the messages, bottom to top */
4051                         Term_putstr(leftcol, i + 1, tb->wid - 1, color, msg);
4052                 }
4053
4054                 else
4055                 {
4056                         int x0 = leftcol + tb->left;
4057
4058                         int sx0 = 0;
4059                         int sx1 = 0;
4060
4061                         if (by1 <= y && y < by2) sx1 = strlen(msg);
4062                         if (y == by1) sx0 = bx1;
4063                         if (y == by2) sx1 = bx2;
4064
4065                         Term_gotoxy(leftcol, i + 1);
4066                         if (x0 < sx0) Term_addstr(sx0 - x0, color, msg);
4067                         if (x0 < sx1) Term_addstr(sx1 - sx0, TERM_YELLOW, msg + (sx0 - x0));
4068                         Term_addstr(-1, color, msg + (sx1 - x0));
4069                 }
4070         }
4071
4072         for (; i < tb->hgt; i++)
4073         {
4074                 /* Erase line */
4075                 Term_erase(0, i + 1, tb->wid);
4076         }
4077
4078         /* Display information when updated */
4079         if (tb->old_cy != tb->cy || (tb->dirty_flags & (DIRTY_ALL | DIRTY_NOT_FOUND | DIRTY_NO_SEARCH)) || tb->dirty_line == tb->cy)
4080         {
4081                 autopick_type an_entry, *entry = &an_entry;
4082
4083                 /* Clear information line */
4084                 for (i = 0; i < DESCRIPT_HGT; i++)
4085                 {
4086                         /* Erase line */
4087                         Term_erase(0, tb->hgt + 2 + i, tb->wid);
4088                 }
4089
4090                 /* Display information */
4091                 if (tb->dirty_flags & DIRTY_NOT_FOUND)
4092                 {
4093 #ifdef JP
4094                         prt(format("¥Ñ¥¿¡¼¥ó¤¬¸«¤Ä¤«¤ê¤Þ¤»¤ó: %s", tb->search_str), tb->hgt + 1 + 1, 0);
4095 #else
4096                         prt(format("Pattern not found: %s", tb->search_str), tb->hgt + 1 + 1, 0);
4097 #endif
4098                 }
4099                 else if (tb->dirty_flags & DIRTY_NO_SEARCH)
4100                 {
4101 #ifdef JP
4102                         prt("¸¡º÷Ãæ¤Î¥Ñ¥¿¡¼¥ó¤¬¤¢¤ê¤Þ¤»¤ó('/'¤Ç¸¡º÷)¡£", tb->hgt + 1 + 1, 0);
4103 #else
4104                         prt("No pattern to search. (Press '/' to search.)", tb->hgt +1 + 1, 0);
4105 #endif
4106                 }
4107                 else if (tb->lines_list[tb->cy][0] == '#')
4108                 {
4109 #ifdef JP
4110                         prt("¤³¤Î¹Ô¤Ï¥³¥á¥ó¥È¤Ç¤¹¡£", tb->hgt +1 + 1, 0);
4111 #else
4112                         prt("This line is a comment.", tb->hgt +1 + 1, 0);
4113 #endif
4114                 }
4115                 else if (tb->lines_list[tb->cy][1] == ':')
4116                 {
4117                         cptr str = NULL;
4118
4119                         switch(tb->lines_list[tb->cy][0])
4120                         {
4121                         case '?':
4122 #ifdef JP
4123                                 str = "¤³¤Î¹Ô¤Ï¾ò·ïʬ´ô¼°¤Ç¤¹¡£";
4124 #else
4125                                 str = "This line is a Conditional Expression.";
4126 #endif
4127
4128                                 break;
4129                         case 'A':
4130 #ifdef JP
4131                                 str = "¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¼Â¹ÔÆâÍƤòÄêµÁ¤·¤Þ¤¹¡£";
4132 #else
4133                                 str = "This line defines a Macro action.";
4134 #endif
4135                                 break;
4136                         case 'P':
4137 #ifdef JP
4138                                 str = "¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¥È¥ê¥¬¡¼¡¦¥­¡¼¤òÄêµÁ¤·¤Þ¤¹¡£";
4139 #else
4140                                 str = "This line defines a Macro trigger key.";
4141 #endif
4142                                 break;
4143                         case 'C':
4144 #ifdef JP
4145                                 str = "¤³¤Î¹Ô¤Ï¥­¡¼ÇÛÃÖ¤òÄêµÁ¤·¤Þ¤¹¡£";
4146 #else
4147                                 str = "This line defines a Keymap.";
4148 #endif
4149                                 break;
4150                         }
4151
4152                         /* Draw the first line */
4153                         if (str) prt(str, tb->hgt +1 + 1, 0);
4154
4155                         str = NULL;
4156
4157                         switch(tb->lines_list[tb->cy][0])
4158                         {
4159                         case '?':
4160                                 if (tb->states[tb->cy] & LSTAT_BYPASS)
4161                                 {
4162 #ifdef JP
4163                                         str = "¸½ºß¤Î¼°¤ÎÃͤϡֵ¶(=0)¡×¤Ç¤¹¡£";
4164 #else
4165                                         str = "The expression is 'False'(=0) currently.";
4166 #endif
4167                                 }
4168                                 else
4169                                 {
4170 #ifdef JP
4171                                         str = "¸½ºß¤Î¼°¤ÎÃͤϡֿ¿(=1)¡×¤Ç¤¹¡£";
4172 #else
4173                                         str = "The expression is 'True'(=1) currently.";
4174 #endif
4175                                 }
4176                                 break;
4177
4178                         default:
4179                                 if (tb->states[tb->cy] & LSTAT_AUTOREGISTER)
4180                                 {
4181 #ifdef JP
4182                                         str = "¤³¤Î¹Ô¤Ï¸å¤Çºï½ü¤µ¤ì¤Þ¤¹¡£";
4183 #else
4184                                         str = "This line will be delete later.";
4185 #endif
4186                                 }
4187
4188                                 else if (tb->states[tb->cy] & LSTAT_BYPASS)
4189                                 {
4190 #ifdef JP
4191                                         str = "¤³¤Î¹Ô¤Ï¸½ºß¤Ï̵¸ú¤Ê¾õÂ֤Ǥ¹¡£";
4192 #else
4193                                         str = "This line is bypassed currently.";
4194 #endif
4195                                 }
4196                                 break;
4197                         }
4198
4199                         /* Draw the second line */
4200                         if (str) prt(str, tb->hgt +1 + 2, 0);
4201                 }
4202
4203                 /* Get description of an autopicker preference line */
4204                 else if (autopick_new_entry(entry, tb->lines_list[tb->cy], FALSE))
4205                 {
4206                         char buf[MAX_LINELEN];
4207                         char temp[MAX_LINELEN];
4208                         cptr t;
4209
4210                         describe_autopick(buf, entry);
4211
4212                         if (tb->states[tb->cy] & LSTAT_AUTOREGISTER)
4213                         {
4214 #ifdef JP
4215                                 strcat(buf, "¤³¤Î¹Ô¤Ï¸å¤Çºï½ü¤µ¤ì¤Þ¤¹¡£");
4216 #else
4217                                 strcat(buf, "  This line will be delete later.");
4218 #endif
4219                         }
4220
4221                         if (tb->states[tb->cy] & LSTAT_BYPASS)
4222                         {
4223 #ifdef JP
4224                                 strcat(buf, "¤³¤Î¹Ô¤Ï¸½ºß¤Ï̵¸ú¤Ê¾õÂ֤Ǥ¹¡£");
4225 #else
4226                                 strcat(buf, "  This line is bypassed currently.");
4227 #endif
4228                         }
4229
4230                         roff_to_buf(buf, 81, temp, sizeof(temp));
4231                         t = temp;
4232                         for (i = 0; i < 3; i++)
4233                         {
4234                                 if(t[0] == 0)
4235                                         break; 
4236                                 else
4237                                 {
4238                                         prt(t, tb->hgt +1 + 1 + i, 0);
4239                                         t += strlen(t) + 1;
4240                                 }
4241                         }
4242                         autopick_free_entry(entry);
4243                 }
4244         }
4245 }
4246
4247
4248 /*
4249  * Kill segment of a line
4250  */
4251 static void kill_line_segment(text_body_type *tb, int y, int x0, int x1, bool whole)
4252 {
4253         char buf[MAX_LINELEN];
4254         cptr s = tb->lines_list[y];
4255         char *d = buf;
4256         int x;
4257
4258         /* No segment? */
4259         if (x0 == x1) return;
4260
4261         /* Kill whole line? */
4262         if (whole && x0 == 0 && s[x1] == '\0' && tb->lines_list[y+1])
4263         {
4264                 int i;
4265
4266                 string_free(tb->lines_list[y]);
4267
4268                 /* Shift lines up */
4269                 for (i = y; tb->lines_list[i+1]; i++)
4270                         tb->lines_list[i] = tb->lines_list[i+1];
4271                 tb->lines_list[i] = NULL;
4272
4273                 /* Expressions need re-evaluation */
4274                 tb->dirty_flags |= DIRTY_EXPRESSION;
4275
4276                 return;
4277         }
4278
4279         /* Before the segment */
4280         for (x = 0; x < x0; x++)
4281                 *(d++) = s[x];
4282
4283         /* After the segment */
4284         for (x = x1; s[x]; x++)
4285                 *(d++) = s[x];
4286
4287         *d = '\0';
4288
4289         /* Replace */
4290         string_free(tb->lines_list[y]);
4291         tb->lines_list[y] = string_make(buf);
4292
4293         /* Expressions may need re-evaluation */
4294         check_expression_line(tb, y);
4295
4296         /* Text is changed */
4297         tb->changed = TRUE;
4298 }
4299
4300
4301 /*
4302  * Get a trigger key and insert ASCII string for the trigger
4303  */
4304 static bool insert_macro_line(text_body_type *tb)
4305 {
4306         char tmp[1024];
4307         char buf[1024];
4308         int i, n = 0;
4309
4310         /* Flush */
4311         flush();
4312
4313         /* Do not process macros */
4314         inkey_base = TRUE;
4315
4316         /* First key */
4317         i = inkey();
4318
4319         /* Read the pattern */
4320         while (i)
4321         {
4322                 /* Save the key */
4323                 buf[n++] = i;
4324
4325                 /* Do not process macros */
4326                 inkey_base = TRUE;
4327
4328                 /* Do not wait for keys */
4329                 inkey_scan = TRUE;
4330
4331                 /* Attempt to read a key */
4332                 i = inkey();
4333         }
4334
4335         /* Terminate */
4336         buf[n] = '\0';
4337
4338         /* Flush */
4339         flush();
4340
4341         /* Convert the trigger */
4342         ascii_to_text(tmp, buf);
4343
4344         /* Null */
4345         if(!tmp[0]) return FALSE;
4346
4347         tb->cx = 0;
4348
4349         /* Insert preference string */
4350         insert_return_code(tb);
4351         string_free(tb->lines_list[tb->cy]);
4352         tb->lines_list[tb->cy] = string_make(format("P:%s", tmp));
4353
4354         /* Acquire action */
4355         i = macro_find_exact(buf);
4356
4357         if (i == -1)
4358         {
4359                 /* Nothing defined */
4360                 tmp[0] = '\0';
4361         }
4362         else
4363         {
4364                 /* Analyze the current action */
4365                 ascii_to_text(tmp, macro__act[i]);
4366         }
4367
4368         /* Insert blank action preference line */
4369         insert_return_code(tb);
4370         string_free(tb->lines_list[tb->cy]);
4371         tb->lines_list[tb->cy] = string_make(format("A:%s", tmp));
4372
4373         return TRUE;
4374 }
4375
4376
4377 /*
4378  * Get a command key and insert ASCII string for the key
4379  */
4380 static bool insert_keymap_line(text_body_type *tb)
4381 {
4382         char tmp[1024];
4383         char buf[2];
4384         int mode;
4385         cptr act;
4386
4387         /* Roguelike */
4388         if (rogue_like_commands)
4389         {
4390                 mode = KEYMAP_MODE_ROGUE;
4391         }
4392
4393         /* Original */
4394         else
4395         {
4396                 mode = KEYMAP_MODE_ORIG;
4397         }
4398
4399         /* Flush */
4400         flush();
4401
4402         /* Get a key */
4403         buf[0] = inkey();
4404         buf[1] = '\0';
4405
4406         /* Flush */
4407         flush();
4408
4409         /* Convert the trigger */
4410         ascii_to_text(tmp, buf);
4411
4412         /* Null */
4413         if(!tmp[0]) return FALSE;
4414
4415         tb->cx = 0;
4416
4417         /* Insert preference string */
4418         insert_return_code(tb);
4419         string_free(tb->lines_list[tb->cy]);
4420         tb->lines_list[tb->cy] = string_make(format("C:%d:%s", mode, tmp));
4421
4422         /* Look up the keymap */
4423         act = keymap_act[mode][(byte)(buf[0])];
4424
4425         /* Insert blank action preference line */
4426         insert_return_code(tb);
4427         string_free(tb->lines_list[tb->cy]);
4428         tb->lines_list[tb->cy] = string_make(format("A:%s", act));
4429
4430         return TRUE;
4431 }
4432
4433
4434 /*
4435  * Execute a single editor command
4436  */
4437 static bool do_editor_command(text_body_type *tb, int com_id)
4438 {
4439         switch(com_id)
4440         {
4441         case EC_QUIT:
4442                 if (tb->changed)
4443                 {
4444 #ifdef JP
4445                         if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¤«¤é½ªÎ»¤·¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© ")) break;
4446 #else
4447                         if (!get_check("Discard all changes and quit. Are you sure? ")) break;
4448 #endif
4449                 }
4450                 return QUIT_WITHOUT_SAVE;
4451
4452         case EC_SAVEQUIT:
4453                 return QUIT_AND_SAVE;
4454
4455         case EC_REVERT:
4456                 /* Revert to original */
4457 #ifdef JP
4458                 if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¸µ¤Î¾õÂÖ¤ËÌᤷ¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© ")) break;
4459 #else
4460                 if (!get_check("Discard all changes and revert to original file. Are you sure? ")) break;
4461 #endif
4462
4463                 free_text_lines(tb->lines_list);
4464                 tb->lines_list = read_pickpref_text_lines(&tb->filename_mode);
4465                 tb->dirty_flags |= DIRTY_ALL | DIRTY_MODE | DIRTY_EXPRESSION;
4466                 tb->cx = tb->cy = 0;
4467                 tb->mark = 0;
4468
4469                 /* Text is not changed */
4470                 tb->changed = FALSE;
4471                 break;
4472
4473         case EC_HELP:
4474                 /* Peruse the main help file */
4475 #ifdef JP
4476                 (void)show_file(TRUE, "jeditor.txt", NULL, 0, 0);
4477 #else
4478                 (void)show_file(TRUE, "editor.txt", NULL, 0, 0);
4479 #endif
4480                 /* Redraw all */
4481                 tb->dirty_flags |= DIRTY_SCREEN;
4482
4483                 break;
4484
4485         case EC_RETURN:
4486                 /* Split a line or insert end of line */
4487
4488                 /* Ignore selection */
4489                 if (tb->mark)
4490                 {
4491                         tb->mark = 0;
4492
4493                         /* Now dirty */
4494                         tb->dirty_flags |= DIRTY_ALL;
4495                 }
4496
4497                 insert_return_code(tb);
4498                 tb->cy++;
4499                 tb->cx = 0;
4500
4501                 /* Now dirty */
4502                 tb->dirty_flags |= DIRTY_ALL;
4503                 break;
4504
4505         case EC_LEFT:
4506                 /* Back */
4507                 if (0 < tb->cx)
4508                 {
4509                         int len;
4510
4511                         tb->cx--;
4512                         len = strlen(tb->lines_list[tb->cy]);
4513                         if (len < tb->cx) tb->cx = len;
4514                 }
4515                 else if (tb->cy > 0)
4516                 {
4517                         tb->cy--;
4518                         tb->cx = strlen(tb->lines_list[tb->cy]);
4519                 }
4520                 break;
4521
4522         case EC_DOWN:
4523                 /* Next line */
4524                 if (tb->lines_list[tb->cy + 1]) tb->cy++;
4525                 break;
4526
4527         case EC_UP:
4528                 /* Previous line */
4529                 if (tb->cy > 0) tb->cy--;
4530                 break;
4531
4532         case EC_RIGHT:
4533         {
4534                 /* Forward */
4535
4536                 int len;
4537 #ifdef JP
4538                 if (iskanji(tb->lines_list[tb->cy][tb->cx])) tb->cx++;
4539 #endif
4540                 tb->cx++;
4541                 len = strlen(tb->lines_list[tb->cy]);
4542                 if (len < tb->cx)
4543                 {
4544                         if (tb->lines_list[tb->cy + 1])
4545                         {
4546                                 tb->cy++;
4547                                 tb->cx = 0;
4548                         }
4549                         else
4550                                 tb->cx = len;
4551                 }
4552                 break;
4553         }
4554
4555         case EC_BOL:
4556                 /* Beginning of line */
4557                 tb->cx = 0;
4558                 break;
4559
4560         case EC_EOL:
4561                 /* End of line */
4562                 tb->cx = strlen(tb->lines_list[tb->cy]);
4563                 break;
4564
4565         case EC_PGUP:
4566                 while (0 < tb->cy && tb->upper <= tb->cy)
4567                         tb->cy--;
4568                 while (0 < tb->upper && tb->cy + 1 < tb->upper + tb->hgt)
4569                         tb->upper--;
4570                 break;
4571
4572         case EC_PGDOWN:
4573                 /* Page down */
4574                 while (tb->cy < tb->upper + tb->hgt && tb->lines_list[tb->cy + 1])
4575                         tb->cy++;
4576                 tb->upper = tb->cy;
4577                 break;
4578
4579         case EC_TOP:
4580                 tb->cy = 0;
4581                 break;
4582
4583         case EC_BOTTOM:
4584                 while (tb->lines_list[tb->cy + 1])
4585                         tb->cy++;
4586                 break;
4587
4588         case EC_CUT:
4589         {       
4590                 int by1, bx1, by2, bx2;
4591                 int y;
4592
4593                 /* Copy the text first */
4594                 do_editor_command(tb, EC_COPY);
4595
4596                 if (tb->my < tb->cy ||
4597                     (tb->my == tb->cy && tb->mx < tb->cx))
4598                 {
4599                         by1 = tb->my;
4600                         bx1 = tb->mx;
4601                         by2 = tb->cy;
4602                         bx2 = tb->cx;
4603                 }
4604                 else
4605                 {
4606                         by2 = tb->my;
4607                         bx2 = tb->mx;
4608                         by1 = tb->cy;
4609                         bx1 = tb->cx;
4610                 }
4611
4612                 /* Kill lines in reverse order */
4613                 for (y = by2; y >= by1; y--)
4614                 {
4615                         int x0 = 0;
4616                         int x1 = strlen(tb->lines_list[y]);
4617
4618                         if (y == by1) x0 = bx1;
4619                         if (y == by2) x1 = bx2;
4620
4621                         kill_line_segment(tb, y, x0, x1, TRUE);
4622                 }
4623
4624                 /* Correct cursor position */
4625                 tb->cy = by1;
4626                 tb->cx = bx1;
4627
4628                 /* Disable selection */
4629                 tb->mark = 0;
4630
4631                 /* Now dirty */
4632                 tb->dirty_flags |= DIRTY_ALL;
4633
4634                 break;
4635         }
4636
4637         case EC_COPY:
4638         {       
4639                 int by1, bx1, by2, bx2;
4640                 int y;
4641                 int len = strlen(tb->lines_list[tb->cy]);
4642
4643                 /* Correct cursor location */
4644                 if (tb->cx > len) tb->cx = len;
4645
4646                 /* Use single line? */
4647                 if (!tb->mark)
4648                 {
4649                         tb->my = tb->cy;
4650                         tb->mx = 0;
4651                         if (tb->lines_list[tb->cy + 1])
4652                         {
4653                                 /* Select a single line */
4654                                 tb->cx = 0;
4655                                 tb->cy++;
4656                         }
4657                         else
4658                         {
4659                                 /* Select bottom line */
4660                                 tb->cx = len;
4661                         }
4662                 }
4663
4664                 if (tb->my < tb->cy ||
4665                     (tb->my == tb->cy && tb->mx < tb->cx))
4666                 {
4667                         by1 = tb->my;
4668                         bx1 = tb->mx;
4669                         by2 = tb->cy;
4670                         bx2 = tb->cx;
4671                 }
4672                 else
4673                 {
4674                         by2 = tb->my;
4675                         bx2 = tb->mx;
4676                         by1 = tb->cy;
4677                         bx1 = tb->cx;
4678                 }
4679
4680                 /* Kill old yank buffer */
4681                 kill_yank_chain(tb);
4682
4683                 /* Copy string to yank buffer */
4684                 for (y = by1; y <= by2; y++)
4685                 {
4686                         int i;
4687                         char buf[MAX_LINELEN];
4688
4689                         int x0 = 0;
4690                         int x1 = strlen(tb->lines_list[y]);
4691
4692                         if (y == by1) x0 = bx1;
4693                         if (y == by2) x1 = bx2;
4694
4695                         for (i = 0; i < x1 - x0; i++)
4696                         {
4697                                 buf[i] = tb->lines_list[y][x0 + i];
4698                         }
4699                         buf[i] = '\0';
4700
4701                         add_str_to_yank(tb, buf);
4702                 }
4703
4704                 /* Disable selection */
4705                 tb->mark = 0;
4706
4707                 /* Now dirty */
4708                 tb->dirty_flags |= DIRTY_ALL;
4709                 break;
4710         }
4711
4712         case EC_PASTE:
4713         {
4714                 /* Paste killed text */
4715
4716                 chain_str_type *chain = tb->yank;
4717                 int len = strlen(tb->lines_list[tb->cy]);
4718
4719                 /* Nothing to do? */
4720                 if (!chain) break;
4721
4722                 /* Correct cursor location */
4723                 if (tb->cx > len) tb->cx = len;
4724
4725                 /* Ignore selection */
4726                 if (tb->mark)
4727                 {
4728                         tb->mark = 0;
4729
4730                         /* Now dirty */
4731                         tb->dirty_flags |= DIRTY_ALL;
4732                 }
4733
4734                 /* Paste text */
4735                 while (chain)
4736                 {
4737                         cptr yank_str = chain->s;
4738
4739                         char buf[MAX_LINELEN];
4740                         int i;
4741                         char rest[MAX_LINELEN], *rest_ptr = rest;
4742
4743                         /* Save preceding string */
4744                         for(i = 0; i < tb->cx; i++)
4745                                 buf[i] = tb->lines_list[tb->cy][i];
4746
4747                         strcpy(rest, &(tb->lines_list[tb->cy][i]));
4748
4749                         /* Paste yank buffer */
4750                         while (*yank_str && i < MAX_LINELEN-1)
4751                         {
4752                                 buf[i++] = *yank_str++;
4753                         }
4754
4755                         /* Terminate */
4756                         buf[i] = '\0';
4757
4758                         chain = chain->next;
4759
4760                         if (chain || tb->yank_eol)
4761                         {
4762                                 /* There is an end of line between chain nodes */
4763
4764                                 insert_return_code(tb);
4765
4766                                 /* Replace this line with new one */
4767                                 string_free(tb->lines_list[tb->cy]);
4768                                 tb->lines_list[tb->cy] = string_make(buf);
4769
4770                                 /* Move to next line */
4771                                 tb->cx = 0;
4772                                 tb->cy++;
4773
4774                                 continue;
4775                         }
4776
4777                         /* Final line doesn't have end of line */
4778
4779                         tb->cx = strlen(buf);
4780
4781                         /* Rest of original line */
4782                         while (*rest_ptr && i < MAX_LINELEN-1)
4783                         {
4784                                 buf[i++] = *rest_ptr++;
4785                         }
4786
4787                         /* Terminate */
4788                         buf[i] = '\0';
4789
4790                         /* Replace this line with new one */
4791                         string_free(tb->lines_list[tb->cy]);
4792                         tb->lines_list[tb->cy] = string_make(buf);
4793
4794                         /* Finish */
4795                         break;
4796                 }
4797
4798                 /* Now dirty */
4799                 tb->dirty_flags |= DIRTY_ALL;
4800
4801                 /* Expressions need re-evaluation */
4802                 tb->dirty_flags |= DIRTY_EXPRESSION;
4803
4804                 /* Text is changed */
4805                 tb->changed = TRUE;
4806
4807                 break;
4808         }
4809
4810         case EC_BLOCK:
4811                 if (tb->mark)
4812                 {
4813                         /* Disable the selection */
4814                         tb->mark = 0;
4815
4816                         /* Redraw text */
4817                         tb->dirty_flags |= DIRTY_ALL;
4818                 }
4819                 else
4820                 {
4821                         tb->mark = MARK_MARK;
4822
4823                         /* Repeating this command swaps cursor position */
4824                         if (com_id == tb->old_com_id)
4825                         {
4826                                 int tmp;
4827
4828                                 tmp = tb->cy;
4829                                 tb->cy = tb->my;
4830                                 tb->my = tmp;
4831                                 tmp = tb->cx;
4832                                 tb->cx = tb->mx;
4833                                 tb->mx = tmp;
4834
4835                                 /* Redraw text */
4836                                 tb->dirty_flags |= DIRTY_ALL;
4837                         }
4838                         else
4839                         {
4840                                 int len = strlen(tb->lines_list[tb->cy]);
4841
4842                                 /* Mark the point 1 */
4843                                 tb->my = tb->cy;
4844                                 tb->mx = tb->cx;
4845
4846                                 /* Correct cursor location */
4847                                 if (tb->cx > len) tb->mx = len;
4848                         }
4849                 }
4850                 break;
4851
4852         case EC_KILL_LINE:
4853         {
4854                 /* Kill rest of line */
4855
4856                 int len = strlen(tb->lines_list[tb->cy]);
4857
4858                 /* Correct cursor location */
4859                 if (tb->cx > len) tb->cx = len;
4860
4861                 /* Ignore selection */
4862                 if (tb->mark)
4863                 {
4864                         tb->mark = 0;
4865
4866                         /* Now dirty */
4867                         tb->dirty_flags |= DIRTY_ALL;
4868                 }
4869
4870                 /* Append only if this command is repeated. */
4871                 if (tb->old_com_id != com_id)
4872                 {
4873                         kill_yank_chain(tb);
4874                         tb->yank = NULL;
4875                 }
4876
4877                 /* Really deleted some text */
4878                 if (tb->cx < len)
4879                 {
4880                         /* Add deleted string to yank buffer */
4881                         add_str_to_yank(tb, &(tb->lines_list[tb->cy][tb->cx]));
4882
4883                         kill_line_segment(tb, tb->cy, tb->cx, len, FALSE);
4884
4885                         /* Now dirty */
4886                         tb->dirty_line = tb->cy;
4887
4888                         /* Leave end of line character */
4889                         break;
4890                 }
4891
4892                 /* Cut the end of line character only */
4893                 if (tb->yank_eol) add_str_to_yank(tb, "");
4894
4895                 /* Cut end of line */
4896                 tb->yank_eol = TRUE;
4897
4898                 do_editor_command(tb, EC_DELETE_CHAR);
4899                 break;
4900         }
4901
4902         case EC_DELETE_CHAR:
4903                 /* DELETE == go forward + BACK SPACE */
4904
4905                 /* Ignore selection */
4906                 if (tb->mark)
4907                 {
4908                         tb->mark = 0;
4909
4910                         /* Now dirty */
4911                         tb->dirty_flags |= DIRTY_ALL;
4912                 }
4913
4914 #ifdef JP
4915                 if (iskanji(tb->lines_list[tb->cy][tb->cx])) tb->cx++;
4916 #endif
4917                 tb->cx++;
4918
4919                 do_editor_command(tb, EC_BACKSPACE);
4920                 break;
4921
4922         case EC_BACKSPACE:
4923         {
4924                 /* BACK SPACE */
4925
4926                 int len, i, j, k;
4927                 char buf[MAX_LINELEN];
4928
4929                 /* Ignore selection */
4930                 if (tb->mark)
4931                 {
4932                         tb->mark = 0;
4933
4934                         /* Now dirty */
4935                         tb->dirty_flags |= DIRTY_ALL;
4936                 }
4937
4938                 len = strlen(tb->lines_list[tb->cy]);
4939                 if (len < tb->cx)
4940                 {
4941                         if (tb->lines_list[tb->cy + 1])
4942                         {
4943                                 tb->cy++;
4944                                 tb->cx = 0;
4945                         }
4946                         else
4947                         {
4948                                 tb->cx = len;
4949                                 break;
4950                         }
4951                 }
4952
4953                 if (tb->cx == 0)
4954                 {
4955                         /* delete a return code and union two lines */
4956                         if (tb->cy == 0) break;
4957                         tb->cx = strlen(tb->lines_list[tb->cy-1]);
4958                         strcpy(buf, tb->lines_list[tb->cy-1]);
4959                         strcat(buf, tb->lines_list[tb->cy]);
4960                         string_free(tb->lines_list[tb->cy-1]);
4961                         string_free(tb->lines_list[tb->cy]);
4962                         tb->lines_list[tb->cy-1] = string_make(buf);
4963
4964                         for (i = tb->cy; tb->lines_list[i+1]; i++)
4965                                 tb->lines_list[i] = tb->lines_list[i+1];
4966
4967                         tb->lines_list[i] = NULL;
4968                         tb->cy--;
4969
4970                         /* Now dirty */
4971                         tb->dirty_flags |= DIRTY_ALL;
4972
4973                         /* Expressions need re-evaluation */
4974                         tb->dirty_flags |= DIRTY_EXPRESSION;
4975
4976                         /* Text is changed */
4977                         tb->changed = TRUE;
4978
4979                         break;
4980                 }
4981
4982                 for (i = j = k = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
4983                 {
4984                         k = j;
4985 #ifdef JP
4986                         if (iskanji(tb->lines_list[tb->cy][i]))
4987                                 buf[j++] = tb->lines_list[tb->cy][i++];
4988 #endif
4989                         buf[j++] = tb->lines_list[tb->cy][i];
4990                 }
4991                 while (j > k)
4992                 {
4993                         tb->cx--;
4994                         j--;
4995                 }
4996                 for (; tb->lines_list[tb->cy][i]; i++)
4997                         buf[j++] = tb->lines_list[tb->cy][i];
4998                 buf[j] = '\0';
4999                 string_free(tb->lines_list[tb->cy]);
5000                 tb->lines_list[tb->cy] = string_make(buf);
5001
5002                 /* Now dirty */
5003                 tb->dirty_line = tb->cy;
5004
5005                 /* Expressions may need re-evaluation */
5006                 check_expression_line(tb, tb->cy);
5007
5008                 /* Text is changed */
5009                 tb->changed = TRUE;
5010
5011                 break;
5012         }
5013
5014         case EC_SEARCH_STR:
5015         {
5016                 byte search_dir;
5017
5018                 /* Become dirty because of item/equip menu */
5019                 tb->dirty_flags |= DIRTY_SCREEN;
5020
5021                 search_dir = get_string_for_search(&tb->search_o_ptr, &tb->search_str);
5022
5023                 if (!search_dir) break;
5024
5025                 if (search_dir == 1) do_editor_command(tb, EC_SEARCH_FORW);
5026                 else do_editor_command(tb, EC_SEARCH_BACK);
5027                 break;
5028         }
5029
5030         case EC_SEARCH_FORW:
5031                 if (tb->search_o_ptr)
5032                 {
5033                         if (!search_for_object(tb, tb->search_o_ptr, TRUE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5034                 }
5035                 else if (tb->search_str)
5036                 {
5037                         if (!search_for_string(tb, tb->search_str, TRUE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5038                 }
5039                 else
5040                 {
5041                         tb->dirty_flags |= DIRTY_NO_SEARCH;
5042                 }
5043                 break;
5044
5045         case EC_SEARCH_BACK:
5046                 if (tb->search_o_ptr)
5047                 {
5048                         if (!search_for_object(tb, tb->search_o_ptr, FALSE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5049                 }
5050                 else if (tb->search_str)
5051                 {
5052                         if (!search_for_string(tb, tb->search_str, FALSE)) tb->dirty_flags |= DIRTY_NOT_FOUND;
5053                 }
5054                 else
5055                 {
5056                         tb->dirty_flags |= DIRTY_NO_SEARCH;
5057                 }
5058                 break;
5059
5060         case EC_SEARCH_OBJ:
5061                 /* Become dirty because of item/equip menu */
5062                 tb->dirty_flags |= DIRTY_SCREEN;
5063
5064                 if (!get_object_for_search(&tb->search_o_ptr, &tb->search_str)) break;
5065
5066                 do_editor_command(tb, EC_SEARCH_FORW);
5067                 break;
5068
5069         case EC_SEARCH_DESTROYED:
5070                 if (!get_destroyed_object_for_search(&tb->search_o_ptr, &tb->search_str)) break;
5071
5072                 do_editor_command(tb, EC_SEARCH_FORW);
5073                 break;
5074
5075         case EC_INSERT_OBJECT:
5076         {
5077                 /* Insert choosen item name */
5078
5079                 autopick_type an_entry, *entry = &an_entry;
5080
5081                 if (!entry_from_choosed_object(entry))
5082                 {
5083                         /* Now dirty because of item/equip menu */
5084                         tb->dirty_flags |= DIRTY_SCREEN;
5085                         break;
5086                 }
5087
5088                 tb->cx = 0;
5089                 insert_return_code(tb);
5090                 string_free(tb->lines_list[tb->cy]);
5091                 tb->lines_list[tb->cy] = autopick_line_from_entry_kill(entry);
5092
5093                 /* Now dirty because of item/equip menu */
5094                 tb->dirty_flags |= DIRTY_SCREEN;
5095
5096                 break;
5097         }
5098
5099         case EC_INSERT_DESTROYED:
5100                 /* Insert a name of last destroyed item */
5101                 if (tb->last_destroyed)
5102                 {
5103                         tb->cx = 0;
5104                         insert_return_code(tb);
5105                         string_free(tb->lines_list[tb->cy]);
5106                         tb->lines_list[tb->cy] = string_make(tb->last_destroyed);
5107
5108                         /* Now dirty */
5109                         tb->dirty_flags |= DIRTY_ALL;
5110
5111                         /* Text is changed */
5112                         tb->changed = TRUE;
5113                 }
5114                 break;
5115
5116         case EC_INSERT_BLOCK:
5117         {
5118                 /* Insert a conditinal expression line */
5119                 char classrace[80];
5120
5121                 /* Conditional Expression for Class and Race */
5122                 sprintf(classrace, "?:[AND [EQU $RACE %s] [EQU $CLASS %s]]", 
5123 #ifdef JP
5124                         rp_ptr->E_title, cp_ptr->E_title
5125 #else
5126                         rp_ptr->title, cp_ptr->title
5127 #endif
5128                         );
5129
5130                 tb->cx = 0;
5131                 insert_return_code(tb);
5132                 string_free(tb->lines_list[tb->cy]);
5133                 tb->lines_list[tb->cy] = string_make(classrace);
5134                 tb->cy++;
5135                 insert_return_code(tb);
5136                 string_free(tb->lines_list[tb->cy]);
5137                 tb->lines_list[tb->cy] = string_make("?:1");
5138
5139                 /* Now dirty */
5140                 tb->dirty_flags |= DIRTY_ALL;
5141
5142                 /* Text is changed */
5143                 tb->changed = TRUE;
5144
5145                 break;
5146         }
5147
5148         case EC_INSERT_MACRO:
5149                 /* Draw_everythig (delete menu) */
5150                 draw_text_editor(tb);
5151
5152                 /* Erase line */
5153                 Term_erase(0, tb->cy - tb->upper + 1, tb->wid);
5154
5155                 /* Prompt */
5156 #ifdef JP
5157                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, "P:<¥È¥ê¥¬¡¼¥­¡¼>: ");
5158 #else
5159                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, "P:<Trigger key>: ");
5160 #endif
5161                 if (insert_macro_line(tb))
5162                 {
5163                         /* Prepare to input action */
5164                         tb->cx = 2;
5165
5166                         /* Now dirty */
5167                         tb->dirty_flags |= DIRTY_ALL;
5168
5169                         /* Text is changed */
5170                         tb->changed = TRUE;
5171                 }
5172
5173                 break;
5174
5175         case EC_INSERT_KEYMAP:
5176                 /* Draw_everythig (delete menu) */
5177                 draw_text_editor(tb);
5178
5179                 /* Erase line */
5180                 Term_erase(0, tb->cy - tb->upper + 1, tb->wid);
5181
5182                 /* Prompt */
5183 #ifdef JP
5184                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, format("C:%d:<¥³¥Þ¥ó¥É¥­¡¼>: ", (rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG)));
5185 #else
5186                 Term_putstr(0, tb->cy - tb->upper + 1, tb->wid - 1, TERM_YELLOW, format("C:%d:<Keypress>: ", (rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG)));
5187 #endif
5188
5189                 if (insert_keymap_line(tb))
5190                 {
5191                         /* Prepare to input action */
5192                         tb->cx = 2;
5193
5194                         /* Now dirty */
5195                         tb->dirty_flags |= DIRTY_ALL;
5196
5197                         /* Text is changed */
5198                         tb->changed = TRUE;
5199                 }                               
5200                 break;
5201
5202         case EC_CL_AUTOPICK: toggle_command_letter(tb, DO_AUTOPICK); break;
5203         case EC_CL_DESTROY: toggle_command_letter(tb, DO_AUTODESTROY); break;
5204         case EC_CL_LEAVE: toggle_command_letter(tb, DONT_AUTOPICK); break;
5205         case EC_CL_QUERY: toggle_command_letter(tb, DO_QUERY_AUTOPICK); break;
5206         case EC_CL_NO_DISP: toggle_command_letter(tb, DO_DISPLAY); break;
5207
5208         case EC_IK_UNAWARE: toggle_keyword(tb, FLG_UNAWARE); break;
5209         case EC_IK_UNIDENTIFIED: toggle_keyword(tb, FLG_UNIDENTIFIED); break;
5210         case EC_IK_IDENTIFIED: toggle_keyword(tb, FLG_IDENTIFIED); break;
5211         case EC_IK_STAR_IDENTIFIED: toggle_keyword(tb, FLG_STAR_IDENTIFIED); break;
5212         case EC_KK_WEAPONS: toggle_keyword(tb, FLG_WEAPONS); break;
5213         case EC_KK_FAVORITE: toggle_keyword(tb, FLG_FAVORITE); break;
5214         case EC_KK_ARMORS: toggle_keyword(tb, FLG_ARMORS); break;
5215         case EC_KK_MISSILES: toggle_keyword(tb, FLG_MISSILES); break;
5216         case EC_KK_DEVICES: toggle_keyword(tb, FLG_DEVICES); break;
5217         case EC_KK_LIGHTS: toggle_keyword(tb, FLG_LIGHTS); break;
5218         case EC_KK_JUNKS: toggle_keyword(tb, FLG_JUNKS); break;
5219         case EC_KK_SPELLBOOKS: toggle_keyword(tb, FLG_SPELLBOOKS); break;
5220         case EC_KK_SHIELDS: toggle_keyword(tb, FLG_SHIELDS); break;
5221         case EC_KK_BOWS: toggle_keyword(tb, FLG_BOWS); break;
5222         case EC_KK_RINGS: toggle_keyword(tb, FLG_RINGS); break;
5223         case EC_KK_AMULETS: toggle_keyword(tb, FLG_AMULETS); break;
5224         case EC_KK_SUITS: toggle_keyword(tb, FLG_SUITS); break;
5225         case EC_KK_CLOAKS: toggle_keyword(tb, FLG_CLOAKS); break;
5226         case EC_KK_HELMS: toggle_keyword(tb, FLG_HELMS); break;
5227         case EC_KK_GLOVES: toggle_keyword(tb, FLG_GLOVES); break;
5228         case EC_KK_BOOTS: toggle_keyword(tb, FLG_BOOTS); break;
5229         case EC_OK_COLLECTING: toggle_keyword(tb, FLG_COLLECTING); break;
5230         case EC_OK_BOOSTED: toggle_keyword(tb, FLG_BOOSTED); break;
5231         case EC_OK_MORE_THAN: toggle_keyword(tb, FLG_MORE_THAN); break;
5232         case EC_OK_MORE_BONUS: toggle_keyword(tb, FLG_MORE_BONUS); break;
5233         case EC_OK_WORTHLESS: toggle_keyword(tb, FLG_WORTHLESS); break;
5234         case EC_OK_ARTIFACT: toggle_keyword(tb, FLG_ARTIFACT); break;
5235         case EC_OK_EGO: toggle_keyword(tb, FLG_EGO); break;
5236         case EC_OK_NAMELESS: toggle_keyword(tb, FLG_NAMELESS); break;
5237         case EC_OK_WANTED: toggle_keyword(tb, FLG_WANTED); break;
5238         case EC_OK_UNIQUE: toggle_keyword(tb, FLG_UNIQUE); break;
5239         case EC_OK_HUMAN: toggle_keyword(tb, FLG_HUMAN); break;
5240         case EC_OK_UNREADABLE:
5241                 toggle_keyword(tb, FLG_UNREADABLE);
5242                 add_keyword(tb, FLG_SPELLBOOKS);
5243                 break;
5244         case EC_OK_REALM1:
5245                 toggle_keyword(tb, FLG_REALM1);
5246                 add_keyword(tb, FLG_SPELLBOOKS);
5247                 break;
5248         case EC_OK_REALM2:
5249                 toggle_keyword(tb, FLG_REALM2);
5250                 add_keyword(tb, FLG_SPELLBOOKS);
5251                 break;
5252         case EC_OK_FIRST:
5253                 toggle_keyword(tb, FLG_FIRST);
5254                 add_keyword(tb, FLG_SPELLBOOKS);
5255                 break;
5256         case EC_OK_SECOND:
5257                 toggle_keyword(tb, FLG_SECOND);
5258                 add_keyword(tb, FLG_SPELLBOOKS);
5259                 break;
5260         case EC_OK_THIRD:
5261                 toggle_keyword(tb, FLG_THIRD);
5262                 add_keyword(tb, FLG_SPELLBOOKS);
5263                 break;
5264         case EC_OK_FOURTH:
5265                 toggle_keyword(tb, FLG_FOURTH);
5266                 add_keyword(tb, FLG_SPELLBOOKS);
5267                 break;
5268         }
5269
5270         /* Save old command */
5271         tb->old_com_id = com_id;
5272
5273         return FALSE;
5274 }
5275
5276
5277 /*
5278  * Insert single letter at cursor position.
5279  */
5280 static void insert_single_letter(text_body_type *tb, int key)
5281 {
5282         int i, j, len;
5283         char buf[MAX_LINELEN];
5284
5285         /* Save preceding string */
5286         for (i = j = 0; tb->lines_list[tb->cy][i] && i < tb->cx; i++)
5287                 buf[j++] = tb->lines_list[tb->cy][i];
5288
5289         /* Add a character */
5290 #ifdef JP
5291         if (iskanji(key))
5292         {
5293                 int next;
5294
5295                 inkey_base = TRUE;
5296                 next = inkey();
5297                 if (j+2 < MAX_LINELEN)
5298                 {
5299                         buf[j++] = key;
5300                         buf[j++] = next;
5301                         tb->cx += 2;
5302                 }
5303                 else
5304                         bell();
5305         }
5306         else
5307 #endif
5308         {
5309                 if (j+1 < MAX_LINELEN)
5310                         buf[j++] = key;
5311                 tb->cx++;
5312         }
5313
5314         /* Add following */
5315         for (; tb->lines_list[tb->cy][i] && j + 1 < MAX_LINELEN; i++)
5316                 buf[j++] = tb->lines_list[tb->cy][i];
5317         buf[j] = '\0';
5318
5319         /* Replace current line with new line */
5320         string_free(tb->lines_list[tb->cy]);
5321         tb->lines_list[tb->cy] = string_make(buf);
5322
5323         /* Move to correct collumn */
5324         len = strlen(tb->lines_list[tb->cy]);
5325         if (len < tb->cx) tb->cx = len;
5326
5327         /* Now dirty */
5328         tb->dirty_line = tb->cy;
5329
5330         /* Expressions may need re-evaluation */
5331         check_expression_line(tb, tb->cy);
5332
5333         /* Text is changed */
5334         tb->changed = TRUE;
5335 }
5336
5337 /*
5338  * In-game editor of Object Auto-picker/Destoryer
5339  */
5340 void do_cmd_edit_autopick(void)
5341 {
5342         static int cx_save = 0;
5343         static int cy_save = 0;
5344
5345         text_body_type text_body, *tb = &text_body;
5346
5347         autopick_type an_entry, *entry = &an_entry;
5348         char buf[MAX_LINELEN];
5349
5350         int i;
5351         int key = -1;
5352
5353         static s32b old_autosave_turn = 0L;
5354         byte quit = 0;
5355
5356         tb->changed = FALSE;
5357         tb->cx = cx_save;
5358         tb->cy = cy_save;
5359         tb->upper = tb->left = 0;
5360         tb->mark = 0;
5361         tb->mx = tb->my = 0;
5362         tb->old_cy = tb->old_upper = tb->old_left = -1;
5363         tb->old_wid = tb->old_hgt = -1;
5364         tb->old_com_id = 0;
5365
5366         tb->yank = NULL;
5367         tb->search_o_ptr = NULL;
5368         tb->search_str = NULL;
5369         tb->last_destroyed = NULL;
5370         tb->dirty_flags = DIRTY_ALL | DIRTY_MODE | DIRTY_EXPRESSION;
5371         tb->dirty_line = -1;
5372         tb->filename_mode = PT_DEFAULT;
5373
5374         /* Autosave */
5375         if (turn > old_autosave_turn + 100L)
5376         {
5377                 do_cmd_save_game(TRUE);
5378                 old_autosave_turn = turn;
5379         }
5380
5381         /* HACK -- Reset start_time to stop counting playtime while edit */
5382         update_playtime();
5383
5384         /* Free old entries */
5385         init_autopicker();
5386
5387         /* Command Description of the 'Last Destroyed Item' */
5388         if (autopick_last_destroyed_object.k_idx)
5389         {
5390                 autopick_entry_from_object(entry, &autopick_last_destroyed_object);
5391                 tb->last_destroyed = autopick_line_from_entry_kill(entry);
5392         }
5393
5394         /* Read or initialize whole text */
5395         tb->lines_list = read_pickpref_text_lines(&tb->filename_mode);
5396
5397         /* Reset cursor position if needed */
5398         for (i = 0; i < tb->cy; i++)
5399         {
5400                 if (!tb->lines_list[i])
5401                 {
5402                         tb->cy = tb->cx = 0;
5403                         break;
5404                 }
5405         }
5406
5407         /* Save the screen */
5408         screen_save();
5409
5410         /* Process requests until done */
5411         while (!quit)
5412         {
5413                 int com_id = 0;
5414                 size_t trig_len;
5415
5416                 /* Draw_everythig */
5417                 draw_text_editor(tb);
5418
5419                 /* Display header line */
5420 #ifdef JP
5421                 prt("(^Q:½ªÎ» ^W:¥»¡¼¥Ö¤·¤Æ½ªÎ», ESC:¥á¥Ë¥å¡¼, ¤½¤Î¾:ÆþÎÏ)", 0, 0);
5422 #else   
5423                 prt("(^Q:quit, ^W:save&quit, ESC:menu, Other:input text)", 0, 0);
5424 #endif
5425                 if (!tb->mark)
5426                 {
5427                         /* Display current position */
5428                         prt (format("(%d,%d)", tb->cx, tb->cy), 0, 60);
5429                 }
5430                 else
5431                 {
5432                         /* Display current position and mark position */
5433                         prt (format("(%d,%d)-(%d,%d)", tb->mx, tb->my, tb->cx, tb->cy), 0, 60);
5434                 }
5435
5436                 /* Place cursor */
5437                 Term_gotoxy(tb->cx - tb->left, tb->cy - tb->upper + 1);
5438
5439                 /* Now clean */
5440                 tb->dirty_flags = 0;
5441                 tb->dirty_line = -1;
5442
5443                 /* Save old key and location */
5444                 tb->old_cy = tb->cy;
5445                 tb->old_upper = tb->upper;
5446                 tb->old_left = tb->left;
5447                 tb->old_wid = tb->wid;
5448                 tb->old_hgt = tb->hgt;
5449
5450                 /* Get a command */
5451                 key = inkey();
5452
5453                 /* Count length of macro trigger which induced this key */
5454                 trig_len = strlen(inkey_macro_trigger_string);
5455
5456                 /* HACK -- ignore macro defined on ASCII keys */
5457                 if (trig_len == 1)
5458                 {
5459                         /* Get original key */
5460                         key = inkey_macro_trigger_string[0];
5461
5462                         /* Kill further macro expansion */
5463                         flush();
5464                 }
5465
5466                 /* Delete key */
5467                 if (key == 0x7F) key = KTRL('d');
5468
5469
5470                 /* Cursor key macroes to direction command */
5471                 if (trig_len > 1)
5472                 {
5473                         switch (key)
5474                         {
5475                         case '2':
5476                                 com_id = EC_DOWN;
5477                                 break;
5478                         case '4':
5479                                 com_id = EC_LEFT;
5480                                 break;
5481                         case '6':
5482                                 com_id = EC_RIGHT;
5483                                 break;
5484                         case '8':
5485                                 com_id = EC_UP;
5486                                 break;
5487                         }
5488
5489                         if (com_id)
5490                         {
5491                                 /*
5492                                  * Un-shifted cursor keys cancells
5493                                  * selection created by shift+cursor.
5494                                  */
5495                                 if (tb->mark & MARK_BY_SHIFT)
5496                                 {
5497                                         tb->mark = 0;
5498
5499                                         /* Now dirty */
5500                                         tb->dirty_flags |= DIRTY_ALL;
5501                                 }
5502                         }
5503
5504                         /* Mega Hack!!! Start selection with shift + cursor keys */
5505                         else
5506                         {
5507                                 char buf[1024];
5508
5509                                 /* Get ascii form */
5510                                 ascii_to_text(buf, inkey_macro_trigger_string);
5511
5512                                 if (strstr(buf, "shift-Down"))
5513                                         com_id = EC_DOWN;
5514                                 else if (strstr(buf, "shift-Left"))
5515                                         com_id = EC_LEFT;
5516                                 else if (strstr(buf, "shift-Right"))
5517                                         com_id = EC_RIGHT;
5518                                 else if (strstr(buf, "shift-Up"))
5519                                         com_id = EC_UP;
5520
5521                                 if (com_id)
5522                                 {
5523                                         /* Kill further macro expansion */
5524                                         flush();
5525
5526                                         /* Start selection */
5527                                         if (!tb->mark)
5528                                         {
5529                                                 int len = strlen(tb->lines_list[tb->cy]);
5530
5531                                                 tb->mark = MARK_MARK | MARK_BY_SHIFT;
5532                                                 tb->my = tb->cy;
5533                                                 tb->mx = tb->cx;
5534
5535                                                 /* Correct cursor location */
5536                                                 if (tb->cx > len) tb->mx = len;
5537                                                 
5538                                                 /* Need to redraw text */
5539                                                 if (com_id == EC_UP || com_id == EC_DOWN)
5540                                                 {
5541                                                         /* Redraw all text */
5542                                                         tb->dirty_flags |= DIRTY_ALL;
5543                                                 }
5544                                                 else
5545                                                 {
5546                                                         tb->dirty_line = tb->cy;
5547                                                 }
5548                                         }
5549                                 }
5550                         }
5551                 }
5552
5553                 if (com_id)
5554                 {
5555                         /* Already done */
5556                 }
5557
5558                 /* Open the menu */
5559                 else if (key == ESCAPE)
5560                 {
5561                         com_id = do_command_menu(0, 0);
5562
5563                         /* Redraw all text later */
5564                         tb->dirty_flags |= DIRTY_SCREEN;
5565                 }
5566
5567                 /* Insert a character */
5568                 else if (!iscntrl(key & 0xff))
5569                 {
5570                         /* Ignore selection */
5571                         if (tb->mark)
5572                         {
5573                                 tb->mark = 0;
5574
5575                                 /* Now dirty */
5576                                 tb->dirty_flags |= DIRTY_ALL;
5577                         }
5578
5579                         insert_single_letter(tb, key);
5580
5581                         /* Next loop */
5582                         continue;
5583                 }
5584
5585                 /* Other commands */
5586                 else
5587                 {
5588                         com_id = get_com_id(key);
5589                 }
5590
5591                 if (com_id) quit = do_editor_command(tb, com_id);
5592         } /* while (1) */
5593
5594         /* Restore the screen */
5595         screen_load();
5596
5597         /* Get the filename of preference */
5598         strcpy(buf, pickpref_filename(tb->filename_mode));
5599
5600         if (quit == QUIT_AND_SAVE)
5601                 write_text_lines(buf, tb->lines_list);
5602
5603         free_text_lines(tb->lines_list);
5604
5605         string_free(tb->last_destroyed);
5606
5607         /* Destroy string chain */
5608         kill_yank_chain(tb);
5609
5610         /* Reload autopick pref */
5611         process_pickpref_file(buf);
5612
5613         /* HACK -- reset start_time so that playtime is not increase while edit */
5614         start_time = time(NULL);
5615
5616         /* Save cursor location */
5617         cx_save = tb->cx;
5618         cy_save = tb->cy;
5619 }