🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.vhdl
Article #483

vhdl-mode.el version 2.22 beta (Was: need emacs editor vhdl mode)

#483
From: Rod Whitby
Date: Sun, 16 Oct 1994 22:28
2192 lines
79192 bytes
>>>>> "Eric" == Eric Holp / 3-7699 (HOLP@RALVM29) D86A/061 <holp@isaac.raleigh.ibm.com> writes:

    Eric> Is anyone aware of an emacs editor package (.el file) that would 
    Eric> setup emacs for editing vhdl files (ie would be referred to as a
    Eric> vhdl major mode)?  If so then please reply w/ ftp site or send 
    Eric> me the code.

    Eric> Thank you.

    Eric> -- 
    Eric> Eric Holp                 Internet reply address: eholp@vnet.ibm.com

Here is the VHDL equivalent of cc-mode (except it's slower because
VHDL is harder to parse).  Other users of this code should drop me a
line so that I can add you to the update list.

Watch out for ^Ls - look for ";;; vhdl-mode.el ends here":

---------------------------------8<--------------------------------------------
;;; vhdl-mode.el --- major mode for editing VHDL code

;; Authors: 1994 Rodney J. Whitby <rwhitby@research.canon.oz.au>
;; Maintainer: rwhitby@research.canon.oz.au
;; Created: June 1994, adapted from cc-mode.el 4.29 by Barry A. Warsaw.
;; Version: $Revision: 2.22 $
;; Last Modified: $Date: 1994/10/14 05:26:46 $
;; Keywords: VHDL editing major-mode

;; Copyright (C) 1992, 1993, 1994 Barry A. Warsaw
;; Copyright (C) 1992, 1993, 1994 Free Software Foundation, Inc.
;; Copyright (C) 1994 Rodney J. Whitby

;; This file is not yet part of GNU Emacs.

;; GNU Emacs 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, or (at your option)
;; any later version.

;; GNU Emacs 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.

;; 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, 675 Mass Ave, Cambridge, MA 02139, USA.

;;; Commentary:

;; This package provides indentation support for VHDL code.

;; Details on VHDL-MODE are now (or will soon be) contained in an
;; accompanying texinfo manual (vhdl-mode.texi).  To submit bug reports,
;; hit "C-c C-b", and please try to include a code sample so I can
;; reproduce your problem.  If you have other questions contact me at
;; the following address: rwhitby@research.canon.oz.au.

