OSDN Git Service

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