Drafts Action Directory

Select Outward

Posted by @kall, Last update 5 months ago

This keyboard action extends the text selection on consecutive presses going Word -> Line -> All

Install

Steps

  • script

    const sel = editor.getSelectedText();
    const text = editor.getText();
    const selRange = editor.getSelectedRange();
    const lineRange = editor.getSelectedLineRange();
    const start = selRange[0];
    const end = selRange[0] + selRange[1];
    
    const newl = /^[\n]$/;
    
    const lineEnd = end === text.length ||newl.test(text[end]);
    
    const lineStart = start === 0 || newl.test(text[start - 1]);
    
    const lineSelected = lineStart && lineEnd;
    
    if (!sel || sel.length == 0) {
      selectWord();
    }
    
    else if (!sel.includes(" ") && !sel.includes("\n") && !lineSelected) {
      selectLine();
    }
    
    else {
      selectAll();
    }
    
    function selectWord() {
    var n = text.length;
    var i = start;
    const delim = /^[\s]$/;
    
    while (i > 0) {
      if (delim.test(text[i - 1])) {
      break;
      }
      --i;
    }
    
    var j = end;
    
    while (j < n) {
      if (delim.test(text[j])) {
        break;
      }
      ++j;
    }
    
    if (j-i) {
      editor.setSelectedRange(i, j-i);
    }
    // curson on an empty line
    else {editor.setSelectedRange(start - 1, 1);}
    }
    
    function selectLine() {
    var eol = lineRange[0] + lineRange[1];
    if ( editor.getTextInRange( eol - 1, 1 ) === '\n' ) {
      eol = eol - 1;
    }
    
    editor.setSelectedRange( lineRange[0], eol - lineRange[0] );
    }
    
    function selectAll() {
    editor.setSelectedRange(0, text.length);
    }

Options

  • After Success Nothing
    Notification Error
    Log Level Error

Comments

Actions available in the Action Directory are uploaded by community members. Use appropriate caution reviewing downloaded actions before use.