[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Mon, 08 Jan 2007 13:15:10 +0100
Thunderbird 22.214.171.124 (X11/20060516)
;;; string-strip.el --- Strip CHARS from STRING
;; Version: 1.1
;; Copyright (C) 2007 Andreas Roehler
;; Author: Andreas Roehler <address@hidden>
;; Keywords: convenience
;; This file 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 2, or (at your option)
;; any later version.
;; This file 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; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;; Needed a function to correct user-input, i.e. wrongly inserted
;; white spaces at the end or the beginning.
;; Used `comment-string-strip' from Stefan Monier's
;; newcomment.el to that purpose, which worked fine so
;; far. However, it requires handling of three arguments,
;; where just one was sufficient in the cases I needed
;; Meanwhile a discussion in emacs-devel took place, which was
;; helpful in many respects. You may view it at
;; Lars Hansen <address@hidden> provided a regexp for
;; the case, the string may be composed of (and
;; preserve) newlines. In the context given now set
;; `string-chars-preserve' to "\\(\\(?:.\\|\n\\)*?\\)"
;; if you need this.
;; Finally I found it useful, not just to make the ends
;; customizable, but the middle part too: Now its
;; completely up to the user to decide, what the string
;; should contain and what chars are to strip from it.
;; (string-strip " foo ") -> "foo"
;; shows the default-behavior
;; You may specify args on the fly, which will override it, thus
;; (string-strip "[:blank:]" "\\[:" ":\\]") -> "blank"
;; (string-strip "[:blank:]" "\\[:.*" ":\\]" "\\(a.+\\)") -> "ank"
;; Changes to previous version:
;; Let optional arguments overrid defaults, interactive
;; spec dropped as it seems not useful, Doku renewed
;; (setq strip-chars-before "[ \t\r\n\f]*")
(defcustom strip-chars-before "[ \t\r\n\f]*"
"Regexp indicating which chars shall be stripped before STRING - which
is defined by `string-chars-preserve'"
;; (setq strip-chars-after "[ \t\r\n\f]*")
(defcustom strip-chars-after "[ \t\r\n\f]*\\'"
"Regexp indicating which chars shall be stripped after STRING - which
is defined by `string-chars-preserve'"
;; (setq string-chars-preserve "\\(\\(?:.\\|\n\\)*?\\)"
(defcustom string-chars-preserve "\\(.*?\\)"
"Parentized regexp indicating which chars shall be constitutes of STRING
thus being preserved - whereas `strip-chars-after' and
`strip-chars-before' indicate what class of chars to strip from
beginning or end of STRING
You may customize this variable. For Example if your string to preserve
may include newlines, use \"\\(\\(?:.\\|\n\\)*?\\)\""
(defun string-strip (str &optional chars-before chars-after chars-preserve)
"Return a copy of STR with leading and trailing CHARS removed.
If no regexp for `chars-before' `chars-after' `chars-preserve' are
defaults of `strip-chars-before' and `strip-chars-after'
`string-chars-preserve' are used instead.
`strip-chars-before' and `strip-chars-after' default is \"[ \t\r\n\f]*\",
i.e. spaces, tabs, carriage returns, newlines and newpages.
`string-chars-preserve' must be a parentized expression,
it defaults to \"\\(.*?\\)\""
(let ((s-c-b (or chars-before
(s-c-a (or chars-after
(s-c-p (or chars-preserve
(concat "\\`" s-c-b s-c-p s-c-a "\\'") str)
(match-string 1 str))))
;;; string-strip.el ends here
|[Prev in Thread]
||[Next in Thread]|
- string-strip.el 1.1,
Andreas Roehler <=