AutoHotkey Community

It is currently May 27th, 2012, 6:25 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 8 posts ] 
Author Message
PostPosted: April 8th, 2009, 12:25 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
FILES

FRBuddy.ahk ; the script (all you need to download)

Will be made by the script if not present:
sarc.ini ; INI file
frdata.dat ; file that stores find/replace strings in hexified format
frdata.bak ; backup file (for update/delete)

PURPOSE

This script can store Find and Replace combinations for text editors. UltraEdit, Notepad++ and PSPad are supported by default. See the below for more information on how to add your editor if not already in the list.

I realise most editors can remember the last +/-10 Find/Replace items or have macro capabilities, but storing them can be useful if you don't need them very often which means it is not very useful to create a macro or add them to your favourites.

You can also store comments so you can describe what the purpose is, you can search the find, replace and comments making it easier to manage large collections.

Many times I've made a (complex) regexp that I would need again later and I stored them in a text file. But I found it to be annoying to copy them to the F&R input fields, hence this script.

Note: Although I say Editors, it can be used for other programs as well as you can see by the Total Commander INI below. It opens the Find Files dialog and fills the 'Search for' (find) and 'Find text' (replace).

HOW IT WORKS

It stores your F&R strings in a text file - hexified to accommodate multiline & "special" characters like %^{}[] etc - and you can add comments to them as well. After you launch the script you can search(!) in your collection or browse using the Up/Down & PgUp/PgDown keys.

It tries to 'guess' your preferred editor based on the active program when you launch the script, so if you are editing in Notepad++ it will select Notepad++ as target editor.

If you prefer you can choose another editor and it will try to activate that editor (which has to be opened, it does not start the editor). You can also LOCK any editor to be the default editor.

Note: It doesn't execute the F&R action, so you can check the settings before you run it.

SHORTCUTS

There are a number of keyboard shortcuts:

F1 = Help :-)
CTRL-Delete or F8 = Delete currently selected row
Enter = Select
Up/Down & Page Up/Page Down = navigate results

ADDING FIND/REPLACE STRINGS

If you click the ADD button you can enter the F&R strings and add a comment.

If your Find and Replace dialog is open it will try to copy the information from it and fill it in for you.

If you EDIT a string, a backup is made of the old list in frdata.bak

EXTRA (using the %_evar_% variable)

There is one special variable you can use in the Find and/or Replacement strings called %_evar_%

If you run the script, you will see you can enter text in the Extra input field. This text can either be a shorthand or can be used as entered.

Shorthand examples:

ds = Date short
dl = Date long
t = time

If you include the special %_evar_% variable somewhere in your FIND or REPLACE string you can insert 'extra' text at the point. Example for the replacement string:

'This document was last updated on %_evar_%'

if you would enter 'ds' (without the ') in the extra input field, it will replace %_evar_% with 3/21/2009 so the entire replacement string that is sent to your editor will be:

'This document was last updated on 3/21/2009'

See the ButtonSelect routine in the script for other examples and to add your own.

STRUCTURE OF THE INI FILE - OR HOW TO ADD ANOTHER EDITOR

If you want to add another editor, just create a new Editor Name as [Section] and use the AHK Window Spy to find the following values:

EditorWindowClass=
-> ahk_class of your editor (must NOT include the text ahk_class)

FRWindowName=
-> WinTitle of the F&R window, CAN include ahk_class

FindClassNN=
-> ClassNN of the Edit control for Find, Edit1 in most cases

ReplaceClassNN=
-> ClassNN of the Edit control for Replace, Edit2 in most cases

Shortcut=
-> The key combination to start F&R in the editor, usually CTRL-R or CTRL-H

EXAMPLE

[Notepad++]
EditorWindowClass=Notepad++
Shortcut=^h
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

Editors in this INI:

- UltraEdit-32*
- Notepad++
- PSPad

On the forum you will find more for:

- Total Commander
- Metapad
- NoteTab Light
- SciTE
- ConText

* see 'BUGS'

'BUGS'

- I have hardcoded an exception for UltraEdit 13+, the reason for this is that UE seems to change its classes each times it launches, although I could perhaps use SetTitleMatchMode, Regex I couldn't be bothered to fix it :-)

TODO

- Nothing I can think of - other than adding more editors :-)

