PageRenderTime 58ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/lisp/FileIO.cs

http://github.com/toshok/shelisp
C# | 206 lines | 174 code | 25 blank | 7 comment | 22 complexity | dd0a03df4c01fa8469509c45d53c9e23 MD5 | raw file
Possible License(s): GPL-3.0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.IO;
  5. namespace Shelisp {
  6. public class FileIO {
  7. [LispBuiltin]
  8. public static Shelisp.Object Fexpand_file_name(L l, Shelisp.Object filename, [LispOptional] Shelisp.Object directory)
  9. {
  10. // XXX fix fallback if directory is L.Qnil (it uses the buffer's default-directory)
  11. if (directory.LispEq (L.Qnil))
  12. directory = new Shelisp.String(Environment.CurrentDirectory);
  13. return new Shelisp.String (Path.Combine ((string)(Shelisp.String)directory, (string)(Shelisp.String)filename));
  14. }
  15. [LispBuiltin]
  16. public static Shelisp.Object Fload(L l, Shelisp.Object filename, [LispOptional] Shelisp.Object missing_ok, Shelisp.Object nomessage, Shelisp.Object nosuffix, Shelisp.Object must_suffix)
  17. {
  18. //Console.WriteLine ("load {0} {1} {2} {3} {4}", filename, missing_ok, nomessage, nosuffix, must_suffix);
  19. if (!((string)(String)filename).EndsWith (".el"))
  20. filename = new Shelisp.String (((string)(Shelisp.String)filename) + ".el");
  21. return Fload_file (l, filename);
  22. }
  23. [LispBuiltin]
  24. public static Shelisp.Object Fload_file(L l, Shelisp.Object filename)
  25. {
  26. bool failed = false;
  27. foreach (var o in (List)l.Vload_path) {
  28. string full_path = Path.Combine ((string)(Shelisp.String)o, (Shelisp.String)filename);
  29. if (Path.GetExtension (full_path) != ".el")
  30. full_path = full_path + ".el";
  31. if (File.Exists (full_path)) {
  32. StreamReader reader = File.OpenText (full_path);
  33. while (!reader.EndOfStream) {
  34. var form = Reader.Read (reader);
  35. if (form == null)
  36. break;
  37. try {
  38. form.Eval (l);
  39. }
  40. catch {
  41. Console.WriteLine ("problematic form was {0}", form);
  42. throw;
  43. }
  44. }
  45. return L.Qt;
  46. }
  47. }
  48. throw new LispFileErrorException ("Cannot open load file", (string)(Shelisp.String)filename);
  49. #if old_impl
  50. try {
  51. StreamReader reader = File.OpenText ((Shelisp.String)filename);
  52. while (!reader.EndOfStream) {
  53. var form = Reader.Read (reader);
  54. if (form == null)
  55. break;
  56. try {
  57. form.Eval (l);
  58. }
  59. catch {
  60. Console.WriteLine ("problematic form was {0}", form);
  61. throw;
  62. }
  63. }
  64. return L.Qt;
  65. }
  66. catch (FileNotFoundException) {
  67. failed = true;
  68. throw new LispFileErrorException ("Cannot open load file", (string)(Shelisp.String)filename);
  69. }
  70. catch (Exception) {
  71. failed = true;
  72. throw;
  73. }
  74. finally {
  75. if (failed)
  76. Console.WriteLine ("failed to load {0}", filename);
  77. else
  78. Console.WriteLine ("done with file {0}", filename);
  79. }
  80. #endif
  81. }
  82. public static Shelisp.Object DoAutoload (L l, Shelisp.Object fun, Shelisp.Object original_fun)
  83. {
  84. string filename = (string)(Shelisp.String)L.CAR (L.CDR (fun));
  85. if (L.NILP (l.Vload_path)) {
  86. Console.WriteLine ("load path = NIL!");
  87. return Fload_file (l, (Shelisp.String)(filename + ".el"));
  88. }
  89. else {
  90. foreach (var o in (List)l.Vload_path) {
  91. string full_path = Path.Combine ((string)(Shelisp.String)o, filename);
  92. if (Path.GetExtension (full_path) != ".el")
  93. full_path = full_path + ".el";
  94. if (File.Exists (full_path)) {
  95. Console.WriteLine ("found {0}", full_path);
  96. return Fload_file (l, (Shelisp.String)full_path);
  97. }
  98. Console.WriteLine ("{0} not found", full_path);
  99. }
  100. }
  101. throw new Exception ("file not found");
  102. }
  103. [LispBuiltin]
  104. public static Shelisp.Object Fautoload(L l, Shelisp.Object function, [LispOptional] Shelisp.Object filename, Shelisp.Object docstring, Shelisp.Object interactive, Shelisp.Object type)
  105. {
  106. if (L.Qt.LispEq (Symbol.Ffboundp (l, function)))
  107. return L.Qnil;
  108. var autoload_function = new List (new Shelisp.Object[] { L.Qautoload, filename, docstring == null ? (Shelisp.Object)(Shelisp.String)"" : docstring, interactive == null ? L.Qnil : interactive, type == null ? L.Qnil : interactive });
  109. Symbol.Ffset (l, function, autoload_function);
  110. //l.Environment = new List (new List(function, function), l.Environment);
  111. return autoload_function;
  112. }
  113. // Features
  114. [LispBuiltin]
  115. public static Shelisp.Object Fprovide(L l, Shelisp.Object feature, params Shelisp.Object[] subfeatures)
  116. {
  117. l.AddFeature ((Symbol)feature);
  118. return L.Qnil;
  119. }
  120. [LispBuiltin]
  121. public static Shelisp.Object Frequire(L l, Shelisp.Object feature, [LispOptional] Shelisp.Object filename, Shelisp.Object noerror)
  122. {
  123. if (!l.IsFeatureLoaded ((Symbol)feature)) {
  124. var filename_s = ((Symbol)feature).name + ".el";
  125. Fload (l, (Shelisp.String)filename_s, L.Qnil, L.Qnil, L.Qnil, L.Qnil);
  126. }
  127. return feature;
  128. }
  129. [LispBuiltin]
  130. public static Shelisp.Object Ffeaturep(L l, Shelisp.Object feature, [LispOptional] Shelisp.Object subfeature)
  131. {
  132. return l.IsFeatureLoaded ((Symbol)feature) ? L.Qt : L.Qnil;
  133. }
  134. [LispBuiltin]
  135. public static Shelisp.Object Funload_feature(L l, Shelisp.Object feature, [LispOptional] Shelisp.Object subfeature)
  136. {
  137. throw new NotImplementedException ();
  138. }
  139. // variable: unload-feature-special-hooks
  140. // Load History
  141. [LispBuiltin]
  142. public static Shelisp.Object Fsymbol_file(L l, Shelisp.Object sym, Shelisp.Object type)
  143. {
  144. throw new NotImplementedException ();
  145. }
  146. // variable: load-history
  147. [LispBuiltin (DocString = @"Alist of elements (REGEXP . HANDLER) for file names handled specially.
  148. If a file name matches REGEXP, all I/O on that file is done by calling
  149. HANDLER. If a file name matches more than one handler, the handler
  150. whose match starts last in the file name gets precedence. The
  151. function `find-file-name-handler' checks this list for a handler for
  152. its argument.
  153. HANDLER should be a function. The first argument given to it is the
  154. name of the I/O primitive to be handled; the remaining arguments are
  155. the arguments that were passed to that primitive. For example, if you
  156. do (file-exists-p FILENAME) and FILENAME is handled by HANDLER, then
  157. HANDLER is called like this:
  158. (funcall HANDLER 'file-exists-p FILENAME)
  159. Note that HANDLER must be able to handle all I/O primitives; if it has
  160. nothing special to do for a primitive, it should reinvoke the
  161. primitive to handle the operation ""the usual way"".
  162. See Info node `(elisp)Magic File Names' for more details.")]
  163. public Shelisp.Object Vfile_name_handler_alist = L.Qnil;
  164. [LispBuiltin (DocString = "Full name of file being loaded by `load'.")]
  165. public Shelisp.Object Vload_file_name = L.Qnil;
  166. [LispBuiltin (DocString = @"Specifies whether to use the system's trash can.
  167. When non-nil, certain file deletion commands use the function
  168. `move-file-to-trash' instead of deleting files outright.
  169. This includes interactive calls to `delete-file' and
  170. `delete-directory' and the Dired deletion commands.")]
  171. public static bool delete_by_moving_to_trash = false;
  172. [LispBuiltin (DocString = @"Non-nil says auto-save a buffer in the file it is visiting, when practical.
  173. Normally auto-save files are written under other names.")]
  174. public static bool auto_save_visited_file_name = false;
  175. [LispBuiltin (DocString = @"The directory for writing temporary files.")]
  176. public static Shelisp.Object temporary_file_directory = L.Qnil;
  177. }
  178. }