all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Miles Bader <miles@gnu.org>
To: emacs-devel@gnu.org
Subject: Re: Combining face and map stuff
Date: Sun, 03 Oct 2010 11:04:49 +0900	[thread overview]
Message-ID: <87y6agxc8u.fsf@catnip.gol.com> (raw)
In-Reply-To: <m3eic8r1hq.fsf@quimbies.gnus.org> (Lars Magne Ingebrigtsen's message of "Sat, 02 Oct 2010 18:41:21 +0200")

You can also have face properties that are lists of
faces/face-attributes, and add or remove stuff from these.

Some example functions below, `add-to-face-property' and
`remove-from-face-property', with which you can do stuff like:

  (add-to-face-property 'variable-pitch START END)
  (add-to-face-property '(:weight bold) (+ START 5) (- END 10))
  ...
  (remove-from-face-property 'variable-pitch START END)
  ...


(defun single-face-p (face)
  "Return non-nil if FACE seems to be a single face or face-attribute list
\(as oppposed to a list of faces/face-attribute-lists)."
  (or (symbolp face)			; face-symbol
      (and (listp face)
	   (keywordp (car face))	; (:attr val ...)
	   (keywordp (cadr face)))))	; (face-symbol :attr val ...)

(defun add-to-face-property (face start end)
  "Add FACE to the face text-property of the region from START to END.
Any existing face text-properties are preserved by adding FACE to
the beginning of a list, making the existin value into a list if
necessary first.  It can be removed with `remove-from-face-property'."
  (while (< start end)
    (let ((next (next-char-property-change start end)))
      (let ((cur (get-char-property start 'face)))
        (when (single-face-p cur)
	  (setq cur (list cur)))
        (push face cur)
	(put-text-property start next 'face cur))
      (setq start next))))

(defun remove-from-face-property (face start end)
  "Remove FACE from the face text-property of the region from START to END.
The assumption is that it was added previously by `add-to-face-property'."
  (while (< start end)
    (let ((next (next-char-property-change start end)))
      (let ((cur (get-char-property start 'face)))
	(when (single-face-p cur)
	  (setq cur (list cur)))
	(when (member face cur)
	  (put-text-property start next 'face (remove face cur))))
      (setq start next))))


-miles

-- 
History, n. An account mostly false, of events mostly unimportant, which are
brought about by rulers mostly knaves, and soldiers mostly fools.



  reply	other threads:[~2010-10-03  2:04 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-10-02 14:21 Combining face and map stuff Lars Magne Ingebrigtsen
2010-10-02 16:21 ` Chong Yidong
2010-10-02 16:41   ` Lars Magne Ingebrigtsen
2010-10-03  2:04     ` Miles Bader [this message]
2010-10-03  4:16     ` Chong Yidong
2010-10-03 13:08       ` Lars Magne Ingebrigtsen
2010-10-03 23:37 ` Stefan Monnier
2010-10-04  7:56   ` Eli Zaretskii
2010-10-04  9:03     ` Miles Bader
2010-10-04  9:42       ` Eli Zaretskii
2010-10-05  0:12         ` Miles Bader
2010-10-05 23:43           ` Stefan Monnier
2010-10-06  3:57             ` Eli Zaretskii
2010-10-07  7:40               ` Stefan Monnier
2010-10-07 13:56                 ` Eli Zaretskii
2010-10-07 18:08                   ` Stefan Monnier
2010-10-12 19:09                     ` Eli Zaretskii
2010-10-12 20:23                       ` Stefan Monnier
2010-10-13 11:41                         ` Eli Zaretskii
2010-10-13 12:42                           ` Stefan Monnier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87y6agxc8u.fsf@catnip.gol.com \
    --to=miles@gnu.org \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.