[SOLVED] Ability to move current line up or down? lazarus / 9-Editor ---------------------------------------------------------------------- knuckles · 2018-02-23 19:27 As the title, is it possible in the editor to move the line where the caret is up or down? In Visual Studio for example I believe Alt+Up and Alt+Down will move the line up and down respectively in the editor. This would be a really useful feature of the SynEdit / Lazarus editor if we could do this, if this is already possible then please advise how to do so ---------------------------------------------------------------------- rvk · 2018-02-23 20:29 Like CTRL + Arrow up and CTRL + Arrow down? ---------------------------------------------------------------------- taazz · 2018-02-23 20:49 I bet he means something like Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TForm1.btnUpClick(Sender :TObject);begin  SynEdit1.Lines.Exchange(SynEdit1.CaretY-1, SynEdit1.CaretY-2 );  SynEdit1.CaretY := SynEdit1.CaretY - 1;end; procedure TForm1.btnDownClick(Sender :TObject);begin  SynEdit1.Lines.Exchange(SynEdit1.CaretY-1, SynEdit1.CaretY );  SynEdit1.CaretY := SynEdit1.CaretY + 1;end;   which is easy to wrap it in an IDE addon......... be right back ---------------------------------------------------------------------- rvk · 2018-02-23 21:34 Ah, now I get it. Can't you use Editor Macros for that? EditorMacros.xml in your config directory: (Ctrl+Alt+Up is 1 line up and Ctrl+Alt+Down is 1 line down) The upside of this method is that indenting is automatically adjusted which isn't the case with SynEdit1.Lines.Exchange I think. Code: XML  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i                                             ---------------------------------------------------------------------- taazz · 2018-02-23 21:51 Find attached a package that installs two extra commands to move the line under the cursor up or down it is written fast and tested little, but if you compile it in lazarus you will find two commands under the source menu (the two commands before the last) and have the shortcuts ctrl+alt+up,ctrl+alt+Down which can be changed in the application options I guess, I did not test it. TODO (some one else I guess) 1) auto enable/disable depending on the cursor position 2) add undo/redo steps in the editor. ---------------------------------------------------------------------- knuckles · 2018-02-24 13:51 Awesome taazz Would love to see this officially implemented in future Lazarus/Synedit releases. ---------------------------------------------------------------------- knuckles · 2018-02-24 13:53 Have not tried your way rvk but I did look before at the editor macro shortcuts, never thought of editing it myself as you have shown. ---------------------------------------------------------------------- rvk · 2018-02-24 14:16 I've also recorded the macro first. Ctrl+Shift+R is record on/off. After that you can look in View>Editor Macros to see how they look. And they worked fine. I moved them to IDE and showed you the file so it's easier to put them in. I do find the macro method more intuitive because if you move a line up and above the block, the indentation is directly adjusted automatically. So you don't need to adjust it. If you begin with this (highlighted line to be moved up) Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // other linetry  // line to movefinallyend; Using the Lazarus Add-in you'll get this if you move the line from inside the try to above the try (I think, I haven't tried the Add-in). Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // other line  // line to movetryfinallyend; And with the macro I got this Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i // other line// line to movetryfinallyend; So, if the method of taazz is going to make it as official "move-line" feature, it would also need to adjust the indentation automatically like it is done during normal copy-methods (and macro method). And now that I think of it... this should be made that you can also do this with an entire block. So you select a complete block of code and press Ctrl+Alt+Up to move it up (and adjusting the indentation accordingly). Moving a complete block would even be more useful than one line (although I always use cut/paste). In notepad++ this already works for Line and blocks with Ctrl+Shift+Up/Down. ---------------------------------------------------------------------- Martin_fr · 2018-02-24 15:58 Look at http://wiki.lazarus.freepascal.org/Editor_Macros_PascalScript with this you can write a macro that: - does not need to use the clipboard - can detect if a selection exists, and act according ---------------------------------------------------------------------- knuckles · 2018-02-24 18:44 Agreed, I just went to move an entire block (about 4 lines of code) up and it only moved the active line up, ctrl+z (undo) clearly doesn't work but was to be expected and already mentioned by taazz. In regards to the indentation I never considered this at the time of writing, but yes I also agree Editor Macros are most likely the ideal way to go, would love to see this feature integrated in one of the future Lazarus releases ---------------------------------------------------------------------- Martin_fr · 2018-02-24 19:20 Patch (on mantis) would be welcome (ping me if there is no reaction to such a patch). Should not be much work. But must include adding IDE shortcuts (no defaults needed, just entries for config) See svn revision: 50091 SynEdit/IDE: zoom via keyboard This shows how to add the shortcuts. ecSwapLineUp ecSwapLineDown or similar. Don't call it "move", because that is too easy mistaken with cursor movement, or scroll movement.... If you like you can make 2 pairs ecSwapLineUp  // ignore selection ecSwapSelectedLinesUp Better names can be proposed.... ------------ About undo. Do NOT access the synedit.lines object. Use TextBetweenPoints and similar. Or better use FTheLinesView.EditInsert and FTheLinesView.EditDelete and similar. Those take care of undo. ---------------------------------------------------------------------- Martin_fr · 2019-11-04 11:47 Lazarus trunk 2.1 now has line-up/down/duplicate keys.