annotate lisp/skk/string.el @ 225:12579d965149 r20-4b11

Import from CVS: tag r20-4b11
author cvs
date Mon, 13 Aug 2007 10:11:40 +0200
parents 262b8bb4a523
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
219
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
1 ;;;; $Id: string.el,v 1.1 1997/12/02 08:48:41 steve Exp $
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
2 ;;;; This file contains some miscellaneous string functions
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
3
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
4 ;; Copyright (C) 1991-1995 Free Software Foundation
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
5
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
6 ;; Author: Sebastian Kremer <sk@thp.Uni-Koeln.DE>
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
7 ;; Per Cederqvist <ceder@lysator.liu.se>
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
8 ;; Inge Wallin <inge@lysator.liu.se>
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
9 ;; Maintainer: elib-maintainers@lysator.liu.se
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
10 ;; Created: before 9 May 1991
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
11 ;; Keywords: extensions, lisp
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
12
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
13 ;;;; This file is part of the GNU Emacs lisp library, Elib.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
14 ;;;;
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
15 ;;;; GNU Elib is free software; you can redistribute it and/or modify
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
16 ;;;; it under the terms of the GNU General Public License as published by
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
17 ;;;; the Free Software Foundation; either version 2, or (at your option)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
18 ;;;; any later version.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
19 ;;;;
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
20 ;;;; GNU Elib is distributed in the hope that it will be useful,
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
21 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
22 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
23 ;;;; GNU General Public License for more details.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
24 ;;;;
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
25 ;;;; You should have received a copy of the GNU General Public License
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
26 ;;;; along with GNU Elib; see the file COPYING. If not, write to
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
27 ;;;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
28 ;;;; Boston, MA 02111-1307, USA
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
29 ;;;;
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
30 ;;;; Author: Sebastian Kremer
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
31 ;;;; sk@thp.Uni-Koeln.DE
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
32 ;;;;
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
33
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
34
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
35 ;;; Commentary:
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
36
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
37 ;;;
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
38 ;;; This file is part of the elisp library Elib.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
39 ;;; It implements simple generic string functions for use in other
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
40 ;;; elisp code: replace regexps in strings, split strings on regexps.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
41 ;;;
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
42
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
43
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
44 ;;; Code:
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
45
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
46 (require 'queue-m)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
47 (provide 'string)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
48
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
49
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
50 ;; This function is a near-equivalent of the elisp function replace-match
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
51 ;; which work on strings instead of a buffer. The FIXEDCASE parameter
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
52 ;; of replace-match is not implemented.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
53
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
54 (defun string-replace-match (regexp string newtext &optional literal global)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
55 "Replace first match of REGEXP in STRING with NEWTEXT.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
56 If no match is found, nil is returned instead of the new string.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
57
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
58 Optional arg LITERAL non-nil means to take NEWTEXT literally. If LITERAL is
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
59 nil, character `\\' is the start of one of the following sequences:
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
60 \\\\ will be replaced by a single \\.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
61 \\& will be replaced by the text which matched the regexp.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
62 \\N where N is a number and 1 <= N <= 9, will be replaced
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
63 by the Nth subexpression in REGEXP. Subexpressions are grouped
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
64 inside \\( \\).
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
65
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
66 Optional arg GLOBAL means to replace all matches instead of only the first."
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
67
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
68 (let ((data (match-data)))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
69 (unwind-protect
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
70
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
71 (if global
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
72 (let ((result "")
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
73 (start 0)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
74 matchbeginning
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
75 matchend)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
76 (while (string-match regexp string start)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
77 (setq matchbeginning (match-beginning 0)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
78 matchend (match-end 0)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
79 result (concat result
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
80 (substring string start matchbeginning)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
81 (if literal
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
82 newtext
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
83 (elib-string-expand-newtext string
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
84 newtext )))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
85 start matchend))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
86
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
87 (if matchbeginning ; matched at least once
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
88 (concat result (substring string start))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
89 nil))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
90
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
91 ;; not GLOBAL
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
92 (if (not (string-match regexp string 0))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
93 nil
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
94 (concat (substring string 0 (match-beginning 0))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
95 (if literal newtext (elib-string-expand-newtext string
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
96 newtext ))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
97 (substring string (match-end 0)))))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
98 (store-match-data data))))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
99
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
100
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
101 (defun elib-string-expand-newtext (string newtext)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
102 ;; Expand \& and \1..\9 (referring to STRING) in NEWTEXT.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
103 ;; Uses match data and fluid vars `newtext', `string'.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
104 ;; Note that in Emacs 18 match data are clipped to current buffer
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
105 ;; size...so the buffer should better not be smaller than STRING.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
106 (let ((pos 0)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
107 (len (length newtext))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
108 (expanded-newtext ""))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
109 (while (< pos len)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
110 (setq expanded-newtext
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
111 (concat expanded-newtext
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
112 (let ((c (aref newtext pos)))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
113 (if (= ?\\ c)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
114 (cond ((= ?\& (setq c (aref newtext
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
115 (setq pos (1+ pos)))))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
116 (match-string 0 string) )
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
117 ((and (>= c ?1)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
118 (<= c ?9))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
119 ;; return empty string if N'th
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
120 ;; sub-regexp did not match:
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
121 (let ((n (- c ?0)))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
122 (if (match-beginning n)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
123 (substring string
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
124 (match-beginning n)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
125 (match-end n))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
126 "")))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
127 (t (char-to-string c)))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
128 (char-to-string c)))))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
129 (setq pos (1+ pos)))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
130 expanded-newtext))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
131
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
132
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
133 (defun string-split (pattern string &optional limit)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
134 "Splitting on regexp PATTERN, turn string STRING into a list of substrings.
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
135 Optional third arg LIMIT (>= 1) is a limit to the length of the
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
136 resulting list."
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
137
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
138 (let* ((data (match-data))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
139 (start (string-match pattern string))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
140 (result-q (queue-create))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
141 (count 1)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
142 (end (if start (match-end 0))) )
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
143 (unwind-protect
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
144 (progn
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
145 (queue-enqueue result-q (substring string 0 start))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
146 (if end ; else nothing left
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
147 (while (and (or (not (integerp limit))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
148 (< count limit))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
149 (string-match pattern string end))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
150 (setq start (match-beginning 0)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
151 count (1+ count) )
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
152 (queue-enqueue result-q (substring string end start))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
153 (setq end (match-end 0)
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
154 start end )))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
155 (if (and (or (not (integerp limit))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
156 (< count limit) )
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
157 end) ; else nothing left
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
158 (queue-enqueue result-q (substring string end)) )
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
159 (queue-all result-q) )
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
160 (store-match-data data) )))
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
161
262b8bb4a523 Import from CVS: tag r20-4b8
cvs
parents:
diff changeset
162 ;;; string.el ends here