2012-11-27 45 views
2

我試圖讓所有打開的緩衝區和yasnippet中的選項卡都與tab鍵一起工作。此刻我可以擁有一個或另一個。下面的代碼是我如何處理yasnippet展開,但因爲我不是一個lisp程序員,所以我在這裏看不到這個錯誤。emacs智能選項卡與yasnippets

如果它無法展開片段,我希望它嘗試從緩衝區中展開。

;; Auto complete settings/tab settings 
;; http://emacsblog.org/2007/03/12/tab-completion-everywhere/ <-- in the comments 
(global-set-key [(tab)] 'smart-tab) 
(defun smart-tab() 
    "This smart tab is minibuffer compliant: it acts as usual in 
    the minibuffer. Else, if mark is active, indents region. Else if 
    point is at the end of a symbol, expands it. Else indents the 
    current line." 
    (interactive) 
    (if (minibufferp) 
     (unless (minibuffer-complete) 
     (dabbrev-expand nil)) 
    (if mark-active 
     (indent-region (region-beginning) 
         (region-end)) 
     (if (looking-at "\\_>") 
      (unless (yas/expand) 
      (dabbrev-expand nil)) 
     (indent-for-tab-command))))) 

回答

1

首先,我嘗試瞭解代碼的功能,以及您想要的功能。

您的代碼

  • first checks如果點在the minibuffer

    • 如果是這樣,那麼它會嘗試完成迷你緩衝區

      • 如果(在迷你緩衝區中)不能完成它,它調用dabbrev-擴大
  • 否則如果點是not in minibuffer

    • 如果某個區域被標記,則縮進該區域。

    • 如果no mark is active,它會檢查,看是否點在end of some word

      • 如果是這樣,它檢查是yasnippet可以擴大。

        • 如果亞斯不能擴展,它調用dabbrev-擴大
      • 如果不是,它試圖縮進當前行

這是你的代碼呢。

由於yas/expand,您的代碼失敗。如果擴展失敗,該命令不會返回。

如果此命令失敗,它將檢查變量yas/fallback-behavior的狀態。如果這個變量的值爲call-other-command,與你的情況一樣,失敗的yas擴展調用綁定到變量yas/trigger-key中保存的密鑰的命令。

就你而言,這個變量是TAB

所以:你是在單詞的末尾,你按TAB鍵來完成它,這會觸發交互smart-tab,要求亞斯/擴大這情況下,它不能擴大呼叫TAB綁定功能,這裏是無限循環。

針對您的問題的解決方案是在smart-tab函數中臨時綁定nil to yas/fallback-behavior

這裏是如何解決它:如果它失敗,yasnippet擴大,然後我得到的錯誤「變量綁定深度超過MAX-specpdl尺寸」

(if (looking-at "\\_>") 
     (let ((yas/fallback-behavior nil)) 
     (unless (yas/expand) 
      (dabbrev-expand nil))) 
    (indent-for-tab-command)) 
+0

目前,它並沒有使用dabbrev-擴大請擴大 – map7

+1

謝謝。事情現在很清楚。我會更新這篇文章。 – alinsoar

+0

我更新了帖子 – alinsoar