aboutsummaryrefslogtreecommitdiffstats
path: root/CWRU/mh-folder-comp
blob: 905000c7a34cb5fe343f09a73c5b3d99a5be55ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
From jwe@che.utexas.edu Wed Sep 21 17:23:40 1994
Flags: 10
Return-Path: jwe@che.utexas.edu
Received: from po.CWRU.Edu (root@po.CWRU.Edu [129.22.4.2]) by odin.INS.CWRU.Edu with ESMTP (8.6.8.1+cwru/CWRU-2.1-ins)
	id RAA04010; Wed, 21 Sep 1994 17:23:39 -0400 (from jwe@che.utexas.edu for <chet@odin.INS.CWRU.Edu>)
Received: from life.ai.mit.edu (life.ai.mit.edu [128.52.32.80]) by po.CWRU.Edu with SMTP (8.6.8.1+cwru/CWRU-2.2)
	id RAA02121; Wed, 21 Sep 1994 17:23:28 -0400 (from jwe@che.utexas.edu for <chet@po.cwru.edu>)
Received: from schoch.che.utexas.edu by life.ai.mit.edu (4.1/AI-4.10) for chet@po.cwru.edu id AA09989; Wed, 21 Sep 94 17:23:17 EDT
Received: from localhost (jwe@localhost) by schoch.che.utexas.edu (8.6.8.1/8.6) with SMTP id QAA05737; Wed, 21 Sep 1994 16:22:01 -0500
Message-Id: <199409212122.QAA05737@schoch.che.utexas.edu>
To: march@tudor.com
Cc: bug-bash@prep.ai.mit.edu
Subject: Re: Completion feature possible?
In-Reply-To: Your message of 21 Sep 94 13:30:22 EDT
Date: Wed, 21 Sep 94 16:22:00 EDT
From: John Eaton <jwe@che.utexas.edu>

Gregory F. March <march@tudor.com> wrote:

: I was having a discussion about MH with one of my friends the other
: day and I got to thinking that the +folder/subfolder scheme for naming
: mail folders is a real pain because completion doesn't work on
: them. Someone then mentioned that zsh (I think) has the ability to
: specify how to complete (I guess where to look for the files) for
: different prefixes. Bash right now knows about '@', '~', and '$' (any
: others?). It would be really helpful if one could define something
: like:
: 
: 	completion '+' "$HOME/Mail"
: 
: in a config file someplace. Would this be easy? Is there a list of
: TODO item that someone might want to add this to?

It would be nice to have a general completion feature like this.

Until that happens, maybe you will find the following patch useful.
It makes MH folder name completion work with bash.  The diffs are
relative to version 1.14.2.

I realize that changes to readline.c and and complete.c are not good
since they add some MH-specific stuff to the readline code and not to
bash, but when I first wrote this, I had no idea what else to do.

Chet, would you consider adding this if it were cleaned up a bit?
Made optional with cpp conditionals?

This feature has been very useful to me for the last several years
(since about 1.05 or 1.06, I think).

Thanks,

--
John W. Eaton      | 4.3BSD is not perfect.  -- Leffler, et al. (1989).
jwe@che.utexas.edu |


-------------------------------cut here-------------------------------
diff -rc bash-1.14.2/bashline.c bash-1.14.2.local/bashline.c
*** bash-1.14.2/bashline.c	Wed Aug  3 09:32:45 1994
--- bash-1.14.2.local/bashline.c	Wed Sep 21 15:39:04 1994
***************
*** 58,63 ****
--- 58,64 ----
  static char *hostname_completion_function ();
  static char *command_word_completion_function ();
  static char *command_subst_completion_function ();
+ static char *mh_folder_completion_function ();
  
  static void snarf_hosts_from_file (), add_host_name ();
  static void sort_hostname_list ();
***************
*** 90,95 ****
--- 91,98 ----
    bash_complete_username_internal (),
    bash_complete_hostname (), bash_possible_hostname_completions (),
    bash_complete_hostname_internal (),
