File: mtools.sl

package info (click to toggle)
jed-extra 2.5.6-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 2,560 kB
  • ctags: 2,375
  • sloc: makefile: 75; ruby: 43; sed: 38; sh: 31
file content (131 lines) | stat: -rw-r--r-- 4,399 bytes parent folder | download | duplicates (9)
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
% Interface to mtools (http://mtools.linux.lu/) 
% for easy floppy read/write under UNIX
% 
% Copyright (c) 2006 Gnter Milde
% Released under the terms of the GNU General Public License (ver. 2 or later)
%
% Versions:
% 1.0 2003-xx-xx  first public version
% 1.1 2004-02-16  use a temp-file for mtools_find_file to preserve the CR-LFs
%                 (bugreport P. Boekholt)
%                 separate the directory listing to mtools_list_dir()
% 1.1.1 2004-06-03 bugfix in mtools_find_file (normalize pathname)
% 1.2   2006-05-29 renamed Jed_Temp_Dir to Jed_Tmp_Directory as this is what
%                  make_tmp_file() uses since 0.99.17-165
% 
% USAGE:
% 
% Put in your jed_library_path and add autoloads for the functions
% (or run make_ini from jedmodes.sf.net/mode/make_ini/).
% Optionally set the custom variable Mtools_Write_Args.
%   variable Mtools_Write_Args = "-D o";  % overwrite existing files
% 
% See extensive mtools documentation with info.
% 
% EXAMPLE: 
%   mtools_find_file("a:")     -> get a listing of the floppy dir (mdir)
%   mtools_find_file("a:test") -> load file "test" from floppy into buffer
%   mtools_write("a:test")     -> write buffer/region to floppy
%   
% RECOMMENDS:
% uri.sl (http://jedmodes.sf.net/mode/uri) to access the floppy using
% the normal find_file/write_buffer/write_region functions (via hooks)

% _debug_info = 1;

% requirements
% autoload("filelist_mode", "filelist");

% --- Variables

% mtools_write doesnot work interactive, so nameclashes result in an error, 
% if no solution scheme is set: 
% custom_variable("Mtools_Write_Args", "-D o");  % overwrite existing file
custom_variable("Mtools_Write_Args", "-D a");  % autorename the new file

% Directory for temporary files
custom_variable("Jed_Tmp_Directory", getenv("TEMP"));
if (Jed_Tmp_Directory == NULL)
  Jed_Tmp_Directory = getenv("TMP");
if (Jed_Tmp_Directory == NULL)
  Jed_Tmp_Directory = "/tmp";

% call mdir for path
define mtools_list_dir(path)
{
   variable status;
   
   sw2buf(path);
   set_readonly(0);
   erase_buffer();
   status = run_shell_cmd("mdir " + path);  % directory listing
   set_buffer_modified_flag(0);
   % error handling
   if (status)
     {
	delbuf(whatbuf);
	verror("mdir returned %d, %s", status, errno_string(status));
     }
   setbuf_info("", path, path, 8);
   define_blocal_var("filename_position", 5);
   % filelist_mode();
   return(not(status)); % success
}

% Find a file using (usually on a floppy) using mtools
% If the filename has no basepart or is just "a:", do a directory listing
% Return success
define mtools_find_file(path)
{
   variable status, flags,
     tmp_file = make_tmp_file(path_concat(Jed_Tmp_Directory, path))
                + path_extname(path); % (to get the mode right)
   
   % standardize the name of the root dir (so path_basename() works)
   !if (is_substr(path, "a:/"))
     (path, ) = strreplace (path, "a:", "a:/", 1);

   % show(_function_name, path, path_basename(path));
   % return a listing if the path is a directory
   if (path_basename(path) == "")
     return mtools_list_dir(path);
     
   % copy file to tmp_file
   status = system(strjoin(["mcopy", path, tmp_file], " "));
   if (status)
     verror("mtools returned %d, %s", status, errno_string(status));

   % open the temp file, delete it and correct buffer settings
   () = find_file(tmp_file);
   delete_file(tmp_file);
   (, , , flags) = getbuf_info();
   % the dirname is always expanded, so I cannot set it to a:<something> :-(
   % setbuf_info(path_basename(path), path_dirname(path), tmp_file, flags);
   % set to empty file and dir names, so save_buffer asks where to save
   % TODO: do not reopen if buffer exists.
   setbuf_info("", "", tmp_file, flags);
   rename_buffer(path); % be sure not to have 2 buffers 
                                       % with the same name
   
   return(not(status)); % success
}

% Write buffer (or if defined region) to a file (usually on a floppy) 
% using mtools
% Return success
define mtools_write(path)
{
   % show("mtools-write", path);
   variable status;
   push_spot();
   !if (is_visible_mark())
     mark_buffer();
   flush ("Saving to " + path );
   status = pipe_region(sprintf("mcopy %s - %s", Mtools_Write_Args, path));
   
   !if (status)
	set_buffer_modified_flag(0);
   else
	verror("mtools returned %d, %s", status, errno_string(status));
   return(not(status)); % success
}