1 ;;; pspp-mode.el --- Major mode for editing PSPP files
3 ;; Copyright (C) 2005,2018,2020 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)
33 (let ((pspp-mode-map (make-keymap)))
34 (define-key pspp-mode-map "\C-j" 'newline-and-indent)
36 "Keymap for PSPP major mode")
40 (add-to-list 'auto-mode-alist '("\\.sps\\'" . pspp-mode))
43 (defun pspp-data-block-p ()
44 "Returns t if current line is inside a data block."
46 (let ((pspp-end-of-block-found nil)
47 (pspp-start-of-block-found nil))
50 (or (bobp) pspp-end-of-block-found)
51 pspp-start-of-block-found))
52 (set 'pspp-end-of-block-found (looking-at "^[ \t]*END[\t ]+DATA\."))
53 (set 'pspp-start-of-block-found (looking-at "^[ \t]*BEGIN[\t ]+DATA"))
56 (and pspp-start-of-block-found (not pspp-end-of-block-found)))))
59 (defconst pspp-indent-width
64 (defconst pspp-indenters
71 "constructs which cause indentation")
74 (defconst pspp-unindenters
75 (concat "^[\t ]*END[\t ]+"
82 ;; Note that "END CASE" and "END FILE" do not unindent.
83 "constructs which cause end of indentation")
86 (defun pspp-indent-line ()
87 "Indent current line as PSPP code."
90 (the-indent 0) ; Default indent to column 0
93 (setq the-indent 0)) ; First line is always non-indented
94 (let ((within-command nil) (blank-line t))
95 ;; If the most recent non blank line ended with a `.' then
96 ;; we're at the start of a new command.
98 (while (and blank-line (not (bobp)))
101 (if (and (not (pspp-data-block-p)) (not (looking-at "^[ \t]*$")))
103 (setq blank-line nil)
105 (if (not (looking-at ".*\\.[ \t]*$"))
106 (setq within-command t))))))
107 ;; If we're not at the start of a new command, then add an indent.
109 (set 'the-indent (+ 1 the-indent))))
110 ;; Set the indentation according to the DO - END blocks
115 (if (not (pspp-comment-p))
116 (cond ((save-excursion
118 (looking-at pspp-indenters))
119 (set 'the-indent (+ the-indent 1)))
121 ((looking-at pspp-unindenters)
122 (set 'the-indent (- the-indent 1)))))
127 (if (looking-at "^[\t ]*ELSE")
128 (set 'the-indent (- the-indent 1))))
130 ;; Stuff in the data-blocks should be untouched
131 (if (not (pspp-data-block-p)) (indent-line-to (* pspp-indent-width the-indent)))))
134 (defun pspp-comment-start-line-p ()
135 "Returns t if the current line is the first line of a comment, nil otherwise"
137 (or (looking-at "^\*")
138 (looking-at "^[\t ]*COMMENT[\t ]")))
141 (defun pspp-comment-end-line-p ()
142 "Returns t if the current line is the candidate for the last line of a comment, nil otherwise"
144 (looking-at ".*\\.[\t ]*$"))
147 (defun pspp-comment-p ()
148 "Returns t if point is in a comment. Nil otherwise."
149 (if (pspp-data-block-p)
151 (let ((pspp-comment-start-found nil)
152 (pspp-comment-end-found nil)
153 (pspp-single-line-comment nil)
157 (while (and (>= lines 0)
158 (not pspp-comment-start-found)
159 (not pspp-comment-end-found))
161 (if (pspp-comment-start-line-p) (set 'pspp-comment-start-found t))
163 (set 'pspp-comment-end-found nil)
166 (if (pspp-comment-end-line-p) (set 'pspp-comment-end-found t))))
167 (set 'lines (forward-line -1))))
170 (set 'pspp-single-line-comment (and
171 (pspp-comment-start-line-p)
172 (pspp-comment-end-line-p))))
174 (or pspp-single-line-comment
175 (and pspp-comment-start-found (not pspp-comment-end-found))))))
178 (defvar pspp-mode-syntax-table
179 (let ((x-pspp-mode-syntax-table (make-syntax-table)))
181 ;; Special chars allowed in variables
182 (modify-syntax-entry ?# "w" x-pspp-mode-syntax-table)
183 (modify-syntax-entry ?@ "w" x-pspp-mode-syntax-table)
184 (modify-syntax-entry ?$ "w" x-pspp-mode-syntax-table)
187 ;; This is incomplete, because:
188 ;; a) Comments can also be given by COMMENT
189 ;; b) The sequence .\n* is interpreted incorrectly.
191 (modify-syntax-entry ?* ". 2" x-pspp-mode-syntax-table)
192 (modify-syntax-entry ?. ". 3" x-pspp-mode-syntax-table)
193 (modify-syntax-entry ?\n "- 41" x-pspp-mode-syntax-table)
196 (modify-syntax-entry ?' "\"" x-pspp-mode-syntax-table)
197 (modify-syntax-entry ?\" "\"" x-pspp-mode-syntax-table)
199 x-pspp-mode-syntax-table)
201 "Syntax table for pspp-mode")
204 (defconst pspp-font-lock-keywords
223 "CLEAR TRANSFORMATIONS"
291 "LOGISITIC REGRESSION"
315 "PEARSON CORRELATIONS"
380 'font-lock-builtin-face)
383 (concat "\\<" (regexp-opt
384 '("ALL" "AND" "BY" "EQ" "GE" "GT" "LE" "LT" "NE" "NOT" "OR" "TO" "WITH")
386 'font-lock-keyword-face)
606 t) "\\>") 'font-lock-function-name-face)
608 '( "\\<[#$@a-zA-Z][a-zA-Z0-9_]*\\>" . font-lock-variable-name-face))
609 "Highlighting expressions for PSPP mode.")
615 (kill-all-local-variables)
616 (use-local-map pspp-mode-map)
617 (set-syntax-table pspp-mode-syntax-table)
619 (set (make-local-variable 'font-lock-keywords-case-fold-search) t)
620 (set (make-local-variable 'font-lock-defaults) '(pspp-font-lock-keywords))
622 (set (make-local-variable 'indent-line-function) 'pspp-indent-line)
623 (set (make-local-variable 'comment-start) "* ")
624 (set (make-local-variable 'compile-command)
628 (setq major-mode 'pspp-mode)
629 (setq mode-name "PSPP")
630 (run-hooks 'pspp-mode-hook))
634 ;;; pspp-mode.el ends here