       (setq completion (try-completion pattern completion-table))
       (cond ((eq completion t))
             ((null completion)
-             (message "Can't find completion for \"%s\"" pattern)
+             (message "Can't find completion for \"%s\" based on line %s" pattern line)
              (ding))
             ((not (string= pattern completion))
-             (delete-region beg end)
+             (delete-region (- end (length pattern)) end)
              (insert completion))
             (t
              (message "Making completion list...")

                      (setq ugly-return (concat ugly-return string))
                      "")))))
      (process-send-string python-process
                            (format ipython-completion-command-string pattern))
      (accept-process-output python-process)
      (setq completions
            (split-string (substring ugly-return 0 (position ?\n ugly-return)) sep))

           (beg (save-excursion (skip-chars-backward "a-z0-9A-Z_./" (point-at-bol))
                                (point)))
           (end (point))
	   (line (buffer-substring-no-properties (point-at-bol) end))
           (pattern (buffer-substring-no-properties beg end))
           (completions nil)
           (completion-table nil)
