0
votes

Can't seem to wrap my head around the proper regex!

MY GOAL

  • add 2 spaces to each line of a selected block of text

MY CONTEXT

  • some markdown tools I used need 2 spaces at the end of each line to properly manage lists, etc.
  • if a file is edited multiple times, I do not want to end up with lines ending with 4+ spaces
  • a block of text can be a line, a paragraph, the whole file content as shown in the editor
  • I have some kind of macro in Notepad++ that does the trick but I want to do the same with Autohotkey to be editor-independant

MY EXAMPLE

----

# 2020-03-17

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.

In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,

MY SNIPPET SO FAR

; CTL+SHIFT+F12
^+F12::
    Clipboard = ; Empty the clipboard so that ClipWait has something to detect
    SendInput, ^c ; Copy selected text
    ClipWait
    OutputText := ""
    Loop, parse, Clipboard, `n, `r
    {
        OutputText .= RegExReplace(A_LoopField,"m)^(.*) *$","$1  `r`n")
    }    
    SendRaw % OutputText
return

MY PROBLEM

Between the character ignored when looping, what I am trying to match and what I try to replace the group with, I end up with far more lines and spaces than needed.

CURRENT OUTPUT

----





    # 2020-03-17





        Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.





            Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.





                In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,





DESIRED OUTPUT

----  

# 2020-03-17  

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.  

Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.  

In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,  

1
I'm not seeing the problem, apart from maybe a bit silly approach. Upon testing your code, I seem to end up with what I assumed to be the desired result, two spaces and a CR+LR at the end of each line. Though, I wasn't really able to make sense of your problem description. Could you maybe try to describe the problem better and include an example input paired with a desired output.0x464e
I would be opened to another approach to achieve my goal, my tool of choice being AHK in this case.projetnumero9

1 Answers

1
votes

You're getting too many lines in the output because the send command is messing up due to the carriage returns, which aren't needed in there anyway. I don't really know why that is happening, and I can't be bothered to find out why since the approach isn't good anyway.
And also your indentation is getting messed up because your text editor automatically adds indentation based on the previous line.

But anyway, sending such (long) input is never a good idea.
Make use of the clipboard and just send a ctrl+v to instantly and reliably paste in the text.
Here's an example of that along another way to add the spaces at the end:

inp := "
(
----

# 2020-03-17

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Aenean commodo ligula eget dolor. Aenean massa. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.

Donec quam felis, ultricies nec, pellentesque eu, pretium quis, sem. Nulla consequat massa quis enim. Donec pede justo, fringilla vel, aliquet nec, vulputate eget, arcu.

In enim justo, rhoncus ut, imperdiet a, venenatis vitae, justo. Nullam dictum felis eu pede mollis pretium. Integer tincidunt. Cras dapibus. Vivamus elementum semper nisi. Aenean vulputate eleifend tellus. Aenean leo ligula, porttitor eu, consequat vitae, eleifend ac, enim. Aliquam lorem ante, dapibus in, viverra quis, feugiat a,
)"

Loop, Parse, inp, `n, `r
    OutputText .= (A_LoopField = "" ? "" : RTrim(A_LoopField) "  ") "`n"
Clipboard := OutputText
SendInput, ^v

The ternary A_LoopField = "" ? "" : RTrim(A_LoopField) " " returns true if the line was empty and then the two spaces aren't added at the end.
I think that's the behavior you were doing for.
And RTrim is used to trim any trailing spaces (or tabs) off the end, so we're sure to end up with just the two we want.
And, of course, at the end of any line we add one line feed `n.

Also, your Regex approach was just fine as well at first it just seemed off to me, but well, here's another way. And I guess this would be more efficient, though you'd have to work with seriously large inputs and/or slow hardware for that to make any meaningful difference haha.