[comp.sys.sun] Text Subwindow line number

boeker@uunet.uu.net (Michael Boeker) (12/12/88)

How can I determine the corresponding line number of a position
(Textsw_index) in a SunView Text Subwindow ?

zifrony@TAURUS.BITNET (12/23/88)

In Sun-Spots Digest, v7n47, unido!infhil!sun1a!boeker@uunet.uu.net
(Michael Boeker) needed to know how to determine the line number of a
position.  As I have encountered a similar problem, needing to find the
line number clicked on by the user, I have the following method to solve
it:

 1. I have created a mapping of the text window, noting in which position
    each line starts by using textsw_index_for_file_line().

 2. I have determined the location of the cursor in the text (the place where
    the user clicked), by using window_get(src, TEXTSW_INSERTION_POINT, 0),
    where src is my Textsw subwindow.  Note that dbxtool does it in a smoother
    way, and keeps the Textsw subwindow readonly; alas, I do not have the
    source of dbxtool, and had to resort to these means to solve the problem.

 3. I looped through my mapping, finding the first line whose first character
    index is greater than my position, and voila, the line is found.

Please note that the file I am working with is not too big, thus the
search loop does not take too long.  For huge files, I suppose a better
way has to be sought to solve the problem.

 Doron Zifrony                  zifrony@taurus.bitnet || zifrony@Math.Tau.Ac.IL
 Msc. Student
 Tel Aviv University
 Israel

rsalz@bbn.com (Rich Salz) (01/05/89)

This little piece of code does it...
#! /bin/sh
# This is a shell archive.  Remove anything before this line, then unpack
# it by saving it into a file and typing "sh file".  To overwrite existing
# files, type "sh file -c".  You can also feed this as standard input via
# unshar, or by typing "sh <file", e.g..  If this archive is complete, you
# will see the following message at the end:
#		"End of shell archive."
# Contents:  line.c
# Wrapped by rsalz@fig.bbn.com on Fri Dec 23 10:19:22 1988
PATH=/bin:/usr/bin:/usr/ucb ; export PATH
if test -f 'line.c' -a "${1}" != "-c" ; then 
  echo shar: Will not clobber existing file \"'line.c'\"
else
echo shar: Extracting \"'line.c'\" \(1144 characters\)
sed "s/^X//" >'line.c' <<'END_OF_FILE'
X/*
X*/
X#include <suntool/sunview.h>
X#include <suntool/textsw.h>
X#include <suntool/selection.h>
X#include <suntool/selection_svc.h>
X#include <suntool/selection_attributes.h>
X
Xextern Textsw	 KWICArea;
X
X/*
X**  Get the line number of the current selection, or -1 if not in the
X**  index window.
X*/
Xint
Xget_line_number()
X{
X    static int		 Saved = -1;
X    Seln_request	*Sbuffer;
X    Seln_holder		 Hold;
X    int			*p;
X
X    /* Get primary selection, see if it is in our window. */
X    Hold = seln_inquire(SELN_PRIMARY);
X    if (Hold.state == SELN_NONE || !seln_holder_same_client(&Hold, KWICArea))
X	/* Nope, get the old one. */
X	return(Saved);
X
X    /* Get the first line of the current selection. */
X    Sbuffer = seln_ask(&Hold, SELN_REQ_FAKE_LEVEL, SELN_LEVEL_LINE,
X			SELN_REQ_FIRST_UNIT, 0, 0);
X    if (Sbuffer->status == SELN_FAILED) {
X	Message("Can't get current selection!");
X	return(-1);
X    }
X
X    /* The data we asked for comes back in words. */
X    p = (int *)Sbuffer->data;
X
X    /* Make sure we got what we asked for. */
X    if (p[0] != (int)SELN_REQ_FAKE_LEVEL || p[2] != (int)SELN_REQ_FIRST_UNIT)
X	return(-1);
X
X    return(Saved = p[3]);
X}
END_OF_FILE
if test 1144 -ne `wc -c <'line.c'`; then
    echo shar: \"'line.c'\" unpacked with wrong size!
fi
# end of 'line.c'
fi
echo shar: End of shell archive.
exit 0
-- 
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.