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