;;; pascal.el --- major mode for editing pascal source in Emacs
-;; Copyright (C) 1993, 1994, 1995, 1996, 1997 Free Software Foundation, Inc.
+;; Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
+;; 2003, 2004, 2005 Free Software Foundation, Inc.
-;; Author: Espen Skoglund <espensk@stud.cs.uit.no>
+;; Author: Espen Skoglund <esk@gnu.org>
;; Keywords: languages
;; This file is part of GNU Emacs.
;; You should have received a copy of the GNU General Public License
;; 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.
+;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+;; Boston, MA 02110-1301, USA.
;;; Commentary:
;; pascal-auto-endcomments t
;; pascal-auto-lineup '(all)
;; pascal-toggle-completions nil
-;; pascal-type-keywords '("array" "file" "packed" "char"
+;; pascal-type-keywords '("array" "file" "packed" "char"
;; "integer" "real" "string" "record")
;; pascal-start-keywords '("begin" "end" "function" "procedure"
;; "repeat" "until" "while" "read" "readln"
;; 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
-;; as well as to bug-gnu-emacs@prep.ai.mit.edu.
+;; find any bugs, you may submit them to: esk@gnu.org as well as to
+;; bug-gnu-emacs@gnu.org.
\f
;;; Code:
(defgroup pascal nil
- "Major mode for editing Pascal source in Emacs"
+ "Major mode for editing Pascal source in Emacs."
:group 'languages)
(defvar pascal-mode-abbrev-table nil
)
(defvar pascal-imenu-generic-expression
- '("^[ \t]*\\(function\\|procedure\\)[ \t\n]+\\([a-zA-Z0-9_.:]+\\)" . (2))
+ '((nil "^[ \t]*\\(function\\|procedure\\)[ \t\n]+\\([a-zA-Z0-9_.:]+\\)" 2))
"Imenu expression for Pascal-mode. See `imenu-generic-expression'.")
(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"
+ '("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"
(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-progbeg-re "\\<\\program\\>")
(defconst pascal-defun-re "\\<\\(function\\|procedure\\|program\\)\\>")
(defconst pascal-sub-block-re "\\<\\(if\\|else\\|for\\|while\\|with\\)\\>")
(defconst pascal-noindent-re "\\<\\(begin\\|end\\|until\\|else\\)\\>")
()
(setq pascal-mode-syntax-table (make-syntax-table))
(modify-syntax-entry ?\\ "." pascal-mode-syntax-table)
- (modify-syntax-entry ?( "()1" pascal-mode-syntax-table)
+ (modify-syntax-entry ?( "()1" pascal-mode-syntax-table)
(modify-syntax-entry ?) ")(4" pascal-mode-syntax-table)
- (modify-syntax-entry ?* ". 23" pascal-mode-syntax-table)
+ (modify-syntax-entry ?* ". 23b" 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)
(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)
+ '("\\<\\(label\\|external\\|forward\\)\\>" . font-lock-constant-face)
'("\\<\\([0-9]+\\)[ \t]*:" 1 font-lock-function-name-face)
; ("of" "to" "for" "if" "then" "else" "case" "while"
; "do" "until" "and" "or" "not" "in" "with" "repeat" "begin" "end")
:type 'boolean
:group 'pascal)
+(defcustom pascal-indent-nested-functions t
+ "*Non-nil means nested functions are indented."
+ :type 'boolean
+ :group 'pascal)
+
(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
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."
- :type '(repeat (choice (const all)
- (const paramlist)
- (const declaration)
- (const case)))
+ :type '(set :extra-offset 8
+ (const :tag "Everything" all)
+ (const :tag "Parameter lists" paramlist)
+ (const :tag "Decalrations" declaration)
+ (const :tag "Case statements" case))
:group 'pascal)
(defcustom pascal-toggle-completions nil
"*Keywords to complete when standing at the first word of a statement.
These are keywords such as begin, repeat, until, readln.
The procedures and variables defined within the Pascal program
-are handled in another way, and should not be added to this list."
+are handled in another way, and should not be added to this list."
:type '(repeat (string :tag "Keyword"))
:group 'pascal)
(defun pascal-declaration-end ()
(let ((nest 1))
(while (and (> nest 0)
- (re-search-forward
- "[:=]\\|\\(\\<record\\>\\)\\|\\(\\<end\\>\\)"
+ (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)))
((match-beginning 3) (setq nest (1+ nest)))))
(= nest 0)))
-
+
(defsubst pascal-within-string ()
(save-excursion
(nth 3 (parse-partial-sexp (pascal-get-beg-of-line) (point)))))
Variables controlling indentation/edit style:
- pascal-indent-level (default 3)
+ pascal-indent-level (default 3)
Indentation of Pascal statements with respect to containing block.
- pascal-case-indent (default 2)
+ pascal-case-indent (default 2)
Indentation for case statements.
- pascal-auto-newline (default nil)
+ pascal-auto-newline (default nil)
Non-nil means automatically newline after semicolons and the punctuation
mark after an end.
+ pascal-indent-nested-functions (default t)
+ Non-nil means nested functions are indented.
pascal-tab-always-indent (default 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.
- pascal-auto-endcomments (default t)
+ 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.
- pascal-auto-lineup (default t)
+ pascal-auto-lineup (default t)
List of contexts where auto lineup of :'s or ='s should be done.
See also the user variables pascal-type-keywords, pascal-start-keywords and
(setq comment-indent-function 'pascal-indent-comment)
(make-local-variable 'parse-sexp-ignore-comments)
(setq parse-sexp-ignore-comments nil)
+ (make-local-variable 'blink-matching-paren-dont-ignore-comments)
+ (setq blink-matching-paren-dont-ignore-comments t)
(make-local-variable 'case-fold-search)
(setq case-fold-search t)
(make-local-variable 'comment-start)
;; Imenu support
(make-local-variable 'imenu-generic-expression)
(setq imenu-generic-expression pascal-imenu-generic-expression)
- (run-hooks 'pascal-mode-hook))
+ (setq imenu-case-fold-search t)
+ (run-mode-hooks 'pascal-mode-hook))
\f
(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.
- (if (null setstar)
+ (if (null setstar)
(pascal-indent-line)
(insert "* "))))
(defun pascal-insert-block ()
"Insert Pascal begin ... end; block in the code with right indentation."
(interactive)
- (pascal-indent-line)
(insert "begin")
(electric-pascal-terminate-line)
(save-excursion
- (electric-pascal-terminate-line)
+ (newline)
(insert "end;")
(beginning-of-line)
(pascal-indent-line)))
(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 "\\|"
+ (reg (concat pascal-beg-block-re "\\|"
pascal-end-block-re "\\|"
pascal-defun-re)))
(while (re-search-backward reg nil 'move)
(pascal-beg-of-defun))
(forward-char 1)
(let ((nest 0) (func 1)
- (reg (concat pascal-beg-block-re "\\|"
+ (reg (concat pascal-beg-block-re "\\|"
pascal-end-block-re "\\|"
pascal-defun-re)))
(while (and (/= func 0)
(catch 'found
(while t
(re-search-forward regexp nil 'move)
- (setq nest (if (match-end 1)
+ (setq nest (if (match-end 1)
(1+ nest)
(1- nest)))
(cond ((eobp)
(caseblock . ind) (cpp . 0)
(declaration . (+ ind pascal-indent-level))
(paramlist . (pascal-indent-paramlist t))
- (comment . (pascal-indent-comment t))
+ (comment . (pascal-indent-comment))
(defun . ind) (contexp . ind)
- (unknown . 0) (string . 0)))
+ (unknown . ind) (string . 0) (progbeg . 0)))
(defun pascal-indent-command ()
"Indent for special part of code."
(let* ((indent-str (pascal-calculate-indent))
(type (car indent-str))
(ind (car (cdr indent-str))))
- (if (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
+ ;; Labels should not be indented.
+ (if (and (looking-at "^[0-9a-zA-Z]+[ \t]*:[^=]")
+ (not (eq type 'declaration)))
(search-forward ":" nil t))
(delete-horizontal-space)
- ;; Some things should not be indented
- (if (or (and (eq type 'declaration) (looking-at pascal-declaration-re))
- (eq type 'cpp)
- (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))))
- ))))
+ (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))))
+ ))))
(defun pascal-calculate-indent ()
"Calculate the indent of the current Pascal line.
(oldpos (point))
(state (save-excursion (parse-partial-sexp (point-min) (point))))
(nest 0) (par 0) (complete (looking-at "[ \t]*end\\>"))
- (elsed (looking-at "[ \t]*else\\>"))
+ (elsed (looking-at "[ \t]*else\\>")) (funccnt 0)
+ (did-func (looking-at "[ \t]*\\(procedure\\|function\\)\\>"))
(type (catch 'nesting
;; Check if inside a string, comment or parenthesis
(cond ((nth 3 state) (throw 'nesting 'string))
(point))
(point)))
(throw 'nesting 'caseblock))
+ (;--Beginning of program
+ (looking-at pascal-progbeg-re)
+ (throw 'nesting 'progbeg))
+ (;--No known statements
+ (bobp)
+ (throw 'nesting 'progbeg))
(;--Nest block outwards
(looking-at pascal-beg-block-re)
(if (= nest 0)
((looking-at "record\\>")
(throw 'nesting 'declaration))
(t (throw 'nesting 'block)))
+ (if (and (looking-at "record\\>") (= nest 1))
+ (setq funccnt (1- funccnt)))
(setq nest (1- nest))))
(;--Nest block inwards
(looking-at pascal-end-block-re)
(if (and (looking-at "end\\s ")
elsed (not complete))
(throw 'nesting 'block))
+ (if (= nest 0)
+ (setq funccnt (1+ funccnt)))
(setq complete t
nest (1+ nest)))
(;--Defun (or parameter list)
- (looking-at pascal-defun-re)
+ (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))
(if (= 0 par)
(throw 'nesting 'defun)
(setq par 0)
(throw 'nesting 'declaration)
(throw 'nesting 'paramlist)))))
(;--Declaration part
- (looking-at pascal-declaration-re)
+ (and (looking-at pascal-declaration-re)
+ (not did-func)
+ (= funccnt 0))
(if (save-excursion
(goto-char oldpos)
(forward-line -1)
(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
+ (if (> par 0) ; Unclosed Parenthesis
(list 'contexp par)
(list type (pascal-indent-level))))))
(skip-chars-forward " \t")
(current-column)))
-(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-comment ()
+ "Return indent for current comment."
+ (save-excursion
+ (re-search-backward "\\((\\*\\)\\|{" nil t)
+ (if (match-beginning 1)
+ (1+ (current-column))
+ (current-column))))
(defun pascal-indent-case ()
"Indent within case statements."
(ind 0))
;; Get right indent
(while (< (point) end)
- (if (re-search-forward
+ (if (re-search-forward
"^[ \t]*[^ \t,:]+[ \t]*\\(,[ \t]*[^ \t,:]+[ \t]*\\)*:"
(marker-position end) 'move)
(forward-char -1))
(let* ((oldpos (point))
(stpos (progn (goto-char (scan-lists (point) -1 1)) (point)))
(stcol (1+ (current-column)))
- (edpos (progn (pascal-declaration-end)
+ (edpos (progn (pascal-declaration-end)
(search-backward ")" (pascal-get-beg-of-line) t)
(point)))
(usevar (re-search-backward "\\<var\\>" stpos t)))
(let ((pos (point-marker)))
(if (and (not (or arg start)) (not (pascal-declaration-beg)))
()
- (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start)
+ (let ((lineup (if (or (looking-at "\\<var\\>\\|\\<record\\>") arg start)
":" "="))
(stpos (if start start
(forward-word 2) (backward-word 1) (point)))
(defun pascal-get-lineup-indent (b e str)
(save-excursion
(let ((ind 0)
- (reg (concat str "\\|\\(\\<record\\>\\)")))
+ (reg (concat str "\\|\\(\\<record\\>\\)\\|" pascal-defun-re)))
(goto-char b)
;; Get rightmost position
(while (< (point) e)
- (if (re-search-forward reg (min e (pascal-get-end-of-line 2)) 'move)
- (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))
- (end-of-line)
- )))))
+ (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)
+ ))))
;; In case no lineup was found
(if (> ind 0)
(1+ ind)
(end-of-line)
(skip-chars-backward " \t")
(1+ (current-column))))))
-
+
\f
;;;
(t "\\<\\(function\\|procedure\\)\\s +"))
"\\<\\(" pascal-str "[a-zA-Z0-9_.]*\\)\\>"))
match)
-
+
(if (not (looking-at "\\<\\(function\\|procedure\\)\\>"))
(re-search-backward "\\<\\(function\\|procedure\\)\\>" nil t))
(forward-char 1)
(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\\)\\>\\)\\|"
+ (while (and (re-search-backward
+ (concat "\\((\\|\\<\\(var\\|type\\|const\\)\\>\\)\\|"
pascal-symbol-re)
(pascal-get-beg-of-line) t)
(not (match-end 1)))
(defun pascal-keyword-completion (keyword-list)
"Give list of all possible completions of keywords in KEYWORD-LIST."
- (mapcar '(lambda (s)
+ (mapcar '(lambda (s)
(if (string-match (concat "\\<" pascal-str) s)
(if (or (null pascal-pred)
(funcall pascal-pred s))
(save-excursion (pascal-var-completion))
(pascal-func-completion 'function)
(pascal-keyword-completion pascal-separator-keywords))))
-
+
;; Now we have built a list of all matches. Give response to caller
(pascal-completion-response))))
(progn
;; Update entry number in list
(setq pascal-last-completions allcomp
- pascal-last-word-numb
+ pascal-last-word-numb
(if (>= pascal-last-word-numb (1- (length allcomp)))
0
(1+ pascal-last-word-numb)))
(if (not (null (cdr allcomp)))
(message "(Complete but not unique)")
(message "(Sole completion)")))
- ;; Display buffer if the current completion didn't help
+ ;; Display buffer if the current completion didn't help
;; on completing the label.
((and (not (null (cdr allcomp))) (= (length pascal-str)
(length match)))
(setq pascal-str (pascal-build-defun-re "[a-zA-Z_]"))
(setq pascal-str (pascal-build-defun-re pascal-str)))
(goto-char (point-min))
-
+
;; Build a list of all possible completions
(while (re-search-forward pascal-str nil t)
(setq match (buffer-substring (match-beginning 2) (match-end 2)))
(if pascal-outline-map
nil
- (if (boundp 'set-keymap-name)
+ (if (fboundp '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))
+ (if (fboundp 'set-keymap-parent)
+ (set-keymap-parent (setq pascal-outline-map (make-sparse-keymap))
+ pascal-mode-map)
+ (setq pascal-outline-map (copy-keymap pascal-mode-map)))
(define-key pascal-outline-map "\M-\C-a" 'pascal-outline-prev-defun)
(define-key pascal-outline-map "\M-\C-e" 'pascal-outline-next-defun)
(define-key pascal-outline-map "\C-c\C-d" 'pascal-outline-goto-defun)
(interactive "P")
(setq pascal-outline-mode
(if (null arg) (not pascal-outline-mode) t))
- (if (boundp 'redraw-mode-line)
+ (if (fboundp 'redraw-mode-line)
(redraw-mode-line))
(if pascal-outline-mode
(progn
(defun pascal-outline-change (b e pascal-flag)
(let ((modp (buffer-modified-p)))
(unwind-protect
- (subst-char-in-region b e (if (= pascal-flag ?\n)
+ (subst-char-in-region b e (if (= pascal-flag ?\n)
?\^M ?\n) pascal-flag)
(set-buffer-modified-p modp))))
(provide 'pascal)
+;;; arch-tag: 04535136-fd93-40b4-a505-c9bebdc051f5
;;; pascal.el ends here