Monday, January 3, 2011

TextWrangler and Stata

With the introduction of syntax highlighting and the ability to handle larger do-files, I've started to use the built-in Stata do-file editor more and more in lieu of BBS's TextWrangler.

However, several times a week I still find myself firing up TW for more complex tasks.  Most often it's when I need to show differences 2 or more versions/revisions of code, do a complex find/replace, character substitution, duplicate line deletion, or regular expression search.  I use it for FTP uploads and inspecting/opening text-versions unknown filetypes on my Mac OSX.  And occasionally, I'll open it if I'm working simultaneously on several do-files since the Mac version of Stata 11 still doesn't support tabbed do-files.

I use the TW for Stata scripts found here and I use the customizable shortcut functions in OSX 10.6 to send my do-files or a section of my do-files to Stata from TW.

The only issue I have with the script above is that it's outdated.  The DataNinja site hasn't been updated in a while and I don't expect it to be any time soon, so it's unlikely that I'll get syntax highlighting for new commands that have been created since then.

One way I've found to update the TW scripts is to grab the new "official" syntax commands from the Stata .app package contents (see #1 below) and then grab the list of all other commands/ado-files I've downloaded from SSC or elsewhere (see #2 below) and add all these to the outdated stata.plist file for TW.




To do so, navigate to this directory to find or install the DataNinja stata.plist file:

~/Library/Application Support/TextWrangler/Language Modules

#1:  To add all the updated/official Stata commands from your up-to-date Stata for Mac OSX app:

  1. Navigate to the Stata(version).app, for me it's StataMP.app in this directory: /applications/Stata/StataMP.app
  2. Right-click on the icon and choose "Show Package Contents"
  3. Navigate to  /applications/Stata/StataMP.app/Contents/Resources/syntax.plist
  4. Open this file in a text editor (ideally TW) and copy/paste all the commands (field-tagged with and to your TW stata.plist file described above.
  5. Go to the Text Menu in TW and choose "Process Duplicate Lines" and remove duplicate commands.  (Also, check to make sure an extra tag from the Stata Package "syntax.plist" didn't make it into your stata.plist TW file.)  Save your file.

#2:  To add all the names of the ado-files/commands you have installed on in your path:

  1.  Essentially want to do the steps above again, but this time you need to paste in the name of all the ado-files installed on your machine & in your path.

  2.  You can get these in Stata by typing -ado-; however, this will give you the command name, description, and source site.  So, the output for all the user ado-files you've installed will look something like this:

[1216] package ecol from http://www.mit.ps.au.dk/stata/ecol_dir
      ecol for Stata v0.0
[1217] package _gclsort from http://fmwww.bc.edu/RePEc/bocode/_
      '_GCLSORT': module to sort a single variable via egen
[1218] package intext from http://fmwww.bc.edu/repec/bocode/i
      'INTEXT': module to read text files into string variables



You only want the command name (e.g., -intext- in the example above), which you can extract into the file "adolist.txt" with the ado-file below.

--This adofile will also insert for you the the requisite field-tags around each Stata command.  Also, this ado-file will automatically install these user programs if you don't have them in your path already: -dropmiss- and -intext-.

--You can get this ado-file from here, place it in your path, and then type: -fixTW- to run it.

***********************! Begin
cap program drop fixTW
program define fixTW
syntax [using/]
cap log close _all
 

**install necessary packages** (see ^ftnote)  
cap which intext  
if _rc ssc install intext, replace  
cap which dropmiss if _rc {  
net install dm89.pkg, replace  
  }
**define using text file**
if `"`using'"' == "" loc using adolistforTW.txt
**create temp log file**
tempfile templog
log using "`templog'", replace text name(log)
ado
log close log
clear
intext using "`templog'", gen(i) len(244)
desc, sh
 

cap dropmiss, force  
if _rc dropmiss  
cap dropmiss, obs force  
if _rc dropmiss, obs
g flag = strpos(i1, "[")
drop if flag!=1
split i1, p("] package")
drop i1 flag i11
split i12, p(" ")
keep i121
g i1 = "
"
g i3 = " "
g adolist = i1 + i121 + i3
keep adolist
outsheet using "`using'", replace noq nonames
noi di as smcl `"Open TW code snippet: {browse "`using'"}"'
noi di as smcl `"Add this code to: {browse "/users/`c(username)'/library/application support/textwrangler/language modules/"}"'
end
**run it -->
    fixTW
***********************! End
**watch for wrapping!!


  3.  Now repeat numbers 4 and 5 from #1(above):  Run the do-file below & copy the results from "adolist.txt"  into the TW stata.plist.  JIC, process duplicate lines again.

  4.  Save the updated stata.plist and restart TW.


One downside of this method is that it will grab the package name, which is not necessarily the command name in all instances.  E.g., it will grab -dirtools- rather than -lall-.  Most packages and commands appear to have the same name, so this hasn't bothered me enough to work on a fix.

_____________________
^Note:  I couldn't get the -net install- to install dropmiss.ado from dm89_1.pkg.  So, I put code in place to run both the old and the new dropmiss code (that is, with and  without the "force" option), I'll update this if I (or others help me) resolve this.

4 comments:

  1. This is a fantastic website and I can not recommend you guys enough. I really appreciate your post. It is very helpful for all the people on the web.seo service

    ReplyDelete
  2. Essentially want to do the steps above again, but this time you need to paste in the name of all the ado-files installed on your machine & in your path. email check

    ReplyDelete