help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: need help with a defun


From: Joakim Hove
Subject: Re: need help with a defun
Date: Tue, 08 Jul 2003 15:57:35 +0200
User-agent: Gnus/5.1003 (Gnus v5.10.3) Emacs/21.2 (gnu/linux)

Try this, 

  (defun sort-text (new-buffer)
    (beginning-of-buffer)

    (let ((string-list))
      (while (search-forward ";" (point-max) 't)
        (save-excursion
        (let ((p2 (1- (point))))
          (backward-char 2)
          (re-search-backward "\\b")
          (let* ((string       (buffer-substring-no-properties (point) p2))
                 (string-assoc (assoc string string-list)))
            (if string-assoc
                (setcdr string-assoc (list (1+ (nth 1 string-assoc))))
              (setq string-list (cons (list string 1) string-list)))))))
      (sort string-list 'string-sort)
      (pop-to-buffer new-buffer)
      (end-of-buffer)
      (insert "---------\n")
      (dolist (string-pair (nreverse string-list))
        (let ((string (nth 0 string-pair))
            (count  (nth 1 string-pair)))
        (insert (format "%3d  %s\n" count string))))))


  (defun string-sort (e1 e2)
    (if (< (nth 1 e1) (nth 1 e2)) 't nil))
      

  (defun interactive-sort-text ()
    (interactive)
    (sort-text "*SORT OUTPUT*"))


it seems to work for me. The original file/buffer is left untouched,
the sorted output ends up in a new (not cleared) buffer.

Good luck

Joakim


-- 
  /--------------------------------------------------------------------\
 / Joakim Hove  / hove@bccs.no  /  (55 5) 84076       |                 \
 | Unifob AS, Avdeling for Beregningsvitenskap (BCCS) | Stabburveien 18 |
 | CMU                                                | 5231 Paradis    |
 \ Thormøhlensgt.55, 5020 Bergen.                     | 55 91 28 18     /
  \--------------------------------------------------------------------/


reply via email to

[Prev in Thread] Current Thread [Next in Thread]