From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Pascal Bourguignon Newsgroups: gmane.emacs.help Subject: Re: Splitting String Date: Tue, 15 Nov 2005 14:09:56 +0100 Organization: Informatimago Message-ID: <87d5l2hxaz.fsf@thalassa.informatimago.com> References: <1132057051.308997.27730@g49g2000cwa.googlegroups.com> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1132066696 11237 80.91.229.2 (15 Nov 2005 14:58:16 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 15 Nov 2005 14:58:16 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Nov 15 15:58:14 2005 Return-path: Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1Ec28s-0007Kt-1l for geh-help-gnu-emacs@m.gmane.org; Tue, 15 Nov 2005 15:50:38 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1Ec28o-0003Fp-N0 for geh-help-gnu-emacs@m.gmane.org; Tue, 15 Nov 2005 09:50:35 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!newsfeed.gamma.ru!Gamma.RU!nntp.theplanet.net!inewsm1.nntp.theplanet.net!easynet-quince!easynet.net!easynet-post2!not-for-mail Original-Newsgroups: gnu.emacs.help Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) Cancel-Lock: sha1:HXZa/A3ORvzU9DdTcu9Ezi8r/j8= Original-Lines: 139 Original-NNTP-Posting-Host: 62.93.174.79 Original-X-Trace: DXC=jV7PoWJ=7lX\4lUd1gYjeXEW@M2bjd]fUbBd4DjF1d]Q Original-Xref: shelby.stanford.edu gnu.emacs.help:135467 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:31063 Archived-At: Jeckob@gmx.net writes: > Hi all, > having problems with some Code i have written, half of the job is done > but now i have problems to get on. here is me code until now : > > (defun format/align-and-newline () > (interactive) > (setq anker (point)) > (let* ((start (progn (beginning-of-line) (point))) > (end (progn (end-of-line) (point))) > (line (buffer-substring start end)) > (words (split-string line))) > (setq testpoint (first words)) > (if (setq posDoppel (position ?: line) ) > (if (position ?. testpoint) > (insert "\n") > (delete-region start end) > (insert (format "%-10s" (first words))) > (dolist (word (cdr words)) > (insert (format "%-10s" word))) > (if (beginning-of-line) > (insert "\n") > (setq anker (point))) > ) > ) > ) > (goto-char anker) > (insert "\n") > ) > (local-set-key (kbd "RET") (function format/align-and-newline)) > ;;end > > If i type a line like this: > abc def ghj klm : nop : qrs : tuv > > the code transforms the string to: > abc def ghj klm : nop : > qrs : tuv > > So, the thing im having problems is to get the string like this : > abc def ghj klm : nop : qrs : tuv > > in other words, if there is a colon typed a space before a word, i need > to have the colon with the word to be in the cell of the list, and not > the colon alone as a word. So, in the example the list should have 7 > elements and not 10. Maybe somebody can give me a hint... thanks in > advance You just write what you mean!!! For example, if I mean that: if one word is a single colon, then it put with a space just before the following word, instead of being put alone, I'd write: (defun pre-process-words (words) (loop with result = '() while words do (if (string= ":" (first words)) (progn (push (format "%s %s" (first words) (second words)) result) (setf words (cddr words))) (progn (push (first words) result) (setf words (cdr words)))) finally (return (nreverse result)))) (defun format/align-and-newline () (interactive) (let* ((anker (point)) (start (progn (beginning-of-line) (point))) (end (progn (end-of-line) (point))) (line (buffer-substring start end)) (words (pre-process-words (split-string line)))) (setq testpoint (first words)) (if (setq posDoppel (position ?: line) ) (if (position ?. testpoint) (insert "\n") (delete-region start end) (insert (format "%-10s" (first words))) (dolist (word (cdr words)) (insert (format "%-10s" word))) (if (beginning-of-line) (insert "\n") (setq anker (point))))) (goto-char anker) (insert "\n"))) But both the following lines: ---------------------------------------- abc def ghj klm : nop : qrs : tuv abc def ghj klm : nop : qrs : tuv ---------------------------------------- will be formated as: ---------------------------------------- abc def ghj klm : nop : qrs : tuv abc def ghj klm : nop : qrs : tuv ---------------------------------------- Since you mean this: > in other words, if there is a colon typed a space before a word, i need > to have the colon with the word to be in the cell of the list, and not > the colon alone as a word. So, in the example the list should have 7 > elements and not 10. Maybe somebody can give me a hint... thanks in > advance then you should parse the colon, SPC, word before splitting the words: Instead of: (words (pre-process-words (split-string line))) you'd write: (words (parse-line/for-align-and-newline line)) and, for example: (defun parse-line/for-align-and-newline (line) ... (cond ... ((string-match "^\\(: [^ ]+\\) *" line start) ; a colon, SPC and a "word" ; made of one or more non-spaces (setf word (match-string 1 line) start (match-end 0))) ...) ...) -- __Pascal Bourguignon__ http://www.informatimago.com/ Litter box not here. You must have moved it again. I'll poop in the sink.