If you're curious after a while on how many times you actualy used it, in the INI you will find the key USED in the SETTINGS section.

CREDITS (see script for references (Rajat, Laszlo, Skan))

The Script:
Code:
; Find & Replace Buddy
; FRBuddy.ahk
; http://www.autohotkey.com/forum/viewtopic.php?t=42971
;
; Credits:
;
; - 320mph by Rajat - you'll recognize the GUI & Search bits and pieces :-)
;   http://www.autohotkey.com/forum/topic3010.html
; - String2hex, Laszlo (there are various versions (of hexify) on the forum
;   http://www.autohotkey.com/forum/topic4934-15.html#158672
;   search the forum, also by Rajat if you are interested)
;   I just fiddled around a bit until I got the results I wanted :-)
; - SKAN for Parent ID DLL wizardry
;   http://www.autohotkey.com/forum/topic29852.html
; - and probably more which I forgot
;
; Notes:
; - jEdit would need alternative method for sending find / replace (no classes!
;   not that difficult, but I don't need it)
; - UltraEdit is hardcoded fix, can't be bothered to ... :-)
; - I start the script via a hotkey (^#R) but I leave that up to your own preference
; - It also works with MS Word but it sometimes fails ...


#SingleInstance force
SetBatchLines, -1
SetKeyDelay, -1
SetTitleMatchMode, 2

; INI section -----------------------------------------------------------------

; Get some info from currently active window (parent of...)

ID := DllCall("GetParent", UInt,WinExist("A")), ID := !ID ? WinExist("A") : ID ; thanks SKAN http://www.autohotkey.com/forum/topic29852.html
WinGetClass, Active_Parent_Window_Class, ahk_id %id%
WinGetTitle, Active_Parent_Window_Title, ahk_id %id%
WinGet, Active_Parent_Process, ProcessName ; for UltraEdit only, see later

CreateINI()

MainWnd=FRBuddy
Version=v0.2
LastText=fadsfSDFDFasdFdfsadfsadFDSFDf

; Various widths, heights & positions for the GUI

GuiWMinus=20
GuiHMinus=250
GuiW:=A_ScreenWidth-GuiWMinus
GuiH:=A_ScreenHeight-GuiHMinus
LVGuiW:=GuiW - 12
LVGuiH:=GuiH - 64
StatusY:=GuiH - 22
ColW:=Ceil(LVGuiW/3)
wInput:=ColW-150
wPadding:=wInput+5
xSelect:=GuiW-241
xAdd:=GuiW-180
xEdit:=GuiW-120
xHelp:=GuiW-58
VisibleRows:=Ceil(LVGuiH/15) ; for pagedown/pageup

; Read Editors from INI file

CreateINI()

FileRead, FullINI, %A_ScriptDir%\sarc.ini
Loop, Parse, FullINI,`r,`n
   {
   RegExMatch(A_LoopField, "\[(.*)]$",Section) ; yes just InStr is probably faster
   If (Section1 <> "")
      Sections.= "|" Section1
   }

StringTrimLeft, Sections, Sections, 10 ; trim first section [settings]

IniRead, Lock, %A_ScriptDir%\sarc.ini, Settings, Lock
IniRead, Editor, %A_ScriptDir%\sarc.ini, Settings, Editor

; Get list of editors

Loop, Parse, Sections, |
   {
   IniRead, ReadTemp, %A_ScriptDir%\sarc.ini, %A_LoopField%, EditorWindowClass
   EditorClasses.="|" ReadTemp
   If (Lock = 1) AND (Editor = A_LoopField)
      StringReplace,Sections,Sections,%A_LoopField%,%A_LoopField%| ; make the chosen editor the selected one in the dropdownlist
   Else
      {
      If (Active_Parent_Window_Class = ReadTemp)
         StringReplace,Sections,Sections,%A_LoopField%,%A_LoopField%| ; make the chosen editor the selected one in the dropdownlist
      }
   ReadTemp=
   }


; we need to make an exeption for UltraEdit, it's class is constantly changing the last 5 characters, crude hack I know
Ultra:="uedit32" . "." . "exe"
If (Active_Parent_Process = Ultra)
      StringReplace,Sections,Sections,UltraEdit,UltraEdit|

StringTrimLeft, EditorClasses, EditorClasses, 1 ; trim first |

If (Lock=1)
   Checked=Checked
Else
   Checked=""

; Gui -------------------------------------------------------------------------

GuiStart:
Gui , 1:Add, Text, x6 y10 w40 h20, Search:
Gui , 1:Add, Edit, x46 y7 w%wInput% h20 vCurrText gGetText,
Gui , 1:Add, Text, xp+%wPadding% y10 w40 h20, Extra:
Gui , 1:Add, Edit, xp+30 y7 w%wInput% h20 vExtra,
Gui , 1:Add, Text, xp+%wPadding% y10 w40 h20, Editor:
Gui , 1:Add, DropDownList, Sort xp+35 y6 w125 vSettings_Editor r10, %Sections%
Gui , 1:Add, Checkbox, 0x8000 xp+130 y10 vLock gLock %checked%, Loc&k
Gui , 1:Add, Button, 0x8000 x%xSelect% y7 w50 h20 Default, &Select
Gui , 1:Add, Button, 0x8000 x%xAdd% y7 w50 h20, &Add
Gui , 1:Add, Button, 0x8000 x%xEdit% y7 w50 h20, &Edit
Gui , 1:Add, Button, 0x8000 x%xHelp% y7 w50 h20, &Help
Gui , 1:Add, ListView, x6 y35 w%LVGuiW% h%LVGuiH% Grid ReadOnly HScroll -0x200000 gSelection,Find|Replace|Comment
Gosub, PopulateList
Gui , 1:Add, Text, x6 y%StatusY% w120 h20 vResults, Results = %Lines%
Gui , 1:Add, Text, xp+675 y%StatusY% w600 h20, %MainWnd% - %version%
Gui , 1:Show, h%GuiH% w%GuiW%, %MainWnd%
Gosub, GetText
Return

; -----------------------------------------------------------------------------

PopulateList:
LV_Delete() ; clear listview
Lines=
FileRead, GetList, %A_ScriptDir%\frdata.dat
Loop, Parse, GetList, `r,`n
   {
   If (A_LoopField <> "") ; skip empty lines if any
      {
      StringSplit,C,A_LoopField,|
      Convert("h2s",c1,c2,c3)
      Col1_%A_Index%:=cv1
      Col2_%A_Index%:=cv2
      Col3_%A_Index%:=cv3
      LV_Add("",Col1_%A_Index%,Col2_%A_Index%,Col3_%A_Index%)
      Lines=%A_Index%
      }
   }
LV_ModifyCol(1, ColW+5)
LV_ModifyCol(2, ColW+5)
LV_ModifyCol(3, ColW-30)
LV_ModifyCol(3, "Sort")
GuiControl,, Results, Results = %Lines%
GetList= ; clear
Return

; Search ----------------------------------------------------------------------

GetText:
   HitList=
   MinLen=1
   ControlGetText, CurrText, Edit1, %MainWnd%
   IfEqual, CurrText, %LastText%
      {
      IfLess, Results, %Lines%
         Gosub PopulateList ; if Edit1 is empty again after del or backspace "restart"
      Return
      }
   StringLen, Check, CurrText
   IfLess, Check, %MinLen%
      {
      Gosub PopulateList ; if Edit1 is empty again after del or backspace "restart"
      Return
      }
   ControlGetText, CurrText, Edit1, %MainWnd%
   LastText = %CurrText%
   IfNotEqual, CurrText, %LastText%, Goto, GetText
   Loop, %lines%
   {
      FullLine:=Col1_%A_Index% . A_Space . Col2_%A_Index% . A_Space . Col3_%A_Index%
      IfInString, FullLine, %CurrText%
         Hitlist.= "|" A_Index
   }

   StringTrimLeft, Hitlist, Hitlist, 1 ; remove first |
   LV_Delete() ; clear listview
   Loop, Parse, Hitlist, |
      LV_Add("",Col1_%A_LoopField%,Col2_%A_LoopField%,Col3_%A_LoopField%)

LastText =
Results := LV_GetCount()
GuiControl,, Results, Results = %Results%
Return

; Keys ------------------------------------------------------------------------

Esc::
Goto, GuiClose
Return

Down::
   IfWinNotActive, %MainWnd%,
   {
      Send, {Down}
      Return
   }
   ControlGetFocus, CurrCtrl, %MainWnd%
   IfEqual, CurrCtrl, Edit1
      ControlSend, SysListView321, {Down}, %MainWnd%
   IfEqual, CurrCtrl, ComboBox1
      ControlSend, ComboBox1, {Down}, %MainWnd%
Return

Up::
   IfWinNotActive, %MainWnd%,
   {
      Send, {Up}
      Return
   }
   ControlGetFocus, CurrCtrl, %MainWnd%
   IfEqual, CurrCtrl, Edit1
      ControlSend, SysListView321, {Up}, %MainWnd%
   IfEqual, CurrCtrl, ComboBox1
      ControlSend, ComboBox1, {Up}, %MainWnd%
Return

Pgdn::
   IfWinNotActive, %MainWnd%,
   {
      Send, {PgDn}
      Return
   }
   ControlGetFocus, CurrCtrl, %MainWnd%
   IfEqual, CurrCtrl, Edit1
      ControlSend, SysListView321, {Down %VisibleRows%}, %MainWnd%
Return

Pgup::
   IfWinNotActive, %MainWnd%,
   {
      Send, {Pgup}
      Return
   }
   ControlGetFocus, CurrCtrl, %MainWnd%
   IfEqual, CurrCtrl, Edit1
      ControlSend, SysListView321, {Up %VisibleRows%}, %MainWnd%
Return

^Del::
F8::
Gui, 1:Submit, NoHide
ControlFocus, SysListView321, %MainWnd%
SelItem := LV_GetNext()
If (SelItem = 0)
   SelItem = 1
LV_GetText(Find, SelItem, 1)
LV_GetText(Replace, SelItem, 2)
LV_GetText(Comments, SelItem, 3)
Gui, 1:Destroy

ScriptHotkeys() ; Toggle hotkeys

MsgBox, 33, Delete, Do you want to delete:`r`n`r`nF: %Find%`r`nR: %Replace%`r`nC: %Comments%
IfMsgBox OK
   {
   AutoTrim, On
   FileMove, %A_ScriptDir%\frdata.dat, %A_ScriptDir%\frdata.bak, 1 ; create backup
   Convert("s2h",Find,Replace,Comments)
   DeleteLine=%cv1%|%cv2%|%cv3%
   Loop, Read, frdata.bak, %A_ScriptDir%\frdata.dat
   {
      ReadLine=%A_LoopReadLine% ; to trim any trailing spaces
      IfNotEqual, ReadLine, %DeleteLine%, FileAppend, %ReadLine%`r`n
   }
   DeleteLine=
   ReadLine=
   }
ScriptHotkeys() ; Toggle hotkeys
Reload
Return

F1::
Gosub ButtonHelp
Return

; Buttons ---------------------------------------------------------------------

ButtonSelect:
   Gui , 1:Submit, NoHide
   ControlFocus, SysListView321, %MainWnd%
   SelItem := LV_GetNext()
   If (SelItem = 0)
      SelItem = 1
   LV_GetText(Find, SelItem, 1)
   LV_GetText(Replace, SelItem, 2)
   Gui, 1:Destroy
   CheckExtra=%Find%%Replace% ; quick & dirty method, could be nicer
   IfInString, CheckExtra, `%_evar_
      {
      ; just some examples what you could do with %_evar_%
      IfEqual, Extra, dl
         FormatTime, Extra, , LongDate
      IfEqual, Extra, ds
         FormatTime, Extra, , ShortDate
      IfEqual, Extra, t
         FormatTime, Extra, , Time
      ;IfEqual, Extra, x
      ;   Extra=_eXample_

      ; If nothing special found, just use the extra text as entered
      StringReplace, Find, Find, `%_evar_`%, %Extra%
      StringReplace, Replace, Replace, `%_evar_`%, %Extra%
      }

ReadEditorSetting()

If (Settings_Editor = "UltraEdit") ; exception for UltraEdit
   WinActivate, UltraEdit,,1
Else
{
   IfWinNotExist, ahk_class %EditorWindowClass%
      {
      MsgBox,48,Failed, %Settings_Editor% not running.`r`nClosing %MainWnd%.
      Goto, GuiClose
      }
   WinActivate, ahk_class %EditorWindowClass%
}

Send, %ShortCut%
WinWaitActive, %FRWindowName%,,2
If ErrorLevel
   {
   MsgBox,48,Failed, Find `& Replace command unsuccessful.`r`nClosing %MainWnd%.
   Goto, GuiClose
   }
Else
   {
   ControlSetText, %FindClassNN%, %Find%, %FRWindowName%
   ControlSetText, %ReplaceClassNN%, %Replace%, %FRWindowName%
   IniRead, Used, %A_ScriptDir%\sarc.ini, Settings, Used
   Used += 1
   IniWrite, %Used%, %A_ScriptDir%\sarc.ini, Settings, Used
   }
Goto, GuiClose
Return

; mouse event -----------------------------------------------------------------

Selection:
IfEqual, A_GuiControlEvent, DoubleClick
    GoTo, ButtonSelect
Return

ButtonAdd:
Gui, 1:Submit
ReadEditorSetting()
ControlGetText, Find, %FindClassNN%, %FRWindowName%
ControlGetText, Replace, %ReplaceClassNN%, %FRWindowName%
StringReplace, Find, Find, `n, `r`n, all
StringReplace, Replace, Replace, `n, `r`n, all
Gui, 20:Add, Text, x10 y10 w100 h20, Find:
Gui, 20:Add, Edit, x70 y10 w400 r1 -Multi vFind, %Find%
Gui, 20:Add, Text, x10 y40 w100 h20, Replace:
Gui, 20:Add, Edit, x70 y40 w400 r1 -Multi vReplace, %Replace%
Gui, 20:Add, Text, x10 y70 w100 h20, Comments:
Gui, 20:Add, Edit, x70 y70 w400 r1 -Multi vComments,
Gui, 20:Add, Button, x70 y100 w70 H25 gButtonSave Default, &Save
Gui, 20:Add, Button, x150 y100 w70 H25 gButtonCancel, &Cancel
Gui, 20:Show
Return

ButtonSave:
Gui, 20:Submit
Gui, 20:Destroy
Convert("s2h",Find,Replace,Comments)
FileAppend,%cv1%|%cv2%|%cv3%`r`n, %A_ScriptDir%\frdata.dat
Reload
Return

ButtonCancel:
Gui, 20:Destroy
Gui, 1:Show
Return

ButtonEdit:
Gui, 1:Submit
ControlFocus, SysListView321, %MainWnd%
SelItem := LV_GetNext()
If (SelItem = 0)
   SelItem = 1
LV_GetText(Find, SelItem, 1)
LV_GetText(Replace, SelItem, 2)
LV_GetText(Comments, SelItem, 3)
Convert("s2h",Find,Replace,Comments)
DeleteLine=%cv1%|%cv2%|%cv3% ; we need this so we can replace it
Gui, 40:Add, Text, x10 y10 w100 h20, Find:
Gui, 40:Add, Edit, x70 y10 w400 r1 -Multi vFind, %Find%
Gui, 40:Add, Text, x10 y40 w100 h20, Replace:
Gui, 40:Add, Edit, x70 y40 w400 r1 -Multi vReplace, %Replace%
Gui, 40:Add, Text, x10 y70 w100 h20, Comments:
Gui, 40:Add, Edit, x70 y70 w400 r1 -Multi vComments, %Comments%
Gui, 40:Add, Button, x70 y100 w70 H25 gButtonUpdate Default, &Update
Gui, 40:Add, Button, x150 y100 w70 H25 gButtonCancel, &Cancel
Gui, 40:Show
Return

ButtonUpdate:
Gui, 40:Submit
StringReplace,Find,Find,`n,`r`n,All ; The edit control removes the `r so we need to add it back in case of multiline
StringReplace,Replace,Replace,`n,`r`n,All ; The edit control removes the `r so we need to add it back in case of multiline
Convert("s2h",Find,Replace,Comments)
ReplaceLine=%cv1%|%cv2%|%cv3%
ScriptHotkeys()
MsgBox, 33, Update, Do you want to Update this entry:`r`n`r`nF: %Find%`r`nR: %Replace%`r`nC: %Comments%
IfMsgBox OK
   {
   AutoTrim, On
   FileMove, %A_ScriptDir%\frdata.dat, frdata.bak, 1 ; create backup
   Loop, Read, frdata.bak, %A_ScriptDir%\frdata.dat
   {
      ReadLine=%A_LoopReadLine% ; to trim any trailing spaces
      IfEqual, ReadLine, %DeleteLine% ; found the old one, replace with new one
         FileAppend, %ReplaceLine%`r`n
      Else ; keep this line
         FileAppend, %ReadLine%`r`n
   }
   DeleteLine=
   ReadLine=
   ReplaceLine=
   }
ScriptHotkeys()
Reload
Return

Lock: ; set specific editor as default
GuiControlGet, Status, , Button1
IniWrite, %Status%, %A_ScriptDir%\sarc.ini, Settings, Lock
GuiControlGet, EditorSave, , ComboBox1
IniWrite, %EditorSave%, %A_ScriptDir%\sarc.ini, Settings, Editor
ControlFocus, Edit1, %MainWnd%
Return

ButtonHelp:
;Gui, 1:Hide
HelpText =
(
Forum
------------------------------------------------------------------------------------
More information at the AHK forum
http://www.autohotkey.com/forum/viewtopic.php?t=42971

Some basic info below.

Keys
------------------------------------------------------------------------------------
F1 = Help
CTRL-Delete or F8 = Delete currently selected row
Enter = Select
Up/Down & PgUP/PgDOWN = navigate results

Structure of the INI file - or how to Add another Editor
------------------------------------------------------------------------------------
If you want to add another editor, just create a new
Editor Name as [Section] and use the AHK Window Spy
to find the following values:

EditorWindowClass=
-> ahk_class of your editor (must NOT include the text ahk_class)

FRWindowName=
-> WinTitle of the F&R window, CAN include ahk_class

FindClassNN=
-> ClassNN of the Edit control for Find, Edit1 in most cases

ReplaceClassNN=
-> ClassNN of the Edit control for Replace, Edit2 in most cases

Shortcut=
-> The key combination to start F&R

Example:

[Notepad++]
EditorWindowClass=Notepad++
Shortcut=^h
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

More at the AHK forum.
)
Gui , 90:+Toolwindow
Gui , 90:Add, Edit, w300 H405 ReadOnly WantCtrlA, %HelpText%
Gui , 90:Show, w320 h420, %MainWnd% - Help
Send ^{Home} ; sends cursor to top in Edit
Return

; Functions -------------------------------------------------------------------


; Check INI -------------------------------------------------------------------

CreateINI() ; create ini on first run if it does not Exist
{
 IniFile = %A_ScriptDir%\sarc.ini
 IfNotExist, %IniFile%
 {
  {
   ini=
(
[Settings]
Lock=0
Editor=Notepad++
Used=0

[UltraEdit]
EditorWindowClass=Afx\:00400000\:8\:00010013\:00000000\:.*
Shortcut=^r
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

[Notepad++]
EditorWindowClass=Notepad++
Shortcut=^h
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

[PSPad]
EditorWindowClass=TfPSPad
Shortcut=^h
FRWindowName=Replace ahk_class TfFindReplace
FindClassNN=Edit2
ReplaceClassNN=Edit1

)
  FileAppend,%ini%,%IniFile%
  ini=
  }
 }
}

; Read INI for Editor ---------------------------------------------------------

ReadEditorSetting()
{
Global
IniRead, EditorWindowClass, %A_ScriptDir%\sarc.ini, %Settings_Editor%, EditorWindowClass
IniRead, Shortcut         , %A_ScriptDir%\sarc.ini, %Settings_Editor%, Shortcut
IniRead, FRWindowName     , %A_ScriptDir%\sarc.ini, %Settings_Editor%, FRWindowName
IniRead, FindClassNN      , %A_ScriptDir%\sarc.ini, %Settings_Editor%, FindClassNN
IniRead, ReplaceClassNN   , %A_ScriptDir%\sarc.ini, %Settings_Editor%, ReplaceClassNN
}

; -----------------------------------------------------------------------------

ScriptHotkeys()
{   
Hotkey, Up  , Toggle, UseErrorLevel
Hotkey, Down, Toggle, UseErrorLevel
Hotkey, ^Del, Toggle, UseErrorLevel
Hotkey, F1  , Toggle, UseErrorLevel
Hotkey, F8  , Toggle, UseErrorLevel
}

; Convert 2 and from hex ------------------------------------------------------

Convert(direction,c1,c2,c3)
{
Global
If (direction = "s2h")
   {
   cv1:=String2Hex(c1)
   cv2:=String2Hex(c2)
   cv3:=String2Hex(c3)
   }
If (direction = "h2s")
   {
   cv1:=Hex2String(c1)
   cv2:=Hex2String(c2)
   cv3:=Hex2String(c3)
   }
Return
}

; -----------------------------------------------------------------------------
; Reference http://www.autohotkey.com/forum/topic4934-15.html#158672

String2Hex(x) ; Convert a string to hex digits (modified to accommodate new line chars)
{
  prevFmt = %A_FormatInteger%
  SetFormat Integer, H ;this function requires hex format
   Loop Parse, x
      hex .= 0x100+Asc(A_LoopField)
   StringReplace hex, hex, 0x1,,All
   SetFormat Integer, %prevFmt% ;restore original integer formatting
   Return hex
}

Hex2String(x) ; Convert a huge hex number to string (modified to suite String2Hex above)
{
  Loop % StrLen(x)/2
   {
      Pos:=(A_Index-1)*2+1
      StringMid hex, x, Pos, 2
      string := string . Chr("0x" hex)
   }
   Return string
}

; -----------------------------------------------------------------------------

GuiClose:
GuiExit:
ExitApp

; the end ---------------------------------------------------------------------


If you don't save the INI below, the script will create a
default INI with UltraEdit, Notepad++ and PSPad support.

INI:
Code:
[Settings]
Lock=0
Editor=Notepad++
Used=0

[UltraEdit]
EditorWindowClass=Afx\:00400000\:8\:00010013\:00000000\:.*
Shortcut=^r
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

[Notepad++]
EditorWindowClass=Notepad++
Shortcut=^h
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

[PSPad]
EditorWindowClass=TfPSPad
Shortcut=^h
FRWindowName=Replace ahk_class TfFindReplace
FindClassNN=Edit2
ReplaceClassNN=Edit1

[Microsoft Word 97]
EditorWindowClass=OpusApp
Shortcut=^h
FRWindowName=Find and Replace ahk_class bosa_sdm_Microsoft Word 8.0
FindClassNN=RichEdit20W1
ReplaceClassNN=RichEdit20W2

[Microsoft Word 2000]
EditorWindowClass=OpusApp
Shortcut=^h
FRWindowName=Find and Replace ahk_class bosa_sdm_Microsoft Word 9.0
FindClassNN=RichEdit20W1
ReplaceClassNN=RichEdit20W2

[Total Commander]
EditorWindowClass=TTOTAL_CMD
Shortcut=!{F7}
FRWindowName=Find Files ahk_class TFindFile
FindClassNN=Edit3
ReplaceClassNN=Edit1

[Metapad]
EditorWindowClass=metapad
Shortcut=^h
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

[NoteTab Light]
EditorWindowClass=TfrmNoteTab
Shortcut=^r
FRWindowName=Find and Replace ahk_class TfrmReplace
FindClassNN=Edit2
ReplaceClassNN=Edit1

[SciTE]
EditorWindowClass=SciTEWindow
Shortcut=^h
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

[ConText]
EditorWindowClass=TfmMain
Shortcut=^r
FRWindowName=Replace Text ahk_class TfmFind
FindClassNN=Edit1
ReplaceClassNN=Edit2

[NotePad2]
EditorWindowClass=Notepad2
Shortcut=^h
FRWindowName=Replace ahk_class #32770
FindClassNN=Edit1
ReplaceClassNN=Edit2

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


Last edited by SoLong&Thx4AllTheFish on February 26th, 2011, 11:50 pm, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: April 8th, 2009, 12:25 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
FRBuddy
Image
(in case you are wondering that is BBClean v1.16 as shell)

Image

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: July 1st, 2010, 1:39 am 
Offline

Joined: June 1st, 2007, 2:00 pm
Posts: 180
I download and will comment in any case.

Best Regards
:wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 18th, 2010, 2:17 pm 
I used to launch a word file with a Hotkey. This word file contained all reg exp documentation for several syntaxes (perl, javascript, notepad++, windows advanced replace …) plus examples and most used rules. But opening MS word is very slow and overkill, and a simple text file doesn’t allow the formatting I was looking for.
So this script may come in handy after I find a way to integrate it and possibly a few mods.
Thanks for sharing it :)


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2011, 8:00 am 
Offline

