OSDN Git Service

ホホー雉ー・「・ッ・サ・ケ、ホ・ミ・ー(inventory[INVEN_TOTAL]、ヒツミ、キ、ニシォニーヌヒイホ・チ・ァ・テ・ッ、キ、ニ、、、ソ)、、タオ。」
[hengband/hengband.git] / src / autopick.c
1 /* File: autopick.c */
2
3 /* Purpose: Object Auto-picker/Destroyer */
4
5 /*
6  * Copyright (c) 2002  Mogami
7  *
8  * This software may be copied and distributed for educational, research, and
9  * not for profit purposes provided that this copyright and statement are
10  * included in all such copies.
11  */
12
13 #include "angband.h"
14
15 #define MAX_LINELEN 1024
16
17 static object_type autopick_last_destroyed_object;
18
19 /*
20  * Macros for Keywords
21  */
22 #define FLG_ALL             0
23 #define FLG_COLLECTING      1
24 #define FLG_UNIDENTIFIED    2
25 #define FLG_IDENTIFIED      3
26 #define FLG_STAR_IDENTIFIED 4
27 #define FLG_BOOSTED         5
28 #define FLG_MORE_THAN       6
29 #define FLG_DICE            7
30 #define FLG_MORE_BONUS      8
31 #define FLG_MORE_BONUS2     9
32 #define FLG_WORTHLESS       10
33 #define FLG_ARTIFACT        11
34 #define FLG_EGO             12
35 #define FLG_NAMELESS        13
36 #define FLG_UNAWARE         14
37 #define FLG_WANTED          15
38 #define FLG_UNIQUE          16
39 #define FLG_HUMAN           17
40 #define FLG_UNREADABLE      18
41 #define FLG_REALM1          19
42 #define FLG_REALM2          20
43 #define FLG_FIRST           21
44 #define FLG_SECOND          22
45 #define FLG_THIRD           23
46 #define FLG_FOURTH          24
47
48 #define FLG_ITEMS           30
49 #define FLG_WEAPONS         31
50 #define FLG_ARMORS          32
51 #define FLG_MISSILES        33
52 #define FLG_DEVICES         34
53 #define FLG_LIGHTS          35
54 #define FLG_JUNKS           36
55 #define FLG_SPELLBOOKS      37
56 #define FLG_HAFTED          38
57 #define FLG_SHIELDS         39
58 #define FLG_BOWS            40
59 #define FLG_RINGS           41
60 #define FLG_AMULETS         42
61 #define FLG_SUITS           43
62 #define FLG_CLOAKS          44
63 #define FLG_HELMS           45
64 #define FLG_GLOVES          46
65 #define FLG_BOOTS           47
66
67 #ifdef JP
68
69 #define KEY_ALL "¤¹¤Ù¤Æ¤Î"
70
71 #ifdef MAC_MPW
72 /*
73  * MEGA HACK -- MPW¤Î¥Ð¥°½ü¤±¡£
74  * pre-processÃæ¤Ë¡Ö¼ý¡×¤Î»ú¤Î2¥Ð¥¤¥ÈÌܤ¬¾¡¼ê¤Ë¾Ã¤¨¤Æ¤·¤Þ¤¦¡£
75  */
76 #define KEY_COLLECTING "\x8e\xfb½¸Ãæ¤Î"
77 #else
78 #define KEY_COLLECTING "¼ý½¸Ãæ¤Î"
79 #endif
80
81 #define KEY_UNIDENTIFIED "̤´ÕÄê¤Î"
82 #define KEY_IDENTIFIED "´ÕÄêºÑ¤ß¤Î"
83 #define KEY_STAR_IDENTIFIED "*´ÕÄê*ºÑ¤ß¤Î"
84 #define KEY_BOOSTED "¥À¥¤¥¹Ìܤΰ㤦"
85 #define KEY_MORE_THAN  "¥À¥¤¥¹ÌÜ"
86 #define KEY_DICE  "°Ê¾å¤Î"
87 #define KEY_MORE_BONUS  "½¤ÀµÃÍ"
88 #define KEY_MORE_BONUS2  "°Ê¾å¤Î"
89 #define KEY_WORTHLESS "̵²ÁÃͤÎ"
90 #define KEY_ARTIFACT "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È"
91 #define KEY_EGO "¥¨¥´"
92 #define KEY_NAMELESS "̵ÌäÎ"
93 #define KEY_UNAWARE "̤ȽÌÀ¤Î"
94 #define KEY_WANTED "¾Þ¶â¼ó¤Î"
95 #define KEY_UNIQUE "¥æ¥Ë¡¼¥¯¡¦¥â¥ó¥¹¥¿¡¼¤Î"
96 #define KEY_HUMAN "¿Í´Ö¤Î"
97 #define KEY_UNREADABLE "Æɤá¤Ê¤¤"
98 #define KEY_REALM1 "Âè°ìÎΰè¤Î"
99 #define KEY_REALM2 "ÂèÆóÎΰè¤Î"
100 #define KEY_FIRST "1ºýÌܤÎ"
101 #define KEY_SECOND "2ºýÌܤÎ"
102 #define KEY_THIRD "3ºýÌܤÎ"
103 #define KEY_FOURTH "4ºýÌܤÎ"
104 #define KEY_ITEMS "¥¢¥¤¥Æ¥à"
105 #define KEY_WEAPONS "Éð´ï"
106 #define KEY_ARMORS "Ëɶñ"
107 #define KEY_MISSILES "Ìð"
108 #define KEY_DEVICES "ËâË¡¥¢¥¤¥Æ¥à"
109 #define KEY_LIGHTS "¸÷¸»"
110 #define KEY_JUNKS "¤¬¤é¤¯¤¿"
111 #define KEY_SPELLBOOKS "ËâË¡½ñ"
112 #define KEY_HAFTED "Æß´ï"
113 #define KEY_SHIELDS "½â"
114 #define KEY_BOWS "µÝ"
115 #define KEY_RINGS "»ØÎØ"
116 #define KEY_AMULETS "¥¢¥ß¥å¥ì¥Ã¥È"
117 #define KEY_SUITS "³»"
118 #define KEY_CLOAKS "¥¯¥í¡¼¥¯"
119 #define KEY_HELMS "³õ"
120 #define KEY_GLOVES "äƼê"
121 #define KEY_BOOTS "·¤"
122
123 #else 
124
125 #define KEY_ALL "all"
126 #define KEY_COLLECTING "collecting"
127 #define KEY_UNIDENTIFIED "unidentified"
128 #define KEY_IDENTIFIED "identified"
129 #define KEY_STAR_IDENTIFIED "*identified*"
130 #define KEY_BOOSTED "dice boosted"
131 #define KEY_MORE_THAN  "more than"
132 #define KEY_DICE  " dice"
133 #define KEY_MORE_BONUS  "more bonus than"
134 #define KEY_MORE_BONUS2  ""
135 #define KEY_WORTHLESS "worthless"
136 #define KEY_ARTIFACT "artifact"
137 #define KEY_EGO "ego"
138 #define KEY_NAMELESS "nameless"
139 #define KEY_UNAWARE "unaware"
140 #define KEY_WANTED "wanted"
141 #define KEY_UNIQUE "unique monster's"
142 #define KEY_HUMAN "human"
143 #define KEY_UNREADABLE "unreadable"
144 #define KEY_REALM1 "first realm's"
145 #define KEY_REALM2 "second realm's"
146 #define KEY_FIRST "first"
147 #define KEY_SECOND "second"
148 #define KEY_THIRD "third"
149 #define KEY_FOURTH "fourth"
150 #define KEY_ITEMS "items"
151 #define KEY_WEAPONS "weapons"
152 #define KEY_ARMORS "armors"
153 #define KEY_MISSILES "missiles"
154 #define KEY_DEVICES "magical devices"
155 #define KEY_LIGHTS "lights"
156 #define KEY_JUNKS "junks"
157 #define KEY_SPELLBOOKS "spellbooks"
158 #define KEY_HAFTED "hafted weapons"
159 #define KEY_SHIELDS "shields"
160 #define KEY_BOWS "bows"
161 #define KEY_RINGS "rings"
162 #define KEY_AMULETS "amulets"
163 #define KEY_SUITS "suits"
164 #define KEY_CLOAKS "cloaks"
165 #define KEY_HELMS "helms"
166 #define KEY_GLOVES "gloves"
167 #define KEY_BOOTS "boots"
168
169 #endif /* JP */
170
171 #define MATCH_KEY(KEY) (!strncmp(ptr, KEY, sizeof(KEY)-1)\
172      ? (ptr += sizeof(KEY)-1, (' '==*ptr) ? ptr++ : 0, TRUE) : FALSE)
173 #define MATCH_KEY2(KEY) (!strncmp(ptr, KEY, sizeof(KEY)-1)\
174      ? (prev_ptr = ptr, ptr += sizeof(KEY)-1, (' '==*ptr) ? ptr++ : 0, TRUE) : FALSE)
175
176 #ifdef JP
177 #define ADD_KEY(KEY) strcat(ptr, KEY)
178 #else
179 #define ADD_KEY(KEY) (strcat(ptr, KEY), strcat(ptr, " "))
180 #endif
181 #define ADD_KEY2(KEY) strcat(ptr, KEY)
182
183 #define ADD_FLG(FLG) (entry->flag[FLG / 32] |= (1L << (FLG % 32)))
184 #define REM_FLG(FLG) (entry->flag[FLG / 32] &= ~(1L << (FLG % 32)))
185 #define ADD_FLG_NOUN(FLG) (ADD_FLG(FLG), prev_flg = FLG)
186 #define IS_FLG(FLG) (entry->flag[FLG / 32] & (1L << (FLG % 32)))
187
188 #ifdef JP
189         static char kanji_colon[] = "¡§";
190 #endif
191
192
193 /*
194  * Reconstruct preference line from entry
195  */
196 cptr autopick_line_from_entry(autopick_type *entry)
197 {
198         char buf[MAX_LINELEN];
199         char *ptr;
200         bool sepa_flag = TRUE;
201
202         *buf = '\0';
203         if (!(entry->action & DO_DISPLAY)) strcat(buf, "(");
204         if (entry->action & DO_QUERY_AUTOPICK) strcat(buf, ";");
205         if (entry->action & DO_AUTODESTROY) strcat(buf, "!");
206         if (entry->action & DONT_AUTOPICK) strcat(buf, "~");
207
208         ptr = buf;
209
210         if (IS_FLG(FLG_ALL)) ADD_KEY(KEY_ALL);
211         if (IS_FLG(FLG_COLLECTING)) ADD_KEY(KEY_COLLECTING);
212         if (IS_FLG(FLG_UNIDENTIFIED)) ADD_KEY(KEY_UNIDENTIFIED);
213         if (IS_FLG(FLG_IDENTIFIED)) ADD_KEY(KEY_IDENTIFIED);
214         if (IS_FLG(FLG_STAR_IDENTIFIED)) ADD_KEY(KEY_STAR_IDENTIFIED);
215         if (IS_FLG(FLG_UNAWARE)) ADD_KEY(KEY_UNAWARE);
216         if (IS_FLG(FLG_BOOSTED)) ADD_KEY(KEY_BOOSTED);
217
218         if (IS_FLG(FLG_MORE_THAN))
219         {
220                 ADD_KEY(KEY_MORE_THAN);
221                 strcat(ptr, format("%d", entry->dice));
222                 ADD_KEY(KEY_DICE);
223         }
224
225         if (IS_FLG(FLG_MORE_BONUS))
226         {
227                 ADD_KEY(KEY_MORE_BONUS);
228                 strcat(ptr, format("%d", entry->bonus));
229                 ADD_KEY(KEY_MORE_BONUS2);
230         }
231
232         if (IS_FLG(FLG_UNREADABLE)) ADD_KEY(KEY_UNREADABLE);
233         if (IS_FLG(FLG_REALM1)) ADD_KEY(KEY_REALM1);
234         if (IS_FLG(FLG_REALM2)) ADD_KEY(KEY_REALM2);
235         if (IS_FLG(FLG_FIRST)) ADD_KEY(KEY_FIRST);
236         if (IS_FLG(FLG_SECOND)) ADD_KEY(KEY_SECOND);
237         if (IS_FLG(FLG_THIRD)) ADD_KEY(KEY_THIRD);
238         if (IS_FLG(FLG_FOURTH)) ADD_KEY(KEY_FOURTH);
239         if (IS_FLG(FLG_WANTED)) ADD_KEY(KEY_WANTED);
240         if (IS_FLG(FLG_UNIQUE)) ADD_KEY(KEY_UNIQUE);
241         if (IS_FLG(FLG_HUMAN)) ADD_KEY(KEY_HUMAN);
242         if (IS_FLG(FLG_WORTHLESS)) ADD_KEY(KEY_WORTHLESS);
243         if (IS_FLG(FLG_NAMELESS)) ADD_KEY(KEY_NAMELESS);
244         if (IS_FLG(FLG_EGO)) ADD_KEY(KEY_EGO);
245
246         if (IS_FLG(FLG_ARTIFACT)) ADD_KEY(KEY_ARTIFACT);
247
248         if (IS_FLG(FLG_ITEMS)) ADD_KEY2(KEY_ITEMS);
249         else if (IS_FLG(FLG_WEAPONS)) ADD_KEY2(KEY_WEAPONS);
250         else if (IS_FLG(FLG_ARMORS)) ADD_KEY2(KEY_ARMORS);
251         else if (IS_FLG(FLG_MISSILES)) ADD_KEY2(KEY_MISSILES);
252         else if (IS_FLG(FLG_DEVICES)) ADD_KEY2(KEY_DEVICES);
253         else if (IS_FLG(FLG_LIGHTS)) ADD_KEY2(KEY_LIGHTS);
254         else if (IS_FLG(FLG_JUNKS)) ADD_KEY2(KEY_JUNKS);
255         else if (IS_FLG(FLG_SPELLBOOKS)) ADD_KEY2(KEY_SPELLBOOKS);
256         else if (IS_FLG(FLG_HAFTED)) ADD_KEY2(KEY_HAFTED);
257         else if (IS_FLG(FLG_SHIELDS)) ADD_KEY2(KEY_SHIELDS);
258         else if (IS_FLG(FLG_BOWS)) ADD_KEY2(KEY_BOWS);
259         else if (IS_FLG(FLG_RINGS)) ADD_KEY2(KEY_RINGS);
260         else if (IS_FLG(FLG_AMULETS)) ADD_KEY2(KEY_AMULETS);
261         else if (IS_FLG(FLG_SUITS)) ADD_KEY2(KEY_SUITS);
262         else if (IS_FLG(FLG_CLOAKS)) ADD_KEY2(KEY_CLOAKS);
263         else if (IS_FLG(FLG_HELMS)) ADD_KEY2(KEY_HELMS);
264         else if (IS_FLG(FLG_GLOVES)) ADD_KEY2(KEY_GLOVES);
265         else if (IS_FLG(FLG_BOOTS)) ADD_KEY2(KEY_BOOTS);
266
267         /* You don't need sepalator after adjective */
268         /* 'artifact' is not true adjective */
269         else if (!IS_FLG(FLG_ARTIFACT))
270                 sepa_flag = FALSE;
271
272         if (entry->name && entry->name[0])
273         {
274                 int i, j = 0;
275
276                 if (sepa_flag) strcat(buf, ":");
277
278                 i = strlen(buf);
279                 while (entry->name[j] && i < MAX_LINELEN - 2 - 1)
280                 {
281 #ifdef JP
282                         if (iskanji(entry->name[j]))
283                                 buf[i++] = entry->name[j++];
284 #endif
285                         buf[i++] = entry->name[j++];
286                 }
287                 buf[i] = '\0';
288         }
289
290         if (entry->insc)
291         {
292                 int i, j = 0;
293                 strcat(buf, "#");
294                 i = strlen(buf);
295
296                 while (entry->insc[j] && i < MAX_LINELEN - 2)
297                 {
298 #ifdef JP
299                         if (iskanji(entry->insc[j]))
300                                 buf[i++] = entry->insc[j++];
301 #endif
302                         buf[i++] = entry->insc[j++];
303                 }
304                 buf[i] = '\0';
305         }
306
307         return string_make(buf);
308 }
309
310
311 /*
312  * Reconstruct preference line from entry and kill entry
313  */
314 static cptr autopick_line_from_entry_kill(autopick_type *entry)
315 {
316         cptr ptr = autopick_line_from_entry(entry);
317
318         /* Free memory for original entry */
319         autopick_free_entry(entry);
320
321         return ptr;
322 }
323
324
325 /*
326  * A function to create new entry
327  */
328 bool autopick_new_entry(autopick_type *entry, cptr str)
329 {
330         cptr insc;
331         int i;
332         byte act = 0;
333         char buf[MAX_LINELEN];
334         cptr prev_ptr, ptr, old_ptr;
335         int prev_flg;
336
337         if (str[1] == ':') switch (str[0])
338         {
339         case '?': case '%':
340         case 'A': case 'P': case 'C':
341                 return FALSE;
342         }
343
344         entry->flag[0] = entry->flag[1] = 0L;
345         entry->dice = 0;
346
347         act = DO_AUTOPICK | DO_DISPLAY;
348         while (1)
349         {
350                 if ((act & DO_AUTOPICK) && *str == '!')
351                 {
352                         act &= ~DO_AUTOPICK;
353                         act |= DO_AUTODESTROY;
354                         str++;
355                 }
356                 else if ((act & DO_AUTOPICK) && *str == '~')
357                 {
358                         act &= ~DO_AUTOPICK;
359                         act |= DONT_AUTOPICK;
360                         str++;
361                 }
362                 else if ((act & DO_AUTOPICK) && *str == ';')
363                 {
364                         act &= ~DO_AUTOPICK;
365                         act |= DO_QUERY_AUTOPICK;
366                         str++;
367                 }
368                 else if ((act & DO_DISPLAY) && *str == '(')
369                 {
370                         act &= ~DO_DISPLAY;
371                         str++;
372                 }
373                 else
374                         break;
375         }
376
377         /* don't mind upper or lower case */
378         insc = NULL;
379         for (i = 0; *str; i++)
380         {
381                 char c = *str++;
382 #ifdef JP
383                 if (iskanji(c))
384                 {
385                         buf[i++] = c;
386                         buf[i] = *str++;
387                         continue;
388                 }
389 #endif
390                 /* Auto-inscription? */
391                 if (c == '#')
392                 {
393                         buf[i] = '\0';
394                         insc = str;
395                         break;
396                 }
397
398                 if (isupper(c)) c = tolower(c);
399
400                 buf[i] = c;
401         }
402         buf[i] = '\0';
403         
404         /* Skip empty line */
405         if (*buf == 0) return FALSE;
406
407         ptr = prev_ptr = buf;
408         old_ptr = NULL;
409
410         while (old_ptr != ptr)
411         {
412                 /* Save current location */
413                 old_ptr = ptr;
414
415                 if (MATCH_KEY(KEY_ALL)) ADD_FLG(FLG_ALL);
416                 if (MATCH_KEY(KEY_COLLECTING)) ADD_FLG(FLG_COLLECTING);
417                 if (MATCH_KEY(KEY_UNIDENTIFIED)) ADD_FLG(FLG_UNIDENTIFIED);
418                 if (MATCH_KEY(KEY_IDENTIFIED)) ADD_FLG(FLG_IDENTIFIED);
419                 if (MATCH_KEY(KEY_STAR_IDENTIFIED)) ADD_FLG(FLG_STAR_IDENTIFIED);
420                 if (MATCH_KEY(KEY_BOOSTED)) ADD_FLG(FLG_BOOSTED);
421
422                 /*** Weapons whose dd*ds is more than nn ***/
423                 if (MATCH_KEY2(KEY_MORE_THAN))
424                 {
425                         int k = 0;
426                         entry->dice = 0;
427
428                         /* Drop leading spaces */
429                         while (' ' == *ptr) ptr++;
430
431                         /* Read number */
432                         while ('0' <= *ptr && *ptr <= '9')
433                         {
434                                 entry->dice = 10 * entry->dice + (*ptr - '0');
435                                 ptr++;
436                                 k++;
437                         }
438
439                         if (k > 0 && k <= 2)
440                         {
441                                 (void)MATCH_KEY(KEY_DICE);
442                                 ADD_FLG(FLG_MORE_THAN);
443                         }
444                         else
445                                 ptr = prev_ptr;
446                 }
447
448                 /*** Items whose magical bonus is more than n ***/
449                 if (MATCH_KEY2(KEY_MORE_BONUS))
450                 {
451                         int k = 0;
452                         entry->bonus = 0;
453
454                         /* Drop leading spaces */
455                         while (' ' == *ptr) ptr++;
456
457                         /* Read number */
458                         while ('0' <= *ptr && *ptr <= '9')
459                         {
460                                 entry->bonus = 10 * entry->bonus + (*ptr - '0');
461                                 ptr++;
462                                 k++;
463                         }
464
465                         if (k > 0 && k <= 2)
466                         {
467                                 (void)MATCH_KEY(KEY_MORE_BONUS2);
468                                 ADD_FLG(FLG_MORE_BONUS);
469                         }
470                         else
471                                 ptr = prev_ptr;
472                 }
473
474                 if (MATCH_KEY(KEY_WORTHLESS)) ADD_FLG(FLG_WORTHLESS);
475                 if (MATCH_KEY(KEY_EGO)) ADD_FLG(FLG_EGO);
476                 if (MATCH_KEY(KEY_NAMELESS)) ADD_FLG(FLG_NAMELESS);
477                 if (MATCH_KEY(KEY_UNAWARE)) ADD_FLG(FLG_UNAWARE);
478                 if (MATCH_KEY(KEY_WANTED)) ADD_FLG(FLG_WANTED);
479                 if (MATCH_KEY(KEY_UNIQUE)) ADD_FLG(FLG_UNIQUE);
480                 if (MATCH_KEY(KEY_HUMAN)) ADD_FLG(FLG_HUMAN);
481                 if (MATCH_KEY(KEY_UNREADABLE)) ADD_FLG(FLG_UNREADABLE);
482                 if (MATCH_KEY(KEY_REALM1)) ADD_FLG(FLG_REALM1);
483                 if (MATCH_KEY(KEY_REALM2)) ADD_FLG(FLG_REALM2);
484                 if (MATCH_KEY(KEY_FIRST)) ADD_FLG(FLG_FIRST);
485                 if (MATCH_KEY(KEY_SECOND)) ADD_FLG(FLG_SECOND);
486                 if (MATCH_KEY(KEY_THIRD)) ADD_FLG(FLG_THIRD);
487                 if (MATCH_KEY(KEY_FOURTH)) ADD_FLG(FLG_FOURTH);
488         }
489
490         /* Not yet found any noun */
491         prev_flg = -1;
492
493         if (MATCH_KEY2(KEY_ARTIFACT)) ADD_FLG_NOUN(FLG_ARTIFACT);
494
495         if (MATCH_KEY2(KEY_ITEMS)) ADD_FLG_NOUN(FLG_ITEMS);
496         else if (MATCH_KEY2(KEY_WEAPONS)) ADD_FLG_NOUN(FLG_WEAPONS);
497         else if (MATCH_KEY2(KEY_ARMORS)) ADD_FLG_NOUN(FLG_ARMORS);
498         else if (MATCH_KEY2(KEY_MISSILES)) ADD_FLG_NOUN(FLG_MISSILES);
499         else if (MATCH_KEY2(KEY_DEVICES)) ADD_FLG_NOUN(FLG_DEVICES);
500         else if (MATCH_KEY2(KEY_LIGHTS)) ADD_FLG_NOUN(FLG_LIGHTS);
501         else if (MATCH_KEY2(KEY_JUNKS)) ADD_FLG_NOUN(FLG_JUNKS);
502         else if (MATCH_KEY2(KEY_SPELLBOOKS)) ADD_FLG_NOUN(FLG_SPELLBOOKS);
503         else if (MATCH_KEY2(KEY_HAFTED)) ADD_FLG_NOUN(FLG_HAFTED);
504         else if (MATCH_KEY2(KEY_SHIELDS)) ADD_FLG_NOUN(FLG_SHIELDS);
505         else if (MATCH_KEY2(KEY_BOWS)) ADD_FLG_NOUN(FLG_BOWS);
506         else if (MATCH_KEY2(KEY_RINGS)) ADD_FLG_NOUN(FLG_RINGS);
507         else if (MATCH_KEY2(KEY_AMULETS)) ADD_FLG_NOUN(FLG_AMULETS);
508         else if (MATCH_KEY2(KEY_SUITS)) ADD_FLG_NOUN(FLG_SUITS);
509         else if (MATCH_KEY2(KEY_CLOAKS)) ADD_FLG_NOUN(FLG_CLOAKS);
510         else if (MATCH_KEY2(KEY_HELMS)) ADD_FLG_NOUN(FLG_HELMS);
511         else if (MATCH_KEY2(KEY_GLOVES)) ADD_FLG_NOUN(FLG_GLOVES);
512         else if (MATCH_KEY2(KEY_BOOTS)) ADD_FLG_NOUN(FLG_BOOTS);
513
514         /* Last 'keyword' must be at the correct location */
515         if (*ptr == ':')
516                 ptr++;
517 #ifdef JP
518         else if (ptr[0] == kanji_colon[0] && ptr[1] == kanji_colon[1])
519                 ptr += 2;
520 #endif
521         else if (*ptr == '\0')
522                 ; /* nothing to do */
523         else
524         {
525                 /* Noun type? */
526                 if (prev_flg != -1)
527                 {
528                         /* A noun type keyword didn't end correctly */
529                         entry->flag[prev_flg/32] &= ~(1L<< (prev_flg%32));
530                         ptr = prev_ptr;
531                 }
532         }
533
534         /* Save this auto-picker entry line */
535         entry->name = string_make(ptr);
536         entry->action = act;
537         entry->insc = string_make(insc);
538
539         return TRUE;
540 }
541
542 /*
543  * A function to delete entry
544  */
545 void autopick_free_entry(autopick_type *entry)
546 {
547         string_free(entry->name);
548         string_free(entry->insc);
549 }
550
551
552 /*
553  * A function for Auto-picker/destroyer
554  * Examine whether the object matches to the entry
555  */
556 static bool is_autopick_aux(object_type *o_ptr, autopick_type *entry, cptr o_name)
557 {
558         int j;
559         cptr ptr = entry->name;
560
561         /*** Unidentified ***/
562         if (IS_FLG(FLG_UNIDENTIFIED)
563             && (object_known_p(o_ptr) || (o_ptr->ident & IDENT_SENSE)))
564                 return FALSE;
565
566         /*** Identified ***/
567         if (IS_FLG(FLG_IDENTIFIED) && !object_known_p(o_ptr))
568                 return FALSE;
569
570         /*** *Identified* ***/
571         if (IS_FLG(FLG_STAR_IDENTIFIED) &&
572             (!object_known_p(o_ptr) || !(o_ptr->ident & IDENT_MENTAL)))
573                 return FALSE;
574
575         /*** Dice boosted (weapon of slaying) ***/
576         if (IS_FLG(FLG_BOOSTED))
577         {
578                 object_kind *k_ptr = &k_info[o_ptr->k_idx];
579                         
580                 switch( o_ptr->tval )
581                 {
582                 case TV_HAFTED:
583                 case TV_POLEARM:
584                 case TV_SWORD:
585                 case TV_DIGGING:
586                         if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
587                                 break;
588                         else
589                                 return FALSE;
590                 default:
591                         return FALSE;
592                 }
593         }
594
595         /*** Weapons which dd*ds is more than nn ***/
596         if (IS_FLG(FLG_MORE_THAN))
597         {
598                 if (o_ptr->dd * o_ptr->ds < entry->dice)
599                         return FALSE;
600         }
601                                 
602         /*** Weapons whic dd*ds is more than nn ***/
603         if (IS_FLG(FLG_MORE_BONUS))
604         {
605                 if (!object_known_p(o_ptr)) return FALSE;
606
607                 if (o_ptr->pval)
608                 {
609                         if (o_ptr->pval < entry->bonus) return FALSE;
610                 }
611                 else
612                 {
613                         if (o_ptr->to_h < entry->bonus &&
614                             o_ptr->to_d < entry->bonus &&
615                             o_ptr->to_a < entry->bonus &&
616                             o_ptr->pval < entry->bonus)
617                                 return FALSE;
618                 }
619         }
620                                 
621         /*** Worthless items ***/
622         if (IS_FLG(FLG_WORTHLESS) && object_value(o_ptr) > 0)
623                 return FALSE;
624
625         /*** Artifact object ***/
626         if (IS_FLG(FLG_ARTIFACT))
627         {
628                 if (!object_known_p(o_ptr) || (!o_ptr->name1 && !o_ptr->art_name))
629                         return FALSE;
630         }
631
632         /*** Ego object ***/
633         if (IS_FLG(FLG_EGO))
634         {
635                 if (!object_known_p(o_ptr) || !o_ptr->name2)
636                         return FALSE;
637         }
638
639         /*** Nameless ***/
640         if (IS_FLG(FLG_NAMELESS))
641         {
642                 switch (o_ptr->tval)
643                 {
644                 case TV_WHISTLE:
645                 case TV_SHOT: case TV_ARROW: case TV_BOLT: case TV_BOW:
646                 case TV_DIGGING: case TV_HAFTED: case TV_POLEARM: case TV_SWORD: 
647                 case TV_BOOTS: case TV_GLOVES: case TV_HELM: case TV_CROWN:
648                 case TV_SHIELD: case TV_CLOAK:
649                 case TV_SOFT_ARMOR: case TV_HARD_ARMOR: case TV_DRAG_ARMOR:
650                 case TV_LITE: case TV_AMULET: case TV_RING: case TV_CARD:
651                         if ((!object_known_p(o_ptr) || o_ptr->inscription
652                              || o_ptr->name1 || o_ptr->name2 || o_ptr->art_name))
653                                 return FALSE;
654                         break;
655                 default:
656                         /* don't match */
657                         return FALSE;
658                 }
659         }
660
661         /*** Unaware items ***/
662         if (IS_FLG(FLG_UNAWARE) && object_aware_p(o_ptr))
663                 return FALSE;
664
665         /*** Wanted monster's corpse/skeletons ***/
666         if (IS_FLG(FLG_WANTED) &&
667             (o_ptr->tval != TV_CORPSE || !object_is_shoukinkubi(o_ptr)))
668                 return FALSE;
669
670         /*** Unique monster's corpse/skeletons/statues ***/
671         if (IS_FLG(FLG_UNIQUE) &&
672             ((o_ptr->tval != TV_CORPSE && o_ptr->tval != TV_STATUE) ||
673              !(r_info[o_ptr->pval].flags1 & RF1_UNIQUE)))
674                 return FALSE;
675
676         /*** Human corpse/skeletons (for Daemon magic) ***/
677         if (IS_FLG(FLG_HUMAN) &&
678             (o_ptr->tval != TV_CORPSE ||
679              !strchr("pht", r_info[o_ptr->pval].d_char)))
680                 return FALSE;
681
682         /*** Unreadable spellbooks ***/
683         if (IS_FLG(FLG_UNREADABLE) &&
684             (o_ptr->tval < TV_LIFE_BOOK ||
685              check_book_realm(o_ptr->tval, o_ptr->sval)))
686                 return FALSE;
687
688         /*** First realm spellbooks ***/
689         if (IS_FLG(FLG_REALM1) && 
690             (REALM1_BOOK != o_ptr->tval ||
691              p_ptr->pclass == CLASS_SORCERER ||
692              p_ptr->pclass == CLASS_RED_MAGE))
693                 return FALSE;
694
695         /*** Second realm spellbooks ***/
696         if (IS_FLG(FLG_REALM2) &&
697             (REALM2_BOOK != o_ptr->tval ||
698              p_ptr->pclass == CLASS_SORCERER ||
699              p_ptr->pclass == CLASS_RED_MAGE))
700                 return FALSE;
701
702         /*** First rank spellbooks ***/
703         if (IS_FLG(FLG_FIRST) &&
704             (o_ptr->tval < TV_LIFE_BOOK || 0 != o_ptr->sval))
705                 return FALSE;
706
707         /*** Second rank spellbooks ***/
708         if (IS_FLG(FLG_SECOND) &&
709             (o_ptr->tval < TV_LIFE_BOOK || 1 != o_ptr->sval))
710                 return FALSE;
711
712         /*** Third rank spellbooks ***/
713         if (IS_FLG(FLG_THIRD) && 
714             (o_ptr->tval < TV_LIFE_BOOK || 2 != o_ptr->sval))
715                 return FALSE;
716
717         /*** Fourth rank spellbooks ***/
718         if (IS_FLG(FLG_FOURTH) &&
719             (o_ptr->tval < TV_LIFE_BOOK || 3 != o_ptr->sval))
720                 return FALSE;
721
722         /*** Items ***/
723         if (IS_FLG(FLG_WEAPONS))
724         {
725                 switch(o_ptr->tval)
726                 {
727                 case TV_BOW: case TV_HAFTED: case TV_POLEARM:
728                 case TV_SWORD: case TV_DIGGING:
729                         break;
730                 default: return FALSE;
731                 }
732         }
733         else if (IS_FLG(FLG_ARMORS))
734         {
735                 switch(o_ptr->tval)
736                 {
737                 case TV_BOOTS: case TV_GLOVES: case TV_CLOAK: case TV_CROWN:
738                 case TV_HELM: case TV_SHIELD: case TV_SOFT_ARMOR:
739                 case TV_HARD_ARMOR: case TV_DRAG_ARMOR:
740                         break;
741                 default: return FALSE;
742                 }
743         }
744         else if (IS_FLG(FLG_MISSILES))
745         {
746                 switch(o_ptr->tval)
747                 {
748                 case TV_SHOT: case TV_BOLT: case TV_ARROW:
749                         break;
750                 default: return FALSE;
751                 }
752         }
753         else if (IS_FLG(FLG_DEVICES))
754         {
755                 switch(o_ptr->tval)
756                 {
757                 case TV_SCROLL: case TV_STAFF: case TV_WAND: case TV_ROD:
758                         break;
759                 default: return FALSE;
760                 }
761         }
762         else if (IS_FLG(FLG_LIGHTS))
763         {
764                 if (!(o_ptr->tval == TV_LITE))
765                         return FALSE;
766         }
767         else if (IS_FLG(FLG_JUNKS))
768         {
769                 switch(o_ptr->tval)
770                 {
771                 case TV_SKELETON: case TV_BOTTLE:
772                 case TV_JUNK: case TV_STATUE:
773                         break;
774                 default: return FALSE;
775                 }
776         }
777         else if (IS_FLG(FLG_SPELLBOOKS))
778         {
779                 if (!(o_ptr->tval >= TV_LIFE_BOOK))
780                         return FALSE;
781         }
782         else if (IS_FLG(FLG_HAFTED))
783         {
784                 if (!(o_ptr->tval == TV_HAFTED))
785                         return FALSE;
786         }
787         else if (IS_FLG(FLG_SHIELDS))
788         {
789                 if (!(o_ptr->tval == TV_SHIELD))
790                         return FALSE;
791         }
792         else if (IS_FLG(FLG_BOWS))
793         {
794                 if (!(o_ptr->tval == TV_BOW))
795                         return FALSE;
796         }
797         else if (IS_FLG(FLG_RINGS))
798         {
799                 if (!(o_ptr->tval == TV_RING))
800                         return FALSE;
801         }
802         else if (IS_FLG(FLG_AMULETS))
803         {
804                 if (!(o_ptr->tval == TV_AMULET))
805                         return FALSE;
806         }
807         else if (IS_FLG(FLG_SUITS))
808         {
809                 if (!(o_ptr->tval == TV_DRAG_ARMOR ||
810                       o_ptr->tval == TV_HARD_ARMOR ||
811                       o_ptr->tval == TV_SOFT_ARMOR))
812                         return FALSE;
813         }
814         else if (IS_FLG(FLG_CLOAKS))
815         {
816                 if (!(o_ptr->tval == TV_CLOAK))
817                         return FALSE;
818         }
819         else if (IS_FLG(FLG_HELMS))
820         {
821                 if (!(o_ptr->tval == TV_CROWN || o_ptr->tval == TV_HELM))
822                         return FALSE;
823         }
824         else if (IS_FLG(FLG_GLOVES))
825         {
826                 if (!(o_ptr->tval == TV_GLOVES))
827                         return FALSE;
828         }
829         else if (IS_FLG(FLG_BOOTS))
830         {
831                 if (!(o_ptr->tval == TV_BOOTS))
832                         return FALSE;
833         }
834
835         /* Keyword don't match */
836         if (*ptr == '^')
837         {
838                 ptr++;
839                 if (strncmp(o_name, ptr, strlen(ptr))) return FALSE;
840         }
841         else
842         {
843 #ifdef JP
844                 if (!strstr_j(o_name, ptr)) return FALSE;
845 #else
846                 if (!strstr(o_name, ptr)) return FALSE;
847 #endif
848         }
849
850         /* TRUE when it need not to be 'collecting' */
851         if (!IS_FLG(FLG_COLLECTING)) return TRUE;
852
853         /* Check if there is a same item */
854         for (j = 0; j < INVEN_PACK; j++)
855         {
856                 /*
857                  * 'Collecting' means the item must be absorbed 
858                  * into an inventory slot.
859                  * But an item can not be absorbed into itself!
860                  */
861                 if ((&inventory[j] != o_ptr) &&
862                     object_similar(&inventory[j], o_ptr))
863                         return TRUE;
864         }
865
866         /* Not collecting */
867         return FALSE;
868 }
869
870
871 /*
872  * A function for Auto-picker/destroyer
873  * Examine whether the object matches to the list of keywords or not.
874  */
875 int is_autopick(object_type *o_ptr)
876 {
877         int i;
878         char o_name[MAX_NLEN];
879
880         if (o_ptr->tval == TV_GOLD) return -1;
881         
882         object_desc(o_name, o_ptr, FALSE, 3);
883
884         /* Force to be lower case string */
885         for (i = 0; o_name[i]; i++)
886         {
887 #ifdef JP
888                 if (iskanji(o_name[i]))
889                         i++;
890                 else
891 #endif
892                 if (isupper(o_name[i]))
893                         o_name[i] = tolower(o_name[i]);
894         }
895         
896         for (i=0; i < max_autopick; i++)
897         {
898                 autopick_type *entry = &autopick_list[i];
899
900                 if (is_autopick_aux(o_ptr, entry, o_name)) return i;
901         }
902
903         /* No matching entry */
904         return -1;
905 }
906
907
908 /*
909  * Automatically destroy items in this grid.
910  */
911 static bool is_opt_confirm_destroy(object_type *o_ptr)
912 {
913         if (!destroy_items) return FALSE;
914
915         /* Known to be worthless? */
916         if (leave_worth)
917                 if (object_value(o_ptr) > 0) return FALSE;
918         
919         if (leave_equip)
920                 if ((o_ptr->tval >= TV_SHOT) && (o_ptr->tval <= TV_DRAG_ARMOR)) return FALSE;
921         
922         if (leave_chest)
923                 if ((o_ptr->tval == TV_CHEST) && o_ptr->pval) return FALSE;
924         
925         if (leave_wanted)
926         {
927                 if (o_ptr->tval == TV_CORPSE
928                     && object_is_shoukinkubi(o_ptr)) return FALSE;
929         }
930         
931         if (leave_corpse)
932                 if (o_ptr->tval == TV_CORPSE) return FALSE;
933         
934         if (leave_junk)
935                 if ((o_ptr->tval == TV_SKELETON) || (o_ptr->tval == TV_BOTTLE) || (o_ptr->tval == TV_JUNK) || (o_ptr->tval == TV_STATUE)) return FALSE;
936         
937         if (o_ptr->tval == TV_GOLD) return FALSE;
938         
939         return TRUE;
940 }
941
942
943 /*
944  *  Auto inscription
945  */
946 void auto_inscribe_item(int item, int idx)
947 {
948         object_type *o_ptr;
949
950         /* Get the item (in the pack) */
951         if (item >= 0) o_ptr = &inventory[item];
952
953         /* Get the item (on the floor) */
954         else o_ptr = &o_list[0 - item];
955
956         /* Auto-inscription or Re-inscribe for resistances {%} */
957         if ((idx < 0 || !autopick_list[idx].insc) && !o_ptr->inscription)
958                 return;
959
960         if (!o_ptr->inscription)
961                 o_ptr->inscription = quark_add(autopick_list[idx].insc);
962
963         if (item > INVEN_PACK)
964         {
965                 /* Redraw inscription */
966                 p_ptr->window |= (PW_EQUIP);
967
968                 /* {.} and {$} effect p_ptr->warning and TRC_TELEPORT_SELF */
969                 p_ptr->update |= (PU_BONUS);
970         }
971         else if (item >= 0)
972         {
973                 /* Redraw inscription */
974                 p_ptr->window |= (PW_INVEN);
975         }
976 }
977
978
979 /*
980  * Automatically destroy an item if it is to be destroyed
981  */
982 bool auto_destroy_item(int item, int autopick_idx)
983 {
984         bool destroy = FALSE;
985         object_type *o_ptr;
986
987         /* Don't destroy equipped items */
988         if (item > INVEN_PACK) return FALSE;
989
990         /* Get the item (in the pack) */
991         if (item >= 0) o_ptr = &inventory[item];
992
993         /* Get the item (on the floor) */
994         else o_ptr = &o_list[0 - item];
995
996         /* Easy-Auto-Destroyer */
997         if (is_opt_confirm_destroy(o_ptr)) destroy = TRUE;
998
999         /* Protected by auto-picker */
1000         if (autopick_idx >= 0 &&
1001             !(autopick_list[autopick_idx].action & DO_AUTODESTROY))
1002                 destroy = FALSE;
1003
1004         if (!always_pickup)
1005         {
1006                 /* Auto-picker/destroyer */
1007                 if (autopick_idx >= 0 &&
1008                     (autopick_list[autopick_idx].action & DO_AUTODESTROY))
1009                         destroy = TRUE;
1010         }
1011
1012         /* Not to be destroyed */
1013         if (!destroy) return FALSE;
1014
1015         /* Now decided to destroy */
1016
1017         disturb(0,0);
1018
1019         /* Artifact? */
1020         if (!can_player_destroy_object(o_ptr))
1021         {
1022                 char o_name[MAX_NLEN];
1023
1024                 /* Describe the object (with {terrible/special}) */
1025                 object_desc(o_name, o_ptr, TRUE, 3);
1026
1027                 /* Message */
1028 #ifdef JP
1029                 msg_format("%s¤ÏÇ˲õÉÔǽ¤À¡£", o_name);
1030 #else
1031                 msg_format("You cannot auto-destroy %s.", o_name);
1032 #endif
1033
1034                 /* Done */
1035                 return TRUE;
1036         }
1037
1038         /* Record name of destroyed item */
1039         COPY(&autopick_last_destroyed_object, o_ptr, object_type);
1040
1041         /* Destroy Later */
1042         o_ptr->marked |= OM_AUTODESTROY;
1043         p_ptr->notice |= PN_AUTODESTROY;
1044
1045         return TRUE;
1046 }
1047
1048
1049 /*
1050  *  Auto-destroy marked item
1051  */
1052 static void delayed_auto_destroy_aux(int item)
1053 {
1054         object_type *o_ptr;
1055
1056         /* Get the item (in the pack) */
1057         if (item >= 0) o_ptr = &inventory[item];
1058
1059         /* Get the item (on the floor) */
1060         else o_ptr = &o_list[0 - item];
1061
1062         if (o_ptr->k_idx && o_ptr->marked & OM_AUTODESTROY)
1063         {
1064                 char o_name[MAX_NLEN];
1065
1066                 /* Describe the object (with {terrible/special}) */
1067                 object_desc(o_name, o_ptr, TRUE, 3);
1068
1069                 /* Eliminate the item (from the pack) */
1070                 if (item >= 0)
1071                 {
1072                         inven_item_increase(item, -(o_ptr->number));
1073                         inven_item_optimize(item);
1074                 }
1075
1076                 /* Eliminate the item (from the floor) */
1077                 else
1078                 {
1079                         delete_object_idx(0 - item);
1080                 }
1081
1082                 /* Print a message */
1083 #ifdef JP
1084                 msg_format("%s¤ò¼«Æ°Ç˲õ¤·¤Þ¤¹¡£", o_name);
1085 #else
1086                 msg_format("Auto-destroying %s.", o_name);
1087 #endif
1088         }
1089 }
1090
1091
1092 /*
1093  *  Auto-destroy marked item in inventry and on floor
1094  */
1095 void delayed_auto_destroy(void)
1096 {
1097         int item;
1098
1099         /* 
1100          * Scan inventry in reverse order to prevent
1101          * skipping after inven_item_optimize()
1102          */
1103         for (item = INVEN_TOTAL - 1; item >= 0 ; item--)
1104                 delayed_auto_destroy_aux(item);
1105
1106         /* Scan the pile of objects */
1107         item = cave[py][px].o_idx;
1108         while (item)
1109         {
1110                 int next = o_list[item].next_o_idx;
1111                 delayed_auto_destroy_aux(-item);
1112                 item = next;
1113         }
1114 }
1115
1116
1117 /*
1118  * Automatically pickup/destroy items in this grid.
1119  */
1120 void auto_pickup_items(cave_type *c_ptr)
1121 {
1122         s16b this_o_idx, next_o_idx = 0;
1123         
1124         /* Scan the pile of objects */
1125         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1126         {
1127                 int idx;
1128         
1129                 /* Acquire object */
1130                 object_type *o_ptr = &o_list[this_o_idx];
1131                 
1132                 /* Acquire next object */
1133                 next_o_idx = o_ptr->next_o_idx;
1134
1135                 idx = is_autopick(o_ptr);
1136
1137                 /* Item index for floor -1,-2,-3,...  */
1138                 auto_inscribe_item((-this_o_idx), idx);
1139
1140                 if (idx >= 0 &&
1141                         (autopick_list[idx].action & (DO_AUTOPICK | DO_QUERY_AUTOPICK)))
1142                 {
1143                         disturb(0,0);
1144
1145                         if (!inven_carry_okay(o_ptr))
1146                         {
1147                                 char o_name[MAX_NLEN];
1148
1149                                 /* Describe the object */
1150                                 object_desc(o_name, o_ptr, TRUE, 3);
1151
1152                                 /* Message */
1153 #ifdef JP
1154                                 msg_format("¥¶¥Ã¥¯¤Ë¤Ï%s¤òÆþ¤ì¤ë·ä´Ö¤¬¤Ê¤¤¡£", o_name);
1155 #else
1156                                 msg_format("You have no room for %s.", o_name);
1157 #endif
1158                                 /* Hack - remember that the item has given a message here. */
1159                                 o_ptr->marked |= OM_NOMSG;
1160
1161                                 continue;
1162                         }
1163                         else if (autopick_list[idx].action & DO_QUERY_AUTOPICK)
1164                         {
1165                                 char out_val[MAX_NLEN+20];
1166                                 char o_name[MAX_NLEN];
1167
1168                                 if (o_ptr->marked & OM_NO_QUERY)
1169                                 {
1170                                         /* Already answered as 'No' */
1171                                         continue;
1172                                 }
1173
1174                                 /* Describe the object */
1175                                 object_desc(o_name, o_ptr, TRUE, 3);
1176
1177 #ifdef JP
1178                                 sprintf(out_val, "%s¤ò½¦¤¤¤Þ¤¹¤«? ", o_name);
1179 #else
1180                                 sprintf(out_val, "Pick up %s? ", o_name);
1181 #endif
1182
1183                                 if (!get_check(out_val))
1184                                 {
1185                                         /* Hack - remember that the item has given a message here. */
1186                                         o_ptr->marked |= (OM_NOMSG | OM_NO_QUERY);
1187                                         continue;
1188                                 }
1189
1190                         }
1191                         py_pickup_aux(this_o_idx);
1192
1193                         continue;
1194                 }
1195                 
1196                 /*
1197                  * Do auto-destroy;
1198                  * When always_pickup is 'yes', we disable
1199                  * auto-destroyer from autopick function, and do only
1200                  * easy-auto-destroyer.
1201                  */
1202                 else
1203                 {
1204                         if (auto_destroy_item((-this_o_idx), idx))
1205                                 continue;
1206                 }
1207         }
1208 }
1209
1210
1211 /*
1212  * Describe which kind of object is Auto-picked/destroyed
1213  */
1214 static void describe_autopick(char *buff, autopick_type *entry)
1215 {
1216         cptr str = entry->name;
1217         byte act = entry->action;
1218         cptr insc = entry->insc;
1219         int i;
1220
1221         bool top = FALSE;
1222
1223 #ifdef JP
1224         cptr before_str[100], body_str;
1225         int before_n = 0;
1226
1227         body_str = "¥¢¥¤¥Æ¥à";
1228
1229         /*** Collecting items ***/
1230         /*** Which can be absorbed into a slot as a bundle ***/
1231         if (IS_FLG(FLG_COLLECTING))
1232                 before_str[before_n++] = "¼ý½¸Ãæ¤Ç´û¤Ë»ý¤Ã¤Æ¤¤¤ë¥¹¥í¥Ã¥È¤Ë¤Þ¤È¤á¤é¤ì¤ë";
1233         
1234         /*** Unidentified ***/
1235         if (IS_FLG(FLG_UNIDENTIFIED))
1236                 before_str[before_n++] = "̤´ÕÄê¤Î";
1237
1238         /*** Identified ***/
1239         if (IS_FLG(FLG_IDENTIFIED))
1240                 before_str[before_n++] = "´ÕÄêºÑ¤ß¤Î";
1241
1242         /*** *Identified* ***/
1243         if (IS_FLG(FLG_STAR_IDENTIFIED))
1244                 before_str[before_n++] = "´°Á´¤Ë´ÕÄêºÑ¤ß¤Î";
1245
1246         /*** Dice boosted (weapon of slaying) ***/
1247         if (IS_FLG(FLG_BOOSTED))
1248         {
1249                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤¬Ä̾ï¤è¤êÂ礭¤¤";
1250                 body_str = "Éð´ï";
1251         }
1252
1253         /*** Weapons whose dd*ds is more than nn ***/
1254         if (IS_FLG(FLG_MORE_THAN))
1255         {
1256                 static char more_than_desc_str[] = "___";
1257                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤ÎºÇÂçÃͤ¬";
1258                 body_str = "Éð´ï";
1259                         
1260                 sprintf(more_than_desc_str,"%d", entry->dice);
1261                 before_str[before_n++] = more_than_desc_str;
1262                 before_str[before_n++] = "°Ê¾å¤Î";
1263         }
1264
1265         /*** Items whose magical bonus is more than nn ***/
1266         if (IS_FLG(FLG_MORE_BONUS))
1267         {
1268                 static char more_bonus_desc_str[] = "___";
1269                 before_str[before_n++] = "½¤ÀµÃͤ¬(+";
1270                         
1271                 sprintf(more_bonus_desc_str,"%d", entry->bonus);
1272                 before_str[before_n++] = more_bonus_desc_str;
1273                 before_str[before_n++] = ")°Ê¾å¤Î";
1274         }
1275
1276         /*** Worthless items ***/
1277         if (IS_FLG(FLG_WORTHLESS))
1278                 before_str[before_n++] = "Ź¤Ç̵²ÁÃͤÈȽÄꤵ¤ì¤ë";
1279
1280         /*** Artifact ***/
1281         if (IS_FLG(FLG_ARTIFACT))
1282         {
1283                 before_str[before_n++] = "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Î";
1284                 body_str = "ÁõÈ÷";
1285         }
1286
1287         /*** Ego ***/
1288         if (IS_FLG(FLG_EGO))
1289         {
1290                 before_str[before_n++] = "¥¨¥´¥¢¥¤¥Æ¥à¤Î";
1291                 body_str = "ÁõÈ÷";
1292         }
1293
1294         /*** Nameless ***/
1295         if (IS_FLG(FLG_NAMELESS))
1296         {
1297                 before_str[before_n++] = "¥¨¥´¤Ç¤â¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Ç¤â¤Ê¤¤";
1298                 body_str = "ÁõÈ÷";
1299         }
1300
1301         /*** Unaware items ***/
1302         if (IS_FLG(FLG_UNAWARE))
1303                 before_str[before_n++] = "̤´ÕÄê¤Ç¤½¤Î¸ú²Ì¤âȽÌÀ¤·¤Æ¤¤¤Ê¤¤";
1304
1305         /*** Wanted monster's corpse/skeletons ***/
1306         if (IS_FLG(FLG_WANTED))
1307         {
1308                 before_str[before_n++] = "¥Ï¥ó¥¿¡¼»ö̳½ê¤Ç¾Þ¶â¼ó¤È¤µ¤ì¤Æ¤¤¤ë";
1309                 body_str = "»àÂΤä¹ü";
1310         }
1311
1312         /*** Human corpse/skeletons (for Daemon magic) ***/
1313         if (IS_FLG(FLG_HUMAN))
1314         {
1315                 before_str[before_n++] = "°­ËâËâË¡¤Ç»È¤¦¤¿¤á¤Î¿Í´Ö¤ä¥Ò¥å¡¼¥Þ¥Î¥¤¥É¤Î";
1316                 body_str = "»àÂΤä¹ü";
1317         }
1318
1319         /*** Unique monster's corpse/skeletons/statues ***/
1320         if (IS_FLG(FLG_UNIQUE))
1321         {
1322                 before_str[before_n++] = "¥æ¥Ë¡¼¥¯¥â¥ó¥¹¥¿¡¼¤Î";
1323                 body_str = "»àÂΤä¹ü";
1324         }
1325
1326         /*** Unreadable spellbooks ***/
1327         if (IS_FLG(FLG_UNREADABLE))
1328         {
1329                 before_str[before_n++] = "¤¢¤Ê¤¿¤¬Æɤá¤Ê¤¤Îΰè¤Î";
1330                 body_str = "ËâË¡½ñ";
1331         }
1332
1333         /*** First realm spellbooks ***/
1334         if (IS_FLG(FLG_REALM1))
1335         {
1336                 before_str[before_n++] = "Âè°ìÎΰè¤Î";
1337                 body_str = "ËâË¡½ñ";
1338         }
1339
1340         /*** Second realm spellbooks ***/
1341         if (IS_FLG(FLG_REALM2))
1342         {
1343                 before_str[before_n++] = "ÂèÆóÎΰè¤Î";
1344                 body_str = "ËâË¡½ñ";
1345         }
1346
1347         /*** First rank spellbooks ***/
1348         if (IS_FLG(FLG_FIRST))
1349         {
1350                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î1ºýÌܤÎ";
1351                 body_str = "ËâË¡½ñ";
1352         }
1353
1354         /*** Second rank spellbooks ***/
1355         if (IS_FLG(FLG_SECOND))
1356         {
1357                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î2ºýÌܤÎ";
1358                 body_str = "ËâË¡½ñ";
1359         }
1360
1361         /*** Third rank spellbooks ***/
1362         if (IS_FLG(FLG_THIRD))
1363         {
1364                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î3ºýÌܤÎ";
1365                 body_str = "ËâË¡½ñ";
1366         }
1367
1368         /*** Fourth rank spellbooks ***/
1369         if (IS_FLG(FLG_FOURTH))
1370         {
1371                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î4ºýÌܤÎ";
1372                 body_str = "ËâË¡½ñ";
1373         }
1374
1375         /*** Items ***/
1376         if (IS_FLG(FLG_ITEMS))
1377                 ; /* Nothing to do */
1378         else if (IS_FLG(FLG_WEAPONS))
1379                 body_str = "Éð´ï";
1380         else if (IS_FLG(FLG_ARMORS))
1381                 body_str = "Ëɶñ";
1382         else if (IS_FLG(FLG_MISSILES))
1383                 body_str = "ÃƤäÌð¤ä¥¯¥í¥¹¥Ü¥¦¤ÎÌð";
1384         else if (IS_FLG(FLG_DEVICES))
1385                 body_str = "´¬Êª¤äËâË¡ËÀ¤ä¾ó¤ä¥í¥Ã¥É";
1386         else if (IS_FLG(FLG_LIGHTS))
1387                 body_str = "¸÷¸»ÍѤΥ¢¥¤¥Æ¥à";
1388         else if (IS_FLG(FLG_JUNKS))
1389                 body_str = "Àޤ줿ËÀÅù¤Î¥¬¥é¥¯¥¿";
1390         else if (IS_FLG(FLG_SPELLBOOKS))
1391                 body_str = "ËâË¡½ñ";
1392         else if (IS_FLG(FLG_HAFTED))
1393                 body_str = "Æß´ï";
1394         else if (IS_FLG(FLG_SHIELDS))
1395                 body_str = "½â";
1396         else if (IS_FLG(FLG_BOWS))
1397                 body_str = "¥¹¥ê¥ó¥°¤äµÝ¤ä¥¯¥í¥¹¥Ü¥¦";
1398         else if (IS_FLG(FLG_RINGS))
1399                 body_str = "»ØÎØ";
1400         else if (IS_FLG(FLG_AMULETS))
1401                 body_str = "¥¢¥ß¥å¥ì¥Ã¥È";
1402         else if (IS_FLG(FLG_SUITS))
1403                 body_str = "³»";
1404         else if (IS_FLG(FLG_CLOAKS))
1405                 body_str = "¥¯¥í¡¼¥¯";
1406         else if (IS_FLG(FLG_HELMS))
1407                 body_str = "¥Ø¥ë¥á¥Ã¥È¤ä´§";
1408         else if (IS_FLG(FLG_GLOVES))
1409                 body_str = "äƼê";
1410         else if (IS_FLG(FLG_BOOTS))
1411                 body_str = "¥Ö¡¼¥Ä";
1412
1413         *buff = '\0';
1414         if (!before_n) 
1415                 strcat(buff, "Á´¤Æ¤Î");
1416         else for (i = 0; i < before_n && before_str[i]; i++)
1417                 strcat(buff, before_str[i]);
1418
1419         strcat(buff, body_str);
1420
1421         if (*str)
1422         {
1423                 if (*str == '^')
1424                 {
1425                         str++;
1426                         top = TRUE;
1427                 }
1428
1429                 strcat(buff, "¤Ç¡¢Ì¾Á°¤¬¡Ö");
1430                 strncat(buff, str, 80);
1431                 if (top)
1432                         strcat(buff, "¡×¤Ç»Ï¤Þ¤ë¤â¤Î");
1433                 else
1434                         strcat(buff, "¡×¤ò´Þ¤à¤â¤Î");
1435         }
1436
1437         if (insc)
1438         {
1439                 strncat(buff, format("¤Ë¡Ö%s¡×", insc), 80);
1440
1441                 if (strstr(insc, "%%all"))
1442                         strcat(buff, "(%%all¤ÏÁ´Ç½ÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
1443                 else if (strstr(insc, "%all"))
1444                         strcat(buff, "(%all¤ÏÁ´Ç½ÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
1445                 else if (strstr(insc, "%%"))
1446                         strcat(buff, "(%%¤ÏÄɲÃǽÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
1447                 else if (strstr(insc, "%"))
1448                         strcat(buff, "(%¤ÏÄɲÃǽÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
1449
1450                 strcat(buff, "¤È¹ï¤ó¤Ç");
1451         }
1452         else
1453                 strcat(buff, "¤ò");
1454
1455         if (act & DONT_AUTOPICK)
1456                 strcat(buff, "ÊüÃÖ¤¹¤ë¡£");
1457         else if (act & DO_AUTODESTROY)
1458                 strcat(buff, "Ç˲õ¤¹¤ë¡£");
1459         else if (act & DO_QUERY_AUTOPICK)
1460                 strcat(buff, "³Îǧ¤Î¸å¤Ë½¦¤¦¡£");
1461         else
1462                 strcat(buff, "½¦¤¦¡£");
1463
1464         if (act & DO_DISPLAY)
1465         {
1466                 if (act & DONT_AUTOPICK)
1467                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'N'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1468                 else if (act & DO_AUTODESTROY)
1469                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'K'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1470                 else
1471                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'M'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1472         }
1473         else
1474                 strcat(buff, "Á´ÂΥޥåפˤÏɽ¼¨¤·¤Ê¤¤");
1475
1476 #else /* JP */
1477
1478         cptr before_str[20], after_str[20], which_str[20], whose_str[20], body_str;
1479         int before_n = 0, after_n = 0, which_n = 0, whose_n = 0;
1480
1481         body_str = "items";
1482
1483         /*** Collecting items ***/
1484         /*** Which can be absorbed into a slot as a bundle ***/
1485         if (IS_FLG(FLG_COLLECTING))
1486                 which_str[which_n++] = "can be absorbed into an existing inventory slot";
1487         
1488         /*** Unidentified ***/
1489         if (IS_FLG(FLG_UNIDENTIFIED))
1490                 before_str[before_n++] = "unidentified";
1491
1492         /*** Identified ***/
1493         if (IS_FLG(FLG_IDENTIFIED))
1494                 before_str[before_n++] = "identified";
1495
1496         /*** *Identified* ***/
1497         if (IS_FLG(FLG_STAR_IDENTIFIED))
1498                 before_str[before_n++] = "fully identified";
1499
1500         /*** Worthless items ***/
1501         if (IS_FLG(FLG_WORTHLESS))
1502         {
1503                 before_str[before_n++] = "worthless";
1504                 which_str[which_n++] = "can not be sold at stores";
1505         }
1506
1507         /*** Artifacto ***/
1508         if (IS_FLG(FLG_ARTIFACT))
1509         {
1510                 before_str[before_n++] = "artifact";
1511         }
1512
1513         /*** Ego ***/
1514         if (IS_FLG(FLG_EGO))
1515         {
1516                 before_str[before_n++] = "ego";
1517         }
1518
1519         /*** Nameless ***/
1520         if (IS_FLG(FLG_NAMELESS))
1521         {
1522                 body_str = "equipment";
1523                 which_str[which_n++] = "is neither ego-item nor artifact";
1524         }
1525
1526         /*** Unaware items ***/
1527         if (IS_FLG(FLG_UNAWARE))
1528         {
1529                 before_str[before_n++] = "unidentified";
1530                 whose_str[whose_n++] = "basic abilities are not known";
1531         }
1532
1533         /*** Dice boosted (weapon of slaying) ***/
1534         if (IS_FLG(FLG_BOOSTED))
1535         {
1536                 body_str = "weapons";
1537                 whose_str[whose_n++] = "damage dice is bigger than normal";
1538         }
1539
1540         /*** Weapons whose dd*ds is more than nn ***/
1541         if (IS_FLG(FLG_MORE_THAN))
1542         {
1543                 static char more_than_desc_str[] =
1544                         "maximum damage from dice is bigger than __";
1545                 body_str = "weapons";
1546                         
1547                 sprintf(more_than_desc_str + sizeof(more_than_desc_str) - 3,
1548                         "%d", entry->dice);
1549                 whose_str[whose_n++] = more_than_desc_str;
1550         }
1551
1552         /*** Items whose magical bonus is more than nn ***/
1553         if (IS_FLG(FLG_MORE_BONUS))
1554         {
1555                 static char more_bonus_desc_str[] =
1556                         "magical bonus is bigger than (+__)";
1557                         
1558                 sprintf(more_bonus_desc_str + sizeof(more_bonus_desc_str) - 4,
1559                         "%d)", entry->bonus);
1560                 whose_str[whose_n++] = more_bonus_desc_str;
1561         }
1562
1563         /*** Wanted monster's corpse/skeletons ***/
1564         if (IS_FLG(FLG_WANTED))
1565         {
1566                 body_str = "corpse or skeletons";
1567                 which_str[which_n++] = "is wanted at the Hunter's Office";
1568         }
1569
1570         /*** Human corpse/skeletons (for Daemon magic) ***/
1571         if (IS_FLG(FLG_HUMAN))
1572         {
1573                 before_str[before_n++] = "humanoid";
1574                 body_str = "corpse or skeletons";
1575                 which_str[which_n++] = "can be used for Daemon magic";
1576         }
1577
1578         /*** Unique monster's corpse/skeletons/statues ***/
1579         if (IS_FLG(FLG_UNIQUE))
1580         {
1581                 before_str[before_n++] = "unique monster's";
1582                 body_str = "corpse or skeletons";
1583         }
1584
1585         /*** Unreadable spellbooks ***/
1586         if (IS_FLG(FLG_UNREADABLE))
1587         {
1588                 body_str = "spellbooks";
1589                 after_str[after_n++] = "of different realms from yours";
1590         }
1591
1592         /*** First realm spellbooks ***/
1593         if (IS_FLG(FLG_REALM1))
1594         {
1595                 body_str = "spellbooks";
1596                 after_str[after_n++] = "of your first realm";
1597         }
1598
1599         /*** Second realm spellbooks ***/
1600         if (IS_FLG(FLG_REALM2))
1601         {
1602                 body_str = "spellbooks";
1603                 after_str[after_n++] = "of your second realm";
1604         }
1605
1606         /*** First rank spellbooks ***/
1607         if (IS_FLG(FLG_FIRST))
1608         {
1609                 before_str[before_n++] = "first one of four";
1610                 body_str = "spellbooks";
1611         }
1612
1613         /*** Second rank spellbooks ***/
1614         if (IS_FLG(FLG_SECOND))
1615         {
1616                 before_str[before_n++] = "second one of four";
1617                 body_str = "spellbooks";
1618         }
1619
1620         /*** Third rank spellbooks ***/
1621         if (IS_FLG(FLG_THIRD))
1622         {
1623                 before_str[before_n++] = "third one of four";
1624                 body_str = "spellbooks";
1625         }
1626
1627         /*** Fourth rank spellbooks ***/
1628         if (IS_FLG(FLG_FOURTH))
1629         {
1630                 before_str[before_n++] = "fourth one of four";
1631                 body_str = "spellbooks";
1632         }
1633
1634         /*** Items ***/
1635         if (IS_FLG(FLG_ITEMS))
1636                 ; /* Nothing to do */
1637         else if (IS_FLG(FLG_WEAPONS))
1638                 body_str = "weapons";
1639         else if (IS_FLG(FLG_ARMORS))
1640                 body_str = "armors";
1641         else if (IS_FLG(FLG_MISSILES))
1642                 body_str = "shots, arrows or crossbow bolts";
1643         else if (IS_FLG(FLG_DEVICES))
1644                 body_str = "scrolls, wands, staves or rods";
1645         else if (IS_FLG(FLG_LIGHTS))
1646                 body_str = "light sources";
1647         else if (IS_FLG(FLG_JUNKS))
1648                 body_str = "junk such as broken sticks";
1649         else if (IS_FLG(FLG_SPELLBOOKS))
1650                 body_str = "spellbooks";
1651         else if (IS_FLG(FLG_HAFTED))
1652                 body_str = "hafted weapons";
1653         else if (IS_FLG(FLG_SHIELDS))
1654                 body_str = "shields";
1655         else if (IS_FLG(FLG_BOWS))
1656                 body_str = "slings, bows or crossbows";
1657         else if (IS_FLG(FLG_RINGS))
1658                 body_str = "rings";
1659         else if (IS_FLG(FLG_AMULETS))
1660                 body_str = "amulets";
1661         else if (IS_FLG(FLG_SUITS))
1662                 body_str = "body armors";
1663         else if (IS_FLG(FLG_CLOAKS))
1664                 body_str = "cloaks";
1665         else if (IS_FLG(FLG_HELMS))
1666                 body_str = "helms or crowns";
1667         else if (IS_FLG(FLG_GLOVES))
1668                 body_str = "gloves";
1669         else if (IS_FLG(FLG_BOOTS))
1670                 body_str = "boots";
1671
1672         /* Prepare a string for item name */
1673         if (*str)
1674         {
1675                 if (*str == '^')
1676                 {
1677                         str++;
1678                         top = TRUE;
1679                         whose_str[whose_n++] = "name is beginning with \"";
1680                 }
1681                 else
1682                         which_str[which_n++] = "have \"";
1683         }
1684
1685
1686         /* Describe action flag */
1687         if (act & DONT_AUTOPICK)
1688                 strcpy(buff, "Leave on floor ");
1689         else if (act & DO_AUTODESTROY)
1690                 strcpy(buff, "Destroy ");
1691         else if (act & DO_QUERY_AUTOPICK)
1692                 strcpy(buff, "Ask to pick up ");
1693         else
1694                 strcpy(buff, "Pickup ");
1695
1696         /* Auto-insctiption */
1697         if (insc)
1698         {
1699                 strncat(buff, format("and inscribe \"%s\"", insc), 80);
1700
1701                 if (strstr(insc, "%all"))
1702                         strcat(buff, ", replacing %all with code string representing all abilities,");
1703                 else if (strstr(insc, "%"))
1704                         strcat(buff, ", replacing % with code string representing extra random abilities,");
1705
1706                 strcat(buff, " on ");
1707         }
1708
1709         /* Adjective */
1710         if (!before_n) 
1711                 strcat(buff, "all ");
1712         else for (i = 0; i < before_n && before_str[i]; i++)
1713         {
1714                 strcat(buff, before_str[i]);
1715                 strcat(buff, " ");
1716         }
1717
1718         /* Item class */
1719         strcat(buff, body_str);
1720
1721         /* of ... */
1722         for (i = 0; i < after_n && after_str[i]; i++)
1723         {
1724                 strcat(buff, " ");
1725                 strcat(buff, after_str[i]);
1726         }
1727
1728         /* which ... */
1729         for (i = 0; i < whose_n && whose_str[i]; i++)
1730         {
1731                 if (i == 0)
1732                         strcat(buff, " whose ");
1733                 else
1734                         strcat(buff, ", and ");
1735
1736                 strcat(buff, whose_str[i]);
1737         }
1738
1739         /* Item name ; whose name is beginning with "str" */
1740         if (*str && top)
1741         {
1742                 strcat(buff, str);
1743                 strcat(buff, "\"");
1744         }
1745
1746         /* whose ..., and which .... */
1747         if (whose_n && which_n)
1748                 strcat(buff, ", and ");
1749
1750         /* which ... */
1751         for (i = 0; i < which_n && which_str[i]; i++)
1752         {
1753                 if (i == 0)
1754                         strcat(buff, " which ");
1755                 else
1756                         strcat(buff, ", and ");
1757
1758                 strcat(buff, which_str[i]);
1759         }
1760
1761         /* Item name ; which have "str" as part of its name */
1762         if (*str && !top)
1763         {
1764                 strncat(buff, str, 80);
1765                 strcat(buff, "\" as part of its name");
1766         }
1767         strcat(buff, ".");
1768
1769         /* Describe whether it will be displayed on the full map or not */
1770         if (act & DO_DISPLAY)
1771         {
1772                 if (act & DONT_AUTOPICK)
1773                         strcat(buff, "  Display these items when you press the N key in the full 'M'ap.");
1774                 else if (act & DO_AUTODESTROY)
1775                         strcat(buff, "  Display these items when you press the K key in the full 'M'ap.");
1776                 else
1777                         strcat(buff, "  Display these items when you press the M key in the full 'M'ap.");
1778         }
1779         else
1780                 strcat(buff, " Not displayed in the full map.");
1781 #endif /* JP */
1782
1783 }
1784
1785
1786 #define MAX_LINES 3000
1787
1788 /*
1789  * Read whole lines of a file to memory
1790  */
1791 static cptr *read_text_lines(cptr filename, bool user)
1792 {
1793         cptr *lines_list = NULL;
1794         FILE *fff;
1795
1796         int lines = 0;
1797         char buf[1024];
1798
1799         if (user)
1800         {
1801                 /* Hack -- drop permissions */
1802                 safe_setuid_drop();
1803                 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
1804         }
1805         else
1806         {
1807                 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
1808         }
1809         
1810         /* Open the file */
1811         fff = my_fopen(buf, "r");
1812
1813         if (fff)
1814         {
1815                 /* Allocate list of pointers */
1816                 C_MAKE(lines_list, MAX_LINES, cptr);
1817
1818                 /* Parse it */
1819                 while (0 == my_fgets(fff, buf, sizeof(buf)))
1820                 {
1821                         lines_list[lines++] = string_make(buf);
1822                         if (lines >= MAX_LINES - 1) break;
1823                 }
1824                 if (lines == 0)
1825                         lines_list[0] = string_make("");
1826
1827                 my_fclose(fff);
1828         }
1829
1830         /* Grab priv's */
1831         safe_setuid_grab();
1832
1833         if (!fff) return NULL;
1834         return lines_list;
1835 }
1836
1837
1838 #define PT_DEFAULT 0
1839 #define PT_WITH_PNAME 1
1840
1841 static cptr *read_pickpref_text_lines(int *filename_mode_p)
1842 {
1843         char buf[1024];
1844         cptr *lines_list;
1845
1846 #ifdef JP
1847         sprintf(buf, "picktype-%s.prf", player_name);
1848 #else
1849         sprintf(buf, "pickpref-%s.prf", player_name);
1850 #endif
1851         lines_list = read_text_lines(buf, TRUE);
1852
1853         if (!lines_list)
1854         {
1855 #ifdef JP
1856                 lines_list = read_text_lines("picktype.prf", TRUE);
1857 #else
1858                 lines_list = read_text_lines("pickpref.prf", TRUE);
1859 #endif
1860                 *filename_mode_p = PT_DEFAULT;
1861         }
1862
1863         if (!lines_list)
1864         {
1865 #ifdef JP
1866                 lines_list = read_text_lines("picktype.prf", FALSE);
1867 #else
1868                 lines_list = read_text_lines("pickpref.prf", FALSE);
1869 #endif
1870                 *filename_mode_p = PT_WITH_PNAME;
1871         }
1872
1873         if (!lines_list)
1874         {
1875                 /* Allocate list of pointers */
1876                 C_MAKE(lines_list, MAX_LINES, cptr);
1877                 lines_list[0] = string_make("");
1878                 *filename_mode_p = PT_WITH_PNAME;
1879         }
1880         return lines_list;
1881 }
1882
1883 /*
1884  * Write whole lines of memory to a file.
1885  */
1886 static bool write_text_lines(cptr filename, cptr *lines_list)
1887 {
1888         FILE *fff;
1889
1890         int lines = 0;
1891         char buf[1024];
1892
1893         /* Hack -- drop permissions */
1894         safe_setuid_drop();
1895
1896         /* Build the filename */
1897         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
1898         
1899         /* Open the file */
1900         fff = my_fopen(buf, "w");
1901         if (fff)
1902         {
1903                 for (lines = 0; lines_list[lines]; lines++)
1904                         my_fputs(fff, lines_list[lines], 1024);
1905
1906                 my_fclose(fff);
1907         }
1908
1909         /* Grab priv's */
1910         safe_setuid_grab();
1911
1912         if (!fff) return FALSE;
1913         return TRUE;
1914 }
1915
1916
1917 /*
1918  * Free memory of lines_list.
1919  */
1920 static void free_text_lines(cptr *lines_list)
1921 {
1922         int lines;
1923
1924         for (lines = 0; lines_list[lines]; lines++)
1925                 string_free(lines_list[lines]);
1926
1927         /* free list of pointers */
1928         C_FREE((char **)lines_list, MAX_LINES, char *);
1929 }
1930
1931
1932 /*
1933  * Delete or insert string
1934  */
1935 static void toggle_string(cptr *lines_list, int flg, int y)
1936 {
1937         autopick_type an_entry, *entry = &an_entry;
1938
1939         if (!autopick_new_entry(entry, lines_list[y]))
1940                 return;
1941
1942         string_free(lines_list[y]);
1943         if (IS_FLG(flg)) 
1944                 REM_FLG(flg);
1945         else
1946                 ADD_FLG(flg);
1947
1948         lines_list[y] = autopick_line_from_entry_kill(entry);
1949 }
1950
1951 /*
1952  * Insert return code and split the line
1953  */
1954 static bool insert_return_code(cptr *lines_list, int cx, int cy)
1955 {
1956         char buf[MAX_LINELEN];
1957         int i, j, k;
1958
1959         for (k = 0; lines_list[k]; k++)
1960                 /* count number of lines */ ;
1961
1962         if (k >= MAX_LINES - 2) return FALSE;
1963         k--;
1964
1965         /* Move down lines */
1966         for (; cy < k; k--)
1967                 lines_list[k+1] = lines_list[k];
1968
1969         /* Split current line */
1970         for (i = j = 0; lines_list[cy][i] && i < cx; i++)
1971         {
1972 #ifdef JP
1973                 if (iskanji(lines_list[cy][i]))
1974                         buf[j++] = lines_list[cy][i++];
1975 #endif
1976                 buf[j++] = lines_list[cy][i];
1977         }
1978         buf[j] = '\0';
1979         lines_list[cy+1] = string_make(&lines_list[cy][i]);
1980         string_free(lines_list[cy]);
1981         lines_list[cy] = string_make(buf);
1982         return TRUE;
1983 }
1984
1985
1986 /*
1987  * Get auto-picker entry from o_ptr.
1988  */
1989 void autopick_entry_from_object(autopick_type *entry, object_type *o_ptr)
1990 {
1991         char o_name[MAX_NLEN];
1992         object_desc(o_name, o_ptr, FALSE, 0);
1993
1994         entry->name = string_make(o_name);
1995         entry->insc = string_make(quark_str(o_ptr->inscription));
1996         entry->action = DO_AUTOPICK | DO_DISPLAY;
1997         entry->flag[0] = entry->flag[1] = 0L;
1998         entry->dice = 0;
1999
2000         if (!object_aware_p(o_ptr))
2001                 ADD_FLG(FLG_UNAWARE);
2002         if (object_value(o_ptr) <= 0)
2003                 ADD_FLG(FLG_WORTHLESS);
2004
2005         if (object_known_p(o_ptr))
2006         {
2007                 if (o_ptr->name2)
2008                         ADD_FLG(FLG_EGO);
2009                 else if (o_ptr->name1 || o_ptr->art_name)
2010                         ADD_FLG(FLG_ARTIFACT);
2011         }
2012
2013         switch(o_ptr->tval)
2014         {
2015                 object_kind *k_ptr; 
2016         case TV_HAFTED: case TV_POLEARM: case TV_SWORD: case TV_DIGGING:
2017                 k_ptr = &k_info[o_ptr->k_idx];
2018                 if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
2019                         ADD_FLG(FLG_BOOSTED);
2020         }
2021
2022         if (o_ptr->tval == TV_CORPSE && object_is_shoukinkubi(o_ptr))
2023         {
2024                 REM_FLG(FLG_WORTHLESS);
2025                 ADD_FLG(FLG_WANTED);
2026         }
2027
2028         if ((o_ptr->tval == TV_CORPSE || o_ptr->tval == TV_STATUE)
2029             && (r_info[o_ptr->pval].flags1 & RF1_UNIQUE))
2030         {
2031                 REM_FLG(FLG_WORTHLESS);
2032                 ADD_FLG(FLG_UNIQUE);
2033         }
2034
2035         if (o_ptr->tval == TV_CORPSE && strchr("pht", r_info[o_ptr->pval].d_char))
2036         {
2037                 REM_FLG(FLG_WORTHLESS);
2038                 ADD_FLG(FLG_HUMAN);
2039         }
2040
2041         if (o_ptr->tval >= TV_LIFE_BOOK &&
2042             !check_book_realm(o_ptr->tval, o_ptr->sval))
2043                 ADD_FLG(FLG_UNREADABLE);
2044
2045         if (REALM1_BOOK == o_ptr->tval &&
2046             p_ptr->pclass != CLASS_SORCERER &&
2047             p_ptr->pclass != CLASS_RED_MAGE)
2048                 ADD_FLG(FLG_REALM1);
2049
2050         if (REALM2_BOOK == o_ptr->tval &&
2051             p_ptr->pclass != CLASS_SORCERER &&
2052             p_ptr->pclass != CLASS_RED_MAGE)
2053                 ADD_FLG(FLG_REALM2);
2054
2055         if (o_ptr->tval >= TV_LIFE_BOOK && 0 == o_ptr->sval)
2056                 ADD_FLG(FLG_FIRST);
2057         if (o_ptr->tval >= TV_LIFE_BOOK && 1 == o_ptr->sval)
2058                 ADD_FLG(FLG_SECOND);
2059         if (o_ptr->tval >= TV_LIFE_BOOK && 2 == o_ptr->sval)
2060                 ADD_FLG(FLG_THIRD);
2061         if (o_ptr->tval >= TV_LIFE_BOOK && 3 == o_ptr->sval)
2062                 ADD_FLG(FLG_FOURTH);
2063
2064         if (o_ptr->tval == TV_SHOT || o_ptr->tval == TV_BOLT
2065                  || o_ptr->tval == TV_ARROW)
2066                 ADD_FLG(FLG_MISSILES);
2067         else if (o_ptr->tval == TV_SCROLL || o_ptr->tval == TV_STAFF
2068                  || o_ptr->tval == TV_WAND || o_ptr->tval == TV_ROD)
2069                 ADD_FLG(FLG_DEVICES);
2070         else if (o_ptr->tval == TV_LITE)
2071                 ADD_FLG(FLG_LIGHTS);
2072         else if (o_ptr->tval == TV_SKELETON || o_ptr->tval == TV_BOTTLE
2073                  || o_ptr->tval == TV_JUNK || o_ptr->tval == TV_STATUE)
2074                 ADD_FLG(FLG_JUNKS);
2075         else if (o_ptr->tval >= TV_LIFE_BOOK)
2076                 ADD_FLG(FLG_SPELLBOOKS);
2077         else if (o_ptr->tval == TV_HAFTED)
2078                 ADD_FLG(FLG_HAFTED);
2079         else if (o_ptr->tval == TV_POLEARM || o_ptr->tval == TV_SWORD
2080                  || o_ptr->tval == TV_DIGGING)
2081                 ADD_FLG(FLG_WEAPONS);
2082         else if (o_ptr->tval == TV_SHIELD)
2083                 ADD_FLG(FLG_SHIELDS);
2084         else if (o_ptr->tval == TV_BOW)
2085                 ADD_FLG(FLG_BOWS);
2086         else if (o_ptr->tval == TV_RING)
2087                 ADD_FLG(FLG_RINGS);
2088         else if (o_ptr->tval == TV_AMULET)
2089                 ADD_FLG(FLG_AMULETS);
2090         else if (o_ptr->tval == TV_DRAG_ARMOR || o_ptr->tval == TV_HARD_ARMOR ||
2091                  o_ptr->tval == TV_SOFT_ARMOR)
2092                 ADD_FLG(FLG_SUITS);
2093         else if (o_ptr->tval == TV_CLOAK)
2094                 ADD_FLG(FLG_CLOAKS);
2095         else if (o_ptr->tval == TV_HELM)
2096                 ADD_FLG(FLG_HELMS);
2097         else if (o_ptr->tval == TV_GLOVES)
2098                 ADD_FLG(FLG_GLOVES);
2099         else if (o_ptr->tval == TV_BOOTS)
2100                 ADD_FLG(FLG_BOOTS);
2101
2102         return;
2103 }
2104
2105
2106 /*
2107  * Choose an item and get auto-picker entry from it.
2108  */
2109 static object_type *choose_object(cptr q, cptr s)
2110 {
2111         int item;
2112
2113         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR | USE_EQUIP))) return NULL;
2114
2115         /* Get the item (in the pack) */
2116         if (item >= 0) return &inventory[item];
2117
2118         /* Get the item (on the floor) */
2119         else return &o_list[0 - item];
2120 }
2121
2122
2123 /*
2124  * Choose an item and get auto-picker entry from it.
2125  */
2126 static bool entry_from_choosed_object(autopick_type *entry)
2127 {
2128         object_type *o_ptr;
2129         cptr q, s;
2130
2131         /* Get an item */
2132 #ifdef JP
2133         q = "¤É¤Î¥¢¥¤¥Æ¥à¤òÅÐÏ¿¤·¤Þ¤¹¤«? ";
2134         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
2135 #else
2136         q = "Entry which item? ";
2137         s = "You have nothing to entry.";
2138 #endif
2139         o_ptr = choose_object(q, s);
2140         if (!o_ptr) return FALSE;
2141
2142         autopick_entry_from_object(entry, o_ptr);
2143         return TRUE;
2144 }
2145
2146
2147 /*
2148  * Choose an item or string for search
2149  */
2150 static bool get_string_for_search(object_type **o_handle, cptr *search_strp)
2151 {
2152         int pos = 0;
2153         cptr q, s;
2154         char buf[MAX_NLEN+20];
2155
2156 #ifdef JP
2157         int k_flag[MAX_NLEN+20];
2158         char prompt[] = "¸¡º÷(^I:»ý¤Áʪ ^L:Ç˲õ¤µ¤ì¤¿Êª): ";
2159 #else
2160         char prompt[] = "Search key(^I:inven ^L:destroyed): ";
2161 #endif
2162         int col = sizeof(prompt) - 1;
2163
2164         if (*search_strp) strcpy(buf, *search_strp);
2165         else buf[0] = '\0';
2166
2167         /* Display prompt */
2168         prt(prompt, 0, 0);
2169
2170         /* Display the default answer */
2171         Term_erase(col, 0, 255);
2172         Term_putstr(col, 0, -1, TERM_YELLOW, buf);
2173
2174         /* Process input */
2175         while (1)
2176         {
2177                 object_type *o_ptr;
2178                 int i;
2179
2180                 /* Place cursor */
2181                 Term_gotoxy(col + pos, 0);
2182
2183                 /* Do not process macros except special keys */
2184                 inkey_special = TRUE;
2185
2186                 /* Get a key */
2187                 i = inkey();
2188
2189                 /* Analyze the key */
2190                 switch (i)
2191                 {
2192                 case ESCAPE:
2193                         pos = 0;
2194                         return FALSE;
2195
2196                 case '\n':
2197                 case '\r':
2198                         if (!pos && *o_handle) return TRUE;
2199                         string_free(*search_strp);
2200                         *search_strp = string_make(buf);
2201                         *o_handle = NULL;
2202                         return TRUE;
2203
2204                 case KTRL('i'):
2205                         /* Get an item */
2206 #ifdef JP
2207                         q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò¸¡º÷¤·¤Þ¤¹¤«? ";
2208                         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
2209 #else
2210                         q = "Entry which item? ";
2211                         s = "You have nothing to entry.";
2212 #endif
2213                         o_ptr = choose_object(q, s);
2214                         if (!o_ptr) return FALSE;
2215
2216                         *o_handle = o_ptr;
2217
2218                         string_free(*search_strp);
2219                         object_desc(buf, *o_handle, FALSE, 3);
2220                         *search_strp = string_make(format("<%s>", buf));
2221                         return TRUE;
2222
2223                 case KTRL('l'):
2224                         if (!autopick_last_destroyed_object.k_idx) break;
2225                         *o_handle = &autopick_last_destroyed_object;
2226
2227                         string_free(*search_strp);
2228                         object_desc(buf, *o_handle, FALSE, 3);
2229                         *search_strp = string_make(format("<%s>", buf));
2230                         return TRUE;
2231
2232                 case 0x7F:
2233                 case '\010':
2234 #ifdef JP
2235                         if (pos > 0)
2236                         {
2237                                 pos--;
2238                                 if (k_flag[pos]) pos--;
2239                         }
2240 #else
2241                         if (pos > 0) pos--;
2242 #endif
2243                         break;
2244
2245                 default:
2246 #ifdef JP
2247                         if (iskanji(i))
2248                         {
2249                                 int next;
2250
2251                                 inkey_base = TRUE;
2252                                 next = inkey ();
2253                                 if (pos + 1 < MAX_NLEN)
2254                                 {
2255                                         buf[pos++] = i;
2256                                         buf[pos] = next;
2257                                         k_flag[pos++] = 1;
2258                                 }
2259                                 else bell();
2260                         }
2261                         else if (pos < MAX_NLEN && isprint(i))
2262                         {
2263                                 buf[pos] = i;
2264                                 k_flag[pos++] = 0;
2265                         }
2266                         else bell();
2267 #else
2268                         if (pos < MAX_NLEN && isprint(i)) buf[pos++] = i;
2269                         else bell();
2270 #endif
2271                         break;
2272                 }
2273
2274                 /* Terminate */
2275                 buf[pos] = '\0';
2276
2277                 /* Update the entry */
2278                 Term_erase(col, 0, 255);
2279                 Term_putstr(col, 0, -1, TERM_WHITE, buf);
2280         }
2281
2282         /* Never reached */
2283 }
2284
2285
2286 /*
2287  * Search next line matches for o_ptr
2288  */
2289 static bool search_for_object(cptr *lines_list, object_type *o_ptr, int *cxp, int *cyp, bool forward)
2290 {
2291         int i;
2292         autopick_type an_entry, *entry = &an_entry;
2293         char o_name[MAX_NLEN];
2294
2295         object_desc(o_name, o_ptr, FALSE, 3);
2296
2297         /* Force to be lower case string */
2298         for (i = 0; o_name[i]; i++)
2299         {
2300 #ifdef JP
2301                 if (iskanji(o_name[i]))
2302                         i++;
2303                 else
2304 #endif
2305                 if (isupper(o_name[i]))
2306                         o_name[i] = tolower(o_name[i]);
2307         }
2308         
2309         i = *cyp;
2310
2311         while (1)
2312         {
2313                 if (forward)
2314                 {
2315                         if (!lines_list[++i]) break;
2316                 }
2317                 else
2318                 {
2319                         if (--i < 0) break;
2320                 }
2321
2322                 if (!autopick_new_entry(entry, lines_list[i])) continue;
2323
2324                 if (is_autopick_aux(o_ptr, entry, o_name))
2325                 {
2326                         *cxp = 0;
2327                         *cyp = i;
2328                         return TRUE;
2329                 }
2330         }
2331
2332         return FALSE;
2333 }
2334
2335
2336 /*
2337  * Search next line matches to the string
2338  */
2339 static bool search_for_string(cptr *lines_list, cptr search_str, int *cxp, int *cyp, bool forward)
2340 {
2341         int i = *cyp;
2342
2343         while (1)
2344         {
2345                 cptr pos;
2346
2347                 if (forward)
2348                 {
2349                         if (!lines_list[++i]) break;
2350                 }
2351                 else
2352                 {
2353                         if (--i < 0) break;
2354                 }
2355 #ifdef JP
2356                 pos = strstr_j(lines_list[i], search_str);
2357 #else
2358                 pos = strstr(lines_list[i], search_str);
2359 #endif
2360                 if (pos)
2361                 {
2362                         *cxp = (int)(pos - lines_list[i]);
2363                         *cyp = i;
2364                         return TRUE;
2365                 }
2366         }
2367
2368         return FALSE;
2369 }
2370
2371
2372 /*
2373  * Initialize auto-picker preference
2374  */
2375 void init_autopicker(void)
2376 {
2377         static const char easy_autopick_inscription[] = "(:=g";
2378         autopick_type entry;
2379         int i;
2380
2381         /* Clear old entries */
2382         for( i = 0; i < max_autopick; i++)
2383                 autopick_free_entry(&autopick_list[i]);
2384
2385         max_autopick = 0;
2386
2387         /* There is always one entry "=g" */
2388         autopick_new_entry(&entry, easy_autopick_inscription);
2389         autopick_list[max_autopick++] = entry;
2390 }
2391
2392
2393
2394 /*
2395  *  Process line for auto picker/destroyer.
2396  */
2397 errr process_pickpref_file_line(char *buf)
2398 {
2399         autopick_type entry;
2400         int i;
2401
2402         if (max_autopick == MAX_AUTOPICK)
2403                 return 1;
2404         
2405         /* Nuke illegal char */
2406         for(i = 0; buf[i]; i++)
2407         {
2408 #ifdef JP
2409                 if (iskanji(buf[i]))
2410                 {
2411                         i++;
2412                         continue;
2413                 }
2414 #endif
2415                 if (isspace(buf[i]) && buf[i] != ' ')
2416                         break;
2417         }
2418         buf[i] = 0;
2419         
2420         if (!autopick_new_entry(&entry, buf)) return 0;
2421
2422         /* Already has the same entry? */ 
2423         for(i = 0; i < max_autopick; i++)
2424                 if(!strcmp(entry.name, autopick_list[i].name)
2425                    && entry.flag[0] == autopick_list[i].flag[0]
2426                    && entry.flag[1] == autopick_list[i].flag[1]
2427                    && entry.dice == autopick_list[i].dice
2428                    && entry.bonus == autopick_list[i].bonus) return 0;
2429
2430         autopick_list[max_autopick++] = entry;
2431         return 0;
2432 }
2433
2434
2435 /*
2436  * Get a trigger key and insert ASCII string for the trigger
2437  */
2438 static bool insert_macro_line(cptr *lines_list, int cy)
2439 {
2440         char tmp[1024];
2441         char buf[1024];
2442         int i, n = 0;
2443
2444         /* Flush */
2445         flush();
2446
2447         /* Do not process macros */
2448         inkey_base = TRUE;
2449
2450         /* First key */
2451         i = inkey();
2452
2453         /* Read the pattern */
2454         while (i)
2455         {
2456                 /* Save the key */
2457                 buf[n++] = i;
2458
2459                 /* Do not process macros */
2460                 inkey_base = TRUE;
2461
2462                 /* Do not wait for keys */
2463                 inkey_scan = TRUE;
2464
2465                 /* Attempt to read a key */
2466                 i = inkey();
2467         }
2468
2469         /* Terminate */
2470         buf[n] = '\0';
2471
2472         /* Flush */
2473         flush();
2474
2475         /* Convert the trigger */
2476         ascii_to_text(tmp, buf);
2477
2478         /* Null */
2479         if(!tmp[0]) return FALSE;
2480
2481         /* Insert preference string */
2482         insert_return_code(lines_list, 0, cy);
2483         string_free(lines_list[cy]);
2484         lines_list[cy] = string_make(format("P:%s", tmp));
2485
2486         /* Insert blank action preference line */
2487         insert_return_code(lines_list, 0, cy);
2488         string_free(lines_list[cy]);
2489         lines_list[cy] = string_make("A:");
2490
2491         return TRUE;
2492 }
2493
2494
2495 /*
2496  * Get a command key and insert ASCII string for the key
2497  */
2498 static bool insert_keymap_line(cptr *lines_list, int cy)
2499 {
2500         char tmp[1024];
2501         char buf[2];
2502         int mode;
2503
2504         /* Roguelike */
2505         if (rogue_like_commands)
2506         {
2507                 mode = KEYMAP_MODE_ROGUE;
2508         }
2509
2510         /* Original */
2511         else
2512         {
2513                 mode = KEYMAP_MODE_ORIG;
2514         }
2515
2516         /* Flush */
2517         flush();
2518
2519         /* Get a key */
2520         buf[0] = inkey();
2521         buf[1] = '\0';
2522
2523         /* Flush */
2524         flush();
2525
2526         /* Convert the trigger */
2527         ascii_to_text(tmp, buf);
2528
2529         /* Null */
2530         if(!tmp[0]) return FALSE;
2531
2532         /* Insert preference string */
2533         insert_return_code(lines_list, 0, cy);
2534         string_free(lines_list[cy]);
2535         lines_list[cy] = string_make(format("C:%d:%s", mode, tmp));
2536
2537         /* Insert blank action preference line */
2538         insert_return_code(lines_list, 0, cy);
2539         string_free(lines_list[cy]);
2540         lines_list[cy] = string_make("A:");
2541
2542         return TRUE;
2543 }
2544
2545
2546 /*
2547  * Description of control commands
2548  */
2549
2550 #define WID_DESC 31
2551
2552 static cptr ctrl_command_desc[] =
2553 {
2554 #ifdef JP
2555 #define LAST_DESTROYED 6
2556         "^P ^N ^B ^F ¾å²¼º¸±¦¤Ë°ÜÆ°",
2557         "^A ^E ¹Ô¤ÎÀèƬ¡¢½ªÃ¼",
2558         "^Q ÆþÎÏ/¥³¥Þ¥ó¥É¥â¡¼¥ÉÀÚ¤êÂؤ¨",
2559         "^R Êѹ¹¤òÁ´¤Æ¼è¤ê¾Ã¤·¤Æ¸µ¤ËÌ᤹",
2560         "------------------------------------",
2561         "^I »ý¤Áʪ/ÁõÈ÷¤«¤éÁªÂò",
2562         "^L",
2563         "^K ¥«¡¼¥½¥ë¤«¤é½ªÃ¼¤Þ¤Çºï½ü",
2564         "^Y ºï½ü(^K)¤·¤¿¹Ô¤òÁÞÆþ",
2565         "^C ¼ï²¡¢¿¦¶È¤Î¾ò·ï¼°¤òÁÞÆþ",
2566         "------------------------------------",
2567         "^S Êѹ¹ (!Ç˲õ/~ÊüÃÖ/½¦¤¦)",
2568         "^G \"(\" Á´ÂΥޥåפÇɽ¼¨¤·¤Ê¤¤",
2569         "^O \"#\" ¼«Æ°¹ï¤ß",
2570         "------------------------------------",
2571         "^U Ì¤´ÕÄê/̤ȽÌÀ/´ÕÄê/*´ÕÄê*",
2572         "^W \"̵²ÁÃͤÎ\"",
2573         "^X ÌµÌÃ/¥¨¥´/¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È",
2574         "^Z \"¼ý½¸Ãæ¤Î\"",
2575         NULL
2576 #else
2577 #define LAST_DESTROYED 6
2578         "^P ^N ^B ^F Move Cursor",
2579         "^A ^E Beginning and End of Line",
2580         "^Q Toggle Insert/Command mode",
2581         "^R Revert to Original File",
2582         "------------------------------------",
2583         "^I Object in Inventry/Equipment",
2584         "^L",
2585         "^K Kill Rest of Line",
2586         "^Y Insert killed(^K) text",
2587         "^C Insert conditional expression",
2588         "------------------------------------",
2589         "^S Toggle(!Destroy/~Leave/Pick)",
2590         "^G \"(\" No display in the 'M'ap",
2591         "^O \"#\" Auto-Inscribe",
2592         "------------------------------------",
2593         "^U Toggle 'identified' state",
2594         "^W \"worthless\"",
2595         "^X Toggle nameless/ego/artifact",
2596         "^Z \"collecting\"",
2597         NULL
2598 #endif
2599 };
2600
2601
2602 #define MAX_YANK MAX_LINELEN
2603 #define DIRTY_ALL 0x01
2604 #define DIRTY_COMMAND 0x02
2605 #define DIRTY_MODE 0x04
2606 #define DIRTY_SCREEN 0x08
2607 #define DIRTY_NOT_FOUND 0x10
2608 #define DIRTY_NO_SEARCH 0x20
2609
2610 /*
2611  * In-game editor of Object Auto-picker/Destoryer
2612  */
2613 void do_cmd_edit_autopick(void)
2614 {
2615         static int cx = 0, cy = 0;
2616         static int upper = 0, left = 0;
2617
2618         object_type *search_o_ptr = NULL;
2619         cptr search_str = NULL;
2620         cptr last_destroyed = NULL;
2621         char last_destroyed_command[WID_DESC+3];
2622         char yank_buf[MAX_YANK];
2623         char classrace[80];
2624         autopick_type an_entry, *entry = &an_entry;
2625         char buf[MAX_LINELEN];
2626         cptr *lines_list;
2627         int filename_mode = PT_WITH_PNAME;
2628
2629         int i, j, k, len;
2630         cptr tmp;
2631
2632         int old_upper = -1, old_left = -1;
2633         int old_cy = -1;
2634         int key = -1, old_key;
2635         bool repeated_clearing = FALSE;
2636         bool edit_mode = FALSE;
2637
2638         byte dirty_flags = DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE;
2639         int dirty_line = -1;
2640
2641         int wid, hgt, old_wid = -1, old_hgt = -1;
2642
2643         static s32b old_autosave_turn = 0L;
2644
2645         /* Autosave */
2646         if (turn > old_autosave_turn + 100L)
2647         {
2648                 do_cmd_save_game(TRUE);
2649                 old_autosave_turn = turn;
2650         }
2651
2652         /* HACK -- Reset start_time to stop counting playtime while edit */
2653         update_playtime();
2654
2655         /* Free old entries */
2656         init_autopicker();
2657
2658         /* Command Description of the 'Last Destroyed Item' */
2659         if (autopick_last_destroyed_object.k_idx)
2660         {
2661                 autopick_entry_from_object(entry, &autopick_last_destroyed_object);
2662                 last_destroyed = autopick_line_from_entry_kill(entry);
2663
2664                 my_strcpy(last_destroyed_command, format("^L \"%s\"", last_destroyed), sizeof(last_destroyed_command));
2665         }
2666         else
2667         {
2668 #ifdef JP
2669                 strcpy(last_destroyed_command, "^L ºÇ¸å¤Ë¼«Æ°Ç˲õ¤·¤¿¥¢¥¤¥Æ¥à̾");
2670 #else
2671                 strcpy(last_destroyed_command, "^L Last destroyed object");
2672 #endif
2673         }
2674         ctrl_command_desc[LAST_DESTROYED] = last_destroyed_command;
2675
2676         /* Conditional Expression for Class and Race */
2677         sprintf(classrace, "?:[AND [EQU $RACE %s] [EQU $CLASS %s]]", 
2678 #ifdef JP
2679                 rp_ptr->E_title, cp_ptr->E_title
2680 #else
2681                 rp_ptr->title, cp_ptr->title
2682 #endif
2683                 );
2684
2685         /* Clear yank buffer */
2686         yank_buf[0] = '\0';
2687
2688         /* Read or initialize whole text */
2689         lines_list = read_pickpref_text_lines(&filename_mode);
2690
2691         /* Reset cursor position if needed */
2692         for (i = 0; i < cy; i++)
2693         {
2694                 if (!lines_list[i])
2695                 {
2696                         cy = cx = 0;
2697                         break;
2698                 }
2699         }
2700
2701         /* Save the screen */
2702         screen_save();
2703
2704         /* Process requests until done */
2705         while (1)
2706         {
2707                 /* Get size */
2708                 Term_get_size(&wid, &hgt);
2709
2710 #ifdef JP
2711                 /* Don't let cursor at second byte of kanji */
2712                 for (i = 0; lines_list[cy][i]; i++)
2713                         if (iskanji(lines_list[cy][i]))
2714                         {
2715                                 i++;
2716                                 if (i == cx)
2717                                 {
2718                                         cx--;
2719                                         break;
2720                                 }
2721                         }
2722 #endif
2723
2724                 /* Scroll if necessary */
2725                 if (cy < upper || upper + hgt - 4 <= cy)
2726                         upper = cy - (hgt-4)/2;
2727                 if (upper < 0)
2728                         upper = 0;
2729                 if ((cx < left + 10 && left > 0) || left + wid - WID_DESC - 5 <= cx)
2730                         left = cx - (wid - WID_DESC)*2/3;
2731                 if (left < 0)
2732                         left = 0;
2733
2734                 /* Redraw whole window after resize */
2735                 if (old_wid != wid || old_hgt != hgt)
2736                         dirty_flags |= DIRTY_SCREEN;
2737
2738                 /* Redraw all text after scroll */
2739                 else if (old_upper != upper || old_left != left)
2740                         dirty_flags |= DIRTY_ALL;
2741
2742
2743                 if (dirty_flags & DIRTY_SCREEN)
2744                 {
2745                         dirty_flags |= (DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE);
2746
2747                         /* Clear screen */
2748                         Term_clear();
2749                 }
2750
2751                 if (dirty_flags & DIRTY_COMMAND)
2752                 {
2753                         /* Display control command */
2754                         for (i = 0; ctrl_command_desc[i]; i++)
2755                                 Term_putstr(wid - WID_DESC, i + 1, WID_DESC, TERM_WHITE, ctrl_command_desc[i]);
2756                 }
2757
2758                 /* Redraw mode line */
2759                 if (dirty_flags & DIRTY_MODE)
2760                 {
2761                         int sepa_length = wid - WID_DESC;
2762
2763                         /* Separator */
2764                         for (i = 0; i < sepa_length; i++)
2765                                 buf[i] = '-';
2766                         buf[i] = '\0';
2767
2768                         /* Mode line */
2769                         if (edit_mode)
2770                                 strncpy(buf + sepa_length - 21, " (INSERT MODE)  ", 16);
2771                         else
2772                                 strncpy(buf + sepa_length - 21, " (COMMAND MODE) ", 16);
2773
2774                         Term_putstr(0, hgt - 3, sepa_length, (byte) (edit_mode ? TERM_YELLOW : TERM_WHITE), buf);
2775                 }
2776                 
2777                 /* Dump up to 20, or hgt-4, lines of messages */
2778                 for (i = 0; i < hgt - 4; i++)
2779                 {
2780                         int leftcol = 0;
2781                         cptr msg;
2782
2783                         /* clean or dirty? */
2784                         if (!(dirty_flags & DIRTY_ALL) && (dirty_line != upper+i))
2785                                 continue;
2786
2787                         msg = lines_list[upper+i];
2788                         if (!msg) break;
2789
2790                         /* Apply horizontal scroll */
2791                         for (j = 0; *msg; msg++, j++)
2792                         {
2793                                 if (j == left) break;
2794 #ifdef JP
2795                                 if (j > left)
2796                                 {
2797                                         leftcol = 1;
2798                                         break;
2799                                 }
2800                                 if (iskanji(*msg))
2801                                 {
2802                                         msg++;
2803                                         j++;
2804                                 }
2805 #endif
2806                         }
2807
2808                         /* Erase line */
2809                         Term_erase(0, i + 1, wid - WID_DESC);
2810
2811                         /* Dump the messages, bottom to top */
2812                         Term_putstr(leftcol, i + 1, wid - WID_DESC - 1, TERM_WHITE, msg);
2813                 }
2814
2815                 for (; i < hgt - 4; i++)
2816                 {
2817                         /* Erase line */
2818                         Term_erase(0, i + 1, wid - WID_DESC);
2819                 }
2820
2821                 /* Display header line */
2822 #ifdef JP
2823                 if (edit_mode)
2824                         prt("^Q ESC ¤Ç¥³¥Þ¥ó¥É¥â¡¼¥É¤Ø°Ü¹Ô¡¢Ä̾ï¤Îʸ»ú¤Ï¤½¤Î¤Þ¤ÞÆþÎÏ", 0, 0);
2825                 else
2826                         prt("q _ ¤Ç½ªÎ»¡¢hjkl2468 ¤Ç°ÜÆ°¡¢^Q a i ¤ÇÆþÎϥ⡼¥É¡¢/ n N ¤Ç¸¡º÷", 0, 0);
2827 #else   
2828                 if (edit_mode)
2829                         prt("Press ^Q ESC to command mode, any letters to insert", 0, 0);
2830                 else
2831                         prt(format("Press q _ to quit, %s to move, ^Q a i to insert mode, /nN to find", rogue_like_commands ? "hjkl" : "2468"), 0, 0);
2832 #endif
2833                 /* Display current position */
2834                 prt (format("(%d,%d)", cx, cy), 0, 70);
2835
2836                 /* Display information when updated */
2837                 if (old_cy != cy || (dirty_flags & (DIRTY_ALL | DIRTY_NOT_FOUND | DIRTY_NO_SEARCH)) || dirty_line == cy)
2838                 {
2839                         /* Clear information line */
2840                         Term_erase(0, hgt - 3 + 1, wid);
2841                         Term_erase(0, hgt - 3 + 2, wid);
2842
2843                         /* Display information */
2844                         if (dirty_flags & DIRTY_NOT_FOUND)
2845                         {
2846 #ifdef JP
2847                                 prt(format("¥Ñ¥¿¡¼¥ó¤¬¸«¤Ä¤«¤ê¤Þ¤»¤ó: %s", search_str), hgt - 3 + 1, 0);
2848 #else
2849                                 prt(format("Pattern not found: %s", search_str), hgt - 3 + 1, 0);
2850 #endif
2851                         }
2852                         else if (dirty_flags & DIRTY_NO_SEARCH)
2853                         {
2854 #ifdef JP
2855                                 prt("¸¡º÷Ãæ¤Î¥Ñ¥¿¡¼¥ó¤¬¤¢¤ê¤Þ¤»¤ó('/'¤Ç¸¡º÷)¡£", hgt - 3 + 1, 0);
2856 #else
2857                                 prt("No pattern to search. (Press '/' to search.)", hgt - 3 + 1, 0);
2858 #endif
2859                         }
2860                         else if (lines_list[cy][0] == '#')
2861                         {
2862 #ifdef JP
2863                                 prt("¤³¤Î¹Ô¤Ï¥³¥á¥ó¥È¤Ç¤¹¡£", hgt - 3 + 1, 0);
2864 #else
2865                                 prt("This line is comment.", hgt - 3 + 1, 0);
2866 #endif
2867                         }
2868                         else if (lines_list[cy][1] == ':')
2869                         {
2870                                 switch(lines_list[cy][0])
2871                                 {
2872                                 case '?':
2873 #ifdef JP
2874                                         prt("¤³¤Î¹Ô¤Ï¾ò·ïʬ´ô¼°¤Ç¤¹¡£", hgt - 3 + 1, 0);
2875 #else
2876                                         prt("This line is Conditional Expression.", hgt - 3 + 1, 0);
2877 #endif
2878                                         break;
2879                                 case 'A':
2880 #ifdef JP
2881                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¼Â¹ÔÆâÍƤòÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2882 #else
2883                                         prt("This line defines Macro action.", hgt - 3 + 1, 0);
2884 #endif
2885                                         break;
2886                                 case 'P':
2887 #ifdef JP
2888                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¥È¥ê¥¬¡¼¡¦¥­¡¼¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2889 #else
2890                                         prt("This line defines Macro trigger key.", hgt - 3 + 1, 0);
2891 #endif
2892                                         break;
2893                                 case 'C':
2894 #ifdef JP
2895                                         prt("¤³¤Î¹Ô¤Ï¥­¡¼ÇÛÃÖ¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2896 #else
2897                                         prt("This line defines Keymap.", hgt - 3 + 1, 0);
2898 #endif
2899                                         break;
2900                                 }
2901                         }
2902
2903                         /* Get description of an autopicker preference line */
2904                         else if (autopick_new_entry(entry, lines_list[cy]))
2905                         {
2906                                 char temp[1024];
2907                                 cptr t;
2908
2909                                 describe_autopick(buf, entry);
2910
2911                                 roff_to_buf(buf, 81, temp);
2912                                 t = temp;
2913                                 for (i = 0; i< 2; i++)
2914                                 {
2915                                         if(t[0] == 0)
2916                                                 break; 
2917                                         else
2918                                         {
2919                                                 prt(t, hgt - 3 + 1 + i, 0);
2920                                                 t += strlen(t) + 1;
2921                                         }
2922                                 }
2923                                 autopick_free_entry(entry);
2924                         }
2925                 }
2926
2927                 /* Place cursor */
2928                 Term_gotoxy(cx - left, cy - upper + 1);
2929
2930                 /* Now clean */
2931                 dirty_flags = 0;
2932                 dirty_line = -1;
2933
2934                 /* Save old key and location */
2935                 old_cy = cy;
2936                 old_key = key;
2937                 old_upper = upper;
2938                 old_left = left;
2939                 old_wid = wid;
2940                 old_hgt = hgt;
2941
2942                 /* Do not process macros except special keys */
2943                 inkey_special = TRUE;
2944
2945                 /* Get a command */
2946                 key = inkey();
2947
2948                 if (edit_mode)
2949                 {
2950                         if (key == ESCAPE)
2951                         {
2952                                 edit_mode = FALSE;
2953
2954                                 /* Mode line is now dirty */
2955                                 dirty_flags |= DIRTY_MODE;
2956                         }
2957
2958                         /* Insert a character */
2959                         else if (!iscntrl(key&0xff))
2960                         {
2961                                 int next;
2962
2963                                 /* Save preceding string */
2964                                 for (i = j = 0; lines_list[cy][i] && i < cx; i++)
2965                                         buf[j++] = lines_list[cy][i];
2966
2967                                 /* Add a character */
2968 #ifdef JP
2969                                 if (iskanji(key))
2970                                 {
2971                                         inkey_base = TRUE;
2972                                         next = inkey();
2973                                         if (j+2 < MAX_LINELEN)
2974                                         {
2975                                                 buf[j++] = key;
2976                                                 buf[j++] = next;
2977                                                 cx += 2;
2978                                         }
2979                                         else
2980                                                 bell();
2981                                 }
2982                                 else
2983 #endif
2984                                 {
2985                                         if (j+1 < MAX_LINELEN)
2986                                                 buf[j++] = key;
2987                                         cx++;
2988                                 }
2989
2990                                 /* Add following */
2991                                 for (; lines_list[cy][i] && j + 1 < MAX_LINELEN; i++)
2992                                         buf[j++] = lines_list[cy][i];
2993                                 buf[j] = '\0';
2994
2995                                 /* Replace current line with new line */
2996                                 string_free(lines_list[cy]);
2997                                 lines_list[cy] = string_make(buf);
2998
2999                                 /* Move to correct collumn */
3000                                 len = strlen(lines_list[cy]);
3001                                 if (len < cx) cx = len;
3002
3003                                 /* Now dirty */
3004                                 dirty_line = cy;
3005                         }
3006                 }
3007                 else
3008                 {
3009                         /* Exit on 'q' */
3010                         if (key == 'q' || key == '_') break;
3011
3012                         switch(key)
3013                         {
3014                         case 'a': case 'i':
3015                                 edit_mode = TRUE;
3016
3017                                 /* Mode line is now dirty */
3018                                 dirty_flags |= DIRTY_MODE;
3019                                 break;
3020                         case '~':
3021                                 if (!autopick_new_entry(entry, lines_list[cy]))
3022                                 {
3023                                         if (old_key != key) repeated_clearing = FALSE;
3024
3025                                         /* Next line */
3026                                         if (lines_list[cy + 1]) cy++;
3027                                         cx = 0;
3028                                         break;
3029                                 }
3030                                 string_free(lines_list[cy]);
3031
3032                                 if (old_key != key)
3033                                 {
3034                                         if (entry->action & DONT_AUTOPICK)
3035                                                 repeated_clearing = TRUE;
3036                                         else
3037                                                 repeated_clearing = FALSE;
3038                                 }
3039
3040                                 entry->action &= ~DO_AUTODESTROY;
3041                                 entry->action &= ~DO_QUERY_AUTOPICK;
3042                                 if (!repeated_clearing)
3043                                 {
3044                                         entry->action &= ~DO_AUTOPICK;
3045                                         entry->action |= DONT_AUTOPICK;
3046                                 }
3047                                 else 
3048                                 {
3049                                         entry->action &= ~DONT_AUTOPICK;
3050                                         entry->action |= DO_AUTOPICK;
3051                                 }
3052
3053                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3054
3055                                 /* Now dirty */
3056                                 dirty_line = cy;
3057
3058                                 /* Next line */
3059                                 if (lines_list[cy + 1]) cy++;
3060                                 cx = 0;
3061                                 break;
3062                         case '!':
3063                                 if (!autopick_new_entry(entry, lines_list[cy]))
3064                                 {
3065                                         if (old_key != key) repeated_clearing = FALSE;
3066
3067                                         /* Next line */
3068                                         if (lines_list[cy + 1]) cy++;
3069                                         cx = 0;
3070                                         break;
3071                                 }
3072                                 string_free(lines_list[cy]);
3073
3074                                 if (old_key != key)
3075                                 {
3076                                         if (entry->action & DO_AUTODESTROY)
3077                                                 repeated_clearing = TRUE;
3078                                         else
3079                                                 repeated_clearing = FALSE;
3080                                 }
3081
3082                                 entry->action &= ~DONT_AUTOPICK;
3083                                 entry->action &= ~DO_QUERY_AUTOPICK;
3084                                 if (!repeated_clearing)
3085                                 {
3086                                         entry->action &= ~DO_AUTOPICK;
3087                                         entry->action |= DO_AUTODESTROY;
3088                                 }
3089                                 else 
3090                                 {
3091                                         entry->action &= ~DO_AUTODESTROY;
3092                                         entry->action |= DO_AUTOPICK;
3093                                 }
3094
3095                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3096
3097                                 /* Now dirty */
3098                                 dirty_line = cy;
3099
3100                                 /* Next line */
3101                                 if (lines_list[cy + 1]) cy++;
3102                                 cx = 0;
3103                                 break;
3104                         case ';':
3105                                 if (!autopick_new_entry(entry, lines_list[cy]))
3106                                 {
3107                                         if (old_key != key) repeated_clearing = FALSE;
3108
3109                                         /* Next line */
3110                                         if (lines_list[cy + 1]) cy++;
3111                                         cx = 0;
3112                                         break;
3113                                 }
3114                                 string_free(lines_list[cy]);
3115
3116                                 if (old_key != key)
3117                                 {
3118                                         if (entry->action & DO_QUERY_AUTOPICK)
3119                                                 repeated_clearing = TRUE;
3120                                         else
3121                                                 repeated_clearing = FALSE;
3122                                 }
3123
3124                                 entry->action &= ~DO_AUTODESTROY;
3125                                 entry->action &= ~DONT_AUTOPICK;
3126                                 if (!repeated_clearing)
3127                                 {
3128                                         entry->action &= ~DO_AUTOPICK;
3129                                         entry->action |= DO_QUERY_AUTOPICK;
3130                                 }
3131                                 else 
3132                                 {
3133                                         entry->action &= ~DO_QUERY_AUTOPICK;
3134                                         entry->action |= DO_AUTOPICK;
3135                                 }
3136
3137                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3138
3139                                 /* Now dirty */
3140                                 dirty_line = cy;
3141
3142                                 /* Next line */
3143                                 if (lines_list[cy + 1]) cy++;
3144                                 cx = 0;
3145                                 break;
3146                         case '(':
3147                                 /* Toggle display on the 'M'ap */
3148                                 if (!autopick_new_entry(entry, lines_list[cy]))
3149                                 {
3150                                         if (old_key != key) repeated_clearing = FALSE;
3151
3152                                         /* Next line */
3153                                         if (lines_list[cy + 1]) cy++;
3154                                         cx = 0;
3155                                         break;
3156                                 }
3157                                 string_free(lines_list[cy]);
3158
3159                                 if (old_key != key)
3160                                 {
3161                                         if (entry->action & DO_DISPLAY)
3162                                                 repeated_clearing = TRUE;
3163                                         else
3164                                                 repeated_clearing = FALSE;
3165                                 }
3166
3167                                 if (!repeated_clearing)
3168                                         entry->action |= DO_DISPLAY;
3169                                 else
3170                                         entry->action &= ~DO_DISPLAY;
3171
3172                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3173
3174                                 /* Now dirty */
3175                                 dirty_line = cy;
3176
3177                                 /* Next line */
3178                                 if (lines_list[cy + 1]) cy++;
3179                                 cx = 0;
3180                                 break;
3181                         case '#':
3182                         case '{':
3183                                 key = KTRL('o');
3184                                 break;
3185                         case 'h': case '4':
3186                                 key = KTRL('b');
3187                                 break;
3188                         case 'l': case '6':
3189                                 key = KTRL('f');
3190                                 break;
3191                         case 'j': case '2':
3192                                 key = KTRL('n');
3193                                 break;
3194                         case 'k': case '8':
3195                                 key = KTRL('p');
3196                                 break;
3197                         case ' ':
3198                                 while (cy < upper + hgt-4 && lines_list[cy + 1])
3199                                         cy++;
3200                                 upper = cy;
3201                                 break;
3202                         case '-': case 'b':
3203                                 while (0 < cy && upper <= cy)
3204                                         cy--;
3205                                 while (0 < upper && cy + 1 < upper + hgt - 4)
3206                                         upper--;
3207                                 break;
3208
3209                         case 'g':
3210                                 cy = 0;
3211                                 break;
3212
3213                         case 'G':
3214                                 while (lines_list[cy + 1])
3215                                         cy++;
3216                                 break;
3217
3218                         case 'm':
3219                                 /* Erase line */
3220                                 Term_erase(0, cy - upper + 1, wid - WID_DESC);
3221
3222                                 /* Prompt */
3223 #ifdef JP
3224                                 Term_putstr(0, cy - upper + 1, wid - WID_DESC - 1, TERM_YELLOW, "P:<¥È¥ê¥¬¡¼¥­¡¼>: ");
3225 #else
3226                                 Term_putstr(0, cy - upper + 1, wid - WID_DESC - 1, TERM_YELLOW, "P:<Trigger key>: ");
3227 #endif
3228                                 if (insert_macro_line(lines_list, cy))
3229                                 {
3230                                         /* Prepare to input action */
3231                                         cx = 2;
3232                                         edit_mode = TRUE;
3233
3234                                         /* Now dirty */
3235                                         dirty_flags |= DIRTY_ALL;
3236                                         dirty_flags |= DIRTY_MODE;
3237                                 }
3238
3239                                 break;
3240
3241                         case 'c':
3242                                 /* Erase line */
3243                                 Term_erase(0, cy - upper + 1, wid - WID_DESC);
3244
3245                                 /* Prompt */
3246 #ifdef JP
3247                                 Term_putstr(0, cy - upper + 1, wid - WID_DESC - 1, TERM_YELLOW, format("C:%d:<¥³¥Þ¥ó¥É¥­¡¼>: ", (rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG)));
3248 #else
3249                                 Term_putstr(0, cy - upper + 1, wid - WID_DESC - 1, TERM_YELLOW, format("C:%d:<Keypress>: ", (rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG)));
3250 #endif
3251
3252                                 if (insert_keymap_line(lines_list, cy))
3253                                 {
3254                                         /* Prepare to input action */
3255                                         cx = 2;
3256                                         edit_mode = TRUE;
3257
3258                                         /* Now dirty */
3259                                         dirty_flags |= DIRTY_ALL;
3260                                         dirty_flags |= DIRTY_MODE;
3261                                 }                               
3262                                 break;
3263                         case '/':
3264                                 /* Become dirty because of item/equip menu */
3265                                 dirty_flags |= DIRTY_SCREEN;
3266
3267                                 if (!get_string_for_search(&search_o_ptr, &search_str))
3268                                         break;
3269
3270                                 /* fall through */
3271                         case 'n':
3272                                 if (search_o_ptr)
3273                                 {
3274                                         if (!search_for_object(lines_list, search_o_ptr, &cx, &cy, TRUE)) dirty_flags |= DIRTY_NOT_FOUND;
3275                                 }
3276                                 else if (search_str)
3277                                 {
3278                                         if (!search_for_string(lines_list, search_str, &cx, &cy, TRUE)) dirty_flags |= DIRTY_NOT_FOUND;
3279                                 }
3280                                 else
3281                                 {
3282                                         dirty_flags |= DIRTY_NO_SEARCH;
3283                                 }
3284                                 break;
3285                         case 'N':
3286                                 if (search_o_ptr)
3287                                 {
3288                                         if (!search_for_object(lines_list, search_o_ptr, &cx, &cy, FALSE)) dirty_flags |= DIRTY_NOT_FOUND;
3289                                 }
3290                                 else if (search_str)
3291                                 {
3292                                         if (!search_for_string(lines_list, search_str, &cx, &cy, FALSE)) dirty_flags |= DIRTY_NOT_FOUND;
3293                                 }
3294                                 else
3295                                 {
3296                                         dirty_flags |= DIRTY_NO_SEARCH;
3297                                 }
3298                                 break;
3299                         }
3300                 }
3301
3302                 switch(key)
3303                 {
3304                 case KTRL('a'):
3305                         /* Beginning of line */
3306                         cx = 0;
3307                         break;
3308                 case KTRL('b'):
3309                         /* Back */
3310                         if (0 < cx)
3311                         {
3312                                 cx--;
3313                                 len = strlen(lines_list[cy]);
3314                                 if (len < cx) cx = len;
3315                         }
3316                         else if (cy > 0)
3317                         {
3318                                 cy--;
3319                                 cx = strlen(lines_list[cy]);
3320                         }
3321                         break;
3322                 case KTRL('c'):
3323                         /* Insert a conditinal expression line */
3324                         insert_return_code(lines_list, 0, cy);
3325                         string_free(lines_list[cy]);
3326                         lines_list[cy] = string_make(classrace);
3327                         cy++;
3328                         insert_return_code(lines_list, 0, cy);
3329                         string_free(lines_list[cy]);
3330                         lines_list[cy] = string_make("?:1");
3331                         cx = 0;
3332
3333                         /* Now dirty */
3334                         dirty_flags |= DIRTY_ALL;
3335                         break;
3336                 case KTRL('e'):
3337                         /* End of line */
3338                         cx = strlen(lines_list[cy]);
3339                         break;
3340                 case KTRL('f'):
3341                         /* Forward */
3342 #ifdef JP
3343                         if (iskanji(lines_list[cy][cx])) cx++;
3344 #endif
3345                         cx++;
3346                         len = strlen(lines_list[cy]);
3347                         if (len < cx)
3348                         {
3349                                 if (lines_list[cy + 1])
3350                                 {
3351                                         cy++;
3352                                         cx = 0;
3353                                 }
3354                                 else
3355                                         cx = len;
3356                         }
3357                         break;
3358                 case KTRL('g'):
3359                         /* Toggle display on the 'M'ap */
3360                         if (!autopick_new_entry(entry, lines_list[cy]))
3361                                 break;
3362                         string_free(lines_list[cy]);
3363
3364                         if (entry->action & DO_DISPLAY)
3365                         {
3366                                 entry->action &= ~DO_DISPLAY;
3367                                 cx++;
3368                         }
3369                         else
3370                         {
3371                                 entry->action |= DO_DISPLAY;
3372                                 if (cx > 0) cx--;
3373                         }
3374
3375                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3376
3377                         /* Now dirty */
3378                         dirty_line = cy;
3379                         break;
3380                 case KTRL('i'):
3381                         /* Insert choosen item name */
3382                         if (!entry_from_choosed_object(entry))
3383                         {
3384                                 /* Now dirty because of item/equip menu */
3385                                 dirty_flags |= DIRTY_SCREEN;
3386                                 break;
3387                         }
3388
3389                         insert_return_code(lines_list, 0, cy);
3390                         string_free(lines_list[cy]);
3391                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3392                         cx = 0;
3393
3394                         /* Now dirty because of item/equip menu */
3395                         dirty_flags |= DIRTY_SCREEN;
3396
3397                         break;
3398                 case KTRL('l'):
3399                         /* Insert a name of last destroyed item */
3400                         if (last_destroyed)
3401                         {
3402                                 insert_return_code(lines_list, 0, cy);
3403                                 string_free(lines_list[cy]);
3404                                 lines_list[cy] = string_make(last_destroyed);
3405                                 cx = 0;
3406
3407                                 /* Now dirty */
3408                                 dirty_flags |= DIRTY_ALL;
3409                         }
3410                         break;
3411                 case '\n': case '\r':
3412                         /* Split a line or insert end of line */
3413                         insert_return_code(lines_list, cx, cy);
3414                         cy++;
3415                         cx = 0;
3416
3417                         /* Now dirty */
3418                         dirty_flags |= DIRTY_ALL;
3419                         break;
3420                 case KTRL('n'):
3421                         /* Next line */
3422                         if (lines_list[cy + 1]) cy++;
3423                         break;
3424                 case KTRL('o'):
3425                         /* Prepare to write auto-inscription text */
3426                         if (!autopick_new_entry(entry, lines_list[cy]))
3427                                 break;
3428                         string_free(lines_list[cy]);
3429
3430                         if (!entry->insc) entry->insc = string_make("");
3431
3432                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3433
3434                         /* Move to collumn for auto inscription */
3435                         for (cx = 0; lines_list[cy][cx]; cx++)
3436                                 if (lines_list[cy][cx] == '#') break;
3437                         cx++;
3438                         edit_mode = TRUE;
3439
3440                         /* Now dirty */
3441                         dirty_line = cy;
3442                         dirty_flags |= DIRTY_MODE;
3443                         break;
3444                 case KTRL('p'):
3445                         /* Previous line */
3446                         if (cy > 0) cy--;
3447                         break;
3448                 case KTRL('q'):
3449                         /* Change mode */
3450                         edit_mode = !edit_mode;
3451                         
3452                         /* Mode line is now dirty */
3453                         dirty_flags |= DIRTY_MODE;
3454                         break;
3455                 case KTRL('r'):
3456                         /* Revert to original */
3457 #ifdef JP
3458                         if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¸µ¤Î¾õÂÖ¤ËÌᤷ¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© "))
3459 #else
3460                         if (!get_check("Discard all change and revert to original file. Are you sure? "))
3461 #endif
3462                                 break;
3463
3464                         free_text_lines(lines_list);
3465                         lines_list = read_pickpref_text_lines(&filename_mode);
3466                         dirty_flags |= DIRTY_ALL | DIRTY_MODE;
3467                         cx = cy = 0;
3468                         edit_mode = FALSE;
3469                         break;
3470                 case KTRL('s'):
3471                         /* Rotate action; pickup/destroy/leave */
3472                         if (!autopick_new_entry(entry, lines_list[cy]))
3473                                 break;
3474                         string_free(lines_list[cy]);
3475
3476                         if (entry->action & DO_AUTOPICK)
3477                         {
3478                                 entry->action &= ~DO_AUTOPICK;
3479                                 entry->action |= DO_AUTODESTROY;
3480                         }
3481                         else if (entry->action & DO_AUTODESTROY)
3482                         {
3483                                 entry->action &= ~DO_AUTODESTROY;
3484                                 entry->action |= DONT_AUTOPICK;
3485                         }
3486                         else if (entry->action & DONT_AUTOPICK)
3487                         {
3488                                 entry->action &= ~DONT_AUTOPICK;
3489                                 entry->action |= DO_AUTOPICK;
3490                         }
3491
3492                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3493
3494                         /* Now dirty */
3495                         dirty_line = cy;
3496
3497                         break;
3498                 case KTRL('t'):
3499                         /* Nothing */
3500                         break;
3501                 case KTRL('u'):
3502                         /* Rotate identify-state; identified/unidentified/... */
3503                         if (!autopick_new_entry(entry, lines_list[cy]))
3504                                 break;
3505                         string_free(lines_list[cy]);
3506
3507                         if (IS_FLG(FLG_UNIDENTIFIED)) 
3508                         {
3509                                 REM_FLG(FLG_UNIDENTIFIED);
3510                                 ADD_FLG(FLG_UNAWARE);
3511                                 REM_FLG(FLG_IDENTIFIED);
3512                                 REM_FLG(FLG_STAR_IDENTIFIED);
3513                         }
3514                         else if (IS_FLG(FLG_UNAWARE)) 
3515                         {
3516                                 REM_FLG(FLG_UNIDENTIFIED);
3517                                 REM_FLG(FLG_UNAWARE);
3518                                 ADD_FLG(FLG_IDENTIFIED);
3519                                 REM_FLG(FLG_STAR_IDENTIFIED);
3520                         }
3521                         else if (IS_FLG(FLG_STAR_IDENTIFIED)) 
3522                         {
3523                                 REM_FLG(FLG_UNIDENTIFIED);
3524                                 REM_FLG(FLG_UNAWARE);
3525                                 REM_FLG(FLG_IDENTIFIED);
3526                                 REM_FLG(FLG_STAR_IDENTIFIED);
3527                         }
3528                         else if (IS_FLG(FLG_IDENTIFIED)) 
3529                         {
3530                                 REM_FLG(FLG_UNIDENTIFIED);
3531                                 REM_FLG(FLG_UNAWARE);
3532                                 REM_FLG(FLG_IDENTIFIED);
3533                                 ADD_FLG(FLG_STAR_IDENTIFIED);
3534                         }
3535                         else
3536                         {
3537                                 ADD_FLG(FLG_UNIDENTIFIED);
3538                                 REM_FLG(FLG_UNAWARE);
3539                                 REM_FLG(FLG_IDENTIFIED);
3540                                 REM_FLG(FLG_STAR_IDENTIFIED);
3541                         }
3542
3543                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3544
3545                         /* Now dirty */
3546                         dirty_line = cy;
3547                         break;
3548                 case KTRL('v'):
3549                         /* Scroll up */
3550                         while (cy < upper + hgt-4 && lines_list[cy + 1])
3551                                 cy++;
3552                         upper = cy;
3553                         break;
3554                 case KTRL('w'):
3555                         /* Toggle 'worthless' */
3556                         toggle_string(lines_list, FLG_WORTHLESS, cy);
3557                         /* Now dirty */
3558                         dirty_line = cy;
3559                         break;
3560                 case KTRL('x'):
3561                         /* Rotate within nameless, ego, artifact */
3562                         if (!autopick_new_entry(entry, lines_list[cy]))
3563                                 break;
3564                         string_free(lines_list[cy]);
3565
3566                         if (IS_FLG(FLG_NAMELESS)) 
3567                         {
3568                                 REM_FLG(FLG_NAMELESS);
3569                                 ADD_FLG(FLG_EGO);
3570                                 REM_FLG(FLG_ARTIFACT);
3571                         }
3572                         else if (IS_FLG(FLG_EGO)) 
3573                         {
3574                                 REM_FLG(FLG_NAMELESS);
3575                                 REM_FLG(FLG_EGO);
3576                                 ADD_FLG(FLG_ARTIFACT);
3577                         }
3578                         else if (IS_FLG(FLG_ARTIFACT)) 
3579                         {
3580                                 REM_FLG(FLG_NAMELESS);
3581                                 REM_FLG(FLG_EGO);
3582                                 REM_FLG(FLG_ARTIFACT);
3583                         }
3584                         else
3585                         {
3586                                 ADD_FLG(FLG_NAMELESS);
3587                                 REM_FLG(FLG_EGO);
3588                                 REM_FLG(FLG_ARTIFACT);
3589                         }
3590
3591                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3592
3593                         /* Now dirty */
3594                         dirty_line = cy;
3595                         break;
3596
3597                 case KTRL('y'):
3598                         /* Paste killed text */
3599                         if (strlen(yank_buf))
3600                         {
3601                                 bool ret = FALSE;
3602
3603                                 for (j = 0; yank_buf[j]; j += strlen(yank_buf + j) + 1)
3604                                 {
3605                                         if (ret && '\n' == yank_buf[j])
3606                                         {
3607                                                 ret = FALSE;
3608                                                 continue;
3609                                         }
3610
3611                                         /* Split current line */
3612                                         insert_return_code(lines_list, cx, cy);
3613
3614                                         /* Save preceding string */
3615                                         for(i = 0; lines_list[cy][i]; i++)
3616                                                 buf[i] = lines_list[cy][i];
3617
3618                                         /* Paste yank buffer */
3619                                         if ('\n' != yank_buf[j])
3620                                         {
3621                                                 int k = j;
3622                                                 while (yank_buf[k] && i < MAX_LINELEN-1)
3623                                                         buf[i++] = yank_buf[k++];
3624                                                 ret = TRUE;
3625                                         }
3626
3627                                         buf[i] = '\0';
3628
3629                                         string_free(lines_list[cy]);
3630                                         lines_list[cy] = string_make(buf);
3631
3632                                         /* Move to the beggining of next line */
3633                                         cx = 0;
3634                                         cy++;
3635                                 }
3636
3637                                 /* Now dirty */
3638                                 dirty_flags |= DIRTY_ALL;
3639                         }
3640                         break;
3641                 case KTRL('z'):
3642                         /* Toggle 'collecting' */
3643                         toggle_string(lines_list, FLG_COLLECTING, cy);
3644                         /* Now dirty */
3645                         dirty_line = cy;
3646                         break;
3647
3648                 case KTRL('k'):
3649                         /* Kill rest of line */
3650                         if ((uint)cx > strlen(lines_list[cy]))
3651                                 cx = (int)strlen(lines_list[cy]);
3652
3653                         /* Save preceding string */
3654                         for (i = 0; lines_list[cy][i] && i < cx; i++)
3655                         {
3656 #ifdef JP
3657                                 if (iskanji(lines_list[cy][i]))
3658                                 {
3659                                         buf[i] = lines_list[cy][i];
3660                                         i++;
3661                                 }
3662 #endif
3663                                 buf[i] = lines_list[cy][i];
3664                         }
3665                         buf[i] = '\0';
3666
3667                         j = 0;
3668                         if (old_key == key)
3669                                 while (yank_buf[j])
3670                                         j += strlen(yank_buf + j) + 1;
3671
3672                         /* Copy following to yank buffer */
3673                         if (lines_list[cy][i])
3674                         {
3675                                 while (lines_list[cy][i] && j < MAX_YANK - 2)
3676                                         yank_buf[j++] = lines_list[cy][i++];
3677                                 i = TRUE;
3678                         }
3679                         else
3680                         {
3681                                 if (j < MAX_YANK - 2)
3682                                         yank_buf[j++] = '\n';
3683                                 i = FALSE;
3684                         }
3685                         yank_buf[j++] = '\0';
3686                         yank_buf[j] = '\0';
3687
3688                         /* Replace current line with 'preceding string' */
3689                         string_free(lines_list[cy]);
3690                         lines_list[cy] = string_make(buf);
3691
3692                         if (i)
3693                         {
3694                                 /* Now dirty */
3695                                 dirty_line = cy;
3696                                 break;
3697                         }
3698
3699                         /* fall through */
3700                 case KTRL('d'):
3701                 case 0x7F:
3702                         /* DELETE == go forward + BACK SPACE */
3703 #ifdef JP
3704                         if (iskanji(lines_list[cy][cx])) cx++;
3705 #endif
3706                         cx++;
3707
3708                         /* fall through */
3709
3710                 case '\010':
3711                         /* BACK SPACE */
3712                         len = strlen(lines_list[cy]);
3713                         if (len < cx)
3714                         {
3715                                 if (lines_list[cy + 1])
3716                                 {
3717                                         cy++;
3718                                         cx = 0;
3719                                 }
3720                                 else
3721                                 {
3722                                         cx = len;
3723                                         break;
3724                                 }
3725                         }
3726
3727                         if (cx == 0)
3728                         {
3729                                 /* delete a return code and union two lines */
3730                                 if (cy == 0) break;
3731                                 cx = strlen(lines_list[cy-1]);
3732                                 strcpy(buf, lines_list[cy-1]);
3733                                 strcat(buf, lines_list[cy]);
3734                                 string_free(lines_list[cy-1]);
3735                                 string_free(lines_list[cy]);
3736                                 lines_list[cy-1] = string_make(buf);
3737                                 for (i = cy; lines_list[i+1]; i++)
3738                                         lines_list[i] = lines_list[i+1];
3739                                 lines_list[i] = NULL;
3740                                 cy--;
3741
3742                                 /* Now dirty */
3743                                 dirty_flags |= DIRTY_ALL;
3744                                 break;
3745                         }
3746
3747                         for (i = j = k = 0; lines_list[cy][i] && i < cx; i++)
3748                         {
3749                                 k = j;
3750 #ifdef JP
3751                                 if (iskanji(lines_list[cy][i]))
3752                                         buf[j++] = lines_list[cy][i++];
3753 #endif
3754                                 buf[j++] = lines_list[cy][i];
3755                         }
3756                         while (j > k)
3757                         {
3758                                 cx--;
3759                                 j--;
3760                         }
3761                         for (; lines_list[cy][i]; i++)
3762                                 buf[j++] = lines_list[cy][i];
3763                         buf[j] = '\0';
3764                         string_free(lines_list[cy]);
3765                         lines_list[cy] = string_make(buf);
3766
3767                         /* Now dirty */
3768                         dirty_line = cy;
3769                         break;
3770                 }
3771
3772         } /* while (1) */
3773
3774         /* Restore the screen */
3775         screen_load();
3776
3777         switch (filename_mode)
3778         {
3779         case PT_DEFAULT:
3780 #ifdef JP
3781                 strcpy(buf, "picktype.prf");
3782 #else
3783                 strcpy(buf, "pickpref.prf");
3784 #endif
3785                 break;
3786
3787         case PT_WITH_PNAME:
3788 #ifdef JP
3789                 sprintf(buf, "picktype-%s.prf", player_name);
3790 #else
3791                 sprintf(buf, "pickpref-%s.prf", player_name);
3792 #endif
3793                 break;
3794         }
3795
3796         write_text_lines(buf, lines_list);
3797         free_text_lines(lines_list);
3798
3799         string_free(last_destroyed);
3800
3801         /* Reload autopick pref */
3802         process_pickpref_file(buf);
3803
3804         /* HACK -- reset start_time so that playtime is not increase while edit */
3805         start_time = time(NULL);
3806 }