[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
string-strip and some more stuff
From: |
Andreas Röhler |
Subject: |
string-strip and some more stuff |
Date: |
Thu, 05 Aug 2010 21:24:44 +0200 |
User-agent: |
Mozilla/5.0 (X11; U; Linux i686; de; rv:1.9.1.11) Gecko/20100711 Thunderbird/3.0.6 |
Hi folks,
as you may know, the focus of
https://code.launchpad.net/s-x-emacs-werkstatt/
is on very basic editing issues.
For example a delivered function `ar-alnum-atpt' will
return all alpha-numerical chars below and around
cursor as a string. `ar-bounds-of-alnum-atpt' returns
the borders of that string as a list and so on.
A second series of functions puts delimiters around the
strings delivered by the former or at all other
objects, i.e. parentheses, braces, but also markups,
blok-strings etc.
Alltogether more functions than I would like to read,
whose existence is knowledgeable in an abstract way, by
the lists of things at the end of thingatpt-utils-base.el
For the moment it looks like this:
...'alnum 'alpha 'ascii 'blank 'cntrl 'digit 'graph 'lower 'nonascii
'print 'punct 'space 'upper 'xdigit ))
...'begin-end-quote 'blok 'doubleslashed-paren 'markup 'ml-attribut
'slashed-paren 'tabledata 'xsl-stylesheet 'xsl-template ))
...'brace 'bracket 'angle-lesser 'angle-greater 'left-right-singlequote
'parentize ))
...'braced 'bracketed 'angled-lesser 'angled-greater
'left-right-singlequoted 'parentized ))
...'backslash 'dollar 'doublequote 'hyphen 'singlequote 'slash
'underscore 'whitespace ))
...'backslashed 'dollared 'doublequoted 'hyphened 'singlequoted 'slashed
'underscored 'whitespaced ))
...'angled-no-nest 'angled-greater-nested 'angled-lesser-nested 'comment
'csv 'date 'defun 'delimited 'email 'filename 'float 'function 'ip 'isbn
'line 'list 'number 'page 'paragraph 'paren 'phone 'python-expression
'region 'sentence 'sexp 'string 'sh-struct 'symbol 'url 'word
'word-alpha-only ))
...'angled-no-nest 'angled-greater-nested 'angled-lesser-nested 'csv
'line 'list 'paragraph 'region 'sentence 'string 'buffer ))
Every single function is not that difficult. However, as these functions
are available at once,
edits are much faster.
Try
M-x ar-doubleslash-paren-alnum-atpt
and afterwards
M-x ar-doubleslashed-paren-atpt
Also some minor utils are coming with. New string-strip
utility below. BTW emacs-like string-strip not just
handles strings, but also a portion of the buffer - the
region.
Enjoy!
Andreas
--
https://code.launchpad.net/~a-roehler/python-mode
https://code.launchpad.net/s-x-emacs-werkstatt/
;;; string-strip.el --- Remove leading and/or trailing spaces/chars
;; Copyright (C) 2010 Andreas Roehler
;; Author: Andreas Roehler <address@hidden>
;; Keywords: lisp
;; This program 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 3 of the License, or
;; (at your option) any later version.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Return a copy of STR with leading and/or trailing
;; spaces/chars removed. If chars to remove are not
;; specified when calling, default values of
;; `strip-chars-before', `strip-chars-after' are used.
;; If no string delivered but region is active,
;; buffer-substring of region is replaced with the
;; result
;; Used a form resulting from a discussion at
;; http://lists.gnu.org/archive/html/emacs-devel/2006-06/msg00428.html
;; (string-match
;; (concat "\\`\\([" s-c-b"]*\\)" s-c-p "\\([" s-c-a "]*\\)\\'") str)
;; A bug showed up, as the chars to remove from the ends
;; also were inside the string to preserve: Rewrite done below
;;; Code:
(defalias 'string-strip 'strip)
(defalias 'string-strip-right 'rstrip)
(defalias 'string-strip-left 'lstrip)
(defcustom strip-chars-before "[ \t\r\n]*"
"Regexp indicating which chars shall be stripped before STRING. "
:type 'string
:group 'convenience)
;; (setq strip-chars-after "[ \t\r\n]*")
(defcustom strip-chars-after "[ \t\r\n]*\\'"
"Regexp indicating which chars shall be stripped after STRING. "
:type 'string
:group 'convenience)
;;;###autoload
(defun rstrip (&optional str chars-after)
"Return a string or change region with trailing characters
removed. Defaults to the value uof
`strip-chars-after', basically whitespace, which is customizable.
(rstrip \"spacious \")
==> \"spacious\""
(interactive "*")
(lexical-let ((chars-after (if chars-after chars-after t)))
(strip str nil chars-after)))
(defun lstrip (&optional str chars-before)
"Return a string or change region with leading characters
removed. Defaults to the value uof
`strip-chars-before', basically whitespace, which is customizable.
(lstrip \" spacious\")
==> \"spacious\""
(interactive "*")
(lexical-let ((chars-before (if chars-before chars-before t)))
(strip str chars-before)))
(defun strip (&optional str chars-before chars-after)
"Return a copy of STR, leading and/or trailing CHARS removed.
Default of `CHARS-BEFORE', `CHARS-AFTER': space, tab, carriage return,
newline, newpage.
If no string is delivered but region is active, strip region.
(strip \" spacious \")
==> \"spacious\""
(interactive "*")
(let* ((got-string (stringp str))
(beg (cond (got-string)
((region-active-p)
(region-beginning))
(t (point-min))))
(end (cond (got-string)
((region-active-p)
(region-end))
(t (point-max))))
(str (cond (got-string str)
(t
(buffer-substring-no-properties beg end))))
(s-c-b (cond ((stringp chars-before)
chars-before)
((and (not chars-before) chars-after) "")
(t strip-chars-before)))
(s-c-a (cond ((stringp chars-after)
chars-after)
((and chars-before (not chars-after)) "")
(t strip-chars-after)))
newstring erg)
(save-excursion
(with-temp-buffer
(insert str)
(unless (string= "" s-c-a)
(skip-chars-backward s-c-a)
(delete-region (point) (point-max)))
(goto-char (point-min))
(unless (string= "" s-c-b)
(skip-chars-forward s-c-b)
(delete-region (point-min) (point)))
(setq erg (buffer-substring-no-properties (point-min)
(point-max)))))
(unless got-string (delete-region beg end)
(insert erg))
(when (interactive-p) (message "%s" erg))
erg))
(provide 'string-strip)
;;; string-strip.el ends here
[Prev in Thread] |
Current Thread |
[Next in Thread] |
- string-strip and some more stuff,
Andreas Röhler <=