From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Lennart Borgman Newsgroups: gmane.emacs.devel Subject: Re: PATCH: num3-mode for highlighting groups of digits in long numbers Date: Wed, 26 Sep 2012 16:58:52 +0200 Message-ID: References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/alternative; boundary=20cf307d03e49dfc5604ca9c0ffa X-Trace: ger.gmane.org 1348671555 26572 80.91.229.3 (26 Sep 2012 14:59:15 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 26 Sep 2012 14:59:15 +0000 (UTC) Cc: Felix Lee , Noah Lavine , Stefan Monnier , Emacs-Devel devel To: Michal Nazarewicz Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Sep 26 16:59:19 2012 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1TGt53-0004ia-Sf for ged-emacs-devel@m.gmane.org; Wed, 26 Sep 2012 16:59:18 +0200 Original-Received: from localhost ([::1]:44693 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TGt4y-0000d4-V6 for ged-emacs-devel@m.gmane.org; Wed, 26 Sep 2012 10:59:12 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:57831) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TGt4q-0000c4-Pu for emacs-devel@gnu.org; Wed, 26 Sep 2012 10:59:11 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1TGt4g-0004As-2s for emacs-devel@gnu.org; Wed, 26 Sep 2012 10:59:04 -0400 Original-Received: from mail-vc0-f169.google.com ([209.85.220.169]:53477) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1TGt4f-0004Ac-TC for emacs-devel@gnu.org; Wed, 26 Sep 2012 10:58:54 -0400 Original-Received: by vcbfl17 with SMTP id fl17so894969vcb.0 for ; Wed, 26 Sep 2012 07:58:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=Yt/ipLwt6Utl0WommMBhzA2lXOzdUjIchwUDFQMA1y0=; b=VqvVpe/yAfAH/4cEWTem5Z2MbsTZT3Oqfxn4fKRy9nKZ4dGSXPnK8JFfKtDJKRLCDD kw3QdJ+DYkSMgYxA6Cc0IVfHNTY33GWmBSRqAHlHdu4YkwFAxDjq6+zmUVPVZbXcHhOu mEW4CTvR4SUP5ihrRLHgGAI13kt8RfoBGHsJkml95NsxwTLsWIhqZXXB38sp1SiM7I1s yNzgh02utE4KNFO7dPaUxFI5ydy47NNJgr6F0sC5BTvh4j+hJjoHpM2e6iNvTFOnO+X8 3g3YIWggegjVtCs1ClDZ4mwBesZKcHNMjWwcZomLfAiITfIpcbzrEHeVHKcRdT7Ag79/ qoTA== Original-Received: by 10.52.69.72 with SMTP id c8mr376057vdu.37.1348671532563; Wed, 26 Sep 2012 07:58:52 -0700 (PDT) Original-Received: by 10.59.6.3 with HTTP; Wed, 26 Sep 2012 07:58:52 -0700 (PDT) Original-Received: by 10.59.6.3 with HTTP; Wed, 26 Sep 2012 07:58:52 -0700 (PDT) In-Reply-To: X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 209.85.220.169 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:153589 Archived-At: --20cf307d03e49dfc5604ca9c0ffa Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Just for your information: There is such a mode already in nXhtml. I called it ocr-users-mode since that I mostly use it for. On Sep 26, 2012 3:06 PM, "Michal Nazarewicz" wrote: > On Wed, Sep 26 2012, Noah Lavine wrote: > > I don't usually post, but something confuses me about this patch > > (which otherwise looks awesome, by the way): > > > > in the doc string for num3-mode, you say > > > > `num3-face-even' face (which is empty by > > default) is the one used for the group closest to the decimal point. > > > > But looking at the faces, it appears that num3-face-odd is '((t)), and > > num3-face-even has lots of attributes. Am I misunderstanding the > > documentation, or did you switch the faces? > > Sorry, you're absolutely right. It's mistake in documentation in my > part. Attached is fixed version of the file. > > -- > Best regards, _ _ > .o. | Liege of Serenely Enlightened Majesty of o' \,=3D./ `o > ..o | Computer Science, Micha=C5=82 =E2=80=9Cmina86=E2=80=9D Nazarewicz = (o o) > ooo +------------------ooO--(_)--Ooo-- > > ---------- >% ---------------------------------------------------------- > > ;;; num3-mode.el --- highlight groups of digits in long numbers -*- > lexical-binding: t -*- > > ;; Copyright (C) 2012 Free Software Foundation, Inc. > > ;; Author: Felix Lee , Michal Nazarewicz < > mina86@mina86.com> > ;; Maintainer: Michal Nazarewicz > ;; Keywoards: faces, minor-mode > > ;; GNU Emacs is free software: you can redistribute it and/or modify > ;; it under the terms of the GNU General Public License as published by > ;; the Free Software Foundation, either version 3 of the License, or > ;; (at your option) any later version. > > ;; GNU Emacs is distributed in the hope that it will be useful, > ;; but WITHOUT ANY WARRANTY; without even the implied warranty of > ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the > ;; GNU General Public License for more details. > > ;; You should have received a copy of the GNU General Public License > ;; along with GNU Emacs. If not, see . > > ;;; Commentary: > > ;; Num3 is a minor mode that makes long numbers more readable by > ;; highlighting groups of 3 (customisable) decimal digits or 4 hex > ;; digits when font-lock is on. Highlighting alternates between two > ;; faces that can be customised. > > ;;; Usage: > > ;; M-x num3-mode toggle for current buffer. > ;; M-x global-num3-mode toggle for all buffers. > ;; > ;; Or add the following to your ~/.emacs file: > ;; (load "path/to/num3") > ;; (global-num3-mode) > > ;;; Code: > > (defgroup num3 nil > "Num3 is a minor mode that makes long numbers more readable by > highlighting groups of decimal digits or 4 hex digits when > font-lock is on." > :group 'text) > > (defcustom num3-group-size 3 > "Number of digits to group in decimal numbers.") > > (defcustom num3-threshold 5 > "Number must be at least that long to start highlighting.") > > (defface num3-face-odd > '((t)) > "Face to add for odd groups of digits." > :group 'num3) > > (defface num3-face-even > '((t :underline t :weight bold :background "#eeeeee")) > "Face to add for even groups of digits. > The default face uses redundant signaling, because this is in > addition to any other font-lock highlighting." > :group 'num3) > > ;;; Implementation: > > (require 'font-lock) > > ;;;###autoload > (define-minor-mode num3-mode > "Toggle num3 minor mode in the current buffer. > Num3 minor mode makes long numbers more readable by highlighting > groups of digits when font-lock mode is on. > > If a number is longer than `num3-threshold', the mode will split > it into a group of `num3-group-size' (if number is decimal) or > 4 (if number is hexadecimal) digits. Hexadecimal number is > detected as one starting with 0x, 0X or #. > > With decimal numbers, fractions are recognised as well and > grouped from the beginning rathar then from end. For instance, > with group size of 3, a number \"12345.12345\" will be split into > groups as follows: \"12|345.123|45\". Fractions without integer > part are also recognised, eg. \".12345\". > > The groups are highlighted alternately using `num3-face-odd' and > `num3-face-even' faces. `num3-face-odd' face (which is empty by > default) is the one used for the group closest to the decimal point, > ie. groups are counted starting with one outwards from the (place > where) decimal point (would be) is." > nil " num3" nil > (if num3-mode > (unless (assoc '-num3-matcher font-lock-keywords) > (font-lock-add-keywords nil '(-num3-matcher) 'append)) > (font-lock-remove-keywords nil '(-num3-matcher))) > (when font-lock-mode > (font-lock-fontify-buffer))) > > ;;;###autoload > (define-globalized-minor-mode global-num3-mode num3-mode turn-on-num3-mod= e) > > ;;;###autoload > (defun turn-on-num3-mode () > "Turn `num3-mode' on if it's not enabled." > (unless num3-mode (num3-mode t))) > > (defconst -num3-number-re > (concat "\\(?:0[xX]\\|#\\)\\([0-9a-fA-F]+\\)" ; 1 =3D hexadecimal > "\\|\\([0-9]+\\)" ; 2 =3D decimal > "\\|\\.\\([0-9]+\\)")) ; 3 =3D fraction > > (defun -num3-matcher (lim) > "Function used as a font-lock-keywoard handler used in `num3-mode'. > Performs fontification of numbers from point to LIM." > (save-excursion > (while (re-search-forward -num3-number-re lim t) > (-num3-int (match-beginning 1) (match-end 1) 4) > (-num3-int (match-beginning 2) (match-end 2) num3-group-size) > (-num3-frac (match-beginning 3) (match-end 3) num3-group-size))) > nil) > > (defun -num3-int (lo hi n) > "Highlight groups of digits in a long number. > LO and HI arguments specify the range where the number is > located. If the length of that region exceeds `num3-threshold', > the function will split it into groups of N digits and fontify > tham alternately using `num3-face-odd' and `num3-face-even' > faces. Grouping is done from the end, eg. (12)(345)." > (when (and lo (>=3D (- hi lo) num3-threshold)) > (let (even) > (while (< lo hi) > (-num3-put even (max lo (- hi n)) hi) > (setq hi (- hi n) even (not even)))))) > > (defun -num3-frac (lo hi n) > "Highlight groups of digits in a long number. > LO and HI arguments specify the range where the number is > located. If the length of that region exceeds `num3-threshold', > the function will split it into groups of N digits and fontify > tham alternately using `num3-face-odd' and `num3-face-even' > faces. Grouping is done from the beginning, eg. (123)(45)." > (when (and lo (>=3D (- hi lo) num3-threshold)) > (let (even) > (while (< lo hi) > (-num3-put even lo (min hi (+ lo n))) > (setq lo (+ lo n) even (not even)))))) > > (defun -num3-put (even lo hi) > "Add font lock text property to highlight a single group of digit. > Use `num3-face-odd' if EVEN is nil and `num3-face-even' if EVEN is > non-nil. The region the face is set to is from LO to HI." > (font-lock-append-text-property lo hi 'face > (if even 'num3-face-even > 'num3-face-odd))) > > (provide 'num3) > > ;;; num3.el ends here > > > --20cf307d03e49dfc5604ca9c0ffa Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable

Just for your information: There is such a mode already in nXhtml. I cal= led it ocr-users-mode since that I mostly use it for.

On Sep 26, 2012 3:06 PM, "Michal Nazarewicz= " <mpn@google.com> wrote:<= br type=3D"attribution">
On Wed, Sep 26 2012, Noah Lavine wrote:
> I don't usually post, but something confuses me about this patch > (which otherwise looks awesome, by the way):
>
> in the doc string for num3-mode, you say
>
> =C2=A0`num3-face-even' face (which is empty by
> default) is the one used for the group closest to the decimal point. >
> But looking at the faces, it appears that num3-face-odd is '((t)),= and
> num3-face-even has lots of attributes. Am I misunderstanding the
> documentation, or did you switch the faces?

Sorry, you're absolutely right. =C2=A0It's mistake in documentation= in my
part. =C2=A0Attached is fixed version of the file.

--
Best regards, =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 = =C2=A0 _ =C2=A0 =C2=A0 _
.o. | Liege of Serenely Enlightened Majesty of =C2=A0 =C2=A0 =C2=A0o' \= ,=3D./ `o
..o | Computer Science, =C2=A0Micha=C5=82 =E2=80=9Cmina86=E2=80=9D Nazarewi= cz =C2=A0 =C2=A0(o o)
ooo +----<email/xmpp: mpn@google.com>--------------ooO--(_)--Ooo--

---------- >% ----------------------------------------------------------=

;;; num3-mode.el --- highlight groups of digits in long numbers =C2=A0-*- l= exical-binding: t -*-

;; Copyright (C) 2012 Free Software Foundation, Inc.

;; Author: Felix Lee <
felix8a@gmail= .com>, Michal Nazarewicz <mi= na86@mina86.com>
;; Maintainer: Michal Nazarewicz <m= ina86@mina86.com>
;; Keywoards: faces, minor-mode

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. =C2=A0See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. =C2=A0If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; Num3 is a minor mode that makes long numbers more readable by
;; highlighting groups of 3 (customisable) decimal digits or 4 hex
;; digits when font-lock is on. =C2=A0Highlighting alternates between two ;; faces that can be customised.

;;; Usage:

;; =C2=A0 =C2=A0 M-x num3-mode =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 toggle fo= r current buffer.
;; =C2=A0 =C2=A0 M-x global-num3-mode =C2=A0 =C2=A0toggle for all buffers.<= br> ;;
;; Or add the following to your ~/.emacs file:
;; =C2=A0 =C2=A0 (load "path/to/num3")
;; =C2=A0 =C2=A0 (global-num3-mode)

;;; Code:

(defgroup num3 nil
=C2=A0 "Num3 is a minor mode that makes long numbers more readable by<= br> highlighting groups of decimal digits or 4 hex digits when
font-lock is on."
=C2=A0 :group 'text)

(defcustom num3-group-size 3
=C2=A0 "Number of digits to group in decimal numbers.")

(defcustom num3-threshold 5
=C2=A0 "Number must be at least that long to start highlighting."= )

(defface num3-face-odd
=C2=A0 '((t))
=C2=A0 "Face to add for odd groups of digits."
=C2=A0 :group 'num3)

(defface num3-face-even
=C2=A0 '((t :underline t :weight bold :background "#eeeeee"))=
=C2=A0 "Face to add for even groups of digits.
The default face uses redundant signaling, because this is in
addition to any other font-lock highlighting."
=C2=A0 :group 'num3)

;;; Implementation:

(require 'font-lock)

;;;###autoload
(define-minor-mode num3-mode
=C2=A0 "Toggle num3 minor mode in the current buffer.
Num3 minor mode makes long numbers more readable by highlighting
groups of digits when font-lock mode is on.

If a number is longer than `num3-threshold', the mode will split
it into a group of `num3-group-size' (if number is decimal) or
4 (if number is hexadecimal) digits. =C2=A0Hexadecimal number is
detected as one starting with 0x, 0X or #.

With decimal numbers, fractions are recognised as well and
grouped from the beginning rathar then from end. =C2=A0For instance,
with group size of 3, a number \"12345.12345\" will be split into=
groups as follows: \"12|345.123|45\". =C2=A0Fractions without int= eger
part are also recognised, eg. \".12345\".

The groups are highlighted alternately using `num3-face-odd' and
`num3-face-even' faces. =C2=A0`num3-face-odd' face (which is empty = by
default) is the one used for the group closest to the decimal point,
ie. groups are counted starting with one outwards from the (place
where) decimal point (would be) is."
=C2=A0 nil " num3" nil
=C2=A0 (if num3-mode
=C2=A0 =C2=A0 =C2=A0 (unless (assoc '-num3-matcher font-lock-keywords)<= br> =C2=A0 =C2=A0 =C2=A0 =C2=A0 (font-lock-add-keywords nil '(-num3-matcher= ) 'append))
=C2=A0 =C2=A0 (font-lock-remove-keywords nil '(-num3-matcher)))
=C2=A0 (when font-lock-mode
=C2=A0 =C2=A0 (font-lock-fontify-buffer)))

;;;###autoload
(define-globalized-minor-mode global-num3-mode num3-mode turn-on-num3-mode)=

;;;###autoload
(defun turn-on-num3-mode ()
=C2=A0 "Turn `num3-mode' on if it's not enabled."
=C2=A0 (unless num3-mode (num3-mode t)))

(defconst -num3-number-re
=C2=A0 (concat =C2=A0 =C2=A0"\\(?:0[xX]\\|#\\)\\([0-9a-fA-F]+\\)"= =C2=A0; 1 =3D hexadecimal
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\\|\\([0-9]+\\)" =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 ;= 2 =3D decimal
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 "\\|\\.\\([0-9]+\\)")) =C2=A0 = =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0; 3 =3D fract= ion

(defun -num3-matcher (lim)
=C2=A0 "Function used as a font-lock-keywoard handler used in `num3-mo= de'.
Performs fontification of numbers from point to LIM."
=C2=A0 (save-excursion
=C2=A0 =C2=A0 (while (re-search-forward -num3-number-re lim t)
=C2=A0 =C2=A0 =C2=A0 (-num3-int =C2=A0(match-beginning 1) (match-end 1) 4)<= br> =C2=A0 =C2=A0 =C2=A0 (-num3-int =C2=A0(match-beginning 2) (match-end 2) num= 3-group-size)
=C2=A0 =C2=A0 =C2=A0 (-num3-frac (match-beginning 3) (match-end 3) num3-gro= up-size)))
=C2=A0 nil)

(defun -num3-int (lo hi n)
=C2=A0 "Highlight groups of digits in a long number.
LO and HI arguments specify the range where the number is
located. =C2=A0If the length of that region exceeds `num3-threshold', the function will split it into groups of N digits and fontify
tham alternately using `num3-face-odd' and `num3-face-even'
faces. =C2=A0Grouping is done from the end, eg. (12)(345)."
=C2=A0 (when (and lo (>=3D (- hi lo) num3-threshold))
=C2=A0 =C2=A0 (let (even)
=C2=A0 =C2=A0 =C2=A0 (while (< lo hi)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (-num3-put even (max lo (- hi n)) hi)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq hi (- hi n) even (not even))))))

(defun -num3-frac (lo hi n)
=C2=A0 "Highlight groups of digits in a long number.
LO and HI arguments specify the range where the number is
located. =C2=A0If the length of that region exceeds `num3-threshold', the function will split it into groups of N digits and fontify
tham alternately using `num3-face-odd' and `num3-face-even'
faces. =C2=A0Grouping is done from the beginning, eg. (123)(45)."
=C2=A0 (when (and lo (>=3D (- hi lo) num3-threshold))
=C2=A0 =C2=A0 (let (even)
=C2=A0 =C2=A0 =C2=A0 (while (< lo hi)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (-num3-put even lo (min hi (+ lo n)))
=C2=A0 =C2=A0 =C2=A0 =C2=A0 (setq lo (+ lo n) even (not even))))))

(defun -num3-put (even lo hi)
=C2=A0 "Add font lock text property to highlight a single group of dig= it.
Use `num3-face-odd' if EVEN is nil and `num3-face-even' if EVEN is<= br> non-nil. =C2=A0The region the face is set to is from LO to HI."
=C2=A0 (font-lock-append-text-property lo hi 'face
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 (if even 'num3-face-even = 'num3-face-odd)))

(provide 'num3)

;;; num3.el ends here


--20cf307d03e49dfc5604ca9c0ffa--