Drafts Action Directory

Number Lines

Posted by drdrang, Last update 4 months ago

Add line numbers (followed by a colon and two spaces) to either the selected text or, if there’s no selection, the entire draft.

Install

Steps

  • script

    // Number the selected lines (or all lines) with colon and
    // two spaces after each line number.
    
    // Get either the current selection or the entire draft.
    var sel = editor.getSelectedText();
    if (!sel || sel.length==0) {
      editor.setSelectedRange(0, editor.getText().length);
      sel = editor.getSelectedText();
      }
    
    // Break the text into lines and number them.
    // Right-justify the numbers and put a colon and
    // two spaces after the line number.
    var lines = sel.split('\n');
    var numLines = lines.length;
    var width = Math.floor(Math.log(numLines)*Math.LOG10E) + 1;
    var numbered = [];
    var lNum;
    for (var i=0; i<numLines; i++) {
      lNum = i + 1;
      numbered.push(lNum.toString().padStart(width) + ':  ' + lines[i]);
    }
    
    // Replace the original text with the line-numbered text.
    editor.setSelectedText(numbered.join('\n'));
    

Options

  • After Success Default
    Notification Info
    Log Level Info

Comments

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