SQLのWHERE条件を作る

A0001
A0002
A0003

といった文字を

IN ('A0001', 'A0002', 'A0003')

のように整形するelispを書いてみた。

(defun sql-where-in (s e)
  (interactive "r")                     ; リージョンを扱うコマンド
  (save-excursion
    (save-restriction           ; 設定されているナロイングの範囲を記録
      (narrow-to-region s e)
      (beginning-of-buffer)
      (while (re-search-forward "^" nil t)
        (replace-match "'"))
      (beginning-of-buffer)
      (while (re-search-forward "\n" nil t)
        (replace-match "',"))
      (beginning-of-buffer)
      (re-search-forward "^" nil t)
      (replace-match "IN (")
      (re-search-forward ",'$" nil t)
      (replace-match ")")
      (widen))))

Emacs Lispテクニックバイブル

Emacs Lispテクニックバイブル