query-replace-regexp に関する質問です。
(defun foo ()
(interactive)
(query-replace-regexp "\\(\\w\\)_\\(\\w\\)" "\\1\\2")))
のような関数を用意して実行することにより、バッファ内にある
aa_bcd, foo_bar
のような文字列を
aabcd, foobar
にできますが、同時に _ の後の1文字を大文字に変換して
aaBcd, fooBar
のようになるようにするには、先の関数 foo をどのように書き変えれば
よいでしょうか。
よろしくお願いします。
--
椎葉 充晴 (Michiharu CBAR)
椎葉さん> query-replace-regexp に関する質問です。
椎葉さん> (defun foo ()
椎葉さん> (interactive)
椎葉さん> (query-replace-regexp "\\(\\w\\)_\\(\\w\\)" "\\1\\2")))
[...]
椎葉さん> aaBcd, fooBar
椎葉さん> のようになるようにするには、先の関数 foo をどのように書き変
椎葉さん> えればよいでしょうか。
query-replace-regexp だけではたぶん無理です。こんな感じでしょうか?
(defun foo2 ()
(interactive)
(let (old new)
(while (re-search-forward "\\(\\w+\\)_\\(\\w+\\)" nil t)
(setq old (match-string 0)
new (concat (match-string 1)
(capitalize (match-string 2))))
(if (y-or-n-p (concat "Query replacing \"" old
"\" with \"" new "\" "))
(replace-match new)))
(message "done")))
--
山岡
ありがとうございました。
山岡様> query-replace-regexp だけではたぶん無理です。こんな感じでしょうか?
山岡様> (defun foo2 ()
山岡様> (interactive)
山岡様> (let (old new)
山岡様> (while (re-search-forward "\\(\\w+\\)_\\(\\w+\\)" nil t)
山岡様> (setq old (match-string 0)
山岡様> new (concat (match-string 1)
山岡様> (capitalize (match-string 2))))
山岡様> (if (y-or-n-p (concat "Query replacing \"" old
山岡様> "\" with \"" new "\" "))
山岡様> (replace-match new)))
山岡様> (message "done")))
foo_bar_aho などに対応するために、
(replace-match new)
を
(progn
(replace-match new)
(backward-char 1))
として使うことにしました。どうもありがとうございました。
(defun y-or-n-p-with-replace-highlight (beg end form &rest args)
(unwind-protect
(progn
(pop-to-buffer (current-buffer))
(replace-highlight beg end)
(y-or-n-p (apply 'format form args)))
(replace-dehighlight)))
何てものをあらかじめ定義しておいて
山岡様> (if (y-or-n-p (concat "Query replacing \"" old
山岡様> "\" with \"" new "\" "))
.の代りに
(if (y-or-n-p-with-replace-highlight
(match-beginning 0) (match-end 0)
"Quely replaceing \"%s\" with \"%s\" "
old new)
などとするとちょっとだけ幸せかも知れません。
--
鈴木圭一 / kei...@nanap.org
PGP finger print (DH/DSS)
0B32 B37E 6DE9 3BC1 68A4 4089 7AAF 2B03 ECBD 614B