OSDN Git Service

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