# HG changeset patch
# User Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
# Date 1400687782 -7200
#      Wed May 21 17:56:22 2014 +0200
# Node ID 54b2161da9858177f122bc1880bb4dbbe0e9cad8
# Parent  9b6e194dd393d586c8edbdd1936d8890bda98bb2
Make use of is_valid_file_id for fid checks

diff -r 9b6e194dd393 -r 54b2161da985 inst/ufl.m
--- a/inst/ufl.m	Mon May 19 12:14:44 2014 +0200
+++ b/inst/ufl.m	Wed May 21 17:56:22 2014 +0200
@@ -31,13 +31,13 @@
   filename = "default.ufl";
 
   if (numel (varargin) < 1)
-    if (fid < 0)
+    if (! is_valid_file_id (fid))
       print_usage ();
     endif
   elseif (! all (cellfun ("ischar", varargin)))
     error ("ufl: all arguments should be strings");
   elseif (strcmpi (varargin{1}, "start"))
-    if (fid >= 0)
+    if (is_valid_file_id (fid))
       error ("ufl: a file is already open");
     else
       if (numel (varargin) > 1)
@@ -48,21 +48,25 @@
       endif
       fid = fopen (filename, "w");
     endif
-    if (fid < 0)
+    if (! is_valid_file_id (fid))
       error (["ufl: could not open file ", filename]);
     endif
   elseif (strcmpi (varargin{1}, "end"))
-    if (fid < 0)
+    if (! is_valid_file_id (fid))
       error ("ufl: no open file");
     else
       fclose (fid);
       fid = -1;
     endif
-  elseif (fid < 0)
+  elseif (! is_valid_file_id (fid))
     error ("ufl: no open file");
   else
-    fprintf (fid, "%s ", varargin{:});
-    fprintf (fid, "\n");
+    if (is_valid_file_id (fid))
+      fprintf (fid, "%s ", varargin{:});
+      fprintf (fid, "\n");
+    else
+      error ("ufl: no open file");
+    endif
   endif
 
 endfunction
# HG changeset patch
# User Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
# Date 1400688965 -7200
#      Wed May 21 18:16:05 2014 +0200
# Node ID ebf63ad7c4bb6c38c9ba9faae6d6a5f7527c031c
# Parent  54b2161da9858177f122bc1880bb4dbbe0e9cad8
ufl errors out if filename exists when start command is issued

diff -r 54b2161da985 -r ebf63ad7c4bb inst/ufl.m
--- a/inst/ufl.m	Wed May 21 17:56:22 2014 +0200
+++ b/inst/ufl.m	Wed May 21 18:16:05 2014 +0200
@@ -46,7 +46,12 @@
           filename = [filename, ".ufl"];
         endif
       endif
-      fid = fopen (filename, "w");
+      [~, err, ~] = stat (filename);
+      if (err == 0)
+        error (["ufl: a file named ", filename, " already exists"]);
+      else
+        fid = fopen (filename, "w");
+      endif
     endif
     if (! is_valid_file_id (fid))
       error (["ufl: could not open file ", filename]);
# HG changeset patch
# User Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
# Date 1400751824 -7200
#      Thu May 22 11:43:44 2014 +0200
# Node ID 1d8594749e04376f629d140d43672763c7c1f14e
# Parent  ebf63ad7c4bb6c38c9ba9faae6d6a5f7527c031c
Ensure that ufl opens a file with .ufl extension

  * inst/ufl.m: force lower case extension

diff -r ebf63ad7c4bb -r 1d8594749e04 inst/ufl.m
--- a/inst/ufl.m	Wed May 21 18:16:05 2014 +0200
+++ b/inst/ufl.m	Thu May 22 11:43:44 2014 +0200
@@ -44,6 +44,8 @@
         filename = varargin{2};
         if (isempty (regexpi (filename, ".ufl$", "match")))
           filename = [filename, ".ufl"];
+        else
+          filename = [filename(1:end-4), ".ufl"];
         endif
       endif
       [~, err, ~] = stat (filename);
# HG changeset patch
# User Eugenio Gianniti <eugenio.gianniti@mail.polimi.it>
# Date 1400754887 -7200
#      Thu May 22 12:34:47 2014 +0200
# Node ID 941a89010ce900263382a4a76eab1a2141affd30
# Parent  1d8594749e04376f629d140d43672763c7c1f14e
Compile right away after ufl end

diff -r 1d8594749e04 -r 941a89010ce9 inst/ufl.m
--- a/inst/ufl.m	Thu May 22 11:43:44 2014 +0200
+++ b/inst/ufl.m	Thu May 22 12:34:47 2014 +0200
@@ -20,15 +20,32 @@
 ## @itemize @bullet
 ## @item start: opens an ufl file. It is possible to specify a name for it,
 ## otherwise 'default.ufl' will be used.
-## @item end: closes an ufl file.
+## @item end: closes the ufl file and compiles it. It is possible to 
+## specify its @var{content}, otherwise a complete problem is assumed.
+## Possible options are: BilinearForm, Functional, FunctionSpace, LinearForm
+## and Problem.
 ## @end itemize
 ##
+## @example
+## @code{ufl start fs.ufl}
+## @code{ufl element = FunctionSpace ("Lagrange", triangle, 1)}
+## @code{ufl end FunctionSpace}
+## This way you can write a ufl file and compile it obtaining a FunctionSpace.
+## @end example
+##
+## @seealso{import_ufl_Problem, import_ufl_BilinearForm, import_ufl_Functional,
+## import_ufl_FunctionSpace, import_ufl_LinearForm} 
 ## @end deftypefn
 
 function ufl (varargin)
 
   persistent fid = -1;
-  filename = "default.ufl";
+  persistent filename = "default.ufl";
+  persistent opts = {"BilinearForm",
+                     "Functional",
+                     "FunctionSpace",
+                     "LinearForm",
+                     "Problem"};
 
   if (numel (varargin) < 1)
     if (! is_valid_file_id (fid))
@@ -65,6 +82,12 @@
       fclose (fid);
       fid = -1;
     endif
+    compile = "Problem";
+    if (numel (varargin) > 1)
+      compile = validatestring (varargin{2}, opts, "ufl", "content");
+    endif
+    eval (["import_ufl_", compile, "(\"", filename(1:end-4), "\");"]);
+    filename = "default.ufl";
   elseif (! is_valid_file_id (fid))
     error ("ufl: no open file");
   else
