File: compress.sl

package info (click to toggle)
jed 0.99.16-5
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 4,312 kB
  • ctags: 4,736
  • sloc: ansic: 36,879; sh: 8,660; makefile: 379
file content (140 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download
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
static variable Compressed_File_Exts
  = [".gz", ".Z", ".bz2"];

static variable Compress_File_Pgms
  = ["gzip",
     "uncompress",
     "bzip2"];

static variable Uncompress_File_Pgms
  = ["gzip -dc %s",
     "uncompress -c %s",
     "bzip2 -dc %s"
     ];

static variable Auto_Compression_Mode = 0;

static define check_is_compressed (file)
{
   variable ext = path_extname (file);
   
   variable i = where (ext == Compressed_File_Exts);

   if (length (i))
     return i[0];
   return -1;
}

static define _write_compressed_region (file, append)
{
   !if (blocal_var_exists ("Auto_Compression_Mode")
	or Auto_Compression_Mode)
     return 0;

   variable i = check_is_compressed (file);
     
   if (i == -1) return 0;

   variable cmd = sprintf ("%s > %s", Compress_File_Pgms[i], file);
   if (append)
     cmd = sprintf ("%s >> %s", Compress_File_Pgms[i], file);
   
   variable status = pipe_region (cmd);

   if (status != 0)
     verror ("%s returned %d", cmd, status);

   return 1;
}

static define write_compressed_region (file)
{
   return _write_compressed_region (file, 0);
}

static define append_compressed_region (file)
{
   return _write_compressed_region (file, 1);
}

static define insert_compressed_file (file)
{
   !if (Auto_Compression_Mode)
     return 0;

   variable i = check_is_compressed (file);
   if (i == -1)
     return 0;

   if (1 != file_status (file))
     return 0;

   variable cmd = sprintf (Uncompress_File_Pgms[i], file);

   run_shell_cmd (cmd);

   return 1;
}
   
static define read_compressed_file (file)
{
   if (insert_compressed_file (file))
     {
	create_blocal_var ("Auto_Compression_Mode");
	return 1;
     }
   return 0;
}


add_to_hook ("_jed_insert_file_hooks", &insert_compressed_file);
add_to_hook ("_jed_read_file_hooks", &read_compressed_file);
append_to_hook ("_jed_write_region_hooks", &write_compressed_region);
append_to_hook ("_jed_append_region_hooks", &append_compressed_region);

static define compressed_set_mode_hook (ext)
{
   variable i, file;

   !if (Auto_Compression_Mode)
     return 0;

   (file,,,) = getbuf_info ();
   i = check_is_compressed (file);
   if (i != -1)
     {
	file = file[[0:strlen(file)-strlen(ext)-2]];
	mode_hook (file_type (file));
	return 1;
     }
   return 0;
}
add_to_hook ("_jed_set_mode_hooks", &compressed_set_mode_hook);

%!%+
%\function{auto_compression_mode}
%\synopsis{Toggle auto-compression-mode}
%\usage{auto_compression_mode ([Int_Type state])}
%\description
% The \var{auto_compression_mode} function toggles the auto-compression-mode
% on or off. When on, files whose names end with \exmp{.gz}, \exmp{.Z}, or
% \exmp{.bz2} will automatically uncompressed when read in, and compressed 
% when written out.
%!%-
   
public define auto_compression_mode ()
{
   if (_NARGS)
     {
	Auto_Compression_Mode = ();
	return;
     }

   variable state = "OFF";

   Auto_Compression_Mode = not Auto_Compression_Mode;
   if (Auto_Compression_Mode)
     state = "ON";

   vmessage ("Auto Compression Mode: %s", state);
}