OSDN Git Service

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