Joined: May 20th, 2009, 6:18 pm
Posts: 129
This is quite a nice script. I think I'm going to fine it quite useful. Thanks, hugov!

So far, I have had two problems, both of which have turned out to be solvable.

One is using FRBuddy with the latest version of KompoZer, because FindClassNN and ReplaceClass NN are the same. This seems rather odd to me, but that's what Window Spy says.

But there's a convenient workaround for this, which might perhaps be of use beyond KompoZer. One can use the shortcut not just to open the "find and replace" dialogue, but to put in the find and replace strings. To do this, one adds to FRBuddy.ahk the following code

Code:
StringReplace,ShortCut,ShortCut,<Find>,%Find%           
StringReplace,ShortCut,ShortCut,<Replace>,%Replace%
StringReplace,Shortcut,ShortCut,<Tab>,%A_Tab%


just before the line

Code:
Send, %ShortCut%


Now one can use KompoZer by adding the following section to sarc.ini:

Code:
[KompoZer]
EditorWindowClass=MozillaUIWindowClass
Shortcut=^f<Find><Tab><Replace>
FRWindowName=Find and Replace ahk_class #32770
FindClassNN=MozillaWindowClass1
ReplaceClassNN=MozillaWindowClass1


The shortcut first opens the "Find and Replace" dialogue, then sends the find string (as <Find> is expanded to this), then a tab takes it to the place for the replace string, and it sends the replace string.

