How to replace a single literal accent character?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

How to replace a single literal accent character?

18 Dec 2018, 10:16

How to replace a single literal accent character?

Code: Select all

; A text string defined as
Text  =  LF = `r`n   |   literal ; = `;
; or the same text string received from an IniFile containing e.g.
; [Settings]
; Info =LF = `r`n   |   literal ; = `;
; by IniRead, Text, %IniFile%, Settings, Info
; should be displayed e.g. in a MsgBox LITERALLY as
; LF = `r`n   |   literal ; = `; 

; This doesn't work for `r`n:
Text := RegExReplace(Text, "``", "````")
MsgBox % Text

; And ; must be replaced too. The correct string that would be displayed correctly would be:
TextC = LF = ``r``n   |   literal `; = ```;
MsgBox % TextC

; But how to do these replacements???
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: How to replace a single literal accent character?

18 Dec 2018, 10:47

Once you write `r`n it gets automatically understood as Carriage Return and Line Feed.

You'll need to write ``r``n or replace "`r`n" with "``r``n" later on.

Not just ` because it isn't there anymore.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

18 Dec 2018, 11:04

Thank you, safetycar!

Yes, I know that rewritting the initial Text string would solve all problems.

But my question refers explicitely to the case that there is such a string defined e.g. in an IniFile (e.g. by a user of my app). I can't belive that there shouldn't be a way to programmatically rewrite such a string...

But I see now that my question in the title was not correct. `r`n can be replaced as required. The real problem seems to be the semicolon ;.
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: How to replace a single literal accent character?

18 Dec 2018, 12:55

I'm not sure what you mean. You only need to deal with escaping ` when you're writing code. When the text comes from other place you don't need to deal with ahk's source way of escaping.

Unless your stored text wants to use by own choice the same escaping as ahk uses. Then you could want to StrReplace(Txt, "``n", "`n") ----> "`n" to LineFeedCharacter (Chr(10)).

But I see strange that a text coming from outside ahk would be needing to escape a semi-colon. But you can do the same as above with each of your special meaning characters.

If that's not what you mean you'll need to explain more detailed examples of what you're trying because that's all I could understand.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

18 Dec 2018, 16:45

Thank you, safetycar, for your interest.

After some further test and recherches I think that my original questions and examples were not correct and useful. (Typical of a newbie...). I do here a new attempt with a new approach.

In the initial part of scripts an information text about the purpose and use of the program is often inserted. The following is a special example:

Code: Select all

; =============================
; MyCrazyApp.ahk
; =============================
; USAGE & NOTES & HELP:
; This ...
; Please note that the following characters and strings:
;    `r`n   (LF)
;    `      (accent grave / backtick)
;    ;      (semicolon)
;    &      (ampersand) 
; must be ...
; =============================
My idea was: It would be practical if the same text could be used at the same time as help text for the user (quasi in double function). The goal would be that the same text should be visible in the script as well as e.g. in a MsgBox as identical plain text.

My first attempt to implement this concept was to use a multiline text variable:

Code: Select all

; =============================
; MyCrazyApp.ahk
; =============================

Help =
(
=============================
MyCrazyApp.ahk
=============================
USAGE & NOTES & HELP:
This ...
Please note that the following characters and strings:
    `r`n   (LF)
    `      (accent grave / backtick)
    ;      (semicolon)
    &      (ampersand) 
must be ...
=============================
)

; ...

Gui, Add, Button, x50 y40 w100 ggHelp, Help
Gui, Show, w200 h100


RETURN

; ...

