AutoHotkey Community

It is currently May 27th, 2012, 12:12 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Indent text anywhere
PostPosted: April 16th, 2011, 3:10 pm 
Offline

Joined: April 8th, 2009, 6:56 pm
Posts: 58
Location: Technology Guru/Tinker'er
I'm always indenting text areas where I believe the information needs to be emphasized… mainly in support forums and on peoples blogs, comments area.

I've got a hotkey setup to push text over 5 chars with a CTRL SHIFT to indent that line…

But what I would really like to do is be able to assign a HOTKEY to allow the selected text [if it's one sentence or a few of them] to be all indented.

I realize I need to limit the width of the text being selected to something like 80 chars [or something], but then I have to chop the end of the sentence close to that length and push it to the next line, where I have to add spaces, measure that line and chop that line; repeating the process until done. Argh! My brain is melting…

My biggest issue; I don't know how to loop through the lines and I'm not understanding how I push the cut portion to the next line, indent and then cut that line; I'm out of my zone on this one.

Has anyone done this? Examples?
Can someone please show me how to do this?

Your time is appreciated…

_________________
Later,
LEHenryJr.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 16th, 2011, 4:34 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
1) Cut selected text
2) http://www.autohotkey.net/~hugov/tf-lib.htm#TF_Wrap
3) http://www.autohotkey.net/~hugov/tf-lib ... sertPrefix
4) Paste

Done :wink: (you can use the clipboard as a variable Clipboard:=TF_Wrap(Clipboard) etc)

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 19th, 2011, 2:18 pm 
Offline

Joined: April 8th, 2009, 6:56 pm
Posts: 58
Location: Technology Guru/Tinker'er
Hugo,
Thanks man… that's a great reference information, but I'm putting efforts in to keeping the whole thing in one file. I see that you can use the file as a great functions reference for other things/functions…

I'm not able to figure out how to do it in one section yet…

I'm going hack with it; see if I can make it work and get back to you guys..
I'm much obliged...

_________________
Later,
LEHenryJr.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 20th, 2011, 4:50 pm 
Offline

Joined: April 8th, 2009, 6:56 pm
Posts: 58
Location: Technology Guru/Tinker'er
Hugo,
You're awesome man, but I just can't figure out the processing on what I need to do to get it in to a single file… thanks anyway.

I did another search on the forum for 'wordwrap' and found this… it was, or seemed much shorter in coding…

Code:
; credit: http://www.autohotkey.com/forum/viewtopic.php?t=40299&highlight=wordwrap
wraplength=60
textToWrap =
(
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.

Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
)

wrap(String, maxWrapLength, separator = "", showSeparator = "")
{
   prefix=>   >
   endChar := "`n" . prefix
   str1 := ""
   
   StringReplace String1, String, `r`n, %A_Space%, All
   StringReplace String1, String1, `n, %A_Space%, All

   If (separator != "")
      StringReplace, String1, String1, %separator%, %endChar%%separator%, All
   Loop Parse, String1, `n
   {
      str := prefix
      length := 0
      line := A_loopfield
    
     Loop Parse, line, %A_Space%
      {
         lengthLoopfield := StrLen(A_loopfield)
         length := length + lengthLoopfield +1
         If (length > maxWrapLength)
         {
            str := str . endChar . A_loopfield . A_Space
            length := lengthLoopfield + 1
         }
         Else
         {
            str := str . A_loopfield . A_Space
         }
      }
      If (str1 = "")
         str1 := str
      Else
         str1 := str1 .  endChar .  str
   }
   If (showSeparator = "")
      StringReplace str1, str1, %separator%, , All
   Return str1
}

MsgBox % wrap(textToWrap, wraplength, "", false)
clipboard= % wrap(textToWrap, wraplength, "", false)


1. It's not keeping the [enter][cr] from the TEXTTOWRAP, so the formatting is jacked up. I'd like to have the function, but I'd speculate I could live without it… :(

2. For the PREFIX, I have tried {space 5}, `, and other variations, but I'm not able to get the five spaces to indent the text.
Any ideas? [confused]


I also found this:
Code:
;credit
; http://www.autohotkey.com/forum/viewtopic.php?t=18307&highlight=wordwrap


wraplength=60
textToWrap =
(
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.

Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
)

WrappedMessage := RegExReplace(texttowrap,"(.{50}\s)","$1`n     ")

MsgBox, Original:`n%texttowrap%
MsgBox, %WrappedMessage%
ExitApp


This seems MUCH simpler, it keeps the [CR] for the text, but it's not indenting the text on every line…like the first two lines of each paragraph…

Any suggestions?

Which one is easier to work with? Which is the better option?

_________________
Later,
LEHenryJr.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 20th, 2011, 8:39 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
This is short and simple
Code:
Clipboard:=TF_Wrap(Clipboard,60)
Clipboard:=TF_InsertPrefix(Clipboard,1,0,"     ")
MsgBox % Clipboard
I don't understand why you wouldn't want to use a library, that is what they are for, they are just as portable as a regular script if you use #include. If you really want to you can simply take the code from both TF functions and use those in one function or label.

Code:
Text=
(join`r`n
Where does it come from?
Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source.

Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
)

MsgBox % WrapLeading(Text)

WrapLeading(Text, Length = 60, Leading = 5, Char = " ")
   {
    break=[ \r?\n]
    Loop % Leading
       InsertText .= Char
    Loop, Parse, Text, `n, `r
       {
       If (StrLen(A_LoopField) > Length)
         {
          LoopField := A_LoopField " " ; just seems to work better by adding a space
          OutPut .= RegExReplace(LoopField, "(.{1," . Length . "})" . Break , "$1`n")
         }
       Else
         OutPut .= A_LoopField "`n"
      }      
    StringTrimRight, OutPut, OutPut, 1
    Loop, Parse, OutPut, `n, `r
      {
       FinalOutPut .= InsertText A_LoopField "`n"
      }
    StringTrimRight, FinalOutPut, FinalOutPut, 1
    Return FinalOutPut
   }

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 21st, 2011, 12:42 am 
Offline

