emacs-devel
[Top][All Lists]
Advanced

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

Re: simple useful functions


From: Stephen J. Turnbull
Subject: Re: simple useful functions
Date: Tue, 02 Nov 2010 11:22:11 +0900

Tak Ota writes:
 > Could you teach me how to perform collect-string equivalent operation
 > with occur mode?

I don't know if you can, maybe there's something in Emacs's version of
`occur' that would allow it.  I didn't understand your requirements,
that's why I asked.

Rather than a separate function, I think it would be useful to augment
occur-mode here, by saving the occur regexp (say to occur-mode-regexp)
and adding the following command to occur mode (untested):

(defun occur-mode-matchers-only ()
  "Leave only text that matches the first group in the occur regexp.
If there is no group, use the whole match.
With more than one group, you should use shy groups for the others.
Be careful with repeated groups; only one of them will be left."
  (interactive)
  (save-excursion
    (toggle-read-only -1)
    (goto-char (point-min))
    (save-match-data
      ;; don't look at this code lest you be turned to stone, bletch
      (re-search-forward occur-mode-regexp nil t)
      (let* ((group (if (match-end 1) 1 0))
             (previous-end (progn (goto-char (match-end group))
                                  (point-marker)))
             temp)
        (delete-region (point-min) (match-beginning group))
        (while (re-search-forward occur-mode-regexp nil t)
          (setq temp (progn (goto-char (match-end group)) (point-marker)))
          (delete-region previous-end (match-beginning group))
          (goto-char previous-end)
          (insert ?\n)
          (setq previous-end temp))
        (delete-region previous-end (point-max))
        (insert ?\n)
        (goto-char (point-max))
        (insert ?\n)
        (toggle-read-only 1)))))



reply via email to

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