And voila, Find and Replace Buddy works with KompoZer.

(One could, alternatively, replace
Code:
Shortcut=^f<Find><Tab><Replace>

by
Code:
Shortcut=^f<Find>!e<Replace>

- using Kompozer's Alt-e key rather than a tab.)

The other problem I have had was that the last program listed in sarc.ini doesn't seem to show up in FRBuddy. I keep thinking that I must be overlooking something. But in any case, after I added at the end of sarc.ini a dummy section for a non-existent program, the problem was solved. The dummy section could even be reduced just to
Code:
[_]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2011, 12:23 pm 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Glad someone finds it useful after all this time (I still use it every day) the only suggestion would be to lookinto SendRaw or Send {Raw} when sending the find/replace strings in case you have modifier characters in them like ! # ^

Can't replicate the ini problem, I have 11 editors sections and they all load fine, but if adding the [_] works thats good to know for other people that might have the same problem.

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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 25th, 2011, 7:13 pm 
Offline

Joined: May 20th, 2009, 6:18 pm
Posts: 129
Quote:
the only suggestion would be to look into SendRaw or Send {Raw} when sending the find/replace strings in case you have modifier characters in them like ! # ^

Good point!

Hmmm, the problem is that I need the shortcut string to be sent partially in ordinary mode, then in raw mode, then in ordinary mode, etc. So it seems the only way is to parse the shortcut string. Thus, instead of the modification to FRBuddy I suggested yesterday, what works is the following:

Replace the line
Code:
Send, %ShortCut%
with
Code:
Loop, parse, ShortCut, <>
{
If A_LoopField = Find
   SendRaw %Find%
Else if A_LoopField = Replace
   SendRaw %Replace%
Else if A_LoopField = Tab
   Send {Tab}
Else
   Send %A_LoopField%
}


With this change, the previously suggested listings for Kompozer in sarc.ini now handle the modifier characters properly.

(Well, I also see that I don't really need <Tab> as {Tab} works just as well, but it's no problem.)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 26th, 2011, 12:01 am 
Offline

Joined: May 27th, 2007, 9:41 am
Posts: 4999
Just for reference:

Not exactly related but your code reminded me of this little code snippet
Send {Raw}{EndRaw}
http://www.autohotkey.com/forum/viewtop ... 865#286865

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


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: Apollo, JamixZol, rbrtryn, Stigg, Yahoo [Bot] and 25 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