+   bash_complete_mh_folder (), bash_possible_mh_folder_completions (),
+   bash_complete_mh_folder_internal (),
    bash_complete_variable (), bash_possible_variable_completions (),
    bash_complete_variable_internal (),
    bash_complete_command (), bash_possible_command_completions (),
***************
*** 134,140 ****
    rl_terminal_name = get_string_value ("TERM");
    rl_instream = stdin;
    rl_outstream = stderr;
!   rl_special_prefixes = "$@";
  
    /* Allow conditional parsing of the ~/.inputrc file. */
    rl_readline_name = "Bash";
--- 137,143 ----
    rl_terminal_name = get_string_value ("TERM");
    rl_instream = stdin;
    rl_outstream = stderr;
!   rl_special_prefixes = "$@+";
  
    /* Allow conditional parsing of the ~/.inputrc file. */
    rl_readline_name = "Bash";
***************
*** 193,198 ****
--- 196,207 ----
    rl_bind_key_in_map ('@', bash_possible_hostname_completions,
  		      emacs_ctlx_keymap);
  
+   rl_add_defun ("complete-mh-folder", bash_complete_mh_folder, META('+'));
+   rl_add_defun ("possible-mh-folder-completions",
+ 		bash_possible_mh_folder_completions, -1);
+   rl_bind_key_in_map ('+', bash_possible_mh_folder_completions,
+ 		      emacs_ctlx_keymap);
+ 
    rl_add_defun ("complete-variable", bash_complete_variable, -1);
    rl_bind_key_in_map ('$', bash_complete_variable, emacs_meta_keymap);
    rl_add_defun ("possible-variable-completions",
***************
*** 656,661 ****
--- 665,677 ----
    if (!matches && *text == '@')
      matches = completion_matches (text, hostname_completion_function);
  
+   /* Another one.  Why not?  If the word starts in '+', then look for
+      matching mh folders for completion first. */
+   if (!matches && *text == '+')
+     {
+       matches = completion_matches (text, mh_folder_completion_function);
+     }
+ 
    /* And last, (but not least) if this word is in a command position, then
       complete over possible command names, including aliases, functions,
       and command names. */
***************
*** 1077,1082 ****
--- 1093,1185 ----
      return ((char *)NULL);
  }
  
+ /* How about a completion function for mh folders? */
+ static char *
+ mh_folder_completion_function (text, state)
+      int state;
+      char *text;
+ {
+   extern int rl_filename_completion_desired;
+ 
+   extern char *get_mh_path ();
+ 
+   static char *mh_path = (char *)NULL;
+   static int len;
+   static int istate;
+   static char *val;
+   char *hint;
+ 
+   static char *mh_folder_hint = (char *)NULL;
+ 
+   /* If we don't have any state, make some. */
+   if (!state)
+     {
+       val = (char *)NULL;
+ 
+       if (mh_path)
+ 	free (mh_path);
+ 
+       mh_path = get_mh_path ();
+       if (!mh_path && !(hint[1] == '/' || hint[1] == '.'))
+ 	return ((char *)NULL);
+ 
+       len = strlen (mh_path);
+     }
+ 
+   if (mh_folder_hint)
+     free (mh_folder_hint);
+ 
+   hint = text;
+   if (*hint == '+')
+     hint++;
+ 
+   mh_folder_hint = (char *)xmalloc (2 + len + strlen (hint));
+   if (*hint == '/' || *hint == '.') {
+     len = -1;
+     sprintf (mh_folder_hint, "%s", hint);
+   } else
+     sprintf (mh_folder_hint, "%s/%s", mh_path, hint);
+ 
+   istate = (val != (char *)NULL);
+ 
+  again:
+   val = filename_completion_function (mh_folder_hint, istate);
+   istate = 1;
+ 
+   if (!val)
+     {
+       return ((char *)NULL);
+     }
+   else
+     {
+       char *ptr = val + len + 1, *temp;
+       struct stat sb;
+       int status = stat (val, &sb);
+ 
+       if (status != 0)
+ 	return ((char *)NULL);
+ 
+       if ((sb.st_mode & S_IFDIR) == S_IFDIR)
+ 	{
+ 	  temp = (char *)xmalloc (2 + strlen (ptr));
+ 	  *temp = '+';
+ 	  strcpy (temp + 1, ptr);
+ 
+ 	  free (val);
+ 	  val = "";
+ 
+ 	  rl_filename_completion_desired = 1;
+ 
+ 	  return (temp);
+ 	}
+       else
+ 	{
+ 	  free (val);
+ 	}
+       goto again;
+     }
+ }
+ 
  /* History and alias expand the line. */
  static char *
  history_expand_line_internal (line)
