2009-10-13 5 views
11

TextMateには、Open/Closeタグとctrl-shift-cmd-wでテキストを折り返して、Open/Closeタグ内の領域の各行を折り返すためにctrl-shift-wを使用できます。 emacs lispを使ってEmacsに同じ機能を実装するにはどうすればいいですか?TextMateのようなOpen/Closeタグの折り返し?

 
emacs 

becomes 

<p>emacs</p> 

そして...

 
emacs 
textmate 
vi 

becomes 

<li>emacs</li> 
<li>textmate</li> 
<li>vi</li> 

答えて

7

This answer(あなたは角括弧を使用するように変更したら)あなたの地域を包むためのソリューションを提供します。

このルーチンは、使用するタグが要求されますし、その型のオープン/クローズタグと地域内のすべての行にタグを付ける必要があります。

(defun my-tag-lines (b e tag) 
    "'tag' every line in the region with a tag" 
    (interactive "r\nMTag for line: ") 
    (save-restriction 
    (narrow-to-region b e) 
    (save-excursion 
     (goto-char (point-min)) 
     (while (< (point) (point-max)) 
     (beginning-of-line) 
     (insert (format "<%s>" tag)) 
     (end-of-line) 
     (insert (format "</%s>" tag)) 
     (forward-line 1))))) 

*注:*あなたはへtagを望んでいた場合常にliにしてから、タグ引数を削除し、\nMTag for line:をインタラクティブ呼び出しから削除し、挿入呼び出しを更新して、"<li\>"を挿入するだけです。

3

yasnippetは、Emacs用のTextmateのスニペット構文の特に優れた実装です。これで、Textmateのすべてのスニペットを読み込むことができます。あなたはそれをインストールする場合は、私が書いたこのスニペットは、あなたが欲しいものを行う必要があります。

(defun wrap-region-or-point-with-html-tag (start end) 
    "Wraps the selected text or the point with a tag" 
    (interactive "r") 
    (let (string) 
    (if mark-active 
     (list (setq string (buffer-substring start end)) 
      (delete-region start end))) 
    (yas/expand-snippet (point) 
         (point) 
         (concat "<${1:p}>" string "$0</${1:$(replace-regexp-in-string \" .*\" \"\" text)}>")))) 

(global-set-key (kbd "C-W") 'wrap-region-or-point-with-html-tag) 

はEDIT:。(オーケー、これはこれを固定での私の最後の試みであることを正確にTextMateののバージョンのようなものですそれも後の文字を無視します。終了タグのスペース)

ご迷惑をおかけして申し訳ありません。この関数は、領域内の各行を編集する必要があります。 sgml-mode deratives、マーク領域については

(defun wrap-lines-in-region-with-html-tag (start end) 
    "Wraps the selected text or the point with a tag" 
    (interactive "r") 
    (let (string) 
    (if mark-active 
     (list (setq string (buffer-substring start end)) 
       (delete-region start end))) 
    (yas/expand-snippet 
    (replace-regexp-in-string "\\(<$1>\\).*\\'" "<${1:p}>" 
     (mapconcat 
     (lambda (line) (format "%s" line)) 
     (mapcar 
     (lambda (match) (concat "<$1>" match "</${1:$(replace-regexp-in-string \" .*\" \"\" text)}>")) 
     (split-string string "[\r\n]")) "\n") t nil 1) (point) (point)))) 
5

、tagifyタイプM-x sgml-tag、あなたは(可能なHTML要素のリストを取得するには、プレスTAB)を使用したいタグ名を入力します。しかし、この方法ではリージョンの各行にタグを付けることはできません。キーボードマクロを記録することで回避できます。

0

Treyの回答のこの変形では、htmlも正しくインデントされます。

(defun wrap-lines-region-html (b e tag) "'tag' every line in the region with a tag" (interactive "r\nMTag for line: ") (setq p (point-marker)) (save-excursion (goto-char b) (while (< (point) p) (beginning-of-line) (indent-according-to-mode) (insert (format "<%s>" tag)) (end-of-line) (insert (format "</%s>" tag)) (forward-line 1))))

関連する問題