pascal.el 56.2 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1
;;; pascal.el --- major mode for editing pascal source in Emacs
Richard M. Stallman's avatar
Richard M. Stallman committed
2

3
;; Copyright (C) 1993, 94, 95, 96, 97, 98, 1999 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4

Richard M. Stallman's avatar
Richard M. Stallman committed
5
;; Author: Espen Skoglund <espensk@stud.cs.uit.no>
Richard M. Stallman's avatar
Richard M. Stallman committed
6
;; Keywords: languages
Richard M. Stallman's avatar
Richard M. Stallman committed
7

Richard M. Stallman's avatar
Richard M. Stallman committed
8
;; This file is part of GNU Emacs.
Richard M. Stallman's avatar
Richard M. Stallman committed
9

Erik Naggum's avatar
Erik Naggum committed
10
;; GNU Emacs is free software; you can redistribute it and/or modify
Richard M. Stallman's avatar
Richard M. Stallman committed
11
;; it under the terms of the GNU General Public License as published by
Erik Naggum's avatar
Erik Naggum committed
12 13
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.
Richard M. Stallman's avatar
Richard M. Stallman committed
14

Erik Naggum's avatar
Erik Naggum committed
15
;; GNU Emacs is distributed in the hope that it will be useful,
Richard M. Stallman's avatar
Richard M. Stallman committed
16 17 18
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.
Richard M. Stallman's avatar
Richard M. Stallman committed
19

Richard M. Stallman's avatar
Richard M. Stallman committed
20
;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
24 25 26

;;; Commentary:

Erik Naggum's avatar
Erik Naggum committed
27 28
;; USAGE
;; =====
Richard M. Stallman's avatar
Richard M. Stallman committed
29

Erik Naggum's avatar
Erik Naggum committed
30 31 32 33
;; Emacs should enter Pascal mode when you find a Pascal source file.
;; When you have entered Pascal mode, you may get more info by pressing
;; C-h m. You may also get online help describing various functions by:
;; C-h f <Name of function you want described>
Richard M. Stallman's avatar
Richard M. Stallman committed
34

Erik Naggum's avatar
Erik Naggum committed
35 36 37 38 39 40 41 42 43 44 45 46
;; If you want to customize Pascal mode to fit you better, you may add
;; these lines (the values of the variables presented here are the defaults):
;;
;; ;; User customization for Pascal mode
;; (setq pascal-indent-level       3
;;       pascal-case-indent        2
;;       pascal-auto-newline       nil
;;       pascal-tab-always-indent  t
;;       pascal-auto-endcomments   t
;;       pascal-auto-lineup        '(all)
;;       pascal-toggle-completions nil
;;       pascal-type-keywords      '("array" "file" "packed" "char" 
47
;; 				     "integer" "real" "string" "record")
Erik Naggum's avatar
Erik Naggum committed
48
;;       pascal-start-keywords     '("begin" "end" "function" "procedure"
49 50
;; 				     "repeat" "until" "while" "read" "readln"
;; 				     "reset" "rewrite" "write" "writeln")
Erik Naggum's avatar
Erik Naggum committed
51
;;       pascal-separator-keywords '("downto" "else" "mod" "div" "then"))
Richard M. Stallman's avatar
Richard M. Stallman committed
52

Erik Naggum's avatar
Erik Naggum committed
53 54 55 56 57 58
;; KNOWN BUGS / BUGREPORTS
;; =======================
;; As far as I know, there are no bugs in the current version of this
;; package.  This may not be true however, since I never use this mode
;; myself and therefore would never notice them anyway.   If you do
;; find any bugs, you may submit them to: espensk@stud.cs.uit.no
Karl Heuer's avatar
Karl Heuer committed
59
;; as well as to bug-gnu-emacs@gnu.org.
Richard M. Stallman's avatar
Richard M. Stallman committed
60 61

;;; Code:
Richard M. Stallman's avatar
Richard M. Stallman committed
62