***************
*** 1628,1633 ****
--- 1731,1773 ----
  {
    bash_specific_completion
      (what_to_do, (Function *)username_completion_function);
+ }
+ 
+ static void
+ bash_complete_mh_folder (ignore, ignore2)
+      int ignore, ignore2;
+ {
+   bash_complete_mh_folder_internal (TAB);
+ }
+ 
+ static void
+ bash_possible_mh_folder_completions (ignore, ignore2)
+      int ignore, ignore2;
+ {
+   bash_complete_mh_folder_internal ('?');
+ }
+ 
+ static void
+ bash_complete_mh_folder_internal (what_to_do)
+      int what_to_do;
+ {
+   Function  *orig_func;
+   CPPFunction *orig_attempt_func;
+   char *orig_rl_completer_word_break_characters;
+   extern char *rl_completer_word_break_characters;
+ 
+   orig_func = rl_completion_entry_function;
+   orig_attempt_func = rl_attempted_completion_function;
+   orig_rl_completer_word_break_characters = rl_completer_word_break_characters;
+   rl_completion_entry_function = (Function *)mh_folder_completion_function;
+   rl_attempted_completion_function = (CPPFunction *)NULL;
+   rl_completer_word_break_characters = " \t\n\"\'";
+ 
+   rl_complete_internal (what_to_do);
+ 
+   rl_completion_entry_function = orig_func;
+   rl_attempted_completion_function = orig_attempt_func;
+   rl_completer_word_break_characters = orig_rl_completer_word_break_characters;
  }
  
  static void
