OSDN Git Service

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