Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Integrating Stata and external text editors


From   "David Elliott" <dcelliott@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Integrating Stata and external text editors
Date   Wed, 30 Aug 2006 10:28:43 -0300

For those using the very excellent NoteTab [ http://notetab.com ]
programmable text editor, Ross Odell created a Clip Library of Stata
related commands that I have used and modified for a number of years.
I offer the library below.  I am not sure if line wraps will appear in
certain places so I am providing a link to the file in my webspace as
well at [ http://www3.ns.sympatico.ca/d.elliott/Stata.clb ].  You have
to have an understanding of NoteTab to know where to place and how to
use this file and regrettably I am not able to support people in
learning these things.  The most used commands are the do|do
selected|do to end but there are others to help with ado programming
and ones at the end for linking to various Stata website URLs.

DCE


X===========Library Text Below=============X
= V5 MultiLine NoSorting TabWidth=30

H="About this Library"
^!INFO [C] This library was written by:^P^PDavid
Elliott^Pd.elliott@ns.sympatico.ca^PHalifax, Nova Scotia,
Canada^Pand^PRoss Odell^Pr.odell@unsw.edu.au^P(Run, Do & List
commands)^PUniversity of New South
Wales^Phttp://www.eng.unsw.edu.au/index.htm

H=";"


H=";Programming Constructs"
These commands help build STATA syntax

H=";ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"


H="Ado Header"
*! version 1.0.0  ^$GetDate("yyyy.mm.dd")$
program define ^?[Ado File Name=] /* ^?[Ado File Description=] */
version ^?[Version (of Stata) Number=7.0]^%NL%

H="while"
while "`^?[Variable=]'" ^?[Relation=<|!=|>|==] ^?[Value=] {
	^&
	}

H="if"
if "`^?[Variable=]'" ^?[Relation=<|!=|>|==] ^?[Value=] {
	^&
	}

H=";"
H=";Generate Commands"


H="macro dir (for debugging)"
macro dir

H="macro string quotes (`"`'"')"
`"`^?[=^&]'"'

H="macro plain quotes (`')"
`^?[=^&]'

H="generate"
^?[by varlist:]generate ^?[type= byte | int | long | float | double |
str# ] ^?[newvar=] = ^?[exp=] ^?[if exp=] ^?[in range=]

H="comment"
/*^?[Comment Text=^&]*/

H="egen"
egen ^?[type=byte|long|int|float|double|string#] ^?[newvar=] =
^?[fcn=count(exp) [, by(varlist)]|diff(varlist) |fill(numlist) [if
exp] and [in range] are not allowed |group(varlist) [, missing]
|iqr(exp) [, by(varlist)] |ma(exp) [, t(#) nomiss] [if exp] is not
allowed|max(exp) [, by(varlist)] |mean(exp) [, by(varlist)]
|median(exp) [, by(varlist)] |min(exp) [, by(varlist)] |mtr(yr inc)
|pctile(exp) [, p(#) by(varlist)] |rank(exp) [, by(varlist)]
|rfirst(varlist) |rlast(varlist) |rmax(varlist) |rmean(varlist)
|rmin(varlist) |rmiss(varlist) |robs(varlist) [, strok] |rsd(varlist)
|rsum(varlist) |sd(exp) [, by(varlist)] |std(exp) [, mean(#) std(#)]
|sum(exp) [, by(varlist)] ] ^?[if exp=] ^?[in range=] ^?[, options
(start with a comma)=]

H="replace"
^?[by varlist:=]replace ^?[oldvar=]=^?[exp=] ^?[if exp=] ^?[in range=]
^?[options=_ |, nopromote]

H=";"


H=";Do/Run/Testing Commands"
To run/do a selection: Highlight one or more lines of text, or parts
thereof, and double click on run or do as desired.

To run/do the entire file:  Make sure that no text is selected.

The clip will:
Save the file if it has been modified.
Write the selected (or all) text to c:\windows\temp\std0001.tmp.
Copy "run filename" to the Windows clipboard.
Make Stata the active window.
Clear the command window (ESC).
Paste the clipboard contents into the Stata command window (Ctrl-v).
Execute the command (Enter).
Return the cursor to the end of the selected text.

H=";ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"


H="run all/select"
^!FocusDoc
;Save the file if it has been modified
^!Save
;Select the highlighted block. If none is highlighted, select all
^!If ^$GetSelSize$ = 0 Next ELSE SelectLines
^!Select All
^!Goto GetSelection
:SelectLines
^!Select Lines
:GetSelection
^!Set %AnyText%=^$GetSelection$
;Write the selected text to a temporary file in the Windows temp. dir.
^!Set %fileName%=^$GetTempFile$
^!TextToFile ^%fileName% ^%AnyText%
^!AppendToFile "^%fileName%" ^%NL%
;Copy "run filename" to the clipboard
^!SetClipboard run "^%fileName%"
;Switch to Stata
^!FocusApp *Stata*
;ESC to clear the Command window, paste the command, hit enter
;^!Keyboard CTRL+4 &100 ESC ALT+E P ENTER
^!Keyboard CTRL+4 CTRL+V ENTER
;Place the cursor at the end of the selected text
^!Jump select_end

H="do selected"
^!FocusDoc
;Save the file if it has been modified
;^!Save
;Select the highlighted block. If none is highlighted, select all
^!If ^$GetSelSize$ = 0 Next ELSE SelectLines
^!Select All
^!Goto GetSelection
:SelectLines
^!Select Lines
:GetSelection
^!Set %AnyText%=^$GetSelection$
;Write the selected text to a temporary file in the Windows temp. dir.
^!Set %fileName%=^$GetTempFile$
^!TextToFile ^%fileName% ^%AnyText%
^!AppendToFile "^%fileName%" ^%NL%
;Copy "run filename" to the clipboard
^!SetClipboard do "^%fileName%"
;Switch to Stata
^!FocusApp *Stata*
;ESC to clear the Command window, paste the command, hit enter
^!Keyboard CTRL+4 CTRL+V ENTER
;Place the cursor at the end of the selected text
^!Jump select_end

H="do to end"
;^!SetDebug on
^!FocusDoc
^!Save
^!Jump Line_Start
^!Set %start_col%=^$Getcol$
^!Set %start_row%=^$Getrow$
^!Jump Doc_End
^!Set %end_col%=^$Getcol$
^!Set %end_row%=^$Getrow$
^!SetCursor ^%start_row%:^%start_col%
^!SelectTo ^%end_row%:^%end_col%
:GetSelection
^!Set %AnyText%=^$GetSelection$
^!Set %fileName%=^$GetTempFile$
^!TextToFile ^%fileName% ^%AnyText%
^!AppendToFile "^%fileName%" ^%NL%
^!SetClipboard do "^%fileName%"
^!FocusApp *Stata*
^!Keyboard CTRL+4 CTRL+V ENTER
^!Jump select_end

H="do all"
;^!SetDebug on
^!FocusDoc
^!Save
^!If ^$GetSelSize$ = 0 Next ELSE SelectLines
^!Select All
^!Goto GetSelection
:SelectLines
^!Select Lines
:GetSelection
^!Set %AnyText%=^$GetSelection$
^!Set %fileName%=^$GetTempFile$
^!TextToFile ^%fileName% ^%AnyText%
^!AppendToFile "^%fileName%" ^%NL%
^!SetClipboard do "^%fileName%"
^!FocusApp *Stata*
^!Keyboard CTRL+4 CTRL+V ENTER
^!Jump select_end

H=";-----------------------"


H="Debug (trace on|off)"
^!SetClipboard set trace ^?[trace==on|off]
;^!SetDebug on
^!FocusApp *Stata*
^!Keyboard CTRL+4 CTRL+V ENTER


H="Cleanup (programs|logs)"
^!SetDebug True
;%NL%program drop _all
^!SetClipboard=discard^%NL%log close^%NL%noisily di in blue "Programs
cleared from memory and Log closed"^%NL%
^!Set %fileName%=^$GetTempFile$
^!TextToFile ^%fileName% ^$GetClipboard$
^!SetClipboard run ^%fileName%, nostop
^!FocusApp *Stata*
^!Keyboard CTRL+4 CTRL+V ENTER


H="Clear Programs from Memory"
^!SetClipboard discard
^!FocusApp *Stata*
^!Keyboard CTRL+4 CTRL+V ENTER
^!FocusApp *NoteTab*

H="Close Log"
^!SetClipboard log close
^!FocusApp *Stata*
;^!Keyboard CTRL+4 &100 ESC ALT+E P ENTER
^!Keyboard CTRL+4 CTRL+V ENTER


H=";"


H=";Commenting"


H=";ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"


H="Add Comment"
^!Replace "^{.}">>"*!* \1" RHSA


H="Uncomment"
^!Replace "^{\*\!\*\s}">>"" RHSA


H=";"


H=";Cleaning"


H=";ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"


H="Strip log"
;^!SetDebug On
^!Replace "^\s+">>"" TWRSA
^!Replace "^(\d+\.*)|(\.)\s">>"" TWRSA
^!Replace \n\>\s		TWRSA

H="Strip Log Cmds to File"
^!SetScreenUpdate Off
^!Set %Bottom%=^$GetLineCount$
^!Set %Source%=^$GetDocName$^
^!Set %Source_ix%=^$GetDocIndex$
^!ToolBar New Document
^!Set %Target%=^$GetDocName$^
^!Set %Target_ix%=^$GetDocIndex$
^!SetWordWrap False
^!Set %Status%="|"
/* Commands from ^%Source% */^%NL%
^!SetDocIndex ^%Source_ix%
^!Set %Start%=^?[Where do you want to start?==_Start From
Top^=Top|Start From Cursor Position^=Cursor]
^!If %Start%<>"Top" START
^!Jump Doc_Start
:START
^!Jump Line_Start
^!If "^$GetChar$" <> "." NEXT
^!Select Paragraph
^!Set %Length%="^$GetSelSize$"
^!If ^%Length%<4 NEXT
^!Dec %Length% 4
^!Set %Command%=^$GetSelection$
^!Set %Command%=^$StrCopy("^%Command%";3;^%Length%)$
^!Set %Status%="^%Status%|"
^!StatusShow ^%Status%
^!SetDocIndex ^%Target_ix%
^%Command%
^!SetDocIndex ^%Source_ix%
:EXTEND
^!Jump +1
^!If "^$GetChar$" <> ">" EXTENDEND
^!Select Paragraph
^!Set %Length%="^$GetSelSize$"
^!If ^%Length%<4 NEXT
^!Dec %Length% 4
^!Set %Command%=^$GetSelection$
^!Set %Command%=^$StrCopy("^%Command%";3;^%Length%)$
^!StatusShow ^%Status%
^!SetDocIndex ^%Target_ix%
^%Command%
^!SetDocIndex ^%Source_ix%
^!GoTo EXTEND
:EXTENDEND
^!SetDocIndex ^%Target_ix%
^!InsertText ^%NL%
^!SetDocIndex ^%Source_ix%
:NEXT
^!Jump +1
^!If ^$GetRow$ = ^%Bottom% FINISH
^!GoTo START
:FINISH
^!SetDocIndex ^%Target_ix%
^!StatusClose
^!SetScreenUpdate On
^!Keyboard ALTDOWN m l c  ALTUP
; ^!SetWordWrap True
; ^!SetWordWrap False
^!SetCursor 1:1
:END

H=";"


H=";Utilities"

List files:
Create a tab delimited list of the files + date + size of a specified
type in a specified directory. Specify the path and mask.

List do-files + *comments
List the do-files along with header comments delimited by "*".

List ado-files + /*comments*/
List the ado-files along with header comments delimited by /**/.

List all ado in subdirecties
Create a list of all ado files subdirectories a-z of the specified
directory (eg, c:\ado\personal\).

Perl Stata to htm
Convert a Stata .log file on disk to an .htm file using UCLA's
stata2htm and Perl.
See http://www.ats.ucla.edu/stat/stata/stata2htm/default.htm


H=";ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"


H="List files"
;Create a tab-delimited list of filename, date, size
^!Set %dirname%=^?{(T=D)directory=^%dirname%}
^!Set %n%=^$StrSize(^%dirname%)$
^!If ^$StrIndex(^%dirname%;^%n%)$<>\ Next Else GetName
^!Set %dirname%=^%dirname%\
:GetName
^!Set %filetype%=^?{file type=^%filetype%}
^!Set %sortby%=^?{(T=C)Sort By==_Name|Date|Size}
^!Set %name%=^$GetFileFirst(^%dirname%;^%filetype%;^%sortby%)$
^!If ^%name%<>"" Next else Close
:WriteName
^!Set %date%=^$GetFileDate(^%name%)$
^!Set %size%=^$GetFileSize(^%name%)$
^!Set %date%=^$StrCopy(^%date%;7;2)$-^$StrCopy(^%date%;5;2)$-^$StrCopy(^%date%;1;4)$
^!InsertText  ^$StrLower(^%name%)$^t^%date%^t^%size%^p
^!Set %name%=^$GetFileNext$
^!If ^%name%="" Next else WriteName
:Close
^!CloseFileFind

H="List do-files + *comments"
^!If ^%curdirname%="" Next ELSE Skip 1
^!Set %curdirname%="c:\"
^!Set %curdirname%=^?{(T=D)directory=^%curdirname%}
^!InsertText do-files in ^%curdirname% ^p
^!Set %name%=^$GetFileFirst(^%curdirname%;*.do;Name)$
^!If ^%name%<>"" Next else Close
:WriteName
;This could be improved
^!Set %nameonly%=^$GetFileName(^%name%)$
^!InsertText ^p^%name% ^p
;^!InsertText ^p^%nameonly% ^p
^!Set %text%=^$GetFileText(^%name%)$
:Loop
^!Set %n%=^$StrPos(^%NL%;^%text%;false)$
^!Inc %n% 1
^!Set %aline%=^$StrCopy(^%text%;1;^%n%)$
^!If ^$StrCopy(^%aline%;1;1)$ = "*" Next ELSE NextFile
^!InsertText ^%aline%
^!Set %text%=^$StrDelete(^%text%;1;^%n%)$
^!GoTo Loop
:NextFile
^!Set %name%=^$GetFileNext$
^!If ^%name%="" Next else WriteName
:Close
^!CloseFileFind

H="List ado-files + /*comments*/"
;^!SetDebug on
^!If ^%curdirname%="" Next ELSE Skip 1
^!Set %curdirname%="c:\"
^!Set %curdirname%=^?{(T=D)directory=^%curdirname%}
^!InsertText ado-files in ^%curdirname% ^p
^!Set %name%=^$GetFileFirst(^%curdirname%;*.ado;Name)$
^!If ^%name%<>"" Next else Close
:WriteName
^!Set %nameonly%=^$GetFileName(^%name%)$
^!InsertText ^p^%name% ^p
;^!InsertText ^p^%nameonly% ^p
;Find comment section between /* and */ delimiters at the top of a file.
^!Set %text%=^$GetFileText(^%name%)$
^!Set %n1%=^$StrPos("/*";^%text%;false)$
^!If ^%n1%<>0 Next Else NextFile
^!Inc %n1% 1
^!Set %text%=^$StrDelete(^%text%;1;^%n1%)$
^!Set %n2%=^$StrPos("*/";^%text%;false)$
^!Dec %n2% 1
^!Set %aline%=^$StrCopy(^%text%;1;^%n2%)$
^!InsertText ^%aline%
:NextFile
^!Set %name%=^$GetFileNext$
^!If ^%name%="" Next else WriteName
:Close
^!CloseFileFind

H="List all ado in subdirectories"
;List all ado files in subdirectories a-z of specified directory.
^!Set trace on
^!Set %curdirname%=^?{(T=D)directory=c:\ado\}
^!IfPathExist ^%curdirname% Next else Error
^!Set %letter%=95
:Dirloop
^!Set %dirname%=^%curdirname%^$DecToChar(^%letter%)$
^!Set %name%=^$GetFileFirst(^%dirname%;*.ado;Name)$
^!If ^%name%<>"" Next else NextDir
^!InsertText ^p
:WriteName
^!Set %nameonly%=^$GetFileName(^%name%)$
^!InsertText ^%name% ^p
:NextFile
^!Set %name%=^$GetFileNext$
^!If ^%name%="" Next else WriteName
:NextDir
^!Inc %letter%
^!If ^%letter%>122 Next else DirLoop
:Close
^!CloseFileFind
^!Goto End
:Error
^!Prompt Path doesn't exist

H="Format Table for WP"
^!If ^"$GetSelSize$" = "0" EMPTY
^!Set %Table%=^$GetSelection$
^!Set %Table%=^$StrReplace("|";"";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("-";"";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("+";"";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("^%NL%^%NL%";"^%NL%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("         ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("        ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("       ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("      ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("     ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("    ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("   ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("  ";"^%TAB%";"^%Table%";No;No)$
^!Set %Table%=^%NL%^$StrReplace("^%NL% ";"^%NL%";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("^%NL%^%TAB%";"^%NL%";"^%Table%";No;No)$
^!Info [L] ^%Table%
^!ToolBar New Document
^!Keyboard ALT+D ALT+P ALT+S 1 0 ENTER
^!Set %Target%=^$GetDocName$
^%Table%
^!SetWordWrap False
^!ToolBar Copy ALL
^!FocusApp WordPerfect 10*
^!IfError NEXT Else SKIP_3
^!Skip Question "Do you want to start WordPerfect?"
^!C:\Program Files\Corel\2002\Programs\wpwin10.exe
:CLEANUP
;^!Close ^%Target% Discard
:EMPTY

H="Alternate Table Format"
^!If ^"$GetSelSize$" = "0" NONE
^!Set %Table%=^$GetSelection$
^!Info [L] ^%Table%
^!Set %Table%=^$StrReplace("|";"";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("-";"";"^%Table%";No;No)$
^!Set %Table%=^$StrReplace("+";"";"^%Table%";No;No)$
^!Info [L] ^%Table%
^!ToolBar New Document
^!Set %Target%=^$GetDocName$
^%Table%
^!SetWordWrap False
^!Select ALL
^!Keyboard ALTDOWN M S T ALTUP

H=";"


H=";ŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻŻ"


H="Stata Website Links"
^!URL ^?[(H=10)Which
site==_Home^=http://www.stata.com/|FAQ^=http://www.stata.com/support/faqs/|Search^=http://www.stata.com/search/|Site
Index^=http://www.stata.com/site.html|Statalist
Main^=http://www.stata.com/statalist/|Statalist
Archives^=http://www.stata.com/statalist/archive/|User program
archive^=http://ideas.uqam.ca/ideas/data/bocbocode.html]

*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index