Set Carret position in Text Field (Win32API) - c++

I'v created a text box and when it gets focus it sets the caret position to 0 rather than the end. I want to set it to the end.
Thanks
|Text Here -> Text Here|

The caret position is basically the same as the current selection -- when the beginning and end are at different positions, you have some text selected. If the beginning and end of selection are at the same place, that's the caret position.
You can set the selection with EM_SETSEL. To put it at the end, use -1 for both the beginning and end of selection.

Related

How to use Applescript text item delimiters to find and replace text in Pages?

I have multiple Pages documents in which I need to replace special set of characters - in our language we have one-character prepositions (e.g. v, s, k, u, a), that can't be orphaned at the end of lines, so I need to replace the preposition and the next space with preposition and non-breakable space. Have been trying to use AppleScript (am quite newbie to programming) like this one:
set findList to {"v ", "s "}
set replaceList to {"v ", "s "}
set AppleScript's text item delimiters to ""
tell application "Pages"
activate
tell body text of front document
repeat with i from 1 to count of findList
set word of (words where it is (item i of findList)) to (item i of replaceList)
end repeat
end tell
end tell
return
This does not work as long as there are any spaces in the findList and replaceList parameters.
So I found, that text item delimiters might help me. I was able to make this script
set theText to "Some of my text with v in it"
set AppleScript's text item delimiters to "v "
set theTextItems to text items of theText
set AppleScript's text item delimiters to "v " --this is v with non-breakable space (alt+space)
set theText to theTextItems as string
set AppleScript's text item delimiters to {""}
theText
which works, but only with plain text set on the first line of the code (when I copy the result to Pages there is truly a non-breakable space).
But now I need to write a script, that works on the whole text of Pages document.
I have tried something like this:
tell application "Pages"
activate
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "v "
set textItems to body text of front document
set AppleScript's text item delimiters to "v " --again v with non-breakable space (alt+space)
tell textItems to set editedText to beginning & "v " & rest --again v with non-breakable space (alt+space)
set AppleScript's text item delimiters to astid
set text of document 1 to editedText
end tell
but I get the error
Can’t get beginning of "here is the whole text of the Pages document"." number -1728 from insertion point 1 of "and again the whole text of the document"
If I change the script to:
tell application "Pages"
activate
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "v "
set textItems to text items of body text of front document
set AppleScript's text item delimiters to "v "
tell textItems to set editedText to beginning & "v " & rest
set AppleScript's text item delimiters to astid
set text of document 1 to editedText
end tell
I get another error
Pages got an error: Can’t get every text item of body text of document 1." number -1728 from every text item of body text of document 1
Can anyone point me to the right direction how to properly script this?
Thanks.
I hope this will help you. This uses AppleScript's text item delimiters to split/join back texts. It can be more compact, but this is a comprehensive way to write it. As you can use it often in your script, it's a good thing to put it in a special subroutine.
I build a list of pairs {search,replace} easier to maintain in one place, and a "repeat" loop to apply every pair of corrections. Don't forget the "my" statement as Pages doesn't own strRepl() and will fire an error.
Unfortunately, extracting text, and putting it back into Pages will loose any text attributes. So here it is :
set findReplaceList to {{"a", "A"}, {"b ", "B"}, {"this", "that"}}
tell application "Pages"
set bodyText to body text of front document -- get the content as text
repeat with thisFindReplaceValues in findReplaceList
copy thisFindReplaceValues to {findItem, replaceItem} -- put first and second item resp. in findItem and replaceItem
set bodyText to my strRepl(bodyText, findItem, replaceItem) -- search and replace text
end repeat
set body text of front document to bodyText -- put the new text back. Loosing attributes.
end tell
on strRepl(SourceStr, searchString, newString)
set saveDelim to AppleScript's text item delimiters
set AppleScript's text item delimiters to searchString -- change ATID : the search item
set temporaryList to every text item in SourceStr -- split the text in parts removing searched items
set AppleScript's text item delimiters to newString -- New ATID : the replace item
set SourceStr to temporaryList as text -- this put back the parts to text with newString between
set AppleScript's text item delimiters to saveDelim -- clean up ATIDs
return SourceStr
end strRepl
This script is a variation on #Chino22's. Given the consistent requirement here (always a single letter, replaced by itself), I've moved to a simple list of single elements and set the replacement when calling the handler.
-- List of prepositions to seek out (added the 'z' as it was prevalent in the article used for testing)
set chList to {"v", "s", "k", "u", "a", "z"}
tell application "Pages"
set bodyText to body text of front document
repeat with prep in chList
-- call replacement handler
set bodyText to my strRepl(bodyText, space & prep & space, space & prep & character id 160)
end repeat
set body text of front document to bodyText
end tell
on strRepl(srcStr, oldStr, newStr)
set AppleScript's text item delimiters to oldStr
considering case
set temporaryList to every text item in srcStr
end considering
set AppleScript's text item delimiters to newStr
set srcStr to temporaryList as text
return srcStr
end strRepl
NB My search and replace strings include a space both before and after the letter. This ensures that only single-letter words are affected. I added a considering case to further restrict the search to lower case letters. The 'character id 160' specifies the non-breaking space. Finally I left out the first and last delimiter commands to reduce clutter. Add them back at your discretion. A single letter followed by punctuation will not be processed.
Regarding some of the errors you were seeing… They are likely a result of Pages having issues with text item delimiters within its tell block. In general, you would need to split the script into three sections, along these lines:
tell application "Pages" to set bt to body text of front document
myriad delimiters stuff, including 'set editedText to…'
tell application "Pages" to set body text of front document to editedText
Using the handler as Chino22 suggests circumvents this issue by putting all that work within the handler (which is outside the tell block). Also, 'beginning' and 'rest' don't mean what you assume they do in applescript. Finally, I have read of recommendations for working at the paragraph level rather than with the entire body text. It may not be an issue for you but perhaps if you are working with very large documents and have issues, it may be worth making some modifications to the script.

Select a word by cursor and get its position in Qt/C++

I have a small Qt/C++ application with a QTextEdit which uploads some text. I want to be able to select a separate word by cursor and to get its position in the text.
For example, in the following sentence: "It is a sunny day".
If I select the word 'sunny', I will get the int 4, as it is placed at the 4th position in the sentence.
How can I achieve that?
You'll have to :
Get the field's text as QString using QTextEdit::text() method
Use QTextEdit.textCursor()->selectionEnd() to know where selection ends.
Use QString::mid to get the substring from 0 to end of selection
Use QString::count to know how many spaces it contains. This will give you access to the word's position.
Something like:
textEdit.text().mid( 0, textEdit.textCursor()->selectionEnd() ).count( ' ' )+1;
Hope it helps. That's minimal, you'll probably want to deal with partial word selection or any others relevant corner case.

WebStorm skip find occurrence hotkey

I've recently moved from Sublime Text 3 to WebStorm 11 and I am missing a key feature when selecting occurrences of the same selection:
The ability to skip a selection (with a hotkey).
Use case
const item = { itemValue: 1, itemName: 'one' };
console.log(item.itemValue);
Selecting the first item and pressing Ctrl + G (Mac) will start selecting the item occurrences inside the object properties, and I want to select only the item right after the const and inside the console.log. Essentially, I want to skip the two item occurrences inside the object.
This is a dummy use case to illustrate the point, I know I can Refactor > Rename it.
Highlight the text you want to edit:
Ctrl+G will find the next occurrence of the highlighted text and select it:
Cmd+G will drop the current selection and select the next one:
Cmd+G one more time will drop the current selection and select the next one:
For anyone that has a different Keymap:
Cmd+G: "Find Next / Move to Next Occurrence"
Ctrl+G: "Add Selection for Next Occurrence"
Right now, the only solution I found is using Find:
Select the text and press Cmd + F (the find bar will open with the selected text as a search)
Press Cmd + G to select the next occurrence
Press Enter to skip it (will select the next occurrence)
Press Enter again
Press Esc to exit the find context
And now the desired items are selected and ready for editing.
At least it's doable, but I'd expect a hotkey to just skip the current occurrence.

