SciTE4AutoHotkey extension - Count occurrences of word

Post your working scripts, libraries and tools for AHK v1.1 and older
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

24 Feb 2015, 09:34

boiler wrote:haichen: Here's one last mod I made to your Lua script that makes CountMarkedWords perfect for me now. Maybe it's more pronounced for me because my editor color scheme is a dark background, but I only want the highlight color for the matches it finds to be on the other matches, not superimposed over the original selection because then you get a combination of your selection highlighting and this match highlighting. Plus, it looked a little strange when selecting multiple lines of text and the linefeed character is highlighted differently from the rest of the text. So I added this if statement around one line in your while loop:

Code: Select all

        if e < editor.CurrentPos or s > editor.CurrentPos then
            scite.SendEditor(SCI_INDICATORFILLRANGE, s, e - s)
        end
Now I have separate control over highlighting of selected text and matched text. In your code, I changed the highlight color and added a line for controlling the alpha channel (transparency) that I like on my dark background.

Code: Select all

    scite.SendEditor(SCI_INDICSETFORE, 0, 0xCCFFFF)
    scite.SendEditor(SCI_INDICSETALPHA, 0, 100)
Thanks again for the great feature.
Boiler, please could you tell me the complete instructions to implement it in my computer? Thanks in advance.
Btw, would it possible to jump through the matched text? Thanks!
Everything is possible!
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

24 Feb 2015, 10:44

empardopo wrote:Boiler, please could you tell me the complete instructions to implement it in my computer?
Do you mean the whole thing, or you just this modification to it? I would follow haichen's instructions for implementing it in general, then I can describe how to modify it for this.
empardopo wrote:Btw, would it possible to jump through the matched text? Thanks!
Yes. Once you highlight the text and it shows the matches, press Control-F and then the "Find Next" button. It will jump through each of the instances of the matched text as you keep pressing "Find Next".
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

24 Feb 2015, 21:02

Another small fix to the count occurrences capability. When selecting text across the end of a line, the text in the status bar isn't displayed correctly because of the CR + LF. This change replaces those characters with "<<" in the status bar so it displays on one line without over-writing itself (doesn't affect the actual text in the editor, of course).

Add the two lines starting with txt = shown below near the end of the mark occurrences function:

Code: Select all

    props['countedOccurrences'] = count
    txt = string.gsub(txt, "\n", "<<")
    txt = string.gsub(txt, "\r", "")
    props['markedWord'] = txt
	if (editor.SelectionEnd - editor.SelectionStart) > 40 then
		props['markedWord'] = string.sub(txt,1,40) .. '...'
	end
haichen at work

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

25 Feb 2015, 03:08

Hallo boiler,
I like changing Color; for the lineendings i prefer CR LF.
What do you think about putting these properties in a propertie file?
I have no time at the moment..
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

25 Feb 2015, 04:02

boiler wrote:
empardopo wrote:Boiler, please could you tell me the complete instructions to implement it in my computer?
Do you mean the whole thing, or you just this modification to it? I would follow haichen's instructions for implementing it in general, then I can describe how to modify it for this.
empardopo wrote:Btw, would it possible to jump through the matched text? Thanks!
Yes. Once you highlight the text and it shows the matches, press Control-F and then the "Find Next" button. It will jump through each of the instances of the matched text as you keep pressing "Find Next".
From the beginning it would be ideal! I'm lost with all mixed posts!
Thanks very much.
Everything is possible!
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

25 Feb 2015, 13:49

haichen at work wrote:I like changing Color; for the lineendings i prefer CR LF.
It turned out that my change for the CR/LF didn't always work, so I removed it. The funny thing is that it doesn't always evidence the problem without it, so I'm not sure what's going on with that. Sometimes a CR/LF in the selected text will cause the text in the status bar to overwrite itself and sometimes it won't.
haichen at work wrote:What do you think about putting these properties in a propertie file?
I have no time at the moment..
I would but I'm not sure what you mean. Are you saying to remove the property definitions from the Lua script and put them in ScitTEUser.properties or someplace like that instead? Is the reason so that it's in a more intuitive place for a user to go and change those settings? Perhaps they belong in a style properties file if you haven an edited one like I do since you might want the colors to be different depending on which style and its associated color scheme you're using.

