1 ;;; pspp-mode.el --- Major mode for editing PSPP files
3 ;; Copyright (C) 2005,2018 Free Software Foundation
4 ;; Author: Scott Andrew Borton <scott@pp.htv.fi>
5 ;; Created: 05 March 2005
7 ;; Keywords: PSPP major-mode
8 ;; This file is not part of GNU Emacs.
11 ;; Based on the example wpdl-mode.el by Scott Borton
13 ;; Copyright (C) 2000, 2003 Scott Andrew Borton <scott@pp.htv.fi>
15 ;; This program is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; This program is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
29 (defvar pspp-mode-hook nil)
31 (let ((pspp-mode-map (make-keymap)))
32 (define-key pspp-mode-map "\C-j" 'newline-and-indent)
34 "Keymap for PSPP major mode")
37 (add-to-list 'auto-mode-alist '("\\.sps\\'" . pspp-mode))
40 (defun pspp-data-block-p ()
41 "Returns t if current line is inside a data block."
44 (pspp-end-of-block-found nil)
45 (pspp-start-of-block-found nil)
49 (or (bobp) pspp-end-of-block-found )
50 pspp-start-of-block-found
53 (set 'pspp-end-of-block-found (looking-at "^[ \t]*END[\t ]+DATA\."))
54 (set 'pspp-start-of-block-found (looking-at "^[ \t]*BEGIN[\t ]+DATA"))
58 (and pspp-start-of-block-found (not pspp-end-of-block-found))
63 (defconst pspp-indent-width
69 (defconst pspp-indenters
76 "constructs which cause indentation"
80 (defconst pspp-unindenters
81 (concat "^[\t ]*END[\t ]+"
89 ;; Note that "END CASE" and "END FILE" do not unindent.
91 "constructs which cause end of indentation"
96 (defun pspp-indent-line ()
97 "Indent current line as PSPP code."
102 (the-indent 0) ; Default indent to column 0
107 (setq the-indent 0) ; First line is always non-indented
116 ;; If the most recent non blank line ended with a `.' then
117 ;; we're at the start of a new command.
120 (while (and blank-line (not (bobp)))
123 (if (and (not (pspp-data-block-p)) (not (looking-at "^[ \t]*$")))
125 (setq blank-line nil)
127 (if (not (looking-at ".*\\.[ \t]*$"))
128 (setq within-command t)
136 ;; If we're not at the start of a new command, then add an indent.
138 (set 'the-indent (+ 1 the-indent))
143 ;; Set the indentation according to the DO - END blocks
148 (if (not (pspp-comment-p))
153 (looking-at pspp-indenters)
155 (set 'the-indent (+ the-indent 1) )
158 ( (looking-at pspp-unindenters)
159 (set 'the-indent (- the-indent 1) )
169 (if (looking-at "^[\t ]*ELSE")
170 (set 'the-indent (- the-indent 1)))
173 ;; Stuff in the data-blocks should be untouched
174 (if (not (pspp-data-block-p)) (indent-line-to (* pspp-indent-width the-indent)))
179 (defun pspp-comment-start-line-p ()
180 "Returns t if the current line is the first line of a comment, nil otherwise"
182 (or (looking-at "^\*")
183 (looking-at "^[\t ]*COMMENT[\t ]")
188 (defun pspp-comment-end-line-p ()
189 "Returns t if the current line is the candidate for the last line of a comment, nil otherwise"
191 (looking-at ".*\\.[\t ]*$")
196 (defun pspp-comment-p ()
197 "Returns t if point is in a comment. Nil otherwise."
198 (if (pspp-data-block-p)
201 (pspp-comment-start-found nil)
202 (pspp-comment-end-found nil)
203 (pspp-single-line-comment nil)
208 (while (and (>= lines 0)
209 (not pspp-comment-start-found)
210 (not pspp-comment-end-found)
213 (if (pspp-comment-start-line-p) (set 'pspp-comment-start-found t))
215 (set 'pspp-comment-end-found nil)
218 (if (pspp-comment-end-line-p) (set 'pspp-comment-end-found t))
221 (set 'lines (forward-line -1))
226 (set 'pspp-single-line-comment (and
227 (pspp-comment-start-line-p)
228 (pspp-comment-end-line-p)))
232 (or pspp-single-line-comment
233 (and pspp-comment-start-found (not pspp-comment-end-found)))
238 (defvar pspp-mode-syntax-table
240 (x-pspp-mode-syntax-table (make-syntax-table))
243 ;; Special chars allowed in variables
244 (modify-syntax-entry ?# "w" x-pspp-mode-syntax-table)
245 (modify-syntax-entry ?@ "w" x-pspp-mode-syntax-table)
246 (modify-syntax-entry ?$ "w" x-pspp-mode-syntax-table)
249 ;; This is incomplete, because:
250 ;; a) Comments can also be given by COMMENT
251 ;; b) The sequence .\n* is interpreted incorrectly.
253 (modify-syntax-entry ?* ". 2" x-pspp-mode-syntax-table)
254 (modify-syntax-entry ?. ". 3" x-pspp-mode-syntax-table)
255 (modify-syntax-entry ?\n "- 41" x-pspp-mode-syntax-table)
259 (modify-syntax-entry ?' "\"" x-pspp-mode-syntax-table)
260 (modify-syntax-entry ?\" "\"" x-pspp-mode-syntax-table)
262 x-pspp-mode-syntax-table)
264 "Syntax table for pspp-mode")
267 (defconst pspp-font-lock-keywords
287 "CLEAR TRANSFORMATIONS"
355 "LOGISITIC REGRESSION"
379 "PEARSON CORRELATIONS"
444 'font-lock-builtin-face)
447 (concat "\\<" (regexp-opt '(
448 "ALL" "AND" "BY" "EQ" "GE" "GT" "LE" "LT"
449 "NE" "NOT" "OR" "TO" "WITH"
450 ) t ) "\\>") 'font-lock-keyword-face)
671 t) "\\>" ) 'font-lock-function-name-face)
673 '( "\\<[#$@a-zA-Z][a-zA-Z0-9_]*\\>" . font-lock-variable-name-face)
678 "Highlighting expressions for PSPP mode.")
683 (kill-all-local-variables)
684 (use-local-map pspp-mode-map)
685 (set-syntax-table pspp-mode-syntax-table)
687 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
688 (set (make-local-variable 'font-lock-defaults) '(pspp-font-lock-keywords))
690 (set (make-local-variable 'indent-line-function) 'pspp-indent-line)
691 (set (make-local-variable 'comment-start ) "* ")
692 (set (make-local-variable 'compile-command)
697 (setq major-mode 'pspp-mode)
698 (setq mode-name "PSPP")
699 (run-hooks 'pspp-mode-hook))
703 ;;; pspp-mode.el ends here