這可能是多一點魯棒性:
(defun toggle-quotes()
(interactive)
(save-excursion
(let ((start (nth 8 (syntax-ppss)))
(quote-length 0) sub kind replacement)
(goto-char start)
(setq sub (buffer-substring start (progn (forward-sexp) (point)))
kind (aref sub 0))
(while (char-equal kind (aref sub 0))
(setq sub (substring sub 1)
quote-length (1+ quote-length)))
(setq sub (substring sub 0 (- (length sub) quote-length)))
(goto-char start)
(delete-region start (+ start (* 2 quote-length) (length sub)))
(setq kind (if (char-equal kind ?\") ?\' ?\"))
(loop for i from 0
for c across sub
for slash = (char-equal c ?\\)
then (if (and (not slash) (char-equal c ?\\)) t nil) do
(unless slash
(when (member c '(?\" ?\'))
(aset sub i
(if (char-equal kind ?\") ?\' ?\")))))
(setq replacement (make-string quote-length kind))
(insert replacement sub replacement))))
它將使用緩衝區的語法信息來查找引號在開頭的引號字符串(即鑑於該字符串的引用),也將嘗試翻轉字符串引號內,除非它們是用反斜槓轉義 - 這看起來可能是一個常見的情況。
PS。我剛剛意識到你也希望它找到三重引號,所以她走了。
太棒了,謝謝:) – Tom 2013-03-22 23:59:45