Ejemplo n.º 1
0
        /// <summary>Save the current function being edited.</summary>
        public void Save()
        {
            if (!IsValidName(editFunctionName))
            {
                editFunctionStyle = invalidFunctionName;
                return;
            }
            else
            {
                editFunctionStyle = validFunctionName;
            }

            int id;

            if (editFunction != null && editFunction.Id != editFunctionName)
            {
                // Changing function name, remove old function
                string oldFunctionFile = functionDir + "/" + editFunction.Id + ".math";
                if (System.IO.File.Exists(oldFunctionFile))
                {
                    try {
                        System.IO.File.Delete(oldFunctionFile);
                        error = null;
                    } catch (Exception e) {
                        error = "Cannot save function: " + e.Message;
                        return;
                    }
                }

                kalc.Functions.Remove(editFunction.Id);

                if (selectedFunction != null && selectedFunction.Id == editFunction.Id)
                {
                    selectedFunction = null;
                }

                if (RunFunction != null && RunFunction.Id == editFunction.Id)
                {
                    RunFunction = null;
                }

                id = WindowIdOfRepeatingFunction(editFunction.Id);
                if (id != -1)
                {
                    envs.Remove(id);
                }
            }

            // Save new function
            try {
                functionFile = functionDir + "/" + editFunctionName + ".math";
                System.IO.File.WriteAllText(functionFile, editFunctionContent);
                error = null;
            } catch (Exception e) {
                error = "Cannot save function: " + e.Message;
                return;
            }

            // Compile new function
            JITFunction f = JITFunction.FromFile(functionFile, kalc);

            f.Compile();
            if (RunFunction != null && RunFunction.Id == f.Id)
            {
                RunFunction = f;
            }

            id = WindowIdOfRepeatingFunction(f.Id);
            if (id != -1)
            {
                envs[id].func = f;
            }

            if (!kalc.Functions.ContainsKey(editFunctionName))
            {
                kalc.Functions.Add(editFunctionName, f);
            }
            else
            {
                kalc.Functions[editFunctionName] = f;
            }

            editFunction = f;
        }