Only in bash-1.14.2.local: bashline.c.orig
diff -rc bash-1.14.2/lib/readline/complete.c bash-1.14.2.local/lib/readline/complete.c
*** bash-1.14.2/lib/readline/complete.c	Tue Jul 26 12:59:57 1994
--- bash-1.14.2.local/lib/readline/complete.c	Wed Sep 21 15:41:19 1994
***************
*** 733,751 ****
  	      if (rl_filename_completion_desired)
  		{
  		  struct stat finfo;
! 		  char *filename = tilde_expand (matches[0]);
  
! 		  if ((stat (filename, &finfo) == 0) && S_ISDIR (finfo.st_mode))
  		    {
! 		      if (rl_line_buffer[rl_point] != '/')
! 			rl_insert_text ("/");
  		    }
! 		  else
  		    {
! 		      if (rl_point == rl_end)
! 			rl_insert_text (temp_string);
  		    }
- 		  free (filename);
  		}
  	      else
  		{
--- 733,768 ----
  	      if (rl_filename_completion_desired)
  		{
  		  struct stat finfo;
! 		  char *tilde_expand ();
! 		  char *plus_expand ();
! 		  char *filename = (char *) NULL;
  
! 		  switch (*matches[0])
  		    {
! 		    case '+':
! 		      filename = plus_expand (matches[0]);
! 		      break;
! 		    case '~':
! 		    default:
! 		      filename = tilde_expand (matches[0]);
! 		      break;
  		    }
! 
! 		  if (filename)
  		    {
! 		      if ((stat (filename, &finfo) == 0)
! 			  && S_ISDIR (finfo.st_mode))
! 			{
! 			  if (rl_line_buffer[rl_point] != '/')
! 			    rl_insert_text ("/");
! 			}
! 		      else
! 			{
! 			  if (rl_point == rl_end)
! 			    rl_insert_text (temp_string);
! 			}
! 		      free (filename);
  		    }
  		}
  	      else
  		{
Only in bash-1.14.2.local/lib/readline: diffs
diff -rc bash-1.14.2/lib/readline/readline.c bash-1.14.2.local/lib/readline/readline.c
*** bash-1.14.2/lib/readline/readline.c	Fri Aug 12 12:47:46 1994
--- bash-1.14.2.local/lib/readline/readline.c	Wed Sep 21 15:36:07 1994
***************
*** 23,28 ****
--- 23,29 ----
  #define READLINE_LIBRARY
  
  #include <stdio.h>
+ #include <string.h>
  #include <sys/types.h>
  #include <fcntl.h>
  #if !defined (NO_SYS_FILE)
***************
*** 3518,3523 ****
--- 3519,3616 ----
  }
  
  #endif /* TEST */
+ 
+ #define cr_whitespace(c) ((c) == '\r' || (c) == '\n' || whitespace(c))
+ 
+ char *
+ get_mh_path ()
+ {
+   static FILE *fp = (FILE *)NULL;
+   char buf[512];      /* XXX */
+   char profile[512];  /* XXX */
+   char *bp;
+   char *temp_home;
+   char *temp_path;
+ 
+   temp_home = (char *)getenv ("HOME");
+   if (!temp_home)
+     return ((char *)NULL);
+ 
+   strcpy (profile, temp_home);
+   strcat (profile, "/.mh_profile");
+ 
+   if (fp)
+     fclose (fp);
+ 
+   fp = fopen (profile, "r");
+   if (fp == (FILE *)NULL)
+     return ((char *)NULL);
+ 
+   while (fgets (buf, 512, fp) != (char *)NULL)  /* XXX */
+     {
+       if ((bp = strstr (buf, "Path:")) != (char *)NULL)
+ 	{
+ 	  bp += 5;
+ 	  while (whitespace (*bp))
+ 	    bp++;
+ 
+ 	  if (*bp == '\0')
+ 	    return ((char *)NULL);
+ 
+ 	  temp_path = (char *)xmalloc (3 + strlen (bp) + strlen (temp_home));
+ 
+ 	  strcpy (temp_path, temp_home);
+ 	  strcat (temp_path, "/");
+ 	  strcat (temp_path, bp);
+ 
+ 	  bp = temp_path;
+ 
+ 	  while (!(cr_whitespace (*bp)))
+ 	    bp++;
+ 
+ 	  *bp = '\0';
+ 
+ 	  return temp_path;
+ 	}
+     }
+ 
+   return ((char *)NULL);
+ }
+ 
+ /* Expand FILENAME if it begins with a plus.  This always returns
+    a new string. */
+ char *
+ plus_expand (filename)
+      char *filename;
+ {
+   static char *dirname = (char *)NULL;
+ 
+   if (filename && *filename == '+')
+     {
+       char *mh_path = get_mh_path ();
+ 
+       if (filename[1] == '/' || filename[1] == '.')
+ 	{
+ 	  dirname = (char *)xmalloc (1 + strlen (filename));
+ 
+ 	  strcpy(dirname, filename+1);
+ 
+ 	  return dirname;
+ 	}
+ 
+       if (mh_path)
+ 	{
+ 	  dirname = (char *)xmalloc (1 + strlen (filename) + strlen (mh_path));
+ 
+ 	  strcpy (dirname, mh_path);
+ 	  strcat (dirname, "/");
+ 	  strcat (dirname, filename+1);
+ 
+ 	  return dirname;
+ 	}
+     }
+   return (char *)NULL;
+ }
  
  
  /*