OSDN Git Service

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