sakai-memoru
12/5/2019 - 12:23 AM

Sakura Macro::Markdownのリスト入力補助

Editorで、markdownを編集している際に、List表記(-, +, *)を入力時に、次の行で、List記号を補完するマクロ

ctrl+shift+Enterにキー登録しておくと、使い勝手が良い。

(function(){

// 
// bulletForMD.js
//  - 210621 bug fix
//  - 191209 bug fix
//  - 191206 initial commit

// local method
var add_line = function(line_str, line_code){
  if(typeof line_code === 'undefined') line_code = '\r\n'
  Editor.GoLineTop(1);
  Editor.Down();
  Editor.InsText(line_code);
  Editor.Up();
  Editor.InsText(line_str);
  Editor.GoLineEnd();
  
}

// do process
var doProcess = function(){
  Editor.TextWrapMethod(0);
  var line_str = Editor.GetLineStr(0);  // get line string on cursor
  Editor.BeginSelect();
  Editor.GoLineTop_Sel();
  var line_num = Editor.GetSelectLineFrom();
  Editor.GoLineEnd_Sel();
  var line_strToEnd = Editor.GetSelectedString(0);
  Editor.TraceOut(line_num);
  //
  if(line_strToEnd !== ''){
    Editor.Delete();
  } 
  Editor.CancelMode();
  var regex_indent = /^([\t ]*)/;
  var indent = line_str.match(regex_indent)[0]
  var add_line_str = '';
  var regex = /(^[\t ]*)(\-\s\[\s\]|[\-+*\>#]|\/\/)/;
  if(regex.test(line_str)){
    regex.exec(line_str);
    var bullet_str = RegExp.$2;
    add_line_str = indent + bullet_str + ' ' + line_strToEnd;
  } else {
    add_line_str = indent + line_strToEnd;
  }
  //Editor.TraceOut(add_line_str);
  add_line(add_line_str);
  Editor.TextWrapMethod(2);
  var line_num_layout = Editor.LogicToLayoutLineNum(line_num,1);
  //Editor.TraceOut(line_num_layout);
  Editor.Jump(line_num_layout + 1,0);
  Editor.GoLineEnd();

}

var doDebug = function(){
  Editor.TextWrapMethod(0);
  var line_str = Editor.GetLineStr(0);  // get line string on cursor
  Editor.BeginSelect();
  Editor.GoLineTop_Sel();
  var line_num = Editor.GetSelectLineFrom();
  Editor.GoLineEnd_Sel();
  var line_strToEnd = Editor.GetSelectedString(0);
  Editor.TraceOut(line_num);
  
}

// --------------------------------------- entry point
if(typeof(Editor) !== 'undefined'){
  doProcess();
  //doDebug();
} else {
  if(typeof(WScript) !== 'undefined'){
    WScript.Echo('This script is for sakura macro. A env is maybe wsh.')
  } else {
    console.log('This script is for sakura macro. A env is maybe node.')
  }
}

}())