OSDN Git Service

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