Added the beginnings of an emacs pspp-mode for editing pspp files.
[pspp-builds.git] / pspp-mode.el
1 ;;; pspp-mode-el -- Major mode for editing PSPP files
2
3
4 ;; Author: John Darrington <john@darrington.wattle.id.au>
5 ;; Created: 05 March 2005
6 ;; Keywords: PSPP major-mode
7
8 ;; Copyright (C) 2005 John Darrington
9 ;; 
10 ;; Based on the example wpdl-mode.el by Scott Borton
11 ;; Author: Scott Andrew Borton <scott@pp.htv.fi>
12
13 ;; Copyright (C) 2000, 2003 Scott Andrew Borton <scott@pp.htv.fi>
14
15 ;; This program is free software; you can redistribute it and/or
16 ;; modify it under the terms of the GNU General Public License as
17 ;; published by the Free Software Foundation; either version 2 of
18 ;; the License, or (at your option) any later version.
19
20 ;; This program is distributed in the hope that it will be
21 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
22 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
23 ;; PURPOSE.  See the GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public
26 ;; License along with this program; if not, write to the Free
27 ;; Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
28 ;; MA 02111-1307 USA
29
30 ;;; Code:
31 (defvar pspp-mode-hook nil)
32 (defvar pspp-mode-map
33   (let ((pspp-mode-map (make-keymap)))
34     (define-key pspp-mode-map "\C-j" 'newline-and-indent)
35     pspp-mode-map)
36   "Keymap for PSPP major mode")
37
38 (add-to-list 'auto-mode-alist '("\\.sps\\'" . pspp-mode))
39
40
41 (defun pspp-data-block ()
42   "Returns t if current line is inside a data block."
43   (interactive)
44
45   (let (
46         (end-data-found nil) 
47         (begin-data-found nil) 
48         (inside-block nil)
49         )
50
51     (save-excursion 
52       (beginning-of-line)
53       (while (not (or (bobp) end-data-found)  )
54
55         (if (looking-at "^[ \t]*END +DATA\.") 
56             (setq end-data-found t)
57           )
58
59         (forward-line -1)
60
61         (if (looking-at "^[ \t]*BEGIN +DATA\.") 
62             (setq begin-data-found t)
63           )
64           
65         )
66       )
67
68     (setq inside-block (and begin-data-found (not end-data-found)))
69
70     inside-block
71     )
72   
73   )
74
75
76 (defun pspp-indent-line ()
77   "Indent current line as PSPP code."
78   (interactive)
79   (beginning-of-line)
80   (let (
81         (verbatim nil)
82         (the-indent 0)          ; Default indent to column 0
83         )
84
85     (if (bobp)
86         (setq the-indent 0)     ; First line is always non-indented
87       )
88   
89
90     (let (
91           (within-command nil)
92           (blank-line t)
93           )
94
95       ;; If the most recent non blank line ended with a `.' then
96       ;; we're at the start of a new command.
97
98       (save-excursion 
99         (while (and blank-line  (not (bobp)))
100           (forward-line -1)
101         
102           (if (and (not (pspp-data-block)) (not (looking-at "^[ \t]*$")))
103               (progn 
104               (setq blank-line nil)
105             
106               (if (not (looking-at ".*\\.[ \t]*$"))
107                 (setq within-command t)
108               )
109               )
110           )
111           )
112         
113       (if within-command (setq the-indent 8) )
114       )
115   
116       )
117
118     (if (not (pspp-data-block))  (indent-line-to the-indent))
119 ))
120
121
122
123 (defvar pspp-mode-syntax-table
124   (let (
125         (x-pspp-mode-syntax-table (make-syntax-table))
126         )
127         
128     (modify-syntax-entry ?_  "w" x-pspp-mode-syntax-table)
129     (modify-syntax-entry ?.  "w" x-pspp-mode-syntax-table)
130     (modify-syntax-entry ?\" "|" x-pspp-mode-syntax-table)
131     (modify-syntax-entry ?\' "|" x-pspp-mode-syntax-table)
132
133     ;; Comment definitions
134       (modify-syntax-entry ?*  "<\n" x-pspp-mode-syntax-table)
135
136 ;;    (modify-syntax-entry ?\n "- 1"  x-pspp-mode-syntax-table)
137 ;;    (modify-syntax-entry ?*  ". 2"  x-pspp-mode-syntax-table)
138
139     (modify-syntax-entry ?\n ">*"  x-pspp-mode-syntax-table)
140
141     x-pspp-mode-syntax-table)
142   
143     "Syntax table for pspp-mode")
144   
145 (defun pspp-mode ()
146   (interactive)
147   (kill-all-local-variables)
148   (use-local-map pspp-mode-map)
149   (set-syntax-table pspp-mode-syntax-table)
150   (setq comment-start "* ")
151   ;; Register our indentation function
152   (set (make-local-variable 'indent-line-function) 'pspp-indent-line)  
153   (setq major-mode 'pspp-mode)
154   (setq mode-name "PSPP")
155   (run-hooks 'pspp-mode-hook))
156
157 (provide 'pspp-mode)
158
159 ;;; pspp-mode.el ends here
160