      SUBROUTINE EDITIN( INTRNL )
      INCLUDE 'SIZES'
      IMPLICIT REAL (A-H,O-Z)
      REAL CART, DENMAT, BONDS, VANRAD
      INTEGER*2 ATBOND
      LOGICAL ERROR,MODATA,INTRNL,LCLDBG, LINALL,AMODE,EDMADE, REDRAW
      COMMON /ATOMS/ CO(3, NUMATM),IE( NUMATM),NATOMS, ATCHG( NUMATM)
      COMMON /GEOM/ COOLD(3, NUMATM),NA( NUMATM),NB( NUMATM),NC( NUMATM)
      COMMON /INTCOR/ XNDOGM(3, NUMATM),INTFRE(3, NUMATM)
      COMMON /EDIT/ MODATA, REDRAW
      CHARACTER*6 ATSYMB, CHAR7*1
      COMMON /ATSYMB/ ATSYMB( 200)
      CHARACTER COMAND*80, ICCC*2
      COMMON /COMM/ COMAND
      COMMON /VALNCE/ MAXVAL(200)
      COMMON /VANRAD/ VANRAD(200)
      LOGICAL DEBUG, DEBUGL, DEBUGN, DEBUGO, DEBUGP, DEBUGI
      COMMON /DEBCOM/ DEBUG, DEBUGL, DEBUGN, DEBUGO, DEBUGP, DEBUGI
      COMMON /DISPLY/ IREM(200), BSCALE, ATBOND( NUMATM, NUMATM),
     .                ISTYPE, LATYPE, IMASK( NUMATM), ISCOLO
      COMMON /LINES/ CART(2, NUMATM),LIST( NUMATM), LATOMS, LINALL
      COMMON /DENSTY/ DENMAT( MPACK ), BONDS( MPACK )
      DIMENSION IVAL( NUMATM)
*
*   LOGICAL INTRNL INDICATES MODE OF ADDING NEW ATOMS
*            .TRUE.   ==>   INTERNAL COORDINATES
*           .FALSE.   ==>   CARTESIAN COORDINATES
*
** LOCAL FUNCTION FOR THIS ROUTINE
      ATDIST( I, J) = SQRT( (CO(1,I)-CO(1,J))**2
     .          + (CO(2,I)-CO(2,J))**2 + (CO(3,I)-CO(3,J))**2 )
