(defun gis-newline (arg)
  "If in a prev. cmd., recall.
If within curr. cmd., insert a newline.
If at end of curr. cmd. and cmd. is complete, send to gis.
If at end of curr. cmd. and cmd. is not complete, insert a newline.
Else (not in any cmd.) recall line."
  (interactive "*P")
  (let
      ((n (get-curr-gis-cmd-num))
       (p (process-mark (get-buffer-process (current-buffer)))))
    (cond
     (n  ; in a prev. cmd.
      (copy-gis-cmd n
                    (- (point)
                       (car (aref prev-gis-cmds n)))))

     ((>= (point) p)
      (if abbrev-mode (expand-abbrev))
      (cond
       ((looking-at "[ \t\n]*\\'")  ; at end of curr. cmd.
        (newline arg)
        (cond
         ((save-excursion
            (and (progn
                   (skip-chars-backward " \t\n")
                   (eq (preceding-char) ?$))
                 (> (point) p)))
          (skip-chars-backward " \t\n")
          (forward-char)
          (delete-region (point) (point-max))
          (send-gis-region (marker-position p) (point)))
         ((complete-magik-p p (point))
		  ;; SWV doesn'tr require $
          ;;(insert "$\n")
          (delete-region (point) (point-max))
          (send-gis-region (marker-position p) (point)))))
       ((looking-at "[ \t\n]*\\$[ \t\n]*\\'")
        (if (complete-magik-p p (point))
            (progn
              (search-forward "$")
              (delete-region (point) (point-max))
              (insert "\n")
              (send-gis-region (marker-position p) (point)))
          (newline arg)))
       (t
        (newline arg))))
         
     (t  ; not in any cmd.
      (delete-region (process-mark (get-buffer-process (current-buffer))) (point-max))
      (let
          ((str (buffer-substring (point-bol) (point-eol)))
           (n (- (point-eol) (point))))
        (goto-char (point-max))
        (insert str)
        (backward-char n)))
)))

(provide 'emacs_5_base)
