Initial Commit
[packages] / xemacs-packages / ess / lisp / essl-sta.el
1 ;;; essl-sta.el --- Stata customization
2
3 ;; Copyright (C) 1999--2000, Thomas Lumley, A. J. Rossini, Brendan Halpin.
4 ;; Copyright (C) 1997--2004 A.J. Rossini, Rich M. Heiberger, Martin
5 ;;      Maechler, Kurt Hornik, Rodney Sparapani, and Stephen Eglen.
6
7 ;; Original Authors: Thomas Lumley <thomas@biostat.washington.edu>,
8 ;;         Brendan Halpin <brendan@essex.ac.uk>
9 ;; Created: 2 Nov 1997
10 ;; Maintainers: ESS-core <ESS-core@stat.math.ethz.ch>
11
12 ;; Keywords: start up, configuration.
13
14 ;; This file is part of ESS (Emacs Speaks Statistics).
15
16 ;; This file is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
19 ;; any later version.
20
21 ;; This file is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 ;; GNU General Public License for more details.
25
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING.  If not, write to
28 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
29
30 ;;; Commentary:
31 ;;; This is based upon Version 0.4 of Stata mode.
32
33
34
35
36 ;;
37 ;; Stata modes.  Emacs modes for using the Stata statistical package
38 ;; Modified from S-mode, comint-mode
39 ;;
40 ;; (c) thomas lumley 1997 
41 ;;
42 ;;  version 0.4  20/7/97
43 ;;
44 ;; This file is free software; you can redistribute it and/or modify
45 ;; it under the terms of the GNU General Public License as published by
46 ;; the Free Software Foundation; either version 2, or (at your option)
47 ;; any later version.
48 ;;
49 ;; This file is distributed in the hope that it will be useful,
50 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
51 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
52 ;; GNU General Public License for more details.
53 ;;
54 ;; You should have received a copy of the GNU General Public License
55 ;; along with GNU Emacs; see the file COPYING.  If not, write to
56 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
57 ;;
58
59
60 (require 'make-regexp)  ; it's now local to the directory.
61 ;;(load-library "make-regexp") ;; this is necessary for
62                              ;; ado-set-font-lock-keywords
63
64 ;(setq max-lisp-eval-depth 500)
65 (eval-when-compile
66   (setq max-lisp-eval-depth (max 600 max-lisp-eval-depth)))
67
68 (defconst ess-help-STA-sec-keys-alist
69   '((?d . "Description")
70     (?e . "Examples")
71     (?o . "Options")
72     (?s . "Also see"))
73   "Help section keys for S4.
74 `key' indicates the keystroke to use to search for the section heading
75 `string' in an Stata help file. `string' is used as part of a
76 regexp-search, and so specials should be quoted.
77 ")
78
79 (defconst ess-help-STA-sec-regex "^[A-Z a-z]+:?\n^[-]+$"
80   "Reg(ular) Ex(pression) of section headers in help file.")
81
82 (defvar STA-syntax-table nil "Syntax table for Stata code.")
83 (if STA-syntax-table
84     nil
85   (setq STA-syntax-table (make-syntax-table))
86   (modify-syntax-entry ?\\ "." STA-syntax-table) ;nullify escape meaning
87   (modify-syntax-entry ?\$ "." STA-syntax-table)
88   (modify-syntax-entry ?` "(\'" STA-syntax-table)
89   (modify-syntax-entry ?\' ")`" STA-syntax-table)
90   (modify-syntax-entry ?/ ". 14" STA-syntax-table)
91   (modify-syntax-entry ?* ". 23" STA-syntax-table)
92   (modify-syntax-entry ?+ "." STA-syntax-table)
93   (modify-syntax-entry ?- "." STA-syntax-table)
94   (modify-syntax-entry ?= "." STA-syntax-table)
95   (modify-syntax-entry ?% "." STA-syntax-table)
96   (modify-syntax-entry ?< "." STA-syntax-table)
97   (modify-syntax-entry ?> "." STA-syntax-table)
98   (modify-syntax-entry ?& "." STA-syntax-table)
99   (modify-syntax-entry ?| "." STA-syntax-table)
100   (modify-syntax-entry ?~ "." STA-syntax-table))
101
102
103 (defun ado-set-font-lock-keywords ()
104   "Create font lock keywords for Stata syntax. This is from the
105 ado-mode of Bill Rising <brising@jhsph.edu>, and uses make-regexp."
106   ;; (make-local-variable 'ado-font-lock-keywords)
107   (interactive)
108   (list
109    ;; special highlighting
110    ;; program definitions
111    (eval-when-compile
112      (make-regexps
113       '(("^\\*!.*") font-lock-keyword-face)
114       ))
115    (eval-when-compile
116      (make-regexps
117       "^"
118       '((
119          "pr" "pro" "prog" "progr" "progra" "program"
120          ) font-lock-keyword-face)
121       "[ \t]+"
122       '((
123          "de" "def" "defi" "defin" "define"
124          "di" "dir"
125          "drop"
126          "l" "li" "lis" "list"
127          ) font-lock-type-face nil)
128       "[ \t]+"
129       '(("[_a-z]+[_a-z0-9]*") font-lock-keyword-face nil)
130       ))
131    (eval-when-compile
132      (make-regexps
133       '(("^[ \t]*version") font-lock-reference-face)
134       "[ \t]*"
135       '(("1.0 2.0 2.1 3.0 3.1 4.0 5.0 6 6.0") font-lock-type-face)
136       ))
137    (eval-when-compile
138      (make-regexps
139       "^"
140       '(("end" "pause"
141          ) font-lock-keyword-face)
142       "[ /t]*.*$"
143       ))
144    ;; delimit command
145    (eval-when-compile
146      (make-regexps
147       '(("^[ \t]*#delimit") font-lock-reference-face)
148       "\\s-*"
149       '(("\\(cr\\|;\\)\\s-*$") font-lock-type-face nil)
150       ))
151    ;; set command (with endless options!)
152    (eval-when-compile
153      (make-regexps
154       '(("^[ \t]*set") font-lock-reference-face)
155       "[ \t]+"
156       '(("adosize" "ANSI" 
157          "b" "be" "bee" "beep" "checksum" "contents" 
158          "d" "di" "dis" "disp" "displ" "displa" "display"
159          "g" "gr" "gra" "grap" "graph" "graphi" "graphic" "graphics"
160          "help"
161          "IBM" 
162          "l" "le" "lev" "leve" "level"
163          "linesize" 
164          "lo" "log"
165          "mat" "mats" "matsi" "matsiz" "matsize"
166          "maxobs" "maxvar" 
167          "mem" "memo" "memor" "memory"
168          "mo" "mor" "more"
169          "obs"
170          "ou" "out" "outp" "outpu" "output"
171          "pagesize" 
172          "r" "rm" "rms" "rmsg"
173          "se" "see" "seed" "seed0" "shell"
174          "te" "tex" "text" "texts" "textsi" "textsiz" "textsize"
175          "tr" "tra" "trac" "trace"
176          "t" "ty" "typ" "type" "video"
177          "vir" "virt" "virtu" "virtua" "virtual"
178          )
179         font-lock-reference-face t)
180       "[ \t]*"
181       '(("[a-zA-Z0-9]*") font-lock-type-face)
182       ))
183    ;; the constraint commands
184    (eval-when-compile
185      (make-regexps
186       "[ \t]+"
187       '((
188          "cons" "const" "constr" "constra" "constrai" "constrain" "constraint" 
189          ) font-lock-reference-face)
190       "[ \t]+"
191       '((
192          "d"
193          "de" "def" "defi" "defin" "define"
194          "di" "dir"
195          "drop"
196          "l" "li" "lis" "list"
197          ) 
198         font-lock-type-face)
199       "\\b"
200       ))
201    ;; the confirm commands - could be a mess!
202    (eval-when-compile
203      (make-regexps
204       "[ \t]+"
205       '((
206          "conf" "confi" "confir" "confirm"
207          ) font-lock-reference-face)
208       "[ \t]+"
209       '((
210          "e" "ex" "exi" "exis" "exist" "existe" "existen" "existenc" "existence"
211          "f" "fi" "fil" "file" 
212          "n" "nu" "num" "numb" "numbe" "number" 
213          "v" "va" "var" "vari" "varia" "variab" "variabl" "variable"
214          ) font-lock-type-face)
215       "\\b"
216       ))
217    (eval-when-compile
218      (make-regexps
219       "[ \t]+"
220       '((
221          "conf" "confi" "confir" "confirm"
222          ) font-lock-reference-face)
223       "[ \t]+"
224       '((
225          "integer"
226          ) font-lock-type-face)
227       "[ \t]+"
228       '((
229          "n" "nu" "num" "numb" "numbe" "number"
230          ) font-lock-type-face)
231       "\\b"
232       ))
233    (eval-when-compile
234      (make-regexps
235       "[ \t]+"
236       '((
237          "conf" "confi" "confir" "confirm"
238          ) font-lock-reference-face)
239       "[ \t]+"
240       '((
241          "n" "ne" "new"
242          ) font-lock-type-face)
243       "[ \t]+"
244       '((
245          "f" "fi" "fil" "file"
246          "v" "va" "var" "vari" "varia" "variab" "variabl" "variable"
247          ) font-lock-type-face)
248       "\\b"
249       ))
250    (eval-when-compile
251      (make-regexps
252       "[ \t]+"
253       '((
254          "conf" "confi" "confir" "confirm"
255          ) font-lock-reference-face)
256       "[ \t]+"
257       '((
258          "byte" "double" "float" "int" "long"
259          "numeric"
260          "str" "stri" "strin" "string"
261          ) font-lock-type-face)
262       "[ \t]+"
263       '((
264          "v" "va" "var" "vari" "varia" "variab" "variabl" "variable" 
265          ) font-lock-type-face)
266       "\\b"
267       ))
268     ;;; the str# won't quite look right, but that's the breaks for using
269     ;;; a tool like this...
270    (eval-when-compile
271      (make-regexps
272       "[ \t]+"
273       '((
274          "conf" "confi" "confir" "confirm"
275          ) font-lock-reference-face)
276       "[ \t]+"
277       '((
278          "str"
279          ) font-lock-type-face)
280       "[1-9]+[0-9]*[ \t]+"
281       '((
282          "v" "va" "var" "vari" "varia" "variab" "variabl" "variable" 
283          ) font-lock-type-face)
284       "\\b"
285       ))
286    ;; the estimates commands
287    (eval-when-compile
288      (make-regexps
289       "[ \t]+"
290       '((
291          "est" "esti" "estim" "estima" "estimat" "estimate" "estimates"
292          ) font-lock-reference-face)
293       "[ \t]+"
294       '((
295          "clear"
296          "di" "dir" "dis" "disp" "displ" "displa" "display"
297          "drop"
298          "h" "ho" "hol" "hold"
299          "li" "lis" "list"
300          "loc" "loca" "local"
301          "mat" "matr" "matri" "matrix"
302          "post"
303          "repost"
304          "sca" "scal" "scala" "scalar" 
305          "u" "un" "unh" "unho" "unhol" "unhold"
306          ) 
307         font-lock-type-face)
308       "\\b"
309       ))
310    ;; the gph commands
311    (eval-when-compile
312      (make-regexps
313       "[ \t]+"
314       '((
315          "gph"
316          ) font-lock-reference-face)
317       "[ \t]+"
318       '((
319          "arc"
320          "box"
321          "clear" "close"
322          "font"
323          "line"
324          "open"
325          "pen" "point"
326          "text"
327          "vline" "vpoint" "vpoly" "vtext"
328          ) 
329         font-lock-type-face)
330       "\\b"
331       ))
332
333    ;; some of the matrix commands
334    (eval-when-compile
335      (make-regexps
336       "[ \t]+"
337       '(("mat" "matr" "matri" "matrix") font-lock-reference-face)
338       "[ \t]+"
339       '(("ac" "acc" "accu" "accum" 
340          "cole" "coleq" 
341          "coln" "colna" "colnam" "cloname" "colnames"
342          "d" "def" "defi" "defin" "define" 
343          "di" "dir" "dispCns" "drop" "drop _all"
344          "glsa" "glsac" "glsacc" "glsaccu" "glsaccum" 
345          "l" "li" "lis" "list" "makeCns" "mlou" "mlout" "post"
346          "rowe" "roweq" 
347          "rown" "rowna" "rownam" "rowname" "rownames"
348          "sco" "scor" "score" 
349          "sub" "subs" "subst" "substi" "substit" "substitu" "substitut" "substitute" 
350          "svd" "syme" "symei" "symeig" "symeige" "symeigen"
351          "veca" "vecac" "vecacc" "vecaccu" "vecaccum"
352          ) 
353         font-lock-type-face)
354       "\\b"
355       ))
356    ;; the ml commands
357    (eval-when-compile
358      (make-regexps
359       "[ \t]+"
360       '(("ml") font-lock-reference-face)
361       "[ \t]+"
362       '(("b" "be" "beg" "begi" "begin" 
363          "check" "count"
364          "de" "dep" "depn" "depna" "depnam" "depname" "depnames" 
365          "di" "dis" "disp" "displ" "displa" "display" 
366          "f" "fu" "fun" "func" "funct" "functi" "functio" "function"
367          "gr" "gra" "grap" "graph"
368          "init"
369          "max" "maxi" "maxim" "maximi" "maximiz" "maximize"
370          "me" "met" "meth" "metho" "method"
371          "ml" "mlo" "mlou" "mlout"
372          "mo" "mod" "mode" "model"
373          "pl" "plo" "plot"
374          "po" "pos" "post" 
375          "q" "qu" "que" "quer" "query"
376          "re" "rep" "repo" "repor" "report"
377          "sa" "sam" "samp" "sampl" "sample"
378          "se" "sea" "sear" "searc" "search"
379          "trace")
380         font-lock-type-face)
381       "\\b"
382       ))
383    ;; the net commands
384    (eval-when-compile
385      (make-regexps
386       "[ \t]+"
387       '(("net") font-lock-reference-face)
388       "[ \t]+"
389       '((
390          "cd"
391          "d" "de" "des" "desc" "descr" "descri" "describ" "describe"
392          "from" "get" "install"
393          "link"
394          "q" "qu" "que" "quer" "query")
395         font-lock-type-face)
396       "\\b"
397       ))
398    (eval-when-compile
399      (make-regexps
400       "[ \t]+"
401       '(("net") font-lock-reference-face)
402       "[ \t]+"
403       '(("set") font-lock-reference-face)
404       "[ \t]+"
405       '(("ado" "other") font-lock-type-face)
406       "\\b"
407       ))
408    (eval-when-compile
409      (make-regexps
410       "[ \t]+"
411       '(("ado") font-lock-reference-face)
412       "[ \t]+"
413       '(("d" "de" "des" "desc" "descr" "descri" "describ" "describe"
414          "dir"
415          "uninstall")
416         font-lock-type-face)
417       "\\b"
418       ))
419    ;; the reshape commands
420    (eval-when-compile
421      (make-regexps
422       "[ \t]+"
423       '(("reshape") font-lock-keyword-face)
424       "[ \t]+"
425       '((
426          "clear"
427          "error"
428          "i" "j"
429          "long"
430          "wide"
431          "xi" "xij")
432         font-lock-type-face)
433       "\\b"
434       ))
435    ;; the return commands
436    (eval-when-compile
437      (make-regexps
438       "[ \t]+"
439       '(("ret" "retu" "retur" "return") font-lock-reference-face)
440       "[ \t]+"
441       '(("add" "clear" "local" "matrix" "scalar") font-lock-type-face)
442       "\\b"
443       ))
444    (eval-when-compile
445      (make-regexps
446       "[ \t]+"
447       '(("sret" "sretu" "sretur" "sreturn") font-lock-reference-face)
448       "[ \t]+"
449       '(("clear" "local") font-lock-type-face)
450       "\\b"
451       ))
452    ;; the sts commands
453    (eval-when-compile
454      (make-regexps
455       "[ \t]+"
456       '(("sts") font-lock-reference-face)
457       "[ \t]+"
458       '((
459          "g"
460          "gen" "gene" "gener" "genera" "generat" "generate"
461          "gr" "gra" "grap" "graph"
462          "l" "li" "lis" "list"
463          "t" "te" "tes" "test"
464          )
465         font-lock-type-face)
466       "\\b"
467       ))
468    ;; the sw commands
469    (eval-when-compile
470      (make-regexps
471       "[ \t]+"
472       '(("sw") font-lock-reference-face)
473       "[ \t]+"
474       '((
475          "cloglog" "cnreg" "cox" "ereg" "gamma" "glm" "gompertz" "hetprob"
476          "llogist" "lnormal" "logistic" "logit" "ologit" "oprobit"
477          "poisson" "probit" "qreg" "reg" "regr" "regre" "regres" "regress"
478          "scobit" "tobit" "weibull"
479          )
480         font-lock-type-face)
481       "\\b"
482       ))
483    ;; the window commands
484    (eval-when-compile
485      (make-regexps
486       "[ \t]+"
487       '((
488          "win" "wind" "windo" "window"
489          ) font-lock-reference-face)
490       "[ \t]+"
491       '((
492          "d"
493          "di" "dia" "dial" "dialo" "dialog"
494          "dir" "drop"
495          "fo" "fop" "fope" "fopen"
496          "fs" "fsa" "fsav" "fsave"
497          "l" "list"
498          "push"
499          "stop" "stopb" "stopbo" "stopbox"
500          ) font-lock-type-face)
501       "\\b"
502       ))
503    ;; the window controls
504    (eval-when-compile
505      (make-regexps
506       "[ \t]+"
507       '((
508          "win" "wind" "windo" "window"
509          ) font-lock-reference-face)
510       "[ \t]+"
511       '((
512          "c" "co" "con" "cont" "contr" "contro" "control"
513          ) font-lock-reference-face)
514       '((
515          "button" "check" "clear"
516          "edit"
517          "mcombo" "msimple"
518          "radbegin"
519          "radend"
520          "radio"
521          "scombo"
522          "ssimple"
523          "static"
524          ) font-lock-type-face)
525       "\\b"
526       ))
527    ;; the window manage commands
528    (eval-when-compile
529      (make-regexps
530       "[ \t]+"
531       '((
532          "win" "wind" "windo" "window"
533          ) font-lock-reference-face)
534       "[ \t]+"
535       '((
536          "man" "mana" "manag" "manage"
537          ) font-lock-reference-face)
538       "[ \t]+"
539       '((
540          "forward"
541          "minimize" 
542          "prefs load" 
543          "prefs save" 
544          "prefs default"
545          "print graph"
546          "print log"
547          "restore"
548          "update variable"
549          ) 
550         font-lock-type-face)
551       "\\b"
552       ))
553    ;; the window menu commands
554    (eval-when-compile
555      (make-regexps
556       "[ \t]+"
557       '((
558          "win" "wind" "windo" "window"
559          ) font-lock-reference-face)
560       "[ \t]+"
561       '((
562          "m" "me" "men" "menu"
563          ) font-lock-reference-face)
564       "[ \t]+"
565       '((
566          "append popout"
567          "append string"
568          "append separator"
569          "clear"
570          "popout"
571          "set"
572          ) 
573         font-lock-type-face)
574       "\\b"
575       ))
576    ;; the xwindow commands
577    (eval-when-compile
578      (make-regexps
579       "[ \t]+"
580       '((
581          "xwin" "xwind" "xwindo" "xwindow"
582          ) font-lock-reference-face)
583       "[ \t]+"
584       '((
585          "de" "def" "defi" "defin" "define" 
586          "di" "dir"
587          "drop"
588          "l" "li" "lis" "list"
589          ) 
590         font-lock-type-face)
591       "\\b"
592       ))
593
594    ;; all the endless Stata keywords (not in a good order)
595    ;; first those keywords which must start line
596    ;; note that these will look like text if preceded by a comment
597    ;; (but comments shouldn't be before the command, anyway)
598
599    (eval-when-compile
600      (make-regexps
601       "^[ \t]+"
602       '((
603          "cap" "capt" "captu" "captur" "capture"
604          "char" "err" "erro" "error" "e" "ex" "exi" "exit" 
605          "par" "pars" "parse" 
606          "set"
607          ) font-lock-reference-face)
608       "\\b"
609       ))
610    ;; here are some keywords which appear in the middle of lines
611    ;; note that the really short abbreviations could make a mess of things
612    ;;
613    ;; These are split to allow compiling!
614    (eval-when-compile
615      (make-regexps
616       "\\b"
617       '((
618          "_huber" "_qreg" "_robust"
619          "acprplot" "adjust" 
620          "adopath" "alpha" 
621          "an" "ano" "anov" "anova" "arch"
622          "areg" "arima" 
623          "as" "ass" "asse" "asser" "assert" 
624          "avplot" "avplots"
625          "bcskew0" 
626          "be" "bee" "beep" 
627          "biprobit" "bitest" "bitesti" "blogit"
628          "boxcox" "bprobit" "br" "break" "brier" 
629          "bro" "brow" "brows" "browse" 
630          "bsqreg" "bstat" "by"
631          "canon" "cat" "cc" "cci" "cchart" "centile" "cf" "ci" "cii" 
632          "clogi" "clogit" "clogitp" "cloglog"
633          "close" "cmdtool" 
634          "cnr" "cnre" "cnreg" "cnsreg" "codebook" "compare" 
635          "copy" 
636          "cor" "corc" "corr" "corre" "correl" "correla" "correlat" "correlate"
637          "corrgram"
638          "cou" "coun" "count" 
639          "cox"  "cprplot" "_crcswxx" "cs" "csi" 
640          "ct" "ctset" "cttost" 
641          "cumul" "cusum")
642         font-lock-reference-face)
643       "\\b"
644       ))
645    (eval-when-compile
646      (make-regexps
647       "[ \t]+"
648       '((
649          "d" "de" "des" "desc" "descr" "descri" "describ" "describe"
650          "dfbeta" "dfuller" "di"
651          "dir" "dis" "disp" "disp_res" "disp_s" 
652          "displ" "displa" "display"
653          "do" "dotplot"
654          "dprobit" "ds" "dstdize" "dwstat"
655          "eivreg" "eq" "ereg"
656          "fac" "fact" "facto" "factor"
657          "fit" "for" "fpredict" 
658          "fracplot" "fracpoly" "fsl"
659          ) font-lock-reference-face)
660       "\\b"
661       ))
662    (eval-when-compile
663      (make-regexps
664       "[ \t]+"
665       '((
666          "gettoken" "gladder" "glm" "glmpred" "glogit" "gnbreg" "gompertz"
667          "gphdot" "gphpen" "graph" "gprobit" "greigen" "grmeanby"
668          "hadimvo" "hausman" "heckman" "heckprob" "hetprob" "hettest" "hilite"
669          "hist" "hlu" "hotel"
670          "iqreg" "istdize" "iis" 
671          "ins" "insp" "inspe" "inspec" "inspect"
672          "integ" "intreg" "ir" "iri" "ivreg"
673          "kap" "kappa" "kapwgt" "kdensity" "ksm" "ksmirnov" "ktau"
674          "kwallis"
675          ) font-lock-reference-face)
676       "\\b"
677       ))
678    (eval-when-compile
679      (make-regexps
680       "[ \t]+"
681       '((
682          "l" "ladder" "lfit" "lincom" "linktest" 
683          "li" "lis" "list"
684          "log"
685          "logistic" 
686          "logi" "logit" 
687          "loneway" "lookfor" 
688          "lo" "loo" "look" "looku" "lookup"
689          "lpredict" "lroc" "lrtest" "ls" "lsens" "lstat" "ltable" "lv" "lvr2plot"
690          "man" "matcproc" "mcc" "mcci"
691          "means" 
692          "mlog" "mlogi" "mlogit"
693          "mor" "more"
694          "mvreg" "mx_param"
695          "n" "nbreg" "newey" "news"
696          "nl" "nlinit" 
697          "no" "noi" "nois" "noisi" "noisil" "noisily"
698          "note" "notes"
699          "nptrend" "numlist"
700          "olog" "ologi" "ologit"
701          "ologitp" 
702          "on" "one" "onew" "onewa" "oneway"
703          "oprob" "oprobi" "oprobit"
704          "oprobitp"
705          "orthog" "orthpoly"
706          "ovtest")
707         font-lock-reference-face)
708       "\\b"
709       ))
710    (eval-when-compile
711      (make-regexps
712       "[ \t]+"
713       '(("pac" "pchart" "pchi" "pcorr" "pergram"
714          "pl" "plo" "plot"
715          "pnorm" "poisgof" "poisson" "pperron"
716          "prais" 
717          "prob" "probi" "probit"
718          "prtest" "prtesti"
719          "pwcorr" "pwd"
720          "q" "qchi" "qnorm" "qqplot" "qreg" "quadchk" "quantile" 
721          "qu" "que" "quer" "query"
722          "qui" "quie" "quiet" "quietl" "quietly"
723          "ranksum" "rchart" "regdw" "regph" 
724          "reg" "reg3" "regr" "regre" "regres" "regress" "reshape"
725          "rot" "rota" "rotat" "rotate"
726          "rreg"
727          "run" "runtest" "rvfplot" "rvpplot"
728          ) font-lock-reference-face)
729       "\\b"
730       ))
731    (eval-when-compile
732      (make-regexps
733       "[ \t]+"
734       '((
735          "sampsi" "sconfirm" 
736          "sco" "scobit" "scor" "score"
737          "sdtest" "sdtesti" "search" "serrbar"
738          "sfrancia" "shell" "shelltool" "shewhart" "signrank" "signtest"
739          "sktest" "slog" "spearman" "spikeplt" "sqreg"
740          "st" "st_is" "st_show" "st_ct"
741          "stcox" "stcoxkm" "stcurv" "stdes"
742          "stem"
743          "stereg" "stir" "stmc" "stmh" "stphplot" "stphtest" 
744          "strate" "streg"
745          "sts" "stse" "stset" "stsum" "stvary" "stweib"
746          "su" "sum" "summ" "summa" "summar" "summari" "summariz" "summarize"
747          "sureg"
748          "svydes" "svyintrg" "svyivreg" "svylc" "svylogit" 
749          "svymean" "svymean" "svymlog" "svyolog" "svyoprob" "svypois" "svyprobt" 
750          "svyprop" "svyratio" "svyreg" "svyset" "svytab" "svytest" "svytotal"
751          "swilk" "symmetry" "symmi" "symplot" "syntax" "sysdir"
752          ) font-lock-reference-face)
753       "\\b"
754       ))
755    (eval-when-compile
756      (make-regexps
757       "[ \t]+"
758       '((
759          "ta" "tab" 
760          "tab1" "tab2" 
761          "tabdisp"
762          "tabi" 
763          "table"
764          "tabu" "tabul" "tabula" "tabulat" "tabulate"
765          "te" "tes" "test"
766          "testnl" "testparm" "tis" 
767          "tob" "tobi" "tobit"
768          "token" "tokeni" "tokeniz" "tokenize" 
769          "touch" "tsreport" "tsset" "tsunab" "ttest" "ttesti"
770          "ty" "typ" "type"
771          "unab" "using"
772          "vce"
773          "verinst" "vif" "vwls"
774          "weibull" "which" "who" "wntestb" "wntestq" 
775          "xchart" "xcorr"
776          "xtclog" "xtdes" "xtgee" "xtgls" "xthaus" "xtintreg" 
777          "xtlogit" "xtnbreg" "xtpois" "xtprobit"
778          "xtrchh" "xtreg" "xtsum" "xttab" "xttest0" "xttobit" "xttrans"
779          "zip" "zinb"
780          ) font-lock-reference-face)
781       "\\b"
782       ))
783
784    ;; conditional statements 
785    ;; if might not work right ('cuz it is also a keyword)
786    (eval-when-compile
787      (make-regexps
788       "^[ \t]*\\sw+[ \t]*"
789       '(("if"
790          ) font-lock-reference-face t t)
791       "\\b"
792       ))
793
794    (eval-when-compile
795      (make-regexps
796       "^[ \t]*"
797       '(("if" "while"
798          ) font-lock-reference-face t t)
799       "[ \t]+.*{"
800       ))
801    ;; else statement (which must just have a {)
802    (eval-when-compile
803      (make-regexps
804       "^[ \t]*"
805       '(("else"
806          ) font-lock-reference-face)
807       "[ \t]*{"
808       ))
809
810    ;; short version of list --- which can get fooled if used as a var
811    (eval-when-compile
812      (make-regexps
813       '(("^[ \t]*l\\b" 
814          ) font-lock-reference-face)
815       ))
816
817    ;; all the Stata options
818    ;; commonly used options
819    (eval-when-compile
820      (make-regexps
821       "[ \t]+"
822       '(("byte" "int" "long" "str[1-9]+[0-9]?" "float" "double"
823          "width" "maxobs" "maxvar"
824          ) font-lock-type-face)
825       "[ \t]+"
826       ))
827    ;; special local variables (used in parsing)
828    (eval-when-compile
829      (make-regexps
830       "^[ \t]+\\(local\\)+[ \t]+"
831       '(("varlist" "exp" "weight" "if" "in" "using" "options"
832          ) font-lock-type-face nil t t)
833       "\\b"
834       ))
835
836    ;; things used with display
837    ;; since these are often split across lines, and Stata commands are hard
838    ;; to delimit, this will highlight even if out of context
839     
840    (eval-when-compile
841      (make-regexps
842       "[ \t]+"
843       '((
844          "_c" "_co" "_con" "_cont" "_conti" "_contin" "_continu" "_continue"
845          "_n" "_ne" "_new" "_newl" "_newli" "_newlin" "_newline"
846          "_quote"
847          "_r" "_re" "_req" "_requ" "_reque" "_reques" "_request"
848          ) 
849         font-lock-type-face)
850       "\\b"
851       ))
852    (eval-when-compile
853      (make-regexps
854       "[ \t]+"
855       '((
856          "_col" "_colu" "_colum" "_column"
857          "_d" "_du" "_dup"
858          "_s" "_sk" "_ski" "_skip"
859          ) 
860         font-lock-type-face)
861       "([1-9]+[0-9]*)\\b"
862       ))
863    (eval-when-compile
864      (make-regexps
865       "\\bin[ \t]+"
866       '((
867          "b" "bl" "blu" "blue" 
868          "g" "gr" "gre" "gree" "green"
869          "r" "re" "red"
870          "w" "wh" "whi" "whit" "white"
871          "y" "ye" "yel" "yell" "yello" "yellow"
872          ) font-lock-type-face)
873       "\\b"
874       ))
875
876    ;; labels
877    (eval-when-compile
878      (make-regexps
879       "[ \t]+"
880       '(("lab" "labe" "label"
881          ) font-lock-reference-face t)
882       "[ \t]+"
883       '((
884          "da" "dat" "data"
885          "de" "def" "defi" "defin" "define"
886          "di" "dir" 
887          "drop" 
888          "l" "li" "lis" "list"
889          "save"
890          "val" "valu" "value" "values"
891          "var" "vari" "varia" "variab" "variabl" "variable"
892          ) font-lock-type-face nil t t)
893       "[ \t]"
894       ))
895
896    ;; all Stata data-altering stuff
897    (eval-when-compile
898      (make-regexps
899       "\\b"
900       '((
901          "_pctile" "_predict"
902          "aorder" "append" 
903          "bcskew0" "bsample" "bs" "bstrap"
904          "cd" "chdir" "clear" "compress" 
905          "contract" "convert" "cross"
906          "dec" "deco" "decod" "decode"
907          "discard" "drop" "dydx"
908          "ed" "edi" "edit" "egen" 
909          "en" "enc" "enco" "encod" "encode"
910          "erase"
911          "expand"
912          "fillin"
913          "form" "forma" "format"
914          "fracgen" "fracpred"
915          "g" "ge" "gen" "gene" "gener" "genera" "generat" "generate"
916          "gsort"
917          "impute" 
918          "infile" "infix" "input" "insheet" "integ" "ipolate"
919          "joinby"
920          "keep" 
921          "lnskew0"
922          ) font-lock-keyword-face)
923       "\\b"
924       ))
925    (eval-when-compile
926      (make-regexps
927       "\\b"
928       '((
929          "mark" "markout" "marksample"
930          "matname" 
931          "mer" "merg" "merge"
932          "mkdir" "mkmat" "mkspline"
933          "mleval" "mlmatsum" "mlsum""mlvecsum"
934          "modify" "mov" "move"
935          "mvdecode" "mvencode" "nlpred" "nobreak" "order" 
936          "ou" "out" "outf" "outfi" "outfil" "outfile"
937          "outs" "outsh" "outshe" "outshee" "outsheet"
938          "pctile" 
939          "post" "postclose" "postfile" 
940          "pre" "pred" "predi" "predic" "predict"
941          "preserve" "range"
942          "recast" "recode" 
943          "ren" "rena" "renam" "rename"
944          "renpfix" "replace" "restore" "rm"
945          "sappend" 
946          "sa" "sav" "save"
947          "sample" "sdrop"
948          "separate"
949          "simul" "sinfile" "smerge" 
950          "smooth" "snapspan" 
951          "so" "sor" "sort"
952          "ssave" "ssort" "stack" 
953          "stbase" "stfill" "stgen" "stjoin" "stsplit" "sttocc" "sttoct"
954          "suse" "svmat"
955          "tsfill" "tsrevar"
956          "u" "us" "use"
957          "xi" "xi:" "xtile" "xpose" 
958          "xtdata" "xtpred"
959          ) font-lock-keyword-face)
960       "\\b"
961       ))
962
963    ;; assignment of macros
964    (eval-when-compile
965      (make-regexps
966       "^[ \t]*"
967       '(("global" "local" "scalar"
968          ) font-lock-reference-face)
969       '(("\\([ \t]+[a-zA-Z_]+[a-zA-Z_0-9]*\\b\\)?"
970          ) font-lock-variable-name-face t)
971       ))
972    ;; choosing temp names
973    (eval-when-compile
974      (make-regexps
975       "^[ \t]*"
976       '(("tempname" "tempfile" "tempvar"
977          ) font-lock-reference-face)
978       '(("\\([ \t]+[a-zA-Z_]+[a-zA-Z_0-9`']*\\)+"
979          ) font-lock-type-face t)
980       ))
981    ;; all variable/macro stuff (put late so it will override)
982    ;; internal constants
983    (eval-when-compile
984      (make-regexps
985       "[^a-zA-Z]"
986       '(("_merge" "_n" "_pi" "_rc" "_N"
987          ) font-lock-variable-name-face)
988       "[^a-zA-Z]"
989       ))
990    ;; some generated vars
991    (eval-when-compile
992      (make-regexps
993       '(("_result([1-9]+)"
994          ) font-lock-variable-name-face)
995       ))
996    ;; global macros
997    (eval-when-compile
998      (make-regexps
999       '(("\\$[a-zA-Z_*]+[a-zA-Z_0-9]*"
1000          ) font-lock-variable-name-face t)
1001       ))
1002    ;; local macros
1003    (eval-when-compile
1004      (make-regexps
1005       "`+"
1006       '(("[a-zA-Z_`*]+[a-zA-Z_0-9]*"    ;has glitch interior ` is highlighted
1007          ) font-lock-variable-name-face t)
1008       "'+"
1009       ))
1010    ;; other macro commands
1011    (eval-when-compile
1012      (make-regexps
1013       "[ \t]*"
1014       '((
1015          "ma" "mac" "macr" "macro"
1016          ) font-lock-reference-face)
1017       "[ \t]+"
1018       '((
1019          "de" "def" "define" 
1020          "di" "dir"
1021          "drop" 
1022          "l" "li" "lis" "list"
1023          "s" "sh" "shi" "shif" "shift"
1024          )
1025         font-lock-type-face)
1026       "[ \t]+"
1027       ))
1028    ;; stata 'functions' i.e. things which require () after them
1029    (eval-when-compile
1030      (make-regexps
1031       "\\b"
1032       '(("_caller"
1033          "abs" "acos" "asin" "atan" "autocode"
1034          "Binomial"
1035          "binorm"
1036          "chiprob" "comb" "cond" "cos" 
1037          "d" "date" "digamma" "day" 
1038          "dofh" "dofm" "dofq" "dofw" "dofy" "dow" "doy"
1039          "e" "exp"
1040          "float" "fprob" "gammap" "get" "group" 
1041          "h" "halfyear" "halfyearly" "hofd"
1042          "ibeta" "index" "int"
1043          "invbinomial" "invchi" "invfprob" "invgammap" "invnchi" "invnorm" "invt"
1044          "length" "ln" "lnfact" "lngamma" "log" "log10" "lower" "ltrim" 
1045          "m" "matrix" "max" "mdy" "min" "missing" "mod" "mofd" "month" "monthly"
1046          "nchi" "normd" "normprob" "npnchi"
1047          "q" "qofd" "quarter" "quarterly"
1048          "r" "real" "recode" "reldif" "replay" "return" "round" "rtrim" 
1049          "s" "scalar" "sign" "sin" "sqrt" "string" "substr" "sum" 
1050          "tan" "tprob" "trigamma" "trim"
1051          "uniform" "uniform0" "upper"
1052          "w" "week" "weekly" "wofd"
1053          "y" "year" "yearly" "yh" "ym" "yofd" "yq" "yw"
1054          )
1055         font-lock-reference-face t)
1056       "("
1057       ))
1058    ;; stata 'functions' i.e. things which require [] after them
1059    (eval-when-compile
1060      (make-regexps
1061       "\\b"
1062       '(("_b" "_coef" "_se")
1063         font-lock-reference-face t)
1064       "\\["
1065       ))
1066    ;; common Stata options which require a () after them
1067    (eval-when-compile
1068      (make-regexps
1069       "[, \t]+"
1070       '(("bands" "by" "connect" "density" "gap" "iterate" "ltolerance" "margin"
1071          "psize" "saving" "tlabel" "tolerance"
1072          "xlabel" "xscale" "ylabel" "yscale")
1073         font-lock-type-face t)
1074       "("
1075       ))
1076    ;; egen 'function' options
1077    (eval-when-compile
1078      (make-regexps
1079       "[ \t]*egen[ \t]+.*=[ \t]*"
1080       '(("count" "diff" "fill" "group" "iqr" 
1081          "ma" "max" "mean" "median" "min" "mtr" "pctile" 
1082          "rank" "rfirst" "rlast" "rmax" "rmean" "rmin" "rmiss" "robs" "rsd" "rsum" 
1083          "sd" "std" "sum")
1084         font-lock-reference-face t)
1085       "(.*)"
1086       ))
1087    ;; All Custom ado files which are 'reliable' and which are not file killers
1088    ;; this might be a useless endeavor --- but I cannot generate tag files
1089    ;; all the s-extensions are listed under Stata's name (since they alter
1090    ;; data and will be moved tot he utils directory
1091    (eval-when-compile
1092      (make-regexps
1093       "[ \t]*"
1094       '(("addnote" "anypath" "autolab" "checkvar" "ck1icd9" "ckicd9"
1095          "datetoe" "dattomdy" "den2dem" "dishis" "dtapath" "dupclean" "echo"
1096          "exdupbil" "ezip2hsa" "getdate" "getlbl" "getnames" "getobs" "gplur"
1097          "icd9" "issorted" "isfile" "jultoe" "jultof" "jultomdy" "knowndup"
1098          "labeldir" "linker" 
1099          "markit" "makewide" "missize" "mpcounts" 
1100          "nodups" "notefile" "prov2zip"
1101          "qcolsum" "qorder" 
1102          "random" "readraw" "readzip" "repart"
1103          "setup" "stdrate"
1104          "timeslot"
1105          "wdatetoe" "wdatomdy" "zip2ezip" 
1106          "_addext" "_brclean" "_brckado" "_brdlog"
1107          "_ckbad" "_ckdunno" "_ckdupl" "_ckmiss" "_ckok" "_ckwarn"
1108          "_delimit" "_filenm" "_lookup" "_mk_ck"
1109          ) font-lock-function-name-face)
1110       "\\b"
1111       ))
1112    ))
1113
1114
1115 (defvar ess-STA-mode-font-lock-keywords (ado-set-font-lock-keywords)
1116   "Set the Stata mode font-lock keywords to Bill Rising's ado-mode keywords.")
1117
1118 (defvar STA-editing-alist
1119   '((paragraph-start              . (concat "^$\\|" page-delimiter))
1120     (paragraph-separate           . (concat "^$\\|" page-delimiter))
1121     (paragraph-ignore-fill-prefix . t)
1122     (require-final-newline        . t)
1123     (comment-start                . "/\* ")
1124     (comment-end                  . " \*/")
1125     (comment-start-skip           . "/\\*+ *")
1126     (comment-column               . 40)
1127     ;;(comment-indent-function      . 'S-comment-indent)
1128     ;;(ess-comment-indent           . 'S-comment-indent)
1129     ;;(ess-indent-line              . 'S-indent-line)
1130     ;;(ess-calculate-indent         . 'S-calculate-indent)
1131     (indent-line-function         . 'S-indent-line)
1132     (parse-sexp-ignore-comments   . t)
1133     (ess-set-style                . ess-default-style)
1134     (ess-local-process-name       . nil)
1135     ;;(ess-keep-dump-files          . 'ask)
1136     (ess-mode-syntax-table        . STA-syntax-table)
1137     (font-lock-defaults           . '(ess-STA-mode-font-lock-keywords
1138                                       nil nil ((?\. . "w")))))
1139   "General options for editing Stata do and ado source files.")
1140
1141 ;; YOU USED TO HAVE TO (with Thomas's version): 
1142 ;;;;; Add the following to your .emacs file
1143 ;;
1144 ;;(autoload 'stata "~/essl-sta.el" "inferior stata mode" t )
1145 ;;(autoload 'stata-help "stata" "stata help mode" t)
1146 ;;(autoload 'stata-mode "~/essl-sta.el" "stata mode" t)
1147 ;;
1148 ;;(if (assoc "\\.do$" auto-mode-alist) nil
1149 ;;  (setq auto-mode-alist
1150 ;;      (append 
1151 ;;       '(("\\.do$" . stata-mode)
1152 ;;         ("\\.ado$" . stata-mode))
1153 ;;       auto-mode-alist)))
1154 ;;
1155
1156
1157 ;; QUESTIONS TO ASK THOMAS:
1158 ;; 1 - are 'help' and 'lookup' the same?
1159 ;; 2 - what is the point of the review buffer?
1160 ;; 3 - how to quit?
1161
1162 ;;
1163 ;; NOTE: all of Thomas's functions have been left here, to be removed
1164 ;; or merged into real locations as we work on this.
1165 ;;
1166
1167
1168 ;;;;;;;;; Things to change 
1169
1170 (defvar stata-switches "-q" 
1171   "*Switches to apply to stata invocation.")
1172
1173 (defvar stata-profile "~/.stataprofile"  
1174   "File to read on startup (nil for no file).")
1175
1176 ;;;;;;;;;;;;;;; 
1177
1178 ;;(require 'comint)
1179
1180 (defun stata-help (the-subject) 
1181   "Stata help in other buffer."
1182   (interactive "sHelp on: ")
1183   (let* ((stata-process (get-process "stata"))
1184          (stata-buffer (process-buffer stata-process))
1185          oldpf oldpb oldpm)
1186     (set-buffer stata-buffer)
1187     (setq oldpf (process-filter stata-process))
1188     (setq oldpb (process-buffer stata-process))
1189     (setq oldpm (marker-position (process-mark stata-process)))
1190     (save-excursion
1191       (if stata-process nil (error "Stata is not running."))
1192       (beginning-of-line)
1193       (if (looking-at ". ") nil  (error "Stata not ready."))
1194       (save-excursion
1195         (set-process-buffer stata-process (get-buffer-create "*stata help*"))
1196         (set-buffer "*stata help*")
1197         (setq buffer-read-only nil)
1198         (set-process-filter stata-process 'ordinary-insertion-filter)
1199         (erase-buffer)
1200         (process-send-string stata-process "help ")
1201         (process-send-string stata-process the-subject)
1202         (process-send-string stata-process "\n")
1203         (stata-prompt-wait stata-process)
1204         ;;(stata-help-mode)
1205         (set-buffer stata-buffer)
1206         (set-process-buffer stata-process oldpb)
1207         (set-process-filter stata-process oldpf)
1208         (set-marker (process-mark stata-process) oldpm)))
1209     (display-buffer "*stata help*")))
1210   
1211 (defun stata-lookup (the-subject) "Stata lookup in other buffer"
1212   (interactive "sLook up: ")
1213   (let* ((stata-process (get-process "stata"))
1214          (stata-buffer (process-buffer stata-process))
1215          oldpf oldpb oldpm)
1216     (set-buffer stata-buffer)
1217     (setq oldpf (process-filter stata-process))
1218     (setq oldpb (process-buffer stata-process))
1219     (setq oldpm (marker-position (process-mark stata-process)))
1220     (save-excursion
1221       (if stata-process nil (error "Stata is not running."))
1222       (beginning-of-line)
1223       (if (looking-at ". ") nil  (error "Stata not ready."))
1224       (save-excursion
1225         (set-process-buffer stata-process (get-buffer-create "*stata help*"))
1226         (set-buffer "*stata help*")
1227         (setq buffer-read-only nil)
1228         (set-process-filter stata-process 'ordinary-insertion-filter)
1229         (erase-buffer)
1230         (process-send-string stata-process "lookup ")
1231         (process-send-string stata-process the-subject)
1232         (process-send-string stata-process "\n")
1233         (stata-prompt-wait stata-process)
1234         (stata-help-mode)
1235         (set-buffer stata-buffer)
1236         (set-process-buffer stata-process oldpb)
1237         (set-process-filter stata-process oldpf)
1238         (set-marker (process-mark stata-process) oldpm)))
1239     (display-buffer "*stata help*")))
1240   
1241 (defun stata-variables () 
1242   "Stata variable list in other buffer."
1243   (interactive)
1244   (let* ((stata-process (get-process "stata"))
1245          (stata-buffer (if stata-process
1246                            (process-buffer stata-process)
1247                          (error "Stata is not running.")))
1248          oldpf oldpb oldpm)
1249     (set-buffer stata-buffer)
1250     (setq oldpf (process-filter stata-process))
1251     (setq oldpb (process-buffer stata-process))
1252     (setq oldpm (marker-position (process-mark stata-process)))
1253     (save-excursion
1254       (if stata-process nil (error "Stata is not running."))
1255       (beginning-of-line)
1256       (if (looking-at ". ") nil  (error "Stata not ready."))
1257        (save-excursion
1258         (set-process-buffer stata-process
1259                             (get-buffer-create "*stata variables*"))
1260         (set-process-filter stata-process 'ordinary-insertion-filter)
1261         (set-buffer "*stata variables*")
1262         (setq buffer-read-only nil)
1263         (erase-buffer)
1264         (process-send-string stata-process "desc \n ")
1265         (stata-prompt-wait stata-process)
1266         (setq buffer-read-only t)
1267         (set-buffer stata-buffer)
1268         (set-process-buffer stata-process oldpb)
1269         (set-marker (process-mark stata-process) oldpm)
1270         (set-process-filter stata-process oldpf)))
1271     (display-buffer "*stata variables*")
1272     (goto-char (point-max))))
1273
1274 (defun stata-review-window ()
1275   (interactive)
1276   (display-buffer "*stata review*"))
1277
1278 (defun stata-rehelp () 
1279   (interactive)
1280   (stata-help (current-word)))
1281
1282 (defun ordinary-insertion-filter (proc string)
1283   (let ((old-buffer (current-buffer)))
1284     (unwind-protect
1285         (let (moving)
1286           (set-buffer (process-buffer proc))
1287           (setq moving (= (point) (process-mark proc)))
1288           (save-excursion
1289             ;; Insert the text, moving the process-marker.
1290             (goto-char (process-mark proc))
1291             (insert string)
1292             (set-marker (process-mark proc) (point)))
1293           (if moving (goto-char (process-mark proc))))
1294       (set-buffer old-buffer))))
1295
1296       
1297 ;;;; <IGNORE>
1298 ;;; This doesn't do anything at the moment.  I have vague plans of
1299 ;;; implementing a menu interface using emacs
1300 ;;;
1301 (defun stata-watch-for-menu-filter (proc string)
1302   (if (string-match "^!!!window!!!" string)
1303       (stata-handle-menu-code proc string)
1304     (comint-output-filter proc string)))
1305
1306 (defun stata-handle-menu-code (proc string)
1307    (let ((old-buffer (current-buffer)))
1308     (unwind-protect
1309         (let (moving)
1310           (set-buffer (process-buffer proc))
1311           (setq moving (= (point)
1312                           (process-mark proc)))
1313           (save-excursion
1314             ;; Insert the text, moving the process-marker.
1315             (goto-char (process-mark proc))
1316             (insert "Handling menu code\n")
1317             (set-marker (process-mark proc) (point)))
1318           (if moving (goto-char (process-mark proc))))
1319       (set-buffer old-buffer))))
1320
1321 ;;;; </IGNORE>
1322     
1323 (defun stata-add-to-review-buffer (string)
1324   "Adds input to review buffer."
1325   (save-excursion
1326     (set-buffer (get-buffer-create "*stata review*"))
1327     (goto-char (point-max))
1328     (insert string)))
1329
1330 (defun stata-prompt-wait (proc &optional start-of-output)
1331   "Wait for a prompt to appear at BOL of current buffer.
1332 PROC is the stata process. Does not change point."
1333   (if start-of-output nil (setq start-of-output (point-min)))
1334   (save-excursion
1335     (while (progn
1336              ;; get output if there is some ready
1337              (accept-process-output proc 0 50) 
1338              (goto-char (marker-position (process-mark proc)))
1339              (beginning-of-line)
1340              (if (< (point) start-of-output) (goto-char start-of-output))
1341              (not (looking-at "^. "))))))
1342
1343 ;;(defvar inferior-stata-mode-map nil
1344 ;;  "Keymap for Stata mode")
1345
1346 ;;(setq inferior-stata-mode-map (cons 'keymap comint-mode-map))
1347 ;;(define-key inferior-stata-mode-map "\M-\t" 'comint-replace-by-expanded-filename)
1348 ;;(define-key inferior-stata-mode-map "\C-c\C-v" 'stata-variables)
1349 ;;(define-key inferior-stata-mode-map "\C-c\C-h" 'stata-help)
1350 ;;(define-key inferior-stata-mode-map "\C-c\C-u" 'stata-lookup)
1351 ;;(define-key inferior-stata-mode-map "\C-c\C-r"   'stata-review-window)
1352 ;;(define-key inferior-stata-mode-map [menu-bar stata] 
1353 ;;  (cons "Stata" (make-sparse-keymap "Stata")))
1354 ;;(define-key inferior-stata-mode-map [menu-bar stata statahelp]
1355 ;;  '("Help on..." . stata-help))
1356 ;;(define-key inferior-stata-mode-map [menu-bar stata lookup]
1357 ;;  '("Look up..." . stata-lookup))
1358 ;;(define-key inferior-stata-mode-map [menu-bar stata variables]
1359 ;;  '("Variables" . stata-variables))
1360 ;;(define-key inferior-stata-mode-map [menu-bar stata review]
1361 ;;  '("Review" . stata-review-window))
1362
1363
1364 ;;(defvar stata-mode-map nil
1365 ;;  "Keymap for Stata mode")
1366   
1367 ;;(setq stata-mode-map (make-sparse-keymap))
1368 ;;(define-key stata-mode-map "\C-c\C-r"    'stata-eval-region)
1369 ;;(define-key stata-mode-map "\C-c\M-r" 'stata-eval-region-and-go)
1370 ;;(define-key stata-mode-map "\C-c\C-b"    'stata-eval-buffer)
1371 ;;(define-key stata-mode-map "\C-c\M-b" 'stata-eval-buffer-and-go)
1372 ;;(define-key stata-mode-map "\C-c\C-f"    'stata-eval-function)
1373 ;;(define-key stata-mode-map "\C-c\C-n"     'stata-eval-line-and-next-line)
1374 ;;(define-key stata-mode-map "\C-c\C-j"    'stata-eval-line)
1375 ;;(define-key stata-mode-map "\C-c\C-r"   'stata-review-window)
1376 ;;(define-key stata-mode-map "\C-c\M-j" 'stata-eval-line-and-go)
1377 ;;(define-key stata-mode-map "\C-c\C-y"    'stata-switch-to-stata)
1378 ;;(define-key stata-mode-map "\C-c\C-z" 'stata-switch-to-end-of-stata)
1379 ;;;;(define-key stata-mode-map "\C-c\C-l"    'stata-load-file)
1380 ;;(define-key stata-mode-map "\C-c\C-h"    'stata-help)
1381 ;;(define-key stata-mode-map "\C-c\C-v"    'stata-variables)
1382 ;;(define-key stata-mode-map "\M-\t" 'comint-replace-by-expanded-filename)
1383 ;;(define-key stata-mode-map "\177" 'backward-delete-char-untabify)
1384 ;;(define-key stata-mode-map "\C-c\C-u" 'stata-lookup)
1385 ;;(define-key stata-mode-map [menu-bar stata] 
1386 ;;  (cons "Stata" (make-sparse-keymap "Stata")))
1387 ;;(define-key stata-mode-map [menu-bar stata lookup]
1388 ;;  '("Look up..." . stata-lookup))
1389 ;;(define-key stata-mode-map [menu-bar stata statahelp]
1390 ;;  '("Help on..." . stata-help))
1391 ;;(define-key stata-mode-map [menu-bar stata variables]
1392 ;;  '("Variables" . stata-variables))
1393 ;;(define-key stata-mode-map [menu-bar stata review]
1394 ;;  '("Review" . stata-review-window))
1395 ;;(define-key stata-mode-map [menu-bar stata eval-line]
1396 ;;  '("Eval line" . stata-eval-line))
1397 ;;(define-key stata-mode-map [menu-bar stata eval-next]
1398 ;;  '("Eval line and next line" . stata-eval-line-and-next-line))
1399 ;;(define-key stata-mode-map [menu-bar stata eval-go]
1400 ;;  '("Eval line and go" . stata-eval-line-and-go))
1401 ;;(define-key stata-mode-map [menu-bar stata eval-buff]
1402 ;;  '("Eval buffer" . stata-eval-buffer))
1403 ;;(define-key stata-mode-map [menu-bar stata eval-buff-go]
1404 ;;  '("Eval buffer and go" . stata-eval-buffer-and-go))
1405 ;;(define-key stata-mode-map [menu-bar stata to-stata]
1406 ;;  '("Switch to stata" . stata-switch-to-stata))
1407 ;;
1408 ;;
1409 ;;
1410
1411 ;(defvar stata-help-mode-map nil)
1412 ;(setq stata-help-mode-map (cons 'keymap help-mode-map))
1413 ;(define-key stata-help-mode-map [mouse-2] 'stata-rehelp)
1414 ;(define-key stata-help-mode-map "\C-c\C-r" 'stata-rehelp)
1415 ;(define-key stata-help-mode-map "\C-c\C-h" 'stata-help)
1416 ;(define-key stata-help-mode-map [menu-bar stata] 
1417 ;  (cons "Stata" (make-sparse-keymap "Stata")))
1418 ;(define-key stata-help-mode-map [menu-bar stata statahelp]
1419 ;  '("Help on..." . stata-help))
1420 ;(define-key stata-help-mode-map [menu-bar stata rehelp]
1421 ;  '("rehelp (hyperlink)" . stata-rehelp))
1422 ;;
1423
1424
1425 ;;(defun inferior-stata-mode ()
1426 ;;"Major mode for running Stata. Based on comint-mode.
1427 ;;Features include Help (\\[stata-help]), Review (\\[stata-review-window]) and
1428 ;;Variables (\\[stata-variables]) mimicking the help, review and 
1429 ;;variables windows of Stata for Windows
1430 ;;\\{inferior-stata-mode-map}"
1431 ;;  (interactive)
1432 ;;  (make-comint "stata" "stata" 
1433 ;;             (and stata-profile
1434 ;;                  (or (file-exists-p stata-profile)
1435 ;;                      (null (message "Startup file %s not found."
1436 ;;                                     stata-profile))) stata-profile)
1437 ;;             stata-switches)
1438 ;;  (switch-to-buffer "*stata*" )
1439 ;;  (setq comint-process-echoes t)
1440 ;;  (set-process-filter (get-process "stata") 'stata-watch-for-menu-filter)
1441 ;;  (setq comint-input-filter-functions
1442 ;;      (cons 'stata-add-to-review-buffer comint-input-filter-functions))
1443 ;;  (save-excursion
1444 ;;    (set-buffer (get-buffer-create "*stata review*"))
1445 ;;    (stata-mode))
1446 ;;  (setq major-mode 'inferior-stata-mode)
1447 ;;  (setq mode-name "inferior Stata")
1448 ;;  (use-local-map inferior-stata-mode-map))
1449 ;;
1450 ;;(defun stata ()
1451 ;;  (interactive)
1452 ;;  (inferior-stata-mode))
1453 ;;
1454
1455 (defun stata-help-mode ()
1456   "Major mode for displaying Stata help in a read-only buffer. 
1457 Active commands are Help (\\[stata-help]) and hyperlink
1458 (\\[stata-rehelp] or mouse-2)." 
1459   (interactive)
1460   (setq major-mode 'stata-help-mode)
1461   (setq mode-name "Stata help")
1462   ;;(use-local-map stata-help-mode-map)
1463   (setq buffer-read-only t))
1464
1465 ;;
1466 ;;
1467 ;;(defun stata-mode ()
1468 ;;"Major mode for editing Stata files. Commands for sending lines to
1469 ;;Stata (\\[stata-eval-line], \\[stata-eval-line-and-go],
1470 ;;\\[stata-eval-line-and-next-line]) 
1471 ;;and for displaying Stata help (\\[stata-help]), variables (\\[stata-variables])
1472 ;; and review window (\\[stata-review-window])
1473 ;;\\{stata-mode-map}"
1474 ;;  (interactive)
1475 ;;  (kill-all-local-variables)
1476 ;;  (setq major-mode 'stata-mode)
1477 ;;  (setq mode-name "Stata")
1478 ;;  (use-local-map stata-mode-map))
1479 ;;
1480 ;;
1481 ;;(defun stata-eval-region (start end)
1482 ;;  "Send the current region to the inferior stata process."
1483 ;;  (interactive "r")
1484 ;;  (process-send-region "stata" start end)
1485 ;;  (process-send-string "stata" "\n"))
1486
1487
1488
1489 ;;(defun stata-eval-buffer ()
1490 ;;  "Send the current buffer to the inferior stata process."
1491 ;;  (interactive)
1492 ;;  (stata-eval-region (point-min) (point-max)))
1493
1494 ;;(defun stata-eval-line ()
1495 ;;  "Send the current line to the inferior stata process."
1496 ;;  (interactive)
1497 ;;  (save-excursion
1498 ;;    (end-of-line)
1499 ;;    (let ((end (point)))
1500 ;;      (beginning-of-line)
1501 ;;      (stata-eval-region (point) end))))
1502
1503 ;;(defun stata-eval-line-and-next-line ()
1504 ;;  "Evaluate the current line  and move to the next line."
1505 ;;  ;; From an idea by Rod Ball (rod@marcam.dsir.govt.nz)
1506 ;;  (interactive)
1507 ;;  (display-buffer (process-buffer (get-process "stata")))
1508 ;;  (save-excursion
1509 ;;    (end-of-line)
1510 ;;    (let ((end (point)))
1511 ;;      (beginning-of-line)
1512 ;;      ;; RDB modified to go to end of S buffer so user can see result
1513 ;;      ;;(stata-eval-visibly (buffer-substring (point) end) nil t)))
1514 ;;      (stata-eval-region (point) end))) 
1515 ;;  (next-line 1))
1516
1517
1518 ;;(defun stata-eval-region-and-go (start end )
1519 ;;  "Send the current region to the inferior S and switch to the process buffer."
1520 ;;  (interactive "r\nP")
1521 ;;  (stata-eval-region start end)
1522 ;;  (stata-switch-to-stata t))
1523
1524 ;;(defun stata-eval-buffer-and-go ()
1525 ;;  "Send the current buffer to the inferior stata and switch to the process buffer."
1526 ;;  (interactive)
1527 ;;  (stata-eval-buffer)
1528 ;;  (stata-switch-to-stata t))
1529
1530
1531 ;;(defun stata-eval-line-and-go ()
1532 ;;  "Send the current line to the inferior stata process and switch to the
1533 ;;process buffer."
1534 ;;  (interactive)
1535 ;;  (stata-eval-line)
1536 ;;  (stata-switch-to-stata t))
1537
1538
1539 ;;(defun stata-switch-to-stata (eob-p)
1540 ;;  "Switch to the current inferior stata process buffer.
1541 ;;With argument, positions cursor at end of buffer."
1542 ;;  (interactive "P")
1543 ;;  (let (stata-process (get-process "stata"))
1544 ;;    (if stata-process 
1545 ;;      (progn
1546 ;;        (switch-to-buffer (process-buffer stata-process))
1547 ;;        (if eob-p (goto-char (point-max))))
1548 ;;      (progn 
1549 ;;      (message "No inferior stata process")
1550 ;;      (ding)))))
1551
1552 ;;(defun stata-switch-to-end-of-stata nil
1553 ;;  "Switch to the end of the inferior stata process buffer."
1554 ;;  (interactive)
1555 ;;  (stata-switch-to-stata t))
1556
1557 ;;; Suggested function from Brendan Halpin:
1558 (defvar ess-STA-delimit-do-file "delimit-do.do")
1559
1560 (defun ess-STA-delimit-do ()
1561   (save-excursion
1562     (let ((commands (buffer-substring-no-properties (region-beginning)
1563                                                     (region-end))))
1564       (set-buffer (get-buffer-create ess-STA-delimit-do-file))
1565       (delete-region (point-min) (point-max))
1566       (insert "#delimit ;\n" 
1567               commands
1568               "\n#delimit cr\n")
1569       (write-file ess-STA-delimit-do-file nil)
1570       (comint-send-string "Stata"
1571                           (format "do %s \n" ess-STA-delimit-do-file)))))
1572 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1573
1574
1575 (provide 'essl-sta)
1576
1577 \f ; Local variables section
1578
1579 ;;; This file is automatically placed in Outline minor mode.
1580 ;;; The file is structured as follows:
1581 ;;; Chapters:     ^L ;
1582 ;;; Sections:    ;;*;;
1583 ;;; Subsections: ;;;*;;;
1584 ;;; Components:  defuns, defvars, defconsts
1585 ;;;              Random code beginning with a ;;;;* comment
1586
1587 ;;; Local variables:
1588 ;;; mode: emacs-lisp
1589 ;;; outline-minor-mode: nil
1590 ;;; mode: outline-minor
1591 ;;; outline-regexp: "\^L\\|\\`;\\|;;\\*\\|;;;\\*\\|(def[cvu]\\|(setq\\|;;;;\\*"
1592 ;;; End:
1593
1594 ;;; essl-sta.el ends here
1595
1596
1597