all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* PATCH: num3-mode for highlighting groups of digits in long numbers
@ 2012-09-06 16:21 Michal Nazarewicz
  2012-09-06 17:23 ` Lars Ingebrigtsen
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Michal Nazarewicz @ 2012-09-06 16:21 UTC (permalink / raw)
  To: emacs-devel; +Cc: Felix Lee

[-- Attachment #1: Type: text/plain, Size: 5604 bytes --]

Hello everyone,

This time I bring to you a patch adding num3-mode which helps read long
numbers by alternating highlighting of group of digits in a long
number, so for instance something like:

	12345678.12345678

would be displayed as ("-" symbolising underscore):

	12345678.12345678
	  ---       ---

This also works for hexadecimal numbers by grouping 4-digits at a time,
ie.:

	0x0123456789ABCDEF
	  ----    ----

The face is customisable and can be added to both odd and even groups.

---------- >% ------------------------------------------------------------

=== modified file 'etc/NEWS'
--- etc/NEWS	2012-09-06 09:29:32 +0000
+++ etc/NEWS	2012-09-06 15:48:03 +0000
@@ -539,6 +539,11 @@
 
 \f
 * New Modes and Packages in Emacs 24.3
+
+** num3-mode is a minor mode that highlights groups of digits in a long
+numbers thus so as to make it easier to read.  Enable in current
+buffer via M-x num3-mode RET or globally via M-x global-num3-mode RET.
+
 \f
 * Incompatible Lisp Changes in Emacs 24.3
 

=== modified file 'lisp/ChangeLog'
--- lisp/ChangeLog	2012-09-06 15:35:08 +0000
+++ lisp/ChangeLog	2012-09-06 15:49:32 +0000
@@ -1,3 +1,7 @@
+2012-09-06  Michal Nazarewicz  <mina86@mina86.com>
+
+	* num3.el: New num3-mode minor mode.
+
 2012-09-06  Stefan Monnier  <monnier@iro.umontreal.ca>
 
 	* emacs-lisp/cl-macs.el (cl--do-arglist): Understand _ on &key args

=== added file 'lisp/num3.el'
--- lisp/num3.el	1970-01-01 00:00:00 +0000
+++ lisp/num3.el	2012-09-06 16:09:55 +0000
@@ -0,0 +1,122 @@
+;;; num3-mode.el --- highlight groups of digits in long numbers  -*- lexical-binding: t -*-
+
+;; Copyright (C) 2012 Free Software Foundation, Inc.
+
+;; Author: Felix Lee <felix8a@gmail.com>
+;;         Michal Nazarewicz <mina86@mina86.com>
+;; Keywoards: faces, minor-mode
+
+;; This file is part of GNU Emacs.
+
+;; 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 <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.  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)
+
+;;; User variables:
+
+(defgroup num3 nil
+  "Num3 is a minor mode that makes long numbers more readable by
+highlighting groups of 3 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 3 digits when font-lock is on."
+  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-mode)
+
+;;;###autoload
+(defun turn-on-num3-mode ()
+  "Turns on `num3-mode' if it's not enabled."
+  (unless num3-mode (num3-mode t)))
+
+(defconst -num3-number-re
+  (concat    "\\(?:0[xX]\\|#\\)\\([0-9a-fA-F]+\\)"  ; 1 = hexadecimal
+          "\\|\\([0-9]+\\)"                         ; 2 = decimal
+          "\\|\\.\\([0-9]+\\)"))                    ; 3 = fraction
+
+(defun -num3-matcher (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)
+  (when (and lo (>= (- 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)
+  (when (and lo (>= (- 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)
+  (font-lock-append-text-property lo hi 'face
+                                  (if even 'num3-face-even 'num3-face-odd)))
+
+(provide 'num3)


[-- Attachment #2.1: Type: text/plain, Size: 0 bytes --]



[-- Attachment #2.2: Type: application/pgp-signature, Size: 835 bytes --]

^ permalink raw reply	[flat|nested] 20+ messages in thread

end of thread, other threads:[~2012-09-26 23:33 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2012-09-06 16:21 PATCH: num3-mode for highlighting groups of digits in long numbers Michal Nazarewicz
2012-09-06 17:23 ` Lars Ingebrigtsen
2012-09-06 21:22 ` Stefan Monnier
2012-09-25 20:40   ` Michal Nazarewicz
2012-09-26  1:07     ` Stefan Monnier
2012-09-26 12:27       ` Michal Nazarewicz
2012-09-26 12:56         ` Noah Lavine
2012-09-26 13:06           ` Michal Nazarewicz
2012-09-26 14:58             ` Lennart Borgman
2012-09-26 20:02               ` Stefan Monnier
2012-09-26 20:01             ` Stefan Monnier
2012-09-26 21:33               ` Michal Nazarewicz
2012-09-26 21:44                 ` Stefan Monnier
2012-09-26 23:33                   ` Michal Nazarewicz
2012-09-26 21:36               ` turn-on/off-*MODE* (was: Re: PATCH: num3-mode for highlighting groups of digits in long numbers) chad
2012-09-26 21:47                 ` turn-on/off-*MODE* Stefan Monnier
2012-09-26 21:59               ` PATCH: num3-mode for highlighting groups of digits in long numbers felix
2012-09-06 23:18 ` Jambunathan K
2012-09-06 23:41   ` felix
2012-09-07  1:14   ` Michal Nazarewicz

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.