|
string-strip.el 1.0: msg#00004emacs.sources
;;; string-strip.el 1.0 --- Strip CHARS from STRING ;; 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. ;; " 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 <lists@xxxxxxx> 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 |
|
| <Prev in Thread] | Current Thread | [Next in Thread> |
|---|---|---|
| Previous by Date: | blank-mode.el v6.3: 00004, Vinicius Jose Latorre |
|---|---|
| Next by Date: | string-strip.el 1.1: 00004, Andreas Roehler |
| Previous by Thread: | blank-mode.el v6.3i: 00004, Vinicius Jose Latorre |
| Next by Thread: | string-strip.el 1.1: 00004, Andreas Roehler |
| Indexes: | [Date] [Thread] [Top] [All Lists] |
| News | FAQ | advertise |