OSDN Git Service

自動拾いの修正値のキーワードがautopick.txt等では「修正値nn以上の」と
[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 = inscribe_flags(o_ptr, quark_str(o_ptr->inscription));
962         else
963                 o_ptr->inscription = inscribe_flags(o_ptr, autopick_list[idx].insc);
964
965         if (item > INVEN_PACK)
966         {
967                 /* Redraw inscription */
968                 p_ptr->window |= (PW_EQUIP);
969
970                 /* {.} and {$} effect p_ptr->warning and TRC_TELEPORT_SELF */
971                 p_ptr->update |= (PU_BONUS);
972         }
973         else if (item >= 0)
974         {
975                 /* Redraw inscription */
976                 p_ptr->window |= (PW_INVEN);
977         }
978 }
979
980
981 /*
982  * Automatically destroy an item if it is to be destroyed
983  */
984 bool auto_destroy_item(int item, int autopick_idx, bool wait_optimize)
985 {
986         bool destroy = FALSE;
987         char o_name[MAX_NLEN];
988         object_type *o_ptr;
989
990         /* Don't destroy equipped items */
991         if (item > INVEN_PACK) return FALSE;
992
993         /* Get the item (in the pack) */
994         if (item >= 0) o_ptr = &inventory[item];
995
996         /* Get the item (on the floor) */
997         else o_ptr = &o_list[0 - item];
998
999         /* Easy-Auto-Destroyer */
1000         if (is_opt_confirm_destroy(o_ptr)) destroy = TRUE;
1001
1002         /* Protected by auto-picker */
1003         if (autopick_idx >= 0 &&
1004             !(autopick_list[autopick_idx].action & DO_AUTODESTROY))
1005                 destroy = FALSE;
1006
1007         if (!always_pickup)
1008         {
1009                 /* Auto-picker/destroyer */
1010                 if (autopick_idx >= 0 &&
1011                     (autopick_list[autopick_idx].action & DO_AUTODESTROY))
1012                         destroy = TRUE;
1013         }
1014
1015         /* Not to be destroyed */
1016         if (!destroy) return FALSE;
1017
1018         /* Now decided to destroy */
1019
1020         disturb(0,0);
1021
1022         /* Describe the object (with {terrible/special}) */
1023         object_desc(o_name, o_ptr, TRUE, 3);
1024
1025         /* Artifact? */
1026         if (!can_player_destroy_object(o_ptr))
1027         {
1028                 /* Message */
1029 #ifdef JP
1030                 msg_format("%s¤ÏÇ˲õÉÔǽ¤À¡£", o_name);
1031 #else
1032                 msg_format("You cannot auto-destroy %s.", o_name);
1033 #endif
1034
1035                 /* Done */
1036                 return TRUE;
1037         }
1038
1039         /* Record name of destroyed item */
1040         COPY(&autopick_last_destroyed_object, o_ptr, object_type);
1041
1042         /* Eliminate the item (from the pack) */
1043         if (item >= 0)
1044         {
1045                 inven_item_increase(item, -(o_ptr->number));
1046
1047                 /*
1048                  * always optimize equipment.
1049                  * optimize inventry only when wait_optimize is FALSE.
1050                  */
1051                 if (!wait_optimize || item > INVEN_PACK)
1052                         inven_item_optimize(item);
1053         }
1054
1055         /* Eliminate the item (from the floor) */
1056         else
1057         {
1058                 delete_object_idx(0 - item);
1059         }
1060
1061         /* Print a message */
1062 #ifdef JP
1063         msg_format("%s¤ò¼«Æ°Ç˲õ¤·¤Þ¤¹¡£", o_name);
1064 #else
1065         msg_format("Auto-destroying %s.", o_name);
1066 #endif
1067                         
1068         return TRUE;
1069 }
1070
1071
1072 /*
1073  * Optimize all inventry items after consumption of staves or scrolls.
1074  */
1075 void optimize_inventry_auto_destroy(void)
1076 {
1077         int i;
1078
1079         for (i = 0; i <= INVEN_PACK; i++)
1080                 inven_item_optimize(i);
1081 }
1082
1083
1084 /*
1085  * Automatically pickup/destroy items in this grid.
1086  */
1087 void auto_pickup_items(cave_type *c_ptr)
1088 {
1089         s16b this_o_idx, next_o_idx = 0;
1090         
1091         /* Scan the pile of objects */
1092         for (this_o_idx = c_ptr->o_idx; this_o_idx; this_o_idx = next_o_idx)
1093         {
1094                 int idx;
1095         
1096                 /* Acquire object */
1097                 object_type *o_ptr = &o_list[this_o_idx];
1098                 
1099                 /* Acquire next object */
1100                 next_o_idx = o_ptr->next_o_idx;
1101
1102                 idx = is_autopick(o_ptr);
1103
1104                 /* Item index for floor -1,-2,-3,...  */
1105                 auto_inscribe_item((-this_o_idx), idx);
1106
1107                 if (idx >= 0 &&
1108                         (autopick_list[idx].action & (DO_AUTOPICK | DO_QUERY_AUTOPICK)))
1109                 {
1110                         disturb(0,0);
1111
1112                         if (!inven_carry_okay(o_ptr))
1113                         {
1114                                 char o_name[MAX_NLEN];
1115
1116                                 /* Describe the object */
1117                                 object_desc(o_name, o_ptr, TRUE, 3);
1118
1119                                 /* Message */
1120 #ifdef JP
1121                                 msg_format("¥¶¥Ã¥¯¤Ë¤Ï%s¤òÆþ¤ì¤ë·ä´Ö¤¬¤Ê¤¤¡£", o_name);
1122 #else
1123                                 msg_format("You have no room for %s.", o_name);
1124 #endif
1125                                 /* Hack - remember that the item has given a message here. */
1126                                 o_ptr->marked |= OM_NOMSG;
1127
1128                                 continue;
1129                         }
1130                         else if (autopick_list[idx].action & DO_QUERY_AUTOPICK)
1131                         {
1132                                 char out_val[MAX_NLEN+20];
1133                                 char o_name[MAX_NLEN];
1134
1135                                 if (o_ptr->marked & OM_NO_QUERY)
1136                                 {
1137                                         /* Already answered as 'No' */
1138                                         continue;
1139                                 }
1140
1141                                 /* Describe the object */
1142                                 object_desc(o_name, o_ptr, TRUE, 3);
1143
1144 #ifdef JP
1145                                 sprintf(out_val, "%s¤ò½¦¤¤¤Þ¤¹¤«? ", o_name);
1146 #else
1147                                 sprintf(out_val, "Pick up %s? ", o_name);
1148 #endif
1149
1150                                 if (!get_check(out_val))
1151                                 {
1152                                         /* Hack - remember that the item has given a message here. */
1153                                         o_ptr->marked |= (OM_NOMSG | OM_NO_QUERY);
1154                                         continue;
1155                                 }
1156
1157                         }
1158                         py_pickup_aux(this_o_idx);
1159
1160                         continue;
1161                 }
1162                 
1163                 /*
1164                  * Do auto-destroy;
1165                  * When always_pickup is 'yes', we disable
1166                  * auto-destroyer from autopick function, and do only
1167                  * easy-auto-destroyer.
1168                  */
1169                 else
1170                 {
1171                         if (auto_destroy_item((-this_o_idx), idx, FALSE))
1172                                 continue;
1173                 }
1174         }
1175 }
1176
1177
1178 /*
1179  * Describe which kind of object is Auto-picked/destroyed
1180  */
1181 static void describe_autopick(char *buff, autopick_type *entry)
1182 {
1183         cptr str = entry->name;
1184         byte act = entry->action;
1185         cptr insc = entry->insc;
1186         int i;
1187
1188         bool top = FALSE;
1189
1190 #ifdef JP
1191         cptr before_str[100], body_str;
1192         int before_n = 0;
1193
1194         body_str = "¥¢¥¤¥Æ¥à";
1195
1196         /*** Collecting items ***/
1197         /*** Which can be absorbed into a slot as a bundle ***/
1198         if (IS_FLG(FLG_COLLECTING))
1199                 before_str[before_n++] = "¼ý½¸Ãæ¤Ç´û¤Ë»ý¤Ã¤Æ¤¤¤ë¥¹¥í¥Ã¥È¤Ë¤Þ¤È¤á¤é¤ì¤ë";
1200         
1201         /*** Unidentified ***/
1202         if (IS_FLG(FLG_UNIDENTIFIED))
1203                 before_str[before_n++] = "̤´ÕÄê¤Î";
1204
1205         /*** Identified ***/
1206         if (IS_FLG(FLG_IDENTIFIED))
1207                 before_str[before_n++] = "´ÕÄêºÑ¤ß¤Î";
1208
1209         /*** *Identified* ***/
1210         if (IS_FLG(FLG_STAR_IDENTIFIED))
1211                 before_str[before_n++] = "´°Á´¤Ë´ÕÄêºÑ¤ß¤Î";
1212
1213         /*** Dice boosted (weapon of slaying) ***/
1214         if (IS_FLG(FLG_BOOSTED))
1215         {
1216                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤¬Ä̾ï¤è¤êÂ礭¤¤";
1217                 body_str = "Éð´ï";
1218         }
1219
1220         /*** Weapons whose dd*ds is more than nn ***/
1221         if (IS_FLG(FLG_MORE_THAN))
1222         {
1223                 static char more_than_desc_str[] = "___";
1224                 before_str[before_n++] = "¥À¥á¡¼¥¸¥À¥¤¥¹¤ÎºÇÂçÃͤ¬";
1225                 body_str = "Éð´ï";
1226                         
1227                 sprintf(more_than_desc_str,"%d", entry->dice);
1228                 before_str[before_n++] = more_than_desc_str;
1229                 before_str[before_n++] = "°Ê¾å¤Î";
1230         }
1231
1232         /*** Items whose magical bonus is more than nn ***/
1233         if (IS_FLG(FLG_MORE_BONUS))
1234         {
1235                 static char more_bonus_desc_str[] = "___";
1236                 before_str[before_n++] = "½¤ÀµÃͤ¬(+";
1237                         
1238                 sprintf(more_bonus_desc_str,"%d", entry->bonus);
1239                 before_str[before_n++] = more_bonus_desc_str;
1240                 before_str[before_n++] = ")°Ê¾å¤Î";
1241         }
1242
1243         /*** Worthless items ***/
1244         if (IS_FLG(FLG_WORTHLESS))
1245                 before_str[before_n++] = "Ź¤Ç̵²ÁÃͤÈȽÄꤵ¤ì¤ë";
1246
1247         /*** Artifact ***/
1248         if (IS_FLG(FLG_ARTIFACT))
1249         {
1250                 before_str[before_n++] = "¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Î";
1251                 body_str = "ÁõÈ÷";
1252         }
1253
1254         /*** Ego ***/
1255         if (IS_FLG(FLG_EGO))
1256         {
1257                 before_str[before_n++] = "¥¨¥´¥¢¥¤¥Æ¥à¤Î";
1258                 body_str = "ÁõÈ÷";
1259         }
1260
1261         /*** Nameless ***/
1262         if (IS_FLG(FLG_NAMELESS))
1263         {
1264                 before_str[before_n++] = "¥¨¥´¤Ç¤â¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È¤Ç¤â¤Ê¤¤";
1265                 body_str = "ÁõÈ÷";
1266         }
1267
1268         /*** Unaware items ***/
1269         if (IS_FLG(FLG_UNAWARE))
1270                 before_str[before_n++] = "̤´ÕÄê¤Ç¤½¤Î¸ú²Ì¤âȽÌÀ¤·¤Æ¤¤¤Ê¤¤";
1271
1272         /*** Wanted monster's corpse/skeletons ***/
1273         if (IS_FLG(FLG_WANTED))
1274         {
1275                 before_str[before_n++] = "¥Ï¥ó¥¿¡¼»ö̳½ê¤Ç¾Þ¶â¼ó¤È¤µ¤ì¤Æ¤¤¤ë";
1276                 body_str = "»àÂΤä¹ü";
1277         }
1278
1279         /*** Human corpse/skeletons (for Daemon magic) ***/
1280         if (IS_FLG(FLG_HUMAN))
1281         {
1282                 before_str[before_n++] = "°­ËâËâË¡¤Ç»È¤¦¤¿¤á¤Î¿Í´Ö¤ä¥Ò¥å¡¼¥Þ¥Î¥¤¥É¤Î";
1283                 body_str = "»àÂΤä¹ü";
1284         }
1285
1286         /*** Unique monster's corpse/skeletons/statues ***/
1287         if (IS_FLG(FLG_UNIQUE))
1288         {
1289                 before_str[before_n++] = "¥æ¥Ë¡¼¥¯¥â¥ó¥¹¥¿¡¼¤Î";
1290                 body_str = "»àÂΤä¹ü";
1291         }
1292
1293         /*** Unreadable spellbooks ***/
1294         if (IS_FLG(FLG_UNREADABLE))
1295         {
1296                 before_str[before_n++] = "¤¢¤Ê¤¿¤¬Æɤá¤Ê¤¤Îΰè¤Î";
1297                 body_str = "ËâË¡½ñ";
1298         }
1299
1300         /*** First realm spellbooks ***/
1301         if (IS_FLG(FLG_REALM1))
1302         {
1303                 before_str[before_n++] = "Âè°ìÎΰè¤Î";
1304                 body_str = "ËâË¡½ñ";
1305         }
1306
1307         /*** Second realm spellbooks ***/
1308         if (IS_FLG(FLG_REALM2))
1309         {
1310                 before_str[before_n++] = "ÂèÆóÎΰè¤Î";
1311                 body_str = "ËâË¡½ñ";
1312         }
1313
1314         /*** First rank spellbooks ***/
1315         if (IS_FLG(FLG_FIRST))
1316         {
1317                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î1ºýÌܤÎ";
1318                 body_str = "ËâË¡½ñ";
1319         }
1320
1321         /*** Second rank spellbooks ***/
1322         if (IS_FLG(FLG_SECOND))
1323         {
1324                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î2ºýÌܤÎ";
1325                 body_str = "ËâË¡½ñ";
1326         }
1327
1328         /*** Third rank spellbooks ***/
1329         if (IS_FLG(FLG_THIRD))
1330         {
1331                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î3ºýÌܤÎ";
1332                 body_str = "ËâË¡½ñ";
1333         }
1334
1335         /*** Fourth rank spellbooks ***/
1336         if (IS_FLG(FLG_FOURTH))
1337         {
1338                 before_str[before_n++] = "Á´4ºý¤ÎÆâ¤Î4ºýÌܤÎ";
1339                 body_str = "ËâË¡½ñ";
1340         }
1341
1342         /*** Items ***/
1343         if (IS_FLG(FLG_ITEMS))
1344                 ; /* Nothing to do */
1345         else if (IS_FLG(FLG_WEAPONS))
1346                 body_str = "Éð´ï";
1347         else if (IS_FLG(FLG_ARMORS))
1348                 body_str = "Ëɶñ";
1349         else if (IS_FLG(FLG_MISSILES))
1350                 body_str = "ÃƤäÌð¤ä¥¯¥í¥¹¥Ü¥¦¤ÎÌð";
1351         else if (IS_FLG(FLG_DEVICES))
1352                 body_str = "´¬Êª¤äËâË¡ËÀ¤ä¾ó¤ä¥í¥Ã¥É";
1353         else if (IS_FLG(FLG_LIGHTS))
1354                 body_str = "¸÷¸»ÍѤΥ¢¥¤¥Æ¥à";
1355         else if (IS_FLG(FLG_JUNKS))
1356                 body_str = "Àޤ줿ËÀÅù¤Î¥¬¥é¥¯¥¿";
1357         else if (IS_FLG(FLG_SPELLBOOKS))
1358                 body_str = "ËâË¡½ñ";
1359         else if (IS_FLG(FLG_HAFTED))
1360                 body_str = "Æß´ï";
1361         else if (IS_FLG(FLG_SHIELDS))
1362                 body_str = "½â";
1363         else if (IS_FLG(FLG_BOWS))
1364                 body_str = "¥¹¥ê¥ó¥°¤äµÝ¤ä¥¯¥í¥¹¥Ü¥¦";
1365         else if (IS_FLG(FLG_RINGS))
1366                 body_str = "»ØÎØ";
1367         else if (IS_FLG(FLG_AMULETS))
1368                 body_str = "¥¢¥ß¥å¥ì¥Ã¥È";
1369         else if (IS_FLG(FLG_SUITS))
1370                 body_str = "³»";
1371         else if (IS_FLG(FLG_CLOAKS))
1372                 body_str = "¥¯¥í¡¼¥¯";
1373         else if (IS_FLG(FLG_HELMS))
1374                 body_str = "¥Ø¥ë¥á¥Ã¥È¤ä´§";
1375         else if (IS_FLG(FLG_GLOVES))
1376                 body_str = "äƼê";
1377         else if (IS_FLG(FLG_BOOTS))
1378                 body_str = "¥Ö¡¼¥Ä";
1379
1380         *buff = '\0';
1381         if (!before_n) 
1382                 strcat(buff, "Á´¤Æ¤Î");
1383         else for (i = 0; i < before_n && before_str[i]; i++)
1384                 strcat(buff, before_str[i]);
1385
1386         strcat(buff, body_str);
1387
1388         if (*str)
1389         {
1390                 if (*str == '^')
1391                 {
1392                         str++;
1393                         top = TRUE;
1394                 }
1395
1396                 strcat(buff, "¤Ç¡¢Ì¾Á°¤¬¡Ö");
1397                 strncat(buff, str, 80);
1398                 if (top)
1399                         strcat(buff, "¡×¤Ç»Ï¤Þ¤ë¤â¤Î");
1400                 else
1401                         strcat(buff, "¡×¤ò´Þ¤à¤â¤Î");
1402         }
1403
1404         if (insc)
1405         {
1406                 strncat(buff, format("¤Ë¡Ö%s¡×", insc), 80);
1407
1408                 if (strstr(insc, "%%all"))
1409                         strcat(buff, "(%%all¤ÏÁ´Ç½ÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
1410                 else if (strstr(insc, "%all"))
1411                         strcat(buff, "(%all¤ÏÁ´Ç½ÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
1412                 else if (strstr(insc, "%%"))
1413                         strcat(buff, "(%%¤ÏÄɲÃǽÎϤòɽ¤¹±Ñ»ú¤Îµ­¹æ¤ÇÃÖ´¹)");
1414                 else if (strstr(insc, "%"))
1415                         strcat(buff, "(%¤ÏÄɲÃǽÎϤòɽ¤¹µ­¹æ¤ÇÃÖ´¹)");
1416
1417                 strcat(buff, "¤È¹ï¤ó¤Ç");
1418         }
1419         else
1420                 strcat(buff, "¤ò");
1421
1422         if (act & DONT_AUTOPICK)
1423                 strcat(buff, "ÊüÃÖ¤¹¤ë¡£");
1424         else if (act & DO_AUTODESTROY)
1425                 strcat(buff, "Ç˲õ¤¹¤ë¡£");
1426         else if (act & DO_QUERY_AUTOPICK)
1427                 strcat(buff, "³Îǧ¤Î¸å¤Ë½¦¤¦¡£");
1428         else
1429                 strcat(buff, "½¦¤¦¡£");
1430
1431         if (act & DO_DISPLAY)
1432         {
1433                 if (act & DONT_AUTOPICK)
1434                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'N'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1435                 else if (act & DO_AUTODESTROY)
1436                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'K'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1437                 else
1438                         strcat(buff, "Á´ÂΥޥå×('M')¤Ç'M'¤ò²¡¤·¤¿¤È¤­¤Ëɽ¼¨¤¹¤ë¡£");
1439         }
1440         else
1441                 strcat(buff, "Á´ÂΥޥåפˤÏɽ¼¨¤·¤Ê¤¤");
1442
1443 #else /* JP */
1444
1445         cptr before_str[20], after_str[20], which_str[20], whose_str[20], body_str;
1446         int before_n = 0, after_n = 0, which_n = 0, whose_n = 0;
1447
1448         body_str = "items";
1449
1450         /*** Collecting items ***/
1451         /*** Which can be absorbed into a slot as a bundle ***/
1452         if (IS_FLG(FLG_COLLECTING))
1453                 which_str[which_n++] = "can be absorbed into an existing inventory slot";
1454         
1455         /*** Unidentified ***/
1456         if (IS_FLG(FLG_UNIDENTIFIED))
1457                 before_str[before_n++] = "unidentified";
1458
1459         /*** Identified ***/
1460         if (IS_FLG(FLG_IDENTIFIED))
1461                 before_str[before_n++] = "identified";
1462
1463         /*** *Identified* ***/
1464         if (IS_FLG(FLG_STAR_IDENTIFIED))
1465                 before_str[before_n++] = "fully identified";
1466
1467         /*** Worthless items ***/
1468         if (IS_FLG(FLG_WORTHLESS))
1469         {
1470                 before_str[before_n++] = "worthless";
1471                 which_str[which_n++] = "can not be sold at stores";
1472         }
1473
1474         /*** Artifacto ***/
1475         if (IS_FLG(FLG_ARTIFACT))
1476         {
1477                 before_str[before_n++] = "artifact";
1478         }
1479
1480         /*** Ego ***/
1481         if (IS_FLG(FLG_EGO))
1482         {
1483                 before_str[before_n++] = "ego";
1484         }
1485
1486         /*** Nameless ***/
1487         if (IS_FLG(FLG_NAMELESS))
1488         {
1489                 body_str = "equipment";
1490                 which_str[which_n++] = "is neither ego-item nor artifact";
1491         }
1492
1493         /*** Unaware items ***/
1494         if (IS_FLG(FLG_UNAWARE))
1495         {
1496                 before_str[before_n++] = "unidentified";
1497                 whose_str[whose_n++] = "basic abilities are not known";
1498         }
1499
1500         /*** Dice boosted (weapon of slaying) ***/
1501         if (IS_FLG(FLG_BOOSTED))
1502         {
1503                 body_str = "weapons";
1504                 whose_str[whose_n++] = "damage dice is bigger than normal";
1505         }
1506
1507         /*** Weapons whose dd*ds is more than nn ***/
1508         if (IS_FLG(FLG_MORE_THAN))
1509         {
1510                 static char more_than_desc_str[] =
1511                         "maximum damage from dice is bigger than __";
1512                 body_str = "weapons";
1513                         
1514                 sprintf(more_than_desc_str + sizeof(more_than_desc_str) - 3,
1515                         "%d", entry->dice);
1516                 whose_str[whose_n++] = more_than_desc_str;
1517         }
1518
1519         /*** Items whose magical bonus is more than nn ***/
1520         if (IS_FLG(FLG_MORE_BONUS))
1521         {
1522                 static char more_bonus_desc_str[] =
1523                         "magical bonus is bigger than (+__)";
1524                         
1525                 sprintf(more_bonus_desc_str + sizeof(more_bonus_desc_str) - 4,
1526                         "%d)", entry->bonus);
1527                 whose_str[whose_n++] = more_bonus_desc_str;
1528         }
1529
1530         /*** Wanted monster's corpse/skeletons ***/
1531         if (IS_FLG(FLG_WANTED))
1532         {
1533                 body_str = "corpse or skeletons";
1534                 which_str[which_n++] = "is wanted at the Hunter's Office";
1535         }
1536
1537         /*** Human corpse/skeletons (for Daemon magic) ***/
1538         if (IS_FLG(FLG_HUMAN))
1539         {
1540                 before_str[before_n++] = "humanoid";
1541                 body_str = "corpse or skeletons";
1542                 which_str[which_n++] = "can be used for Daemon magic";
1543         }
1544
1545         /*** Unique monster's corpse/skeletons/statues ***/
1546         if (IS_FLG(FLG_UNIQUE))
1547         {
1548                 before_str[before_n++] = "unique monster's";
1549                 body_str = "corpse or skeletons";
1550         }
1551
1552         /*** Unreadable spellbooks ***/
1553         if (IS_FLG(FLG_UNREADABLE))
1554         {
1555                 body_str = "spellbooks";
1556                 after_str[after_n++] = "of different realms from yours";
1557         }
1558
1559         /*** First realm spellbooks ***/
1560         if (IS_FLG(FLG_REALM1))
1561         {
1562                 body_str = "spellbooks";
1563                 after_str[after_n++] = "of your first realm";
1564         }
1565
1566         /*** Second realm spellbooks ***/
1567         if (IS_FLG(FLG_REALM2))
1568         {
1569                 body_str = "spellbooks";
1570                 after_str[after_n++] = "of your second realm";
1571         }
1572
1573         /*** First rank spellbooks ***/
1574         if (IS_FLG(FLG_FIRST))
1575         {
1576                 before_str[before_n++] = "first one of four";
1577                 body_str = "spellbooks";
1578         }
1579
1580         /*** Second rank spellbooks ***/
1581         if (IS_FLG(FLG_SECOND))
1582         {
1583                 before_str[before_n++] = "second one of four";
1584                 body_str = "spellbooks";
1585         }
1586
1587         /*** Third rank spellbooks ***/
1588         if (IS_FLG(FLG_THIRD))
1589         {
1590                 before_str[before_n++] = "third one of four";
1591                 body_str = "spellbooks";
1592         }
1593
1594         /*** Fourth rank spellbooks ***/
1595         if (IS_FLG(FLG_FOURTH))
1596         {
1597                 before_str[before_n++] = "fourth one of four";
1598                 body_str = "spellbooks";
1599         }
1600
1601         /*** Items ***/
1602         if (IS_FLG(FLG_ITEMS))
1603                 ; /* Nothing to do */
1604         else if (IS_FLG(FLG_WEAPONS))
1605                 body_str = "weapons";
1606         else if (IS_FLG(FLG_ARMORS))
1607                 body_str = "armors";
1608         else if (IS_FLG(FLG_MISSILES))
1609                 body_str = "shots, arrows or crossbow bolts";
1610         else if (IS_FLG(FLG_DEVICES))
1611                 body_str = "scrolls, wands, staves or rods";
1612         else if (IS_FLG(FLG_LIGHTS))
1613                 body_str = "light sources";
1614         else if (IS_FLG(FLG_JUNKS))
1615                 body_str = "junk such as broken sticks";
1616         else if (IS_FLG(FLG_SPELLBOOKS))
1617                 body_str = "spellbooks";
1618         else if (IS_FLG(FLG_HAFTED))
1619                 body_str = "hafted weapons";
1620         else if (IS_FLG(FLG_SHIELDS))
1621                 body_str = "shields";
1622         else if (IS_FLG(FLG_BOWS))
1623                 body_str = "slings, bows or crossbows";
1624         else if (IS_FLG(FLG_RINGS))
1625                 body_str = "rings";
1626         else if (IS_FLG(FLG_AMULETS))
1627                 body_str = "amulets";
1628         else if (IS_FLG(FLG_SUITS))
1629                 body_str = "body armors";
1630         else if (IS_FLG(FLG_CLOAKS))
1631                 body_str = "cloaks";
1632         else if (IS_FLG(FLG_HELMS))
1633                 body_str = "helms or crowns";
1634         else if (IS_FLG(FLG_GLOVES))
1635                 body_str = "gloves";
1636         else if (IS_FLG(FLG_BOOTS))
1637                 body_str = "boots";
1638
1639         /* Prepare a string for item name */
1640         if (*str)
1641         {
1642                 if (*str == '^')
1643                 {
1644                         str++;
1645                         top = TRUE;
1646                         whose_str[whose_n++] = "name is beginning with \"";
1647                 }
1648                 else
1649                         which_str[which_n++] = "have \"";
1650         }
1651
1652
1653         /* Describe action flag */
1654         if (act & DONT_AUTOPICK)
1655                 strcpy(buff, "Leave on floor ");
1656         else if (act & DO_AUTODESTROY)
1657                 strcpy(buff, "Destroy ");
1658         else if (act & DO_QUERY_AUTOPICK)
1659                 strcpy(buff, "Ask to pick up ");
1660         else
1661                 strcpy(buff, "Pickup ");
1662
1663         /* Auto-insctiption */
1664         if (insc)
1665         {
1666                 strncat(buff, format("and inscribe \"%s\"", insc), 80);
1667
1668                 if (strstr(insc, "%all"))
1669                         strcat(buff, ", replacing %all with code string representing all abilities,");
1670                 else if (strstr(insc, "%"))
1671                         strcat(buff, ", replacing % with code string representing extra random abilities,");
1672
1673                 strcat(buff, " on ");
1674         }
1675
1676         /* Adjective */
1677         if (!before_n) 
1678                 strcat(buff, "all ");
1679         else for (i = 0; i < before_n && before_str[i]; i++)
1680         {
1681                 strcat(buff, before_str[i]);
1682                 strcat(buff, " ");
1683         }
1684
1685         /* Item class */
1686         strcat(buff, body_str);
1687
1688         /* of ... */
1689         for (i = 0; i < after_n && after_str[i]; i++)
1690         {
1691                 strcat(buff, " ");
1692                 strcat(buff, after_str[i]);
1693         }
1694
1695         /* which ... */
1696         for (i = 0; i < whose_n && whose_str[i]; i++)
1697         {
1698                 if (i == 0)
1699                         strcat(buff, " whose ");
1700                 else
1701                         strcat(buff, ", and ");
1702
1703                 strcat(buff, whose_str[i]);
1704         }
1705
1706         /* Item name ; whose name is beginning with "str" */
1707         if (*str && top)
1708         {
1709                 strcat(buff, str);
1710                 strcat(buff, "\"");
1711         }
1712
1713         /* whose ..., and which .... */
1714         if (whose_n && which_n)
1715                 strcat(buff, ", and ");
1716
1717         /* which ... */
1718         for (i = 0; i < which_n && which_str[i]; i++)
1719         {
1720                 if (i == 0)
1721                         strcat(buff, " which ");
1722                 else
1723                         strcat(buff, ", and ");
1724
1725                 strcat(buff, which_str[i]);
1726         }
1727
1728         /* Item name ; which have "str" as part of its name */
1729         if (*str && !top)
1730         {
1731                 strncat(buff, str, 80);
1732                 strcat(buff, "\" as part of its name");
1733         }
1734         strcat(buff, ".");
1735
1736         /* Describe whether it will be displayed on the full map or not */
1737         if (act & DO_DISPLAY)
1738         {
1739                 if (act & DONT_AUTOPICK)
1740                         strcat(buff, "  Display these items when you press the N key in the full 'M'ap.");
1741                 else if (act & DO_AUTODESTROY)
1742                         strcat(buff, "  Display these items when you press the K key in the full 'M'ap.");
1743                 else
1744                         strcat(buff, "  Display these items when you press the M key in the full 'M'ap.");
1745         }
1746         else
1747                 strcat(buff, " Not displayed in the full map.");
1748 #endif /* JP */
1749
1750 }
1751
1752
1753 #define MAX_LINES 3000
1754
1755 /*
1756  * Read whole lines of a file to memory
1757  */
1758 static cptr *read_text_lines(cptr filename, bool user)
1759 {
1760         cptr *lines_list = NULL;
1761         FILE *fff;
1762
1763         int lines = 0;
1764         char buf[1024];
1765
1766         if (user)
1767         {
1768                 /* Hack -- drop permissions */
1769                 safe_setuid_drop();
1770                 path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
1771         }
1772         else
1773         {
1774                 path_build(buf, sizeof(buf), ANGBAND_DIR_PREF, filename);
1775         }
1776         
1777         /* Open the file */
1778         fff = my_fopen(buf, "r");
1779
1780         if (fff)
1781         {
1782                 /* Allocate list of pointers */
1783                 C_MAKE(lines_list, MAX_LINES, cptr);
1784
1785                 /* Parse it */
1786                 while (0 == my_fgets(fff, buf, sizeof(buf)))
1787                 {
1788                         lines_list[lines++] = string_make(buf);
1789                         if (lines >= MAX_LINES - 1) break;
1790                 }
1791                 if (lines == 0)
1792                         lines_list[0] = string_make("");
1793
1794                 my_fclose(fff);
1795         }
1796
1797         /* Grab priv's */
1798         safe_setuid_grab();
1799
1800         if (!fff) return NULL;
1801         return lines_list;
1802 }
1803
1804
1805 #define PT_DEFAULT 0
1806 #define PT_WITH_PNAME 1
1807
1808 static cptr *read_pickpref_text_lines(int *filename_mode_p)
1809 {
1810         char buf[1024];
1811         cptr *lines_list;
1812
1813 #ifdef JP
1814         sprintf(buf, "picktype-%s.prf", player_name);
1815 #else
1816         sprintf(buf, "pickpref-%s.prf", player_name);
1817 #endif
1818         lines_list = read_text_lines(buf, TRUE);
1819
1820         if (!lines_list)
1821         {
1822 #ifdef JP
1823                 lines_list = read_text_lines("picktype.prf", TRUE);
1824 #else
1825                 lines_list = read_text_lines("pickpref.prf", TRUE);
1826 #endif
1827                 *filename_mode_p = PT_DEFAULT;
1828         }
1829
1830         if (!lines_list)
1831         {
1832 #ifdef JP
1833                 lines_list = read_text_lines("picktype.prf", FALSE);
1834 #else
1835                 lines_list = read_text_lines("pickpref.prf", FALSE);
1836 #endif
1837                 *filename_mode_p = PT_WITH_PNAME;
1838         }
1839
1840         if (!lines_list)
1841         {
1842                 /* Allocate list of pointers */
1843                 C_MAKE(lines_list, MAX_LINES, cptr);
1844                 lines_list[0] = string_make("");
1845                 *filename_mode_p = PT_WITH_PNAME;
1846         }
1847         return lines_list;
1848 }
1849
1850 /*
1851  * Write whole lines of memory to a file.
1852  */
1853 static bool write_text_lines(cptr filename, cptr *lines_list)
1854 {
1855         FILE *fff;
1856
1857         int lines = 0;
1858         char buf[1024];
1859
1860         /* Hack -- drop permissions */
1861         safe_setuid_drop();
1862
1863         /* Build the filename */
1864         path_build(buf, sizeof(buf), ANGBAND_DIR_USER, filename);
1865         
1866         /* Open the file */
1867         fff = my_fopen(buf, "w");
1868         if (fff)
1869         {
1870                 for (lines = 0; lines_list[lines]; lines++)
1871                         my_fputs(fff, lines_list[lines], 1024);
1872
1873                 my_fclose(fff);
1874         }
1875
1876         /* Grab priv's */
1877         safe_setuid_grab();
1878
1879         if (!fff) return FALSE;
1880         return TRUE;
1881 }
1882
1883
1884 /*
1885  * Free memory of lines_list.
1886  */
1887 static void free_text_lines(cptr *lines_list)
1888 {
1889         int lines;
1890
1891         for (lines = 0; lines_list[lines]; lines++)
1892                 string_free(lines_list[lines]);
1893
1894         /* free list of pointers */
1895         C_FREE((char **)lines_list, MAX_LINES, char *);
1896 }
1897
1898
1899 /*
1900  * Delete or insert string
1901  */
1902 static void toggle_string(cptr *lines_list, int flg, int y)
1903 {
1904         autopick_type an_entry, *entry = &an_entry;
1905
1906         if (!autopick_new_entry(entry, lines_list[y]))
1907                 return;
1908
1909         string_free(lines_list[y]);
1910         if (IS_FLG(flg)) 
1911                 REM_FLG(flg);
1912         else
1913                 ADD_FLG(flg);
1914
1915         lines_list[y] = autopick_line_from_entry_kill(entry);
1916 }
1917
1918 /*
1919  * Insert return code and split the line
1920  */
1921 static bool insert_return_code(cptr *lines_list, int cx, int cy)
1922 {
1923         char buf[MAX_LINELEN];
1924         int i, j, k;
1925
1926         for (k = 0; lines_list[k]; k++)
1927                 /* count number of lines */ ;
1928
1929         if (k >= MAX_LINES - 2) return FALSE;
1930         k--;
1931
1932         /* Move down lines */
1933         for (; cy < k; k--)
1934                 lines_list[k+1] = lines_list[k];
1935
1936         /* Split current line */
1937         for (i = j = 0; lines_list[cy][i] && i < cx; i++)
1938         {
1939 #ifdef JP
1940                 if (iskanji(lines_list[cy][i]))
1941                         buf[j++] = lines_list[cy][i++];
1942 #endif
1943                 buf[j++] = lines_list[cy][i];
1944         }
1945         buf[j] = '\0';
1946         lines_list[cy+1] = string_make(&lines_list[cy][i]);
1947         string_free(lines_list[cy]);
1948         lines_list[cy] = string_make(buf);
1949         return TRUE;
1950 }
1951
1952
1953 /*
1954  * Get auto-picker entry from o_ptr.
1955  */
1956 void autopick_entry_from_object(autopick_type *entry, object_type *o_ptr)
1957 {
1958         char o_name[MAX_NLEN];
1959         object_desc(o_name, o_ptr, FALSE, 0);
1960
1961         entry->name = string_make(o_name);
1962         entry->insc = string_make(quark_str(o_ptr->inscription));
1963         entry->action = DO_AUTOPICK | DO_DISPLAY;
1964         entry->flag[0] = entry->flag[1] = 0L;
1965         entry->dice = 0;
1966
1967         if (!object_aware_p(o_ptr))
1968                 ADD_FLG(FLG_UNAWARE);
1969         if (object_value(o_ptr) <= 0)
1970                 ADD_FLG(FLG_WORTHLESS);
1971
1972         if (object_known_p(o_ptr))
1973         {
1974                 if (o_ptr->name2)
1975                         ADD_FLG(FLG_EGO);
1976                 else if (o_ptr->name1 || o_ptr->art_name)
1977                         ADD_FLG(FLG_ARTIFACT);
1978         }
1979
1980         switch(o_ptr->tval)
1981         {
1982                 object_kind *k_ptr; 
1983         case TV_HAFTED: case TV_POLEARM: case TV_SWORD: case TV_DIGGING:
1984                 k_ptr = &k_info[o_ptr->k_idx];
1985                 if ((o_ptr->dd != k_ptr->dd) || (o_ptr->ds != k_ptr->ds))
1986                         ADD_FLG(FLG_BOOSTED);
1987         }
1988
1989         if (o_ptr->tval == TV_CORPSE && object_is_shoukinkubi(o_ptr))
1990         {
1991                 REM_FLG(FLG_WORTHLESS);
1992                 ADD_FLG(FLG_WANTED);
1993         }
1994
1995         if ((o_ptr->tval == TV_CORPSE || o_ptr->tval == TV_STATUE)
1996             && (r_info[o_ptr->pval].flags1 & RF1_UNIQUE))
1997         {
1998                 REM_FLG(FLG_WORTHLESS);
1999                 ADD_FLG(FLG_UNIQUE);
2000         }
2001
2002         if (o_ptr->tval == TV_CORPSE && strchr("pht", r_info[o_ptr->pval].d_char))
2003         {
2004                 REM_FLG(FLG_WORTHLESS);
2005                 ADD_FLG(FLG_HUMAN);
2006         }
2007
2008         if (o_ptr->tval >= TV_LIFE_BOOK &&
2009             !check_book_realm(o_ptr->tval, o_ptr->sval))
2010                 ADD_FLG(FLG_UNREADABLE);
2011
2012         if (REALM1_BOOK == o_ptr->tval &&
2013             p_ptr->pclass != CLASS_SORCERER &&
2014             p_ptr->pclass != CLASS_RED_MAGE)
2015                 ADD_FLG(FLG_REALM1);
2016
2017         if (REALM2_BOOK == o_ptr->tval &&
2018             p_ptr->pclass != CLASS_SORCERER &&
2019             p_ptr->pclass != CLASS_RED_MAGE)
2020                 ADD_FLG(FLG_REALM2);
2021
2022         if (o_ptr->tval >= TV_LIFE_BOOK && 0 == o_ptr->sval)
2023                 ADD_FLG(FLG_FIRST);
2024         if (o_ptr->tval >= TV_LIFE_BOOK && 1 == o_ptr->sval)
2025                 ADD_FLG(FLG_SECOND);
2026         if (o_ptr->tval >= TV_LIFE_BOOK && 2 == o_ptr->sval)
2027                 ADD_FLG(FLG_THIRD);
2028         if (o_ptr->tval >= TV_LIFE_BOOK && 3 == o_ptr->sval)
2029                 ADD_FLG(FLG_FOURTH);
2030
2031         if (o_ptr->tval == TV_SHOT || o_ptr->tval == TV_BOLT
2032                  || o_ptr->tval == TV_ARROW)
2033                 ADD_FLG(FLG_MISSILES);
2034         else if (o_ptr->tval == TV_SCROLL || o_ptr->tval == TV_STAFF
2035                  || o_ptr->tval == TV_WAND || o_ptr->tval == TV_ROD)
2036                 ADD_FLG(FLG_DEVICES);
2037         else if (o_ptr->tval == TV_LITE)
2038                 ADD_FLG(FLG_LIGHTS);
2039         else if (o_ptr->tval == TV_SKELETON || o_ptr->tval == TV_BOTTLE
2040                  || o_ptr->tval == TV_JUNK || o_ptr->tval == TV_STATUE)
2041                 ADD_FLG(FLG_JUNKS);
2042         else if (o_ptr->tval >= TV_LIFE_BOOK)
2043                 ADD_FLG(FLG_SPELLBOOKS);
2044         else if (o_ptr->tval == TV_HAFTED)
2045                 ADD_FLG(FLG_HAFTED);
2046         else if (o_ptr->tval == TV_POLEARM || o_ptr->tval == TV_SWORD
2047                  || o_ptr->tval == TV_DIGGING)
2048                 ADD_FLG(FLG_WEAPONS);
2049         else if (o_ptr->tval == TV_SHIELD)
2050                 ADD_FLG(FLG_SHIELDS);
2051         else if (o_ptr->tval == TV_BOW)
2052                 ADD_FLG(FLG_BOWS);
2053         else if (o_ptr->tval == TV_RING)
2054                 ADD_FLG(FLG_RINGS);
2055         else if (o_ptr->tval == TV_AMULET)
2056                 ADD_FLG(FLG_AMULETS);
2057         else if (o_ptr->tval == TV_DRAG_ARMOR || o_ptr->tval == TV_HARD_ARMOR ||
2058                  o_ptr->tval == TV_SOFT_ARMOR)
2059                 ADD_FLG(FLG_SUITS);
2060         else if (o_ptr->tval == TV_CLOAK)
2061                 ADD_FLG(FLG_CLOAKS);
2062         else if (o_ptr->tval == TV_HELM)
2063                 ADD_FLG(FLG_HELMS);
2064         else if (o_ptr->tval == TV_GLOVES)
2065                 ADD_FLG(FLG_GLOVES);
2066         else if (o_ptr->tval == TV_BOOTS)
2067                 ADD_FLG(FLG_BOOTS);
2068
2069         return;
2070 }
2071
2072
2073 /*
2074  * Choose an item and get auto-picker entry from it.
2075  */
2076 static object_type *choose_object(cptr q, cptr s)
2077 {
2078         int item;
2079
2080         if (!get_item(&item, q, s, (USE_INVEN | USE_FLOOR | USE_EQUIP))) return NULL;
2081
2082         /* Get the item (in the pack) */
2083         if (item >= 0) return &inventory[item];
2084
2085         /* Get the item (on the floor) */
2086         else return &o_list[0 - item];
2087 }
2088
2089
2090 /*
2091  * Choose an item and get auto-picker entry from it.
2092  */
2093 static bool entry_from_choosed_object(autopick_type *entry)
2094 {
2095         object_type *o_ptr;
2096         cptr q, s;
2097
2098         /* Get an item */
2099 #ifdef JP
2100         q = "¤É¤Î¥¢¥¤¥Æ¥à¤òÅÐÏ¿¤·¤Þ¤¹¤«? ";
2101         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
2102 #else
2103         q = "Entry which item? ";
2104         s = "You have nothing to entry.";
2105 #endif
2106         o_ptr = choose_object(q, s);
2107         if (!o_ptr) return FALSE;
2108
2109         autopick_entry_from_object(entry, o_ptr);
2110         return TRUE;
2111 }
2112
2113
2114 /*
2115  * Choose an item or string for search
2116  */
2117 static bool get_string_for_search(object_type **o_handle, cptr *search_strp)
2118 {
2119         int pos = 0;
2120         cptr q, s;
2121         char buf[MAX_NLEN+20];
2122
2123 #ifdef JP
2124         int k_flag[MAX_NLEN+20];
2125         char prompt[] = "¸¡º÷(^I:»ý¤Áʪ ^L:Ç˲õ¤µ¤ì¤¿Êª): ";
2126 #else
2127         char prompt[] = "Search key(^I:inven ^L:destroyed): ";
2128 #endif
2129         int col = sizeof(prompt) - 1;
2130
2131         if (*search_strp) strcpy(buf, *search_strp);
2132         else buf[0] = '\0';
2133
2134         /* Display prompt */
2135         prt(prompt, 0, 0);
2136
2137         /* Display the default answer */
2138         Term_erase(col, 0, 255);
2139         Term_putstr(col, 0, -1, TERM_YELLOW, buf);
2140
2141         /* Process input */
2142         while (1)
2143         {
2144                 object_type *o_ptr;
2145                 int i;
2146
2147                 /* Place cursor */
2148                 Term_gotoxy(col + pos, 0);
2149
2150                 /* Do not process macros except special keys */
2151                 inkey_special = TRUE;
2152
2153                 /* Get a key */
2154                 i = inkey();
2155
2156                 /* Analyze the key */
2157                 switch (i)
2158                 {
2159                 case ESCAPE:
2160                         pos = 0;
2161                         return FALSE;
2162
2163                 case '\n':
2164                 case '\r':
2165                         if (!pos && *o_handle) return TRUE;
2166                         string_free(*search_strp);
2167                         *search_strp = string_make(buf);
2168                         *o_handle = NULL;
2169                         return TRUE;
2170
2171                 case KTRL('i'):
2172                         /* Get an item */
2173 #ifdef JP
2174                         q = "¤É¤Î¥¢¥¤¥Æ¥à¤ò¸¡º÷¤·¤Þ¤¹¤«? ";
2175                         s = "¥¢¥¤¥Æ¥à¤ò»ý¤Ã¤Æ¤¤¤Ê¤¤¡£";
2176 #else
2177                         q = "Entry which item? ";
2178                         s = "You have nothing to entry.";
2179 #endif
2180                         o_ptr = choose_object(q, s);
2181                         if (!o_ptr) return FALSE;
2182
2183                         *o_handle = o_ptr;
2184
2185                         string_free(*search_strp);
2186                         object_desc(buf, *o_handle, FALSE, 3);
2187                         *search_strp = string_make(format("<%s>", buf));
2188                         return TRUE;
2189
2190                 case KTRL('l'):
2191                         if (!autopick_last_destroyed_object.k_idx) break;
2192                         *o_handle = &autopick_last_destroyed_object;
2193
2194                         string_free(*search_strp);
2195                         object_desc(buf, *o_handle, FALSE, 3);
2196                         *search_strp = string_make(format("<%s>", buf));
2197                         return TRUE;
2198
2199                 case 0x7F:
2200                 case '\010':
2201 #ifdef JP
2202                         if (pos > 0)
2203                         {
2204                                 pos--;
2205                                 if (k_flag[pos]) pos--;
2206                         }
2207 #else
2208                         if (pos > 0) pos--;
2209 #endif
2210                         break;
2211
2212                 default:
2213 #ifdef JP
2214                         if (iskanji(i))
2215                         {
2216                                 int next;
2217
2218                                 inkey_base = TRUE;
2219                                 next = inkey ();
2220                                 if (pos + 1 < MAX_NLEN)
2221                                 {
2222                                         buf[pos++] = i;
2223                                         buf[pos] = next;
2224                                         k_flag[pos++] = 1;
2225                                 }
2226                                 else bell();
2227                         }
2228                         else if (pos < MAX_NLEN && isprint(i))
2229                         {
2230                                 buf[pos] = i;
2231                                 k_flag[pos++] = 0;
2232                         }
2233                         else bell();
2234 #else
2235                         if (pos < MAX_NLEN && isprint(i)) buf[pos++] = i;
2236                         else bell();
2237 #endif
2238                         break;
2239                 }
2240
2241                 /* Terminate */
2242                 buf[pos] = '\0';
2243
2244                 /* Update the entry */
2245                 Term_erase(col, 0, 255);
2246                 Term_putstr(col, 0, -1, TERM_WHITE, buf);
2247         }
2248
2249         /* Never reached */
2250 }
2251
2252
2253 /*
2254  * Search next line matches for o_ptr
2255  */
2256 static bool search_for_object(cptr *lines_list, object_type *o_ptr, int *cxp, int *cyp, bool forward)
2257 {
2258         int i;
2259         autopick_type an_entry, *entry = &an_entry;
2260         char o_name[MAX_NLEN];
2261
2262         object_desc(o_name, o_ptr, FALSE, 3);
2263
2264         /* Force to be lower case string */
2265         for (i = 0; o_name[i]; i++)
2266         {
2267 #ifdef JP
2268                 if (iskanji(o_name[i]))
2269                         i++;
2270                 else
2271 #endif
2272                 if (isupper(o_name[i]))
2273                         o_name[i] = tolower(o_name[i]);
2274         }
2275         
2276         i = *cyp;
2277
2278         while (1)
2279         {
2280                 if (forward)
2281                 {
2282                         if (!lines_list[++i]) break;
2283                 }
2284                 else
2285                 {
2286                         if (--i < 0) break;
2287                 }
2288
2289                 if (!autopick_new_entry(entry, lines_list[i])) continue;
2290
2291                 if (is_autopick_aux(o_ptr, entry, o_name))
2292                 {
2293                         *cxp = 0;
2294                         *cyp = i;
2295                         return TRUE;
2296                 }
2297         }
2298
2299         return FALSE;
2300 }
2301
2302
2303 /*
2304  * Search next line matches to the string
2305  */
2306 static bool search_for_string(cptr *lines_list, cptr search_str, int *cxp, int *cyp, bool forward)
2307 {
2308         int i = *cyp;
2309
2310         while (1)
2311         {
2312                 cptr pos;
2313
2314                 if (forward)
2315                 {
2316                         if (!lines_list[++i]) break;
2317                 }
2318                 else
2319                 {
2320                         if (--i < 0) break;
2321                 }
2322 #ifdef JP
2323                 pos = strstr_j(lines_list[i], search_str);
2324 #else
2325                 pos = strstr(lines_list[i], search_str);
2326 #endif
2327                 if (pos)
2328                 {
2329                         *cxp = (int)(pos - lines_list[i]);
2330                         *cyp = i;
2331                         return TRUE;
2332                 }
2333         }
2334
2335         return FALSE;
2336 }
2337
2338
2339 /*
2340  * Initialize auto-picker preference
2341  */
2342 void init_autopicker(void)
2343 {
2344         static const char easy_autopick_inscription[] = "(:=g";
2345         autopick_type entry;
2346         int i;
2347
2348         /* Clear old entries */
2349         for( i = 0; i < max_autopick; i++)
2350                 autopick_free_entry(&autopick_list[i]);
2351
2352         max_autopick = 0;
2353
2354         /* There is always one entry "=g" */
2355         autopick_new_entry(&entry, easy_autopick_inscription);
2356         autopick_list[max_autopick++] = entry;
2357 }
2358
2359
2360
2361 /*
2362  *  Process line for auto picker/destroyer.
2363  */
2364 errr process_pickpref_file_line(char *buf)
2365 {
2366         autopick_type entry;
2367         int i;
2368
2369         if (max_autopick == MAX_AUTOPICK)
2370                 return 1;
2371         
2372         /* Nuke illegal char */
2373         for(i = 0; buf[i]; i++)
2374         {
2375 #ifdef JP
2376                 if (iskanji(buf[i]))
2377                 {
2378                         i++;
2379                         continue;
2380                 }
2381 #endif
2382                 if (isspace(buf[i]) && buf[i] != ' ')
2383                         break;
2384         }
2385         buf[i] = 0;
2386         
2387         if (!autopick_new_entry(&entry, buf)) return 0;
2388
2389         /* Already has the same entry? */ 
2390         for(i = 0; i < max_autopick; i++)
2391                 if(!strcmp(entry.name, autopick_list[i].name)
2392                    && entry.flag[0] == autopick_list[i].flag[0]
2393                    && entry.flag[1] == autopick_list[i].flag[1]
2394                    && entry.dice == autopick_list[i].dice
2395                    && entry.bonus == autopick_list[i].bonus) return 0;
2396
2397         autopick_list[max_autopick++] = entry;
2398         return 0;
2399 }
2400
2401
2402 /*
2403  * Get a trigger key and insert ASCII string for the trigger
2404  */
2405 static bool insert_macro_line(cptr *lines_list, int cy)
2406 {
2407         char tmp[1024];
2408         char buf[1024];
2409         int i, n = 0;
2410
2411         /* Flush */
2412         flush();
2413
2414         /* Do not process macros */
2415         inkey_base = TRUE;
2416
2417         /* First key */
2418         i = inkey();
2419
2420         /* Read the pattern */
2421         while (i)
2422         {
2423                 /* Save the key */
2424                 buf[n++] = i;
2425
2426                 /* Do not process macros */
2427                 inkey_base = TRUE;
2428
2429                 /* Do not wait for keys */
2430                 inkey_scan = TRUE;
2431
2432                 /* Attempt to read a key */
2433                 i = inkey();
2434         }
2435
2436         /* Terminate */
2437         buf[n] = '\0';
2438
2439         /* Flush */
2440         flush();
2441
2442         /* Convert the trigger */
2443         ascii_to_text(tmp, buf);
2444
2445         /* Null */
2446         if(!tmp[0]) return FALSE;
2447
2448         /* Insert preference string */
2449         insert_return_code(lines_list, 0, cy);
2450         string_free(lines_list[cy]);
2451         lines_list[cy] = string_make(format("P:%s", tmp));
2452
2453         /* Insert blank action preference line */
2454         insert_return_code(lines_list, 0, cy);
2455         string_free(lines_list[cy]);
2456         lines_list[cy] = string_make("A:");
2457
2458         return TRUE;
2459 }
2460
2461
2462 /*
2463  * Get a command key and insert ASCII string for the key
2464  */
2465 static bool insert_keymap_line(cptr *lines_list, int cy)
2466 {
2467         char tmp[1024];
2468         char buf[2];
2469         int mode;
2470
2471         /* Roguelike */
2472         if (rogue_like_commands)
2473         {
2474                 mode = KEYMAP_MODE_ROGUE;
2475         }
2476
2477         /* Original */
2478         else
2479         {
2480                 mode = KEYMAP_MODE_ORIG;
2481         }
2482
2483         /* Flush */
2484         flush();
2485
2486         /* Get a key */
2487         buf[0] = inkey();
2488         buf[1] = '\0';
2489
2490         /* Flush */
2491         flush();
2492
2493         /* Convert the trigger */
2494         ascii_to_text(tmp, buf);
2495
2496         /* Null */
2497         if(!tmp[0]) return FALSE;
2498
2499         /* Insert preference string */
2500         insert_return_code(lines_list, 0, cy);
2501         string_free(lines_list[cy]);
2502         lines_list[cy] = string_make(format("C:%d:%s", mode, tmp));
2503
2504         /* Insert blank action preference line */
2505         insert_return_code(lines_list, 0, cy);
2506         string_free(lines_list[cy]);
2507         lines_list[cy] = string_make("A:");
2508
2509         return TRUE;
2510 }
2511
2512
2513 /*
2514  * Description of control commands
2515  */
2516
2517 #define WID_DESC 31
2518
2519 static cptr ctrl_command_desc[] =
2520 {
2521 #ifdef JP
2522 #define LAST_DESTROYED 6
2523         "^P ^N ^B ^F ¾å²¼º¸±¦¤Ë°ÜÆ°",
2524         "^A ^E ¹Ô¤ÎÀèƬ¡¢½ªÃ¼",
2525         "^Q ÆþÎÏ/¥³¥Þ¥ó¥É¥â¡¼¥ÉÀÚ¤êÂؤ¨",
2526         "^R Êѹ¹¤òÁ´¤Æ¼è¤ê¾Ã¤·¤Æ¸µ¤ËÌ᤹",
2527         "------------------------------------",
2528         "^I »ý¤Áʪ/ÁõÈ÷¤«¤éÁªÂò",
2529         "^L",
2530         "^K ¥«¡¼¥½¥ë¤«¤é½ªÃ¼¤Þ¤Çºï½ü",
2531         "^Y ºï½ü(^K)¤·¤¿¹Ô¤òÁÞÆþ",
2532         "^C ¼ï²¡¢¿¦¶È¤Î¾ò·ï¼°¤òÁÞÆþ",
2533         "------------------------------------",
2534         "^S Êѹ¹ (!Ç˲õ/~ÊüÃÖ/½¦¤¦)",
2535         "^G \"(\" Á´ÂΥޥåפÇɽ¼¨¤·¤Ê¤¤",
2536         "^O \"#\" ¼«Æ°¹ï¤ß",
2537         "------------------------------------",
2538         "^U Ì¤´ÕÄê/̤ȽÌÀ/´ÕÄê/*´ÕÄê*",
2539         "^W \"̵²ÁÃͤÎ\"",
2540         "^X ÌµÌÃ/¥¨¥´/¥¢¡¼¥Æ¥£¥Õ¥¡¥¯¥È",
2541         "^Z \"¼ý½¸Ãæ¤Î\"",
2542         NULL
2543 #else
2544 #define LAST_DESTROYED 6
2545         "^P ^N ^B ^F Move Cursor",
2546         "^A ^E Beginning and End of Line",
2547         "^Q Toggle Insert/Command mode",
2548         "^R Revert to Original File",
2549         "------------------------------------",
2550         "^I Object in Inventry/Equipment",
2551         "^L",
2552         "^K Kill Rest of Line",
2553         "^Y Insert killed(^K) text",
2554         "^C Insert conditional expression",
2555         "------------------------------------",
2556         "^S Toggle(!Destroy/~Leave/Pick)",
2557         "^G \"(\" No display in the 'M'ap",
2558         "^O \"#\" Auto-Inscribe",
2559         "------------------------------------",
2560         "^U Toggle 'identified' state",
2561         "^W \"worthless\"",
2562         "^X Toggle nameless/ego/artifact",
2563         "^Z \"collecting\"",
2564         NULL
2565 #endif
2566 };
2567
2568
2569 #define MAX_YANK MAX_LINELEN
2570 #define DIRTY_ALL 0x01
2571 #define DIRTY_COMMAND 0x02
2572 #define DIRTY_MODE 0x04
2573 #define DIRTY_SCREEN 0x08
2574 #define DIRTY_NOT_FOUND 0x10
2575 #define DIRTY_NO_SEARCH 0x20
2576
2577 /*
2578  * In-game editor of Object Auto-picker/Destoryer
2579  */
2580 void do_cmd_edit_autopick(void)
2581 {
2582         static int cx = 0, cy = 0;
2583         static int upper = 0, left = 0;
2584
2585         object_type *search_o_ptr = NULL;
2586         cptr search_str = NULL;
2587         cptr last_destroyed = NULL;
2588         char last_destroyed_command[WID_DESC+3];
2589         char yank_buf[MAX_YANK];
2590         char classrace[80];
2591         autopick_type an_entry, *entry = &an_entry;
2592         char buf[MAX_LINELEN];
2593         cptr *lines_list;
2594         int filename_mode = PT_WITH_PNAME;
2595
2596         int i, j, k, len;
2597         cptr tmp;
2598
2599         int old_upper = -1, old_left = -1;
2600         int old_cy = -1;
2601         int key = -1, old_key;
2602         bool repeated_clearing = FALSE;
2603         bool edit_mode = FALSE;
2604
2605         byte dirty_flags = DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE;
2606         int dirty_line = -1;
2607
2608         int wid, hgt, old_wid = -1, old_hgt = -1;
2609
2610         static s32b old_autosave_turn = 0L;
2611
2612         /* Autosave */
2613         if (turn > old_autosave_turn + 100L)
2614         {
2615                 do_cmd_save_game(TRUE);
2616                 old_autosave_turn = turn;
2617         }
2618
2619         /* HACK -- Reset start_time to stop counting playtime while edit */
2620         update_playtime();
2621
2622         /* Free old entries */
2623         init_autopicker();
2624
2625         /* Command Description of the 'Last Destroyed Item' */
2626         if (autopick_last_destroyed_object.k_idx)
2627         {
2628                 autopick_entry_from_object(entry, &autopick_last_destroyed_object);
2629                 last_destroyed = autopick_line_from_entry_kill(entry);
2630
2631                 my_strcpy(last_destroyed_command, format("^L \"%s\"", last_destroyed), sizeof(last_destroyed_command));
2632         }
2633         else
2634         {
2635 #ifdef JP
2636                 strcpy(last_destroyed_command, "^L ºÇ¸å¤Ë¼«Æ°Ç˲õ¤·¤¿¥¢¥¤¥Æ¥à̾");
2637 #else
2638                 strcpy(last_destroyed_command, "^L Last destroyed object");
2639 #endif
2640         }
2641         ctrl_command_desc[LAST_DESTROYED] = last_destroyed_command;
2642
2643         /* Conditional Expression for Class and Race */
2644         sprintf(classrace, "?:[AND [EQU $RACE %s] [EQU $CLASS %s]]", 
2645 #ifdef JP
2646                 rp_ptr->E_title, cp_ptr->E_title
2647 #else
2648                 rp_ptr->title, cp_ptr->title
2649 #endif
2650                 );
2651
2652         /* Clear yank buffer */
2653         yank_buf[0] = '\0';
2654
2655         /* Read or initialize whole text */
2656         lines_list = read_pickpref_text_lines(&filename_mode);
2657
2658         /* Reset cursor position if needed */
2659         for (i = 0; i < cy; i++)
2660         {
2661                 if (!lines_list[i])
2662                 {
2663                         cy = cx = 0;
2664                         break;
2665                 }
2666         }
2667
2668         /* Save the screen */
2669         screen_save();
2670
2671         /* Process requests until done */
2672         while (1)
2673         {
2674                 /* Get size */
2675                 Term_get_size(&wid, &hgt);
2676
2677 #ifdef JP
2678                 /* Don't let cursor at second byte of kanji */
2679                 for (i = 0; lines_list[cy][i]; i++)
2680                         if (iskanji(lines_list[cy][i]))
2681                         {
2682                                 i++;
2683                                 if (i == cx)
2684                                 {
2685                                         cx--;
2686                                         break;
2687                                 }
2688                         }
2689 #endif
2690
2691                 /* Scroll if necessary */
2692                 if (cy < upper || upper + hgt - 4 <= cy)
2693                         upper = cy - (hgt-4)/2;
2694                 if (upper < 0)
2695                         upper = 0;
2696                 if ((cx < left + 10 && left > 0) || left + wid - WID_DESC - 5 <= cx)
2697                         left = cx - (wid - WID_DESC)*2/3;
2698                 if (left < 0)
2699                         left = 0;
2700
2701                 /* Redraw whole window after resize */
2702                 if (old_wid != wid || old_hgt != hgt)
2703                         dirty_flags |= DIRTY_SCREEN;
2704
2705                 /* Redraw all text after scroll */
2706                 else if (old_upper != upper || old_left != left)
2707                         dirty_flags |= DIRTY_ALL;
2708
2709
2710                 if (dirty_flags & DIRTY_SCREEN)
2711                 {
2712                         dirty_flags |= (DIRTY_ALL | DIRTY_COMMAND | DIRTY_MODE);
2713
2714                         /* Clear screen */
2715                         Term_clear();
2716                 }
2717
2718                 if (dirty_flags & DIRTY_COMMAND)
2719                 {
2720                         /* Display control command */
2721                         for (i = 0; ctrl_command_desc[i]; i++)
2722                                 Term_putstr(wid - WID_DESC, i + 1, WID_DESC, TERM_WHITE, ctrl_command_desc[i]);
2723                 }
2724
2725                 /* Redraw mode line */
2726                 if (dirty_flags & DIRTY_MODE)
2727                 {
2728                         int sepa_length = wid - WID_DESC;
2729
2730                         /* Separator */
2731                         for (i = 0; i < sepa_length; i++)
2732                                 buf[i] = '-';
2733                         buf[i] = '\0';
2734
2735                         /* Mode line */
2736                         if (edit_mode)
2737                                 strncpy(buf + sepa_length - 21, " (INSERT MODE)  ", 16);
2738                         else
2739                                 strncpy(buf + sepa_length - 21, " (COMMAND MODE) ", 16);
2740
2741                         Term_putstr(0, hgt - 3, sepa_length, (byte) (edit_mode ? TERM_YELLOW : TERM_WHITE), buf);
2742                 }
2743                 
2744                 /* Dump up to 20, or hgt-4, lines of messages */
2745                 for (i = 0; i < hgt - 4; i++)
2746                 {
2747                         int leftcol = 0;
2748                         cptr msg;
2749
2750                         /* clean or dirty? */
2751                         if (!(dirty_flags & DIRTY_ALL) && (dirty_line != upper+i))
2752                                 continue;
2753
2754                         msg = lines_list[upper+i];
2755                         if (!msg) break;
2756
2757                         /* Apply horizontal scroll */
2758                         for (j = 0; *msg; msg++, j++)
2759                         {
2760                                 if (j == left) break;
2761 #ifdef JP
2762                                 if (j > left)
2763                                 {
2764                                         leftcol = 1;
2765                                         break;
2766                                 }
2767                                 if (iskanji(*msg))
2768                                 {
2769                                         msg++;
2770                                         j++;
2771                                 }
2772 #endif
2773                         }
2774
2775                         /* Erase line */
2776                         Term_erase(0, i + 1, wid - WID_DESC);
2777
2778                         /* Dump the messages, bottom to top */
2779                         Term_putstr(leftcol, i + 1, wid - WID_DESC - 1, TERM_WHITE, msg);
2780                 }
2781
2782                 for (; i < hgt - 4; i++)
2783                 {
2784                         /* Erase line */
2785                         Term_erase(0, i + 1, wid - WID_DESC);
2786                 }
2787
2788                 /* Display header line */
2789 #ifdef JP
2790                 if (edit_mode)
2791                         prt("^Q ESC ¤Ç¥³¥Þ¥ó¥É¥â¡¼¥É¤Ø°Ü¹Ô¡¢Ä̾ï¤Îʸ»ú¤Ï¤½¤Î¤Þ¤ÞÆþÎÏ", 0, 0);
2792                 else
2793                         prt("q _ ¤Ç½ªÎ»¡¢hjkl2468 ¤Ç°ÜÆ°¡¢^Q a i ¤ÇÆþÎϥ⡼¥É¡¢/ n N ¤Ç¸¡º÷", 0, 0);
2794 #else   
2795                 if (edit_mode)
2796                         prt("Press ^Q ESC to command mode, any letters to insert", 0, 0);
2797                 else
2798                         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);
2799 #endif
2800                 /* Display current position */
2801                 prt (format("(%d,%d)", cx, cy), 0, 70);
2802
2803                 /* Display information when updated */
2804                 if (old_cy != cy || (dirty_flags & (DIRTY_ALL | DIRTY_NOT_FOUND | DIRTY_NO_SEARCH)) || dirty_line == cy)
2805                 {
2806                         /* Clear information line */
2807                         Term_erase(0, hgt - 3 + 1, wid);
2808                         Term_erase(0, hgt - 3 + 2, wid);
2809
2810                         /* Display information */
2811                         if (dirty_flags & DIRTY_NOT_FOUND)
2812                         {
2813 #ifdef JP
2814                                 prt(format("¥Ñ¥¿¡¼¥ó¤¬¸«¤Ä¤«¤ê¤Þ¤»¤ó: %s", search_str), hgt - 3 + 1, 0);
2815 #else
2816                                 prt(format("Pattern not found: %s", search_str), hgt - 3 + 1, 0);
2817 #endif
2818                         }
2819                         else if (dirty_flags & DIRTY_NO_SEARCH)
2820                         {
2821 #ifdef JP
2822                                 prt("¸¡º÷Ãæ¤Î¥Ñ¥¿¡¼¥ó¤¬¤¢¤ê¤Þ¤»¤ó('/'¤Ç¸¡º÷)¡£", hgt - 3 + 1, 0);
2823 #else
2824                                 prt("No pattern to search. (Press '/' to search.)", hgt - 3 + 1, 0);
2825 #endif
2826                         }
2827                         else if (lines_list[cy][0] == '#')
2828                         {
2829 #ifdef JP
2830                                 prt("¤³¤Î¹Ô¤Ï¥³¥á¥ó¥È¤Ç¤¹¡£", hgt - 3 + 1, 0);
2831 #else
2832                                 prt("This line is comment.", hgt - 3 + 1, 0);
2833 #endif
2834                         }
2835                         else if (lines_list[cy][1] == ':')
2836                         {
2837                                 switch(lines_list[cy][0])
2838                                 {
2839                                 case '?':
2840 #ifdef JP
2841                                         prt("¤³¤Î¹Ô¤Ï¾ò·ïʬ´ô¼°¤Ç¤¹¡£", hgt - 3 + 1, 0);
2842 #else
2843                                         prt("This line is Conditional Expression.", hgt - 3 + 1, 0);
2844 #endif
2845                                         break;
2846                                 case 'A':
2847 #ifdef JP
2848                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¼Â¹ÔÆâÍƤòÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2849 #else
2850                                         prt("This line defines Macro action.", hgt - 3 + 1, 0);
2851 #endif
2852                                         break;
2853                                 case 'P':
2854 #ifdef JP
2855                                         prt("¤³¤Î¹Ô¤Ï¥Þ¥¯¥í¤Î¥È¥ê¥¬¡¼¡¦¥­¡¼¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2856 #else
2857                                         prt("This line defines Macro trigger key.", hgt - 3 + 1, 0);
2858 #endif
2859                                         break;
2860                                 case 'C':
2861 #ifdef JP
2862                                         prt("¤³¤Î¹Ô¤Ï¥­¡¼ÇÛÃÖ¤òÄêµÁ¤·¤Þ¤¹¡£", hgt - 3 + 1, 0);
2863 #else
2864                                         prt("This line defines Keymap.", hgt - 3 + 1, 0);
2865 #endif
2866                                         break;
2867                                 }
2868                         }
2869
2870                         /* Get description of an autopicker preference line */
2871                         else if (autopick_new_entry(entry, lines_list[cy]))
2872                         {
2873                                 char temp[1024];
2874                                 cptr t;
2875
2876                                 describe_autopick(buf, entry);
2877
2878                                 roff_to_buf(buf, 81, temp);
2879                                 t = temp;
2880                                 for (i = 0; i< 2; i++)
2881                                 {
2882                                         if(t[0] == 0)
2883                                                 break; 
2884                                         else
2885                                         {
2886                                                 prt(t, hgt - 3 + 1 + i, 0);
2887                                                 t += strlen(t) + 1;
2888                                         }
2889                                 }
2890                                 autopick_free_entry(entry);
2891                         }
2892                 }
2893
2894                 /* Place cursor */
2895                 Term_gotoxy(cx - left, cy - upper + 1);
2896
2897                 /* Now clean */
2898                 dirty_flags = 0;
2899                 dirty_line = -1;
2900
2901                 /* Save old key and location */
2902                 old_cy = cy;
2903                 old_key = key;
2904                 old_upper = upper;
2905                 old_left = left;
2906                 old_wid = wid;
2907                 old_hgt = hgt;
2908
2909                 /* Do not process macros except special keys */
2910                 inkey_special = TRUE;
2911
2912                 /* Get a command */
2913                 key = inkey();
2914
2915                 if (edit_mode)
2916                 {
2917                         if (key == ESCAPE)
2918                         {
2919                                 edit_mode = FALSE;
2920
2921                                 /* Mode line is now dirty */
2922                                 dirty_flags |= DIRTY_MODE;
2923                         }
2924
2925                         /* Insert a character */
2926                         else if (!iscntrl(key&0xff))
2927                         {
2928                                 int next;
2929
2930                                 /* Save preceding string */
2931                                 for (i = j = 0; lines_list[cy][i] && i < cx; i++)
2932                                         buf[j++] = lines_list[cy][i];
2933
2934                                 /* Add a character */
2935 #ifdef JP
2936                                 if (iskanji(key))
2937                                 {
2938                                         inkey_base = TRUE;
2939                                         next = inkey();
2940                                         if (j+2 < MAX_LINELEN)
2941                                         {
2942                                                 buf[j++] = key;
2943                                                 buf[j++] = next;
2944                                                 cx += 2;
2945                                         }
2946                                         else
2947                                                 bell();
2948                                 }
2949                                 else
2950 #endif
2951                                 {
2952                                         if (j+1 < MAX_LINELEN)
2953                                                 buf[j++] = key;
2954                                         cx++;
2955                                 }
2956
2957                                 /* Add following */
2958                                 for (; lines_list[cy][i] && j + 1 < MAX_LINELEN; i++)
2959                                         buf[j++] = lines_list[cy][i];
2960                                 buf[j] = '\0';
2961
2962                                 /* Replace current line with new line */
2963                                 string_free(lines_list[cy]);
2964                                 lines_list[cy] = string_make(buf);
2965
2966                                 /* Move to correct collumn */
2967                                 len = strlen(lines_list[cy]);
2968                                 if (len < cx) cx = len;
2969
2970                                 /* Now dirty */
2971                                 dirty_line = cy;
2972                         }
2973                 }
2974                 else
2975                 {
2976                         /* Exit on 'q' */
2977                         if (key == 'q' || key == '_') break;
2978
2979                         switch(key)
2980                         {
2981                         case 'a': case 'i':
2982                                 edit_mode = TRUE;
2983
2984                                 /* Mode line is now dirty */
2985                                 dirty_flags |= DIRTY_MODE;
2986                                 break;
2987                         case '~':
2988                                 if (!autopick_new_entry(entry, lines_list[cy]))
2989                                 {
2990                                         if (old_key != key) repeated_clearing = FALSE;
2991
2992                                         /* Next line */
2993                                         if (lines_list[cy + 1]) cy++;
2994                                         cx = 0;
2995                                         break;
2996                                 }
2997                                 string_free(lines_list[cy]);
2998
2999                                 if (old_key != key)
3000                                 {
3001                                         if (entry->action & DONT_AUTOPICK)
3002                                                 repeated_clearing = TRUE;
3003                                         else
3004                                                 repeated_clearing = FALSE;
3005                                 }
3006
3007                                 entry->action &= ~DO_AUTODESTROY;
3008                                 entry->action &= ~DO_QUERY_AUTOPICK;
3009                                 if (!repeated_clearing)
3010                                 {
3011                                         entry->action &= ~DO_AUTOPICK;
3012                                         entry->action |= DONT_AUTOPICK;
3013                                 }
3014                                 else 
3015                                 {
3016                                         entry->action &= ~DONT_AUTOPICK;
3017                                         entry->action |= DO_AUTOPICK;
3018                                 }
3019
3020                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3021
3022                                 /* Now dirty */
3023                                 dirty_line = cy;
3024
3025                                 /* Next line */
3026                                 if (lines_list[cy + 1]) cy++;
3027                                 cx = 0;
3028                                 break;
3029                         case '!':
3030                                 if (!autopick_new_entry(entry, lines_list[cy]))
3031                                 {
3032                                         if (old_key != key) repeated_clearing = FALSE;
3033
3034                                         /* Next line */
3035                                         if (lines_list[cy + 1]) cy++;
3036                                         cx = 0;
3037                                         break;
3038                                 }
3039                                 string_free(lines_list[cy]);
3040
3041                                 if (old_key != key)
3042                                 {
3043                                         if (entry->action & DO_AUTODESTROY)
3044                                                 repeated_clearing = TRUE;
3045                                         else
3046                                                 repeated_clearing = FALSE;
3047                                 }
3048
3049                                 entry->action &= ~DONT_AUTOPICK;
3050                                 entry->action &= ~DO_QUERY_AUTOPICK;
3051                                 if (!repeated_clearing)
3052                                 {
3053                                         entry->action &= ~DO_AUTOPICK;
3054                                         entry->action |= DO_AUTODESTROY;
3055                                 }
3056                                 else 
3057                                 {
3058                                         entry->action &= ~DO_AUTODESTROY;
3059                                         entry->action |= DO_AUTOPICK;
3060                                 }
3061
3062                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3063
3064                                 /* Now dirty */
3065                                 dirty_line = cy;
3066
3067                                 /* Next line */
3068                                 if (lines_list[cy + 1]) cy++;
3069                                 cx = 0;
3070                                 break;
3071                         case ';':
3072                                 if (!autopick_new_entry(entry, lines_list[cy]))
3073                                 {
3074                                         if (old_key != key) repeated_clearing = FALSE;
3075
3076                                         /* Next line */
3077                                         if (lines_list[cy + 1]) cy++;
3078                                         cx = 0;
3079                                         break;
3080                                 }
3081                                 string_free(lines_list[cy]);
3082
3083                                 if (old_key != key)
3084                                 {
3085                                         if (entry->action & DO_QUERY_AUTOPICK)
3086                                                 repeated_clearing = TRUE;
3087                                         else
3088                                                 repeated_clearing = FALSE;
3089                                 }
3090
3091                                 entry->action &= ~DO_AUTODESTROY;
3092                                 entry->action &= ~DONT_AUTOPICK;
3093                                 if (!repeated_clearing)
3094                                 {
3095                                         entry->action &= ~DO_AUTOPICK;
3096                                         entry->action |= DO_QUERY_AUTOPICK;
3097                                 }
3098                                 else 
3099                                 {
3100                                         entry->action &= ~DO_QUERY_AUTOPICK;
3101                                         entry->action |= DO_AUTOPICK;
3102                                 }
3103
3104                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3105
3106                                 /* Now dirty */
3107                                 dirty_line = cy;
3108
3109                                 /* Next line */
3110                                 if (lines_list[cy + 1]) cy++;
3111                                 cx = 0;
3112                                 break;
3113                         case '(':
3114                                 /* Toggle display on the 'M'ap */
3115                                 if (!autopick_new_entry(entry, lines_list[cy]))
3116                                 {
3117                                         if (old_key != key) repeated_clearing = FALSE;
3118
3119                                         /* Next line */
3120                                         if (lines_list[cy + 1]) cy++;
3121                                         cx = 0;
3122                                         break;
3123                                 }
3124                                 string_free(lines_list[cy]);
3125
3126                                 if (old_key != key)
3127                                 {
3128                                         if (entry->action & DO_DISPLAY)
3129                                                 repeated_clearing = TRUE;
3130                                         else
3131                                                 repeated_clearing = FALSE;
3132                                 }
3133
3134                                 if (!repeated_clearing)
3135                                         entry->action |= DO_DISPLAY;
3136                                 else
3137                                         entry->action &= ~DO_DISPLAY;
3138
3139                                 lines_list[cy] = autopick_line_from_entry_kill(entry);
3140
3141                                 /* Now dirty */
3142                                 dirty_line = cy;
3143
3144                                 /* Next line */
3145                                 if (lines_list[cy + 1]) cy++;
3146                                 cx = 0;
3147                                 break;
3148                         case '#':
3149                         case '{':
3150                                 key = KTRL('o');
3151                                 break;
3152                         case 'h': case '4':
3153                                 key = KTRL('b');
3154                                 break;
3155                         case 'l': case '6':
3156                                 key = KTRL('f');
3157                                 break;
3158                         case 'j': case '2':
3159                                 key = KTRL('n');
3160                                 break;
3161                         case 'k': case '8':
3162                                 key = KTRL('p');
3163                                 break;
3164                         case ' ':
3165                                 while (cy < upper + hgt-4 && lines_list[cy + 1])
3166                                         cy++;
3167                                 upper = cy;
3168                                 break;
3169                         case '-': case 'b':
3170                                 while (0 < cy && upper <= cy)
3171                                         cy--;
3172                                 while (0 < upper && cy + 1 < upper + hgt - 4)
3173                                         upper--;
3174                                 break;
3175
3176                         case 'g':
3177                                 cy = 0;
3178                                 break;
3179
3180                         case 'G':
3181                                 while (lines_list[cy + 1])
3182                                         cy++;
3183                                 break;
3184
3185                         case 'm':
3186                                 /* Erase line */
3187                                 Term_erase(0, cy - upper + 1, wid - WID_DESC);
3188
3189                                 /* Prompt */
3190 #ifdef JP
3191                                 Term_putstr(0, cy - upper + 1, wid - WID_DESC - 1, TERM_YELLOW, "P:<¥È¥ê¥¬¡¼¥­¡¼>: ");
3192 #else
3193                                 Term_putstr(0, cy - upper + 1, wid - WID_DESC - 1, TERM_YELLOW, "P:<Trigger key>: ");
3194 #endif
3195                                 if (insert_macro_line(lines_list, cy))
3196                                 {
3197                                         /* Prepare to input action */
3198                                         cx = 2;
3199                                         edit_mode = TRUE;
3200
3201                                         /* Now dirty */
3202                                         dirty_flags |= DIRTY_ALL;
3203                                         dirty_flags |= DIRTY_MODE;
3204                                 }
3205
3206                                 break;
3207
3208                         case 'c':
3209                                 /* Erase line */
3210                                 Term_erase(0, cy - upper + 1, wid - WID_DESC);
3211
3212                                 /* Prompt */
3213 #ifdef JP
3214                                 Term_putstr(0, cy - upper + 1, wid - WID_DESC - 1, TERM_YELLOW, format("C:%d:<¥³¥Þ¥ó¥É¥­¡¼>: ", (rogue_like_commands ? KEYMAP_MODE_ROGUE : KEYMAP_MODE_ORIG)));
3215 #else
3216                                 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)));
3217 #endif
3218
3219                                 if (insert_keymap_line(lines_list, cy))
3220                                 {
3221                                         /* Prepare to input action */
3222                                         cx = 2;
3223                                         edit_mode = TRUE;
3224
3225                                         /* Now dirty */
3226                                         dirty_flags |= DIRTY_ALL;
3227                                         dirty_flags |= DIRTY_MODE;
3228                                 }                               
3229                                 break;
3230                         case '/':
3231                                 /* Become dirty because of item/equip menu */
3232                                 dirty_flags |= DIRTY_SCREEN;
3233
3234                                 if (!get_string_for_search(&search_o_ptr, &search_str))
3235                                         break;
3236
3237                                 /* fall through */
3238                         case 'n':
3239                                 if (search_o_ptr)
3240                                 {
3241                                         if (!search_for_object(lines_list, search_o_ptr, &cx, &cy, TRUE)) dirty_flags |= DIRTY_NOT_FOUND;
3242                                 }
3243                                 else if (search_str)
3244                                 {
3245                                         if (!search_for_string(lines_list, search_str, &cx, &cy, TRUE)) dirty_flags |= DIRTY_NOT_FOUND;
3246                                 }
3247                                 else
3248                                 {
3249                                         dirty_flags |= DIRTY_NO_SEARCH;
3250                                 }
3251                                 break;
3252                         case 'N':
3253                                 if (search_o_ptr)
3254                                 {
3255                                         if (!search_for_object(lines_list, search_o_ptr, &cx, &cy, FALSE)) dirty_flags |= DIRTY_NOT_FOUND;
3256                                 }
3257                                 else if (search_str)
3258                                 {
3259                                         if (!search_for_string(lines_list, search_str, &cx, &cy, FALSE)) dirty_flags |= DIRTY_NOT_FOUND;
3260                                 }
3261                                 else
3262                                 {
3263                                         dirty_flags |= DIRTY_NO_SEARCH;
3264                                 }
3265                                 break;
3266                         }
3267                 }
3268
3269                 switch(key)
3270                 {
3271                 case KTRL('a'):
3272                         /* Beginning of line */
3273                         cx = 0;
3274                         break;
3275                 case KTRL('b'):
3276                         /* Back */
3277                         if (0 < cx)
3278                         {
3279                                 cx--;
3280                                 len = strlen(lines_list[cy]);
3281                                 if (len < cx) cx = len;
3282                         }
3283                         else if (cy > 0)
3284                         {
3285                                 cy--;
3286                                 cx = strlen(lines_list[cy]);
3287                         }
3288                         break;
3289                 case KTRL('c'):
3290                         /* Insert a conditinal expression line */
3291                         insert_return_code(lines_list, 0, cy);
3292                         string_free(lines_list[cy]);
3293                         lines_list[cy] = string_make(classrace);
3294                         cy++;
3295                         insert_return_code(lines_list, 0, cy);
3296                         string_free(lines_list[cy]);
3297                         lines_list[cy] = string_make("?:1");
3298                         cx = 0;
3299
3300                         /* Now dirty */
3301                         dirty_flags |= DIRTY_ALL;
3302                         break;
3303                 case KTRL('e'):
3304                         /* End of line */
3305                         cx = strlen(lines_list[cy]);
3306                         break;
3307                 case KTRL('f'):
3308                         /* Forward */
3309 #ifdef JP
3310                         if (iskanji(lines_list[cy][cx])) cx++;
3311 #endif
3312                         cx++;
3313                         len = strlen(lines_list[cy]);
3314                         if (len < cx)
3315                         {
3316                                 if (lines_list[cy + 1])
3317                                 {
3318                                         cy++;
3319                                         cx = 0;
3320                                 }
3321                                 else
3322                                         cx = len;
3323                         }
3324                         break;
3325                 case KTRL('g'):
3326                         /* Toggle display on the 'M'ap */
3327                         if (!autopick_new_entry(entry, lines_list[cy]))
3328                                 break;
3329                         string_free(lines_list[cy]);
3330
3331                         if (entry->action & DO_DISPLAY)
3332                         {
3333                                 entry->action &= ~DO_DISPLAY;
3334                                 cx++;
3335                         }
3336                         else
3337                         {
3338                                 entry->action |= DO_DISPLAY;
3339                                 if (cx > 0) cx--;
3340                         }
3341
3342                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3343
3344                         /* Now dirty */
3345                         dirty_line = cy;
3346                         break;
3347                 case KTRL('i'):
3348                         /* Insert choosen item name */
3349                         if (!entry_from_choosed_object(entry))
3350                         {
3351                                 /* Now dirty because of item/equip menu */
3352                                 dirty_flags |= DIRTY_SCREEN;
3353                                 break;
3354                         }
3355
3356                         insert_return_code(lines_list, 0, cy);
3357                         string_free(lines_list[cy]);
3358                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3359                         cx = 0;
3360
3361                         /* Now dirty because of item/equip menu */
3362                         dirty_flags |= DIRTY_SCREEN;
3363
3364                         break;
3365                 case KTRL('l'):
3366                         /* Insert a name of last destroyed item */
3367                         if (last_destroyed)
3368                         {
3369                                 insert_return_code(lines_list, 0, cy);
3370                                 string_free(lines_list[cy]);
3371                                 lines_list[cy] = string_make(last_destroyed);
3372                                 cx = 0;
3373
3374                                 /* Now dirty */
3375                                 dirty_flags |= DIRTY_ALL;
3376                         }
3377                         break;
3378                 case '\n': case '\r':
3379                         /* Split a line or insert end of line */
3380                         insert_return_code(lines_list, cx, cy);
3381                         cy++;
3382                         cx = 0;
3383
3384                         /* Now dirty */
3385                         dirty_flags |= DIRTY_ALL;
3386                         break;
3387                 case KTRL('n'):
3388                         /* Next line */
3389                         if (lines_list[cy + 1]) cy++;
3390                         break;
3391                 case KTRL('o'):
3392                         /* Prepare to write auto-inscription text */
3393                         if (!autopick_new_entry(entry, lines_list[cy]))
3394                                 break;
3395                         string_free(lines_list[cy]);
3396
3397                         if (!entry->insc) entry->insc = string_make("");
3398
3399                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3400
3401                         /* Move to collumn for auto inscription */
3402                         for (cx = 0; lines_list[cy][cx]; cx++)
3403                                 if (lines_list[cy][cx] == '#') break;
3404                         cx++;
3405                         edit_mode = TRUE;
3406
3407                         /* Now dirty */
3408                         dirty_line = cy;
3409                         dirty_flags |= DIRTY_MODE;
3410                         break;
3411                 case KTRL('p'):
3412                         /* Previous line */
3413                         if (cy > 0) cy--;
3414                         break;
3415                 case KTRL('q'):
3416                         /* Change mode */
3417                         edit_mode = !edit_mode;
3418                         
3419                         /* Mode line is now dirty */
3420                         dirty_flags |= DIRTY_MODE;
3421                         break;
3422                 case KTRL('r'):
3423                         /* Revert to original */
3424 #ifdef JP
3425                         if (!get_check("Á´¤Æ¤ÎÊѹ¹¤òÇË´þ¤·¤Æ¸µ¤Î¾õÂÖ¤ËÌᤷ¤Þ¤¹¡£¤è¤í¤·¤¤¤Ç¤¹¤«¡© "))
3426 #else
3427                         if (!get_check("Discard all change and revert to original file. Are you sure? "))
3428 #endif
3429                                 break;
3430
3431                         free_text_lines(lines_list);
3432                         lines_list = read_pickpref_text_lines(&filename_mode);
3433                         dirty_flags |= DIRTY_ALL | DIRTY_MODE;
3434                         cx = cy = 0;
3435                         edit_mode = FALSE;
3436                         break;
3437                 case KTRL('s'):
3438                         /* Rotate action; pickup/destroy/leave */
3439                         if (!autopick_new_entry(entry, lines_list[cy]))
3440                                 break;
3441                         string_free(lines_list[cy]);
3442
3443                         if (entry->action & DO_AUTOPICK)
3444                         {
3445                                 entry->action &= ~DO_AUTOPICK;
3446                                 entry->action |= DO_AUTODESTROY;
3447                         }
3448                         else if (entry->action & DO_AUTODESTROY)
3449                         {
3450                                 entry->action &= ~DO_AUTODESTROY;
3451                                 entry->action |= DONT_AUTOPICK;
3452                         }
3453                         else if (entry->action & DONT_AUTOPICK)
3454                         {
3455                                 entry->action &= ~DONT_AUTOPICK;
3456                                 entry->action |= DO_AUTOPICK;
3457                         }
3458
3459                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3460
3461                         /* Now dirty */
3462                         dirty_line = cy;
3463
3464                         break;
3465                 case KTRL('t'):
3466                         /* Nothing */
3467                         break;
3468                 case KTRL('u'):
3469                         /* Rotate identify-state; identified/unidentified/... */
3470                         if (!autopick_new_entry(entry, lines_list[cy]))
3471                                 break;
3472                         string_free(lines_list[cy]);
3473
3474                         if (IS_FLG(FLG_UNIDENTIFIED)) 
3475                         {
3476                                 REM_FLG(FLG_UNIDENTIFIED);
3477                                 ADD_FLG(FLG_UNAWARE);
3478                                 REM_FLG(FLG_IDENTIFIED);
3479                                 REM_FLG(FLG_STAR_IDENTIFIED);
3480                         }
3481                         else if (IS_FLG(FLG_UNAWARE)) 
3482                         {
3483                                 REM_FLG(FLG_UNIDENTIFIED);
3484                                 REM_FLG(FLG_UNAWARE);
3485                                 ADD_FLG(FLG_IDENTIFIED);
3486                                 REM_FLG(FLG_STAR_IDENTIFIED);
3487                         }
3488                         else if (IS_FLG(FLG_STAR_IDENTIFIED)) 
3489                         {
3490                                 REM_FLG(FLG_UNIDENTIFIED);
3491                                 REM_FLG(FLG_UNAWARE);
3492                                 REM_FLG(FLG_IDENTIFIED);
3493                                 REM_FLG(FLG_STAR_IDENTIFIED);
3494                         }
3495                         else if (IS_FLG(FLG_IDENTIFIED)) 
3496                         {
3497                                 REM_FLG(FLG_UNIDENTIFIED);
3498                                 REM_FLG(FLG_UNAWARE);
3499                                 REM_FLG(FLG_IDENTIFIED);
3500                                 ADD_FLG(FLG_STAR_IDENTIFIED);
3501                         }
3502                         else
3503                         {
3504                                 ADD_FLG(FLG_UNIDENTIFIED);
3505                                 REM_FLG(FLG_UNAWARE);
3506                                 REM_FLG(FLG_IDENTIFIED);
3507                                 REM_FLG(FLG_STAR_IDENTIFIED);
3508                         }
3509
3510                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3511
3512                         /* Now dirty */
3513                         dirty_line = cy;
3514                         break;
3515                 case KTRL('v'):
3516                         /* Scroll up */
3517                         while (cy < upper + hgt-4 && lines_list[cy + 1])
3518                                 cy++;
3519                         upper = cy;
3520                         break;
3521                 case KTRL('w'):
3522                         /* Toggle 'worthless' */
3523                         toggle_string(lines_list, FLG_WORTHLESS, cy);
3524                         /* Now dirty */
3525                         dirty_line = cy;
3526                         break;
3527                 case KTRL('x'):
3528                         /* Rotate within nameless, ego, artifact */
3529                         if (!autopick_new_entry(entry, lines_list[cy]))
3530                                 break;
3531                         string_free(lines_list[cy]);
3532
3533                         if (IS_FLG(FLG_NAMELESS)) 
3534                         {
3535                                 REM_FLG(FLG_NAMELESS);
3536                                 ADD_FLG(FLG_EGO);
3537                                 REM_FLG(FLG_ARTIFACT);
3538                         }
3539                         else if (IS_FLG(FLG_EGO)) 
3540                         {
3541                                 REM_FLG(FLG_NAMELESS);
3542                                 REM_FLG(FLG_EGO);
3543                                 ADD_FLG(FLG_ARTIFACT);
3544                         }
3545                         else if (IS_FLG(FLG_ARTIFACT)) 
3546                         {
3547                                 REM_FLG(FLG_NAMELESS);
3548                                 REM_FLG(FLG_EGO);
3549                                 REM_FLG(FLG_ARTIFACT);
3550                         }
3551                         else
3552                         {
3553                                 ADD_FLG(FLG_NAMELESS);
3554                                 REM_FLG(FLG_EGO);
3555                                 REM_FLG(FLG_ARTIFACT);
3556                         }
3557
3558                         lines_list[cy] = autopick_line_from_entry_kill(entry);
3559
3560                         /* Now dirty */
3561                         dirty_line = cy;
3562                         break;
3563
3564                 case KTRL('y'):
3565                         /* Paste killed text */
3566                         if (strlen(yank_buf))
3567                         {
3568                                 bool ret = FALSE;
3569
3570                                 for (j = 0; yank_buf[j]; j += strlen(yank_buf + j) + 1)
3571                                 {
3572                                         if (ret && '\n' == yank_buf[j])
3573                                         {
3574                                                 ret = FALSE;
3575                                                 continue;
3576                                         }
3577
3578                                         /* Split current line */
3579                                         insert_return_code(lines_list, cx, cy);
3580
3581                                         /* Save preceding string */
3582                                         for(i = 0; lines_list[cy][i]; i++)
3583                                                 buf[i] = lines_list[cy][i];
3584
3585                                         /* Paste yank buffer */
3586                                         if ('\n' != yank_buf[j])
3587                                         {
3588                                                 int k = j;
3589                                                 while (yank_buf[k] && i < MAX_LINELEN-1)
3590                                                         buf[i++] = yank_buf[k++];
3591                                                 ret = TRUE;
3592                                         }
3593
3594                                         buf[i] = '\0';
3595
3596                                         string_free(lines_list[cy]);
3597                                         lines_list[cy] = string_make(buf);
3598
3599                                         /* Move to the beggining of next line */
3600                                         cx = 0;
3601                                         cy++;
3602                                 }
3603
3604                                 /* Now dirty */
3605                                 dirty_flags |= DIRTY_ALL;
3606                         }
3607                         break;
3608                 case KTRL('z'):
3609                         /* Toggle 'collecting' */
3610                         toggle_string(lines_list, FLG_COLLECTING, cy);
3611                         /* Now dirty */
3612                         dirty_line = cy;
3613                         break;
3614
3615                 case KTRL('k'):
3616                         /* Kill rest of line */
3617                         if ((uint)cx > strlen(lines_list[cy]))
3618                                 cx = (int)strlen(lines_list[cy]);
3619
3620                         /* Save preceding string */
3621                         for (i = 0; lines_list[cy][i] && i < cx; i++)
3622                         {
3623 #ifdef JP
3624                                 if (iskanji(lines_list[cy][i]))
3625                                 {
3626                                         buf[i] = lines_list[cy][i];
3627                                         i++;
3628                                 }
3629 #endif
3630                                 buf[i] = lines_list[cy][i];
3631                         }
3632                         buf[i] = '\0';
3633
3634                         j = 0;
3635                         if (old_key == key)
3636                                 while (yank_buf[j])
3637                                         j += strlen(yank_buf + j) + 1;
3638
3639                         /* Copy following to yank buffer */
3640                         if (lines_list[cy][i])
3641                         {
3642                                 while (lines_list[cy][i] && j < MAX_YANK - 2)
3643                                         yank_buf[j++] = lines_list[cy][i++];
3644                                 i = TRUE;
3645                         }
3646                         else
3647                         {
3648                                 if (j < MAX_YANK - 2)
3649                                         yank_buf[j++] = '\n';
3650                                 i = FALSE;
3651                         }
3652                         yank_buf[j++] = '\0';
3653                         yank_buf[j] = '\0';
3654
3655                         /* Replace current line with 'preceding string' */
3656                         string_free(lines_list[cy]);
3657                         lines_list[cy] = string_make(buf);
3658
3659                         if (i)
3660                         {
3661                                 /* Now dirty */
3662                                 dirty_line = cy;
3663                                 break;
3664                         }
3665
3666                         /* fall through */
3667                 case KTRL('d'):
3668                 case 0x7F:
3669                         /* DELETE == go forward + BACK SPACE */
3670 #ifdef JP
3671                         if (iskanji(lines_list[cy][cx])) cx++;
3672 #endif
3673                         cx++;
3674
3675                         /* fall through */
3676
3677                 case '\010':
3678                         /* BACK SPACE */
3679                         len = strlen(lines_list[cy]);
3680                         if (len < cx)
3681                         {
3682                                 if (lines_list[cy + 1])
3683                                 {
3684                                         cy++;
3685                                         cx = 0;
3686                                 }
3687                                 else
3688                                 {
3689                                         cx = len;
3690                                         break;
3691                                 }
3692                         }
3693
3694                         if (cx == 0)
3695                         {
3696                                 /* delete a return code and union two lines */
3697                                 if (cy == 0) break;
3698                                 cx = strlen(lines_list[cy-1]);
3699                                 strcpy(buf, lines_list[cy-1]);
3700                                 strcat(buf, lines_list[cy]);
3701                                 string_free(lines_list[cy-1]);
3702                                 string_free(lines_list[cy]);
3703                                 lines_list[cy-1] = string_make(buf);
3704                                 for (i = cy; lines_list[i+1]; i++)
3705                                         lines_list[i] = lines_list[i+1];
3706                                 lines_list[i] = NULL;
3707                                 cy--;
3708
3709                                 /* Now dirty */
3710                                 dirty_flags |= DIRTY_ALL;
3711                                 break;
3712                         }
3713
3714                         for (i = j = k = 0; lines_list[cy][i] && i < cx; i++)
3715                         {
3716                                 k = j;
3717 #ifdef JP
3718                                 if (iskanji(lines_list[cy][i]))
3719                                         buf[j++] = lines_list[cy][i++];
3720 #endif
3721                                 buf[j++] = lines_list[cy][i];
3722                         }
3723                         while (j > k)
3724                         {
3725                                 cx--;
3726                                 j--;
3727                         }
3728                         for (; lines_list[cy][i]; i++)
3729                                 buf[j++] = lines_list[cy][i];
3730                         buf[j] = '\0';
3731                         string_free(lines_list[cy]);
3732                         lines_list[cy] = string_make(buf);
3733
3734                         /* Now dirty */
3735                         dirty_line = cy;
3736                         break;
3737                 }
3738
3739         } /* while (1) */
3740
3741         /* Restore the screen */
3742         screen_load();
3743
3744         switch (filename_mode)
3745         {
3746         case PT_DEFAULT:
3747 #ifdef JP
3748                 strcpy(buf, "picktype.prf");
3749 #else
3750                 strcpy(buf, "pickpref.prf");
3751 #endif
3752                 break;
3753
3754         case PT_WITH_PNAME:
3755 #ifdef JP
3756                 sprintf(buf, "picktype-%s.prf", player_name);
3757 #else
3758                 sprintf(buf, "pickpref-%s.prf", player_name);
3759 #endif
3760                 break;
3761         }
3762
3763         write_text_lines(buf, lines_list);
3764         free_text_lines(lines_list);
3765
3766         string_free(last_destroyed);
3767
3768         /* Reload autopick pref */
3769         process_pickpref_file(buf);
3770
3771         /* HACK -- reset start_time so that playtime is not increase while edit */
3772         start_time = time(NULL);
3773 }