s4
changeset 947:b7f9c4ce8cbd
Add auto-completion of \begin\end
author | HIROSE Yuuji <yuuji@gentei.org> |
---|---|
date | Wed, 27 Oct 2021 23:45:01 +0900 |
parents | 420ad90116e6 |
children | 1310b29b6ab1 |
files | s4-main.js |
diffstat | 1 files changed, 49 insertions(+), 5 deletions(-) [+] |
line diff
1.1 --- a/s4-main.js Tue Oct 26 13:16:29 2021 +0900 1.2 +++ b/s4-main.js Wed Oct 27 23:45:01 2021 +0900 1.3 @@ -616,8 +616,33 @@ 1.4 e.preventDefault(); 1.5 } 1.6 } 1.7 - function textInsert(area, pos, string) { 1.8 - 1.9 + function textInsert(area, string, pos1, pos2) { 1.10 + console.log("str="+string); 1.11 + area.setRangeText(string, pos1||area.selectionStart, 1.12 + pos2||pos1||area.selectionStart); 1.13 + area.selectionStart += string.length; 1.14 + } 1.15 + function beginningOfLine(area, pos) { 1.16 + pos = pos||area.selectionStart; 1.17 + let b = area.value.lastIndexOf("\n", pos); 1.18 + if (pos>1 && area.value.charCodeAt(pos)==10) 1.19 + b = area.value.lastIndexOf("\n", pos-1);; 1.20 + return b>=0 ? b : 0; 1.21 + } 1.22 + function isInBeginEnd(area, pos){ 1.23 + pos = pos||area.selectionStart; 1.24 + let bol = beginningOfLine(area, pos); 1.25 + let thisline = area.value.substr(bol); 1.26 + console.log("curchar="+area.value.charCodeAt(pos)); 1.27 + console.log("prechar="+area.value.charCodeAt(pos-1)); 1.28 + console.log("bol="+bol+", thisline="+thisline); 1.29 + let match = thisline.search(/\\(begin|end){([A-Za-z]*)/), lm, be; 1.30 + if (match >= 0) { 1.31 + lm = RegExp.lastMatch; 1.32 + be = RegExp.$1; 1.33 + return RegExp.$2 1.34 + } 1.35 + return null; 1.36 } 1.37 function helpMarkdownBrace(e) { 1.38 if (!mathjax) return; 1.39 @@ -629,14 +654,32 @@ 1.40 if (beg >= 0) { 1.41 let env = text.substr(beg).search(/\\begin{(.*?)}/); 1.42 if (env >= 0) { 1.43 - let endenv = "{"+RegExp.$1+"}"; 1.44 - area.setRangeText(endenv, pos, pos); 1.45 - area.selectionStart += endenv.length; 1.46 + textInsert(area, "{"+RegExp.$1+"}", pos); 1.47 e.preventDefault(); 1.48 } 1.49 } 1.50 } 1.51 } 1.52 + function helpMarkdownBraceClose(e) { 1.53 + if (!mathjax) return; 1.54 + let area = e.target, pos = area.selectionStart, text = area.value, 1.55 + begin = "\\begin", end = "\\end"; 1.56 + if (text.substr(pos).startsWith("}")) { 1.57 + area.setRangeText("", pos, pos+1); 1.58 + // e.preventDefault(); 1.59 + } 1.60 + let inbegend = isInBeginEnd(area, pos); 1.61 + let nextendpos = text.substr(pos).indexOf("\\end{"); 1.62 + let nextcurend = text.substr(pos).indexOf("\\end{"+inbegend+"}"); 1.63 + console.log("ib="+inbegend+", nepos="+nextendpos+", ncur="+nextcurend); 1.64 + if (nextcurend<0 || nextendpos!=nextcurend) { 1.65 + area.setRangeText("}\n\n\\end{"+inbegend+"}", pos, pos); 1.66 + area.selectionStart = pos+2; 1.67 + e.preventDefault(); 1.68 + } 1.69 + console.log(inbegend); 1.70 + 1.71 + } 1.72 function helpMarkdownPreview(area) { 1.73 if (!mathjax) { 1.74 alert("no"+e.target) 1.75 @@ -677,6 +720,7 @@ 1.76 case "(": helpMarkdownParen(e); break; 1.77 case "p": if (e.metaKey) helpMarkdownPreview(e.target); break; 1.78 case "{": helpMarkdownBrace(e); break; 1.79 + case "}": helpMarkdownBraceClose(e); break; 1.80 } 1.81 } 1.82 /* Init event listeners */