public void ActionComment(IUserInterface ui) { if (main.isTrace) { return; } int b; int e; azukiControl.GetSelection(out b, out e); int st = azukiControl.GetLineHeadIndexFromCharIndex(b); b = azukiControl.GetLineHeadIndexFromCharIndex(e); int li = azukiControl.GetLineIndexFromCharIndex(e); int ed = b + azukiControl.GetLineLength(li); azukiControl.SetSelection(st, ed); string line = azukiControl.GetSelectedText(); string[] lines = line.Split(new string[] { "\r\n" }, StringSplitOptions.None); //チェック bool flg = false; foreach (string s in lines) { if (s.Length < 1 || s[0] != '\'') { flg = true; break; } } line = ""; if (flg) { // 'をつける foreach (string s in lines) { line += "'" + s + "\r\n"; } line = line.Substring(0, line.Length - 2); } else { // 'をカットする foreach (string s in lines) { line += s.Substring(1) + "\r\n"; } line = line.Substring(0, line.Length - 2); } azukiControl.Document.Replace(line, st, ed); azukiControl.SetSelection(st, st + line.Length); }
public bool SearchFindNextEx(string sTextPtn, bool searchUseRegex, bool mmltarget) { //AzukiのAnnの検索処理を利用 Sgry.Azuki.Document azdoc = ac.Document; int startIndex; int endIndex; Regex regex; if (searchAnchorIndex == -1) { searchAnchorIndex = select.Begin; } else { ac.GetSelection(out int st, out int ed); searchAnchorIndex = ed; } // determine where to start text search if (0 <= searchAnchorIndex) { startIndex = searchAnchorIndex; } else { startIndex = Math.Max(azdoc.CaretIndex, azdoc.AnchorIndex); } endIndex = select.End; bool fnd = false; int opIndex = 0; int conIndex = 0; int conEndIndex = 0; while (startIndex < endIndex) { // 対象がmml行か調べる。 if (fmt == EnmMmlFileFormat.MUC) { //対象行の文字列を得る int trgIndex = ac.GetLineIndexFromCharIndex(startIndex); int trgHeadIndex = ac.GetLineHeadIndexFromCharIndex(startIndex); int trgEndIndex = ac.Document.GetLineEndIndexFromCharIndex(startIndex); string con = ac.Document.GetLineContent(trgIndex); Regex rp = new Regex("^[A-Za-z0-9]+[ |\\t]"); //パート Match match1 = rp.Match(con, 0); rp = new Regex("^#\\s*\\*[0-9]+"); //マクロ Match match2 = rp.Match(con, 0); rp = new Regex("^[#|!][@_A-Za-z0-9]*[ |\\t]"); //TAG Match match3 = rp.Match(con, 0); //対象行ではない場合は次の行 if (!match1.Success && !match2.Success && !match3.Success) { startIndex = trgEndIndex; continue; } //対象行の場合は次の検索へ opIndex = trgHeadIndex; conIndex = match1.Success ? (match1.Index + match1.Length) : ( match2.Success ? (match2.Index + match2.Length) : (match3.Index + match3.Length) ) + opIndex; conEndIndex = trgEndIndex; } else if (fmt == EnmMmlFileFormat.MML) { //対象行の文字列を得る int trgIndex = ac.GetLineIndexFromCharIndex(startIndex); int trgHeadIndex = ac.GetLineHeadIndexFromCharIndex(startIndex); int trgEndIndex = ac.Document.GetLineEndIndexFromCharIndex(startIndex); string con = ac.Document.GetLineContent(trgIndex); Regex rp = new Regex("^[A-Za-z]+[0-9]*[ |\\t]"); Match match1 = rp.Match(con, 0); rp = new Regex("^[#|!][A-Za-z0-9]*[ |\\t]"); Match match2 = rp.Match(con, 0); rp = new Regex("^#\\S*\\*"); Match match3 = rp.Match(con, 0); //対象行ではない場合は次の行 if (!match1.Success && !match2.Success && !match3.Success) { startIndex = trgEndIndex; continue; } //対象行の場合は次の検索へ opIndex = trgHeadIndex; conIndex = match1.Success ? (match1.Index + match1.Length) : ( match2.Success ? (match2.Index + match2.Length) : (match3.Index + match3.Length) ) + opIndex; conEndIndex = trgEndIndex; } else if (fmt == EnmMmlFileFormat.MDL) { //対象行の文字列を得る int trgIndex = ac.GetLineIndexFromCharIndex(startIndex); int trgHeadIndex = ac.GetLineHeadIndexFromCharIndex(startIndex); int trgEndIndex = ac.Document.GetLineEndIndexFromCharIndex(startIndex); string con = ac.Document.GetLineContent(trgIndex); Regex rp = new Regex("^[A-Za-z]+[0-9]*[ |\\t]"); Match match1 = rp.Match(con, 0); rp = new Regex("^[#|!][A-Za-z0-9]*[ |\\t]"); Match match2 = rp.Match(con, 0); rp = new Regex("^'%\\S+[ |\\t]"); Match match3 = rp.Match(con, 0); //対象行ではない場合は次の行 if (!match1.Success && !match2.Success && !match3.Success) { startIndex = trgEndIndex; continue; } //対象行の場合は次の検索へ opIndex = trgHeadIndex; conIndex = match1.Success ? (match1.Index + match1.Length) : ( match2.Success ? (match2.Index + match2.Length) : (match3.Index + match3.Length) ) + opIndex; conEndIndex = trgEndIndex; } else { //gwi/etc //対象行の文字列を得る int trgIndex = ac.GetLineIndexFromCharIndex(startIndex); int trgHeadIndex = ac.GetLineHeadIndexFromCharIndex(startIndex); int trgEndIndex = ac.Document.GetLineEndIndexFromCharIndex(startIndex); string con = ac.Document.GetLineContent(trgIndex); Regex rp = new Regex("^'[A-Za-z0-9\\-\\,\\+]+_*[ |\\t]"); //パート1 Match match1 = rp.Match(con, 0); rp = new Regex("^'[A-Za-z0-9\\-\\,\\+]+~*[ |\\t]"); //パート2 Match match2 = rp.Match(con, 0); rp = new Regex("^'%\\S+[ |\\t]"); //エイリアス Match match3 = rp.Match(con, 0); //対象行ではない場合は次の行 if (!match1.Success && !match2.Success && !match3.Success) { startIndex = trgEndIndex; continue; } //対象行の場合は次の検索へ opIndex = trgHeadIndex; conIndex = ( match1.Success ? (match1.Index + match1.Length) : ( match2.Success ? (match2.Index + match2.Length) : (match3.Index + match3.Length) ) ) + opIndex; conEndIndex = trgEndIndex; } //検索対象位置に置換対象の文字列があるか調べる if (searchUseRegex) { // Regular expression search. // get regex object from context regex = searchRegex; if (regex == null) { return(false); } // ensure that "RightToLeft" option of the regex object is NOT set RegexOptions opt = regex.Options; if ((opt & RegexOptions.RightToLeft) != 0) { opt &= ~(RegexOptions.RightToLeft); regex = new Regex(regex.ToString(), opt); searchRegex = regex; } if (mmltarget) { result = azdoc.FindNext(regex, opIndex, conIndex); } else { result = azdoc.FindNext(regex, conIndex, conEndIndex); } } else { // normal text pattern matching. if (startIndex < azdoc.Length) { if (mmltarget) { result = azdoc.FindNext(sTextPtn, opIndex, conIndex, searchMatchCase); } else { result = azdoc.FindNext(sTextPtn, conIndex, conEndIndex, searchMatchCase); } } else { result = null; } } if (result == null) { startIndex = conEndIndex; continue; } fnd = true; break; } if (!fnd) { return(false); } // select the result if (result == null) { return(false); } if (select.Begin != select.End && result.Begin > select.End) { return(false); } ac.Document.SetSelection(result.Begin, result.End); ac.View.SetDesiredColumn(); ac.ScrollToCaret(); searchAnchorIndex = result.End; return(true); }