**
**                               
      CHAR7 = CHAR(7)
      ICCC = 'CC'
      IF ( INTRNL ) ICCC = 'IC'
      IIATOM = READA( COMAND, 1, ERROR)
      IF ( ERROR ) RETURN
      GOTO 51
 50   IIATOM=IIATOM + 1
 51   CONTINUE
      CALL PLOT (0,0,8)
      WRITE( COMAND,'(A2,'' of atom '', I4,'':'')') ICCC, IIATOM
      CALL UPROMP( COMAND( 1: INDEX( COMAND, ':')) )
      READ ( *, '( A )' ) COMAND
      CALL LCLEAN(COMAND, COMAND, .TRUE.)
      IF ( COMAND(:1) .EQ. 'Q') RETURN
      IF ( COMAND(:1) .EQ. ' ') RETURN
      CALL GETIC(COMAND,IT,B,IB,A,IA,D,ID,MA,MB,MC,ERROR)
      IF ( MA.GE.IIATOM .OR. (MA.LT.1 .AND. IIATOM.GT.1)) THEN
         ERROR=.TRUE.
         WRITE (*,*) CHAR7//'ERROR IN NA.'
      ENDIF
      IF ( MB.GE.IIATOM .OR. (MB.LT.1 .AND. IIATOM.GT.2)) THEN
         ERROR=.TRUE.
         WRITE (*,*) CHAR7//'ERROR IN NB.'
      ENDIF
      IF ( MC.GE.IIATOM .OR. (MC.LT.1 .AND. IIATOM.GT.3)) THEN
         ERROR=.TRUE.
         WRITE (*,*) CHAR7//'ERROR IN NC.'
      ENDIF
      IF ( MA.EQ.MB .AND. IIATOM.GT.1) THEN
         ERROR = .TRUE.
         WRITE (*,*) CHAR7//'ERROR - NA AND NB ARE EQUAL.'
      ENDIF
      IF ( MA.EQ.MC .AND. IIATOM.GT.1) THEN
         ERROR=.TRUE.
         WRITE (*,*) CHAR7//'ERROR - NA AND NC ARE EQUAL.'
      ENDIF
      IF ( MB.EQ.MC .AND. IIATOM.GT.2) THEN
         ERROR=.TRUE.
         WRITE (*,*) CHAR7//'ERROR - NB AND NC ARE EQUAL.'
      ENDIF
      IF (ERROR) THEN
          WRITE (*,*) CHAR7//'DATA ERROR.'
          IF ( IT .LT. 1 .OR. IT .GT. 200 ) THEN
             WRITE (*,1050) IIATOM, '??????',B,IB,A,IA,D,ID,MA,MB,MC
          ELSE
             WRITE (*,1050) IIATOM, ATSYMB(IT),B,IB,A,IA,D,ID,MA,MB,MC
          ENDIF
 1050     FORMAT ( 1X, I4, ':', A6, 1X, 3( F8.3, 1X, I2, 1X), 3(1X,I4))
          WRITE (*,* ) 'Please try again from the start',
     .                         ' of that last line.'
          ERROR=.FALSE.
          GO TO 51
       ENDIF
       NATOMS=NATOMS+1
       IE(NATOMS)=IT
       IF ( INTRNL ) THEN
          XNDOGM(1,NATOMS)=B
          XNDOGM(2,NATOMS)=A
          XNDOGM(3,NATOMS)=D
          IF (NATOMS .EQ. 1) THEN
             INTFRE(1,NATOMS)=0
          ELSE
             INTFRE(1,NATOMS)=IB
          ENDIF
          IF (NATOMS .LE. 2) THEN
             INTFRE(2,NATOMS)=0
          ELSE
             INTFRE(2,NATOMS)=IA
          ENDIF
          IF (NATOMS .LE. 3) THEN
             INTFRE(3,NATOMS)=0
          ELSE
             INTFRE(3,NATOMS)=ID
          ENDIF
          NA(NATOMS)=MA
          NB(NATOMS)=MB
          NC(NATOMS)=MC
          TEMPXX=CO(1,1)
          TEMPYY=CO(2,1)
          TEMPZZ=CO(3,1)
          CALL GMETRY(NUATOM,NATOMS,IE,XNDOGM,NA,NB,NC,CO,ERROR)
          CALL TOTROT(TEMPXX,TEMPYY,TEMPZZ)
       ELSE
          CO(1,NATOMS)=B
          CO(2,NATOMS)=A
          CO(3,NATOMS)=D
          IF (NATOMS .EQ. 1) THEN
             INTFRE(1,NATOMS)=0
          ELSE
             INTFRE(1,NATOMS)=IB
          ENDIF
          IF (NATOMS .LE. 2) THEN
             INTFRE(2,NATOMS)=0
          ELSE
             INTFRE(2,NATOMS)=IA
          ENDIF
          IF (NATOMS .LE. 3) THEN
             INTFRE(3,NATOMS)=0
          ELSE
             INTFRE(3,NATOMS)=ID
          ENDIF
          NA(NATOMS)=MA
          NB(NATOMS)=MB
          NC(NATOMS)=MC
          CALL XYZMND( NATOMS, CO, NA, NB, NC, XNDOGM)
       ENDIF
       DO 40 I= NATOMS, IIATOM, -1
          NA(I+1) = NA( I)
          NB(I+1) = NB( I)
          NC(I+1) = NC( I)
          IE(I+1) = IE( I)
          DO 40 J=1, 3
             CO(J,I+1) = CO(J,I)
             XNDOGM(J,I+1) = XNDOGM(J,I)
             INTFRE(J,I+1) = INTFRE(J,I)
  40   CONTINUE
       NA(IIATOM +1) = NA( NATOMS)
       NB(IIATOM +1) = NB( NATOMS)
       NC(IIATOM +1) = NC( NATOMS)
       IE(IIATOM +1) = IE( NATOMS)
       CO(J,IIATOM +1) = CO(J,NATOMS)
       XNDOGM(J,IIATOM +1) = XNDOGM(J,NATOMS)
       INTFRE(J,IIATOM +1) = INTFRE(J,NATOMS)
       BONDS( 1 ) = 0
       CALL SETBON( .FALSE.)
       DO 54 J=1,NATOMS
          IF ( IE( J) .GE. 99 ) GOTO 54
          NBOND=0
          SSEP1=VANRAD(IE(J))
          DO 52 I=1,NATOMS
             IF ( IE( I) .GE. 99 ) GOTO 52
             IF ( I .EQ. J) GO TO 52
C?             DIST=ATDIST(J,I)
C?             SSEP=(SSEP1+VANRAD(IE(I)))*BSCALE
C?             IF ( DIST .LE. SSEP) NBOND=NBOND+1
             IF ( ATBOND( J, I ) .GT. 0 ) NBOND = NBOND+1
  52      CONTINUE
          IF ( NBOND .GT. MAXVAL(IE(J)) .AND. NBOND .GT. IVAL(J) ) THEN
             IVAL(J)=NBOND
             WRITE (*,1007) CHAR7,J,NBOND
1007         FORMAT ( 1X,A,'ATOM ',I4,' has ',I2,' BONDS:' )
             WRITE (*,*)
             COMAND = '     '
             IIT = 1
             DO 53 I=1,NATOMS
                IF ( IE( I) .GE. 99) GOTO 53
                IF ( I .EQ. J) GOTO 53
                DIST=ATDIST(J,I)
C?                SSEP=(SSEP1+VANRAD(IE(I)))*BSCALE
C?                IF (DIST .LE. SSEP) THEN
                IF ( ATBOND( J, I) .GT. 0 ) THEN
                   WRITE (COMAND(IIT:),1006) I,DIST
 1006              FORMAT ( ' To: ',I3,' by ',F6.4,'; ' )
                   IIT = INDEX( COMAND(:IIT), ';') + IIT
                   IF ( IIT .GE. 60) THEN
                      WRITE (*,*) COMAND(:IIT)
                      IIT = 1
                   ENDIF
                ENDIF
  53         CONTINUE
             IF ( IIT .GT. 1) WRITE (*,*) COMAND(:IIT)
          ENDIF
  54      CONTINUE
          IF ( NATOMS .GT. 3) THEN
             CALL SETLAB
             CALL LDRAW(NATOMS)
          ELSE
             CALL PICTUR
          ENDIF
          REDRAW = .TRUE.
          MODATA=.TRUE.
          GO TO 50
      END
