本周末我显然有一个强大的痒,为我的Emacs环境添加了大量的功能。我可以自己做一些基础知识,并追捕其他东西,但是我找不到解决方案(而且我在Lisp上做得不够好)。
我经常处理HTML字符串,有时如果我将它们从一个块移动到另一个块(或者一种语言移动到另一个块),字符串会在不转义的地方被破坏。所以,我想要一个像这样的函数:
(defun smart-yank-in-string()
(if (stringp) ; Check if the point is in a string
; Check if the region created from the point to the end of the yank ends the string
; (and there is more yank left that isn't ";")
; Escape quotes for those locations recursively by prepending \
; Insert result into buffer @ mark
))
任何聪明的想法?我认为它涉及使用kill-new
存储变量并遍历它,但我在elisp中不够熟悉以解决它。
答案 0 :(得分:4)
接下来的yank应该插入转义字符串:
(defun escape-doublequotes-at-car-of-kill-ring ()
"Escape doublequotes in car of kill-ring "
(interactive)
(with-temp-buffer
(insert (car kill-ring))
(goto-char (point-min))
(while (search-forward "\"" nil t 1)
(replace-match "\\\\\""))
(kill-new (buffer-substring-no-properties (point-min) (point-max)))))
答案 1 :(得分:0)
这是另一种选择
(defun my-yank()
(interactive)
(if (nth 3 (syntax-ppss)) ;; Checks if inside a string
(insert-for-yank (replace-regexp-in-string "[\\\"]"
"\\\\\\&"
(current-kill 0)
t))
(call-interactively 'yank)))
调用时的命令检查点是否在字符串中,如果是,则它会撤消被拉出的文本,否则它会正常拉伸。一个缺点是你不能在字符串里面使用yank-pop
。
答案 2 :(得分:0)
也许您可以按照以下方式执行(保证100%非功能性代码):
(defun my-kill-quoted-string (start end)
"Like kill-region but takes of unquoting/requoting."
(interactive "r")
(let ((str (buffer-extract-substring start end)))
(if (nth 3 (syntax-ppss))
;; Unquote according to mode and context. E.g. we should unquote " and things like that in HTML.
(setq str (replace-regexp-in-string "\\\\\"" "\"" str)))
(put-text-property 0 (length str) 'yank-handler
(list (lambda (str)
(if (not (nth 3 (syntax-ppss)))
(insert str)
;; Requote according to mode and context.
(insert (replace-regexp-in-string "[\\\"]" "\\\\\\&" str))))))
(kill-new str)))