Example #1
0
        /// <summary>
        /// Find and capitalize the first letter of the beginning of all
        /// sentences in a document.
        /// </summary>
        internal static void CapSentence()
        {
            // Get current scintilla instance.
            IntPtr           currentScint     = PluginBase.GetCurrentScintilla();
            ScintillaGateway scintillaGateway = new ScintillaGateway(currentScint);

            try
            {
                // Get the length of the document.
                int length = scintillaGateway.GetLength();
                // Get the text in the document.
                string allText = scintillaGateway.GetText(length + 1);

                // Convert the text to char array for easy manipulation.
                char[] charArrayAllText = allText.ToCharArray();

                char firstLetter   = new char();
                bool capAfterPunct = true;

                // For the length of the selected text...
                for (int i = 0; i < allText.Length; i++)
                {
                    // If there is punctuation that ends a sentence
                    // the next word should be capitalized.
                    if (allText[i] == '.' || allText[i] == '?' || allText[i] == '!' || allText[i] == '\r')
                    {
                        capAfterPunct = true;
                    }
                    // Don't capitalize Markdown titles in this method.
                    else if (allText[i] == '#')
                    {
                        capAfterPunct = false;
                    }

                    if (capAfterPunct && !ignoreChars.Contains(allText[i]))
                    {
                        // If the current character is not a whitespace character
                        // convert the first letter of the word to uppercase.
                        firstLetter = Convert.ToChar(allText[i].ToString().ToUpperInvariant());
                        // Replace the correct char in the selected text with its uppercase letter.
                        charArrayAllText.SetValue(firstLetter, i);
                        // Revert to false until beginning of next sentence.
                        capAfterPunct = false;
                    }
                }
                // Convert char array back to string.
                allText = new string(charArrayAllText);
                // Replace the document text with the new text.
                scintillaGateway.SelectAll();
                scintillaGateway.ReplaceSel(allText);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }
Example #2
0
 /// <summary>
 /// Gets all text of the current document.
 /// </summary>
 static public string GetAllText(this ScintillaGateway scintilla)
 => scintilla.GetText(scintilla.GetTextLength() + 1);
Example #3
0
 static public string AllText(this ScintillaGateway document)
 {
     return(document.GetText(document.GetLength() + 10));
 }
Example #4
0
        /// <summary>
        /// Perform an XPath search.
        /// </summary>
        /// <param name="xpathStrings">List of strings to search against.</param>
        public void DoSearch(string[] xpathStrings)
        {
            try
            {
                worker.ReportProgress(0, new ProgressReporter("Processing..."));
                XmlDocument XMLdoc = new XmlDocument();
                try
                {
                    IntPtr        curScintilla = PluginBase.GetCurrentScintilla();
                    int           length       = (int)Win32.SendMessage(curScintilla, SciMsg.SCI_GETLENGTH, 0, 0) + 1;
                    StringBuilder sb           = new StringBuilder(length);

                    //Construct Scintilla Gateway, pull text and append document text into sb object.
                    //This prevents the need for massive reconstruction
                    ScintillaGateway Scintilla = new ScintillaGateway(curScintilla);
                    sb.Append(Scintilla.GetText(length));

                    string doc = sb.ToString();

                    if (Main.settings.IgnoreDocType)
                    {
                        XMLdoc.XmlResolver = null;
                    }
                    XMLdoc.LoadXml(doc.Replace("&", "&amp;"));
                }
                catch (XmlException xex)
                {
                    worker.ReportProgress(0, new ProgressReporter("Document ERROR"));
                    worker.ReportProgress(0, new ProgressReporter(xex.Message, "Document ERROR"));
                    try
                    {
                        IntPtr curScintilla = PluginBase.GetCurrentScintilla();
                        int    startPos     = (int)Win32.SendMessage(curScintilla, SciMsg.SCI_POSITIONFROMLINE, xex.LineNumber - 1, 0) + xex.LinePosition - 1;
                        Win32.SendMessage(curScintilla, SciMsg.SCI_GOTOPOS, startPos, 0);
                    }
                    catch { }
                    return;
                }

                bool bitDefaultRemoved = false;

                if (Main.settings.defaultNamespace == "")
                {
                    int Before = XMLdoc.OuterXml.Length;
                    XMLdoc = Main.RemoveDefaultNamespaces(XMLdoc);
                    if (XMLdoc.OuterXml.Length != Before)
                    {
                        bitDefaultRemoved = true;
                    }
                }

                XPathNavigator      navigator = XMLdoc.CreateNavigator();
                XmlNamespaceManager xnm       = new XmlNamespaceManager(XMLdoc.NameTable);
                Main.DNSCount = 1;
                xnm           = Main.GetNameSpaces(XMLdoc.SelectSingleNode("/*"), xnm);
                XPathExpression xpe;
                object          result;
                string          strres = "";
                System.Xml.XPath.XPathResultType xprt = System.Xml.XPath.XPathResultType.Any;

                int i = 1;

                foreach (string s in xpathStrings)
                {
                    if (worker.CancellationPending)
                    {
                        return;
                    }
                    string xPath = s;
                    //xPath Comments support.
                    //I know xPath 1.0 does not support these, but it is nice to be able to store comments at least in NPP.
                    while (xPath.Contains("(:") && xPath.Contains(":)"))
                    {
                        int intStart = xPath.IndexOf("(:");
                        int intEnd   = xPath.IndexOf(":)", i);

                        if (intEnd <= intStart)
                        {
                            intEnd = xPath.Length - 2;
                        }

                        xPath = xPath.Remove(intStart, intEnd - intStart + 2);
                    }
                    if (xPath != "")
                    {
                        try
                        {
                            xpe    = XPathExpression.Compile(xPath, xnm);
                            result = navigator.Evaluate(xpe);
                            xprt   = xpe.ReturnType;
                            strres = result.ToString();
                        }
                        catch (System.Xml.XPath.XPathException xpx)
                        {
                            worker.ReportProgress(0, new ProgressReporter(i + ": ERROR"));
                            worker.ReportProgress(0, new ProgressReporter(xpx.Message.Replace("'" + xPath + "'", "The xPath statement"), i + ": ERROR"));
                            i++;
                            continue;
                        }
                        if (xprt == System.Xml.XPath.XPathResultType.NodeSet)
                        {
                            XPathNodeIterator xpni = navigator.Select(xPath, xnm);
                            string            ss   = "s";
                            try
                            {
                                if (xpni.Count == 1)
                                {
                                    ss = "";
                                }
                            }
                            catch (Exception ex)
                            {
                                worker.ReportProgress(0, new ProgressReporter(i + ": ERROR"));
                                worker.ReportProgress(0, new ProgressReporter(ex.Message, i + ": ERROR"));
                                i++;
                                continue;
                            }
                            TreeNode tNode = new TreeNode(i + ": " + xpni.Count + " Hit" + ss);
                            tNode.Tag = "X:" + xPath;
                            worker.ReportProgress(0, new ProgressReporter(tNode));
                            while (xpni.MoveNext())
                            {
                                if (worker.CancellationPending)
                                {
                                    return;
                                }
                                if (xpni.Current is IHasXmlNode)
                                {
                                    XmlNode Node     = ((IHasXmlNode)xpni.Current).GetNode();
                                    string  pos      = Main.FindXPath(Node, bitDefaultRemoved);
                                    string  NodeText = Main.NodetoText(xpni.Current.OuterXml);
                                    if (NodeText.StartsWith("Text") || NodeText.StartsWith("CDATA"))
                                    {
                                        tNode = new TreeNode(NodeText);
                                    }
                                    else
                                    {
                                        TreeNode[] childNodes = GetChildren(Node, bitDefaultRemoved);
                                        if (childNodes != null)
                                        {
                                            tNode = new TreeNode(NodeText, childNodes);
                                        }
                                    }
                                    tNode.Tag = pos;
                                    worker.ReportProgress(0, new ProgressReporter(tNode, i + ": " + xpni.Count + " Hit" + ss));
                                }
                            }
                        }
                        else
                        {
                            worker.ReportProgress(0, new ProgressReporter(i + ": " + xprt + ": " + strres));
                        }
                    }
                    i++;
                }
            }
            catch (Exception ex)
            {
                worker.ReportProgress(0, new ProgressReporter("An unexpected error has occured: " + ex.Message));
            }
        }