2013-03-22 49 views
4

我正在尋找一個emacs命令,該命令將在該點下的字符串上切換周圍的引號字符,例如,用光標在字符串中的「酒吧」,命中關鍵之間改變:有沒有辦法在emacs中的單引號和雙引號之間切換字符串?

foo = 'bar' <---> foo = "bar" 

獎勵積分它將:

  • 手柄切換Python的三重引號字符串(''' < - - >"""

  • 自動改變反斜線字符串作爲適當的內部逸出。

例如,

foo = 'bar "quote"' <---> foo = "bar \"quote\"" 

回答

4

這裏有一個快速的黑客,讓你開始:

(defun toggle-quotes() 
    "Toggle single quoted string to double or vice versa, and 
    flip the internal quotes as well. Best to run on the first 
    character of the string." 
    (interactive) 
    (save-excursion 
    (re-search-backward "[\"']") 
    (let* ((start (point)) 
      (old-c (char-after start)) 
      new-c) 
     (setq new-c 
      (case old-c 
       (?\" "'") 
       (?\' "\""))) 
     (setq old-c (char-to-string old-c)) 
     (delete-char 1) 
     (insert new-c) 
     (re-search-forward old-c) 
     (backward-char 1) 
     (let ((end (point))) 
     (delete-char 1) 
     (insert new-c) 
     (replace-string new-c old-c nil (1+ start) end))))) 

功能交換內部引號相反的,這是接近獎金2

+0

太棒了,謝謝:) – Tom 2013-03-22 23:59:45

6

這可能是多一點魯棒性:

(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。我剛剛意識到你也希望它找到三重引號,所以她走了。

-2

這是我爲JavaScript編寫的一個函數,可能有幫助嗎?

function swap_str(e, r, t) { 
 
    return e = e.split(r).join("WHAK_a_SWAP"), e = e.split(t).join("WHAK_b_SWAP"), e = e.split("WHAK_a_SWAP").join(t), 
 
    e = e.split("WHAK_b_SWAP").join(r); 
 
} 
 
//test 1 
 
var str = 'this is "test" of a \'test\' of swapping strings'; 
 
var manipulated = swap_str(str,"'",'"'); 
 
document.writeln(manipulated) 
 
//test 2 
 
manipulated = swap_str(manipulated,"'",'"'); 
 
document.writeln('<hr>'+manipulated)

0

這裏的東西更強大,因爲它不會刪除引號之間的整個文本(這樣做可以防止save-excursion從保持它在哪裏的地步,這是一種痛苦) 。還處理(聯合)反斜槓嵌套引號。

(defun toggle-quotes() 
    (interactive) 
    (let* ((beg (nth 8 (syntax-ppss))) 
     (orig-quote (char-after beg)) 
     (new-quote (case orig-quote 
         (?\' ?\") 
         (?\" ?\')))) 
    (save-restriction 
    (widen) 
    (save-excursion 
     (catch 'done 
     (unless new-quote 
      (message "Not inside a string") 
      (throw 'done nil)) 
     (goto-char beg) 
     (delete-char 1) 
     (insert-char new-quote) 
     (while t 
      (cond ((eobp) 
       (throw 'done nil)) 
       ((= (char-after) orig-quote) 
       (delete-char 1) 
       (insert-char new-quote) 
       (throw 'done nil)) 
       ((= (char-after) ?\\) 
       (forward-char 1) 
       (when (= (char-after) orig-quote) 
        (delete-char -1)) 
       (forward-char 1)) 
       ((= (char-after) new-quote) 
       (insert-char ?\\) 
       (forward-char 1)) 
       (t (forward-char 1))))))))) 
相關問題