Enumerate existing text in Vim (make numbered list out of existing text)

I have a source document with the following text
Here is a bunch of text
...
Collect underpants
???
Profit!
...
More text
I would like to visually select the middle three lines and insert numbers in front of them:
Here is a bunch of text
...
1. Collect underpants
2. ???
3. Profit!
...
More text
All the solutions I found either put the numbers on their own new lines or prepended the actual line of the file.
How can I prepend a range of numbers to existing lines, starting with 1?
It makes for a good macro.
Add the first number to your line, and put your cursor back at the beginning.
Start a macro with qq (or q<any letter>)
Copy the number with yf<space> (yank find )
Move down a line with j
Paste your yank with P
Move back to the beginning of the line with 0
Increment the number with Ctrl-a
Back to the beginning again with 0 (incrementing positions you at the end of the number)
End the macro by typing q again
Play the macro with #q (or #<the letter you picked>)
Replay the macro as many times as you want with <number>## (## replays the last macro)
Profit!
To summarize the fun way, this GIF image is i1. <Esc>0qqyf jP0^a0q10#q.
To apply enumeration for all lines:
:let i=1 | g/^/s//\=i.'. '/ | let i=i+1
To enumerate only selected lines:
:let i=1 | '<,'>g/^/s//\=i.'. '/ | let i=i+1
Set non recursive mapping with following command and type ,enum in command mode when cursor is inside the lines you are going to enumerate.
:nn ,enum {j<C-v>}kI0. <Esc>vipg<C-a>
TL;DR
You can type :help CTRL-A to see an answer on your question.
{Visual}g CTRL-A Add [count] to the number or alphabetic character in
the highlighted text. If several lines are
highlighted, each one will be incremented by an
additional [count] (so effectively creating a
[count] incrementing sequence).
For Example, if you have this list of numbers:
1.
1.
1.
1.
Move to the second "1." and Visually select three
lines, pressing g CTRL-A results in:
1.
2.
3.
4.
If you have a paragraph (:help paragraph) you can select it (look at :help object-select). Suppose each new line in the paragraph needs to be enumerated.
{ jump to the beginning of current paragraph
j skip blank line, move one line down
<C-v> emulates Ctrl-v, turns on Visual mode
} jump to the end of current paragraph
k skip blank line, move one line up
required region selected, we can make multi row edit:
I go into Insert mode and place cursor in the beginning of each line
0. is added in the beginning of each line
<Esc> to change mode back to Normal
You should get list prepended with zeros. If you already have such, you can omit this part.
vip select inner paragraph (list prepended with "0. ")
g<C-a> does the magic
I have found it easier to enumerate with zeroes instead of omitting first line of the list to enumerate as said in documentation.
Note: personally I have no mappings. It is easier to remember what g <C-a> does and use it directly. Answer above describes usage of pure <C-a> which requires you to manually count whatever, on the other hand g <C-a> can increment numbers with given value (aka step) and have it's "internal counter".
Create a map for #DmitrySandalov solution:
vnoremap <silent> <Leader>n :<C-U>let i=1 \| '<,'>g/^/s//\=i.'. '/ \| let i=i+1 \| nohl<CR>

Add spaces at cursor position

I would like to know if it is possible to add spaces (30 spaces) at cursor position
I tried to do it with regex but I don't know how to represent the actual cursor position in regex.
30iSPACE will add 30 spaces at the cursor position in command mode.
You can use vim register for this:
"a defines register a and if you cut a whitespace with "ax makes register a has whitespace. Then use:
30"ap
Cut a whitespace with x and paste it with 30p
Note: Registers don't forget its value so first solution is more useful.
In addition to already given answers I can say that cursor position is represented in regex with \%#, so s/\%#/\=repeat(" ", 30)/ will add 30 spaces at cursor position just like 30i<Space><Esc>.