unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Pascal Bourguignon <spam@mouse-potato.com>
Subject: Re: Splitting String
Date: Tue, 15 Nov 2005 14:09:56 +0100	[thread overview]
Message-ID: <87d5l2hxaz.fsf@thalassa.informatimago.com> (raw)
In-Reply-To: 1132057051.308997.27730@g49g2000cwa.googlegroups.com

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. 

  reply	other threads:[~2005-11-15 13:09 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-15 12:17 Splitting String Jeckob
2005-11-15 13:09 ` Pascal Bourguignon [this message]
2005-11-15 14:46   ` Jeckob

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87d5l2hxaz.fsf@thalassa.informatimago.com \
    --to=spam@mouse-potato.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).