OSDN Git Service

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