From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Miles Bader Newsgroups: gmane.emacs.devel Subject: Re: Combining face and map stuff Date: Sun, 03 Oct 2010 11:04:49 +0900 Message-ID: <87y6agxc8u.fsf@catnip.gol.com> References: <87lj6gsgyt.fsf@stupidchicken.com> Reply-To: Miles Bader NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1286071512 31750 80.91.229.12 (3 Oct 2010 02:05:12 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Sun, 3 Oct 2010 02:05:12 +0000 (UTC) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Oct 03 04:05:11 2010 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1P2Dwo-0001QX-NQ for ged-emacs-devel@m.gmane.org; Sun, 03 Oct 2010 04:05:10 +0200 Original-Received: from localhost ([127.0.0.1]:42640 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P2Dwh-000057-OC for ged-emacs-devel@m.gmane.org; Sat, 02 Oct 2010 22:04:59 -0400 Original-Received: from [140.186.70.92] (port=56143 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1P2Dwb-000051-Nv for emacs-devel@gnu.org; Sat, 02 Oct 2010 22:04:54 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1P2Dwa-0006X4-8p for emacs-devel@gnu.org; Sat, 02 Oct 2010 22:04:53 -0400 Original-Received: from smtp12.dentaku.gol.com ([203.216.5.74]:57166) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1P2Dwa-0006Wx-1R; Sat, 02 Oct 2010 22:04:52 -0400 Original-Received: from 218.231.154.125.eo.eaccess.ne.jp ([218.231.154.125] helo=catnip.gol.com) by smtp12.dentaku.gol.com with esmtpa (Dentaku) id 1P2DwY-0007VW-DO; Sun, 03 Oct 2010 11:04:50 +0900 Original-Received: by catnip.gol.com (Postfix, from userid 1000) id 9B116DF8C; Sun, 3 Oct 2010 11:04:49 +0900 (JST) System-Type: x86_64-unknown-linux-gnu In-Reply-To: (Lars Magne Ingebrigtsen's message of "Sat, 02 Oct 2010 18:41:21 +0200") Original-Lines: 53 X-Virus-Scanned: ClamAV GOL (outbound) X-Abuse-Complaints: abuse@gol.com X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:131270 Archived-At: 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.