OSDN Git Service

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