OSDN Git Service

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