gHelp:          ; "Plain text to plain text" – HOW TO DO THIS??? 
  Help := RegExReplace(Help, "`r`n", "``r``n")  ; This works for `r`n
 ;Help := RegExReplace(Help, ...                ; ??? for `
  MsgBox % Help
Return
In the above example `r`n can be replaced to receive the same plain text in the MsgBox. Could it really be that there is no solution to implement this concept for `?

But there would also be another question immediately: Is there a solution for % included in the multiline text variable???

If all this fails, is there another method (instead of the multiline text variable) to implement this concept???
safetycar
Posts: 435
Joined: 12 Aug 2017, 04:27

Re: How to replace a single literal accent character?

18 Dec 2018, 18:22

I think some of those are working by luck and for the accent there's no trace of it on the final string so you can restore it.

Think about doing something along this lines:

Code: Select all

; =============
; Documentation
; =============
FileRead, Script, %A_ScriptFullPath%
MsgBox % Script
You can do the reads line by line or trim the string but that's the general idea.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

18 Dec 2018, 18:49

safetycar wrote:
18 Dec 2018, 18:22
FileRead, Script, %A_ScriptFullPath%
Which, however, is not working with compiled scripts.

So there is no way...
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

19 Dec 2018, 04:06

But now...maybe a surprise!!! Or even a miracle???

Code: Select all

; 👻 Copy this script to your computer, save it as a UTF-8 or Unicode file and run it!

Text  =  
(
| LF = `r`n | backtick = ​` | backtick and semicolon = ​`; | backtick between two characters = un​`easy |
)


Gui, Show, Hide w300 h300

Text := RegExReplace(Text, "`r", "``r")
Text := RegExReplace(Text, "`n", "``n")
Text := RegExReplace(Text, "​`", "``")

MsgBox %Text%`r`n`r`nIs the visible text dispayed here identical to the text visible in the script?`r`n`r`nIf so, how is this miracle possible??????????????

Return
Last edited by newbieforever on 19 Dec 2018, 07:37, edited 1 time in total.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

19 Dec 2018, 05:53

Or simply by ONE SINGLE REPLACEMENT!!!

Code: Select all

; 👻 Copy this script to your computer, save it as a UTF-8 or Unicode file and run it!

Text  =  | LF = `​r`​n | backtick = `​ | backtick and semicolon = `​; | backtick between two characters = un`​easy |

Gui, Show, Hide

Text := RegExReplace(Text, "`​", "``")

MsgBox %Text%`r`n`r`nIs the visible text dispayed here identical to the text visible in the script?`r`n`r`nIf so, how is this miracle possible??????????????

Return

; THE TRICK IS: Insert in text every ` with a subsequent ZeroWithSpace (a Unicode character), and do the replacement for ` followed by ZeroWithSpace!

:*:*bt::
  Send ``{Space}{U+200B}    ; Results in ` followed by a ZWS!
Return
Last edited by newbieforever on 20 Dec 2018, 03:16, edited 1 time in total.
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

19 Dec 2018, 11:29

To make the miracle perfect: a solution without any replacement!!!

To remember the original idea:

In the initial part of scripts an information text about the purpose and use of the program is often inserted. My idea is: It would be practical if the same script info text could be used at the same time as help text for the user (quasi in double function). The goal would be that the same text should be visible in the script as well as e.g. in a MsgBox as identical plain text.

If only there weren't those "headache characters"...

But, there is a workaround solution:

Code: Select all

; 👻 Copy this script to your computer, save it as a UTF-8 (or Unicode) file and run it!
; Is the visible text dispayed in the MsgBox identical to the text visible in the script?
; If so, how is this miracle possible??????????????

Help  = 
(
This could be a larger carefully composed Info & Help Text at the beginning of a script.

All these problematic characters, even the "headache characters" ˋ and %, can be included in such an Info & Help text:

- Backtick (accent grave):
          ˋ    | Examples:  ˋrˋn  ˋ;  unˋeasy 
- Percent:
          %   | Examples:  %Size%  100%
- Other characters:
          \ . * ? + [ { | ( ) } ] ^ $ < > & # ' ´ ~ / §

But this same text can be made available in a very simple way to the user as Info & Help text which is displayed e.g. upon applying a Help key (here F8)!

Compare the text displayed in the MsgBox to the visible plain text included in the script!
)

Gui, Show, w100 h100 Hide

GoSub Show

RETURN

Show:
F8::
  MsgBox %Help%
Return

; THE TRICK IS: Instead of normal ` and % other (unicode) characters that look almost exactly like the normal should be used:

:*:*bb::
  Send {U+02CB}	        ; 'MODIFIER LETTER GRAVE ACCENT'
Return

:*:*pp::
  Send {U+FF05}         ; 'FULLWIDTH PERCENT SIGN'
Return
Last edited by newbieforever on 20 Dec 2018, 12:25, edited 3 times in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to replace a single literal accent character?

19 Dec 2018, 22:47

I wasn't sure what you wanted. Have a look for 'verbatim', here. Cheers.
jeeswg's characters tutorial - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=7&t=26486
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

20 Dec 2018, 05:41

Thanks for your advice––valuable as always from jeeswg!

But, to summarize my idea and the found scripting-trick solution once again in conclusion and summary, for the case somebody finds them useful:

Code: Select all

; 👻 A SCRIPTING TRICK useful for a special purpose...

Help  =         ; !!! NOTE: Replacement characters for `and % are used here: ˋ {U+02CB} and % {U+FF05} !!!
(
This could be a larger carefully composed Info & Help Text at the beginning of a script, intended for all *readers* of the script.

All these problematic characters, even the "headache characters" ˋ and %, can be included in such an Info & Help text:

- Backtick (accent grave):
          ˋ    | Examples:  ˋrˋn  ˋ;  unˋeasy 
- Percent:
          %   | Examples:  %Size%  100%
- Other characters:
          \ . * ? + [ { | ( ) } ] ^ $ < > & # ' ´ ~ / §

But this same text can be made available in a very simple way to the *users* of the (possibly compiled) script as Info & Help text, i.e. the text which is displayed e.g. upon applying a Help key (here F8)!

Compare the text displayed in the MasgBox to the visible plain text included in the script!
)

Gui, Show, w100 h100 Hide

GoSub Show

RETURN

Show:
F8::                ; The Help key
  MsgBox %Help%
Return

; THE TRICK IS:
; =============
; Instead of normal ` and % other (unicode) characters that look almost exactly like the normal should be used:

:*:*bb::
  Send {U+02CB}	        ; 'MODIFIER LETTER GRAVE ACCENT'
Return

:*:*pp::
  Send {U+FF05}         ; 'FULLWIDTH PERCENT SIGN'
Return

; ANOTHER SIMILAR TRICK (but not including %!!!) WOULD BE:
; ========================================================
; Insert in text every ` with a subsequent ZeroWithSpace (a unicode character), and do the replacement for ` followed by ZeroWithSpace!

:*:*bt::
  Send ``{Space}{U+200B}    ; Results in ` followed by a 'ZERO WIDTH SPACE'
Return

; Example:
; Help  = Abra`​Cadabra LF = `​r`​n etc.         ; ` followed by a ZWS!
; Help := RegExReplace(Help, "`​", "``")       ; When you see that, you can't believe this crazy command could work.
                                              ; But: In "`" there is a ZWS after `!!!
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to replace a single literal accent character?

20 Dec 2018, 06:27

- Thanks for the compliment.
- I've thought about this issue a lot.
- I've generally decided against using invisible, lookalike or non-lookalike replacement characters.
- Generally I would make every character WYSIWYG, with the possible exceptions of ` and %.
- You can define pc := "%", and then use %pc% to use literal percent signs.
- You can make would-be variables verbatim text e.g. %var%, and manually use StrReplace to replace the text. This is more forwards compatible.
- You can also put things in curly braces {}, and then put the text through the Format function. Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
newbieforever
Posts: 493
Joined: 24 Aug 2016, 03:34

Re: How to replace a single literal accent character?

20 Dec 2018, 07:39

jeeswg wrote:
20 Dec 2018, 06:27
- I've generally decided against using invisible, lookalike or non-lookalike replacement characters.
- Generally I would make every character WYSIWYG ...
I understand. But, if I correctly see your answer, this doesn't refer exactly to my specific idea/concept, which is intended to be used only for the purpose described:

The info text included at the begining of the script (intended for a reader of the script) should be a normally readable plain text (like a book text), and it should at the same time be made available as a normally readable help text for the user. So if the script info text contains ` and %, these characters shall be represented visually as such, both in the script info text and in a help msgbox. Let's call this main feature of the concept the "WYS equivalence". That's all I'm referring to here. My approach was intended to result in a small scripting wizzard converting the script info to user help text (hence my incorrect question in the title of this topic), but at the end it is only a scripting trick.

As far as I can see, no other solutions have been described here. Or were they?
Last edited by newbieforever on 20 Dec 2018, 14:37, edited 3 times in total.
garry
Posts: 3770
Joined: 22 Dec 2013, 12:50

Re: How to replace a single literal accent character?

20 Dec 2018, 07:50

I tried to show the characters in EDIT & MsgBox
EDIT : added MsgBox

Code: Select all

Gui, Font,s12 cBlack ,Lucida Console 
Help=
(Ltrim Join`r`n  % `
This could be a larger carefully composed Info & Help Text at the beginning of a script, intended for all *readers* of the script.
All these problematic characters, even the  "headache characters"  ˋ and %, can be included in such an Info & Help text:
- Backtick (accent grave):
          ˋ    | Examples:  ˋrˋn  ˋ;  unˋeasy 
- Percent:
          %   | Examples:  %Size%  100%
%a_scriptdir%\test.txt
- Other characters:
          \ . * ? + [ { | ( ) } ] ^ $ < > & # ' ´ ~ / §
But this same text can be made available in a very simple way to the *users* of the (possibly compiled) script as Info & Help text, i.e. the text which is displayed e.g. upon applying a Help key (here F8)!
Compare the text displayed in the MsgBox to the visible plain text included in the script!
)
Gui,add,edit,x10 y10 w1000 h300 vED1,%help%
gui,add,text,x0 y0 h0 w0 vT1
Gui,add,button,x10 y330 w120 h35 gA,Msgbox
GuiControl, Focus,T1
Gui, Show,x10 y10 w1050 h380,TEST
RETURN
Guiclose:
exitapp
a:
Gui,submit,nohide
msgbox,%help%
return

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: ccqcl, DataLife, Google [Bot], Rohwedder and 141 guests