gnu-emacs-sources
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

string-strip.el 1.0


From: Andreas Roehler
Subject: string-strip.el 1.0
Date: Mon, 08 Jan 2007 10:35:09 +0100
User-agent: Thunderbird 1.5.0.4 (X11/20060516)

;;; string-strip.el 1.0 --- Strip CHARS from STRING

;; 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.

;;; Commentary:

;; Needed a function to correct user-input, i.e. wrongly inserted
;; white spaces at the end or the beginning.
;; " foo   " -> "foo"

;; Used `comment-string-strip' from Stefan Monier's
;; newcomment.el to that purpose, which worked fine so
;; far. However, found it unnecessary difficult to
;; employ, as it requires handling of three arguments,
;; where just one was sufficient in the cases I needed
;; it.

;; Meanwhile a discussion in emacs-devel took place, which was
;; helpful in many respects. You may view it at

;; http://comments.gmane.org/gmane.emacs.devel/55961

;; 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-strip-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.

;;; Code:

;; (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-strip-preserve'"

:type 'string
:group 'convenience)

;; (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-strip-preserve'"

:type 'string
:group 'convenience)

;; (setq string-strip-preserve  "\\(\\(?:.\\|\n\\)*?\\)"
(defcustom string-strip-preserve  "\\(.*?\\)"
   "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\\)*?\\)\" - an expression posted by Lars Hansen"

:type 'string
:group 'convenience)

(defun string-strip (str)
 "Return a copy of STR with leading and trailing CHARS removed.
Customize `strip-chars-before' and `strip-chars-after' respectively.
Default is `[[:space:]\n]*', i.e. spaces, tabs and newlines."
 (interactive)
 (save-match-data
   (string-match
(concat "\\`" strip-chars-before string-strip-preserve strip-chars-after "\\'") str)
   (match-string 1 str)))

(provide 'string-strip)
;;; string-strip.el ends here





reply via email to

[Prev in Thread] Current Thread [Next in Thread]