Simple AHK script not working - c++

I had read lots of pages on AHK but haven't found any that explains how to make a script that enables me to replace "for" when it's typed by the following:
for(int i=0;i<CONDITION;i++)
{
}
I would like it to set cursor focus on inside the brackets to start writing the loop-code right away.
Here is what I have came up until now:
::for::for(int i=0;i<CONDITION;i++),
{,
,
}
Should replace "for" with the code at top of the post but gets the following error:
Error at line 2.
linetext: ,,
Error: this line does not contain recognised action.
The program will exit.

A hotkey (or hotstring) that executes more than one line must list its first line beneath the hotkey (or hotstring).
https://autohotkey.com/docs/FAQ.htm#autoexec
Comma, semicolon, and other characters such as {}^!+# have special meaning in AHK and need to be escaped in order to be interpreted differently than it normally would.
https://autohotkey.com/docs/commands/_EscapeChar.htm
::for::for(int i=0`;i<CONDITION`;i`{+`}`{+`})`n`{`{`}`n`n`{`}`}{Up}
The easiest way to send such a text is this:
; #If WinActive("ahk_class Notepad")
::for::
ClipSaved := ClipboardAll ; save clipboard
clipboard := "" ; empty clipboard
clipboard = ; send this text to the clipboard:
(
for(int i=0;i<CONDITION;i++)
{
}
)
ClipWait, 1 ; wait for the clipboard to contain data
Send, ^v
Send, {Up}
clipboard := ClipSaved ; restore original clipboard
return
; #If

Also fairly simple is this approach, which works well in Scite and Notepad++ which handles tabbing automatically:
::for::
SendRaw,
(
For(int i=0;i<CONDITION;i++)
{
}
)
Send, {Up}{End}
return

Related

Running regex from windows BATCH

I'm trying to use regex running from a batch
<zed>(.*?)<zed>
to find values I've stored in a file
<process>34593845387<process>
<zed>M567<zed>
<encode>UTF16<encode>
I'm able to do that from java not from batch
You'll probably have to use something like powershell, or another tool. The basics of what you can get in batch won't be enough. Once you do, you'll probably want a regex like:
<zed>([^<]+)<
That way if later the format changes a bit from:
<zed>234<zed>
to
<zed>234< /zed>
or something, it will still work. It's happened to me before :)
Findstr can technically use regex, but it's limited to character sets and can't handle capturing.
If your data looks exactly like that (nothing to the left of <zed>), you can tokenize the string using < and > as delimiters and store the value of <zed> as the second token in the string.
for /F "tokens=1,2 delims=<>" %%A in (data.txt) do if "%%A"=="zed" set zed_value=%%B"
You can then access the variable with %zed_value%. If you have multiple <zed> fields, the variable will contain the value of the last one.
Why not use cscript to access use Javascript regex?
type data.txt | cscript //nologo match.js "<zed>(.*)<zed>"
Where match.js is defined as:
if (WScript.Arguments.Count() !== 1) {
WScript.Echo("Syntax: match.js regex");
WScript.Quit(1);
}
var rx = new RegExp(WScript.Arguments(0), "i");
var matched = false;
while (!WScript.StdIn.AtEndOfStream) {
var str = WScript.StdIn.ReadLine();
if (str.match(rx)) {
WScript.Echo(str);
matched = true;
}
}
if (!matched) {
WScript.Quit(1);
}

Validate input from an inputbox without leaving the inputbox

I created a function with an inputdialog to move lines conditionally (tnx to Romainl).
First thing to do is a search, then to invoke the code below.
My Code:
if !exists("char")
let char = "Move Lines with search match after/before?
\ \n
\ \nMove Line Backwards: Start input with: '?'
\ \nMove Line Forwards: Start input with: '/'
\ \n
\ \np.e.
\ \n?=\\s*$
\"
endif
let a = inputdialog(char)
if a == ""
return
endif
if matchstr(a, '^?') != ''
let minplus = '-'
elseif matchstr(a, '^/') != ''
let minplus = '+'
else
echo "wrong input: input does not start with '?' or '/'"
return
endif
I would like to change the "return" command in a "return back to inputdialog" command:
I would like to check the input entered in the inputbox immediately without leaving the inputbox, is that possible?
The call to inputdialog() is a single, blocking call in Vimscript. None of your code can run while it's open. No events (that can be hooked into with :autocmd are fired. In general, there's no parallelism in Vim.
The best you can do is re-launch the inputdialog() (possibly initialized with the previously entered text) when the validation fails.
Alternatively, you'd have to implement your own input control (e.g. using getchar()). There, you can run validation while waiting for the next pressed character.

Bold text through C++ write statement

I'm working on a dictionary server via telnet, and I'd like it to return it in this format:
**word** (wordType): wordDef wordDef wordDef wordDef
wordDef wordDef wordDef.
Right now I'm outputting the code using:
write( my_socket, ("%s", word.data() ), word.length() ); // Bold this
write( my_socket, ("%s", theRest.data() ), theRest.length() );
So I'd like that first line to be bolded.
Edit
Sorry, I forgot to mention that this is for a command line.
Consider using using something like VT100 escape sequences. Since your server is telnet based the user is likely to have a client that supports various terminal modes.
For instance if you wanted to turn on bold for a VT100 terminal you would output
ESC[1m
where "ESC" is the character value 0x1b. To switch back to normal formatting output
ESC[0m
To use this in your application you can change the example lines from your question to the following.
std::string str = "Hello!"
write( my_socket, "\x1b[1m", 4); // Turn on bold formatting
write( my_socket, str.c_str(), str.size()); // output string
write( my_socket, "\x1b[0m", 4); // Turn all formatting off
There other terminal modes such as VT52, VT220, etc. You might want to look into using ncurses although it might be a bit heavy if all you need is simple bold on/off.

win32 - Second call to EM_SETTEXTEX won't properly show the appended text. Why?

I'm trying to append text to a rich edit control by appending the original string and resending the EM_SETTEXTEX message.
char outputText[4096] = "{\\rtf1\\ansi\\ansicpg0\\deff0{\\colortbl;\\red0\\green0\\blue0;\\red255\\green0\\blue0;\\red50\\green205\\blue50;\\red255\\green140\\blue0;}TEST";
SETTEXTEX s;s.flags = ST_DEFAULT;s.codepage = CP_ACP;
SendMessage(hOutputWndText,EM_SETTEXTMODE,(WPARAM)TM_RICHTEXT,NULL);
SendMessage(hOutputWndText,EM_SETTEXTEX,(WPARAM)&s,(LPARAM)outputText);
I know I do not have a closing bracket on the string but it shows what I want.
TEST
Now I append the string and "re-set" the text inside the rich edit control. Notice, I add a closing bracket just incase.
strcat_s(outputText,"NEWSTUFF}");
SendMessage(hOutputWndText,EM_SETTEXTEX,(WPARAM)&s,(LPARAM)outputText);
And the output this time.
NEWSTUFF}
What gives? I printed the variable outputText to the console and I get the complete string.
{\rtf1\ansi\ansicpg0\deff0{\colortbl;\red0\green0\blue0;\red255\green0\blue0;\red50\green205\blue50;\red255\green140\blue0;}TESTNEWSTUFF}

How to auto-collapse certain comments in Visual Studio 2010?

A colleague of mine uses a abomination text editor that routinely leaves comment blocks all over the code. Needless to say, this is driving me rather mad. The comment blocks look like this:
/* EasyCODE ) */
/* EasyCODE ( 0
WndProc */
/* EasyCODE F */
i.e. they all start with EasyCODE and most of them span several lines. Thankfully, VS2010 can collapse comment blocks, so I don't have to see them all the time.
Is there a way to automate that? A way to automatically collapse all those horrible EasyCODE blocks would be godsent!
Here is a macro that should do it. There are some weirder EasyCode comments that it doesn't catch but it mostly does the trick.
Imports System
Imports EnvDTE
Imports EnvDTE80
Imports EnvDTE90
Imports EnvDTE90a ' remove for VS2008
Imports EnvDTE100 ' remove for VS2008
Imports System.Diagnostics
Imports System.Collections.Generic
Public Module HideEasyCODEComments
''
'' Collapse all EasyCODE comment blocks
''
Sub ToggleSummaryCommentsOutlineExpansion()
If (DTE.ActiveDocument Is Nothing) Then
Exit Sub
End If
If (DTE.UndoContext.IsOpen) Then
DTE.UndoContext.Close()
End If
DTE.SuppressUI = True
Try
DTE.UndoContext.Open("ToggleSummaryCommentsOutline")
Catch
End Try
Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
Dim line As Integer = objSelection.CurrentLine
objSelection.StartOfDocument()
' find all EasyCODE blocks
While objSelection.FindText("^.*\/\* EasyCODE.*((\n.*\*\/)|(\n.*\/\*.*)|(\n\/\/.*))*", vsFindOptions.vsFindOptionsRegularExpression)
DTE.ExecuteCommand("Edit.HideSelection")
End While
objSelection.StartOfDocument()
objSelection.GotoLine(line)
DTE.UndoContext.Close()
DTE.SuppressUI = False
End Sub
End Module
Create a new macro in the macro IDE (Tools->Macros->Macro IDE), paste the above code into it, then assign a keyboard shortcut to it (Tools->Options->Environment->Keyboard, search for it in the listbox). Hit the keyboard shortcut and all EasyCode comments will be gone.
Have fun!
You can't do it automatically. However, you can select a piece of code, and choose from the context menu Outlining/Hide Selection (Ctrl+M Ctrl+H). So select the ugly comments and do it this way.
Taken from here.