On a suggestion from limodou, I have decided it would be an excellent idea to add a forum where people can post DrScripts they write that they want to share.
I'll start things off with two variations of an incredibly simple (but useful) DrScript:
I've made an alternate Autocomplete script in doctor script. I prefer this one because it's more like bash's.
If there is only one completition it inserts it, without the dialog box. If there are multiple completitions with a common part it inserts the common part. In case of multiple completitions without common part it pops up a dialog.
Here is the script:
#drscript
import re, sets
current_pos= DrDocument.GetCurrentPos()
first_part= DrDocument.GetTextRange(0, current_pos)
match= re.search("\W(\w+)$", first_part)
if match: # we are typing a word...
current_word= match.group(1)
# current_word doesn't contain special characters... otherwise: current_word=re.excape(current_word)
words= re.findall("\W("+current_word+"\w+)\W", DrDocument.GetText(), re.IGNORECASE)
if len(words) > 0: # we found similar words...
distinct_words= list(sets.Set(words))
if len(distinct_words) == 1:
# only one match insert the text and move the caret
DrDocument.InsertText(current_pos, distinct_words[0][len(current_word):])
DrDocument.GotoPos(current_pos+len(distinct_words[0])-len(current_word))
else:
# find longest common part
common= distinct_words[0]
for w in distinct_words[1:]:
while common != w[:len(common)]: # this will sure exit because current_word must be common
common= common[:-1]
if len(common) == len(current_word):
break
if len(common) > len(current_word):
# multiple matches with common part, insert the common part
DrDocument.InsertText(current_pos, common[len(current_word):])
DrDocument.GotoPos(current_pos+len(common)-len(current_word))
else:
# different matches with no common part, create the popup dialog with all the matches
distinct_words.sort()
dialog_choices= " ".join(distinct_words)
DrDocument.AutoCompShow(len(current_word), dialog_choices)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
I have made my first DrScript!
It looks for the definition of class/method.
It only searchs in the actual file (it wil not search in the included modules)
Hope it will be usefull to someone else...
#------------------------------------------------
#drscript
#FindDef
#This drScript looks for word under cursor, and serches for is def
#======================
#ab 2005/12/25
#version 0.1
#======================
import re
x, y = DrDocument.GetSelection()
if x == y:
#nothing selected
gcp = DrDocument.GetCurrentPos()
st = 0
end = 0
#is there any word under cursor?
#if not, look until one is found
while st == end:
st = DrDocument.WordStartPosition(gcp, 1)
end = DrDocument.WordEndPosition(gcp, 1)
gcp += 1
if gcp >= DrDocument.GetTextLength():
break
if st != end:
#word found
DrDocument.SetSelection(st, end)
myword = DrDocument.GetSelectedText()
else:
myword=''
else:
#take the current selection
myword = DrDocument.GetSelectedText()
DrFrame.SetStatusText(myword, 2)
#shows myword in the statusbar
reinspect = re.compile(r'(^[ \t]*?class\s.*'+myword+'[(:])|(^[ \t]*?def\s.*'+myword+'[(:])', re.MULTILINE)
targetText = DrDocument.GetText()
matcher = reinspect.finditer(targetText)
#Now, try to select the line with the class/method definition
try:
match = matcher.next()
except:
match = None
while (match is not None):
pos=match.start()
line=DrDocument.LineFromPosition(pos)
DrDocument.ScrollToLine(line)
DrDocument.GotoLine(line)
break
try:
match = matcher.next()
except:
match = None
#------------------------------------------------
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
Here goes my first plugin.
His purpose is to show in the browser tree the current line of the editor.
This reverses the normal behaviour of the browser:
- instead of selecting a line of the tree and getting the correspondent line of the source, with this plugin we get on the browser tree the class or def of the current line of the source.
This utility is particulary usefull in large scripts with lots of defs & classes, where I get lost easily :)
The second purpose was to get acquainted to DrPlugin machinery …
Hope it helps someone else.
AB
PS: The last time I have posted, the code lost the indentation. I don't know how to avoid it!
import wx
import drScrolledMessageDialog
from drSourceBrowser import drSourceBrowserPanel
def OnAbout(DrFrame):
Version = "0.0.1"
NameAndVersion = "ShowLine:\n\nVersion: " + Version + "\n"
AboutString = NameAndVersion + "By Antonio Barbosa"
d = drScrolledMessageDialog.ScrolledMessageDialog(DrFrame, AboutString, "About")
d.ShowModal()
d.Destroy()
def OnHelp(DrFrame):
HelpString = "Shows current edit line in the browser tree"
d = drScrolledMessageDialog.ScrolledMessageDialog(DrFrame, HelpString, "Help")
d.ShowModal()
d.Destroy()
def Plugin(DrFrame):
def OnShowMyLine (event):
browsepanel=DrFrame.SourceBrowser
browsetree=browsepanel.classtree
currentline = DrFrame.txtDocument.GetCurrentLine()
currentpos = DrFrame.txtDocument.PositionFromLine(currentline)
i=-1
for pos in browsepanel.ItemsPos:
i+=1
if pos==currentpos:
break
elif pos>currentpos:
i-=1
break
if i<0:
i=0
treeindex=browsepanel.ItemsIndex[i]
browsetree.SelectItem(treeindex,True)
Hi,
DrPlugin: RunTab1
This plugin simplifies what we have to do when working with multi-files scripts.
Normally when we are editing a file that is NOT the MAIN one, we have to do: Save actual file; Select the main file (the 1st tab, usually); Run; And finally, when program ends, select again the previous working file...
With this plugin, only one key / button is needed.
AB.
PS: I have used a trick to avoid loosing the indentation spaces: I put a | on the beginning of each line: I think it is easier to remove the | than try to guess the indentation :).
#-->8---------------------------------------
# Programmer: Antonio Barbosa
# Note: Initial Release 22 Fev 2006
#
#Plugin
#RunTab1
import wx
import drScrolledMessageDialog
from drSourceBrowser import drSourceBrowserPanel
def OnAbout(DrFrame):
Version = "0.0.1"
NameAndVersion = "Runs first page: " + Version + "\n"
AboutString = NameAndVersion + "By Antonio Barbosa"
d = drScrolledMessageDialog.ScrolledMessageDialog(DrFrame, AboutString, "About")
d.ShowModal()
d.Destroy()
def OnHelp(DrFrame):
HelpString = "Runs allways the first script.\n It is suposed to be the main one...\n"
HelpString += "\n\nWhat does this plugin:\n"
HelpString += "\t Saves Actual Document\n"
HelpString += "\t Selects First Page\n"
HelpString += "\t Runs First Page script\n"
HelpString += "\t And finally, restores the previous working page\n"
d = drScrolledMessageDialog.ScrolledMessageDialog(DrFrame, HelpString, "Help")
d.ShowModal()
d.Destroy()
def OnPreferences(DrFrame):
DrFrame.ShowMessage("Nothing to configure ... ;)", "PREFS")
It checks the file (if there is an error, it will show the line, but not start the script).
The current file will be saved automatically.
(ther could be also SaveAll command)
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
On a suggestion from limodou, I have decided it would be an excellent idea to add a forum where people can post DrScripts they write that they want to share.
I'll start things off with two variations of an incredibly simple (but useful) DrScript:
#MakeInt
#drscript
DrDocument.SetSelectedText("int(" + DrDocument.GetSelectedText() + ")")
#********************
#MakeStr
#drscript
DrDocument.SetSelectedText("str(" + DrDocument.GetSelectedText() + ")")
#********************
Bind one of these to a key, then fly through a document. For example:
x = chkSomething.GetValue()
y = chkSomethingElse.GetValue()
becomes
x = int(chkSomething.GetValue())
y = int(chkSomethingElse.GetValue())
which may become necessary depending on the version of python/wxPython you are using.
Hi!
I've made an alternate Autocomplete script in doctor script. I prefer this one because it's more like bash's.
If there is only one completition it inserts it, without the dialog box. If there are multiple completitions with a common part it inserts the common part. In case of multiple completitions without common part it pops up a dialog.
Here is the script:
#drscript
import re, sets
current_pos= DrDocument.GetCurrentPos()
first_part= DrDocument.GetTextRange(0, current_pos)
match= re.search("\W(\w+)$", first_part)
if match: # we are typing a word...
current_word= match.group(1)
# current_word doesn't contain special characters... otherwise: current_word=re.excape(current_word)
words= re.findall("\W("+current_word+"\w+)\W", DrDocument.GetText(), re.IGNORECASE)
if len(words) > 0: # we found similar words...
distinct_words= list(sets.Set(words))
if len(distinct_words) == 1:
# only one match insert the text and move the caret
DrDocument.InsertText(current_pos, distinct_words[0][len(current_word):])
DrDocument.GotoPos(current_pos+len(distinct_words[0])-len(current_word))
else:
# find longest common part
common= distinct_words[0]
for w in distinct_words[1:]:
while common != w[:len(common)]: # this will sure exit because current_word must be common
common= common[:-1]
if len(common) == len(current_word):
break
if len(common) > len(current_word):
# multiple matches with common part, insert the common part
DrDocument.InsertText(current_pos, common[len(current_word):])
DrDocument.GotoPos(current_pos+len(common)-len(current_word))
else:
# different matches with no common part, create the popup dialog with all the matches
distinct_words.sort()
dialog_choices= " ".join(distinct_words)
DrDocument.AutoCompShow(len(current_word), dialog_choices)
Hi,
I have made my first DrScript!
It looks for the definition of class/method.
It only searchs in the actual file (it wil not search in the included modules)
Hope it will be usefull to someone else...
#------------------------------------------------
#drscript
#FindDef
#This drScript looks for word under cursor, and serches for is def
#======================
#ab 2005/12/25
#version 0.1
#======================
import re
x, y = DrDocument.GetSelection()
if x == y:
#nothing selected
gcp = DrDocument.GetCurrentPos()
st = 0
end = 0
#is there any word under cursor?
#if not, look until one is found
while st == end:
st = DrDocument.WordStartPosition(gcp, 1)
end = DrDocument.WordEndPosition(gcp, 1)
gcp += 1
if gcp >= DrDocument.GetTextLength():
break
if st != end:
#word found
DrDocument.SetSelection(st, end)
myword = DrDocument.GetSelectedText()
else:
myword=''
else:
#take the current selection
myword = DrDocument.GetSelectedText()
DrFrame.SetStatusText(myword, 2)
#shows myword in the statusbar
reinspect = re.compile(r'(^[ \t]*?class\s.*'+myword+'[(:])|(^[ \t]*?def\s.*'+myword+'[(:])', re.MULTILINE)
targetText = DrDocument.GetText()
matcher = reinspect.finditer(targetText)
#Now, try to select the line with the class/method definition
try:
match = matcher.next()
except:
match = None
while (match is not None):
pos=match.start()
line=DrDocument.LineFromPosition(pos)
DrDocument.ScrollToLine(line)
DrDocument.GotoLine(line)
break
try:
match = matcher.next()
except:
match = None
#------------------------------------------------
Hello a_barbosa,
thank you for contributing your useful script.
I'll add it to my personal script collection. ;)
Hi,
Here goes my first plugin.
His purpose is to show in the browser tree the current line of the editor.
This reverses the normal behaviour of the browser:
- instead of selecting a line of the tree and getting the correspondent line of the source, with this plugin we get on the browser tree the class or def of the current line of the source.
This utility is particulary usefull in large scripts with lots of defs & classes, where I get lost easily :)
The second purpose was to get acquainted to DrPlugin machinery …
Hope it helps someone else.
AB
PS: The last time I have posted, the code lost the indentation. I don't know how to avoid it!
#------------------------------------
# Programmer: Antonio Barbosa
# E-mail: ab@jn.pt
# Note: Initial Release 19 Fev 2006
#
#Plugin
#ShowLineinBrowser
#This plugin is only a Test: shows current line
import wx
import drScrolledMessageDialog
from drSourceBrowser import drSourceBrowserPanel
def OnAbout(DrFrame):
Version = "0.0.1"
NameAndVersion = "ShowLine:\n\nVersion: " + Version + "\n"
AboutString = NameAndVersion + "By Antonio Barbosa"
d = drScrolledMessageDialog.ScrolledMessageDialog(DrFrame, AboutString, "About")
d.ShowModal()
d.Destroy()
def OnHelp(DrFrame):
HelpString = "Shows current edit line in the browser tree"
d = drScrolledMessageDialog.ScrolledMessageDialog(DrFrame, HelpString, "Help")
d.ShowModal()
d.Destroy()
def Plugin(DrFrame):
def OnShowMyLine (event):
browsepanel=DrFrame.SourceBrowser
browsetree=browsepanel.classtree
currentline = DrFrame.txtDocument.GetCurrentLine()
currentpos = DrFrame.txtDocument.PositionFromLine(currentline)
i=-1
for pos in browsepanel.ItemsPos:
i+=1
if pos==currentpos:
break
elif pos>currentpos:
i-=1
break
if i<0:
i=0
treeindex=browsepanel.ItemsIndex[i]
browsetree.SelectItem(treeindex,True)
ID_MYLINE = DrFrame.GetNewId()
DrFrame.viewmenu.Append(ID_MYLINE, "Show Line")
DrFrame.Bind(wx.EVT_MENU, OnShowMyLine, id=ID_MYLINE)
DrFrame.AddPluginShortcutFunction("ShowLine", "Show Line", OnShowMyLine)
DrFrame.AddPluginPopUpMenuFunction("ShowLine", "Show Line", OnShowMyLine)
Oops, my script lost the indentation tabs!
Is there a way to aviod this «feature»?
AB
Nice, thank you for contributing.
Yes, I too find it very useful, and had myself
the thought, to implement such a thing.
Then I don't need to edit myself, :-)
Hi,
DrPlugin: RunTab1
This plugin simplifies what we have to do when working with multi-files scripts.
Normally when we are editing a file that is NOT the MAIN one, we have to do: Save actual file; Select the main file (the 1st tab, usually); Run; And finally, when program ends, select again the previous working file...
With this plugin, only one key / button is needed.
AB.
PS: I have used a trick to avoid loosing the indentation spaces: I put a | on the beginning of each line: I think it is easier to remove the | than try to guess the indentation :).
#-->8---------------------------------------
I have made a similar DrScript instead of a plugin.
Because I'm not changing projects so often,
I hardcoded it.
My Script looks like:
#drscript
if DrFrame.CheckSyntax():
->if DrDocument.GetModify():
->->DrFrame.OnSave(None)
->->DrFrame.runcommand((DrFrame.pythexecw + " -u " + ' "c:/Python24/Lib/site-packages/wx-2.621-msw-ansi/wx/py/PyCrust.py"'), "Running")
I like this escpecially because:
It checks the file (if there is an error, it will show the line, but not start the script).
The current file will be saved automatically.
(ther could be also SaveAll command)