pascal.el 51.6 KB
Newer Older
Richard M. Stallman's avatar
Richard M. Stallman committed
1 2
;;; pascal.el  -  Major mode for editing pascal source in emacs.

Richard M. Stallman's avatar
Richard M. Stallman committed
3
;;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
Richard M. Stallman's avatar
Richard M. Stallman committed
4

Richard M. Stallman's avatar
Richard M. Stallman committed
5 6
;;; Author: Espen Skoglund (espensk@stud.cs.uit.no)
;;; 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

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

Richard M. Stallman's avatar
Richard M. Stallman committed
15 16 17 18
;;; This program is distributed in the hope that it will be useful,
;;; 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 21 22
;;; You should have received a copy of the GNU General Public License
;;; along with this program; if not, write to the Free Software
;;; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Richard M. Stallman's avatar
Richard M. Stallman committed
23 24 25 26 27

;;; Commentary:

;;; USAGE
;;; =====
Richard M. Stallman's avatar
Richard M. Stallman committed
28 29 30

;;; 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
Richard M. Stallman's avatar
Richard M. Stallman committed
31
;;; C-h m. You may also get online help describing various functions by:
Richard M. Stallman's avatar
Richard M. Stallman committed
32 33 34 35 36 37 38 39 40 41 42
;;; C-h f <Name of function you want described>

;;; 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
43
;;;       pascal-auto-lineup        '(all)
Richard M. Stallman's avatar
Richard M. Stallman committed
44 45 46 47 48 49
;;;       pascal-toggle-completions nil
;;;       pascal-type-keywords      '("array" "file" "packed" "char" 
;;; 				      "integer" "real" "string" "record")
;;;       pascal-start-keywords     '("begin" "end" "function" "procedure"
;;; 				      "repeat" "until" "while" "read" "readln"
;;; 				      "reset" "rewrite" "write" "writeln")
50
;;;       pascal-separator-keywords '("downto" "else" "mod" "div" "then"))
Richard M. Stallman's avatar
Richard M. Stallman committed
51 52 53 54

;;; KNOWN BUGS / BUGREPORTS
;;; =======================
;;; As far as I know, there are no bugs in the current version of this
Richard M. Stallman's avatar
Richard M. Stallman committed
55 56 57 58 59 60
;;; 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
;;; as well as to bug-gnu-emacs@prep.ai.mit.edu.

;;; Code:
Richard M. Stallman's avatar
Richard M. Stallman committed
61

62 63
(defconst pascal-mode-version "2.3"
  "Version of `pascal.el'.")
Richard M. Stallman's avatar
Richard M. Stallman committed
64 65 66 67 68 69 70

