|
string-strip.el 1.1: msg#00005emacs.sources
;;; string-strip.el --- Strip CHARS from STRING ;; Version: 1.1 ;; Copyright (C) 2007 Andreas Roehler ;; Author: Andreas Roehler <andreas.roehler@xxxxxxxxxxxxx> ;; 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. ;; 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 ;; 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 <lists@xxxxxxx> 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 ;;; 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-chars-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-chars-preserve'" :type 'string :group 'convenience) ;; (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\\)*?\\)\"" :type 'string :group 'convenience) (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 specified, 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 \"\\(.*?\\)\"" (save-match-data (let ((s-c-b (or chars-before strip-chars-before)) (s-c-a (or chars-after strip-chars-after)) (s-c-p (or chars-preserve string-chars-preserve))) (string-match (concat "\\`" s-c-b s-c-p s-c-a "\\'") str) (match-string 1 str)))) (provide 'string-strip) ;;; string-strip.el ends here |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | string-strip.el 1.0: 00005, Andreas Roehler |
|---|---|
| Next by Date: | video-file.el, tumme.el: thumbnails of video files: 00005, Markus Triska |
| Previous by Thread: | string-strip.el 1.0i: 00005, Andreas Roehler |
| Next by Thread: | video-file.el, tumme.el: thumbnails of video files: 00005, Markus Triska |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |