OSDN Git Service

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