(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
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
(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)
  (define-key pascal-mode-map "\r"       'electric-pascal-terminate-line)
  (define-key pascal-mode-map "\t"       'electric-pascal-tab)
  (define-key pascal-mode-map "\e\t"     'pascal-complete-word)
  (define-key pascal-mode-map "\e?"      'pascal-show-completions)
  (define-key pascal-mode-map "\177"     'backward-delete-char-untabify)
  (define-key pascal-mode-map "\e\C-h"   'pascal-mark-defun)
  (define-key pascal-mode-map "\C-cb"    'pascal-insert-block)
  (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)
  (define-key pascal-mode-map "\e\C-a"   'pascal-beg-of-defun)
  (define-key pascal-mode-map "\e\C-e"   'pascal-end-of-defun)
  (define-key pascal-mode-map "\C-cg"    'pascal-goto-defun)
  (define-key pascal-mode-map "\C-c\C-o"  'pascal-outline)
Richard M. Stallman's avatar
Richard M. Stallman committed
92 93
;;; 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
;  (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)
  )
  
(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\\)\\>")
(defconst pascal-defun-re       "\\<\\(function\\|procedure\\|program\\)\\>")
116
(defconst pascal-sub-block-re   "\\<\\(if\\|else\\|for\\|while\\|with\\)\\>")
117
(defconst pascal-noindent-re    "\\<\\(begin\\|end\\|until\\|else\\)\\>")
Richard M. Stallman's avatar
Richard M. Stallman committed
118 119 120 121 122 123 124
(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
125 126 127 128 129 130 131

(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))
Richard M. Stallman's avatar
Richard M. Stallman committed
132 133 134
  (modify-syntax-entry ?\\ "\\"  pascal-mode-syntax-table)
  (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
135
  (modify-syntax-entry ?* ". 23" pascal-mode-syntax-table)
Richard M. Stallman's avatar
Richard M. Stallman committed
136 137 138 139 140 141 142 143 144 145 146 147 148
  (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)
  (modify-syntax-entry ?_ "w"    pascal-mode-syntax-table)
  (modify-syntax-entry ?\' "\""  pascal-mode-syntax-table))

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
(defvar pascal-font-lock-keywords
  (list
   '("^[ \t]*\\(function\\|pro\\(cedure\\|gram\\)\\)\\>[ \t]*\\(\\sw+\\)?"
     (1 font-lock-keyword-face) (3 font-lock-function-name-face nil t))
;   ("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)
   '("\\<\\(label\\|external\\|forward\\)\\>" . font-lock-reference-face)
   '("\\<\\([0-9]+\\)[ \t]*:" 1 font-lock-reference-face)
;   ("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]+\\)?"
     (1 font-lock-keyword-face) (2 font-lock-reference-face nil t)))
  "Additional expressions to highlight in Pascal mode.")

Richard M. Stallman's avatar
Richard M. Stallman committed
170
(defvar pascal-indent-level 3
Richard M. Stallman's avatar
Richard M. Stallman committed
171
  "*Indentation of Pascal statements with respect to containing block.")
172

Richard M. Stallman's avatar
Richard M. Stallman committed
173 174
(defvar pascal-case-indent 2
  "*Indentation for case statements.")
175

Richard M. Stallman's avatar
Richard M. Stallman committed
176 177 178
(defvar pascal-auto-newline nil
  "*Non-nil means automatically newline after simcolons and the punctation mark
after an end.")
179

Richard M. Stallman's avatar
Richard M. Stallman committed
180 181 182
(defvar pascal-tab-always-indent t
  "*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.")
183

Richard M. Stallman's avatar
Richard M. Stallman committed
184 185 186
(defvar pascal-auto-endcomments t
  "*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.")
187 188 189 190 191 192 193 194 195

(defvar pascal-auto-lineup '(all)
  "*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)
will do all lineups.")

Richard M. Stallman's avatar
Richard M. Stallman committed
196 197 198 199 200
(defvar pascal-toggle-completions nil
  "*Non-nil means that \\<pascal-mode-map>\\[pascal-complete-label] should \
not display a completion buffer when
the label couldn't be completed, but instead toggle the possible completions
with repeated \\[pascal-complete-label]'s.")
201

Richard M. Stallman's avatar
Richard M. Stallman committed
202 203 204 205 206
(defvar pascal-type-keywords
  '("array" "file" "packed" "char" "integer" "real" "string" "record")
  "*Keywords for types used when completing a word in a declaration or parmlist.
\(eg. integer, real, char.)  The types defined within the Pascal program
will be completed runtime, and should not be added to this list.")
207

Richard M. Stallman's avatar
Richard M. Stallman committed
208 209 210 211 212 213 214
(defvar pascal-start-keywords
  '("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.
\(eg. begin, repeat, until, readln.)
The procedures and variables defined within the Pascal program
will be completed runtime and should not be added to this list.")
215

216
(defvar pascal-separator-keywords
Richard M. Stallman's avatar
Richard M. Stallman committed
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  '("downto" "else" "mod" "div" "then")
  "*Keywords to complete when NOT standing at the first word of a statement.
\(eg. downto, else, mod, then.) 
Variables and function names defined within the
Pascal program are completed runtime and should not be added to this list.")

;;;
;;;  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)))
	    ((match-beginning 2) (setq nest (1- nest)))))))

246

Richard M. Stallman's avatar
Richard M. Stallman committed
247 248 249
(defun pascal-declaration-beg ()
  (let ((nest 1))
    (while (and (> nest 0)
250
		(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
251 252 253 254
      (cond ((match-beginning 1) (setq nest 0))
	    ((match-beginning 2) (setq nest (1- nest)))
	    ((match-beginning 3) (setq nest (1+ nest)))))
    (= nest 0)))
255

Richard M. Stallman's avatar
Richard M. Stallman committed
256 257 258 259 260
  
(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
261 262 263

;;;###autoload
(defun pascal-mode ()
Richard M. Stallman's avatar
Richard M. Stallman committed
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
  "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:

 pascal-indent-level      (default 3)
    Indentation of Pascal statements with respect to containing block.
 pascal-case-indent       (default 2)
    Indentation for case statements.
 pascal-auto-newline      (default nil)
    Non-nil means automatically newline after simcolons and the punctation mark
    after an end.
293
 pascal-tab-always-indent (default t)
Richard M. Stallman's avatar
Richard M. Stallman committed
294 295
    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.
Richard M. Stallman's avatar
Richard M. Stallman committed
296 297 298
 pascal-auto-endcomments  (default t)
    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.
299 300
 pascal-auto-lineup       (default t)
    List of contexts where auto lineup of :'s or ='s hould be done.
Richard M. Stallman's avatar
Richard M. Stallman committed
301 302

See also the user variables pascal-type-keywords, pascal-start-keywords and
303
pascal-separator-keywords.
Richard M. Stallman's avatar
Richard M. Stallman committed
304 305 306 307 308 309 310 311 312 313 314 315

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)
Richard M. Stallman's avatar
Richard M. Stallman committed
316
  (setq comment-indent-function 'pascal-indent-comment)
Richard M. Stallman's avatar
Richard M. Stallman committed
317 318 319 320
  (make-local-variable 'parse-sexp-ignore-comments)
  (setq parse-sexp-ignore-comments t)
  (make-local-variable 'case-fold-search)
  (setq case-fold-search t)
321 322 323 324 325 326
  (make-local-variable 'comment-start-skip)
  (setq comment-start-skip "(\\*+ *\\|{ *")
  (make-local-variable 'comment-end)
  (setq comment-end "}")
  (make-local-variable 'font-lock-keywords)
  (setq font-lock-keywords pascal-font-lock-keywords)
Richard M. Stallman's avatar
Richard M. Stallman committed
327 328
  (run-hooks 'pascal-mode-hook))

Richard M. Stallman's avatar
Richard M. Stallman committed
329 330


Richard M. Stallman's avatar
Richard M. Stallman committed
331 332 333 334 335 336
;;;
;;;  Electric functions
;;;
(defun electric-pascal-terminate-line ()
  "Terminate line and indent next line."
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
337
  ;; First, check if current line should be indented
Richard M. Stallman's avatar
Richard M. Stallman committed
338 339 340
  (save-excursion
    (beginning-of-line)
    (skip-chars-forward " \t")
Richard M. Stallman's avatar
Richard M. Stallman committed
341
    (if (looking-at pascal-autoindent-lines-re)
Richard M. Stallman's avatar
Richard M. Stallman committed
342
	(pascal-indent-line)))
Richard M. Stallman's avatar
Richard M. Stallman committed
343
  (delete-horizontal-space) ; Removes trailing whitespaces
Richard M. Stallman's avatar
Richard M. Stallman committed
344
  (newline)
Richard M. Stallman's avatar
Richard M. Stallman committed
345
  ;; Indent next line
Richard M. Stallman's avatar
Richard M. Stallman committed
346 347 348 349 350 351 352 353 354
  (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
355
      (cond ((looking-at "\\*[ \t]+)")
Richard M. Stallman's avatar
Richard M. Stallman committed
356 357
	     ;; Delete region between `*' and `)' if there is only whitespaces.
	     (forward-char 1)
Richard M. Stallman's avatar
Richard M. Stallman committed
358
	     (delete-horizontal-space))
Richard M. Stallman's avatar
Richard M. Stallman committed
359 360 361 362 363
	    ((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
364 365 366
    (if (null setstar)	
	(pascal-indent-line)
      (insert "*  "))))
Richard M. Stallman's avatar
Richard M. Stallman committed
367 368


Richard M. Stallman's avatar
Richard M. Stallman committed
369 370
(defun electric-pascal-semi-or-dot ()
  "Insert `;' or `.' character and reindent the line."
Richard M. Stallman's avatar
Richard M. Stallman committed
371 372 373 374 375 376 377 378 379
  (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
380
  "Insert `:' and do all indentions except line indent on this line."
Richard M. Stallman's avatar
Richard M. Stallman committed
381 382
  (interactive)
  (insert last-command-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
383 384 385 386 387 388 389 390 391
  ;; 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
392
(defun electric-pascal-equal ()
Richard M. Stallman's avatar
Richard M. Stallman committed
393
  "Insert `=', and do indention if within type declaration."
Richard M. Stallman's avatar
Richard M. Stallman committed
394 395
  (interactive)
  (insert last-command-char)
Richard M. Stallman's avatar
Richard M. Stallman committed
396
  (if (eq (car (pascal-calculate-indent)) 'declaration)
Richard M. Stallman's avatar
Richard M. Stallman committed
397 398 399 400
      (let ((pascal-tab-always-indent nil))
	(pascal-indent-command))))

(defun electric-pascal-tab ()
Richard M. Stallman's avatar
Richard M. Stallman committed
401
  "Function called when TAB is pressed in Pascal mode."
Richard M. Stallman's avatar
Richard M. Stallman committed
402 403
  (interactive)
  ;; Do nothing if within a string.
Richard M. Stallman's avatar
Richard M. Stallman committed
404 405 406 407 408 409 410 411 412 413 414
  (if (pascal-within-string)
      (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))
      (insert "\t"))
    (pascal-indent-command)))


Richard M. Stallman's avatar
Richard M. Stallman committed
415 416 417 418 419

;;;
;;; Interactive functions
;;;
(defun pascal-insert-block ()
Richard M. Stallman's avatar
Richard M. Stallman committed
420
  "Insert Pascal begin ... end; block in the code with right indentation."
Richard M. Stallman's avatar
Richard M. Stallman committed
421 422 423 424 425 426 427 428 429 430 431
  (interactive)
  (pascal-indent-line)
  (insert "begin")
  (electric-pascal-terminate-line)
  (save-excursion
    (electric-pascal-terminate-line)
    (insert "end;")
    (beginning-of-line)
    (pascal-indent-line)))

(defun pascal-star-comment ()
Richard M. Stallman's avatar
Richard M. Stallman committed
432
  "Insert Pascal star comment at point."
Richard M. Stallman's avatar
Richard M. Stallman committed
433 434 435 436 437 438
  (interactive)
  (pascal-indent-line)
  (insert "(*")
  (electric-pascal-terminate-line)
  (save-excursion
    (electric-pascal-terminate-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
439 440 441
    (delete-horizontal-space)
    (insert ")"))
  (insert "  "))
Richard M. Stallman's avatar
Richard M. Stallman committed
442

Richard M. Stallman's avatar
Richard M. Stallman committed
443
(defun pascal-mark-defun ()
Richard M. Stallman's avatar
Richard M. Stallman committed
444
  "Mark the current pascal function (or procedure).
Richard M. Stallman's avatar
Richard M. Stallman committed
445
This puts the mark at the end, and point at the beginning."
Richard M. Stallman's avatar
Richard M. Stallman committed
446 447
  (interactive)
  (push-mark (point))
Richard M. Stallman's avatar
Richard M. Stallman committed
448
  (pascal-end-of-defun)
Richard M. Stallman's avatar
Richard M. Stallman committed
449
  (push-mark (point))
Richard M. Stallman's avatar
Richard M. Stallman committed
450 451 452
  (pascal-beg-of-defun)
  (if (fboundp 'zmacs-activate-region)
      (zmacs-activate-region)))
Richard M. Stallman's avatar
Richard M. Stallman committed
453 454

(defun pascal-comment-area (start end)
Richard M. Stallman's avatar
Richard M. Stallman committed
455 456 457 458 459 460 461 462 463
  "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
464 465 466 467 468 469 470 471
  (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
472
    (insert pascal-exclude-str-end)
Richard M. Stallman's avatar
Richard M. Stallman committed
473 474 475 476
    (setq end (point))
    (newline)
    (goto-char start)
    (beginning-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
477
    (insert pascal-exclude-str-start)
Richard M. Stallman's avatar
Richard M. Stallman committed
478 479 480 481 482 483 484 485 486 487 488
    (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
489 490
  "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
491 492 493 494 495 496 497
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
498
	(setq start (progn (search-backward pascal-exclude-str-start nil t)
Richard M. Stallman's avatar
Richard M. Stallman committed
499
			   (point)))
Richard M. Stallman's avatar
Richard M. Stallman committed
500
	(setq end (progn (search-forward pascal-exclude-str-end nil t)
Richard M. Stallman's avatar
Richard M. Stallman committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
			 (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
526 527
(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
528
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557
  (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
	       (if (= 0 func)
		   (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
558
  (interactive)
Richard M. Stallman's avatar
Richard M. Stallman committed
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
  (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
587

588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
(defun pascal-end-of-statement ()
  "Move forward to end of current statement."
  (interactive)
  (let ((nest 0) pos
	(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
625
(defun pascal-downcase-keywords ()
Richard M. Stallman's avatar
Richard M. Stallman committed
626
  "Downcase all Pascal keywords in the buffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
627 628 629 630
  (interactive)
  (pascal-change-keywords 'downcase-word))

(defun pascal-upcase-keywords ()
Richard M. Stallman's avatar
Richard M. Stallman committed
631
  "Upcase all Pascal keywords in the buffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
632 633 634 635
  (interactive)
  (pascal-change-keywords 'upcase-word))

(defun pascal-capitalize-keywords ()
Richard M. Stallman's avatar
Richard M. Stallman committed
636
  "Capitalize all Pascal keywords in the buffer."
Richard M. Stallman's avatar
Richard M. Stallman committed
637 638 639
  (interactive)
  (pascal-change-keywords 'capitalize-word))

Richard M. Stallman's avatar
Richard M. Stallman committed
640
;; Change the keywords according to argument.
Richard M. Stallman's avatar
Richard M. Stallman committed
641 642
(defun pascal-change-keywords (change-word)
  (save-excursion
Richard M. Stallman's avatar
Richard M. Stallman committed
643 644 645 646 647 648 649 650
    (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
651 652 653 654 655

;;;
;;; Other functions
;;;
(defun pascal-set-auto-comments ()
Richard M. Stallman's avatar
Richard M. Stallman committed
656 657 658 659
  "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
660 661 662
  (save-excursion
    (forward-line -1)
    (skip-chars-forward " \t")
Richard M. Stallman's avatar
Richard M. Stallman committed
663
    (if (and (looking-at "\\<end;")
Richard M. Stallman's avatar
Richard M. Stallman committed
664 665
	     (not (save-excursion
		    (end-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
666
		    (search-backward "{" (pascal-get-beg-of-line) t))))
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 698 699 700 701
	(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
702

Richard M. Stallman's avatar
Richard M. Stallman committed
703 704


Richard M. Stallman's avatar
Richard M. Stallman committed
705
;;;
Richard M. Stallman's avatar
Richard M. Stallman committed
706 707 708 709 710 711 712 713 714 715 716
;;; Indentation
;;;
(defconst pascal-indent-alist
  '((block . (+ ind pascal-indent-level))
    (case . (+ ind pascal-case-indent))
    (declaration . (+ ind pascal-indent-level))
    (paramlist . (pascal-indent-paramlist t))
    (comment . (pascal-indent-comment t))
    (defun . ind) (contexp . ind)
    (unknown . 0) (string . 0)))

Richard M. Stallman's avatar
Richard M. Stallman committed
717
(defun pascal-indent-command ()
Richard M. Stallman's avatar
Richard M. Stallman committed
718 719 720 721
  "Indent for special part of code."
  (let* ((indent-str (pascal-calculate-indent))
	 (type (car indent-str))
	 (ind (car (cdr indent-str))))
722 723 724
    (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
725 726
	   (pascal-indent-paramlist)
	   (pascal-indent-paramlist))
727 728 729
	  ((and (eq type 'declaration)
		(or (memq 'all pascal-auto-lineup)
		    (memq 'declaration  pascal-auto-lineup)))
Richard M. Stallman's avatar
Richard M. Stallman committed
730
	   (pascal-indent-declaration))
731 732 733
	  ((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
734 735 736
	   (pascal-indent-case)))
    (if (looking-at "[ \t]+$")
	(skip-chars-forward " \t"))))
Richard M. Stallman's avatar
Richard M. Stallman committed
737 738

(defun pascal-indent-line ()
Richard M. Stallman's avatar
Richard M. Stallman committed
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762
  "Indent current line as a Pascal statement."
  (let* ((indent-str (pascal-calculate-indent))
	 (type (car indent-str))
	 (ind (car (cdr indent-str))))
    (if (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
	(search-forward ":" nil t))
    (delete-horizontal-space)
    ;; Some thing should not be indented
    (if (or (and (eq type 'declaration) (looking-at pascal-declaration-re))
	    (looking-at pascal-defun-re))
	()
      ;; Other things should have no extra indent
      (if (looking-at pascal-noindent-re)
	  (indent-to ind)
	;; But most lines are treated this way:
	(indent-to (eval (cdr (assoc type pascal-indent-alist))))
	))))

(defun pascal-calculate-indent ()
  "Calculate the indent of the current Pascal line.
Return a list of two elements: (INDENT-TYPE INDENT-LEVEL)."
  (save-excursion
    (let* ((oldpos (point))
	   (state (save-excursion (parse-partial-sexp (point-min) (point))))
763 764
	   (nest 0) (par 0) (complete nil)
	   (elsed (looking-at "[ \t]*else\\>"))
Richard M. Stallman's avatar
Richard M. Stallman committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	   (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)))
			  (setq par (1+ (current-column)))))
		   ;; Loop until correct indent is found
		   (while t
		     (backward-sexp 1)
		     (cond (;--Nest block outwards
			    (looking-at pascal-beg-block-re)
			    (if (= nest 0)
				(cond ((looking-at "case\\>")
				       (throw 'nesting 'case))
				      ((looking-at "record\\>")
				       (throw 'nesting 'declaration))
782
				      (t (throw 'nesting 'block)))
Richard M. Stallman's avatar
Richard M. Stallman committed
783 784 785
			      (setq nest (1- nest))))
			   (;--Nest block inwards
			    (looking-at pascal-end-block-re)
786 787 788
			    (if (and (looking-at "end\\s ")
				     elsed (not complete))
				(throw 'nesting 'block))
Richard M. Stallman's avatar
Richard M. Stallman committed
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806
			    (setq complete t
				  nest (1+ nest)))
			   (;--Defun (or parameter list)
			    (looking-at pascal-defun-re)
			    (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
			    (looking-at pascal-declaration-re)
807 808 809 810
			    (if (save-excursion
				  (goto-char oldpos)
				  (forward-line -1)
				  (looking-at "^[ \t]*$"))
Richard M. Stallman's avatar
Richard M. Stallman committed
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
				(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))
			   (;--No known statements
			    (bobp)
			    (throw 'nesting 'unknown))
			   )))))
      ;; 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
833 834
  (save-excursion
    (beginning-of-line)
Richard M. Stallman's avatar
Richard M. Stallman committed
835 836 837 838
    (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
839 840 841
    (skip-chars-forward " \t")
    (current-column)))

Richard M. Stallman's avatar
Richard M. Stallman committed
842 843 844 845 846 847 848 849 850 851 852 853 854 855
(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."
    (let* ((stcol (save-excursion
		    (re-search-backward "(\\*\\|{" nil t)
		    (1+ (current-column)))))
      (if arg stcol
	(delete-horizontal-space)
	(indent-to stcol))))

(defun pascal-indent-case ()
  "Indent within case statements."
  (skip-chars-forward ": \t")
856 857 858
  (let ((end (prog2
		 (end-of-line)
		 (point-marker)
Richard M. Stallman's avatar
Richard M. Stallman committed
859
	       (re-search-backward "\\<case\\>" nil t)))
860
	(beg (point)) oldpos
Richard M. Stallman's avatar
Richard M. Stallman committed
861 862 863
	(ind 0))
    ;; Get right indent
    (while (< (point) (marker-position end))
864 865 866 867
      (if (re-search-forward 
	   "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
	   (marker-position end) 'move)
	  (forward-char -1))
Richard M. Stallman's avatar
Richard M. Stallman committed
868 869 870
      (delete-horizontal-space)
      (if (> (current-column) ind)
	  (setq ind (current-column)))
871
      (pascal-end-of-statement))
Richard M. Stallman's avatar
Richard M. Stallman committed
872
    (goto-char beg)
873
    (setq oldpos (marker-position end))
Richard M. Stallman's avatar
Richard M. Stallman committed
874 875
    ;; Indent all case statements
    (while (< (point) (marker-position end))
876 877 878
      (if (re-search-forward
	   "^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
	   (marker-position end) 'move)
Richard M. Stallman's avatar
Richard M. Stallman committed
879 880 881 882 883 884
	  (forward-char -1))
      (indent-to (1+ ind))
      (if (/= (following-char) ?:)
	  ()
	(forward-char 1)
	(delete-horizontal-space)
885 886 887 888
	(insert " "))
      (setq oldpos (point))
      (pascal-end-of-statement))
    (goto-char oldpos)))
Richard M. Stallman's avatar
Richard M. Stallman committed
889 890 891 892 893

(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
894
  (save-excursion
Richard M. Stallman's avatar
Richard M. Stallman committed
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
    (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
	    (while (<= (point) (marker-position edpos))
	      (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)
	(while (<= (point) (marker-position edpos))
	  (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)
      (goto-char (marker-position pos)))))

;  "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)
	  (reg (concat str "\\|\\(\\<record\\>\\)"))
	  nest)
      (goto-char b)
      ;; Get rightmost position
      (while (< (point) e)
	(setq nest 1)
	(if (re-search-forward reg (min e (pascal-get-end-of-line 2)) 'move)
976 977 978 979 980 981 982 983 984 985
	    (progn
	      ;; Skip record blocks
	      (if (match-beginning 1)
		  (pascal-declaration-end)
		(progn
		  (goto-char (match-beginning 0))
		  (skip-chars-backward " \t")
		  (if (> (current-column) ind)
		      (setq ind (current-column)))
		  (goto-char (match-end 0)))))))
Richard M. Stallman's avatar
Richard M. Stallman committed
986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024
      ;; 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
;;;
(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
  (if (string= str "")
      (setq str "[a-zA-Z_]"))
  (let ((str (concat (cond ((eq type 'procedure) "\\<\\(procedure\\)\\s +")
			   ((eq type 'function) "\\<\\(function\\)\\s +")
			   (t "\\<\\(function\\|procedure\\)\\s +"))
		     "\\<\\(" str "[a-zA-Z0-9_.]*\\)\\>"))
	match)
Richard M. Stallman's avatar
Richard M. Stallman committed
1025
  
Richard M. Stallman's avatar
Richard M. Stallman committed
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 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075
    (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)
      (if (re-search-forward str (pascal-get-end-of-line) t)
	  (progn (setq match (buffer-substring (match-beginning 2)
					       (match-end 2)))
		 (if (or (null predicate)
			 (funcall prdicate match))
		     (setq all (cons match all)))))
      (goto-char (match-beginning 0)))))

(defun pascal-get-completion-decl ()
  ;; Macro for searching through current declaration (var, type or const)
  ;; for matches of `str' and adding the occurence tp `all'
  (let ((end (save-excursion (pascal-declaration-end)
			     (point)))
	match)
    ;; Traverse lines
    (while (< (point) end)
      (if (re-search-forward "[:=]" (pascal-get-end-of-line) t)
	  ;; Traverse current line
	  (while (and (re-search-backward 
		       (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|" 
			       pascal-symbol-re)
		       (pascal-get-beg-of-line) t)
		      (not (match-end 1)))
	    (setq match (buffer-substring (match-beginning 0) (match-end 0)))
	    (if (string-match (concat "\\<" str) match)
		(if (or (null predicate)
			(funcall predicate match))
		    (setq all (cons match all))))))
      (if (re-search-forward "\\<record\\>" (pascal-get-end-of-line) t)
	  (pascal-declaration-end)
	(forward-line 1)))))

(defun pascal-type-completion ()
  "Calculate all possible completions for types."
  (let ((start (point))
	goon)
    ;; Search for all reachable type declarations
    (while (or (pascal-beg-of-defun)
	       (setq goon (not goon)))
      (save-excursion
	(if (and (< start (prog1 (save-excursion (pascal-end-of-defun)
						 (point))
			    (forward-char 1)))
		 (re-search-forward
1076
		  "\\<type\\>\\|\\<\\(begin\\|function\\|procedure\\)\\>"
Richard M. Stallman's avatar
Richard M. Stallman committed
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139
		  start t)
		 (not (match-end 1)))
	    ;; Check current type declaration
	    (pascal-get-completion-decl))))))

(defun pascal-var-completion ()
  "Calculate all possible completions for variables (or constants)."
  (let ((start (point))
	goon twice)
    ;; Search for all reachable var declarations
    (while (or (pascal-beg-of-defun)
	       (setq goon (not goon)))
      (save-excursion
	(if (> start (prog1 (save-excursion (pascal-end-of-defun)
					    (point))))
	    () ; Declarations not reacable
	  (if (search-forward "(" (pascal-get-end-of-line) t)
	      ;; Check parameterlist
		(pascal-get-completion-decl))
	  (setq twice 2)
	  (while (>= (setq twice (1- twice)) 0)
	    (cond ((and (re-search-forward
			 (concat "\\<\\(var\\|const\\)\\>\\|"
				 "\\<\\(begin\\|function\\|procedure\\)\\>")
			 start t)
			(not (match-end 2)))
		   ;; Check var/const declarations
		   (pascal-get-completion-decl))
		  ((match-end 2)
		   (setq twice 0)))))))))


(defun pascal-keyword-completion (keyword-list)
  "Give list of all possible completions of keywords in KEYWORD-LIST."
  (mapcar '(lambda (s) 
	     (if (string-match (concat "\\<" str) s)
		 (if (or (null predicate)
			 (funcall predicate s))
		     (setq all (cons s all)))))
	  keyword-list))

;; Function passed to completing-read, try-completion or
;; all-completions to get completion on STR. If predicate is non-nil,
;; it must be a function to be called for every match to check if this
;; should really be a match. If flag is t, the function returns a list
;; of all possible completions. If it is nil it returns a string, the
;; longest possible completion, or t if STR is an exact match. If flag
;; is 'lambda, the function returns t if STR is an exact match, nil
;; otherwise.

(defun pascal-completion (str predicate flag)
  (save-excursion
    (let ((all nil))
      ;; Set buffer to use for searching labels. This should be set
      ;; within functins which use pascal-completions
      (set-buffer buffer-to-use)

      ;; Determine what should be completed
      (let ((state (car (pascal-calculate-indent))))
	(cond (;--Within a declaration or parameterlist
	       (or (eq state 'declaration) (eq state 'paramlist)
		   (and (eq state 'defun)
			(save-excursion
1140 1141
			  (re-search-backward ")[ \t]*:"
					      (pascal-get-beg-of-line) t))))
Richard M. Stallman's avatar
Richard M. Stallman committed
1142 1143 1144 1145 1146
	       (if (or (eq state 'paramlist) (eq state 'defun))
		   (pascal-beg-of-defun))
	       (pascal-type-completion)
	       (pascal-keyword-completion pascal-type-keywords))
	      (;--Starting a new statement
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	       (and (not (eq state 'contexp))
		    (save-excursion
		      (skip-chars-backward "a-zA-Z0-9_.")
		      (backward-sexp 1)
		      (or (looking-at pascal-nosemi-re)
			  (progn
			    (forward-sexp 1)
			    (looking-at "\\s *\\(;\\|:[^=]\\)")))))
	       (save-excursion (pascal-var-completion))
	       (pascal-func-completion 'procedure)
	       (pascal-keyword-completion pascal-start-keywords))
Richard M. Stallman's avatar
Richard M. Stallman committed
1158 1159 1160
	      (t;--Anywhere else
	       (save-excursion (pascal-var-completion))
	       (pascal-func-completion 'function)
1161
	       (pascal-keyword-completion pascal-separator-keywords))))
1162
      
Richard M. Stallman's avatar
Richard M. Stallman committed
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207
      ;; Now we have built a list of all matches. Give response to caller
      (pascal-completion-response))))

(defun pascal-completion-response ()
  (cond ((or (equal flag 'lambda) (null flag))
	 ;; This was not called by all-completions
	 (if (null all)
	     ;; Return nil if there was no matching label
	     nil
	   ;; Get longest string common in the labels
	   (let* ((elm (cdr all))
		  (match (car all))
		  (min (length match))
		  exact tmp)
	     (if (string= match str)
		 ;; Return t if first match was an exact match
		 (setq match t)
	       (while (not (null elm))
		 ;; Find longest common string
		 (if (< (setq tmp (pascal-string-diff match (car elm))) min)
		     (progn
		       (setq min tmp)
		       (setq match (substring match 0 min))))
		 ;; Terminate with match=t if this is an exact match
		 (if (string= (car elm) str)
		     (progn
		       (setq match t)
		       (setq elm nil))
		   (setq elm (cdr elm)))))
	     ;; If this is a test just for exact match, return nil ot t
	     (if (and (equal flag 'lambda) (not (equal match 't)))
		 nil
	       match))))
	;; If flag is t, this was called by all-completions. Return
	;; list of all possible completions
	(flag
	 all)))

(defvar pascal-last-word-numb 0)
(defvar pascal-last-word-shown nil)
(defvar pascal-last-completions nil)

(defun pascal-complete-word ()
  "Complete word at current point.
\(See also `pascal-toggle-completions', `pascal-type-keywords',
1208
`pascal-start-keywords' and `pascal-separator-keywords'.)"
Richard M. Stallman's avatar
Richard M. Stallman committed
1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226
  (interactive)
  (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
	 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
	 (str (buffer-substring b e))
	 ;; The following variable is used in pascal-completion
	 (buffer-to-use (current-buffer))
	 (allcomp (if (and pascal-toggle-completions
			   (string= pascal-last-word-shown str))
		      pascal-last-completions
		    (all-completions str 'pascal-completion)))
	 (match (if pascal-toggle-completions
		    "" (try-completion
			str (mapcar '(lambda (elm) (cons elm 0)) allcomp)))))
    ;; Delete old string
    (delete-region b e)

    ;; Toggle-completions inserts whole labels
    (if pascal-toggle-completions
Richard M. Stallman's avatar
Richard M. Stallman committed
1227
	(progn
Richard M. Stallman's avatar
Richard M. Stallman committed
1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	  ;; Update entry number in list
	  (setq pascal-last-completions allcomp
		pascal-last-word-numb 
		(if (>= pascal-last-word-numb (1- (length allcomp)))
		    0
		  (1+ pascal-last-word-numb)))
	  (setq pascal-last-word-shown (elt allcomp pascal-last-word-numb))
	  ;; Display next match or same string if no match was found
	  (if (not (null allcomp))
	      (insert "" pascal-last-word-shown)
	    (insert "" str)
	    (message "(No match)")))
      ;; The other form of completion does not necessarly do that.

      ;; Insert match if found, or the original string if no match
      (if (or (null match) (equal match 't))
	  (progn (insert "" str)
		 (message "(No match)"))
	(insert "" match))
      ;; Give message about current status of completion
      (cond ((equal match 't)
	     (if (not (null (cdr allcomp)))
		 (message "(Complete but not unique)")
	       (message "(Sole completion)")))
	    ;; Display buffer if the current completion didn't help 
	    ;; on completing the label.
	    ((and (not (null (cdr allcomp))) (= (length str) (length match)))
	     (with-output-to-temp-buffer "*Completions*"
	       (display-completion-list allcomp))
	     ;; Wait for a keypress. Then delete *Completion*  window
	     (momentary-string-display "" (point))
	     (delete-window (get-buffer-window (get-buffer "*Completions*")))
	     )))))

(defun pascal-show-completions ()
  "Show all possible completions at current point."
  (interactive)
  (let* ((b (save-excursion (skip-chars-backward "a-zA-Z0-9_") (point)))
	 (e (save-excursion (skip-chars-forward "a-zA-Z0-9_") (point)))
	 (str (buffer-substring b e))
	 ;; The following variable is used in pascal-completion
	 (buffer-to-use (current-buffer))
	 (allcomp (if (and pascal-toggle-completions
			   (string= pascal-last-word-shown str))
		      pascal-last-completions
		    (all-completions str 'pascal-completion))))
    ;; Show possible completions in a temporary buffer.
    (with-output-to-temp-buffer "*Completions*"
      (display-completion-list allcomp))
    ;; Wait for a keypress. Then delete *Completion*  window
    (momentary-string-display "" (point))
    (delete-window (get-buffer-window (get-buffer "*Completions*")))))


(defun pascal-get-default-symbol ()
  "Return symbol around current point as a string."
  (save-excursion
    (buffer-substring (progn
			(skip-chars-backward " \t")
			(skip-chars-backward "a-zA-Z0-9_")
			(point))
		      (progn
			(skip-chars-forward "a-zA-Z0-9_")
			(point)))))

(defun pascal-build-defun-re (str &optional arg)
  "Return function/procedure starting with STR as regular expression.
With optional second arg non-nil, STR is the complete name of the instruction."
  (if arg
      (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "\\)\\>")
    (concat "^\\(function\\|procedure\\)[ \t]+\\(" str "[a-zA-Z0-9_]*\\)\\>")))

;; Function passed to completing-read, try-completion or
;; all-completions to get completion on any function name. If
;; predicate is non-nil, it must be a function to be called for every
;; match to check if this should really be a match. If flag is t, the
;; function returns a list of all possible completions. If it is nil
;; it returns a string, the longest possible completion, or t if STR
;; is an exact match. If flag is 'lambda, the function returns t if
;; STR is an exact match, nil otherwise.

(defun pascal-comp-defun (str predicate flag)
  (save-excursion
    (let ((all nil)
	  match)

      ;; Set buffer to use for searching labels. This should be set
      ;; within functins which use pascal-completions
      (set-buffer buffer-to-use)

      (let ((str str))
	;; Build regular expression for functions
	(if (string= str "")
	    (setq str (pascal-build-defun-re "[a-zA-Z_]"))
	  (setq str (pascal-build-defun-re str)))
	(goto-char (point-min))
      
	;; Build a list of all possible completions
	(while (re-search-forward str nil t)
	  (setq match (buffer-substring (match-beginning 2) (match-end 2)))
	  (if (or (null predicate)
		  (funcall predicate match))
	      (setq all (cons match all)))))

      ;; Now we have built a list of all matches. Give response to caller
      (pascal-completion-response))))

(defun pascal-goto-defun ()
  "Move to specified Pascal function/procedure.
The default is a name found in the buffer around point."
  (interactive)
  (let* ((default (pascal-get-default-symbol))
	 ;; The following variable is used in pascal-comp-function
	 (buffer-to-use (current-buffer))
	 (default (if (pascal-comp-defun default nil 'lambda)
		      default ""))
	 (label (if (not (string= default ""))
		    ;; Do completion with default
		    (completing-read (concat "Label: (default " default ") ")
				     'pascal-comp-defun nil t "")
		  ;; There is no default value. Complete without it
		  (completing-read "Label: "
				   'pascal-comp-defun nil t ""))))
    ;; If there was no response on prompt, use default value
    (if (string= label "")
	(setq label default))
    ;; Goto right place in buffer if label is not an empty string
    (or (string= label "")
	(progn
	  (goto-char (point-min))
	  (re-search-forward (pascal-build-defun-re label t))
	  (beginning-of-line)))))



;;;
;;; Pascal-outline-mode
;;;
(defvar pascal-outline-map nil "Keymap used in Pascal Outline mode.")

(if pascal-outline-map
    nil
  (if (boundp 'set-keymap-name)
      (set-keymap-name pascal-outline-map 'pascal-outline-map))
  (if (not (boundp 'set-keymap-parent))
      (setq pascal-outline-map (copy-keymap pascal-mode-map))
    (setq pascal-outline-map (make-sparse-keymap))
    (set-keymap-parent pascal-outline-map pascal-mode-map))
  (define-key pascal-outline-map "\e\C-a"   'pascal-outline-prev-defun)
  (define-key pascal-outline-map "\e\C-e"   'pascal-outline-next-defun)
1378
  (define-key pascal-outline-map "\C-c\C-d" 'pascal-outline-goto-defun)
Richard M. Stallman's avatar
Richard M. Stallman committed
1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
  (define-key pascal-outline-map "\C-c\C-s" 'pascal-show-all)
  (define-key pascal-outline-map "\C-c\C-h" 'pascal-hide-other-defuns))

(defvar pascal-outline-mode nil "Non-nil while using Pascal Outline mode.")
(make-variable-buffer-local 'pascal-outline-mode)
(set-default 'pascal-outline-mode nil)
(if (not (assoc 'pascal-outline-mode minor-mode-alist))
    (setq minor-mode-alist (append minor-mode-alist
				   (list '(pascal-outline-mode " Outl")))))

(defun pascal-outline (&optional arg)
  "Outline-line minor mode for Pascal mode.
When in Pascal Outline mode, portions
of the text being edited may be made invisible. \\<pascal-outline-map>

Pascal Outline mode provides some additional commands.

\\[pascal-outline-prev-defun]\
\t- Move to previous function/procedure, hiding everything else.
\\[pascal-outline-next-defun]\
\t- Move to next function/procedure, hiding everything else.
\\[pascal-outline-goto-defun]\
\t- Goto function/procedure prompted for in minibuffer,
\t  hide all other functions.
\\[pascal-show-all]\t- Show the whole buffer.
\\[pascal-hide-other-defuns]\
\t- Hide everything but the current function (function under the cursor).
\\[pascal-outline]\t- Leave pascal-outline-mode."
  (interactive "P")
  (setq pascal-outline-mode
	(if (null arg) (not pascal-outline-mode) t))
  (if (boundp 'redraw-mode-line)
      (redraw-mode-line))
  (if pascal-outline-mode
      (progn
	(setq selective-display t)
	(use-local-map pascal-outline-map))
    (progn
      (setq selective-display nil)
      (pascal-show-all)
      (use-local-map pascal-mode-map))))

(defun pascal-outline-change (b e flag)
  (let ((modp (buffer-modified-p)))
    (unwind-protect
	(subst-char-in-region b e (if (= flag ?\n) ?\^M ?\n) flag)
      (set-buffer-modified-p modp))))

(defun pascal-show-all ()
  "Show all of the text in the buffer."
  (interactive)
  (pascal-outline-change (point-min) (point-max) ?\n))

(defun pascal-hide-other-defuns ()
  "Show only the current defun."
  (interactive)
  (save-excursion
    (let ((beg (progn (if (not (looking-at "\\(function\\|procedure\\)\\>"))
			  (pascal-beg-of-defun))
		      (point)))
	  (end (progn (pascal-end-of-defun)
		      (backward-sexp 1)
		      (search-forward "\n\\|\^M" nil t)
		      (point)))
	  (opoint (point-min)))
      (goto-char (point-min))

      ;; Hide all functions before current function
      (while (re-search-forward "^\\(function\\|procedure\\)\\>" beg 'move)
	(pascal-outline-change opoint (1- (match-beginning 0)) ?\^M)
	(setq opoint (point))
	;; Functions may be nested
	(if (> (progn (pascal-end-of-defun) (point)) beg)
	    (goto-char opoint)))
      (if (> beg opoint)
	  (pascal-outline-change opoint (1- beg) ?\^M))

      ;; Show current function
      (pascal-outline-change beg end ?\n)
      ;; Hide nested functions
      (forward-char 1)
      (while (re-search-forward "^\\(function\\|procedure\\)\\>" end 'move)
	(setq opoint (point))
	(pascal-end-of-defun)
	(pascal-outline-change opoint (point) ?\^M))

      (goto-char end)
      (setq opoint end)

      ;; Hide all function after current function
      (while (re-search-forward "^\\(function\\|procedure\\)\\>" nil 'move)
	(pascal-outline-change opoint (1- (match-beginning 0)) ?\^M)
	(setq opoint (point))
	(pascal-end-of-defun))
      (pascal-outline-change opoint (point-max) ?\^M)

      ;; Hide main program
      (if (< (progn (forward-line -1) (point)) end)
Richard M. Stallman's avatar
Richard M. Stallman committed
1477
	  (progn