2014-03-02 15 views
1

本週末我顯然有一個強大的癢癢,爲我的Emacs環境增加了很多功能。我可以自己做一些基礎知識,並搜索其他內容,但是我一直無法找到解決方案(而且我自己也不夠好)。在emacs中自動轉義抽取的字符串

我經常使用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解決它。

+1

你混淆刻度點看來。你能添加一個最簡單的例子嗎?或者檢查一下http://abo-abo.github.io/lispy/#sec-3-25是否是你想要的。 –

+0

哎呀,對不起,修正了上面的問題。我的意思是說。 –

回答

3

接着抽出應插入轉義字符串:

(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))))) 
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

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)))