Example #1
0
        /// <summary>
        /// check if the User Defined Language for "OpenEdgeABL" exists in the
        /// userDefineLang.xml file, if it does it updates it, if it doesn't exists it creates it and asks the user
        /// to restart Notepad++
        /// Can also only check and not install it by setting onlyCheckInstall to true
        /// </summary>
        public static bool InstallUdl(bool onlyCheckInstall = false)
        {
            var encoding    = TextEncodingDetect.GetFileEncoding(Config.FileNppUdlXml);
            var fileContent = File.Exists(Config.FileNppUdlXml) ? Utils.ReadAllText(Config.FileNppUdlXml, encoding) : @"<NotepadPlus />";
            var regex       = new Regex("<UserLang name=\"OpenEdgeABL\".*?</UserLang>", RegexOptions.Singleline | RegexOptions.IgnoreCase);
            var matches     = regex.Match(fileContent);

            if (matches.Success)
            {
                if (onlyCheckInstall)
                {
                    return(true);
                }
                // if it already exists in the file, delete the existing one
                fileContent = regex.Replace(fileContent, @"");
            }
            else
            {
                if (onlyCheckInstall)
                {
                    return(false);
                }
                // if it doesn't exist in the file
                UserCommunication.Notify("It seems to be the first time that you use this plugin.<br>In order to activate the syntax highlighting, you must restart notepad++.<br><br><i>Please note that if a document is opened at the next start, you will have to manually close/reopen it to see the changes.</i><br><br><b>Sorry for the inconvenience</b>!", MessageImg.MsgInfo, "Information", "Installing syntax highlighting");
            }
            if (fileContent.ContainsFast(@"<NotepadPlus />"))
            {
                fileContent = fileContent.Replace(@"<NotepadPlus />", "<NotepadPlus>\r\n" + DataResources.UDL + "\r\n</NotepadPlus>");
            }
            else
            {
                fileContent = fileContent.Replace(@"<NotepadPlus>", "<NotepadPlus>\r\n" + DataResources.UDL);
            }
            // write to userDefinedLang.xml
            try {
                Utils.FileWriteAllText(Config.FileNppUdlXml, fileContent, encoding);
            } catch (Exception e) {
                if (e is UnauthorizedAccessException)
                {
                    UserCommunication.Notify("<b>Couldn't access the file :</b><br>" + Config.FileNppUdlXml + "<br><br>This means i couldn't correctly applied the syntax highlighting feature!<br><br><i>Please make sure to allow write access to this file (Right click on file > Security > Check what's needed to allow total control to current user)</i>", MessageImg.MsgError, "Syntax highlighting", "Can't access userDefineLang.xml");
                }
                else
                {
                    ErrorHandler.ShowErrors(e, "Error while accessing userDefineLang.xml");
                }
                return(false);
            }
            return(true);
        }