# Enable hyperlinks for “see” in indexing?

I'm currently using packages hyperref and imakeidx to create my book's index. This index is big enough that it spans multiple pages. It contains some entries such as

% In the index under "B"...

\index{bar!foo|see {foobar}}%

% Several pages later in the index, under "F"...

\index{foobar}%


Since I'm using package hyperref, index-entries that point to a specific page in the mainmatter are rendered as hyperlinks in the final PDF. I wonder if there is a way to hyperlink the see entries as well. That is, I'd like to render "bar, see foobar" as a hyperlink that points to the page of the index where "foobar" is listed.

Is this possible? and if so, what's the best way to do it?

A preliminary version, working for top-level index items only, not working for entries like bar!foo itself if |see {foo} is meant.

There is a hypertarget defined and retrieved with \seelink.

The multiple usage of \index{foobar} will prevent same target names.

\documentclass{article}

\usepackage{blindtext}
\usepackage{imakeidx}
\usepackage{xparse}
\usepackage{letltxmacro}
\usepackage{hyperref}

\ExplSyntaxOn
\AtBeginDocument{%

\LetLtxMacro\indexorig\index

\newcommand{\hyperindextarget}[1]{%
\hypertarget{#1}{}%
}

\RenewDocumentCommand{\index}{om}{%
\IfValueTF{#1}{%
}{%
}%
}
}

}

\seq_new:N \g_quuxxplusone_target_seq

\IfValueTF{#1}{%
\seq_if_in:NnTF \g_quuxxplusone_target_seq {#2} {%
\indexorig[#1]{#2}%
}{% False branch
\seq_gput_right:Nn \g_quuxxplusone_target_seq {#2}
\indexorig[#1]{#2|hyperindextarget{#2}}%
}%
}{% No optional argument
\seq_if_in:NnTF \g_quuxxplusone_target_seq {#2} {%
\indexorig{#2}%
}{% False branch
\seq_gput_right:Nn \g_quuxxplusone_target_seq {#2}
\indexorig{#2|hyperindextarget{#2}}%
}%
}%
}

\ExplSyntaxOff

\makeindex

\begin{document}

Foo
\clearpage
\blindtext[5]
% In the index under "B"...

\index{bar}

% Several pages later in the index, under "F"...

\blindtext[15]

\index{foobar}%

\blindtext[15]

\index{foobar}