Emacs根据font-face从字符串中删除文本

时间:2013-08-01 16:29:07

标签: emacs elisp substring font-lock emacs-faces

我通过拨打buffer-substring获得了一个字符串。有没有办法根据face设置从该字符串中删除文本?作为一个随机的例子:

(save-excursion 
  (let ((end (point)))
(ignore-errors 
  (while (not (looking-at "[^][ \t\r\n(){}]+:"))
    (backward-sexp)))

不知道SO会如何强调这一点,但在Emacs中,save-excursionletignore-errorswhile都被突出显示为关键字和正则表达式参数将looking-at突出显示为字符串。 buffer-substring的返回值类似于

#("    (save-excursion 
      (let ((end (point)))
    (ignore-errors 
      (while (not (looking-at \"[^][ \\t\\r\\n(){}]+:\"))
        (backward-sexp)))" 0 5 (fontified t) 5 19 (fontified t face font-lock-keyword-face) 19 28 (fontified t) 28 31 (fontified t face font-lock-keyword-face) 31 50 (fontified t) 50 63 (fontified t face font-lock-keyword-face) 63 65 (fontified t) 65 69 (fontified t) 69 74 (fontified t face font-lock-keyword-face) 74 75 (fontified t) 75 92 (fontified t) 92 94 (fontified t face font-lock-string-face) 94 95 (fontified t face (font-lock-negation-char-face font-lock-string-face)) 95 112 (fontified t face font-lock-string-face) 112 115 (fontified t) 115 137 (fontified t))

鉴于示例字符串,我将如何剥离具有face font-lock-keyword-face的所有内容?也就是说,我希望能够做类似

的事情
(foo-bar *that-region* 'font-lock-keyword-face)

并让它返回

(
  ( ((end (point)))
( 
  ( (not (looking-at "[^][ \t\r\n(){}]+:"))
    (backward-sexp)))

1 个答案:

答案 0 :(得分:2)

您必须使用text-property-any查找违规面孔的起点,并使用text-property-not-all查找结束面。然后你需要迭代。

(defun kill-text-with-property (start end property value &optional object)
  "Delete the text with the PROPERTY in the part of OBJECT from START and END."
  (interactive "r\nsProperty: \nsValue: ")
  (let ((delenda ()) (here start))
    ;; collect the list of regions to kill
    ;; note that delenda contains the regions in the reverse order so that
    ;; deleting the later ones do not affect the boundaries of the ealier ones
    (while (< here end)
      (let ((beg (text-property-any here end property value object))
            (stop (and beg (text-property-not-all beg end property value object) end)))
        (if (null beg)
            (setq here end)     ; done
          (push (cons beg stop) delenda)
          (setq here stop))))
    (if (stringp object)
        ;; collect the complements of delenda into a new string
        (....)
      ;; buffer: kill the delenda regions
      (with-current-buffer (or object (current-buffer))
        (dolist (pair delenda)
          (kill-region (car pair) (cdr pair)))))))