Joined: April 8th, 2009, 6:56 pm
Posts: 58
Location: Technology Guru/Tinker'er
Hugo, Thanks again…
I did; I tried to pull out all the functions from the TF.AHK, but I didn't know what was out of whack for me… and to be perfectly honest, your code is far exceeds me; I was just having a hard time finding out what I was doing wrong. That's when I went searching for other examples/code…

Mainly, I have one file [AHK] that does everything; I've been putting efforts in to keeping it that way because it's the main one I use. But I do use the other one for bitly.AHK— that works great and it's in a separate AHK file completely.

I'd speculate, for now, I'd like to keep everything in one file so I can compile it. I haven't been able to compile my file if they link to something else external…

I made my final changes to it:
Code:
;cut selected text and indent/ wrapping at 60 chars.
#!i::
send, ^x
clipwait, 1

Text=
(join`r`n
%clipboard%
)

;send % WrapLeading(Text)
clipboard=% WrapLeading(Text)
send, ^v
return

WrapLeading(Text, Length = 60, Leading = 5, Char = " ")
   {
    break=[ \r?\n]
    Loop % Leading
       InsertText .= Char
    Loop, Parse, Text, `n, `r
       {
       If (StrLen(A_LoopField) > Length)
         {
          LoopField := A_LoopField " " ; just seems to work better by adding a space
          OutPut .= RegExReplace(LoopField, "(.{1," . Length . "})" . Break , "$1`n")
         }
       Else
         OutPut .= A_LoopField "`n"
      }     
    StringTrimRight, OutPut, OutPut, 1
    Loop, Parse, OutPut, `n, `r
      {
       FinalOutPut .= InsertText A_LoopField "`n"
      }
    StringTrimRight, FinalOutPut, FinalOutPut, 1
    Return FinalOutPut
   }


So I can do a WINDOWS + I and then indent the text/ wrapping at 60 chars.
Works perfect! :)

Thanks for your help...
I hope you have a nice day!

_________________
Later,
LEHenryJr.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 21st, 2011, 8:47 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
lehenryjr wrote:
I haven't been able to compile my file if they link to something else external…
That's peculiar, because if you compile the compiler makes sure everything that is needed (#includes, LIB) is included, external files such as icons and other non AutoHotkey resources (DLLs, images) will not be included unless you use some 'tricks' (search the forum on how to include an icon IN the script for example). Anyway, glad it works.

_________________
AHK FAQ
TF : Text files & strings lib, TF Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 26th, 2011, 2:50 am 
Offline

Joined: March 24th, 2004, 2:34 pm
Posts: 299
I'm not sure if this will help you, but I use the following to indent blocks of text (or code) a variable amount. I use this very often (several times per day) to move blocks of text a number of spaces to the right or left.

You might like to skip to the end of this post and try the "summarized" example I have written and included near the bottom of this post. It may be the easiest way for you to understand what this code does and you might like to try it before reading any further.

In the following code, press ALT+SHIFT+I to invoke my "Indent" hotkey.
If you wish to indent from one through nine spaces, follow the ALT+SHIFT+I with a single digit from 1 to 9 followed by the ENTER, SPACE or ESC key.
Alternatively, if you wish to indent more than 9 spaces, enter a number from 10 thru 99 without the need to follow it by anything.

After you do this, the semicolon will take on a different role.

Everytime you press the semicolon, it will execute the subroutine Lab_Semi_Indent_Fun() and that will cause the text to the right of the cursor to be indented a number corresponding to the number you entered and the cursor will then drop down to the next line in the position ready to indent that line.

So, if you wish to indent six rows, you can just press the semicolon six times. Alternatively, you can use the "typematic" effect of the keyboard and press and hold the semicolon.

When you are finished using the semicolon in this way, you can press CTL-F12 to reload your script and the semicolon will revert to normal.

I hope this might come close to what you want to do.

SUMMARIZED EXAMPLE:

This is the example I recommend you try in order to see first hand what this function does and how it works. This example moves text in your current document on the current line. It moves the text seven spaces to the right on ten successive lines:

1) move your cursor to the position from which you wish to begin this move.
2) press ALT+SHIFT+I followed by 7 followed by ENTER, SPACE or ESC.
3) press the semicolon ten times as quickly as you like.
4) press CTL+F12 to reload your script file and cause the semicolon to revert back to its previous state.




I use the following:

Code:
 
^F12::reload                                                                                     
!+i::                                                                                             ; I-indent
  Lab_Semi_Indent_Fun()
  RETURN
Lab_Semi_Indent_Fun(){                                                                             ; set dynamic indent hotkey
  Global Len
  Input Len, L2,{ENTER}{ESC}{TAB}{SPACE}                                                           ; number of chars to indent
  Hotkey `;,Lab_Semi_Indent_Sub,ON                                                                 ; dynamically create hotkey
}
Lab_Semi_Indent_Sub:
  Send {SPACE %Len%}{LEFT %Len%}{DOWN}                                                             ; indent
  RETURN



Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google Feedfetcher and 15 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group