How to insert a multi-line template with auto-indent?

How to insert a multi-line template with auto-indent?

12
Basic UserBasic User
12

    Sep 15, 2011#1

    Hi,

    Is it possible to create a template item that inserts at the tab indent and keeps that tab indent position for multi line templates?
    So if the file I'm working on and at the position I am at is tab indented twice, it will paste the mutli line template at indent tab 2 instead of 1st line correct at cursor and the rest at no indent. Kind of auto detect how many tab intents we are at and make that the insert start for each line?

    Thanks

    Darcey

    6,688587
    Grand MasterGrand Master
    6,688587

      Sep 15, 2011#2

      Unfortunately inserting a multi-line template is always done with ignoring the auto-indent feature. You need to store such strings in a macro because inserting a multi-line string with a macro results in auto-indenting every inserted line if auto-indent feature is enabled. For example

      Code: Select all

      InsertMode
      ColumnModeOff
      "if()
      {
      
      }
      "
      Key UP ARROW
      Key UP ARROW
      Key UP ARROW
      Key UP ARROW
      Key RIGHT ARROW
      Key RIGHT ARROW
      Key RIGHT ARROW
      inserts an IF statement with the braces and caret blinking for further input between the round brackets. After inserting the condition, pressing twice key DOWN and editing can continue with first statement. A combination of single line templates inserted by hotkey with multi-line macros with hotkeys stored together in a single macro file automatically loaded on startup of UltraEdit makes coding very efficient.

      Often, but not too often needed strings can be stored either in an auto-completion file (single line strings only) or are inserted with the tag list feature. Tags can be also multi-line strings, but are inserted also always without auto-indent. But tags have the advantage that they can be used to insert strings around a current selection (as templates also offer with [$replace$]).

      And the HTML toolbar commands can be also used as help for inserting often needed strings via a toolbar click for other languages too, not just for HTML and XHTML because the text to insert text, the icon and the tooltip can be customized for most of those commands. If you are a C/C++ programmer and never write HTML, why not redefining the HTML toolbar for C/C++.

      12
      Basic UserBasic User
      12

        Sep 15, 2011#3

        Interesting... Thanks for the info mofi.

        Is it possible to achieve this insert with a script? Is it possible to detect tab indent the cursor is on when the script is run and insert a string? Could be a simpler approach.

        Thanks

        Darcey

        6,688587
        Grand MasterGrand Master
        6,688587

          Sep 15, 2011#4

          Similar to the macro above is the following script.

          Code: Select all

          if (UltraEdit.document.length > 0) {
             UltraEdit.insertMode();
             if (typeof(UltraEdit.columnModeOff) == "function") UltraEdit.columnModeOff();
             else if (typeof(UltraEdit.activeDocument.columnModeOff) == "function") UltraEdit.activeDocument.columnModeOff();
             var nColumn = UltraEdit.activeDocument.currentColumnNum;
             if (typeof(UltraEdit.activeDocumentIdx) == "undefined") nColumn++;
             var nLine = UltraEdit.activeDocument.currentLineNum;
             var sIndent = "";
             if (nColumn > 1) {
                UltraEdit.activeDocument.gotoLineSelect(0,1);
                if (UltraEdit.activeDocument.isSel()) {
                   sIndent = UltraEdit.activeDocument.selection.replace(/^([ \t]+).*$/,"$1");
                   if (sIndent == UltraEdit.activeDocument.selection) sIndent = "";
                }
             }
             UltraEdit.activeDocument.gotoLine(nLine,nColumn);
             UltraEdit.activeDocument.write("if()\r\n"+sIndent+"{\r\n"+sIndent+"\t\r\n"+sIndent+"}");
             UltraEdit.activeDocument.gotoLine(nLine,nColumn+3);
          }
          But there are some disadvantages with the script as is.

          The script does not recognize the type of line terminator of the active file. Well, depending on version of UE/UES the property UltraEdit.activeDocument.lineTerminator could be used to make the script independent of line terminator type of active file.

          Indent and unindent must be done within the script itself. But the script cannot really find out which settings are currently used for the active file. Are tabs or spaces used and if spaces, how many spaces? Well, if you want a script just for you and all your files are using always the same indent, then this is no real problem because you can use fixed spaces/tabs in the string.

          A problem with script execution is that the status of the script is written to active output window after clearing current content. This behavior can be turned off with a configuration setting. But when you use this setting, don't forget to enable it temporarily when you next time develop a script because then also no errors detected by the Javascript interpreter are written to the output window.

          The main disadvantage is that every script execution results in displaying the small Cancel dialog. While for macros there is a macro property to turn this off and break a macro execution with pressing ESC, it is not possible for scripts to turn off displaying the Cancel dialog because script execution can't be breaked with ESC.

          12
          Basic UserBasic User
          12

            Sep 16, 2011#5

            Thanks Mofi, I will look into this more.