How to enable tab complete only in term-line-mode

Solution 1:

I solved it with my customised function my-tab with some code written by phil.I bind tab key to my-tab, it will send a raw tab when in term-char-mode and do complete in other cases, also two quick tab will do yas-expand, by binding yas-next-field to "M-n", tab completion even works inside a snippet expansion.

The problem is when company and yasnnipet has the same prefix, then tab only do completion. Is it possible to set a delay time for company completion after tab key press, make it greater than my-double-key-timeout, so double tab will do yas-expand, if the user want company completion, he can just type one tab and wait. (sovled with sit for)

I don't know why I can't just bind tab to send a raw tab in term-raw-map.

(defvar my-double-key-timeout 0.25
  "The number of seconds to wait for a second key press.")

 (defun my-tab ()
  "Move to the beginning of the current line on the first key stroke,
and to the beginning of the buffer if there is a second key stroke
within `my-double-key-timeout' seconds."
  (interactive)
  (let ((last-called (get this-command 'my-last-call-time)) )
        (is-term (string= "term-mode" major-mode)))
    (if (and is-term (term-in-char-mode))
    (term-send-raw-string "\t")
      (if (and (eq last-command this-command)
           last-called
           (<= (time-to-seconds (time-since last-called))
           my-double-key-timeout))
      (yas-expand) 

            (if (sit-for my-double-key-timeout)
             (complete-indent-fold)))

    (put this-command 'my-last-call-time (current-time))))

(defun complete-indent-fold()
  (interactive)
  (if (looking-at outline-regexp)
      (if (equal major-mode 'org-mode) (org-cycle) (my-outline-cycle))
    (if (looking-at "\\_>") (company-complete) (indent-for-tab-command))))

Please help me make this better. Thank you!