2013-04-02 107 views
6

在org-模式文件,與像以下代碼:縮進代碼

#+begin_src emacs-lisp 
(add-to-list 'org-tab-before-tab-emulation-hook 
      (lambda() 
       (when (within-the-body-of-a-begin-src-block) 
       (indent-for-tab-command--as-if-in-lisp-mode)))) 
#+end_src 

我想TAB鍵縮進代碼,因爲它會如果它是在緩衝在lisp模式下。

我需要的是:

  • 弄清楚光標是否是一個src塊中的一種方式。它在標題行上時不需要觸發,因爲在這種情況下,默認的組織摺疊應該發生。
  • 一種根據標題中指定的模式(本例中爲emacs-lisp)縮進代碼的方法。

組織可以根據模式語法突出顯示src塊,並且TAB掛鉤在那裏。這看起來可以。

+1

既然你正在編輯當前的代碼,會不會''C -c'''進入編輯模式就夠了? – gongzhitaao

+0

是的,我知道這個快捷方式,但編輯很多短片段時感覺太重了,比如在emacs config-org文件中。 – user103576

+0

可能對此有幫助[thread](http://lists.gnu.org/archive/html/emacs-orgmode/2012-02/msg00847.html) – gongzhitaao

回答

1

這裏有一個粗略的解決方案:

(defun indent-org-src-block-line() 
    "Indent the current line of emacs lisp code." 
    (interactive) 
    (let ((info (org-babel-get-src-block-info 'light))) 
    (when info 
     (let ((lang (nth 0 info))) 
     (when (string= lang "emacs-lisp") 
      (let ((indent-line-function 'lisp-indent-line)) 
      (indent-for-tab-command))))))) 

(add-to-list 'org-tab-before-tab-emulation-hook 
      'indent-org-src-block-line) 

它只能處理與Emacs口齒不清塊。我只用src塊進行了非縮進測試(不是org的默認值)。

一般而言,使一種模式在另一種模式下工作很困難 - 許多鍵盤命令會發生衝突。但是一些更基本的筆畫,比如縮進,換行,評論標籤(org會用#來評論lisp代碼,這是錯誤的)似乎可以使它們工作併產生最大的影響。

9

只是移動點進去的代碼塊,然後按抄送「

這會彈出elisp的模式緩衝,語法higlighting廣告都...

1
(defun my/org-cleanup() 
    (interactive) 
    (org-edit-special) 
    (indent-buffer) 
    (org-edit-src-exit)) 

應該這樣做,其中`縮進緩衝」被定義爲:

(defun indent-buffer() 
    (interactive) 
    (indent-region (point-min) (point-max))) 
15

由於Emacs的24.1您現在可以設置以下選項:

(setq org-src-tab-acts-natively t) 

...並且應該處理所有的src塊。

+0

這是我沒有想到會找到的最佳答案周:-) –

+0

當這樣做時,遇到'yasnippet fallback loop'的Yasnippet用戶可能會發現這個線程很有用:https://github.com/joaotavora/yasnippet/issues/761 –