SRecoder Manual
SRecode is the Semantic Recoder. Where Semantic will parse source files into lists of tags, the Semantic Recoder will aid in converting Semantic tags and various other information back into various types of code.
While the SRecode tool provides a template language, templates for several languages, and even a sequence of heuristics that aid the user in choosing a template to insert, this is not the main goal of SRecode.
The goal of SRecode is to provide an application framework where someone can write a complex code generator, and the underlying template commonality allows it to work in multiple languages with ease.
Copyright © 2007–2024 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, with the Front-Cover Texts being “A GNU Manual,” and with the Back-Cover Texts as in (a) below. A copy of the license is included in the section entitled “GNU Free Documentation License”.
(a) The FSF’s Back-Cover Text is: “You have the freedom to copy and modify this GNU manual.”
- Quick Start
- User Templates
- Parts of SRecode
- SRecode Minor Mode
- Template Writing
- Dictionaries
- Developing Template Functions
- Template Naming Conventions
- Inserting Tag Lists
- Application Writing
- GNU Free Documentation License
- Index
Next: User Templates, Previous: SRecoder Manual, Up: SRecoder Manual [Index]
1 Quick Start
When you install CEDET and enable SRecode, an SRecoder
menu
item should appear.
To toggle SRecode minor mode on and off use:
M-x srecode-minor-mode RET
or
M-x global-srecode-minor-mode RET
or add
(srecode-minor-mode 1)
into a language hook function to force it on (which is the default) or
pass in -1
to force it off.
See SRecode Minor Mode for more on using the minor mode.
Use the menu to insert templates into the current file.
You can add your own templates in ~/.srecode, or update the template map path:
- Option: srecode-map-load-path ¶
Global load path for SRecode template files.
Once installed, you can start inserting templates using the menu, or the command:
- Command: srecode-insert template-name &rest dict-entries ¶
Insert the template template-name into the current buffer at point. dict-entries are additional dictionary values to add.
SRecode Insert will prompt for a template name. Template names are
specific to each major mode. A typical name is of the form:
CONTEXT:NAME
where a CONTEXT might be something like
file
or declaration
. The same NAME can occur in
multiple contexts.
Next: Parts of SRecode, Previous: Quick Start, Up: SRecoder Manual [Index]
2 User Templates
SRecode builds and maintains a map of all template files. The root template files resides in the SRecode distribution. User written templates files are saved in ~/.srecode, along with the SRecode map file.
- Variable: srecode-map-save-file ¶
The save location for SRecode’s map file.
Template files end with a .srt extension. Details on how to write templates are in Template Writing.
Each template file you write is dedicated to a single major mode. In it, you can write templates within the same context and with the same name as core templates. You can force your templates to override the core templates for a particular major mode by setting the priority. See Special Variables.
To get going quickly, open a new .srt file. It will start in
the SRecode template writing mode. Use the SRecode minor mode
menu to insert the empty
file template.
When using templates in other modes (such as C++ or Emacs Lisp templates), use the “Edit Template” menu to find a template you would like to update. Copy it into your user template file, and change it.
If you were to update declaration:function
in your user
template file, then you would get this new template instead of the one
that comes with SRecode. Higher level applications should always
use declaration:function
when generating their own code, so
higher level templates will then adopt your changes to
declaration:function
into themselves.
You can also override variables. Core variables are stored in the
SRecode root template file default.srt, and that contains
the copyright usually used, and some basic file setup formats.
Override variables like this by specifying a mode
of
default
like this:
set mode "default"
Next: SRecode Minor Mode, Previous: User Templates, Up: SRecoder Manual [Index]
3 Parts of SRecode
The SRecode system is made up of several layers which work together to generate code.
- Template Layer
- Template Manager
- Dictionary
- Template Insertion
- Template Insertion Context
- Applications
- Field Editing
3.1 Template Layer
The template layer provides a way to write, and compile templates. The template layer is the scheme used to insert text into an Emacs buffer.
The SRecode template layer is more advanced than other modes like the
Emacs packages skeleton
or tempo
in that it allows
multiple layers of templates to be created with the same names. This
means that SRecode can provide a wide range of templates, and users
can override only the small sections they want, instead of either
accepting someone else’s template, or writing large new templates of
their own.
Templates are written in .srt files. You can learn how to author new .srt files Template Writing.
While the template system was designed for SRecode based applications it can also be used independently for simple template insertion during typical coding.
3.2 Template Manager
Once templates have been written, a scheme for loading and selecting templates is needed. The template manager has a loader for finding template files, and determining which templates are relevant to the current buffer. Template files are sorted by priority, with user templates being found first, and system level default templates last. Templates are also sorted by application. Each application has its own templates, and are kept separate from the generic templates.
3.3 Dictionary
Dictionaries contain values associated with variable. Variables are
used in macros in a template. Variables are what allows a generic
template such as a function to be made specific, such as a function
named foo. The value of a variable can be one of three things; a
string, a list of more dictionaries, or a special
srecode-dictionary-compound-value
object subclass. See
Variables for more.
3.4 Template Insertion
The template insertion layer involves extensions to the basic template layer. A wide range of custom variables are available for mixing derived data as macros into the plain text of a template.
In addition, templates can be declared with arguments. These arguments represent predetermined sets of dictionary values, such as features of the current file name, user name, time, etc.
Some arguments are major-mode specific, such as the :el
or
:cpp
arguments.
3.5 Template Insertion Context
A context can be provided for templates in a file. This helps
auto-selection of templates by name, or allows templates in different
contexts to have the same name. Some standard contexts are
file
, declaration
, and classdecl
.
A context can be automatically derived as well based on the parsing state from Semantic. See (semantic)Semantic Manual.
3.6 Applications
Commands that do a particular user task which involves also writing
Emacs Lisp code. Applications are at the top layer. These
applications have their own template files and logic needed to fill in
dictionaries or position a cursor. SRecode comes with an example
srecode-document
application for creating comments for Semantic
tags. The CEDET application EDE has a project type that is an
SRecode application.
3.7 Field Editing
If the variable srecode-insert-ask-variable-method
is set to
’field, then variables that would normally ask a question, will
instead create “fields” in the buffer. A field-editing layer
provides simple interaction through the fields. Typing in a field
will cause all variable locations that are the same to edit at the
same time. Pressing TAB on a field will move you to the
next field.
Next: Template Writing, Previous: Parts of SRecode, Up: SRecoder Manual [Index]
4 SRecode Minor Mode
The Semantic Recode minor mode enables a keymap and menu that provides simple access to different templates or template applications.
The key prefix is C-c /.
If the variable srecode-takeover-INS-key
is set, then the key
INSERT can also be used.
The most important key is bound to srecode-insert
which is
C-c / /, or INSERT INSERT. Quick Start.
Major key bindings are:
- C-c / /
Insert a template whose name is typed into the minibuffer.
- C-c / <lower case letter>
Reserved for direct binding of simple templates to keys using a key binding command in the template file.
- C-c / <upper case letter>
Reserved for template applications (Such as comment or get/set inserter.)
- C-c / E
Edit the code of a template.
- C-c / .
Insert template again. This will cause the previously inserted template to be inserted again.
4.1 Field Editing
By default, when inserting a template, if the user needs to enter text to fill in a part of the template, then the minibuffer is used to query for that information. SRecode also supports a field-editing mode that can be used instead. To enable it set:
- Function: srecode-insert-ask-variable-method ¶
Determine how to ask for a dictionary value when inserting a template. Only the ASK style inserter will query the user for a value. Dictionary value references that ask begin with the ? character. Possible values are:
ask
Prompt in the minibuffer as the value is inserted.
field
Use the dictionary macro name as the inserted value, and place a field there. Matched fields change together.
Field editing mode is supported in newer versions of Emacs. You will not be prompted to fill in values while the template is inserted. Instead, short regions will be highlighted, and the cursor placed in a field. Typing in the field will then fill in the value. Several fields might be linked together. In that case, typing in one area will modify the other linked areas. Pressing TAB will move between editable fields in the template.
Once the cursor moves out of the are inserted by the template, all the fields are canceled.
NOTE: Some conveniences in templates, such as completion, or character restrictions are lost when using field editing mode.
Next: Dictionaries, Previous: SRecode Minor Mode, Up: SRecoder Manual [Index]
5 Template Writing
srecode-template-mode
is the major mode used for designing new
templates. SRecode files (Extension .srt) are made up of
variable settings and template declarations.
Here is an overview of the terminology you will need for the next few sections:
- template file
A file with a .srt extension which contains settings, variables, and templates.
- template
One named entity which represents a block of text that will be inserted. The text is compiled into a sequence of insertable entities. The entities are string constants, and macros.
- macro
A macro is a text sequence within template text that is replaced with some other value.
- dictionary
A table of variable names and values.
- subdictionary
A dictionary that is subordinate under another dictionary as a value to some variable.
- variable
A variable is an entry in a dictionary which has a value.
Next: Templates, Up: Template Writing [Index]
5.1 Variables
Variables can be set up and used in templates. Many variables you may use are set up via template arguments, but some may be preferences a user can set up that are used in system templates.
When expanding a template, variables are stored in a dictionary. Dictionary entries are variables. Variables defined in templates can have string like values.
A variable can be set like this:
set VARNAME "some value"
Note that a VARIABLE is a name in a dictionary that can be used in a MACRO in a template. The macro references some variable by name.
- String Values
- Multi-string Values
- Section Show
- Special Variables
- Automatic Loop Variables
- Compound Variable Values
Next: Multi-string Values, Up: Variables [Index]
5.1.1 String Values
Variables can be set to strings. Strings may contain newlines or any
other characters. Strings are interpreted by the Emacs Lisp reader so
\n
, \t
, and \"
work.
When a string is inserted as part of a template, nothing within the string is interpreted, such as template escape characters.
Next: Section Show, Previous: String Values, Up: Variables [Index]
5.1.2 Multi-string Values
A variable can be set to multiple strings. A compound value is usually used when you want to use dictionary entries as part of a variable later on.
Multi-string variable values are set like string values except there are more than one. For example
set NAME "this" "-mode"
These two strings will be concatenated together.
A more useful thing is to include dictionary variables and concatenate those into the string. Use the “macro” keyword to include the name of a variable. This is like macros in a template. For example:
set NAME macro "MODE" "-mode"
will extract the value of the dictionary variable MODE and append “-mode” to the end.
Next: Special Variables, Previous: Multi-string Values, Up: Variables [Index]
5.1.3 Section Show
To set a variable to show a template section, use the show
command. Sections are blocks of a template wrapped in section macros.
If there is a section macro using NAME it will be shown for each
dictionary associated with the NAME macro.
show NAME
This will enable that section.
NOTE: May 11, 2008: I haven’t used this yet, so I don’t know if it works.
Next: Automatic Loop Variables, Previous: Section Show, Up: Variables [Index]
5.1.4 Special Variables
Some variables have special meaning that changes attributes when templates are compiled, including:
escape-start
This is the character sequence that escapes from raw text to template macro names. The ability to change the escape characters are key for enabling SRecode templates to work across many kinds of languages.
escape-end
This is the character sequence that escapes the end of a template macro name.
Example:
set escape_start "$" set escape_end "$"
mode
This is the major mode, as a string with the full Emacs Lisp symbol in it. All templates in this file will be installed into the template table for this major mode.
Multiple template files can use the same mode, and all those templates will be available in buffers of that mode.
Example:
set mode "emacs-lisp-mode"
priority
The priority of a file is a number in a string constant that indicates where it lies in the template search order. System templates default to low priority numbers. User templates default to high priority numbers. You can specify the priority of your template to insert it anywhere in the template search list.
If there are multiple templates with the same context and name, the template with the highest priority number will be used.
If multiple files have the same priority, then the sort order is unpredictable. If no template names match, then it doesn’t matter.
Example:
set priority "35"
application
If a template file contains templates only needed for a particular application, then specify an application. Template files for an application are stored in the template repository, but are not used in the generic template insertion case.
The application with a particular name will access these templates from Lisp code.
Example:
set application "document"
project
If a template file contains templates, or template overrides specific to a set of files under a particular directory, then that template file can specify a “project” that it belongs to.
Set the “project” special variable to a directory name. Only files under that directory will be able to access the templates in that file.
Any template file that has a project specified will get have a priority that is set between SRecode base templates, and user defined templates.
Templates can be compiled via a project system, such as EDE. EDE loaded templates will get a project set automatically.
Example:
set project "/tmp/testproject"
If you need to insert the characters that belong to the variables
escape_start
or escape_end
, then place those into
a variable. For example
set escape_start "$" set escape_end "$" set DOLLAR "$"
Next: Compound Variable Values, Previous: Special Variables, Up: Variables [Index]
5.1.5 Automatic Loop Variables
When section macros are used, that section is repeated for each subdictionary bound to the loop variable.
Each dictionary added will automatically get values for positional macros which will enable different sections. The automatic section variables are.
Jump to: | S |
---|
Jump to: | S |
---|