Skip to content

Commit c11aded

Browse files
vspinubbatsov
authored andcommitted
New utility function cider-add-face
1 parent 425761b commit c11aded

File tree

2 files changed

+58
-0
lines changed

2 files changed

+58
-0
lines changed

cider-util.el

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,30 @@ Unless you specify a BUFFER it will default to the current one."
309309
(define-button-type 'cider-plain-button
310310
'face nil)
311311

312+
(defun cider-add-face (regexp face &optional foreground-only sub-expr object)
313+
"Propertize all occurrences of REGEXP with FACE.
314+
If FOREGROUND-ONLY is non-nil, change only the foreground of matched
315+
regions. SUB-EXPR is a sub-expression of REGEXP to be
316+
propertized (defaults to 0). OBJECT is an object to be
317+
propertized (defaults to current buffer)."
318+
(setq sub-expr (or sub-expr 0))
319+
(when (and regexp face)
320+
(let ((beg 0)
321+
(end 0))
322+
(with-current-buffer (or (and (bufferp object) object)
323+
(current-buffer))
324+
(while (if (stringp object)
325+
(string-match regexp object end)
326+
(re-search-forward regexp nil t))
327+
(setq beg (match-beginning sub-expr)
328+
end (match-end sub-expr))
329+
(if foreground-only
330+
(let ((face-spec (list (cons 'foreground-color
331+
(face-attribute face :foreground nil t)))))
332+
(font-lock-prepend-text-property beg end 'face face-spec object))
333+
(put-text-property beg end 'face face object)))))))
334+
335+
312336
;;; Colors
313337

314338
(defun cider-scale-color (color scale)

test/cider-util-tests.el

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,37 @@
218218
:not :to-be-truthy)
219219
(expect (cider-ansi-color-string-p "'an-ansi-str")
220220
:not :to-be-truthy)))
221+
222+
(describe "cider-add-face"
223+
:var (str)
224+
225+
(before-each
226+
(setq str "aaa bbb\n cccc\n dddd"))
227+
228+
(describe "works in strings"
229+
(it "fontifies with correct face"
230+
(cider-add-face "c+" 'font-lock-comment-face nil nil str)
231+
(expect (get-pos-property 1 'face str)
232+
:to-be nil)
233+
(expect (get-pos-property 10 'face str)
234+
:to-be 'font-lock-comment-face))
235+
(it "fontifies foreground with correct face"
236+
(cider-add-face "b+" 'font-lock-comment-face t nil str)
237+
(expect (get-pos-property 5 'face str)
238+
:to-equal `((foreground-color . ,(face-attribute 'font-lock-comment-face
239+
:foreground nil t)))))
240+
(it "fontifies sub-expression correctly"
241+
(cider-add-face "\\(a\\)aa" 'font-lock-comment-face nil 1 str)
242+
(expect (get-pos-property 0 'face str)
243+
:to-be 'font-lock-comment-face)
244+
(expect (get-pos-property 1 'face str)
245+
:to-be nil)))
246+
247+
(describe "works in buffers"
248+
(it "fontifies with correct face"
249+
(with-temp-buffer
250+
(insert "aaa bbb\n cccc\n ddddd")
251+
(goto-char 1)
252+
(cider-add-face "c+" 'font-lock-comment-face)
253+
(expect (get-pos-property 11 'face)
254+
:to-be 'font-lock-comment-face)))))

0 commit comments

Comments
 (0)