Edit: Or perhaps you were referring to the extension.properties file That may be the best place for it.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

25 Feb 2015, 15:01

empardopo wrote:From the beginning it would be ideal! I'm lost with all mixed posts!
Well, haichen created an extension file that you can import, but I'm not sure what he has included as updates at this point. I would have to uninstall mine and install his in order to tell. But you can try his and see what it includes.

First step is to download his file: com.haichen.CountMarkedWords.s4x

Then, right click on the SciTE4AutoHotkey toolbar to see the context menu, then select "Extensions" then "Extensions Manager".

From the Extension Manager, choose "Install extension", then select the file you just downloaded. Close and save that.

Then go to the Options menu and choose "Open User properties". In the SciTEUser.properties file that will open up, add the following the code below to the bottom. (this my version where I replaced the file attribute with selection length, which I find more useful, and I have a reminder that it's case insensitive)

Code: Select all

# Show number of occurrences of highlighted word
statusbar.text.1=\
Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | Selection: $(SelLength) chars |  $(countedOccurrences) occurrences of "$(markedWord)" found (case insensitive)
Then save it and restart SciTE4AutoHotkey.
Guest

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

26 Feb 2015, 03:05

boiler wrote:Edit: Or perhaps you were referring to the extension.properties file That may be the best place for it.
Yes that was the idea.

I can upload a new extensionfile after work.
Meanwhile i've put the color,alpha and lineending in the extension.properties file:

The extension.properties file (same dir as extension.lua):

Code: Select all

# Write properties specific to this extension here
ForegroundColor=0xff0000
Alpha=100
LineEndings=CRLF
extension.lua:

Code: Select all

-- Lua script specific to this extension
-- Declare event handler table

-- http://lua-users.org/wiki/SciteMarkWord
function clearOccurrences()
    scite.SendEditor(SCI_SETINDICATORCURRENT, 0)
    scite.SendEditor(SCI_INDICATORCLEARRANGE, 0, editor.Length)
end


function markOccurrences()
	local SelectColor = props["ForegroundColor"] or "0x0000FF"
	local Alpha = props["Alpha"] or "100"
	local LineEndings = props["LineEndings"] or "CRLF"
	
	if Alpha=="" then Alpha=25  end
	if SelectColor=="" then SelectColor=0xFF00  end
	if LineEndings=="" then LineEndings="<<<<"  end
	
    if editor.SelectionStart == editor.SelectionEnd then
		clearOccurrences()
		props['countedOccurrences'] = 'No'
        props['markedWord'] = ''
        return
    end
    clearOccurrences()
    scite.SendEditor(SCI_INDICSETSTYLE, 0, INDIC_ROUNDBOX)
    scite.SendEditor(SCI_INDICSETFORE, 0, SelectColor)
    scite.SendEditor(SCI_INDICSETALPHA, 0, Alpha)
    
    local txt = editor:GetSelText()
    local count = 0
    local dirty = 100000
    local flags = 0 --SCFIND_MATCHCASE
    local s,e = editor:findtext(txt,flags,0)
	while e do
	    dirty=dirty-1
	    if dirty<1 then break end
		if e < editor.CurrentPos or s > editor.CurrentPos then
            scite.SendEditor(SCI_INDICATORFILLRANGE, s, e - s)
        end
		s,e = editor:findtext(txt,flags,e+1)
		count = count +1
	end

    props['countedOccurrences'] = count
    txt = string.gsub(txt, "\n", string.sub(LineEndings,3,4))
    txt = string.gsub(txt, "\r", string.sub(LineEndings,1,2))
    props['markedWord'] = txt
    if (editor.SelectionEnd - editor.SelectionStart) > 40 then
		props['markedWord'] = string.sub(txt,1,40) .. '...'
    end
    return 
end

-- Add user event handler OnDoubleClick
local events = {
    OnUpdateUI = function(shift,ctrl,alt)
    if markOccurrences() then     return true     end
    return false
    end
}
RegisterEvents(events)
Hopefully this is correctly done.

The code in SciTEUser.properties has not changed.

Code: Select all

# Show number of occurrences of highlighted word
statusbar.text.1=\
Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | Selection: $(SelLength) chars |  $(countedOccurrences) occurrences of "$(markedWord)" found (case insensitive)
The colors and the lineending sign could now be changed in the extension.properties file
haichen
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

26 Feb 2015, 06:11

Guest wrote:
boiler wrote:Edit: Or perhaps you were referring to the extension.properties file That may be the best place for it.
Yes that was the idea.

I can upload a new extensionfile after work.
Meanwhile i've put the color,alpha and lineending in the extension.properties file:

The extension.properties file (same dir as extension.lua):

Code: Select all

# Write properties specific to this extension here
ForegroundColor=0xff0000
Alpha=100
LineEndings=CRLF
extension.lua:

Code: Select all

-- Lua script specific to this extension
-- Declare event handler table

-- http://lua-users.org/wiki/SciteMarkWord
function clearOccurrences()
    scite.SendEditor(SCI_SETINDICATORCURRENT, 0)
    scite.SendEditor(SCI_INDICATORCLEARRANGE, 0, editor.Length)
end


function markOccurrences()
	local SelectColor = props["ForegroundColor"] or "0x0000FF"
	local Alpha = props["Alpha"] or "100"
	local LineEndings = props["LineEndings"] or "CRLF"
	
	if Alpha=="" then Alpha=25  end
	if SelectColor=="" then SelectColor=0xFF00  end
	if LineEndings=="" then LineEndings="<<<<"  end
	
    if editor.SelectionStart == editor.SelectionEnd then
		clearOccurrences()
		props['countedOccurrences'] = 'No'
        props['markedWord'] = ''
        return
    end
    clearOccurrences()
    scite.SendEditor(SCI_INDICSETSTYLE, 0, INDIC_ROUNDBOX)
    scite.SendEditor(SCI_INDICSETFORE, 0, SelectColor)
    scite.SendEditor(SCI_INDICSETALPHA, 0, Alpha)
    
    local txt = editor:GetSelText()
    local count = 0
    local dirty = 100000
    local flags = 0 --SCFIND_MATCHCASE
    local s,e = editor:findtext(txt,flags,0)
	while e do
	    dirty=dirty-1
	    if dirty<1 then break end
		if e < editor.CurrentPos or s > editor.CurrentPos then
            scite.SendEditor(SCI_INDICATORFILLRANGE, s, e - s)
        end
		s,e = editor:findtext(txt,flags,e+1)
		count = count +1
	end

    props['countedOccurrences'] = count
    txt = string.gsub(txt, "\n", string.sub(LineEndings,3,4))
    txt = string.gsub(txt, "\r", string.sub(LineEndings,1,2))
    props['markedWord'] = txt
    if (editor.SelectionEnd - editor.SelectionStart) > 40 then
		props['markedWord'] = string.sub(txt,1,40) .. '...'
    end
    return 
end

-- Add user event handler OnDoubleClick
local events = {
    OnUpdateUI = function(shift,ctrl,alt)
    if markOccurrences() then     return true     end
    return false
    end
}
RegisterEvents(events)
Hopefully this is correctly done.

The code in SciTEUser.properties has not changed.

Code: Select all

# Show number of occurrences of highlighted word
statusbar.text.1=\
Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | Selection: $(SelLength) chars |  $(countedOccurrences) occurrences of "$(markedWord)" found (case insensitive)
The colors and the lineending sign could now be changed in the extension.properties file
haichen
Chanes made in extension properties. My extension properties file is in C:\Users\empardopo\Documents\AutoHotkey\SciTE\Extensions\com.yourname.extname.
I don't find any file called extension.lua... Have I make one?
Changes made in SciTEUser.properties.

If I change color in extension.properties file I don't get any changes.

Any idea?

Btw, I get this... I can't see the "Selection: chars" words!!
Attachments
Selection.png
Selection.png (2.5 KiB) Viewed 6434 times
Everything is possible!
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

26 Feb 2015, 07:08

empardopo wrote:I don't find any file called extension.lua... Have I make one?
You should just be able to create a new file with that name with an editor and put it in the same directory. But if you don't have a Lua file, how are you getting the occurrence count to show up in your status bar like your picture shows? You must have the Lua code somewhere.
empardopo wrote:Changes made in SciTEUser.properties. If I change color in extension.properties file I don't get any changes. Any idea?
The extension.properties file needs to be in the same directory as the Lua file. And you say there is no Lua file there. It sounds like yours may not be set up as an extension.
empardopo wrote:Btw, I get this... I can't see the "Selection: chars" words!!
Did you really update your SciTEUser.properties file to match the latest? It looks like it still has $(FileAttr) in it instead of Selection: $(SelLength) chars.
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

26 Feb 2015, 07:37

boiler wrote:
empardopo wrote:I don't find any file called extension.lua... Have I make one?
You should just be able to create a new file with that name with an editor and put it in the same directory. But if you don't have a Lua file, how are you getting the occurrence count to show up in your status bar like your picture shows? You must have the Lua code somewhere.
empardopo wrote:Changes made in SciTEUser.properties. If I change color in extension.properties file I don't get any changes. Any idea?
The extension.properties file needs to be in the same directory as the Lua file. And you say there is no Lua file there. It sounds like yours may not be set up as an extension.
empardopo wrote:Btw, I get this... I can't see the "Selection: chars" words!!
Did you really update your SciTEUser.properties file to match the latest? It looks like it still has $(FileAttr) in it instead of Selection: $(SelLength) chars.
I've just created the Lua file in the same directory...
My SciTEUser.properties file has the next code

Code: Select all

# User initialization file for SciTE4AutoHotkey
#
# You are encouraged to edit this file!
#

# Import the platform-specific settings
import _platform

# Import the settings that can be edited by the bundled properties editor
import _config

# Add here your own settings
#highlight.current.word=1
statusbar.text.1=\
#Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | $(FileAttr)  |  $(countedOccurrences) Occurrences of "$(markedWord)" found
Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | Selection: $(SelLength) chars |  $(countedOccurrences) occurrences of "$(markedWord)" found (case insensitive)

I modified the file UserLuaScript.lua but I don't get the word Selection and I don't know why?
Everything is possible!
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

26 Feb 2015, 09:37

empardopo wrote:

Code: Select all

statusbar.text.1=\
#Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | $(FileAttr)  |  $(countedOccurrences) Occurrences of "$(markedWord)" found
Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | Selection: $(SelLength) chars |  $(countedOccurrences) occurrences of "$(markedWord)" found (case insensitive)
I modified the file UserLuaScript.lua but I don't get the word Selection and I don't know why?
It's because you can't have a commented line between the statusbar.text.1=\ and the continuation of that line that defines the text. If you do, it will continue to use what you previously had defined. Get rid of the line inbetween starting with #Line and it should work.
User avatar
empardopo
Posts: 336
Joined: 06 Oct 2013, 12:50
Location: Spain
Contact:

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

26 Feb 2015, 09:42

boiler wrote:
empardopo wrote:

Code: Select all

statusbar.text.1=\
#Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | $(FileAttr)  |  $(countedOccurrences) Occurrences of "$(markedWord)" found
Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | Selection: $(SelLength) chars |  $(countedOccurrences) occurrences of "$(markedWord)" found (case insensitive)
I modified the file UserLuaScript.lua but I don't get the word Selection and I don't know why?
It's because you can't have a commented line between the statusbar.text.1=\ and the continuation of that line that defines the text. If you do, it will continue to use what you previously had defined. Get rid of the line inbetween starting with #Line and it should work.
Perfect. Now, I can see the complet line containing the Selection word.
The colors and the lineending sign could now be changed in the extension.properties file
If I change the color in extension.properties file I get no change. Any idea?

Thanks very much.
Everything is possible!
ozzii
Posts: 481
Joined: 30 Oct 2013, 06:04

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

27 Feb 2015, 04:00

@haichen
The version is the same, normal or?
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

27 Feb 2015, 04:37

empardopo wrote:If I change the color in extension.properties file I get no change. Any idea?
You probably don't have the latest version of the lua file. If you download and install haichen's latest .s4x file, it should work. I just did, and it works great.

It's much better with the color in the properties file because you can change it and see the new color without having to restart SciTE4AutoHotkey.
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

27 Feb 2015, 04:50

One other thing I tried to do but abandoned was to be able to toggle whether it was case sensitive with a hotkey. I was able get Lua to run my function at the press of the key, but I wanted the status bar to reflect my change right when I pressed the key (from [ ] Match case to [X] Match case). It would do it, but not until I moved the cursor in the editor.

Anyone know how to have a Lua script force an immediate update of the SciTE status bar?
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

28 Feb 2015, 19:10

OK, this is my last suggested additional feature to haichen's extension for counting occurrences. :D

This allows you to toggle whether it finds words by matching case or not and as whole words or not. The status bar indicates which mode it's in. You toggle it from the Tools menu or with hotkeys (Ctrl-Alt-C for case matching and Ctrl-Alt-W for whole word matching), and if you forget which hotkeys they are, they're shown in the menu.

Change the status bar definition in the SciTEUser.properties file to this:

Code: Select all

statusbar.text.1=\
Line: $(LineNumber) | Column: $(ColumnNumber) | $(OverType) | ($(EOLMode)) | Selection: $(SelLength) chars |  $(countedOccurrences) occurrences of "$(markedWord)" found, case $(matchCase), $(wholeWord)whole word
Add this to the extension.properties file:

Code: Select all

command.name.12.*=Match Case Toggle
command.subsystem.12.*=3
command.12.*=matchCaseToggle
command.mode.12.*=savebefore:no
command.shortcut.12.*=Alt+Ctrl+C

command.name.13.*=Whole Word Toggle
command.subsystem.13.*=3
command.13.*=wholeWordToggle
command.mode.13.*=savebefore:no
command.shortcut.13.*=Alt+Ctrl+W
Add these functions to the beginning of the extention.lua file:

Code: Select all

OnOpen = function()
    props['matchCase'] = 'insensitive'
    props['wholeWord'] = 'not '
    return true
end


function matchCaseToggle()
    if props['matchCase'] == 'insensitive' then
        props['matchCase'] = 'sensitive'
    else
        props['matchCase'] = 'insensitive'
    end
    scite.UpdateStatusBar()
end

function wholeWordToggle()
    if props['wholeWord'] == 'not ' then
        props['wholeWord'] = ''
    else
        props['wholeWord'] = 'not '
    end
    scite.UpdateStatusBar()
end
And also in the extension.lua file, insert the two if/then/end blocks of code between the local flags = 0 line and the local se,e = ... lines as shown:

Code: Select all

    local flags = 0
    if props['matchCase'] == 'sensitive' then
        flags = SCFIND_MATCHCASE
    end
    if props['wholeWord'] == '' then
        flags = flags + SCFIND_WHOLEWORD
    end
    local s,e = editor:findtext(txt,flags,0)
User avatar
boiler
Posts: 16931
Joined: 21 Dec 2014, 02:44

Re: SciTE4AutoHotkey v3.0.06.01 [Updated October 12 2014]

01 Mar 2015, 21:56

Thanks, lexikos. Yes, it's fine with me for someone to move my posts.

Return to “Scripts and Functions (v1)”

Who is online

Users browsing this forum: No registered users and 226 guests