      subroutine view( pb_id, display, libdb )

      implicit	none
cc
cc Purpose:	Allow the user to view a file in the CMS library.
cc
      include	'cmsdef.fin'
      include	'libed.fin'
      include	'smg_refs.fin'
      include	'($smgdef)'
      include	'($rmsdef)'
      include	'($iodef)'

      integer*4				pb_id
      record /display/			display
      record /library_data_block/	libdb

      record /fetch_data_block/		fetdb
      integer*4		kb_id

      integer*4		cms_stat, smg_stat
      integer*4		i, j, pos
      integer*4		tokens_found, token_start(1:10), token_length(1:10)
      integer*4		count

      integer*4		text_display, control, terminator
      character*1	text
      character*39	generation
      character*79	element
      character*80	label
      character*255	string

      logical		done, pause, more

      integer*4	chklen
      external	chklen

cc Get the element that is at the cursor.
      smg_stat = smg$set_cursor_abs( display.id, display.line, 1 )
      smg_stat = smg$read_from_display( display.id, string )
      call find_tokens( string, 10, 
     +		tokens_found, token_start, token_length )

      do i = 1, tokens_found
         if ((token_start(i) .le. display.column) .and.
     +	     (token_start(i) + token_length(i) - 1 .ge. display.column) )then
            pos = i
         endif
      enddo

      i = token_start(pos)
      j = i + token_length(pos) - 1
      element = string(i:j)

      smg_stat = 
     +	smg$create_virtual_display( 1, 80, control, smg$m_border )

      smg_stat = 
     +	smg$create_virtual_display( 21, 80, text_display, smg$m_border )

      smg_stat = 
     + 	smg$paste_virtual_display( control, pb_id, 24, 1 )

      smg_stat = smg$create_virtual_keyboard( kb_id )

cc Open the element.
      cms_stat = cms$fetch_open(
     +			fetdb, 
     +			display.library(1:chklen(display.library)),
     +			element(1:chklen(element)),
     +			display.generation(1:chklen(display.generation)),
     +			1, 1, generation, )

      label = 
     +		element(1:chklen(element))//
     +		', '//generation

      smg_stat = smg$label_border( text_display, label(1:chklen(label)) )

      smg_stat = 
     + 	smg$paste_virtual_display( text_display, pb_id, 2, 1 )

      done = .not. cms_stat
      more = .true.
      count = 0

cc Loop until EOF or until the user wants no more.
      do while (.not. done)

cc   FETCH a line of the element
         cms_stat = cms$fetch_get( fetdb, string )
         count = count + 1

cc   Check to see if we've reache EOF.
cc	Note: CMS returns RMS$_EOF, not CMS$_EOF as documented.
         done = cms_stat .eq. rms$_eof

cc   If we've put out 20 lines, pause.
         pause = mod(count, 20) .eq. 0

         if (.not. done) then
            smg_stat = smg$put_with_scroll( text_display, string )
         endif

cc     Ask if the user wants to continue.
         if (pause .and. (.not. done)) then
            smg_stat = smg$put_chars( control, 'More?', 1, 74 )
            smg_stat = smg$read_string(	kb_id, text,
     + 					, 1, 
     + 		io$m_escape+io$m_nofiltr+io$m_noecho+io$m_trmnoecho, ,
     + 					,
     + 					,
     + 					terminator, control)

            done = (terminator .eq. smg$k_trm_ctrlz) .or.
     +		(text .eq. 'n') .or. (text .eq. 'N')

            if ( terminator .eq. smg$k_trm_ctrlw )
     +            smg_stat = smg$repaint_screen( pb_id )

            more = .not. done

         endif

      enddo

cc Close the element.
      cms_stat = cms$fetch_close( fetdb )

      if (more .and. done) then
         smg_stat = smg$put_chars( control,
     +		'Press any key to continue...', 1, 51 )

         smg_stat = smg$read_string(	kb_id, text,
     + 					, 1, 
     + 		io$m_escape+io$m_nofiltr+io$m_noecho+io$m_trmnoecho, ,
     + 					,
     + 					,
     + 					terminator, control)

      endif

cc Clean up and return.
      smg_stat = smg$delete_virtual_keyboard( kb_id )

      smg_stat = smg$pop_virtual_display( control, pb_id )
      smg_stat = smg$pop_virtual_display( text_display, pb_id )

      end
