--- company-matlab-shell.el	2017-11-08 12:51:36.875700408 -0500
+++ company-matlab-shell-new.el	2017-10-12 14:48:42.414467116 -0400
@@ -24,51 +24,56 @@
 ;; the following code is mostly taken from matlab.el, (C) Eric M. Ludlam
 (defun company-matlab-shell-tab ()
    "Send [TAB] to the currently running matlab process and retrieve completion."
-   (goto-char (point-max))
-   (let ((inhibit-field-text-motion t))
-     (beginning-of-line))
-   (re-search-forward comint-prompt-regexp)
-   (let* ((lastcmd (buffer-substring (point) (matlab-point-at-eol)))
-	  (tempcmd lastcmd)
-	  (completions nil)
-	  (limitpos nil))
-     ;; search for character which limits completion, and limit command to it
-     (setq limitpos
-	   (if (string-match ".*\\([( /[,;=']\\)" lastcmd)
-	       (1+ (match-beginning 1))
-	     0))
-     (setq lastcmd (substring lastcmd limitpos))
-     ;; Whack the old command so we can insert it back later.
-     (delete-region (+ (point) limitpos) (matlab-point-at-eol))
-     ;; double every single quote
-     (while (string-match "[^']\\('\\)\\($\\|[^']\\)" tempcmd)
-       (setq tempcmd (replace-match "''" t t tempcmd 1)))
-     ;; collect the list
-     (setq completions (matlab-shell-completion-list tempcmd))
+   (let ((orig-point (point)))
      (goto-char (point-max))
-     (insert lastcmd)
-     completions))
+     (let ((inhibit-field-text-motion t))
+       (beginning-of-line))
+     (re-search-forward comint-prompt-regexp)
+     
+     (let* ((lastcmd (buffer-substring-no-properties (point) orig-point))
+	    (eol-offset (if (> orig-point (point))
+			    (- orig-point (point-max))
+			  0))
+	    (tempcmd lastcmd)
+	    (completions nil)
+	    (limitpos nil))
+       ;; search for character which limits completion, and limit command to it
+       (setq limitpos
+	     (if (string-match ".*\\([( /[,;=']\\)" lastcmd)
+		 (1+ (match-beginning 1))
+	       0))
+       (setq lastcmd (substring lastcmd limitpos))
+       ;; double every single quote
+       (while (string-match "[^']\\('\\)\\($\\|[^']\\)" tempcmd)
+	 (setq tempcmd (replace-match "\"" t t tempcmd 1)))
+       ;; collect the list
+       (setq completions (matlab-shell-completion-list tempcmd))
+       (goto-char (+ (point-max) eol-offset))
+       completions)))
 
 (defun company-matlab-shell-grab-symbol ()
   (when (string= (buffer-name (current-buffer)) (concat "*" matlab-shell-buffer-name "*"))
     (save-excursion
-      (goto-char (point-max))
-      (let ((inhibit-field-text-motion t))
-	(beginning-of-line))
-      (re-search-forward comint-prompt-regexp)
-      (let* ((lastcmd (buffer-substring (point) (matlab-point-at-eol)))
-	     limitpos)
-	(setq limitpos
-	      (if (string-match ".*\\([( /[,;=']\\)" lastcmd)
-		  (1+ (match-beginning 1))
-		0))
-	(substring-no-properties lastcmd limitpos)))))
+      (let ((orig-point (point))) 
+	(goto-char (point-max))
+	(let ((inhibit-field-text-motion t))
+	  (beginning-of-line))
+	(re-search-forward comint-prompt-regexp)
+	(when (>= orig-point (point))
+	 (let* ((lastcmd (buffer-substring (point) orig-point))
+		limitpos)
+	   (setq limitpos
+		 (if (string-match ".*\\([( /[,;=']\\)" lastcmd)
+		     (1+ (match-beginning 1))
+		   0))
+	   (substring-no-properties lastcmd limitpos)))))))
 
 
 (defun company-matlab-shell-get-completions ()
   (when (string= (buffer-name (current-buffer)) (concat "*" matlab-shell-buffer-name "*"))
     (mapcar 'car (company-matlab-shell-tab))))
 
+
 ;;;###autoload
 (defun company-matlab-shell (command &optional arg &rest ignored)
   "A `company-mode' completion back-end for Matlab-Shell."
