我想刪除附加文本之間的特殊字符,如:[「{'<(等..這樣我可以刪除像」這是一個非常......長文本「與文本一個簡單的鍵盤快捷鍵我在尋找一些已經存在的模式,但是我沒有找到任何類似的東西,所以我創建了一些lisp代碼,在大多數情況下都能很好地執行,但是它在所有情況下都不能正常工作。我有下面的文本條目,我把光標放在「^」的位置,然後我會刪除所有的文字「,但它不起作用:刪除Emacs中的封閉文本
」aaaaa]> [more text] aaaaa「
------------^
我的Lisp代碼如下:
;; returns the enclosing character for the character "c"
(defun get-enc-char (c) (cond
((string= c "(") ")")
((string= c "[") "]")
((string= c "{") "}")
((string= c ">") "<")
((string= c "<") ">")
((string= c "'") "'")
((string= c "\"") "\"")
(t nil)
)
)
(defun delete-enclosed-text()
"Delete texts between any pair of delimiters."
(interactive)
(save-excursion
(let (p1 p2 mychar)
; look for one of those characters and store the cursor position
(skip-chars-backward "^([\'\"><{") (setq p1 (point))
; store the char at this point, look for its enclosed char and advance
; the cursor newly (this done to avoid the cases when the char and
; its enclosed-char are the same like " or ' chars.
(backward-char 1) (setq mychar (thing-at-point 'char)) (forward-char 1)
; look forward for the enclosed char
(skip-chars-forward (concatenate 'string "^" (get-enc-char mychar))) (setq p2 (point))
; only delete the region if we found the enclosed character
(if (looking-at "[\]\}\"\'\)<>]") (kill-region p1 p2)))))
它是如何失敗? – sds
這似乎是因爲您正在尋找所有的括號類型,而不是包含您要刪除的特定區域的特定類型。所以當你在雙引號字符串中有一個尖括號時,它會變得困惑。嘗試確定支架類型,只查找那些。 – jpkotta
FWIW,我通常使用'expand-region'包來選擇(也許刪除)這樣的區域。但它肯定需要更多擊鍵。 – jpkotta