;; YOU CAN IGNORE ALL BYTE-COMPILER WARNINGS. They are the result of
;; the multi-Emacsen support. FSF Emacs 19 and XEmacs 19 (formerly
;; Lucid) do things differently and there's no way to shut the
;; byte-compiler up at the necessary granularity.  Let me say this
;; again: YOU CAN IGNORE ALL BYTE-COMPILER WARNINGS (you'd be
;; surprised at how many people don't follow this advice :-).

;; To use VHDL-MODE, add the following to your .emacs file.  This
;; assumes you will use .hdl extensions for your VHDL source:
;;
;; (autoload 'vhdl-mode   "vhdl-mode" "VHDL Editing Mode" t)
;; (setq auto-mode-alist
;;   (append '(("\\.hdl$"  . vhdl-mode)   ; to edit VHDL code
;;            ) auto-mode-alist))
;;
;; If you would like to join the beta testers list, send add/drop
;; requests to rwhitby@research.canon.oz.au.
;;
;; Many, many thanks go out to all the folks on the beta test list.
;; Without their patience, testing, insight, and code contributions,
;; and encouragement vhdl-mode.el would be a far inferior package.

;; LCD Archive Entry:
;; vhdl-mode.el|Rodney J. Whitby|rwhitby@research.canon.oz.au
;; |Major mode for editing VHDL code
;; |$Date: 1994/10/14 05:26:46 $|$Revision: 2.22 $|

;;; Code:


;; user definable variables
;; vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv

(defvar vhdl-inhibit-startup-warnings-p nil
  "*If non-nil, inhibits start up compatibility warnings.")
(defvar vhdl-strict-syntax-p nil
  "*If non-nil, all syntactic symbols must be found in `vhdl-offsets-alist'.
If the syntactic symbol for a particular line does not match a symbol
in the offsets alist, an error is generated, otherwise no error is
reported and the syntactic symbol is ignored.")
(defvar vhdl-echo-syntactic-information-p nil
  "*If non-nil, syntactic info is echoed when the line is indented.")
(defvar vhdl-basic-offset 2
  "*Amount of basic offset used by + and - symbols in `vhdl-offsets-alist'.")

(defconst vhdl-offsets-alist-default
  '((string                . -1000)
    (block-open            . 0)
    (block-close           . 0)
    (block-intro           . +)
    (statement             . 0)
    (statement-cont        . +)
    ;; some people might prefer
    ;;(statement-cont        . vhdl-lineup-math)
    (case-alternative      . +)
    (label                 . 2)
    (comment               . vhdl-lineup-comment)
    (arglist-intro         . vhdl-lineup-arglist-intro-after-paren)
    (arglist-cont          . 0)
    (arglist-cont-nonempty . vhdl-lineup-arglist)
    (arglist-close         . vhdl-lineup-arglist)
    )
  "Default settings for offsets of syntactic elements.
Do not change this constant!  See the variable `vhdl-offsets-alist' for
more information.")

(defvar vhdl-offsets-alist (copy-alist vhdl-offsets-alist-default)
  "*Association list of syntactic element symbols and indentation offsets.
As described below, each cons cell in this list has the form:

    (SYNTACTIC-SYMBOL . OFFSET)

When a line is indented, vhdl-mode first determines the syntactic
context of the line by generating a list of symbols called syntactic
elements.  This list can contain more than one syntactic element and
the global variable `vhdl-syntactic-context' contains the context list
for the line being indented.  Each element in this list is actually a
cons cell of the syntactic symbol and a buffer position.  This buffer
position is call the relative indent point for the line.  Some
syntactic symbols may not have a relative indent point associated with
them.

After the syntactic context list for a line is generated, vhdl-mode
calculates the absolute indentation for the line by looking at each
syntactic element in the list.  First, it compares the syntactic
element against the SYNTACTIC-SYMBOL's in `vhdl-offsets-alist'.  When it
finds a match, it adds the OFFSET to the column of the relative indent
point.  The sum of this calculation for each element in the syntactic
list is the absolute offset for line being indented.

If the syntactic element does not match any in the `vhdl-offsets-alist',
an error is generated if `vhdl-strict-syntax-p' is non-nil, otherwise
the element is ignored.

Actually, OFFSET can be an integer, a function, a variable, or one of
the following symbols: `+', `-', `++', or `--'.  These latter
designate positive or negative multiples of `vhdl-basic-offset',
respectively: *1, *-1, *2, and *-2. If OFFSET is a function, it is
called with a single argument containing the cons of the syntactic
element symbol and the relative indent point.  The function should
return an integer offset.

Here is the current list of valid syntactic element symbols:

 string                 -- inside multi-line string
 block-open             -- statement block open brace
 block-close            -- statement block close brace
 block-intro            -- the first line in a new statement block
 statement              -- a VHDL statement
 statement-cont         -- a continuation of a VHDL statement
 case-alternative       -- a case statement alternative key
 label                  -- any non-special VHDL label
 comment                -- a line containing only a comment
 arglist-intro          -- the first line in an argument list
 arglist-cont           -- subsequent argument list lines when no
                           arguments follow on the same line as the
                           the arglist opening paren
 arglist-cont-nonempty  -- subsequent argument list lines when at
                           least one argument follows on the same
                           line as the arglist opening paren
 arglist-close          -- the solo close paren of an argument list
")

(defvar vhdl-tab-always-indent t
  "*Controls the operation of the TAB key.
If t, hitting TAB always just indents the current line.  If nil,
hitting TAB indents the current line if point is at the left margin or
in the line's indentation, otherwise it insert a real tab character.
If other than nil or t, then tab is inserted only within literals
-- defined as comments and strings -- and inside preprocessor
directives, but line is always reindented.

Note that indentation of lines containing only comments is also
controlled by the `vhdl-comment-only-line-offset' variable.")

(defvar vhdl-comment-only-line-offset 0
  "*Extra offset for line which contains only the start of a comment.
Can contain an integer or a cons cell of the form:

 (NON-ANCHORED-OFFSET . ANCHORED-OFFSET)

Where NON-ANCHORED-OFFSET is the amount of offset given to
non-column-zero anchored comment-only lines, and ANCHORED-OFFSET is
the amount of offset to give column-zero anchored comment-only lines.
Just an integer as value is equivalent to (<val> . 0)")

(defvar vhdl-special-indent-hook nil
  "*Hook for user defined special indentation adjustments.
This hook gets called after a line is indented by the mode.")

(defvar vhdl-style-alist
  '(("IEEE"
     (vhdl-basic-offset . 4)
     (vhdl-comment-only-line-offset . 0)
     (vhdl-offsets-alist . ((statement-block-intro . +)
			    (case-alternative . +)
			    (label . -)
			    (statement-cont . +)
			    (arglist-cont-nonempty . +)
			    ))
     )
    )
  "Styles of Indentation.
Elements of this alist are of the form:

  (STYLE-STRING (VARIABLE . VALUE) [(VARIABLE . VALUE) ...])

where STYLE-STRING is a short descriptive string used to select a
style, VARIABLE is any vhdl-mode variable, and VALUE is the intended
value for that variable when using the selected style.

There is one special case when VARIABLE is `vhdl-offsets-alist'.  In this
case, the VALUE is a list containing elements of the form:

  (SYNTACTIC-SYMBOL . VALUE)

as described in `vhdl-offsets-alist'.  These are passed directly to
`vhdl-set-offset' so there is no need to set every syntactic symbol in
your style, only those that are different from the default.")

;; dynamically append the default value of most variables
(or (assoc "Default" vhdl-style-alist)
    (let* ((varlist '(vhdl-inhibit-startup-warnings-p
		      vhdl-strict-syntax-p
		      vhdl-echo-syntactic-information-p
		      vhdl-basic-offset
		      vhdl-offsets-alist
		      vhdl-tab-always-indent
		      vhdl-comment-only-line-offset))
	   (default (cons "Default"
			  (mapcar
			   (function
			    (lambda (var)
			      (cons var (symbol-value var))
			      ))
			   varlist))))
      (setq vhdl-style-alist (cons default vhdl-style-alist))))

(defvar vhdl-mode-hook nil
  "*Hook called by `vhdl-mode'.")

(defvar vhdl-mode-menu
  '(["Comment Out Region"     comment-region (mark)]
    ["Indent Expression"      vhdl-indent-exp
     (memq (following-char) '(?\( ?\[ ?\{))]
    ["Indent Line"            vhdl-indent-command t]
    ["Backward Statement"     vhdl-beginning-of-statement t]
    ["Forward Statement"      vhdl-end-of-statement t]
    )
  "XEmacs 19 (formerly Lucid) menu for VHDL mode.")


;; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
;; NO USER DEFINABLE VARIABLES BEYOND THIS POINT

(defconst vhdl-emacs-features
  (let ((major (and (boundp 'emacs-major-version)
		    emacs-major-version))
	(minor (and (boundp 'emacs-minor-version)
		    emacs-minor-version))
	flavor)
    ;; figure out version numbers if not already discovered
    (and (or (not major) (not minor))
	 (string-match "\\([0-9]+\\).\\([0-9]+\\)" emacs-version)
	 (setq major (string-to-int (substring emacs-version
					       (match-beginning 1)
					       (match-end 1)))
	       minor (string-to-int (substring emacs-version
					       (match-beginning 2)
					       (match-end 2)))))
    (if (not (and major minor))
	(error "Cannot figure out the major and minor version numbers."))
    ;; calculate the major version
    (cond
     ((= major 18) (setq major 'v18))	;Emacs 18
     ((= major 4)  (setq major 'v18))	;Epoch 4
     ((= major 19) (setq major 'v19	;Emacs 19
			 flavor (if (or (string-match "Lucid" emacs-version)
					(string-match "XEmacs" emacs-version))
				    'XEmacs 'FSF)))
     ;; I don't know
     (t (error "Cannot recognize major version number: %s" major)))
    ;; lets do some minimal sanity checking.
    (if (and (or
	      ;; Emacs 18 is brain dead
	      (eq major 'v18)
	      ;; Lemacs before 19.6 had bugs
	      (and (eq major 'v19) (eq flavor 'XEmacs) (< minor 6))
	      ;; FSF 19 before 19.21 had bugs
	      (and (eq major 'v19) (eq flavor 'FSF) (< minor 21)))
	     (not vhdl-inhibit-startup-warnings-p))
	(with-output-to-temp-buffer "*vhdl-mode warnings*"
	  (print (format
"The version of Emacs that you are running, %s,
has known bugs in its syntax.c parsing routines which will affect the
performance of vhdl-mode. You should strongly consider upgrading to the
latest available version.  vhdl-mode may continue to work, after a
fashion, but strange indentation errors could be encountered."
		     emacs-version))))
    (list major flavor))
  "A list of features extant in the Emacs you are using.
There are many flavors of Emacs out there, each with different
features supporting those needed by vhdl-mode.  Here's the current
supported list, along with the values for this variable:

 Emacs 18/Epoch 4:           (v18)
 XEmacs (formerly Lucid) 19: (v19 XEmacs)
 FSF Emacs 19:               (v19 FSF).")

(defvar vhdl-mode-abbrev-table nil
  "Abbrev table in use in vhdl-mode buffers.")
(define-abbrev-table 'vhdl-mode-abbrev-table ())

(defvar vhdl-mode-map ()
  "Keymap used in vhdl-mode buffers.")
(if vhdl-mode-map
    ()
  ;; TBD: should we even worry about naming this keymap. My vote: no,
  ;; because FSF and XEmacs (formerly Lucid) do it differently.
  (setq vhdl-mode-map (make-sparse-keymap))
  ;; put standard keybindings into MAP
  (define-key vhdl-mode-map "\M-\C-f"   'vhdl-forward-sexp)
  (define-key vhdl-mode-map "\M-\C-b"   'vhdl-backward-sexp)
  (define-key vhdl-mode-map "\t"        'vhdl-indent-command)
  (define-key vhdl-mode-map "\177"      'backward-delete-char-untabify)
  ;; these are new keybindings, with no counterpart to BOCM
  (define-key vhdl-mode-map "\C-c\C-b"  'vhdl-submit-bug-report)
  (define-key vhdl-mode-map "\C-c\C-c"  'comment-region)
  (define-key vhdl-mode-map "\C-c\C-o"  'vhdl-set-offset)
  (define-key vhdl-mode-map "\C-c\C-s"  'vhdl-show-syntactic-information)
  (define-key vhdl-mode-map "\C-c\C-v"  'vhdl-version)
  ;; in XEmacs (formerly Lucid) 19, we want the menu to popup when
  ;; the 3rd button is hit.  In 19.10 and beyond this is done
  ;; automatically if we put the menu on mode-popup-menu variable,
  ;; see c-common-init. RMS decided that this feature should not be
  ;; included for FSF's Emacs.
  (if (and (boundp 'current-menubar)
	   (not (boundp 'mode-popup-menu)))
      (define-key vhdl-mode-map 'button3 'vhdl-popup-menu))
  )

(defun vhdl-populate-syntax-table (table)
  ;; Populate the syntax TABLE
  ;; DO NOT TRY TO SET _ (UNDERSCORE) TO WORD CLASS!
  (modify-syntax-entry ?\\ "\\"    table)
  (modify-syntax-entry ?+  "."     table)
  (modify-syntax-entry ?/  "."     table)
  (modify-syntax-entry ?=  "."     table)
  (modify-syntax-entry ?%  "."     table)
  (modify-syntax-entry ?<  "."     table)
  (modify-syntax-entry ?>  "."     table)
  (modify-syntax-entry ?&  "."     table)
  (modify-syntax-entry ?|  "."     table)
  (modify-syntax-entry ?\' "."     table)
  (modify-syntax-entry ?*  "."     table))

(defvar vhdl-mode-syntax-table nil
  "Syntax table used in vhdl-mode buffers.")
(if vhdl-mode-syntax-table
    ()
  (setq vhdl-mode-syntax-table (make-syntax-table))
  (vhdl-populate-syntax-table vhdl-mode-syntax-table)
  ;; add extra comment syntax
  (modify-syntax-entry ?-  ". 12"  vhdl-mode-syntax-table)
  (modify-syntax-entry ?\n ">"     vhdl-mode-syntax-table)
  (modify-syntax-entry ?\^m ">"    vhdl-mode-syntax-table))

(defvar vhdl-syntactic-context nil
  "Variable containing syntactic analysis list during indentation.")
(defvar vhdl-comment-start-regexp nil
  "Buffer local variable describing how comment are introduced.")

(make-variable-buffer-local 'vhdl-comment-start-regexp)


;; main entry points for the modes
;;;###autoload
(defun vhdl-mode ()
  "Major mode for editing VHDL code.
vhdl-mode $Revision: 2.22 $
To submit a problem report, enter `\\[vhdl-submit-bug-report]' from a
vhdl-mode buffer.  This automatically sets up a mail buffer with version
information already added.  You just need to add a description of the
problem, including a reproducable test case and send the message.

Note that the details of configuring vhdl-mode will soon be moved to the
accompanying texinfo manual.  Until then, please read the README file
that came with the vhdl-mode distribution.

The hook variable `vhdl-mode-hook' is run with no args, if that value is
bound and has a non-nil value.

Key bindings:
\\{vhdl-mode-map}"
  (interactive)
  (kill-all-local-variables)
  (set-syntax-table vhdl-mode-syntax-table)
  (setq major-mode 'vhdl-mode
	mode-name "VHDL"
	local-abbrev-table vhdl-mode-abbrev-table)
  (use-local-map vhdl-mode-map)
  (vhdl-common-init)
  (setq comment-start "-- "
	comment-end   ""
	vhdl-comment-start-regexp "--")
  (run-hooks 'vhdl-mode-hook))

(defun vhdl-common-init ()
  ;; Common initializations for vhdl-mode.
  ;; make local variables
  (make-local-variable 'paragraph-start)
  (make-local-variable 'paragraph-separate)
  (make-local-variable 'paragraph-ignore-fill-prefix)
  (make-local-variable 'require-final-newline)
  (make-local-variable 'parse-sexp-ignore-comments)
  (make-local-variable 'indent-line-function)
  (make-local-variable 'indent-region-function)
  (make-local-variable 'comment-start)
  (make-local-variable 'comment-end)
  (make-local-variable 'comment-column)
  (make-local-variable 'comment-start-skip)
  ;; now set their values
  (setq paragraph-start (concat "^$\\|" page-delimiter)
	paragraph-separate paragraph-start
	paragraph-ignore-fill-prefix t
	require-final-newline t
	parse-sexp-ignore-comments t
	indent-line-function 'vhdl-indent-line
	comment-column 32
	comment-start-skip "-- *")
  ;; setup the comment indent variable in a Emacs version portable way
  ;; ignore any byte compiler warnings you might get here
  (if (boundp 'comment-indent-function)
      (progn
	   (make-local-variable 'comment-indent-function)
	   (setq comment-indent-function 'vhdl-comment-indent))
    (make-local-variable 'comment-indent-hook)
    (setq comment-indent-hook 'vhdl-comment-indent))
  ;; put VHDL menu into menubar and on popup menu for XEmacs (formerly
  ;; Lucid) 19. I think this happens automatically for FSF Emacs 19.
  (if (and (boundp 'current-menubar)
	   current-menubar
	   (not (assoc mode-name current-menubar)))
      (progn
	(set-buffer-menubar (copy-sequence current-menubar))
	(add-menu nil mode-name vhdl-mode-menu)))
  (if (boundp 'mode-popup-menu)
      (setq mode-popup-menu
	    (cons (concat mode-name " Mode Commands") vhdl-mode-menu)))
  )


;; macros must be defined before first use
(defmacro vhdl-point (position)
  ;; Returns the value of point at certain commonly referenced POSITIONs.
  ;; POSITION can be one of the following symbols:
  ;; 
  ;; bol  -- beginning of line
  ;; eol  -- end of line
  ;; bod  -- beginning of defun
  ;; boi  -- back to indentation
  ;; eoi  -- last whitespace on line
  ;; ionl -- indentation of next line
  ;; iopl -- indentation of previous line
  ;; bonl -- beginning of next line
  ;; bopl -- beginning of previous line
  ;; 
  ;; This function does not modify point or mark.
  (or (and (eq 'quote (car-safe position))
	   (null (cdr (cdr position))))
      (error "bad buffer position requested: %s" position))
  (setq position (nth 1 position))
  (` (let ((here (point)))
       (,@ (cond
	    ((eq position 'bol)  '((beginning-of-line)))
	    ((eq position 'eol)  '((end-of-line)))
	    ((eq position 'bod)  '((save-match-data
				     (vhdl-beginning-of-defun))))
	    ((eq position 'boi)  '((back-to-indentation)))
	    ((eq position 'eoi)  '((end-of-line)(skip-chars-backward " \t")))
	    ((eq position 'bonl) '((forward-line 1)))
	    ((eq position 'bopl) '((forward-line -1)))
	    ((eq position 'iopl)
	     '((forward-line -1)
	       (back-to-indentation)))
	    ((eq position 'ionl)
	     '((forward-line 1)
	       (back-to-indentation)))
	    (t (error "unknown buffer position requested: %s" position))
	    ))
       (prog1
	   (point)
	 (goto-char here))
       ;; workaround for an Emacs18 bug -- blech! Well, at least it
       ;; doesn't hurt for v19
       (,@ nil)
       )))

(defmacro vhdl-safe (&rest body)
  ;; safely execute BODY, return nil if an error occurred
  (` (condition-case nil
	 (progn (,@ body))
       (error nil))))


;; active regions
(defun vhdl-keep-region-active ()
  ;; do whatever is necessary to keep the region active in XEmacs
  ;; (formerly Lucid). ignore byte-compiler warnings you might see
  (and (boundp 'zmacs-region-stays)
       (setq zmacs-region-stays t)))

(defun vhdl-read-offset (langelem)
  ;; read new offset value for LANGELEM from minibuffer. return a
  ;; legal value only
  (let ((oldoff (format "%s" (cdr-safe (assq langelem vhdl-offsets-alist))))
	(errmsg "Offset must be int, func, var, or one of +, -, ++, --: ")
	(prompt "Offset: ")
	offset input interned)
    (while (not offset)
      (setq input (read-string prompt oldoff)
	    offset (cond ((string-equal "+" input) '+)
			 ((string-equal "-" input) '-)
			 ((string-equal "++" input) '++)
			 ((string-equal "--" input) '--)
			 ((string-match "^-?[0-9]+$" input)
			  (string-to-int input))
			 ((fboundp (setq interned (intern input)))
			  interned)
			 ((boundp interned) interned)
			 ;; error, but don't signal one, keep trying
			 ;; to read an input value
			 (t (ding)
			    (setq prompt errmsg)
			    nil))))
    offset))

(defun vhdl-set-offset (symbol offset &optional add-p)
  "Change the value of a syntactic element symbol in `vhdl-offsets-alist'.
SYMBOL is the syntactic element symbol to change and OFFSET is the new
offset for that syntactic element.  Optional ADD says to add SYMBOL to
`vhdl-offsets-alist' if it doesn't already appear there."
  (interactive
   (let* ((langelem
	   (intern (completing-read
		    (concat "Syntactic symbol to change"
			    (if current-prefix-arg " or add" "")
			    ": ")
		    (mapcar
		     (function
		      (lambda (langelem)
			(cons (format "%s" (car langelem)) nil)))
		     vhdl-offsets-alist)
		    nil (not current-prefix-arg)
		    ;; initial contents tries to be the last element
		    ;; on the syntactic analysis list for the current
		    ;; line
		    (let* ((syntax (vhdl-guess-basic-syntax))
			   (len (length syntax))
			   (ic (format "%s" (car (nth (1- len) syntax)))))
		      (if (memq 'v19 vhdl-emacs-features)
			  (cons ic 0)
			ic))
		    )))
	  (offset (vhdl-read-offset langelem)))
     (list langelem offset current-prefix-arg)))
  ;; sanity check offset
  (or (eq offset '+)
      (eq offset '-)
      (eq offset '++)
      (eq offset '--)
      (integerp offset)
      (fboundp offset)
      (boundp offset)
      (error "Offset must be int, func, var, or one of +, -, ++, --: %s"
	     offset))
  (let ((entry (assq symbol vhdl-offsets-alist)))
    (if entry
	(setcdr entry offset)
      (if add-p
	  (setq vhdl-offsets-alist (cons (cons symbol offset) vhdl-offsets-alist))
	(error "%s is not a valid syntactic symbol." symbol))))
  (vhdl-keep-region-active))

(defun vhdl-set-style (style &optional local)
  "Set vhdl-mode variables to use one of several different indentation styles.
STYLE is a string representing the desired style and optional LOCAL is
a flag which, if non-nil, means to make the style variables being
changed buffer local, instead of the default, which is to set the
global variables.  Interactively, the flag comes from the prefix
argument.  The styles are chosen from the `vhdl-style-alist' variable."
  (interactive (list (completing-read "Use which VHDL indentation style? "
                                      vhdl-style-alist nil t)
		     current-prefix-arg))
  (let ((vars (cdr (assoc style vhdl-style-alist))))
    (or vars
	(error "Invalid VHDL indentation style `%s'" style))
    ;; set all the variables
    (mapcar
     (function
      (lambda (varentry)
	(let ((var (car varentry))
	      (val (cdr varentry)))
	  (and local
	       (make-local-variable var))
	  ;; special case for vhdl-offsets-alist
	  (if (not (eq var 'vhdl-offsets-alist))
	      (set var val)
	    ;; reset vhdl-offsets-alist to the default value first
	    (setq vhdl-offsets-alist (copy-alist vhdl-offsets-alist-default))
	    ;; now set the langelems that are different
	    (mapcar
	     (function
	      (lambda (langentry)
		(let ((langelem (car langentry))
		      (offset (cdr langentry)))
		  (vhdl-set-offset langelem offset)
		  )))
	     val))
	  )))
     vars))
  (vhdl-keep-region-active))


;; Return `comment' if in a comment, `string' if in a string
;; literal, or nil if not in a comment at all.  Optional LIM is used
;; as the backward limit of the search.  If omitted, or nil,
;; (point-min) is used."

;; This is for all v19 Emacsen supporting either 1-bit or 8-bit syntax
(defun vhdl-in-literal (&optional lim)
  ;; Determine if point is in a VHDL literal. we cache the last point
  ;; calculated if the cache is enabled
  (if (and (boundp 'vhdl-in-literal-cache)
	   vhdl-in-literal-cache
	   (= (point) (aref vhdl-in-literal-cache 0)))
      (aref vhdl-in-literal-cache 1)
    (let ((rtn (save-excursion
		 (let* ((lim (or lim (point-min)))
			(state (parse-partial-sexp lim (point))))
		   (cond
		    ((nth 3 state) 'string)
		    ((nth 4 state) 'comment)
		    (t nil))))))
      ;; cache this result if the cache is enabled
      (and (boundp 'vhdl-in-literal-cache)
	   (setq vhdl-in-literal-cache (vector (point) rtn)))
      rtn)))

;; Skipping of "syntactic whitespace" for Emacs 19.  Syntactic
;; whitespace is defined as lexical whitespace, VHDL and VHDL style
;; comments, and preprocessor directives.  Search no farther back or
;; forward than optional LIM.  If LIM is omitted, (point-min)
;; is used for backward skipping, (point-max) is used for forward
;; skipping.

(defun vhdl-forward-syntactic-ws (&optional lim)
  ;; Forward skip of syntactic whitespace for Emacs 19.
  (save-restriction
    (let* ((lim (or lim (point-max)))
	   (here lim)
	   (hugenum (point-max)))
      (narrow-to-region lim (point))
      (while (/= here (point))
	(setq here (point))
	(forward-comment hugenum))
      )))

(defun vhdl-backward-syntactic-ws (&optional lim)
  ;; Backward skip over syntactic whitespace for Emacs 19.
  (save-restriction
    (let* ((lim (or lim (point-min)))
	   (here lim)
	   (hugenum (- (point-max))))
      (if (< lim (point))
	  (progn
	    (narrow-to-region lim (point))
	    (while (/= here (point))
	      (setq here (point))
	      (forward-comment hugenum)
	      )))
      )))

;; This is used by indent-for-comment to decide how much to indent a
;; comment in VHDL code based on its context.
(defun vhdl-comment-indent ()
  (if (looking-at (concat "^\\(" vhdl-comment-start-regexp "\\)"))
      0				;Existing comment at bol stays there.
    (let ((opoint (point))
	  placeholder)
      (save-excursion
	(beginning-of-line)
	(cond
	 ;; CASE 3: use comment-column if previous line is a
	 ;; comment-only line indented to the left of comment-column
	 ((save-excursion
	    (beginning-of-line)
	    (and (not (bobp))
		 (forward-line -1))
	    (skip-chars-forward " \t")
	    (prog1
		(looking-at vhdl-comment-start-regexp)
	      (setq placeholder (point))))
	  (goto-char placeholder)
	  (if (< (current-column) comment-column)
	      comment-column
	    (current-column)))
	 ;; CASE 4: If comment-column is 0, and nothing but space
	 ;; before the comment, align it at 0 rather than 1.
	 ((progn
	    (goto-char opoint)
	    (skip-chars-backward " \t")
	    (and (= comment-column 0) (bolp)))
	  0)
	 ;; CASE 5: indent at comment column except leave at least one
	 ;; space.
	 (t (max (1+ (current-column))
		 comment-column))
	 )))))


;; constant regular expressions for looking at various constructs
(defconst vhdl-symbol-key "\\(\\w\\|\\s_\\)+"
  "Regexp describing a VHDL symbol.
We cannot use just `word' syntax class since `_' cannot be in word
class.  Putting underscore in word class breaks forward word movement
behavior that users are familiar with.")
(defconst vhdl-label-key
  (concat vhdl-symbol-key "\\s-*:\\([^=]\\|$\\)")
  "Regexp describing any label.")
(defconst vhdl-case-alternative-key
  "\\(when[( \t]+\\S .*\\)=>"
  "Regexp describing a case statement alternative key.")

(defun vhdl-first-word (point)
  "If the keyword at POINT is at boi, then return (current-column) at
that point, else nil."
  (save-excursion
    (and (goto-char point)
	 (eq (point) (vhdl-point 'boi))
	 (current-column))))

(defun vhdl-last-word (point)
  "If the keyword at POINT is at eoi, then return (current-column) at
that point, else nil."
  (save-excursion
    (and (goto-char point)
	 (eq (save-excursion (forward-sexp) (point))
	     (vhdl-point 'eoi))
	 (current-column))))

(defconst vhdl-begin-re
  "\\b\\(is\\|begin\\|block\\|component\\|generate\\|then\\|else\\|loop\\|process\\|units\\|record\\)\\b\\([^_]\\|\\'\\)"
  "A regular expression that matches all known \"begin\" keywords.")

(defun vhdl-begin-p (&optional lim)
  "Return t if we are looking at a \"begin\" keyword.
Assumes that the caller will make sure that we are not inside a literal, and
that we are not in the middle of an identifier that just happens to contain
a \"begin\" keyword."
  (and (looking-at vhdl-begin-re)
       (cond
	;; "[architecture|case|configuration|entity|package|
	;;   procedure|function] ... is":
	((and (looking-at "is")
	      (save-excursion
		(vhdl-beginning-of-statement 1 lim)
		;; v-b-o-s could have left us at point-min
		(and (bobp)
		     (vhdl-forward-syntactic-ws))
		(looking-at
		 "\\(architecture\\|case\\|configuration\\|entity\\|package\\|procedure\\|function\\)\\b[^_]")))
	 t)
	;; "begin", "then":
	((looking-at "begin\\|then\\|else")
	 t)
	;; "block", "component", "generate", "loop", "process",
	;; "units", "record":
	((and (looking-at
	       "block\\|component\\|generate\\|loop\\|process\\|units\\|record")
	      (save-excursion
		(backward-sexp)
		(not (looking-at "end\\s-+\\w"))))
	 t)
	;; "for" (inside configuration declaration):
	;; %%% currently not handled
	)))
  
(defconst vhdl-end-re "\\b\\(end\\|else\\|elsif\\)\\b\\([^_]\\|\\'\\)")

(defun vhdl-end-p (&optional lim)
  (looking-at vhdl-end-re))

(defun vhdl-corresponding-begin (&optional lim)
  "If the word at the current position corresponds to an \"end\"
keyword, then return a vector containing enough information to find
the corresponding \"begin\" keyword, else return nil.  The keyword to
search backward for is aref 0.  The column in which the keyword must
appear is aref 1 or nil if any column is suitable.  The supplementary
keyword to search forward for is aref 2 or nil if this is not
required.
Assumes that the caller will make sure that we are not in the middle
of an identifier that just happens to contain an \"end\" keyword."
  (save-excursion
    (let (pos)
      (if (and (vhdl-end-p lim)
	       (not (vhdl-in-literal lim)))
	  (if (looking-at "el")
	      ;; "else", "elsif":
	      (vector "if\\|elsif" (vhdl-first-word (point)) "then")
	    ;; "end ...":
	    (setq pos (point))
	    (forward-sexp)
	    (skip-chars-forward " \t\n")
	    (cond
	     ;; "end if":
	     ((looking-at "if\\b[^_]")
	      (vector "else\\|elsif\\|if"
		      (vhdl-first-word pos)
		      "else\\|then"))
	     ;; "end component", "end units", "end record":
	     ((looking-at "\\(component\\|units\\|record\\)\\b[^_]")
	      (vector (buffer-substring (match-beginning 1)
					(match-end 1))
		      (vhdl-first-word pos)
		      nil))
	     ;; "end block", "end process":
	     ((looking-at "\\(block\\|process\\)\\b[^_]")
	      (vector "begin" (vhdl-first-word pos) nil))
	     ;; "end case":
	     ((looking-at "case\\b[^_]")
	      (vector "case" (vhdl-first-word pos) "is"))
	     ;; "end generate":
	     ((looking-at "generate\\b[^_]")
	      (vector "generate\\|for\\|if"
		      (vhdl-first-word pos)
		      "generate"))
	     ;; "end loop":
	     ((looking-at "loop\\b[^_]")
	      (vector "loop\\|while\\|for"
		      (vhdl-first-word pos)
		      "loop"))
	     ;; "end for" (inside configuration declaration):
	     ;; %%% currently not handled
	     ;; "end [id]":
	     (t
	      (vector "begin\\|architecture\\|configuration\\|entity\\|package\\|procedure\\|function"
		      (vhdl-first-word pos)
		      ;; return an alist of (statement . keyword) mappings
		      '(
			;; "begin ... end [id]":
			("begin"          . nil)
			;; "architecture ... is ... begin ... end [id]":
			("architecture"   . "is")
			;; "configuration ... is ... end [id]":
			("configuration"  . "is")
			;; "entity ... is ... end [id]":
			("entity"         . "is")
			;; "package ... is ... end [id]":
			("package"        . "is")
			;; "procedure ... is ... begin ... end [id]":
			("procedure"      . "is")
			;; "function ... is ... begin ... end [id]":
			("function"       . "is")
			)))
	     ))) ; "end ..."
      )))

(defun vhdl-corresponding-end (&optional lim)
  "If the word at the current position corresponds to a \"begin\"
keyword, then return a vector containing enough information to find
the corresponding \"end\" keyword, else return nil.  The keyword to
search forward for is aref 0.  The column in which the keyword must
appear is aref 1 or nil if any column is suitable.
Assumes that the caller will make sure that we are not in the middle
of an identifier that just happens to contain a \"begin\" keyword."
  (save-excursion
    (and (vhdl-begin-p lim)
	 (not (vhdl-in-literal lim))
	 (cond
	  ;; "is", "generate", "loop":
	  ((looking-at "is\\|generate\\|loop")
	   (vector "end"
		   (and (vhdl-last-word (point))
			(or (vhdl-first-word (point))
			    (save-excursion
			      (vhdl-beginning-of-statement 1 lim)
			      (vhdl-first-word (point)))))))
	  ;; "begin", "component", "else", "units", "record":
	  ((looking-at
	    "begin\\|component\\|else\\|units\\|record")
	   (vector "end"
		   (and (vhdl-last-word (point))
			(vhdl-first-word (point)))))
	  ;; "block", "process":
	  ((looking-at
	    "block\\|process")
	   (vector "end"
		   (or (vhdl-first-word (point))
		       (save-excursion
			 (vhdl-beginning-of-statement 1 lim)
			 (vhdl-first-word (point))))))
	  ;; "then":
	  ((looking-at "then")
	   (vector "elsif\\|else\\|end"
		   (and (vhdl-last-word (point))
			(or (vhdl-first-word (point))
			    (save-excursion
			      (vhdl-beginning-of-statement 1 lim)
			      (vhdl-first-word (point)))))))
	  ;; "for" (inside configuration declaration):
	  ;; %%% currently not handled
	  ))))
    
(defun vhdl-forward-sexp (&optional count lim)
  "Move forward across one balanced expression (sexp).
With COUNT, do it that many times."
  (interactive "p")
  (let ((count (or count 1))
	end-vec target)
    (save-excursion
      (while (> count 0)
	;; skip whitespace
	(skip-chars-forward " \t\n")
	;; Check for an unbalanced "end" keyword
	(if (and (vhdl-end-p lim)
		 (/= (preceding-char) ?_)
		 (not (vhdl-in-literal lim))
		 (not (looking-at "else")))
	    (error
	     "Containing expression ends prematurely in vhdl-forward-sexp"))
	;; If the current keyword is a "begin" keyword, then find the
	;; corresponding "end" keyword.
	(if (and (setq end-vec (vhdl-corresponding-end lim))
		 (/= (preceding-char) ?_))
	    (let (
		  ;; end-re is the statement keyword to search for
		  (end-re
		   (concat "\\b\\(" (aref end-vec 0) "\\)\\b\\([^_]\\|\\'\\)"))
		  ;; column is either the statement keyword target column
		  ;; or nil
		  (column (aref end-vec 1))
		  foundp)
	      ;; Look for the statement keyword.
	      (while (and (not foundp)
			  (re-search-forward end-re nil t)
			  (goto-char (match-beginning 0)))
		;; If we are in a literal, or not in the right target
		;; column, then try again.
		(if (or (and column (/= (current-column) column))
			(= (preceding-char) ?_)
			(vhdl-in-literal lim))
		    (forward-char)
		  ;; An "else" keyword corresponds to both the opening brace
		  ;; of the following sexp and the closing brace of the
		  ;; previous sexp.
		  (if (not (save-match-data (looking-at "else")))
		      (goto-char (match-end 1)))
		  (setq foundp t))
		)
	      (if (not foundp)
		  (error "Unbalanced keywords in vhdl-forward-sexp"))
	      )
	  ;; If the current keyword is not a "begin" keyword, then just
	  ;; perform the normal forward-sexp.
	  (forward-sexp)
	  )
	(setq count (1- count))
	)
      (setq target (point)))
    (goto-char target)
    nil))

(defun vhdl-backward-sexp (&optional count lim)
  "Move backward across one balanced expression (sexp).
With COUNT, do it that many times.  LIM bounds any required backward
searches."
  (interactive "p")
  (let ((count (or count 1))
	begin-vec target)
    (save-excursion
      (while (> count 0)
	;; Perform the normal backward-sexp, unless we are looking at
	;; "else" - an "else" keyword corresponds to both the opening brace
	;; of the following sexp and the closing brace of the previous sexp.
	(if (and (looking-at "else\\b\\([^_]\\|\\'\\)")
		 (/= (preceding-char) ?_)
		 (not (vhdl-in-literal lim)))
	    nil
	  (backward-sexp)
	  (if (and (vhdl-begin-p lim)
		   (not (vhdl-in-literal lim)))
	      (error "Containing expression ends prematurely")))
	;; If the current keyword is an "end" keyword, then find the
	;; corresponding "begin" keyword.
	(if (and (setq begin-vec (vhdl-corresponding-begin lim))
		 (/= (preceding-char) ?_))
	    (let (
		  ;; begin-re is the statement keyword to search for
		  (begin-re
		   (concat "\\b\\(" (aref begin-vec 0) "\\)\\b[^_]"))
		  ;; column is either the statement keyword target column
		  ;; or nil
		  (column (aref begin-vec 1))
		  (last-backward (point)) last-forward
		  foundp)
	      ;; Look for the statement keyword.
	      (while (and (not foundp)
			  (re-search-backward begin-re lim t))
		;; If we are in a literal or in the wrong column,
		;; then try again.
		(if (or (and column (/= (current-column) column))
			(= (preceding-char) ?_)
			(vhdl-in-literal lim))
		    (backward-char)
		  ;; If there is a supplementary keyword, then search
		  ;; forward for it.
		  (if (and (setq begin-re (aref begin-vec 2))
			   (or (not (listp begin-re))
			       ;; If aref 2 is an alist, then find the
			       ;; element corresponding to the actual
			       ;; keyword that we found.
			       (let ((keyword
				      (buffer-substring (match-beginning 1)
							(match-end 1))))
				 (setq begin-re
				       (assoc keyword begin-re))
				 (and begin-re
				      (setq begin-re (cdr begin-re))))))
		      (and
		       (setq begin-re
			     (concat "\\b\\(" begin-re "\\)\\b[^_]"))
		       (save-excursion
			 (setq last-forward (point))
			 ;; Look for the supplementary keyword
			 ;; (bounded by the backward search start
			 ;; point).
			 (while (and (not foundp)
				     (re-search-forward begin-re
							last-backward t)
				     (goto-char (match-beginning 1)))
			   ;; If we are in a literal, then try again.
			   (if (or (= (preceding-char) ?_)
				   (vhdl-in-literal last-forward))
			       (forward-char)
			     ;; We have found the supplementary keyword.
			     ;; Save the position of the keyword in foundp.
			     (setq foundp (point)))
			   )
			 foundp)
		       ;; If the supplementary keyword was found, then
		       ;; move point to the supplementary keyword.
		       (goto-char foundp))
		    ;; If there was no supplementary keyword, then
		    ;; point is already at the statement keyword.
		    (setq foundp t)))
		) ; end of the search for the statement keyword
	      (if (not foundp)
		  (error "Unbalanced keywords in vhdl-backward-sexp"))
	      ))
	(setq count (1- count))
	)
      (setq target (point)))
    (goto-char target)
    nil))


(defun vhdl-backward-to-open (&optional lim)
  "Move backward to the previous open paren or \"begin\" keyword."
  (let (foundp)
    (while (and (not foundp)
		(re-search-backward (concat "\\s(\\|\\s)\\|" vhdl-begin-re)
				    nil t))
      (if (vhdl-in-literal lim)
	  (forward-char)
	(cond
	 ;; opening paren:
	 ((looking-at "\\s(")
	  (setq foundp t))
	 ;; closing paren:
	 ((looking-at "\\s)")
	  (forward-char)
	  (backward-sexp))
	 ;; "begin" keyword:
	 ((and (vhdl-begin-p lim)
	       (/= (preceding-char) ?_))
	  (setq foundp t))
	 ))
      )
    (if (not foundp)
	(error "Unbalanced keywords in vhdl-backward-to-open"))
    ))

(defconst vhdl-conditional-re
  "\\b\\(case\\|for\\|if\\|elsif\\|while\\)\\b[^_]")

(defun vhdl-conditional-p (&optional lim)
  (and (looking-at vhdl-conditional-re)
       (cond
	;; "case", "for", "if":
	((and (looking-at "case\\|for\\|if")
	      (save-excursion
		(backward-sexp)
		(not (looking-at "end\\s-+\\w"))))
	 t)
	;; "elsif", "while":
	((looking-at "elsif\\|while")
	 t)
	)))
  
(defconst vhdl-leader-re
  "\\b\\(block\\|component\\|process\\)\\b[^_]")

(defun vhdl-end-of-leader ()
  (save-excursion
    (cond ((looking-at "block\\|process")
	   (if (save-excursion
		 (forward-sexp)
		 (skip-chars-forward " \t\n")
		 (= (following-char) ?\())
	       (forward-sexp 2)
	     (forward-sexp))
	   (point))
	  ((looking-at "component")
	   (forward-sexp)
	   (point))
	  (t nil)
	  )))

(defun vhdl-beginning-of-statement (&optional count lim)
  "Go to the beginning of the innermost VHDL statement.
With prefix arg, go back N - 1 statements.  If already at the
beginning of a statement then go to the beginning of the preceding
one.  If within a string or comment, or next to a comment (only
whitespace between), move by sentences instead of statements.

When called from a program, this function takes 2 optional args: the
prefix arg, and a buffer position limit which is the farthest back to
search."
  (interactive "p")
  (let ((here (point))
	(count (or count 1))
	(lim (or lim (point-min)))
	state)
    (save-excursion
      (goto-char lim)
      (setq state (parse-partial-sexp (point) here nil nil)))
    (if (and (interactive-p)
	     (or (nth 3 state)
		 (nth 4 state)
		 (looking-at (concat "[ \t]*" comment-start-skip))))
	(forward-sentence (- count))
      (while (> count 0)
	(vhdl-beginning-of-statement-1 lim)
	(setq count (1- count))))
    ;; its possible we've been left up-buf of lim
    (goto-char (max (point) lim))
    )
  (vhdl-keep-region-active))

(defun vhdl-beginning-of-statement-1 (&optional lim)
  ;; move to the start of the current statement, or the previous
  ;; statement if already at the beginning of one.
  (let ((firstp t)
	(here (point))
	donep
	(last-begin (point)))
    (while (not donep)
      ;; stop at beginning of buffer
      (if (bobp) (setq donep t)
	;; go backwards one balanced expression, but be careful of
	;; unbalanced paren being reached
	(if (not (vhdl-safe (progn (backward-sexp 1) t)))
	    (progn
	      (if firstp
		  (vhdl-backward-to-open lim)
		(goto-char last-begin))
	      ;; skip over any unary operators, or other special
	      ;; characters appearing at front of identifier
	      (save-excursion
		(vhdl-backward-syntactic-ws lim)
		(skip-chars-backward "-+!*&:.~ \t\n")
		(if (= (preceding-char) ?\()
		    (setq last-begin (point))))
	      (goto-char last-begin)
	      (setq last-begin (point)
		    donep t)))
	;; see if we're in a literal. if not, then this bufpos may be
	;; a candidate for stopping
	(cond
	 ;; CASE 0: did we hit the error condition above?
	 (donep)
	 ;; CASE 1: are we in a literal?
	 ((vhdl-in-literal lim))
	 ;; CASE 2: is this the first time we're checking?
	 (firstp (setq firstp nil
		       last-begin (point)))
	 ;; CASE 3: are we looking at an open paren?
	 ((looking-at "\\s(")
	  (setq last-begin (point)))
	 ;; CASE 4: are we looking at a conditional keyword?
	 ((and (vhdl-conditional-p lim)
	       (/= (preceding-char) ?_))
	  (setq last-begin (point)
		donep t))
	 ;; CASE 5: have we crossed a statement barrier?
	 ((let ((lim (point))
		crossedp pos)
	    (save-excursion
	      (while (and (not crossedp)
			  (< (point) last-begin)
			  (re-search-forward (concat ";\\|" vhdl-begin-re)
					     (1+ last-begin) t)
			  (goto-char (match-beginning 0)))
		;; If we are looking at a semicolon, then use last-begin as-is.
		(if (and (= (following-char) ?\;)
			 (not (vhdl-in-literal lim)))
		    (setq crossedp t
			  donep t)
		  ;; If we are looking at a "begin", then stop
		  (if (and (vhdl-begin-p)
			   (not (looking-at "else"))
			   (/= (preceding-char) ?_)
			   (not (vhdl-in-literal lim)))
		      (progn
			;; If it's a leader "begin", then find the right place
			(if (and (looking-at vhdl-leader-re)
				 (setq pos (vhdl-end-of-leader))
				 (<= pos here))
			    (progn
			      (goto-char pos)
			      (skip-chars-forward " \t\n")
			      (setq last-begin (min (point) here))))
			(setq crossedp t
			      donep t))
		    (forward-char)))
		))
	    crossedp))
	 ;; CASE 6: ignore labels
	 ((looking-at vhdl-label-key))
	 ;; CASE 7: are we looking at an end keyword?
	 ((and (vhdl-end-p lim)
	       (not (looking-at "else")))
	  (goto-char (vhdl-point 'boi))
	  (setq last-begin (point)
		donep t))
	 ;; CASE 8: nothing special
	 (t (setq last-begin (point)))
	 )))
    (goto-char last-begin)
    ;; we always do want to skip over non-whitespace modifier
    ;; characters that didn't get skipped above
    (skip-chars-backward "-+!*&:.~" (vhdl-point 'boi))))


(defconst vhdl-defun-re
  "\\b\\(architecture\\|block\\|configuration\\|entity\\|package\\|process\\|procedure\\|function\\)\\b[^_]")

(defun vhdl-defun-p ()
  (save-excursion
    (and (looking-at vhdl-defun-re)
	 (goto-char (match-beginning 1))
	 (cond
	  ;; "architecture", "configuration", "entity",
	  ;; "package", "procedure", "function":
	  ((looking-at
	    "architecture\\|configuration\\|entity\\|package\\|procedure\\|function")
	   t)
	  ;; "block", "process":
	  ((and (looking-at "block\\|process")
		(save-excursion
		  (backward-sexp)
		  (not (looking-at "end\\s-+\\w"))))
	   t)
	  ))))
  
(defun vhdl-corresponding-defun ()
  "If the word at the current position corresponds to a \"defun\"
keyword, then return a string that can be used to find the
corresponding \"begin\" keyword, else return nil."
  (save-excursion
    (and (vhdl-defun-p)
	 (cond
	  ;; "block", "process":
	  ((looking-at "block\\|process")
	   (buffer-substring (match-beginning 0) (match-end 0)))
	  ;; "architecture", "configuration", "entity", "package",
	  ;; "procedure", "function":
	  ((looking-at
	    "architecture\\|configuration\\|entity\\|package\\|procedure\\|function")
	   "is")
	  ))))

(defun vhdl-beginning-of-defun (&optional count)
  "Move backward to the beginning of a VHDL defun.
With argument, do it that many times.
Returns t unless search stops due to beginning or end of buffer."
  (interactive "p")
  (let ((count (or count 1))
	(last-forward (point))
	foundp)
    (while (> count 0)
      (setq foundp nil)
      (goto-char last-forward)
      (let ((last-backward (1+ (point))) begin-string)
	(while (and (not foundp)
		    (re-search-backward vhdl-defun-re nil 'move))
	  ;; If we are in a literal, then try again.
	  (if (or (= (preceding-char) ?_)
		  (vhdl-in-literal (point-min)))
	      (backward-char)
	    (if (setq begin-string (vhdl-corresponding-defun))
		;; This is a real defun keyword.
		;; Find the corresponding "begin" keyword.
		;; Look for the begin keyword.
		(while (and (not foundp)
			    (setq last-forward (point))
			    (search-forward begin-string last-backward t)
			    )
		  (if (or (= (preceding-char) ?_)
			  (vhdl-in-literal last-forward))
		      (forward-char)
		    ;; We have found the begin keyword.
		    (goto-char last-forward)
		    (beginning-of-line)
		    (setq foundp t))
		  ) ; end of the search for the begin keyword
	      ))
	  ) ; end of the search for the defun keyword
	)
      (setq count (1- count))
      )
    (vhdl-keep-region-active)
    foundp))
    
(defun vhdl-find-enclosing-defun ()
  (let* ((current (current-indentation))
	 (indent current))
    (while (and (not (bobp))
		(>= indent current))
      (vhdl-beginning-of-defun)
      (setq indent (current-indentation))
      )))


(defun vhdl-forward-to-open (&optional lim)
  ;; Replacement for (scan-lists from 1 -1)
  "Return the position of the first open paren or \"begin\" keyword."
  (let (foundp)
    (while (and (not foundp)
		(re-search-forward (concat "\\s(\\|\\s)\\|" vhdl-begin-re
					   "\\|" vhdl-end-re)
				   nil t)
		(goto-char (match-beginning 0)))
      (if (vhdl-in-literal lim)
	  (forward-char)
	(cond
	 ;; opening paren:
	 ((looking-at "\\s(")
	  (forward-char)
	  (setq foundp (point)))
	 ;; closing paren:
	 ((looking-at "\\s)")
	  (error "Containing expression ends prematurely in vhdl-forward-to-open"))
	 ;; "begin" keyword:
	 ((and (vhdl-begin-p lim)
	       (/= (preceding-char) ?_))
	  (forward-sexp)
	  (setq foundp (point)))
	 ;; "end" keyword:
	 ((and (vhdl-end-p lim)
	       (/= (preceding-char) ?_))
	  (error "Containing expression ends prematurely in vhdl-forward-to-open"))
	 (t
	  (forward-char))
	 ))
      )
    (or foundp
	(error "Unbalanced keywords in vhdl-forward-to-open"))
    ))

(defun vhdl-corresponding-mid (&optional lim)
  (cond
   ((looking-at "is\\|block\\|process")
    "begin")
   ((looking-at "then")
    "<else>")
   (t
    "end")))


;; utilities for moving and querying around syntactic elements
(defun vhdl-parse-state ()
  ;; Finds and records all open parens between some important point
  ;; earlier in the file and point.
  (save-excursion
    (let* ((here (point))
	   (pos (progn (vhdl-find-enclosing-defun) (point)))
	   (at-bob (bobp)) (last-bod pos) enclosingp
	   mid-state state sexp-start sexp-mid sexp-end)
      ;; cache last bod position
      (while (catch 'backup-bod
	       (setq mid-state nil
		     state nil)
	       (while (and pos (< pos here))
		 (setq pos (vhdl-safe
			    (progn (vhdl-forward-to-open last-bod)
				   (point))))
		 (if (and pos (<= pos here))
		     ;; pos is before here
		     (progn
		       (backward-char)
		       (or (looking-at "\\s(")
			   (backward-sexp))
		       (if (and (looking-at "else\\b[^_]")
				(not
				 (and (car state)
				      (consp (car state))
				      (equal (car mid-state) "<else>"))))
			   ;; the "else" wasn't a block-open
			   nil
			 (setq sexp-start (point))
			 (setq sexp-mid (vhdl-corresponding-mid last-bod))
			 (setq sexp-end (vhdl-safe
					 (save-excursion
					   (vhdl-forward-sexp 1 last-bod)
					   (point))))
			 ;; If our containing-sexp is a sexp-mid, and we
			 ;; are looking at "begin", then delete the
			 ;; containing-sexp from the list.
			 (if (and (car state)
				  (not (consp (car state)))
				  (looking-at (car mid-state)))
			     (setq mid-state (cdr mid-state)
				   state (cdr state)))
			 (if (and sexp-end (<= sexp-end here))
			     ;; we want to record both the start and end
			     ;; of this sexp, but we only want to record
			     ;; the last-most of any of them before here
			     (progn
			       ;; Make sure it's a "begin", not a paren.
			       (if (not (looking-at "\\s("))
				   (setq mid-state (cons sexp-mid
							 (if (consp (car state))
							     (cdr mid-state)
							   mid-state))
					 state (cons (cons sexp-start sexp-end)
						     (if (consp (car state))
							 (cdr state)
						       state))))
			       (setq pos sexp-end))
			   ;; we're contained in this sexp so put sexp-start on
			   ;; front of list
			   (setq mid-state (cons sexp-mid mid-state)
				 state (cons sexp-start state))
			   (if (not (looking-at "\\s("))
			       (setq enclosingp t))))
		       (goto-char pos))
		   ;; pos is after here, or not found
		   (or enclosingp
		       at-bob
		       (progn
			 (goto-char last-bod)
			 (vhdl-find-enclosing-defun)
			 (setq pos (point))
			 (setq at-bob (bobp))
			 (setq last-bod (point))
			 (throw 'backup-bod t)))
		   ))
	       nil))
      (vector state mid-state))))

(defun test-vhdl-parse-state ()
  (interactive)
  (let (state here (delay 0.5))
    (setq here (point))
    (message "%s" (prin1-to-string (setq state (vhdl-parse-state))))
    (setq state (nreverse (aref state 0)))
    (while state
      (if (consp (car state))
	  (progn
	    (goto-char (car (car state)))
	    (sit-for delay)
	    (goto-char (cdr (car state))))
	(goto-char (car state)))
      (sit-for delay)
      (setq state (cdr state)))
    (goto-char here)
    ))

(setq elp-function-list
      '(
	vhdl-guess-basic-syntax
	vhdl-parse-state
	vhdl-narrow-to-enclosing-sexp
	vhdl-beginning-of-statement
	vhdl-beginning-of-statement-1
	vhdl-find-enclosing-defun
	vhdl-beginning-of-defun
	vhdl-forward-sexp
	vhdl-backward-sexp
	vhdl-corresponding-begin
	vhdl-corresponding-mid
	vhdl-corresponding-end
	vhdl-corresponding-defun
	vhdl-forward-to-open
	vhdl-backward-to-open
	vhdl-begin-p
	vhdl-end-p
	vhdl-defun-p
	vhdl-conditional-p
	vhdl-first-word
	vhdl-last-word
	vhdl-keep-region-active
	vhdl-end-of-leader
	vhdl-forward-syntactic-ws
	vhdl-backward-syntactic-ws
	vhdl-in-literal
	))

;; (elp-instrument-list elp-function-list)

(defun trace-all-functions ()
  (interactive)
  (let ((list elp-function-list))
    (while list
      (trace-function-background (car list))
      (setq list (cdr list)))))


;; defuns for calculating the syntactic state and indenting a single
;; line of VHDL code
(defmacro vhdl-add-syntax (symbol &optional relpos)
  ;; a simple macro to append the syntax in symbol to the syntax list.
  ;; try to increase performance by using this macro
  (` (setq syntax (cons (cons (, symbol) (, relpos)) syntax))))

(defun vhdl-narrow-to-enclosing-sexp (state lim)
  ;; find the most nested opening brace, if there is one.
  (let (enclosing-sexp current-sexp
	containing-sexp preceding-sexp containing-begin)
    (and state
	 (save-excursion
	   (goto-char lim)
	   (skip-chars-forward " \t")
	   (and (vhdl-end-p)
		(/= (preceding-char) ?_)
		(not (vhdl-in-literal (point-min)))
		(forward-sexp))
	   (skip-chars-backward " \t")
	   (while (and state
		       (not enclosing-sexp))
	     (setq current-sexp (car state)
		   state (cdr state))
	     (if (consp current-sexp)
		 ;; if cdr == point, then current sexp is the brace
		 ;; that opens the sexp we close
		 (if (= (cdr current-sexp) (point))
		     (setq current-sexp (car current-sexp))
		   ;; otherwise, it is the preceding sexp
		   (or preceding-sexp
		       (setq preceding-sexp (car current-sexp)))
		   (setq current-sexp nil))
	       ;; ignore the bufpos if its been narrowed out by the
	       ;; containing libunit
	       (if (<= current-sexp (point-min))
		   (setq current-sexp nil)))
	     ;; if we've already found the containing-sexp, then this must
	     ;; be the enclosing-sexp.
	     (and containing-sexp
		  (setq enclosing-sexp current-sexp))
	     ;; if we haven't already found the containing-sexp, then this
	     ;; is it.
	     (and (not containing-sexp)
		  (setq containing-sexp current-sexp)
		  (setq containing-begin
			(save-excursion
			  (goto-char containing-sexp)
			  (vhdl-begin-p))))
	     )
	   enclosing-sexp)
	 (narrow-to-region
	  (progn
	    (goto-char enclosing-sexp)
	    (vhdl-point 'bol))
	  ;; end point is the end of the current line
	  (progn
	    (goto-char lim)
	    (vhdl-point 'eol))))
    ;; return the containing-sexp point
    (vector containing-sexp preceding-sexp containing-begin)))

(defun vhdl-skip-case-alternative-forward (&optional lim)
  ;; skip forward over case/when bodies, with optional maximal
  ;; limit. if no next case alternative is found, nil is returned and point
  ;; is not moved
  (let ((lim (or lim (point-max)))
	(here (point))
	donep foundp)
    (while (and (< (point) lim)
		(not donep))
      (if (and (re-search-forward vhdl-case-alternative-key lim 'move)
	       (save-match-data
		 (not (vhdl-in-literal)))
	       (/= (match-beginning 0) here))
	  (progn
	    (goto-char (match-beginning 0))
	    (setq donep t
		  foundp t))))
    (if (not foundp)
	(goto-char here))
    foundp))

(defun vhdl-guess-basic-syntax ()
  ;; guess the syntactic description of the current line of VHDL code.
  (save-excursion
    (save-restriction
      (beginning-of-line)
      (let* ((indent-point (point))
	     (case-fold-search nil)
	     (state (vhdl-parse-state))
	     (mid-state (aref state 1))
	     (state (aref state 0))
	     literal insexp-p containing-sexp preceding-sexp containing-begin
	     char-before-ip char-after-ip begin-after-ip end-after-ip
	     lim syntax placeholder
	     )

	(setq insexp-p
	      (vhdl-narrow-to-enclosing-sexp state indent-point))
	(setq containing-sexp (aref insexp-p 0))
	(setq preceding-sexp (aref insexp-p 1))
	(setq containing-begin (aref insexp-p 2))

	;; set the limit on the farthest back we need to search
	(setq lim (if containing-sexp
		      (save-excursion
			(goto-char containing-sexp)
			(vhdl-point 'bol))
		    (point-min)))

	;; cache char before and after indent point, and move point to
	;; the most likely position to perform the majority of tests
	(goto-char indent-point)
	(skip-chars-forward " \t")
	(setq char-after-ip (following-char))
	(setq begin-after-ip (vhdl-begin-p))
	(setq end-after-ip (vhdl-end-p))
	(vhdl-backward-syntactic-ws lim)
	(setq char-before-ip (preceding-char))
	(goto-char indent-point)
	(skip-chars-forward " \t")

	;; are we in a literal?
	(setq literal (vhdl-in-literal lim))

	;; now figure out syntactic qualities of the current line
	(cond
	 ;; CASE 1: in a string.
	 ;; CASE 2: in a comment.
	 ((memq literal '(string comment))
	  (vhdl-add-syntax literal (vhdl-point 'bopl)))
	 ;; CASE 5: Line is at top level.
	 ((null containing-sexp)
	  (cond
	   ;; CASE 5A: we are looking at a top-level defun opening brace
	   (begin-after-ip
	    ;; if there is a preceding-sexp then indent relative to it
	    (if preceding-sexp
		(progn
		  (goto-char preceding-sexp)
		  ;; if not at boi, then defun-opening braces are hung on
		  ;; right side, so we need a different relpos
		  (if (/= (point) (vhdl-point 'boi))
		      (vhdl-beginning-of-statement 1 lim)))
	      ;; otherwise, use the preceding statement
	      (vhdl-beginning-of-statement 1 lim))
	    ;; c-b-o-s could have left us at point-min
	    (and (bobp)
		 (vhdl-forward-syntactic-ws indent-point))
	    (vhdl-add-syntax 'block-open (vhdl-point 'bol))
	    )
	   ;; CASE 5D: we are looking at a top-level statement-cont
	   ((= char-before-ip ?,)
	    (goto-char indent-point)
	    (vhdl-backward-syntactic-ws lim)
	    (while (and (< lim (point))
			(= (preceding-char) ?,))
	      (beginning-of-line)
	      (vhdl-backward-syntactic-ws lim))
	    (vhdl-beginning-of-statement 1 lim)
	    (vhdl-add-syntax 'statement-cont (vhdl-point 'boi)))
	   ;; CASE 5H: we are looking at a top-level statement
	   ((progn
	      (vhdl-backward-syntactic-ws lim)
	      (or (bobp)
		  (= (preceding-char) ?\;)))
	    (vhdl-beginning-of-statement 1 lim)
	    (vhdl-add-syntax 'statement (vhdl-point 'bol)))
	   ;; CASE 5J: we are at a top-level statement-cont
	   (t
	    (vhdl-beginning-of-statement 1 lim)
	    (vhdl-add-syntax 'statement-cont (vhdl-point 'boi)))
	   ))				; end CASE 5
	 ;; CASE 6: line is an expression, not a statement.  Most
	 ;; likely we are either in a function prototype or a function
	 ;; call argument list
	 ((null containing-begin)
	  (vhdl-backward-syntactic-ws containing-sexp)
	  (cond
	   ;; CASE 6A: we are looking at the arglist closing paren
	   ((and (/= char-before-ip ?,)
		 (eq char-after-ip ?\)))
	    (goto-char containing-sexp)
	    (vhdl-add-syntax 'arglist-close (vhdl-point 'boi)))
	   ;; CASE 6B: we are looking at the first argument in an empty
	   ;; argument list. Use arglist-close if we're actually
	   ;; looking at a close paren.
	   ((eq char-before-ip ?\()
	    (goto-char containing-sexp)
	    (vhdl-add-syntax 'arglist-intro (vhdl-point 'boi)))
	   ;; CASE 6D: we are looking at an arglist continuation line,
	   ;; but the preceding argument is on the same line as the
	   ;; opening paren.  This case includes multi-line
	   ;; mathematical paren groupings.
	   ((and (save-excursion
		   (goto-char (1+ containing-sexp))
		   (skip-chars-forward " \t")
		   (not (eolp)))
		 (save-excursion
		   (vhdl-beginning-of-statement)
		   (skip-chars-backward " \t(")
		   (<= (point) containing-sexp)))
	    (goto-char containing-sexp)
	    (vhdl-add-syntax 'arglist-cont-nonempty (vhdl-point 'boi)))
	   ;; CASE 6F: we are looking at just a normal arglist
	   ;; continuation line
	   (t (vhdl-beginning-of-statement 1 containing-sexp)
	      (forward-char 1)
	      (vhdl-forward-syntactic-ws indent-point)
	      (vhdl-add-syntax 'arglist-cont (vhdl-point 'boi)))
	   ))
	 ;; CASE 7: A defun mid open
	 ((and begin-after-ip
	       (looking-at (car mid-state)))
	  (goto-char containing-sexp)
	  ;; if not at boi, then defun-opening braces are hung on
	  ;; right side, so we need a different relpos
	  (if (/= (point) (vhdl-point 'boi))
	      (vhdl-beginning-of-statement))
	  (vhdl-add-syntax 'block-open (vhdl-point 'boi)))
	 ;; CASE 8: block close brace
	 (end-after-ip
	  (goto-char containing-sexp)
	  (if (/= (point) (vhdl-point 'boi))
	      (vhdl-beginning-of-statement))
	  (vhdl-add-syntax 'block-close (vhdl-point 'boi)))
	 ;; CASE 9: A continued statement
	 ((and (not (memq char-before-ip '(?\; ?:)))
	       (> (point)
		  (save-excursion
		    (vhdl-beginning-of-statement 1 containing-sexp)
		    (setq placeholder (point))))
	       (/= placeholder containing-sexp))
	  (goto-char indent-point)
	  (skip-chars-forward " \t")
	  (cond
	   ;; CASE 9B: open braces for libunit
	   (begin-after-ip
	    (cond
	     ;; CASE 9B.3: catch-all for unknown construct.
	     (t
	      ;; Even though this isn't right, it's the best I'm going
	      ;; to do for now. Exceptions probably fall through to
	      ;; here, but aren't supported yet.  Also, after the next
	      ;; release, I may call a recognition hook like so:
	      ;; (run-hooks 'vhdl-recognize-hook), but I dunno.
	      (vhdl-add-syntax 'statement-cont placeholder)
	      (vhdl-add-syntax 'block-open))
	     ))
	   ;; CASE 9D: continued statement. find the accurate
	   ;; beginning of statement
	   (t
	    (vhdl-beginning-of-statement 1 placeholder)
	    (vhdl-add-syntax 'statement-cont (point)))
	   ))
	 ;; Statement. But what kind?
	 ;; CASE 12: A case alternative key
	 ((looking-at vhdl-case-alternative-key)
	  (goto-char containing-sexp)
	  ;; for a case alternative key, we set relpos the first
	  ;; non-whitespace char on the line containing the case key.
	  (vhdl-add-syntax 'case-alternative (vhdl-point 'boi)))
	 ;; CASE 13: any other label
	 ((looking-at vhdl-label-key)
	  (goto-char containing-sexp)
	  (vhdl-add-syntax 'label (vhdl-point 'boi)))
	 ;; CASE 15: statement catchall
	 (t
	  ;; we know its a statement, but we need to find out if it is
	  ;; the first statement in a block
	  (goto-char containing-sexp)
	  ;; if it's a leader "begin" keyword, then find the end of the
	  ;; leader, else just forward-sexp past the other trailing "begin"
	  ;; keywords.
	  (if (looking-at vhdl-leader-re)
	      (goto-char (vhdl-end-of-leader))
	    (forward-sexp))
	  (vhdl-forward-syntactic-ws indent-point)
	  ;; we want to ignore labels when skipping forward
	  (let ((ignore-re (concat vhdl-case-alternative-key "\\|"
				   vhdl-label-key))
		incase-p)
	    (while (looking-at ignore-re)
	      (if (looking-at vhdl-case-alternative-key)
		  (progn
		    (setq incase-p t)
		    ;; we also want to skip over the body of the
		    ;; case/when statement if that doesn't put us at
		    ;; after the indent-point
		    (while (vhdl-skip-case-alternative-forward indent-point))))
	      (forward-line 1)
	      (vhdl-forward-syntactic-ws indent-point))
	    (cond
	     ;; CASE 15A: we saw a case/when statement so we must be
	     ;; in a switch statement.  find out if we are at the
	     ;; statement just after a case alternative key
	     ((and incase-p
		   (save-excursion
		     (goto-char indent-point)
		     (vhdl-backward-syntactic-ws containing-sexp)
		     (back-to-indentation)
		     (setq placeholder (point))
		     (looking-at vhdl-case-alternative-key)))
	      (vhdl-add-syntax 'block-intro placeholder))
	     ;; CASE 15B: continued statement
	     ((= char-before-ip ?,)
	      (vhdl-add-syntax 'statement-cont (vhdl-point 'boi)))
	     ;; CASE 15D: any old statement
	     ((< (point) indent-point)
	      (vhdl-add-syntax 'statement (vhdl-point 'boi))
	      (if begin-after-ip
		  (vhdl-add-syntax 'block-open)))
	     ;; CASE 15F: first statement in a block
	     (t (goto-char containing-sexp)
		(if (/= (point) (vhdl-point 'boi))
		    (vhdl-beginning-of-statement))
		(vhdl-add-syntax 'block-intro (vhdl-point 'boi))
		(if begin-after-ip
		    (vhdl-add-syntax 'block-open)))
	     )))
	 )

	;; now we need to look at any modifiers
	(goto-char indent-point)
	(skip-chars-forward " \t")
	(if (looking-at vhdl-comment-start-regexp)
	    ;; we are looking at a comment. if the comment is at or to
	    ;; the right of comment-column, then all we want on the
	    ;; syntax list is comment, otherwise, the
	    ;; indentation of the comment is relative to where a
	    ;; normal statement would indent
	    (if (< (current-column) comment-column)
		(vhdl-add-syntax 'comment)
	      ;; reset syntax kludge
	      (setq syntax nil)
	      (vhdl-add-syntax 'comment)))
	;; return the syntax
	syntax))))


;; indent via syntactic language elements
(defun vhdl-get-offset (langelem)
  ;; Get offset from LANGELEM which is a cons cell of the form:
  ;; (SYMBOL . RELPOS).  The symbol is matched against
  ;; vhdl-offsets-alist and the offset found there is either returned,
  ;; or added to the indentation at RELPOS.  If RELPOS is nil, then
  ;; the offset is simply returned.
  (let* ((symbol (car langelem))
	 (relpos (cdr langelem))
	 (match  (assq symbol vhdl-offsets-alist))
	 (offset (cdr-safe match)))
    ;; offset can be a number, a function, a variable, or one of the
    ;; symbols + or -
    (cond
     ((not match)
      (if vhdl-strict-syntax-p
	  (error "don't know how to indent a %s" symbol)
	(setq offset 0
	      relpos 0)))
     ((eq offset '+)  (setq offset vhdl-basic-offset))
     ((eq offset '-)  (setq offset (- vhdl-basic-offset)))
     ((eq offset '++) (setq offset (* 2 vhdl-basic-offset)))
     ((eq offset '--) (setq offset (* 2 (- vhdl-basic-offset))))
     ((and (not (numberp offset))
	   (fboundp offset))
      (setq offset (funcall offset langelem)))
     ((not (numberp offset))
      (setq offset (eval offset)))
     )
    (+ (if (and relpos
		(< relpos (vhdl-point 'bol)))
	   (save-excursion
	     (goto-char relpos)
	     (current-column))
	 0)
       offset)))

(defun vhdl-indent-line (&optional syntax)
  ;; indent the current line as VHDL code. Optional SYNTAX is the
  ;; syntactic information for the current line. Returns the amount of
  ;; indentation change
  (let* ((vhdl-syntactic-context (or syntax (vhdl-guess-basic-syntax)))
	 (pos (- (point-max) (point)))
	 (indent (apply '+ (mapcar 'vhdl-get-offset vhdl-syntactic-context)))
	 (shift-amt  (- (current-indentation) indent)))
    (and vhdl-echo-syntactic-information-p
	 (message "syntax: %s, indent= %d" vhdl-syntactic-context indent))
    (if (zerop shift-amt)
	nil
      (delete-region (vhdl-point 'bol) (vhdl-point 'boi))
      (beginning-of-line)
      (indent-to indent))
    (if (< (point) (vhdl-point 'boi))
	(back-to-indentation)
      ;; If initial point was within line's indentation, position after
      ;; the indentation.  Else stay at same point in text.
      (if (> (- (point-max) pos) (point))
	  (goto-char (- (point-max) pos)))
      )
    (run-hooks 'vhdl-special-indent-hook)
    shift-amt))

(defun vhdl-indent-command (&optional whole-exp)
  "Indent current line as VHDL code, or in some cases insert a tab character.

If `vhdl-tab-always-indent' is t, always just indent the current line.
If nil, indent the current line only if point is at the left margin or
in the line's indentation; otherwise insert a tab.  If other than nil
or t, then tab is inserted only within literals (comments and strings)
and inside preprocessor directives, but line is always reindented.

A numeric argument, regardless of its value, means indent rigidly all
the lines of the expression starting after point so that this line
becomes properly indented.  The relative indentation among the lines
of the expression are preserved."
  (interactive "P")
  (let ((bod (vhdl-point 'bod)))
    (if whole-exp
	;; If arg, always indent this line as VHDL
	;; and shift remaining lines of expression the same amount.
	(let ((shift-amt (vhdl-indent-line))
	      beg end)
	  (save-excursion
	    (if (eq vhdl-tab-always-indent t)
		(beginning-of-line))
	    (setq beg (point))
	    (forward-sexp 1)
	    (setq end (point))
	    (goto-char beg)
	    (forward-line 1)
	    (setq beg (point)))
	  (if (> end beg)
	      (indent-code-rigidly beg end (- shift-amt))))
      ;; No arg supplied, use vhdl-tab-always-indent to determine
      ;; behavior
      (cond
       ;; CASE 1: indent when at column zero or in lines indentation,
       ;; otherwise insert a tab
       ((not vhdl-tab-always-indent)
	(if (save-excursion
	      (skip-chars-backward " \t")
	      (not (bolp)))
	    (insert-tab)
	  (vhdl-indent-line)))
       ;; CASE 2: just indent the line
       ((eq vhdl-tab-always-indent t)
	(vhdl-indent-line))
       ;; CASE 3: if in a literal, insert a tab, but always indent the
       ;; line
       (t
	(if (vhdl-in-literal bod)
	    (insert-tab))
	(vhdl-indent-line)
	)))))

(defun vhdl-show-syntactic-information ()
  "Show syntactic information for current line."
  (interactive)
  (message "syntactic analysis: %s" (vhdl-guess-basic-syntax))
  (vhdl-keep-region-active))


;; Standard indentation line-ups
(defun vhdl-lineup-arglist (langelem)
  ;; lineup the current arglist line with the arglist appearing just
  ;; after the containing paren which starts the arglist.
  (save-excursion
    (let* ((containing-sexp
	    (save-excursion
	      ;; arglist-cont-nonempty gives relpos ==
	      ;; to boi of containing-sexp paren. This
	      ;; is good when offset is +, but bad
	      ;; when it is vhdl-lineup-arglist, so we
	      ;; have to special case a kludge here.
	      (if (memq (car langelem) '(arglist-intro arglist-cont-nonempty))
		  (progn
		    (beginning-of-line)
		    (backward-up-list 1)
		    (skip-chars-forward " \t" (vhdl-point 'eol)))
		(goto-char (cdr langelem)))
	      (point)))
	   (cs-curcol (save-excursion
			(goto-char (cdr langelem))
			(current-column))))
      (if (save-excursion
	    (beginning-of-line)
	    (looking-at "[ \t]*)"))
	  (progn (goto-char (match-end 0))
		 (forward-sexp -1)
		 (forward-char 1)
		 (vhdl-forward-syntactic-ws)
		 (- (current-column) cs-curcol))
	(goto-char containing-sexp)
	(or (eolp)
	    (let ((eol (vhdl-point 'eol))
		  (here (progn
			  (forward-char 1)
			  (skip-chars-forward " \t")
			  (point))))
	      (vhdl-forward-syntactic-ws)
	      (if (< (point) eol)
		  (goto-char here))))
	(- (current-column) cs-curcol)
	))))

(defun vhdl-lineup-arglist-intro-after-paren (langelem)
  ;; lineup an arglist-intro line to just after the open paren
  (save-excursion
    (let ((cs-curcol (save-excursion
		       (goto-char (cdr langelem))
		       (current-column)))
	  (ce-curcol (save-excursion
		       (beginning-of-line)
		       (backward-up-list 1)
		       (skip-chars-forward " \t" (vhdl-point 'eol))
		       (current-column))))
      (- ce-curcol cs-curcol -1))))

(defun vhdl-lineup-comment (langelem)
  ;; support old behavior for comment indentation. we look at
  ;; vhdl-comment-only-line-offset to decide how to indent comment
  ;; only-lines
  (save-excursion
    (back-to-indentation)
    ;; at or to the right of comment-column
    (if (>= (current-column) comment-column)
	(vhdl-comment-indent)
      ;; otherwise, indent as specified by vhdl-comment-only-line-offset
      (if (not (bolp))
	  (or (car-safe vhdl-comment-only-line-offset)
	      vhdl-comment-only-line-offset)
	(or (cdr-safe vhdl-comment-only-line-offset)
	    (car-safe vhdl-comment-only-line-offset)
	    -1000			;jam it against the left side
	    )))))

(defun vhdl-lineup-math (langelem)
  ;; line up math statement-cont after the equals
  (save-excursion
    (let* ((relpos (cdr langelem))
	   (equalp (save-excursion
		     (goto-char (vhdl-point 'boi))
		     (skip-chars-forward "^=" (vhdl-point 'eol))
		     (and (= (following-char) ?=)
			  (- (point) (vhdl-point 'boi)))))
	   (curcol (progn
		     (goto-char relpos)
		     (current-column)))
	   donep)
      (while (and (not donep)
		  (< (point) (vhdl-point 'eol)))
	(skip-chars-forward "^=" (vhdl-point 'eol))
	(if (vhdl-in-literal (cdr langelem))
	    (forward-char 1)
	  (setq donep t)))
      (if (/= (following-char) ?=)
	  ;; there's no equal sign on the line
	  vhdl-basic-offset
	;; calculate indentation column after equals and ws, unless
	;; our line contains an equals sign
	(if (not equalp)
	    (progn
	      (forward-char 1)
	      (skip-chars-forward " \t")
	      (setq equalp 0)))
	(- (current-column) equalp curcol))
      )))


;; defuns for submitting bug reports

(defconst vhdl-version "$Revision: 2.22 $"
  "vhdl-mode version number.")
(defconst vhdl-mode-help-address "rwhitby@research.canon.oz.au"
  "Address accepting submission of bug reports.")

(defun vhdl-version ()
  "Echo the current version of vhdl-mode in the minibuffer."
  (interactive)
  (message "Using vhdl-mode %s" vhdl-version)
  (vhdl-keep-region-active))

;; get reporter-submit-bug-report when byte-compiling
(and (fboundp 'eval-when-compile)
     (eval-when-compile
      (require 'reporter)))

(defun vhdl-submit-bug-report ()
  "Submit via mail a bug report on vhdl-mode."
  (interactive)
  ;; load in reporter
  (let ((reporter-prompt-for-summary-p t))
    (and
     (y-or-n-p "Do you want to submit a report on vhdl-mode? ")
     (require 'reporter)
     (reporter-submit-bug-report
      vhdl-mode-help-address
      (concat "vhdl-mode " vhdl-version)
      (let ((vars (list
		   ;; report only the vars that affect indentation
		   'vhdl-basic-offset
		   'vhdl-offsets-alist
		   'vhdl-comment-only-line-offset
		   'vhdl-tab-always-indent
		   'defun-prompt-regexp
		   'tab-width
		   )))
	(if (not (boundp 'defun-prompt-regexp))
	    (delq 'defun-prompt-regexp vars)
	  vars))
      (function
       (lambda ()
	 (insert
	  (if vhdl-special-indent-hook
	      (concat "\n@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n"
		      "vhdl-special-indent-hook is set to '"
		      (format "%s" vhdl-special-indent-hook)
		      ".\nPerhaps this is your problem?\n"
		      "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n\n")
	    "\n")
	  (format "vhdl-emacs-features: %s\n" vhdl-emacs-features)
	  )))
      nil
      "Dear Rod,"
      ))))


;; menus for XEmacs (formerly Lucid)
(defun vhdl-popup-menu (e)
  "Pops up the VHDL menu."
  (interactive "@e")
  (popup-menu (cons (concat mode-name " Mode Commands") vhdl-mode-menu))
  (vhdl-keep-region-active))

(provide 'vhdl-mode)
;;; vhdl-mode.el ends here
---------------------------------8<--------------------------------------------

-- 
--------------------------------------------- _--_|\  |
Rod Whitby (rwhitby@research.canon.oz.au)    /      \ |
Canon Information Systems Research Australia \_.--._/ |
1 Thomas Holt Drive, North Ryde, N.S.W., 2113.     v  |
Technical Details
Message-ID: <RWHITBY.94Oct17082829@bing.research.canon.oz.au>
Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!howland.reston.ans.net!gatech!newsxfer.itd.umich.edu!zip.eecs.umich.edu!caen!msuinfo!harbinger.cc.monash.edu.au!news.cs.su.oz.au!metro!mama.research.canon.oz.au!news
References: <37krl8$1om0@sernews.raleigh.ibm.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1089ad,c9aeb1fe65cbd23d
X-Google-Attributes: gid1089ad,public
X-Google-Thread: 107729,123d6ca6e3ab2c27,start
X-Google-Attributes: gid107729,public
X-Google-ArrivalTime: 1994-10-19 02:34:07 PST
Newsgroups: comp.lang.vhdl,gnu.emacs.sources
Path: arc1-ca.pugleaf.net!bga.com!news.sprintlink.net!howland.reston.ans.net!gatech!newsxfer.itd.umich.edu!zip.eecs.umich.edu!caen!msuinfo!harbinger.cc.monash.edu.au!news.cs.su.oz.au!metro!mama.research.canon.oz.au!news
From: rwhitby@research.canon.oz.au (Rod Whitby)
Subject: vhdl-mode.el version 2.22 beta (Was: need emacs editor vhdl mode)
In-Reply-To: holp@isaac.raleigh.ibm.com's message of 14 Oct 1994 12:50:16 +1000
To: eholp@vnet.ibm.com
Message-ID: <RWHITBY.94Oct17082829@bing.research.canon.oz.au>
Lines: 2192
Sender: news@research.canon.oz.au
Nntp-Posting-Host: bing
Organization: Canon Information Systems Research Australia
References: <37krl8$1om0@sernews.raleigh.ibm.com>
Date: Sun, 16 Oct 1994 22:28:29 GMT