63 64 65 66
(defgroup pascal nil
  "Major mode for editing Pascal source in Emacs"
  :group 'languages)

Richard M. Stallman's avatar
Richard M. Stallman committed
67 68 69 70 71 72
(defvar pascal-mode-abbrev-table nil
  "Abbrev table in use in Pascal-mode buffers.")
(define-abbrev-table 'pascal-mode-abbrev-table ())

(defvar pascal-mode-map ()
  "Keymap used in Pascal mode.")
Richard M. Stallman's avatar
Richard M. Stallman committed
73 74 75 76 77 78 79
(if pascal-mode-map
    ()
  (setq pascal-mode-map (make-sparse-keymap))
  (define-key pascal-mode-map ";"        'electric-pascal-semi-or-dot)
  (define-key pascal-mode-map "."        'electric-pascal-semi-or-dot)
  (define-key pascal-mode-map ":"        'electric-pascal-colon)
  (define-key pascal-mode-map "="        'electric-pascal-equal)
80
  (define-key pascal-mode-map "#"        'electric-pascal-hash)
Richard M. Stallman's avatar
Richard M. Stallman committed
81 82
  (define-key pascal-mode-map "\r"       'electric-pascal-terminate-line)
  (define-key pascal-mode-map "\t"       'electric-pascal-tab)
83 84
  (define-key pascal-mode-map "\M-\t"    'pascal-complete-word)
  (define-key pascal-mode-map "\M-?"     'pascal-show-completions)
Richard M. Stallman's avatar
Richard M. Stallman committed
85
  (define-key pascal-mode-map "\177"     'backward-delete-char-untabify)
86 87
  (define-key pascal-mode-map "\M-\C-h"  'pascal-mark-defun)
  (define-key pascal-mode-map "\C-c\C-b" 'pascal-insert-block)
Richard M. Stallman's avatar
Richard M. Stallman committed
88 89 90
  (define-key pascal-mode-map "\M-*"     'pascal-star-comment)
  (define-key pascal-mode-map "\C-c\C-c" 'pascal-comment-area)
  (define-key pascal-mode-map "\C-c\C-u" 'pascal-uncomment-area)
91 92 93 94
  (define-key pascal-mode-map "\M-\C-a"  'pascal-beg-of-defun)
  (define-key pascal-mode-map "\M-\C-e"  'pascal-end-of-defun)
  (define-key pascal-mode-map "\C-c\C-d" 'pascal-goto-defun)
  (define-key pascal-mode-map "\C-c\C-o" 'pascal-outline)
Richard M. Stallman's avatar
Richard M. Stallman committed
95 96
;;; A command to change the whole buffer won't be used terribly
;;; often, so no need for a key binding.
Richard M. Stallman's avatar
Richard M. Stallman committed
97 98 99 100
;  (define-key pascal-mode-map "\C-cd"    'pascal-downcase-keywords)
;  (define-key pascal-mode-map "\C-cu"    'pascal-upcase-keywords)
;  (define-key pascal-mode-map "\C-cc"    'pascal-capitalize-keywords)
  )
101 102 103 104

(defvar pascal-imenu-generic-expression
  '("^[ \t]*\\(function\\|procedure\\)[ \t\n]+\\([a-zA-Z0-9_.:]+\\)" . (2))
  "Imenu expression for Pascal-mode.  See `imenu-generic-expression'.")
105

Richard M. Stallman's avatar
Richard M. Stallman committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
(defvar pascal-keywords
  '("and" "array" "begin" "case" "const" "div" "do" "downto" "else" "end" 
    "file" "for" "function" "goto" "if" "in" "label" "mod" "nil" "not" "of" 
    "or" "packed" "procedure" "program" "record" "repeat" "set" "then" "to" 
    "type" "until" "var" "while" "with"
    ;; The following are not standard in pascal, but widely used.
    "get" "put" "input" "output" "read" "readln" "reset" "rewrite" "write"
    "writeln"))

;;;
;;; Regular expressions used to calculate indent, etc.
;;;
(defconst pascal-symbol-re      "\\<[a-zA-Z_][a-zA-Z_0-9.]*\\>")
(defconst pascal-beg-block-re   "\\<\\(begin\\|case\\|record\\|repeat\\)\\>")
(defconst pascal-end-block-re   "\\<\\(end\\|until\\)\\>")
(defconst pascal-declaration-re "\\<\\(const\\|label\\|type\\|var\\)\\>")
122
(defconst pascal-progbeg-re     "\\<\\program\\>")
Richard M. Stallman's avatar
Richard M. Stallman committed
123
(defconst pascal-defun-re       "\\<\\(function\\|procedure\\|program\\)\\>")
124
(defconst pascal-sub-block-re   "\\<\\(if\\|else\\|for\\|while\\|with\\)\\>")
125
(defconst pascal-noindent-re    "\\<\\(begin\\|end\\|until\\|else\\)\\>")
Richard M. Stallman's avatar
Richard M. Stallman committed
126 127 128 129 130 131 132
(defconst pascal-nosemi-re      "\\<\\(begin\\|repeat\\|then\\|do\\|else\\)\\>")
(defconst pascal-autoindent-lines-re
  "\\<\\(label\\|var\\|type\\|const\\|until\\|end\\|begin\\|repeat\\|else\\)\\>")

;;; Strings used to mark beginning and end of excluded text
(defconst pascal-exclude-str-start "{-----\\/----- EXCLUDED -----\\/-----")
(defconst pascal-exclude-str-end " -----/\\----- EXCLUDED -----/\\-----}")
Richard M. Stallman's avatar
Richard M. Stallman committed
133 134 135 136 137 138 139

(defvar pascal-mode-syntax-table nil
  "Syntax table in use in Pascal-mode buffers.")

(if pascal-mode-syntax-table
    ()
  (setq pascal-mode-syntax-table (make-syntax-table))
140
  (modify-syntax-entry ?\\ "."   pascal-mode-syntax-table)
Richard M. Stallman's avatar
Richard M. Stallman committed
141 142
  (modify-syntax-entry ?( "()1"  pascal-mode-syntax-table)  
  (modify-syntax-entry ?) ")(4"  pascal-mode-syntax-table)
Richard M. Stallman's avatar
Richard M. Stallman committed
143
  (modify-syntax-entry ?* ". 23" pascal-mode-syntax-table)
Richard M. Stallman's avatar
Richard M. Stallman committed
144 145 146 147 148 149 150 151 152 153
  (modify-syntax-entry ?{ "<"    pascal-mode-syntax-table)
  (modify-syntax-entry ?} ">"    pascal-mode-syntax-table)
  (modify-syntax-entry ?+ "."    pascal-mode-syntax-table)
  (modify-syntax-entry ?- "."    pascal-mode-syntax-table)
  (modify-syntax-entry ?= "."    pascal-mode-syntax-table)
  (modify-syntax-entry ?% "."    pascal-mode-syntax-table)
  (modify-syntax-entry ?< "."    pascal-mode-syntax-table)
  (modify-syntax-entry ?> "."    pascal-mode-syntax-table)
  (modify-syntax-entry ?& "."    pascal-mode-syntax-table)
  (modify-syntax-entry ?| "."    pascal-mode-syntax-table)
154
  (modify-syntax-entry ?_ "_"    pascal-mode-syntax-table)
Richard M. Stallman's avatar
Richard M. Stallman committed
155 156
  (modify-syntax-entry ?\' "\""  pascal-mode-syntax-table))

157
(defconst pascal-font-lock-keywords (purecopy
158
  (list
159 160 161 162
   '("^[ \t]*\\(function\\|pro\\(cedure\\|gram\\)\\)\\>[ \t]*\\([a-z]\\)"
     1 font-lock-keyword-face)
   '("^[ \t]*\\(function\\|pro\\(cedure\\|gram\\)\\)\\>[ \t]*\\([a-z][a-z0-9_]*\\)"
     3 font-lock-function-name-face t)
163 164 165 166 167
;   ("type" "const" "real" "integer" "char" "boolean" "var"
;    "record" "array" "file")
   (cons (concat "\\<\\(array\\|boolean\\|c\\(har\\|onst\\)\\|file\\|"
		 "integer\\|re\\(al\\|cord\\)\\|type\\|var\\)\\>")
	 'font-lock-type-face)
168
   '("\\<\\(label\\|external\\|forward\\)\\>" . font-lock-constant-face)
169
   '("\\<\\([0-9]+\\)[ \t]*:" 1 font-lock-function-name-face)
170 171 172 173 174 175 176
;   ("of" "to" "for" "if" "then" "else" "case" "while"
;    "do" "until" "and" "or" "not" "in" "with" "repeat" "begin" "end")
   (concat "\\<\\("
	   "and\\|begin\\|case\\|do\\|e\\(lse\\|nd\\)\\|for\\|i[fn]\\|"
	   "not\\|o[fr]\\|repeat\\|t\\(hen\\|o\\)\\|until\\|w\\(hile\\|ith\\)"
	   "\\)\\>")
   '("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
177 178 179
     1 font-lock-keyword-face)
   '("\\<\\(goto\\)\\>[ \t]*\\([0-9]+\\)?"
     2 font-lock-keyword-face t)))
180
  "Additional expressions to highlight in Pascal mode.")
181
(put 'pascal-mode 'font-lock-defaults '(pascal-font-lock-keywords nil t))
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
(defcustom pascal-indent-level 3
  "*Indentation of Pascal statements with respect to containing block."
  :type 'integer
  :group 'pascal)

(defcustom pascal-case-indent 2
  "*Indentation for case statements."
  :type 'integer
  :group 'pascal)

(defcustom pascal-auto-newline nil
  "*Non-nil means automatically insert newlines in certain cases.
These include after semicolons and after the punctuation mark after an `end'."
  :type 'boolean
  :group 'pascal)

199 200 201 202 203
(defcustom pascal-indent-nested-functions t
  "*Non-nil means nested functions are indented."
  :type 'boolean
  :group 'pascal)

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
(defcustom pascal-tab-always-indent t
  "*Non-nil means TAB in Pascal mode should always reindent the current line.
If this is nil, TAB inserts a tab if it is at the end of the line
and follows non-whitespace text."
  :type 'boolean
  :group 'pascal)

(defcustom pascal-auto-endcomments t
  "*Non-nil means automatically insert comments after certain `end's.
Specifically, this is done after the ends of cases statements and functions.
The name of the function or case is included between the braces."
  :type 'boolean
  :group 'pascal)

(defcustom pascal-auto-lineup '(all)
219 220 221 222 223
  "*List of contexts where auto lineup of :'s or ='s should be done.
Elements can be of type: 'paramlist', 'declaration' or 'case', which will
do auto lineup in parameterlist, declarations or case-statements
respectively. The word 'all' will do all lineups. '(case paramlist) for
instance will do lineup in case-statements and parameterlist, while '(all)
224
will do all lineups."
225 226 227 228 229
  :type '(set :extra-offset 8
	      (const :tag "Everything" all)
	      (const :tag "Parameter lists" paramlist)
	      (const :tag "Decalrations" declaration)
	      (const :tag "Case statements" case))
230 231 232
  :group 'pascal)

(defcustom pascal-toggle-completions nil
233 234 235
  "*Non-nil means \\<pascal-mode-map>\\[pascal-complete-word] should try all possible completions one by one.
Repeated use of \\[pascal-complete-word] will show you all of them.
Normally, when there is more than one possible completion,
236 237 238
it displays a list of all possible completions."
  :type 'boolean
  :group 'pascal)
239

240
(defcustom pascal-type-keywords
Richard M. Stallman's avatar
Richard M. Stallman committed
241 242
  '("array" "file" "packed" "char" "integer" "real" "string" "record")
  "*Keywords for types used when completing a word in a declaration or parmlist.
243 244 245 246 247
These include integer, real, char, etc.
The types defined within the Pascal program
are handled in another way, and should not be added to this list."
  :type '(repeat (string :tag "Keyword"))
  :group 'pascal)
248

249
(defcustom pascal-start-keywords
Richard M. Stallman's avatar
Richard M. Stallman committed
250 251 252
  '("begin" "end" "function" "procedure" "repeat" "until" "while"
    "read" "readln" "reset" "rewrite" "write" "writeln")
  "*Keywords to complete when standing at the first word of a statement.
253
These are keywords such as begin, repeat, until, readln.
Richard M. Stallman's avatar
Richard M. Stallman committed
254
The procedures and variables defined within the Pascal program
255 256 257
are handled in another way,  and should not be added to this list."
  :type '(repeat (string :tag "Keyword"))
  :group 'pascal)
258

259
(defcustom pascal-separator-keywords
Richard M. Stallman's avatar
Richard M. Stallman committed
260 261
  '("downto" "else" "mod" "div" "then")
  "*Keywords to complete when NOT standing at the first word of a statement.
262 263 264 265 266 267
These are keywords such as downto, else, mod, then.
Variables and function names defined within the Pascal program
are handled in another way, and should not be added to this list."
  :type '(repeat (string :tag "Keyword"))
  :group 'pascal)

Richard M. Stallman's avatar
Richard M. Stallman committed
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289

;;;
;;;  Macros
;;;

(defsubst pascal-get-beg-of-line (&optional arg)
  (save-excursion
    (beginning-of-line arg)
    (point)))

(defsubst pascal-get-end-of-line (&optional arg)
  (save-excursion
    (end-of-line arg)
    (point)))

(defun pascal-declaration-end ()
  (let ((nest 1))
    (while (and (> nest 0)
		(re-search-forward 
		 "[:=]\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)" 
		 (save-excursion (end-of-line 2) (point)) t))
      (cond ((match-beginning 1) (setq nest (1+ nest)))
290 291
	    ((match-beginning 2) (setq nest (1- nest)))
	    ((looking-at "[^(\n]+)") (setq nest 0))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
292

293

Richard M. Stallman's avatar
Richard M. Stallman committed
294 295 296
(defun pascal-declaration-beg ()
  (let ((nest 1))
    (while (and (> nest 0)
297
		(re-search-backward "[:=]\\|\\<\\(type\\|var\\|label\\|const\\)\\>\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)" (pascal-get-beg-of-line 0) t))
Richard M. Stallman's avatar
Richard M. Stallman committed
298 299 300 301
      (cond ((match-beginning 1) (setq nest 0))
	    ((match-beginning 2) (setq nest (1- nest)))
	    ((match-beginning 3) (setq nest (1+ nest)))))
    (= nest 0)))
302

Richard M. Stallman's avatar
Richard M. Stallman committed
303 304 305 306 307
  
(defsubst pascal-within-string ()
  (save-excursion
    (nth 3 (parse-partial-sexp (pascal-get-beg-of-line) (point)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
308 309 310

;;;###autoload
(defun pascal-mode ()
Richard M. Stallman's avatar
Richard M. Stallman committed
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
  "Major mode for editing Pascal code. \\<pascal-mode-map>
TAB indents for Pascal code.  Delete converts tabs to spaces as it moves back.

\\[pascal-complete-word] completes the word around current point with respect \
to position in code
\\[pascal-show-completions] shows all possible completions at this point.

Other useful functions are:

\\[pascal-mark-defun]\t- Mark function.
\\[pascal-insert-block]\t- insert begin ... end;
\\[pascal-star-comment]\t- insert (* ... *)
\\[pascal-comment-area]\t- Put marked area in a comment, fixing nested comments.
\\[pascal-uncomment-area]\t- Uncomment an area commented with \
\\[pascal-comment-area].
\\[pascal-beg-of-defun]\t- Move to beginning of current function.
\\[pascal-end-of-defun]\t- Move to end of current function.
\\[pascal-goto-defun]\t- Goto function prompted for in the minibuffer.
\\[pascal-outline]\t- Enter pascal-outline-mode (see also pascal-outline).

Variables controlling indentation/edit style:

333
 pascal-indent-level (default 3)
Richard M. Stallman's avatar
Richard M. Stallman committed
334
    Indentation of Pascal statements with respect to containing block.
335
 pascal-case-indent (default 2)
Richard M. Stallman's avatar
Richard M. Stallman committed
336
    Indentation for case statements.
337
 pascal-auto-newline (default nil)
338 339
    Non-nil means automatically newline after semicolons and the punctuation
    mark after an end.
340 341
 pascal-indent-nested-functions (default t)
    Non-nil means nested functions are indented.
342
 pascal-tab-always-indent (default t)
Richard M. Stallman's avatar
Richard M. Stallman committed
343 344
    Non-nil means TAB in Pascal mode should always reindent the current line,
    regardless of where in the line point is when the TAB command is used.
345
 pascal-auto-endcomments (default t)
Richard M. Stallman's avatar
Richard M. Stallman committed
346 347
    Non-nil means a comment { ... } is set after the ends which ends cases and
    functions. The name of the function or case will be set between the braces.
348
 pascal-auto-lineup (default t)
349
    List of contexts where auto lineup of :'s or ='s should be done.
Richard M. Stallman's avatar
Richard M. Stallman committed
350 351

See also the user variables pascal-type-keywords, pascal-start-keywords and
352
pascal-separator-keywords.
Richard M. Stallman's avatar
Richard M. Stallman committed
353 354 355 356 357 358 359 360 361 362 363 364

Turning on Pascal mode calls the value of the variable pascal-mode-hook with
no args, if that value is non-nil."
  (interactive)
  (kill-all-local-variables)
  (use-local-map pascal-mode-map)
  (setq major-mode 'pascal-mode)
  (setq mode-name "Pascal")
  (setq local-abbrev-table pascal-mode-abbrev-table)
  (set-syntax-table pascal-mode-syntax-table)
  (make-local-variable 'indent-line-function)
  (setq indent-line-function 'pascal-indent-line)
365
  (make-local-variable 'comment-indent-function)
Richard M. Stallman's avatar
Richard M. Stallman committed
366
  (setq comment-indent-function 'pascal-indent-comment)
Richard M. Stallman's avatar
Richard M. Stallman committed
367
  (make-local-variable 'parse-sexp-ignore-comments)
368
  (setq parse-sexp-ignore-comments nil)
369 370
  (make-local-variable 'blink-matching-paren-dont-ignore-comments)
  (setq blink-matching-paren-dont-ignore-comments t)
Richard M. Stallman's avatar
Richard M. Stallman committed
371 372
  (make-local-variable 'case-fold-search)
  (setq case-fold-search t)
373 374
  (make-local-variable 'comment-start)
  (setq comment-start "{")
375 376 377 378
  (make-local-variable 'comment-start-skip)
  (setq comment-start-skip "(\\*+ *\\|{ *")
  (make-local-variable 'comment-end)
  (setq comment-end "}")
379
  ;; Font lock support
380 381
  (make-local-variable 'font-lock-defaults)
  (setq font-lock-defaults '(pascal-font-lock-keywords nil t))
382 383 384
  ;; Imenu support
  (make-local-variable 'imenu-generic-expression)
  (setq imenu-generic-expression pascal-imenu-generic-expression)
Dave Love's avatar
Dave Love committed
385
  (setq imenu-case-fold-search t)
Richard M. Stallman's avatar
Richard M. Stallman committed
386 387
  (run-hooks 'pascal-mode-hook))

Richard M. Stallman's avatar
Richard M. Stallman committed
388 389


Richard M. Stallman's avatar
Richard M. Stallman committed
390 391 392 393 394 395
;;;
;;;  Electric functions
;;;
(defun electric-pascal-terminate-line ()
  "Terminate line and indent next line."
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
396
  ;; First, check if current line should be indented
Richard M. Stallman's avatar
Richard M. Stallman committed
397 398 399
  (save-excursion
    (beginning-of-line)
    (skip-chars-forward " \t")
Richard M. Stallman's avatar
Richard M. Stallman committed
400
    (if (looking-at pascal-autoindent-lines-re)
Richard M. Stallman's avatar
Richard M. Stallman committed
401
	(pascal-indent-line)))
Richard M. Stallman's avatar
Richard M. Stallman committed
402
  (delete-horizontal-space) ; Removes trailing whitespaces
Richard M. Stallman's avatar
Richard M. Stallman committed
403
  (newline)
Richard M. Stallman's avatar
Richard M. Stallman committed
404
  ;; Indent next line
Richard M. Stallman's avatar
Richard M. Stallman committed
405 406 407 408 409 410 411 412 413
  (pascal-indent-line)
  ;; Maybe we should set some endcomments
  (if pascal-auto-endcomments
      (pascal-set-auto-comments))
  ;; Check if we shall indent inside comment
  (let ((setstar nil))
    (save-excursion
      (forward-line -1)
      (skip-chars-forward " \t")
Richard M. Stallman's avatar
Richard M. Stallman committed
414
      (cond ((looking-at "\\*[ \t]+)")
Richard M. Stallman's avatar
Richard M. Stallman committed
415 416
	     ;; Delete region between `*' and `)' if there is only whitespaces.
	     (forward-char 1)
Richard M. Stallman's avatar
Richard M. Stallman committed
417
	     (delete-horizontal-space))
Richard M. Stallman's avatar
Richard M. Stallman committed
418 419 420 421 422
	    ((and (looking-at "(\\*\\|\\*[^)]")
		  (not (save-excursion
			 (search-forward "*)" (pascal-get-end-of-line) t))))
	     (setq setstar t))))
    ;; If last line was a star comment line then this one shall be too.
Richard M. Stallman's avatar
Richard M. Stallman committed
423 424 425
    (if (null setstar)	
	(pascal-indent-line)
      (insert "*  "))))
Richard M. Stallman's avatar
Richard M. Stallman committed
426 427


Richard M. Stallman's avatar
Richard M. Stallman committed
428 429
(defun electric-pascal-semi-or-dot ()
  "Insert `;' or `.' character and reindent the line."
Richard M. Stallman's avatar
Richard M. Stallman committed
430 431 432 433 434 435 436 437 438
  (interactive)
  (insert last-command-char)
  (save-excursion
    (beginning-of-line)
    (pascal-indent-line))
  (if pascal-auto-newline
      (electric-pascal-terminate-line)))

(defun electric-pascal-colon ()
Richard M. Stallman's avatar
Richard M. Stallman committed
439
  "Insert `:' and do all indentions except line indent on this line."
Richard M. Stallman's avatar
Richard M. Stallman committed
440 441
  (interactive)
  (insert last-command-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
442 443 444 445 446 447 448 449 450
  ;; Do nothing if within string.
  (if (pascal-within-string)
      ()
    (save-excursion
      (beginning-of-line)
      (pascal-indent-line))
    (let ((pascal-tab-always-indent nil))
      (pascal-indent-command))))

Richard M. Stallman's avatar
Richard M. Stallman committed
451
(defun electric-pascal-equal ()
Richard M. Stallman's avatar
Richard M. Stallman committed
452
  "Insert `=', and do indention if within type declaration."
Richard M. Stallman's avatar
Richard M. Stallman committed
453 454
  (interactive)
  (insert last-command-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
455
  (if (eq (car (pascal-calculate-indent)) 'declaration)
Richard M. Stallman's avatar
Richard M. Stallman committed
456 457 458
      (let ((pascal-tab-always-indent nil))
	(pascal-indent-command))))

459
(defun electric-pascal-hash ()
460
  "Insert `#', and indent to column 0 if this is a CPP directive."
461 462 463 464 465 466
  (interactive)
  (insert last-command-char)
  (if (save-excursion (beginning-of-line) (looking-at "^[ \t]*#"))
      (save-excursion (beginning-of-line)
		      (delete-horizontal-space))))

Richard M. Stallman's avatar
Richard M. Stallman committed
467
(defun electric-pascal-tab ()
Richard M. Stallman's avatar
Richard M. Stallman committed
468
  "Function called when TAB is pressed in Pascal mode."
Richard M. Stallman's avatar
Richard M. Stallman committed
469
  (interactive)
470 471 472 473
  ;; Do nothing if within a string or in a CPP directive.
  (if (or (pascal-within-string)
	  (and (not (bolp))
	       (save-excursion (beginning-of-line) (eq (following-char) ?#))))
Richard M. Stallman's avatar
Richard M. Stallman committed
474 475 476 477 478 479
      (insert "\t")
    ;; If pascal-tab-always-indent, indent the beginning of the line.
    (if pascal-tab-always-indent
	(save-excursion
	  (beginning-of-line)
	  (pascal-indent-line))
480 481 482 483 484
      (if (save-excursion
	    (skip-chars-backward " \t")
	    (bolp))
	  (pascal-indent-line)
	(insert "\t")))
Richard M. Stallman's avatar
Richard M. Stallman committed
485 486 487
    (pascal-indent-command)))


Richard M. Stallman's avatar
Richard M. Stallman committed
488 489 490 491 492

;;;
;;; Interactive functions
;;;
(defun pascal-insert-block ()
Richard M. Stallman's avatar
Richard M. Stallman committed
493
  "Insert Pascal begin ... end; block in the code with right indentation."
Richard M. Stallman's avatar
Richard M. Stallman committed
494 495 496 497
  (interactive)
  (insert "begin")
  (electric-pascal-terminate-line)
  (save-excursion
498
    (newline)
Richard M. Stallman's avatar
Richard M. Stallman committed
499 500 501 502 503
    (insert "end;")
    (beginning-of-line)
    (pascal-indent-line)))

(defun pascal-star-comment ()
Richard M. Stallman's avatar
Richard M. Stallman committed
504
  "Insert Pascal star comment at point."
Richard M. Stallman's avatar
Richard M. Stallman committed
505 506 507 508 509 510
  (interactive)
  (pascal-indent-line)
  (insert "(*")
  (electric-pascal-terminate-line)
  (save-excursion
    (electric-pascal-terminate-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
511 512 513
    (delete-horizontal-space)
    (insert ")"))
  (insert "  "))
Richard M. Stallman's avatar
Richard M. Stallman committed
514

Richard M. Stallman's avatar
Richard M. Stallman committed
515
(defun pascal-mark-defun ()
Richard M. Stallman's avatar
Richard M. Stallman committed
516
  "Mark the current pascal function (or procedure).
Richard M. Stallman's avatar
Richard M. Stallman committed
517
This puts the mark at the end, and point at the beginning."
Richard M. Stallman's avatar
Richard M. Stallman committed
518 519
  (interactive)
  (push-mark (point))
Richard M. Stallman's avatar
Richard M. Stallman committed
520
  (pascal-end-of-defun)
Richard M. Stallman's avatar
Richard M. Stallman committed
521
  (push-mark (point))
Richard M. Stallman's avatar
Richard M. Stallman committed
522 523 524
  (pascal-beg-of-defun)
  (if (fboundp 'zmacs-activate-region)
      (zmacs-activate-region)))
Richard M. Stallman's avatar
Richard M. Stallman committed
525 526

(defun pascal-comment-area (start end)
Richard M. Stallman's avatar
Richard M. Stallman committed
527 528 529 530 531 532 533 534 535
  "Put the region into a Pascal comment.
The comments that are in this area are \"deformed\":
`*)' becomes `!(*' and `}' becomes `!{'.
These deformed comments are returned to normal if you use
\\[pascal-uncomment-area] to undo the commenting.

The commented area starts with `pascal-exclude-str-start', and ends with
`pascal-include-str-end'.  But if you change these variables,
\\[pascal-uncomment-area] won't recognize the comments."
Richard M. Stallman's avatar
Richard M. Stallman committed
536 537 538 539 540 541 542 543
  (interactive "r")
  (save-excursion
    ;; Insert start and endcomments
    (goto-char end)
    (if (and (save-excursion (skip-chars-forward " \t") (eolp))
	     (not (save-excursion (skip-chars-backward " \t") (bolp))))
	(forward-line 1)
      (beginning-of-line))
Richard M. Stallman's avatar
Richard M. Stallman committed
544
    (insert pascal-exclude-str-end)
Richard M. Stallman's avatar
Richard M. Stallman committed
545 546 547 548
    (setq end (point))
    (newline)
    (goto-char start)
    (beginning-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
549
    (insert pascal-exclude-str-start)
Richard M. Stallman's avatar
Richard M. Stallman committed
550 551 552 553 554 555 556 557 558 559 560
    (newline)
    ;; Replace end-comments within commented area
    (goto-char end)
    (save-excursion
      (while (re-search-backward "\\*)" start t)
	(replace-match "!(*" t t)))
    (save-excursion
      (while (re-search-backward "}" start t)
	(replace-match "!{" t t)))))

(defun pascal-uncomment-area ()
Richard M. Stallman's avatar
Richard M. Stallman committed
561 562
  "Uncomment a commented area; change deformed comments back to normal.
This command does nothing if the pointer is not in a commented
Richard M. Stallman's avatar
Richard M. Stallman committed
563 564 565 566 567 568 569
area.  See also `pascal-comment-area'."
  (interactive)
  (save-excursion
    (let ((start (point))
	  (end (point)))
      ;; Find the boundaries of the comment
      (save-excursion
Richard M. Stallman's avatar
Richard M. Stallman committed
570
	(setq start (progn (search-backward pascal-exclude-str-start nil t)
Richard M. Stallman's avatar
Richard M. Stallman committed
571
			   (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
572
	(setq end (progn (search-forward pascal-exclude-str-end nil t)
Richard M. Stallman's avatar
Richard M. Stallman committed
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
			 (point))))
      ;; Check if we're really inside a comment
      (if (or (equal start (point)) (<= end (point)))
	  (message "Not standing within commented area.")
	(progn
	  ;; Remove endcomment
	  (goto-char end)
	  (beginning-of-line)
	  (let ((pos (point)))
	    (end-of-line)
	    (delete-region pos (1+ (point))))
	  ;; Change comments back to normal
	  (save-excursion
	    (while (re-search-backward "!{" start t)
	      (replace-match "}" t t)))
	  (save-excursion
	    (while (re-search-backward "!(\\*" start t)
	      (replace-match "*)" t t)))
	  ;; Remove startcomment
	  (goto-char start)
	  (beginning-of-line)
	  (let ((pos (point)))
	    (end-of-line)
	    (delete-region pos (1+ (point)))))))))

Richard M. Stallman's avatar
Richard M. Stallman committed
598 599
(defun pascal-beg-of-defun ()
  "Move backward to the beginning of the current function or procedure."
Richard M. Stallman's avatar
Richard M. Stallman committed
600
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622
  (catch 'found
    (if (not (looking-at (concat "\\s \\|\\s)\\|" pascal-defun-re)))
	(forward-sexp 1))
    (let ((nest 0) (max -1) (func 0)
	  (reg (concat pascal-beg-block-re "\\|" 
		       pascal-end-block-re "\\|"
		       pascal-defun-re)))
      (while (re-search-backward reg nil 'move)
	(cond ((let ((state (save-excursion
			      (parse-partial-sexp (point-min) (point)))))
		 (or (nth 3 state) (nth 4 state))) ; Inside string or comment
	       ())
	      ((match-end 1)                       ; begin|case|record|repeat
	       (if (and (looking-at "\\<record\\>") (>= max 0))
		   (setq func (1- func)))
	       (setq nest (1+ nest)
		     max (max nest max)))
	      ((match-end 2)                       ; end|until
	       (if (and (= nest max) (>= max 0))
		   (setq func (1+ func)))
	       (setq nest (1- nest)))
	      ((match-end 3)                       ; function|procedure
623
	       (if (= 0 func)
Richard M. Stallman's avatar
Richard M. Stallman committed
624 625 626 627 628 629
		   (throw 'found t)
		 (setq func (1- func)))))))
    nil))

(defun pascal-end-of-defun ()
  "Move forward to the end of the current function or procedure."
Richard M. Stallman's avatar
Richard M. Stallman committed
630
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658
  (if (looking-at "\\s ")
      (forward-sexp 1))
  (if (not (looking-at pascal-defun-re))
      (pascal-beg-of-defun))
  (forward-char 1)
  (let ((nest 0) (func 1)
	(reg (concat pascal-beg-block-re "\\|" 
		     pascal-end-block-re "\\|"
		     pascal-defun-re)))
    (while (and (/= func 0)
		(re-search-forward reg nil 'move))
      (cond ((let ((state (save-excursion
			      (parse-partial-sexp (point-min) (point)))))
		 (or (nth 3 state) (nth 4 state))) ; Inside string or comment
	       ())
	    ((match-end 1)
	     (setq nest (1+ nest))
	     (if (save-excursion
		   (goto-char (match-beginning 0))
		   (looking-at "\\<record\\>"))
		 (setq func (1+ func))))
	    ((match-end 2)
	     (setq nest (1- nest))
	     (if (= nest 0)
		 (setq func (1- func))))
	    ((match-end 3)
	     (setq func (1+ func))))))
  (forward-line 1))
Richard M. Stallman's avatar
Richard M. Stallman committed
659

660 661 662
(defun pascal-end-of-statement ()
  "Move forward to end of current statement."
  (interactive)
663 664
  (let ((parse-sexp-ignore-comments t)
	(nest 0) pos
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697
	(regexp (concat "\\(" pascal-beg-block-re "\\)\\|\\("
			pascal-end-block-re "\\)")))
    (if (not (looking-at "[ \t\n]")) (forward-sexp -1))
    (or (looking-at pascal-beg-block-re)
	;; Skip to end of statement
	(setq pos (catch 'found
		    (while t
		      (forward-sexp 1)
		      (cond ((looking-at "[ \t]*;")
			     (skip-chars-forward "^;")
			     (forward-char 1)
			     (throw 'found (point)))
			    ((save-excursion
			       (forward-sexp -1)
			       (looking-at pascal-beg-block-re))
			     (goto-char (match-beginning 0))
			     (throw 'found nil))
			    ((eobp)
			     (throw 'found (point))))))))
    (if (not pos)
	;; Skip a whole block
	(catch 'found
	  (while t
	    (re-search-forward regexp nil 'move)
	    (setq nest (if (match-end 1) 
			   (1+ nest)
			 (1- nest)))
	    (cond ((eobp)
		   (throw 'found (point)))
		  ((= 0 nest)
		   (throw 'found (pascal-end-of-statement))))))
      pos)))

Richard M. Stallman's avatar
Richard M. Stallman committed
698
(defun pascal-downcase-keywords ()
Richard M. Stallman's avatar
Richard M. Stallman committed
699
  "Downcase all Pascal keywords in the buffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
700 701 702 703
  (interactive)
  (pascal-change-keywords 'downcase-word))

(defun pascal-upcase-keywords ()
Richard M. Stallman's avatar
Richard M. Stallman committed
704
  "Upcase all Pascal keywords in the buffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
705 706 707 708
  (interactive)
  (pascal-change-keywords 'upcase-word))

(defun pascal-capitalize-keywords ()
Richard M. Stallman's avatar
Richard M. Stallman committed
709
  "Capitalize all Pascal keywords in the buffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
710 711 712
  (interactive)
  (pascal-change-keywords 'capitalize-word))

Richard M. Stallman's avatar
Richard M. Stallman committed
713
;; Change the keywords according to argument.
Richard M. Stallman's avatar
Richard M. Stallman committed
714 715
(defun pascal-change-keywords (change-word)
  (save-excursion
Richard M. Stallman's avatar
Richard M. Stallman committed
716 717 718 719 720 721 722 723
    (let ((keyword-re (concat "\\<\\("
			      (mapconcat 'identity pascal-keywords "\\|")
			      "\\)\\>")))
      (goto-char (point-min))
      (while (re-search-forward keyword-re nil t)
	(funcall change-word -1)))))


Richard M. Stallman's avatar
Richard M. Stallman committed
724 725 726 727 728

;;;
;;; Other functions
;;;
(defun pascal-set-auto-comments ()
Richard M. Stallman's avatar
Richard M. Stallman committed
729 730 731 732
  "Insert `{ case }' or `{ NAME }' on this line if appropriate.
Insert `{ case }' if there is an `end' on the line which
ends a case block.  Insert `{ NAME }' if there is an `end'
on the line which ends a function or procedure named NAME."
Richard M. Stallman's avatar
Richard M. Stallman committed
733 734 735
  (save-excursion
    (forward-line -1)
    (skip-chars-forward " \t")
Richard M. Stallman's avatar
Richard M. Stallman committed
736
    (if (and (looking-at "\\<end;")
Richard M. Stallman's avatar
Richard M. Stallman committed
737 738
	     (not (save-excursion
		    (end-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
739
		    (search-backward "{" (pascal-get-beg-of-line) t))))
740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774
	(let ((type (car (pascal-calculate-indent))))
	  (if (eq type 'declaration)
	      ()
	    (if (eq type 'case)
		;; This is a case block
		(progn
		  (end-of-line)
		  (delete-horizontal-space)
		  (insert " { case }"))
	      (let ((nest 1))
		;; Check if this is the end of a function
		(save-excursion
		  (while (not (or (looking-at pascal-defun-re) (bobp)))
		    (backward-sexp 1)
		    (cond ((looking-at pascal-beg-block-re)
			   (setq nest (1- nest)))
			  ((looking-at pascal-end-block-re)
			   (setq nest (1+ nest)))))
		  (if (bobp)
		      (setq nest 1)))
		(if (zerop nest)
		    (progn
		      (end-of-line)
		      (delete-horizontal-space)
		      (insert " { ")
		      (let (b e)
			(save-excursion
			  (setq b (progn (pascal-beg-of-defun)
					 (skip-chars-forward "^ \t")
					 (skip-chars-forward " \t")
					 (point))
				e (progn (skip-chars-forward "a-zA-Z0-9_")
					 (point))))
			(insert-buffer-substring (current-buffer) b e))
		      (insert " }"))))))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
775

Richard M. Stallman's avatar
Richard M. Stallman committed
776 777


Richard M. Stallman's avatar
Richard M. Stallman committed
778
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
779 780 781 782 783
;;; Indentation
;;;
(defconst pascal-indent-alist
  '((block . (+ ind pascal-indent-level))
    (case . (+ ind pascal-case-indent))
784
    (caseblock . ind) (cpp . 0)
Richard M. Stallman's avatar
Richard M. Stallman committed
785 786 787 788
    (declaration . (+ ind pascal-indent-level))
    (paramlist . (pascal-indent-paramlist t))
    (comment . (pascal-indent-comment t))
    (defun . ind) (contexp . ind)
789
    (unknown . ind) (string . 0) (progbeg . 0)))
Richard M. Stallman's avatar
Richard M. Stallman committed
790

Richard M. Stallman's avatar
Richard M. Stallman committed
791
(defun pascal-indent-command ()
Richard M. Stallman's avatar
Richard M. Stallman committed
792 793 794 795
  "Indent for special part of code."
  (let* ((indent-str (pascal-calculate-indent))
	 (type (car indent-str))
	 (ind (car (cdr indent-str))))
796 797 798
    (cond ((and (eq type 'paramlist)
		(or (memq 'all pascal-auto-lineup)
		    (memq 'paramlist pascal-auto-lineup)))
Richard M. Stallman's avatar
Richard M. Stallman committed
799 800
	   (pascal-indent-paramlist)
	   (pascal-indent-paramlist))
801 802 803
	  ((and (eq type 'declaration)
		(or (memq 'all pascal-auto-lineup)
		    (memq 'declaration  pascal-auto-lineup)))
Richard M. Stallman's avatar
Richard M. Stallman committed
804
	   (pascal-indent-declaration))
805 806 807
	  ((and (eq type 'case) (not (looking-at "^[ \t]*$"))
		(or (memq 'all pascal-auto-lineup)
		    (memq 'case pascal-auto-lineup)))
Richard M. Stallman's avatar
Richard M. Stallman committed
808 809 810
	   (pascal-indent-case)))
    (if (looking-at "[ \t]+$")
	(skip-chars-forward " \t"))))
Richard M. Stallman's avatar
Richard M. Stallman committed
811 812

(defun pascal-indent-line ()
Richard M. Stallman's avatar
Richard M. Stallman committed
813 814 815 816
  "Indent current line as a Pascal statement."
  (let* ((indent-str (pascal-calculate-indent))
	 (type (car indent-str))
	 (ind (car (cdr indent-str))))
817 818 819
    ;; Labels should not be indented.
    (if (and (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
	     (not (eq type 'declaration)))
Richard M. Stallman's avatar
Richard M. Stallman committed
820 821
	(search-forward ":" nil t))
    (delete-horizontal-space)
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837
    (cond (; Some things should not be indented
	   (or (and (eq type 'declaration) (looking-at pascal-declaration-re))
	       (eq type 'cpp))
	   ())
	  (; Other things should have no extra indent
	   (looking-at pascal-noindent-re)
	   (indent-to ind))
	  (; Nested functions should be indented
	   (looking-at pascal-defun-re)
	   (if (and pascal-indent-nested-functions
		    (eq type 'defun))
	       (indent-to (+ ind pascal-indent-level))
	     (indent-to ind)))
	  (; But most lines are treated this way
	   (indent-to (eval (cdr (assoc type pascal-indent-alist))))
	   ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
838 839 840 841 842

(defun pascal-calculate-indent ()
  "Calculate the indent of the current Pascal line.
Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
  (save-excursion
843 844
    (let* ((parse-sexp-ignore-comments t)
	   (oldpos (point))
Richard M. Stallman's avatar
Richard M. Stallman committed
845
	   (state (save-excursion (parse-partial-sexp (point-min) (point))))
846
	   (nest 0) (par 0) (complete (looking-at "[ \t]*end\\>"))
847 848
	   (elsed (looking-at "[ \t]*else\\>")) (funccnt 0)
	   (did-func (looking-at "[ \t]*\\(procedure\\|function\\)\\>"))
Richard M. Stallman's avatar
Richard M. Stallman committed
849 850 851 852 853 854
	   (type (catch 'nesting
		   ;; Check if inside a string, comment or parenthesis
		   (cond ((nth 3 state) (throw 'nesting 'string))
			 ((nth 4 state) (throw 'nesting 'comment))
			 ((> (car state) 0)
			  (goto-char (scan-lists (point) -1 (car state)))
855 856 857 858
			  (setq par (1+ (current-column))))
			 ((save-excursion (beginning-of-line)
					  (eq (following-char) ?#))
			  (throw 'nesting 'cpp)))
Richard M. Stallman's avatar
Richard M. Stallman committed
859 860 861
		   ;; Loop until correct indent is found
		   (while t
		     (backward-sexp 1)
862 863 864 865 866 867 868 869 870 871 872 873 874
		     (cond (;--Escape from case statements
			    (and (looking-at "[A-Za-z0-9]+[ \t]*:[^=]")
				 (not complete)
				 (save-excursion (skip-chars-backward " \t")
						 (bolp))
				 (= (save-excursion
				      (end-of-line) (backward-sexp) (point))
				    (point))
				 (> (save-excursion (goto-char oldpos)
						    (beginning-of-line)
						    (point))
				    (point)))
			    (throw 'nesting 'caseblock))
875 876 877 878 879 880
			   (;--Beginning of program
			    (looking-at pascal-progbeg-re)
			    (throw 'nesting 'progbeg))
			   (;--No known statements
			    (bobp)
			    (throw 'nesting 'progbeg))
881
			   (;--Nest block outwards
Richard M. Stallman's avatar
Richard M. Stallman committed
882 883 884 885 886 887
			    (looking-at pascal-beg-block-re)
			    (if (= nest 0)
				(cond ((looking-at "case\\>")
				       (throw 'nesting 'case))
				      ((looking-at "record\\>")
				       (throw 'nesting 'declaration))
888
				      (t (throw 'nesting 'block)))
889 890
			      (if (and (looking-at "record\\>") (= nest 1))
				  (setq funccnt (1- funccnt)))
Richard M. Stallman's avatar
Richard M. Stallman committed
891 892 893
			      (setq nest (1- nest))))
			   (;--Nest block inwards
			    (looking-at pascal-end-block-re)
894 895 896
			    (if (and (looking-at "end\\s ")
				     elsed (not complete))
				(throw 'nesting 'block))
897 898
			    (if (= nest 0)
				(setq funccnt (1+ funccnt)))
Richard M. Stallman's avatar
Richard M. Stallman committed
899 900 901
			    (setq complete t
				  nest (1+ nest)))
			   (;--Defun (or parameter list)
902 903 904 905 906 907 908
			    (and (looking-at pascal-defun-re)
				 (progn (setq funccnt (1- funccnt)
					      did-func t)
					(or (bolp) (< funccnt 0))))
			    ;; Prevent searching whole buffer
			    (if (and (bolp) (>= funccnt 0))
				(throw 'nesting 'progbeg))
Richard M. Stallman's avatar
Richard M. Stallman committed
909 910 911 912 913 914 915 916 917 918 919 920 921
			    (if (= 0 par)
				(throw 'nesting 'defun)
			      (setq par 0)
			      (let ((n 0))
				(while (re-search-forward
					"\\(\\<record\\>\\)\\|\\<end\\>"
					oldpos t)
				  (if (match-end 1)
				      (setq n (1+ n)) (setq n (1- n))))
				(if (> n 0)
				    (throw 'nesting 'declaration)
				  (throw 'nesting 'paramlist)))))
			   (;--Declaration part
922 923 924
			    (and (looking-at pascal-declaration-re)
				 (not did-func)
				 (= funccnt 0))
925 926 927 928
			    (if (save-excursion
				  (goto-char oldpos)
				  (forward-line -1)
				  (looking-at "^[ \t]*$"))
Richard M. Stallman's avatar
Richard M. Stallman committed
929 930 931 932 933 934 935 936 937 938 939
				(throw 'nesting 'unknown)
			      (throw 'nesting 'declaration)))
			   (;--If, else or while statement
			    (and (not complete)
				 (looking-at pascal-sub-block-re))
			    (throw 'nesting 'block))
			   (;--Found complete statement
			    (save-excursion (forward-sexp 1)
					    (= (following-char) ?\;))
			    (setq complete t))
			   )))))
940

Richard M. Stallman's avatar
Richard M. Stallman committed
941 942 943 944 945 946 947 948
      ;; Return type of block and indent level.
      (if (> par 0)                               ; Unclosed Parenthesis 
	  (list 'contexp par)
	(list type (pascal-indent-level))))))

(defun pascal-indent-level ()
  "Return the indent-level the current statement has.
Do not count labels, case-statements or records."
Richard M. Stallman's avatar
Richard M. Stallman committed
949 950
  (save-excursion
    (beginning-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
951 952 953 954
    (if (looking-at "[ \t]*[0-9a-zA-Z]+[ \t]*:[^=]")
	(search-forward ":" nil t)
      (if (looking-at ".*=[ \t]*record\\>")
	  (search-forward "=" nil t)))
Richard M. Stallman's avatar
Richard M. Stallman committed
955 956 957
    (skip-chars-forward " \t")
    (current-column)))

Richard M. Stallman's avatar
Richard M. Stallman committed
958 959 960 961
(defun pascal-indent-comment (&optional arg)
  "Indent current line as comment.
If optional arg is non-nil, just return the
column number the line should be indented to."
962 963 964 965 966 967
  (let* ((stcol (save-excursion
		  (re-search-backward "(\\*\\|{" nil t)
		  (1+ (current-column)))))
    (if arg stcol
      (delete-horizontal-space)
      (indent-to stcol))))
Richard M. Stallman's avatar
Richard M. Stallman committed
968 969 970

(defun pascal-indent-case ()
  "Indent within case statements."
971 972
  (let ((savepos (point-marker))
	(end (prog2
973 974
		 (end-of-line)
		 (point-marker)
Richard M. Stallman's avatar
Richard M. Stallman committed
975
	       (re-search-backward "\\<case\\>" nil t)))
976
	(beg (point)) oldpos
Richard M. Stallman's avatar
Richard M. Stallman committed
977 978
	(ind 0))
    ;; Get right indent
979
    (while (< (point) end)
980 981 982 983
      (if (re-search-forward 
	   "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
	   (marker-position end) 'move)
	  (forward-char -1))
984
      (if (< (point) end)
985 986 987 988 989
	  (progn
	    (delete-horizontal-space)
	    (if (> (current-column) ind)
		(setq ind (current-column)))
	    (pascal-end-of-statement))))
Richard M. Stallman's avatar
Richard M. Stallman committed
990
    (goto-char beg)
991
    (setq oldpos (marker-position end))
Richard M. Stallman's avatar
Richard M. Stallman committed
992
    ;; Indent all case statements
993
    (while (< (point) end)
994
      (if (re-search-forward
995
	   "^[ \t]*[^][ \t,\\.:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
996
	   (marker-position end) 'move)
Richard M. Stallman's avatar
Richard M. Stallman committed
997 998 999 1000 1001 1002
	  (forward-char -1))
      (indent-to (1+ ind))
      (if (/= (following-char) ?:)
	  ()
	(forward-char 1)
	(delete-horizontal-space)
1003 1004 1005
	(insert " "))
      (setq oldpos (point))
      (pascal-end-of-statement))
1006
    (goto-char savepos)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1007 1008 1009 1010 1011

(defun pascal-indent-paramlist (&optional arg)
  "Indent current line in parameterlist.
If optional arg is non-nil, just return the
indent of the current line in parameterlist."
Richard M. Stallman's avatar
Richard M. Stallman committed
1012
  (save-excursion
Richard M. Stallman's avatar
Richard M. Stallman committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
    (let* ((oldpos (point))
	   (stpos (progn (goto-char (scan-lists (point) -1 1)) (point)))
	   (stcol (1+ (current-column)))
	   (edpos (progn (pascal-declaration-end) 
			 (search-backward ")" (pascal-get-beg-of-line) t)
			 (point)))
	   (usevar (re-search-backward "\\<var\\>" stpos t)))
      (if arg (progn
		;; If arg, just return indent
		(goto-char oldpos)
		(beginning-of-line)
		(if (or (not usevar) (looking-at "[ \t]*var\\>"))
		    stcol (+ 4 stcol)))
	(goto-char stpos)
	(forward-char 1)
	(delete-horizontal-space)
	(if (and usevar (not (looking-at "var\\>")))
	    (indent-to (+ 4 stcol)))
	(pascal-indent-declaration nil stpos edpos)))))

(defun pascal-indent-declaration (&optional arg start end)
  "Indent current lines as declaration, lining up the `:'s or `='s."
  (let ((pos (point-marker)))
    (if (and (not (or arg start)) (not (pascal-declaration-beg)))
	()
      (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start) 
			":" "="))
	    (stpos (if start start
		       (forward-word 2) (backward-word 1) (point)))
	    (edpos (set-marker (make-marker)
			       (if end end
				 (max (progn (pascal-declaration-end)
					     (point))
				      pos))))
	    ind)

	(goto-char stpos)
	;; Indent lines in record block
	(if arg
1052
	    (while (<= (point) edpos)
Richard M. Stallman's avatar
Richard M. Stallman committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
	      (beginning-of-line)
	      (delete-horizontal-space)
	      (if (looking-at "end\\>")
		  (indent-to arg)
		(indent-to (+ arg pascal-indent-level)))
	      (forward-line 1)))

	;; Do lineup
	(setq ind (pascal-get-lineup-indent stpos edpos lineup))
	(goto-char stpos)
1063
	(while (and (<= (point) edpos) (not (eobp)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
	  (if (search-forward lineup (pascal-get-end-of-line) 'move)
	      (forward-char -1))
	  (delete-horizontal-space)
	  (indent-to ind)
	  (if (not (looking-at lineup))
	      (forward-line 1) ; No more indent if there is no : or =
	    (forward-char 1)
	    (delete-horizontal-space)
	    (insert " ")
	    ;; Indent record block
	    (if (looking-at "record\\>")
		(pascal-indent-declaration (current-column)))
	    (forward-line 1)))))

    ;; If arg - move point
    (if arg (forward-line -1)
1080
      (goto-char pos))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1081 1082 1083 1084 1085 1086

;  "Return the indent level that will line up several lines within the region
;from b to e nicely. The lineup string is str."
(defun pascal-get-lineup-indent (b e str)
  (save-excursion
    (let ((ind 0)
1087
	  (reg (concat str "\\|\\(\\<record\\>\\)\\|" pascal-defun-re)))
Richard M. Stallman's avatar
Richard M. Stallman committed
1088 1089 1090
      (goto-char b)
      ;; Get rightmost position
      (while (< (point) e)
1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105
	(and (re-search-forward reg (min e (pascal-get-end-of-line 2)) 'move)
	     (cond ((match-beginning 1)
		    ;; Skip record blocks
		    (pascal-declaration-end))
		   ((match-beginning 2)
		    ;; We have entered a new procedure.  Exit.
		    (goto-char e))
		   (t
		    (goto-char (match-beginning 0))
		    (skip-chars-backward " \t")
		    (if (> (current-column) ind)
			(setq ind (current-column)))
		    (goto-char (match-end 0))
		    (end-of-line)
		    ))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
      ;; In case no lineup was found
      (if (> ind 0)
	  (1+ ind)
	;; No lineup-string found
	(goto-char b)
	(end-of-line)
	(skip-chars-backward " \t")
	(1+ (current-column))))))
    


;;;
;;; Completion
;;;
1120 1121 1122 1123 1124 1125
(defvar pascal-str nil)
(defvar pascal-all nil)
(defvar pascal-pred nil)
(defvar pascal-buffer-to-use nil)
(defvar pascal-flag nil)

Richard M. Stallman's avatar
Richard M. Stallman committed
1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
(defun pascal-string-diff (str1 str2)
  "Return index of first letter where STR1 and STR2 differs."
  (catch 'done
    (let ((diff 0))
      (while t
	(if (or (> (1+ diff) (length str1))
		(> (1+ diff) (length str2)))
	    (throw 'done diff))
	(or (equal (aref str1 diff) (aref str2 diff))
	    (throw 'done diff))
	(setq diff (1+ diff))))))

;; Calculate all possible completions for functions if argument is `function',
;; completions for procedures if argument is `procedure' or both functions and
;; procedures otherwise.

(defun pascal-func-completion (type)
  ;; Build regular expression for function/procedure names
1144 1145 1146 1147 1148 1149 1150
  (if (string= pascal-str "")
      (setq pascal-str "[a-zA-Z_]"))
  (let ((pascal-str (concat (cond
			     ((eq type 'procedure) "\\<\\(procedure\\)\\s +")
			     ((eq type 'function) "\\<\\(function\\)\\s +")
			     (t "\\<\\(function\\|procedure\\)\\s +"))
			    "\\<\\(" pascal-str "[a-zA-Z0-9_.]*\\)\\>"))
Richard M. Stallman's avatar
Richard M. Stallman committed
1151
	match)
1152
    
Richard M. Stallman's avatar
Richard M. Stallman committed
1153 1154 1155 1156 1157 1158
    (if (not (looking-at "\\<\\(function\\|procedure\\)\\>"))
	(re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t))
    (forward-char 1)

    ;; Search through all reachable functions
    (while (pascal-beg-of-defun)