OSDN Git Service

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