[PATCH text-modes] Add format-spec.el

Jerry James <[email protected]>
Newsgroups gmane.emacs.xemacs.patches
Message-ID <CAHCOHQkN0K=EM89OVCfkDycOZ0ogv3nj8bR3J_Cp9OWu512qoA@mail.gmail.com>
PATCH text-modes

I'm working on STARTTLS support, and need format-spec.el from Emacs
for one bit of it.  This seems like it could be a generally useful bit
of elisp, so I propose adding it to the packages.

While I was messing around with text-modes, I also removed a bunch of
spurious executable permissions from xpm-mode.el and nearly all of the
.xpm files.

diff -r da2c71235bd8 ChangeLog
--- a/ChangeLog Thu May 15 21:13:00 2014 +0200
+++ b/ChangeLog Wed Jun 11 09:11:25 2014 -0600
@@ -1,3 +1,9 @@
+2014-06-11  Jerry James  <[email protected]>
+
+ * Makefile (ELCS): Add format-spec.elc and realphabetize.
+ * xpm-mode.el: Remove spurious executable permissions.
+ * *.xpm: Ditto.
+
 2014-05-15  Norbert Koch  <[email protected]>

  * Makefile (VERSION): XEmacs package 2.03 released.
diff -r da2c71235bd8 Makefile
--- a/Makefile Thu May 15 21:13:00 2014 +0200
+++ b/Makefile Wed Jun 11 09:11:25 2014 -0600
@@ -27,14 +27,14 @@
 REQUIRES = ispell fsf-compat xemacs-base
 CATEGORY = standard

-ELCS = autoinsert.elc crontab.elc filladapt.elc flyspell.elc folding.elc \
- hexl.elc htmlize.elc image-mode.elc iso-acc.elc iso-ascii.elc \
- iso-cvt.elc iso-insert.elc iso-swed.elc nroff-mode.elc scribe.elc \
- swedish.elc tabify.elc tpum.elc underline.elc whitespace.elc \
- whitespace-mode.elc whitespace-visual-mode.elc winmgr-mode.elc \
- ws-mode.elc xpm-mode.elc xrdb-mode.elc ansi-color.elc \
- rtf-support.elc apache-mode.elc po-mode.elc po-compat.elc \
- css-mode.elc desktop-entry-mode.elc
+ELCS = ansi-color.elc apache-mode.elc autoinsert.elc crontab.elc css-mode.elc \
+ desktop-entry-mode.elc filladapt.elc flyspell.elc folding.elc \
+ format-spec.elc hexl.elc htmlize.elc image-mode.elc iso-acc.elc \
+ iso-ascii.elc iso-cvt.elc iso-insert.elc iso-swed.elc nroff-mode.elc \
+ po-compat.elc po-mode.elc rtf-support.elc scribe.elc swedish.elc \
+ tabify.elc tpum.elc underline.elc whitespace.elc whitespace-mode.elc \
+ whitespace-visual-mode.elc winmgr-mode.elc ws-mode.elc xpm-mode.elc \
+ xrdb-mode.elc

 DATA_FILES = xpm-black-color-icon-48-48.xpm xpm-marker-icon-48-48.xpm \
              xpm-rotate-cw-icon-48-48.xpm xpm-shift-up-icon-48-48.xpm \
diff -r da2c71235bd8 format-spec.el
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/format-spec.el Wed Jun 11 09:11:25 2014 -0600
@@ -0,0 +1,77 @@
+;;; format-spec.el --- functions for formatting arbitrary formatting strings
+
+;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
+
+;; Author: Lars Magne Ingebrigtsen <[email protected]>
+;; Keywords: tools
+
+;; This file is part of XEmacs.
+
+;; XEmacs 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.
+
+;; XEmacs 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 XEmacs.  If not, see <http://www.gnu.org/licenses/>.
+
+;;; Commentary:
+
+;;; Code:
+
+(defun format-spec (format specification)
+  "Return a string based on FORMAT and SPECIFICATION.
+FORMAT is a string containing `format'-like specs like \"bash %u %k\",
+while SPECIFICATION is an alist mapping from format spec characters
+to values.  Any text properties on a %-spec itself are propagated to
+the text that it generates."
+  (with-temp-buffer
+    (insert format)
+    (goto-char (point-min))
+    (while (search-forward "%" nil t)
+      (cond
+       ;; Quoted percent sign.
+       ((eq (char-after) ?%)
+ (delete-char 1))
+       ;; Valid format spec.
+       ((looking-at "\\([-0-9.]*\\)\\([a-zA-Z]\\)")
+ (let* ((num (match-string 1))
+       (spec (string-to-char (match-string 2)))
+       (val (assq spec specification)))
+  (unless val
+    (error "Invalid format character: `%%%c'" spec))
+  (setq val (cdr val))
+  ;; Pad result to desired length.
+  (let ((text (format (concat "%" num "s") val)))
+    ;; Insert first, to preserve text properties.
+    (insert-and-inherit text)
+    ;; Delete the specifier body.
+    (delete-region (+ (match-beginning 0) (length text))
+   (+ (match-end 0) (length text)))
+    ;; Delete the percent sign.
+    (delete-region (1- (match-beginning 0)) (match-beginning 0)))))
+       ;; Signal an error on bogus format strings.
+       (t
+ (error "Invalid format string"))))
+    (buffer-string)))
+
+(defun format-spec-make (&rest pairs)
+  "Return an alist suitable for use in `format-spec' based on PAIRS.
+PAIRS is a list where every other element is a character and a value,
+starting with a character."
+  (let (alist)
+    (while pairs
+      (unless (cdr pairs)
+ (error "Invalid list of pairs"))
+      (push (cons (car pairs) (cadr pairs)) alist)
+      (setq pairs (cddr pairs)))
+    (nreverse alist)))
+
+(provide 'format-spec)
+
+;;; format-spec.el ends here

-- 
Jerry James
http://www.jamezone.org/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.