[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5. Customizing Indentation

The vhdl-offsets-alist variable is where you customize all your indentations. You simply need to decide what additional offset you want to add for every syntactic symbol. You can use the command C-c O (vhdl-set-offset) as the way to set offsets, both interactively and from your mode hook. Also, you can set up styles of indentation. Most likely, you'll find one of the pre-defined styles will suit your needs, but if not, this section will describe how to set up basic editing configurations. See 5.3 Styles for an explanation of how to set up named styles.

As mentioned previously, the variable vhdl-offsets-alist is an association list between syntactic symbols and the offsets to be applied for those symbols. In fact, these offset values can be an integer, a function or variable name, or one of the following symbols: +, -, ++, --, *, or /. The symbol values have the following meanings:

So, for example, because most of the default offsets are defined in terms of +, -, and 0, if you like the general indentation style, but you use 2 spaces instead of 4 spaces per level, you can probably achieve your style just by changing vhdl-basic-offset like so (in your `.emacs' file):

 
(setq vhdl-basic-offset 2)

To change indentation styles more radically, you will want to change the value associated with the syntactic symbols in the vhdl-offsets-alist variable. First, I'll show you how to do that interactively, then I'll describe how to make changes to your `.emacs' file so that your changes are more permanent.

5.1 Interactive Customization  
5.2 Permanent Indentation  
5.3 Styles  
5.4 Advanced Customizations  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.1 Interactive Customization

As an example of how to customize indentation, let's change the style of the example above from:

 
  1: inverter : process
  2: begin
  3:   q <= not d;
  4:   wait on d;
  5: end inverter;

to:
 
  1: inverter : process
  2: begin
  3:     q <= not d;
  4:     wait on d;
  5: end inverter;

In other words, we want to change the indentation of the statements inside the inverter process. Notice that the construct we want to change starts on line 3. To change the indentation of a line, we need to see which syntactic component affect the offset calculations for that line. Hitting C-c C-x on line 3 yields:

 
((statement-block-intro . 20))

So we know that to change the offset of the first signal assignment, we need to change the indentation for the statement-block-intro syntactic symbol. To do this interactively, just hit C-c O (vhdl-set-offset). This prompts you for the syntactic symbol to change, providing a reasonable default. In this case, the default is statement-block-intro, which is just the syntactic symbol we want to change!

After you hit return, VHDL Mode will then prompt you for the new offset value, with the old value as the default. The default in this case is `+', so hit backspace to delete the `+', then hit `++' and RET. This will associate an offset of twice the basic indent with the syntactic symbol statement-block-intro in the vhdl-offsets-alist variable.

To check your changes quickly, just enter M-x vhdl-indent-defun to reindent the entire function. The example should now look like:

 
  1: inverter : process
  2: begin
  3:     q <= not d;
  4:     wait on d;
  5: end inverter;

Notice how just changing the offset on line 3 is all we needed to do. Since the other affected lines are indented relative to line 3, they are automatically indented the way you'd expect. For more complicated examples, this may not always work. The general approach to take is to always start adjusting offsets for lines higher up in the file, then re-indent and see if any following lines need further adjustments.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.2 Permanent Indentation

To make this change permanent, you need to add some lisp code to your `.emacs' file. VHDL Mode provides a vhdl-mode-hook that you can use to customize your language editing styles. This hook gets run as the last thing when you enter VHDL Mode.

Here's a simplified example of what you can add to your `.emacs' file to make the changes described in the previous section (5.1 Interactive Customization) more permanent. See the Emacs manuals for more information on customizing Emacs via hooks. See 9. Sample `.emacs' file for a more complete sample `.emacs' file. (2)

 
(defun my-vhdl-mode-hook ()
  ;; my customizations for all of vhdl-mode
  (vhdl-set-offset 'statement-block-intro '++)
  ;; other customizations can go here
  )
(add-hook 'vhdl-mode-hook 'my-vhdl-mode-hook)

For complex customizations, you will probably want to set up a style that groups all your customizations under a single name. See 5.3 Styles for details.

The offset value can also be a function, and this is how power users gain enormous flexibility in customizing indentation. See 5.4 Advanced Customizations for details.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3 Styles

Most people only need to edit code formatted in just a few well-defined and consistent styles. For example, their organization might impose a "blessed" style that all its programmers must conform to. Similarly, people who work on GNU software will have to use the GNU coding style on C code. Some shops are more lenient, allowing some variety of coding styles, and as programmers come and go, there could be a number of styles in use. For this reason, VHDL Mode makes it convenient for you to set up logical groupings of customizations called styles, associate a single name for any particular style, and pretty easily start editing new or existing code using these styles. This chapter describes how to set up styles and how to edit your C code using styles.

5.3.1 Built-in Styles  
5.3.2 Adding Styles  
5.3.3 File Styles  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3.1 Built-in Styles

If you're lucky, one of VHDL Mode's built-in styles might be just what you're looking for. Some of the most common VHDL styles are already built-in. These include:

If you'd like to experiment with these built-in styles you can simply type the following in a VHDL Mode buffer:

 
M-x vhdl-set-style RET.

You will be prompted for one of the above styles (with completion). Enter one of the styles and hit RET. Note however that setting a style in this way does not automatically re-indent your file. For commands that you can use to view the effect of your changes, see 4. Indentation Commands.

Once you find a built-in style you like, you can make the change permanent by adding a call to your `.emacs' file. Let's say for example that you want to use the IEEE style in all your files. You would add this:

 
(defun my-vhdl-mode-hook ()
  ;; use IEEE style for all VHDL code
  (vhdl-set-style "IEEE")
  ;; other customizations can go here
  )
(add-hook 'vhdl-mode-hook 'my-vhdl-mode-hook)

See section 5.2 Permanent Indentation.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3.2 Adding Styles

If none of the built-in styles is appropriate, you'll probably want to add a new style definition. Styles are kept in the vhdl-style-alist variable, but you probably won't want to modify this variable directly. VHDL Mode provides a function, called vhdl-add-style, that you can use to easily add new styles or update existing styles. This function takes two arguments, a stylename string, and an association list description of style customizations. If stylename is not already in vhdl-style-alist, the new style is added, otherwise the style already associated with stylename is changed to the new description. This function also takes an optional third argument, which if non-nil, automatically institutes the new style in the current buffer.

The sample `.emacs' file provides a concrete example of how a new style can be added and automatically set. See section 9. Sample `.emacs' file.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.3.3 File Styles

The Emacs manual describes how you can customize certain variables on a per-file basis by including a Local Variable block at the end of the file. So far, you've only seen a functional interface to VHDL Mode, which is highly inconvenient for use in a Local Variable block. VHDL Mode provides two variables that make it easier for you to customize your style on a per-file basis.

The variable vhdl-file-style can be set to a style name string as described in 5.3.1 Built-in Styles. When the file is visited, VHDL Mode will automatically set the file's style to this style using vhdl-set-style.

Another variable, vhdl-file-offsets, takes an association list similar to what is allowed in vhdl-offsets-alist. When the file is visited, VHDL Mode will automatically institute these offets using vhdl-set-offset. See section 5. Customizing Indentation.

Note that file style settings (i.e. vhdl-file-style) are applied before file offset settings (i.e. vhdl-file-offsets).


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4 Advanced Customizations

For most users, VHDL Mode will support their coding styles with very little need for customizations. Usually, one of the standard styles defined in vhdl-style-alist will do the trick. Sometimes, one of the syntactic symbol offsets will need to be tweeked slightly, or perhaps vhdl-basic-offset will need to be changed. However, some styles require a more advanced ability for customization, and one of the real strengths of VHDL Mode is that the syntactic analysis model provides a very flexible framework for customizing indentation. This allows you to perform special indentation calculations for situations not handled by the mode directly.

5.4.1 Custom Indentation Functions  
5.4.2 Other Special Indentations  


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4.1 Custom Indentation Functions

One of the most common ways to customize VHDL Mode is by writing custom indentation functions and associating them with specific syntactic symbols (see 6. Syntactic Symbols). VHDL Mode itself uses custom indentation functions to provide more sophisticated indentation, for example when lining up selected signal assignments:

 
%%% TBD %%%

In this example, the statement-cont syntactic symbol has an offset of +, and vhdl-basic-offset is 2, so lines 4 through 6 are simply indented two spaces to the right of line 3. But perhaps we'd like VHDL Mode to be a little more intelligent so that it offsets the waveform descriptions relative to the signal assignment operator in line 3. To do this, we have to write a custom indentation function which finds the column of signal assignment operator on the first line of the statement. Here is the lisp code (from the `vhdl-mode.el' source file) that implements this:

 
(defun vhdl-lineup-statement-cont (langelem)
  ;; line up statement-cont after the assignment operator
  (save-excursion
    (let* ((relpos (cdr langelem))
	   (assignp (save-excursion
		     (goto-char (vhdl-point 'boi))
		     (and (re-search-forward "\\(<\\|:\\)="
					     (vhdl-point 'eol) t)
			  (- (point) (vhdl-point 'boi)))))
	   (curcol (progn
		     (goto-char relpos)
		     (current-column)))
	   foundp)
      (while (and (not foundp)
		  (< (point) (vhdl-point 'eol)))
	(re-search-forward "\\(<\\|:\\)=\\|(" (vhdl-point 'eol) 'move)
	(if (vhdl-in-literal (cdr langelem))
	    (forward-char)
	  (if (= (preceding-char) ?\()
	      ;; skip over any parenthesized expressions
	      (goto-char (min (vhdl-point 'eol)
			      (scan-lists (point) 1 1)))
	    ;; found an assignment operator (not at eol)
	    (setq foundp (not (looking-at "\\s-*$"))))))
      (if (not foundp)
	  ;; there's no assignment operator on the line
	  vhdl-basic-offset
	;; calculate indentation column after assign and ws, unless
	;; our line contains an assignment operator
	(if (not assignp)
	    (progn
	      (forward-char)
	      (skip-chars-forward " \t")
	      (setq assignp 0)))
	(- (current-column) assignp curcol))
      )))

Custom indent functions take a single argument, which is a syntactic component cons cell (see 3.1 Syntactic Analysis). The function returns an integer offset value that will be added to the running total indentation for the lne. Note that what actually gets returned is the difference between the column that the signal assignment operator is on, and the column of the buffer relative position passed in the function's argument. Remember that VHDL Mode automatically adds in the column of the component's relative buffer position and we don't want that value added into the final total twice.

Now, to associate the function vhdl-lineup-statement-cont with the statement-cont syntactic symbol, we can add something like the following to our vhdl-mode-hook:

 
(vhdl-set-offset 'statement-cont 'vhdl-lineup-statement-cont)

Now the function looks like this after re-indenting (using M-x vhdl-indent-defun):

 
%%% TBD %%%

Custom indentation functions can be as simple or as complex as you like, and any syntactic symbol that appears in vhdl-offsets-alist can have a custom indentation function associated with it. Note however that using many custom indentation functions may have a performance impact on VHDL Mode.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

5.4.2 Other Special Indentations

One other variable is available for you to customize VHDL Mode: vhdl-special-indent-hook. This is a standard hook variable that is called after every line is indented by VHDL Mode. You can use it to do any special indentation or line adjustments your style dictates, such as adding extra indentation to the port map clause in a component instantiation, etc. Note however, that you should not change point or mark inside your vhdl-special-indent-hook functions.


[ << ] [ >> ]           [Top] [Contents] [Index] [ ? ]

This document was generated by XEmacs shared group account on December, 19 2009 using texi2html 1.65.