xref: /Universal-ctags/man/ctags-client-tools.7.rst.in (revision 554afab1b2953ee940a4d0b0af34e29afc15e743)
100e59525SMasatake YAMATO.. _ctags-client-tools(7):
200e59525SMasatake YAMATO
300e59525SMasatake YAMATO==============================================================
4b28d5302SMasatake YAMATOctags-client-tools
500e59525SMasatake YAMATO==============================================================
6*554afab1SHiroo HAYASHI---------------------------------------------------------------------------------
700e59525SMasatake YAMATOHints for developing a tool using @CTAGS_NAME_EXECUTABLE@ command and tags output
8*554afab1SHiroo HAYASHI---------------------------------------------------------------------------------
900e59525SMasatake YAMATO:Version: @VERSION@
10dccba5efSHiroo HAYASHI:Manual group: Universal Ctags
1100e59525SMasatake YAMATO:Manual section: 7
1200e59525SMasatake YAMATO
1300e59525SMasatake YAMATOSYNOPSIS
1400e59525SMasatake YAMATO--------
1500e59525SMasatake YAMATO|	**@CTAGS_NAME_EXECUTABLE@** [options] [file(s)]
1600e59525SMasatake YAMATO|	**@ETAGS_NAME_EXECUTABLE@** [options] [file(s)]
1700e59525SMasatake YAMATO
1800e59525SMasatake YAMATO
1900e59525SMasatake YAMATODESCRIPTION
2000e59525SMasatake YAMATO-----------
219cee5f42SMasatake YAMATO**Client tool** means a tool running the @CTAGS_NAME_EXECUTABLE@ command
229cee5f42SMasatake YAMATOand/or reading a tags file generated by @CTAGS_NAME_EXECUTABLE@ command.
2300e59525SMasatake YAMATOThis man page gathers hints for people who develop client tools.
2400e59525SMasatake YAMATO
2500e59525SMasatake YAMATO
2600e59525SMasatake YAMATOPSEUDO-TAGS
2700e59525SMasatake YAMATO-----------
2800e59525SMasatake YAMATO**Pseudo-tags**, stored in a tag file, indicate how
295842bcc6SMasatake YAMATO@CTAGS_NAME_EXECUTABLE@ generated the tags file: whether the
3000e59525SMasatake YAMATOtags file is sorted or not, which version of tags file format is used,
3100e59525SMasatake YAMATOthe name of tags generator, and so on. The opposite term for
3200e59525SMasatake YAMATOpseudo-tags is **regular-tags**. A regular-tag is for a language
335842bcc6SMasatake YAMATOobject in an input file. A pseudo-tag is for the tags file
3400e59525SMasatake YAMATOitself. Client tools may use pseudo-tags as reference for processing
3500e59525SMasatake YAMATOregular-tags.
3600e59525SMasatake YAMATO
3700e59525SMasatake YAMATOA pseudo-tag is stored in a tags file in the same format as
385842bcc6SMasatake YAMATOregular-tags as described in tags(5), except that pseudo-tag names
395842bcc6SMasatake YAMATOare prefixed with "!_". For the general information about
4000e59525SMasatake YAMATOpseudo-tags, see "TAG FILE INFORMATION" in tags(5).
4100e59525SMasatake YAMATO
425842bcc6SMasatake YAMATOAn example of a pseudo tag::
4300e59525SMasatake YAMATO
4400e59525SMasatake YAMATO	!_TAG_PROGRAM_NAME	Universal Ctags	/Derived from Exuberant Ctags/
4500e59525SMasatake YAMATO
4600e59525SMasatake YAMATOThe value, "2", associated with the pseudo tag "TAG_PROGRAM_NAME", is
479cee5f42SMasatake YAMATOused in the field for input file. The description, "Derived from
489cee5f42SMasatake YAMATOExuberant Ctags", is used in the field for pattern.
4900e59525SMasatake YAMATO
50dccba5efSHiroo HAYASHIUniversal Ctags extends the naming scheme of the classical pseudo-tags
51bb84f88aSHiroo HAYASHIavailable in Exuberant Ctags for emitting language specific
5200e59525SMasatake YAMATOinformation as pseudo tags::
5300e59525SMasatake YAMATO
5400e59525SMasatake YAMATO	!_{pseudo-tag-name}!{language-name}	{associated-value}	/{description}/
5500e59525SMasatake YAMATO
5600e59525SMasatake YAMATOThe language-name is appended to the pseudo-tag name with a separator, "!".
5700e59525SMasatake YAMATO
5800e59525SMasatake YAMATOAn example of pseudo tag with a language suffix::
5900e59525SMasatake YAMATO
6000e59525SMasatake YAMATO	!_TAG_KIND_DESCRIPTION!C	f,function	/function definitions/
6100e59525SMasatake YAMATO
625842bcc6SMasatake YAMATOThis pseudo-tag says "the function kind of C language is enabled
6300e59525SMasatake YAMATOwhen generating this tags file." ``--pseudo-tags`` is the option for
6400e59525SMasatake YAMATOenabling/disabling individual pseudo-tags. When enabling/disabling a
655842bcc6SMasatake YAMATOpseudo tag with the option, specify the tag name only
665842bcc6SMasatake YAMATO"TAG_KIND_DESCRIPTION", without the prefix ("!_") or the suffix ("!C").
6700e59525SMasatake YAMATO
6800e59525SMasatake YAMATO
6900e59525SMasatake YAMATOOptions for Pseudo-tags
7000e59525SMasatake YAMATO~~~~~~~~~~~~~~~~~~~~~~~
7100e59525SMasatake YAMATO``--extras=+p`` (or ``--extras=+{pseudo}``)
7200e59525SMasatake YAMATO	Forces writing pseudo-tags.
7300e59525SMasatake YAMATO
7400e59525SMasatake YAMATO	@CTAGS_NAME_EXECUTABLE@ emits pseudo-tags by default when writing tags
755842bcc6SMasatake YAMATO	to a regular file (e.g. "tags'.) However, when specifying ``-o -``
7600e59525SMasatake YAMATO	or ``-f -`` for writing tags to standard output,
779cee5f42SMasatake YAMATO	@CTAGS_NAME_EXECUTABLE@ doesn't emit pseudo-tags. ``--extras=+p`` or
785842bcc6SMasatake YAMATO	``--extras=+{pseudo}`` will force pseudo-tags to be written.
7900e59525SMasatake YAMATO
8000e59525SMasatake YAMATO``--list-pseudo-tags``
8112d83e6dSMasatake YAMATO	Lists available types of pseudo-tags and shows whether they are enabled or disabled.
8200e59525SMasatake YAMATO
8300e59525SMasatake YAMATO	Running @CTAGS_NAME_EXECUTABLE@ with ``--list-pseudo-tags`` option
8400e59525SMasatake YAMATO	lists available pseudo-tags. Some of pseudo-tags newly introduced
85dccba5efSHiroo HAYASHI	in Universal Ctags project are disabled by default. Use
865842bcc6SMasatake YAMATO	``--pseudo-tags=...`` to enable them.
8700e59525SMasatake YAMATO
8800e59525SMasatake YAMATO``--pseudo-tags=[+|-]names|*``
8912d83e6dSMasatake YAMATO	Specifies a list of pseudo-tag types to include in the output.
9000e59525SMasatake YAMATO
919cee5f42SMasatake YAMATO	The parameters are a set of pseudo tag names. Valid pseudo tag names
929cee5f42SMasatake YAMATO	can be listed with ``--list-pseudo-tags``. Surround each name in the set
939cee5f42SMasatake YAMATO	with braces, like "{TAG_PROGRAM_AUTHOR}". You don't have to include the "!_"
949cee5f42SMasatake YAMATO	pseudo tag prefix when specifying a name in the option argument for ``--pseudo-tags=``
9500e59525SMasatake YAMATO	option.
9600e59525SMasatake YAMATO
9700e59525SMasatake YAMATO	pseudo-tags don't have a notation using one-letter flags.
9800e59525SMasatake YAMATO
995842bcc6SMasatake YAMATO	If a name is preceded by either the '+' or '-' characters, that
1005842bcc6SMasatake YAMATO	tags's effect has been added or removed. Otherwise the names replace
1015842bcc6SMasatake YAMATO	any current settings. All entries are included if '*' is given.
10200e59525SMasatake YAMATO
10300e59525SMasatake YAMATO``--fields=+E`` (or ``--fields=+{extras}``)
10400e59525SMasatake YAMATO	Attach "extras:pseudo" field to pseudo-tags.
10500e59525SMasatake YAMATO
10600e59525SMasatake YAMATO	An example of pseudo tags with the field::
10700e59525SMasatake YAMATO
10800e59525SMasatake YAMATO		!_TAG_PROGRAM_NAME	Universal Ctags	/Derived from Exuberant Ctags/	extras:pseudo
10900e59525SMasatake YAMATO
11000e59525SMasatake YAMATO	If the name of a normal tag in a tag file starts with "!_", a
11100e59525SMasatake YAMATO	client tool cannot distinguish whether the tag is a regular-tag or
11200e59525SMasatake YAMATO	pseudo-tag.  The fields attached with this option help the tool
11300e59525SMasatake YAMATO	distinguish them.
11400e59525SMasatake YAMATO
11500e59525SMasatake YAMATO
11600e59525SMasatake YAMATOList of notable pseudo-tags
11700e59525SMasatake YAMATO~~~~~~~~~~~~~~~~~~~~~~~~~~~
11812d83e6dSMasatake YAMATORunning ctags with ``--list-pseudo-tags`` option lists available types
11900e59525SMasatake YAMATOof pseudo-tags with short descriptions. This subsection shows hints
12000e59525SMasatake YAMATOfor using notable ones.
12100e59525SMasatake YAMATO
122dccba5efSHiroo HAYASHI``TAG_EXTRA_DESCRIPTION``  (new in Universal Ctags)
12300e59525SMasatake YAMATO	Indicates the names and descriptions of enabled extras::
12400e59525SMasatake YAMATO
12500e59525SMasatake YAMATO	  !_TAG_EXTRA_DESCRIPTION	{extra-name}	/description/
12600e59525SMasatake YAMATO	  !_TAG_EXTRA_DESCRIPTION!{language-name}	{extra-name}	/description/
12700e59525SMasatake YAMATO
12812d83e6dSMasatake YAMATO	If your tool relies on some extra tags (extras), refer to
12912d83e6dSMasatake YAMATO	the pseudo-tags of this type. A tool can reject the tags file that
13000e59525SMasatake YAMATO	doesn't include expected extras, and raise an error in an early
13100e59525SMasatake YAMATO	stage of processing.
13200e59525SMasatake YAMATO
13300e59525SMasatake YAMATO	An example of the pseudo-tags::
13400e59525SMasatake YAMATO
13500e59525SMasatake YAMATO	  $ @CTAGS_NAME_EXECUTABLE@ --extras=+p --pseudo-tags='{TAG_EXTRA_DESCRIPTION}' -o - input.c
13600e59525SMasatake YAMATO	  !_TAG_EXTRA_DESCRIPTION	anonymous	/Include tags for non-named objects like lambda/
13700e59525SMasatake YAMATO	  !_TAG_EXTRA_DESCRIPTION	fileScope	/Include tags of file scope/
13800e59525SMasatake YAMATO	  !_TAG_EXTRA_DESCRIPTION	pseudo	/Include pseudo tags/
13900e59525SMasatake YAMATO	  !_TAG_EXTRA_DESCRIPTION	subparser	/Include tags generated by subparsers/
14000e59525SMasatake YAMATO	  ...
14100e59525SMasatake YAMATO
14200e59525SMasatake YAMATO	A client tool can know "{anonymous}", "{fileScope}", "{pseudo}",
1439cee5f42SMasatake YAMATO	and "{subparser}" extras are enabled from the output.
14400e59525SMasatake YAMATO
145dccba5efSHiroo HAYASHI``TAG_FIELD_DESCRIPTION``  (new in Universal Ctags)
14600e59525SMasatake YAMATO	Indicates the names and descriptions of enabled fields::
14700e59525SMasatake YAMATO
14800e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION	{field-name}	/description/
14900e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION!{language-name}	{field-name}	/description/
15000e59525SMasatake YAMATO
15100e59525SMasatake YAMATO	If your tool relies on some fields, refer to the pseudo-tags of
15212d83e6dSMasatake YAMATO	this type.  A tool can reject a tags file that doesn't include
15300e59525SMasatake YAMATO	expected fields, and raise an error in an early stage of
15400e59525SMasatake YAMATO	processing.
15500e59525SMasatake YAMATO
15600e59525SMasatake YAMATO	An example of the pseudo-tags::
15700e59525SMasatake YAMATO
15800e59525SMasatake YAMATO	  $ @CTAGS_NAME_EXECUTABLE@ --fields-C=+'{macrodef}' --extras=+p --pseudo-tags='{TAG_FIELD_DESCRIPTION}' -o - input.c
15900e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION	file	/File-restricted scoping/
16000e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION	input	/input file/
16100e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION	name	/tag name/
16200e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION	pattern	/pattern/
16300e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION	typeref	/Type and name of a variable or typedef/
16400e59525SMasatake YAMATO	  !_TAG_FIELD_DESCRIPTION!C	macrodef	/macro definition/
16500e59525SMasatake YAMATO	  ...
16600e59525SMasatake YAMATO
16700e59525SMasatake YAMATO	A client tool can know "{file}", "{input}", "{name}", "{pattern}",
16800e59525SMasatake YAMATO	and "{typeref}" fields are enabled from the output.
16900e59525SMasatake YAMATO	The fields are common in languages. In addition to the common fields,
17000e59525SMasatake YAMATO	the tool can known "{macrodef}" field of C language is also enabled.
17100e59525SMasatake YAMATO
172dccba5efSHiroo HAYASHI``TAG_FILE_ENCODING``  (new in Universal Ctags)
17300e59525SMasatake YAMATO	TBW
17400e59525SMasatake YAMATO
17500e59525SMasatake YAMATO``TAG_FILE_FORMAT``
17600e59525SMasatake YAMATO	See also tags(5).
17700e59525SMasatake YAMATO
17800e59525SMasatake YAMATO``TAG_FILE_SORTED``
17900e59525SMasatake YAMATO	See also tags(5).
18000e59525SMasatake YAMATO
181dccba5efSHiroo HAYASHI``TAG_KIND_DESCRIPTION`` (new in Universal Ctags)
18200e59525SMasatake YAMATO	Indicates the names and descriptions of enabled kinds::
18300e59525SMasatake YAMATO
18400e59525SMasatake YAMATO	  !_TAG_KIND_DESCRIPTION!{language-name}	{kind-letter},{kind-name}	/description/
18500e59525SMasatake YAMATO
18600e59525SMasatake YAMATO	If your tool relies on some kinds, refer to the pseudo-tags of
18712d83e6dSMasatake YAMATO	this type.  A tool can reject the tags file that doesn't include
18800e59525SMasatake YAMATO	expected kinds, and raise an error in an early stage of
18900e59525SMasatake YAMATO	processing.
19000e59525SMasatake YAMATO
19100e59525SMasatake YAMATO	Kinds are language specific, so a language name is  always
19200e59525SMasatake YAMATO	appended to the tag name as suffix.
19300e59525SMasatake YAMATO
19400e59525SMasatake YAMATO	An example of the pseudo-tags::
19500e59525SMasatake YAMATO
19600e59525SMasatake YAMATO	  $ @CTAGS_NAME_EXECUTABLE@ --extras=+p --kinds-C=vfm --pseudo-tags='{TAG_KIND_DESCRIPTION}' -o - input.c
19700e59525SMasatake YAMATO	  !_TAG_KIND_DESCRIPTION!C	f,function	/function definitions/
19800e59525SMasatake YAMATO	  !_TAG_KIND_DESCRIPTION!C	m,member	/struct, and union members/
19900e59525SMasatake YAMATO	  !_TAG_KIND_DESCRIPTION!C	v,variable	/variable definitions/
20000e59525SMasatake YAMATO	  ...
20100e59525SMasatake YAMATO
20200e59525SMasatake YAMATO	A client tool can know "{function}", "{member}", and "{variable}"
20300e59525SMasatake YAMATO	kinds of C language are enabled from the output.
20400e59525SMasatake YAMATO
205dccba5efSHiroo HAYASHI``TAG_KIND_SEPARATOR`` (new in Universal Ctags)
20600e59525SMasatake YAMATO	TBW
20700e59525SMasatake YAMATO
208dccba5efSHiroo HAYASHI``TAG_OUTPUT_EXCMD`` (new in Universal Ctags)
2095d8cabe6SMasatake YAMATO	Indicates the specified type of EX command with ``--excmd`` option.
2105d8cabe6SMasatake YAMATO
211dccba5efSHiroo HAYASHI``TAG_OUTPUT_FILESEP`` (new in Universal Ctags)
21200e59525SMasatake YAMATO	TBW
21300e59525SMasatake YAMATO
214dccba5efSHiroo HAYASHI``TAG_OUTPUT_MODE`` (new in Universal Ctags)
21500e59525SMasatake YAMATO	TBW
21600e59525SMasatake YAMATO
217dccba5efSHiroo HAYASHI``TAG_PATTERN_LENGTH_LIMIT`` (new in Universal Ctags)
21800e59525SMasatake YAMATO	TBW
21900e59525SMasatake YAMATO
220dccba5efSHiroo HAYASHI``TAG_PROC_CWD`` (new in Universal Ctags)
2219cee5f42SMasatake YAMATO	Indicates the working directory of @CTAGS_NAME_EXECUTABLE@ during processing.
22200e59525SMasatake YAMATO
22300e59525SMasatake YAMATO	This pseudo-tag helps a client tool solve the absolute paths for
22400e59525SMasatake YAMATO	the input files for tag entries even when they are tagged with
22500e59525SMasatake YAMATO	relative paths.
22600e59525SMasatake YAMATO
22700e59525SMasatake YAMATO	An example of the pseudo-tags::
22800e59525SMasatake YAMATO
22900e59525SMasatake YAMATO	  $ cat tags
23000e59525SMasatake YAMATO	  !_TAG_PROC_CWD	/tmp/	//
23100e59525SMasatake YAMATO	  main	input.c	/^int main (void) { return 0; }$/;"	f	typeref:typename:int
23200e59525SMasatake YAMATO	  ...
23300e59525SMasatake YAMATO
23400e59525SMasatake YAMATO	From the regular tag for "main", the client tool can know the
23500e59525SMasatake YAMATO	"main" is at "input.c".  However, it is a relative path. So if the
23600e59525SMasatake YAMATO	directory where @CTAGS_NAME_EXECUTABLE@ run and the directory
23700e59525SMasatake YAMATO	where the client tool runs are different, the client tool cannot
23800e59525SMasatake YAMATO	find "input.c" from the file system. In that case,
23900e59525SMasatake YAMATO	``TAG_PROC_CWD`` gives the tool a hint; "input.c" may be at "/tmp".
24000e59525SMasatake YAMATO
24100e59525SMasatake YAMATO``TAG_PROGRAM_NAME``
24200e59525SMasatake YAMATO	TBW
24300e59525SMasatake YAMATO
244dccba5efSHiroo HAYASHI``TAG_ROLE_DESCRIPTION`` (new in Universal Ctags)
245cea85fadSMasatake YAMATO	Indicates the names and descriptions of enabled roles::
246cea85fadSMasatake YAMATO
247cea85fadSMasatake YAMATO	  !_TAG_ROLE_DESCRIPTION!{language-name}!{kind-name}	{role-name}	/description/
248cea85fadSMasatake YAMATO
249cea85fadSMasatake YAMATO	If your tool relies on some roles, refer to the pseudo-tags of
250cea85fadSMasatake YAMATO	this type. Note that a role owned by a disabled kind is not listed
251cea85fadSMasatake YAMATO	even if the role itself is enabled.
252cea85fadSMasatake YAMATO
25300e59525SMasatake YAMATOREDUNDANT-KINDS
25400e59525SMasatake YAMATO---------------
25500e59525SMasatake YAMATOTBW
25600e59525SMasatake YAMATO
257520b3aa2SHiroo HAYASHIMULTIPLE-LANGUAGES FOR AN INPUT FILE
258520b3aa2SHiroo HAYASHI------------------------------------
259520b3aa2SHiroo HAYASHIUniversal ctags can run multiple parsers.
260520b3aa2SHiroo HAYASHIThat means a parser, which supports multiple parsers, may output tags for
261520b3aa2SHiroo HAYASHIdifferent languages.  ``language``/``l`` field can be used to show the language
262520b3aa2SHiroo HAYASHIfor each tag.
26300e59525SMasatake YAMATO
264520b3aa2SHiroo HAYASHI.. code-block:: console
26500e59525SMasatake YAMATO
266520b3aa2SHiroo HAYASHI	$ cat /tmp/foo.html
267520b3aa2SHiroo HAYASHI	<html>
268520b3aa2SHiroo HAYASHI	<script>var x = 1</script>
269520b3aa2SHiroo HAYASHI	<h1>title</h1>
270520b3aa2SHiroo HAYASHI	</html>
271520b3aa2SHiroo HAYASHI	$ ./ctags -o - --extras=+g /tmp/foo.html
272520b3aa2SHiroo HAYASHI	title	/tmp/foo.html	/^  <h1>title<\/h1>$/;"	h
273520b3aa2SHiroo HAYASHI	x	/tmp/foo.html	/var x = 1/;"	v
274520b3aa2SHiroo HAYASHI	$ ./ctags -o - --extras=+g --fields=+l /tmp/foo.html
275520b3aa2SHiroo HAYASHI	title	/tmp/foo.html	/^  <h1>title<\/h1>$/;"	h	language:HTML
276520b3aa2SHiroo HAYASHI	x	/tmp/foo.html	/var x = 1/;"	v	language:JavaScript
27700e59525SMasatake YAMATO
27800e59525SMasatake YAMATOUTILIZING READTAGS
27900e59525SMasatake YAMATO-----------------------------------
2807221c13bSAmaiKinonoSee readtags(1) to know how to use readtags. This section is for discussing
2817221c13bSAmaiKinonosome notable topics for client tools.
28200e59525SMasatake YAMATO
2837221c13bSAmaiKinonoBuild Filter/Sorter Expressions
2847221c13bSAmaiKinono~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2857221c13bSAmaiKinonoCertain escape sequences in expressions are recognized by readtags. For
2867221c13bSAmaiKinonoexample, when searching for a tag that matches ``a\?b``, if using a filter
2877221c13bSAmaiKinonoexpression like ``'(eq? $name "a\?b")'``, since ``\?`` is translated into a
2887221c13bSAmaiKinonosingle ``?`` by readtags, it actually searches for ``a?b``.
2897221c13bSAmaiKinono
2907221c13bSAmaiKinonoAnother problem is if a single quote appear in filter expressions (which is
2917221c13bSAmaiKinonoalso wrapped by single quotes), it terminates the expression, producing broken
2927221c13bSAmaiKinonoexpressions, and may even cause unintended shell injection. Single quotes can
2937221c13bSAmaiKinonobe escaped using ``'"'"'``.
2947221c13bSAmaiKinono
2957221c13bSAmaiKinonoSo, client tools need to:
2967221c13bSAmaiKinono
2977221c13bSAmaiKinono* Replace ``\`` by ``\\``
2987221c13bSAmaiKinono* Replace ``'`` by ``'"'"'``
2997221c13bSAmaiKinono
3007221c13bSAmaiKinonoinside the expressions. If the expression also contains strings, ``"`` in the
3017221c13bSAmaiKinonostrings needs to be replaced by ``\"``.
3027221c13bSAmaiKinono
303c71475d3SMasanari IidaClient tools written in Lisp could build the expression using lists. ``prin1``
3047221c13bSAmaiKinono(in Common Lisp style Lisps) and ``write`` (in Scheme style Lisps) can
3057221c13bSAmaiKinonotranslate the list into a string that can be directly used. For example, in
3067221c13bSAmaiKinonoEmacsLisp:
3077221c13bSAmaiKinono
3087221c13bSAmaiKinono.. code-block:: EmacsLisp
3097221c13bSAmaiKinono
3107221c13bSAmaiKinono   (let ((name "hi"))
3117221c13bSAmaiKinono     (prin1 `(eq? $name ,name)))
3127221c13bSAmaiKinono   => "(eq\\? $name "hi")"
3137221c13bSAmaiKinono
3147221c13bSAmaiKinonoThe "?" is escaped, and readtags can handle it. Scheme style Lisps should do
3157221c13bSAmaiKinonoproper escaping so the expression readtags gets is just the expression passed
3167221c13bSAmaiKinonointo ``write``. Common Lisp style Lisps may produce unrecognized escape
3177221c13bSAmaiKinonosequences by readtags, like ``\#``. Readtags provides some aliases for these
3187221c13bSAmaiKinonoLisps:
3197221c13bSAmaiKinono
3207221c13bSAmaiKinono* Use ``true`` for ``#t``.
3217221c13bSAmaiKinono* Use ``false`` for ``#f``.
3227221c13bSAmaiKinono* Use ``nil`` or ``()`` for ``()``.
323aeff9ba3SAmaiKinono* Use ``(string->regexp "PATTERN")`` for ``#/PATTERN/``. Use
324aeff9ba3SAmaiKinono  ``(string->regexp "PATTERN" :case-fold true)`` for ``#/PATTERN/i``. Notice
325aeff9ba3SAmaiKinono  that ``string->regexp`` doesn't require escaping "/" in the pattern.
3267221c13bSAmaiKinono
3277221c13bSAmaiKinonoNotice that even when the client tool uses this method, ``'`` still needs to be
3287221c13bSAmaiKinonoreplaced by ``'"'"'`` to prevent broken expressions and shell injection.
3297221c13bSAmaiKinono
33078621a0aSAmaiKinonoAnother thing to notice is that missing fields are represented by ``#f``, and
33178621a0aSAmaiKinonoapplying string operators to them will produce an error. You should always
33278621a0aSAmaiKinonocheck if a field is missing before applying string operators. See the
33378621a0aSAmaiKinono"Filtering" section in readtags(1) to know how to do this. Run "readtags -H
33478621a0aSAmaiKinonofilter" to see which operators take string arguments.
33578621a0aSAmaiKinono
3367221c13bSAmaiKinonoParse Readtags Output
3377221c13bSAmaiKinono~~~~~~~~~~~~~~~~~~~~~
3387221c13bSAmaiKinonoIn the output of readtags, tabs can appear in all field values (e.g., the tag
339c71475d3SMasanari Iidaname itself could contain tabs), which makes it hard to split the line into
3407221c13bSAmaiKinonofields. Client tools should use the ``-E`` option, which keeps the escape
3417221c13bSAmaiKinonosequences in the tags file, so the only field that could contain tabs is the
3427221c13bSAmaiKinonopattern field.
3437221c13bSAmaiKinono
3449018ae98SAmaiKinonoThe pattern field could:
3459018ae98SAmaiKinono
3469018ae98SAmaiKinono- Use a line number. It will look like ``number;"`` (e.g. ``10;"``).
347792fa325SAmaiKinono- Use a search pattern. It will look like ``/pattern/;"`` or ``?pattern?;"``.
348792fa325SAmaiKinono  Notice that the search pattern could contain tabs.
3499018ae98SAmaiKinono- Combine these two, like ``number;/pattern/;"`` or ``number;?pattern?;"``.
3509018ae98SAmaiKinono
351792fa325SAmaiKinonoThese are true for tags files using extended format, which is the default one.
352792fa325SAmaiKinonoThe legacy format (i.e. ``--format=1``) doesn't include the semicolons. It's
353792fa325SAmaiKinonoold and barely used, so we won't discuss it here.
354792fa325SAmaiKinono
3559018ae98SAmaiKinonoClient tools could split the line using the following steps:
3567221c13bSAmaiKinono
3577221c13bSAmaiKinono* Find the first 2 tabs in the line, so we get the name and input field.
3589018ae98SAmaiKinono* From the 2nd tab:
3597221c13bSAmaiKinono
3609018ae98SAmaiKinono  * If a ``/`` follows, then the pattern delimiter is ``/``.
3619018ae98SAmaiKinono  * If a ``?`` follows, then the pattern delimiter is ``?``.
3629018ae98SAmaiKinono  * If a number follows, then:
36349bbb286SMasatake YAMATO
3649018ae98SAmaiKinono    * If a ``;/`` follows the number, then the delimiter is ``/``.
3659018ae98SAmaiKinono    * If a ``;?`` follows the number, then the delimiter is ``?``.
3669018ae98SAmaiKinono    * If a ``;"`` follows the number, then the field uses only line number, and
3679018ae98SAmaiKinono      there's no pattern delimiter (since there's no regex pattern). In this
3689018ae98SAmaiKinono      case the pattern field ends at the 3rd tab.
3697221c13bSAmaiKinono
37078621a0aSAmaiKinono* After the opening delimiter, find the next unescaped pattern delimiter, and
37178621a0aSAmaiKinono  that's the closing delimiter. It will be followed by ``;"`` and then a tab.
37278621a0aSAmaiKinono  That's the end of the pattern field. By "unescaped pattern delimiter", we
37378621a0aSAmaiKinono  mean there's an even number (including 0) of backslashes before it.
3747221c13bSAmaiKinono* From here, split the rest of the line into fields by tabs.
3757221c13bSAmaiKinono
3767221c13bSAmaiKinonoThen, the escape sequences in fields other than the pattern field should be
3777221c13bSAmaiKinonotranslated. See "Proposal" in tags(5) to know about all the escape sequences.
378792fa325SAmaiKinono
379792fa325SAmaiKinonoMake Use of the Pattern Field
380792fa325SAmaiKinono~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
381792fa325SAmaiKinono
382792fa325SAmaiKinonoThe pattern field specifies how to find a tag in its source file. The code
383792fa325SAmaiKinonogenerating this field seems to have a long history, so there are some pitfalls
384792fa325SAmaiKinonoand it's a bit hard to handle. A client tool could simply require the ``line:``
385792fa325SAmaiKinonofield and jump to the line it specifies, to avoid using the pattern field. But
386792fa325SAmaiKinonoanyway, we'll discuss how to make the best use of it here.
387792fa325SAmaiKinono
388792fa325SAmaiKinonoYou should take the words here merely as suggestions, and not standards. A
389792fa325SAmaiKinonoclient tool could definitely develop better (or simpler) ways to use the
390792fa325SAmaiKinonopattern field.
391792fa325SAmaiKinono
392792fa325SAmaiKinonoFrom the last section, we know the pattern field could contain a line number
393792fa325SAmaiKinonoand a search pattern. When it only contains the line number, handling it is
394792fa325SAmaiKinonoeasy: you simply go to that line.
395792fa325SAmaiKinono
396792fa325SAmaiKinonoThe search pattern resembles an EX command, but as we'll see later, it's
397792fa325SAmaiKinonoactually not a valid one, so some manual work are required to process it.
398792fa325SAmaiKinono
399792fa325SAmaiKinonoThe search pattern could look like ``/pat/``, called "forward search pattern",
400792fa325SAmaiKinonoor ``?pat?``, called "backward search pattern". Using a search pattern means
401792fa325SAmaiKinonoeven if the source file is updated, as long as the part containing the tag
402792fa325SAmaiKinonodoesn't change, we could still locate the tag correctly by searching.
403792fa325SAmaiKinono
404792fa325SAmaiKinonoWhen the pattern field only contains the search pattern, you just search for
405792fa325SAmaiKinonoit. The search direction (forward/backward) doesn't matter, as it's decided
406792fa325SAmaiKinonosolely by whether the ``-B`` option is enabled, and not the actual context. You
407792fa325SAmaiKinonocould always start the search from say the beginning of the file.
408792fa325SAmaiKinono
409792fa325SAmaiKinonoWhen both the search pattern and the line number are presented, you could make
410792fa325SAmaiKinonogood use of the line number, by going to the line first, then searching for the
411b51e7b0cSK.Takatanearest occurrence of the pattern. A way to do this is to search both forward
412b51e7b0cSK.Takataand backward for the pattern, and when there is a occurrence on both sides, go
413792fa325SAmaiKinonoto the nearer one.
414792fa325SAmaiKinono
415792fa325SAmaiKinonoWhat's good about this is when there are multiple identical lines in the source
416792fa325SAmaiKinonofile (e.g. the COMMON block in Fortran), this could help us find the correct
417792fa325SAmaiKinonoone, even after the source file is updated and the tag position is shifted by a
418792fa325SAmaiKinonofew lines.
419792fa325SAmaiKinono
420792fa325SAmaiKinonoNow let's discuss how to search for the pattern. After you trim the ``/`` or
421792fa325SAmaiKinono``?`` around it, the pattern resembles a regex pattern. It should be a regex
422792fa325SAmaiKinonopattern, as required by being a valid EX command, but it's actually not, as
423792fa325SAmaiKinonoyou'll see below.
424792fa325SAmaiKinono
425792fa325SAmaiKinonoIt could begin with a ``^``, which means the pattern starts from the beginning
426792fa325SAmaiKinonoof a line. It could also end with an *unescaped* ``$`` which means the pattern
427792fa325SAmaiKinonoends at the end of a line. Let's keep this information, and trim them too.
428792fa325SAmaiKinono
429792fa325SAmaiKinonoNow the remaining part is the actual string containing the tag. Some characters
430792fa325SAmaiKinonoare escaped:
431792fa325SAmaiKinono
432792fa325SAmaiKinono* ``\``.
433792fa325SAmaiKinono* ``$``, but only at the end of the string.
434792fa325SAmaiKinono* ``/``, but only in forward search patterns.
435792fa325SAmaiKinono* ``?``, but only in backward search patterns.
436792fa325SAmaiKinono
437792fa325SAmaiKinonoYou need to unescape these to get the literal string. Now you could convert
438792fa325SAmaiKinonothis literal string to a regexp that matches it (by escaping, like
439792fa325SAmaiKinono``re.escape`` in Python or ``regexp-quote`` in Elisp), and assemble it with
440792fa325SAmaiKinono``^`` or ``$`` if the pattern originally has it, and finally search for the tag
441792fa325SAmaiKinonousing this regexp.
442792fa325SAmaiKinono
443792fa325SAmaiKinonoRemark: About a Previous Format of the Pattern Field
444792fa325SAmaiKinono~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
445792fa325SAmaiKinono
446dccba5efSHiroo HAYASHIIn some earlier versions of Universal Ctags, the line number in the pattern
447792fa325SAmaiKinonofield is the actual line number minus one, for forward search patterns; or plus
448792fa325SAmaiKinonoone, for backward search patterns. The idea is to resemble an EX command: you
449792fa325SAmaiKinonogo to the line, then search forward/backward for the pattern, and you can
450792fa325SAmaiKinonoalways find the correct one. But this denies the purpose of using a search
451792fa325SAmaiKinonopattern: to tolerate file updates. For example, the tag is at line 50,
452792fa325SAmaiKinonoaccording to this scheme, the pattern field should be::
453792fa325SAmaiKinono
454792fa325SAmaiKinono	49;/pat/;"
455792fa325SAmaiKinono
456792fa325SAmaiKinonoThen let's assume that some code above are removed, and the tag is now at
457792fa325SAmaiKinonoline 45. Now you can't find it if you search forward from line 49.
458792fa325SAmaiKinono
459dccba5efSHiroo HAYASHIDue to this reason, Universal Ctags turns to use the actual line number. A
460792fa325SAmaiKinonoclient tool could distinguish them by the ``TAG_OUTPUT_EXCMD`` pseudo tag, it's
461792fa325SAmaiKinono"combine" for the old scheme, and "combineV2" for the present scheme. But
462792fa325SAmaiKinonoprobably there's no need to treat them differently, since "search for the
463b51e7b0cSK.Takatanearest occurrence from the line" gives good result on both schemes.
46400e59525SMasatake YAMATO
465d75d4cb9SMasatake YAMATOJSON OUTPUT
466d75d4cb9SMasatake YAMATO-----------
467d75d4cb9SMasatake YAMATOUniversal Ctags supports `JSON <https://www.json.org/>`_ (strictly
468d75d4cb9SMasatake YAMATOspeaking `JSON Lines <https://jsonlines.org/>`_) output format if the
469d75d4cb9SMasatake YAMATOctags executable is built with ``libjansson``.  JSON output goes to
470d75d4cb9SMasatake YAMATOstandard output by default.
471d75d4cb9SMasatake YAMATO
472d75d4cb9SMasatake YAMATOFormat
473d75d4cb9SMasatake YAMATO~~~~~~
474d75d4cb9SMasatake YAMATOEach JSON line represents a tag.
475d75d4cb9SMasatake YAMATO
476d75d4cb9SMasatake YAMATO.. code-block:: console
477d75d4cb9SMasatake YAMATO
478d75d4cb9SMasatake YAMATO	$ ctags --extras=+p --output-format=json --fields=-s input.py
479d75d4cb9SMasatake YAMATO	{"_type": "ptag", "name": "JSON_OUTPUT_VERSION", "path": "0.0", "pattern": "in development"}
480d75d4cb9SMasatake YAMATO	{"_type": "ptag", "name": "TAG_FILE_SORTED", "path": "1", "pattern": "0=unsorted, 1=sorted, 2=foldcase"}
481d75d4cb9SMasatake YAMATO	...
482d75d4cb9SMasatake YAMATO	{"_type": "tag", "name": "Klass", "path": "/tmp/input.py", "pattern": "/^class Klass:$/", "language": "Python", "kind": "class"}
483d75d4cb9SMasatake YAMATO	{"_type": "tag", "name": "method", "path": "/tmp/input.py", "pattern": "/^    def method(self):$/", "language": "Python", "kind": "member", "scope": "Klass", "scopeKind": "class"}
484d75d4cb9SMasatake YAMATO	...
485d75d4cb9SMasatake YAMATO
486d75d4cb9SMasatake YAMATOA key not starting with ``_`` is mapped to a field of ctags.
487d75d4cb9SMasatake YAMATO"``--output-format=json --list-fields``" options list the fields.
488d75d4cb9SMasatake YAMATO
489d75d4cb9SMasatake YAMATOA key starting with ``_`` represents meta information of the JSON
490d75d4cb9SMasatake YAMATOline.  Currently only ``_type`` key is used. If the value for the key
491d75d4cb9SMasatake YAMATOis ``tag``, the JSON line represents a normal tag. If the value is
492d75d4cb9SMasatake YAMATO``ptag``, the line represents a pseudo-tag.
493d75d4cb9SMasatake YAMATO
494d75d4cb9SMasatake YAMATOThe output format can be changed in the
495d75d4cb9SMasatake YAMATOfuture. ``JSON_OUTPUT_VERSION`` pseudo-tag provides a change
496d75d4cb9SMasatake YAMATOclient-tools to handle the changes.  Current version is "0.0". A
497d75d4cb9SMasatake YAMATOclient-tool can extract the version with ``path`` key from the
498d75d4cb9SMasatake YAMATOpseudo-tag.
499d75d4cb9SMasatake YAMATO
500d75d4cb9SMasatake YAMATOThe JSON output format is newly designed and has no limitation found
501d75d4cb9SMasatake YAMATOin the default tags file format.
502d75d4cb9SMasatake YAMATO
503d75d4cb9SMasatake YAMATO* The values for ``kind`` key are represented in long-name flags.
504d75d4cb9SMasatake YAMATO  No one-letter is here.
505d75d4cb9SMasatake YAMATO
506d75d4cb9SMasatake YAMATO* Scope names and scope kinds have distinguished keys: ``scope`` and ``scopeKind``.
507d75d4cb9SMasatake YAMATO  They are combined in the default tags file format.
508d75d4cb9SMasatake YAMATO
509d75d4cb9SMasatake YAMATOData type used in a field
510d75d4cb9SMasatake YAMATO~~~~~~~~~~~~~~~~~~~~~~~~~
511d75d4cb9SMasatake YAMATOValues for the most of all keys are represented in JSON string type.
512d75d4cb9SMasatake YAMATOHowever, some of them are represented in string, integer, and/or boolean type.
513d75d4cb9SMasatake YAMATO
514d75d4cb9SMasatake YAMATO"``--output-format=json --list-fields``" options show What kind of data type
515d75d4cb9SMasatake YAMATOused in a field of JSON.
516d75d4cb9SMasatake YAMATO
517d75d4cb9SMasatake YAMATO.. code-block:: console
518d75d4cb9SMasatake YAMATO
519d75d4cb9SMasatake YAMATO	$ ctags --output-format=json --list-fields
520d75d4cb9SMasatake YAMATO	#LETTER NAME           ENABLED LANGUAGE         JSTYPE FIXED DESCRIPTION
521d75d4cb9SMasatake YAMATO	F       input          yes     NONE             s--    no    input file
522d75d4cb9SMasatake YAMATO	...
523d75d4cb9SMasatake YAMATO	P       pattern        yes     NONE             s-b    no    pattern
524d75d4cb9SMasatake YAMATO	...
525d75d4cb9SMasatake YAMATO	f       file           yes     NONE             --b    no    File-restricted scoping
526d75d4cb9SMasatake YAMATO	...
527d75d4cb9SMasatake YAMATO	e       end            no      NONE             -i-    no    end lines of various items
528d75d4cb9SMasatake YAMATO	...
529d75d4cb9SMasatake YAMATO
530d75d4cb9SMasatake YAMATO``JSTYPE`` column shows the data types.
531d75d4cb9SMasatake YAMATO
532d75d4cb9SMasatake YAMATO'``s``'
533d75d4cb9SMasatake YAMATO	string
534d75d4cb9SMasatake YAMATO
535d75d4cb9SMasatake YAMATO'``i``'
536d75d4cb9SMasatake YAMATO	integer
537d75d4cb9SMasatake YAMATO
538d75d4cb9SMasatake YAMATO'``b``'
539d75d4cb9SMasatake YAMATO	boolean (true or false)
540d75d4cb9SMasatake YAMATO
541d75d4cb9SMasatake YAMATOFor an example, the value for ``pattern`` field of ctags takes a string or a boolean value.
542d75d4cb9SMasatake YAMATO
54300e59525SMasatake YAMATOSEE ALSO
54400e59525SMasatake YAMATO--------
545c396dfafSMasatake YAMATOctags(1), ctags-lang-python(7), ctags-incompatibilities(7), tags(5), readtags(1)
546