emacs-devel
[Top][All Lists]
Advanced

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

Re: [PATCH] New command to invert lines in region


From: Mathias Dahl
Subject: Re: [PATCH] New command to invert lines in region
Date: Sun, 16 Oct 2016 00:25:12 +0200

 
OK, i found it: reverse-region.  Pretty similar.
It doesn't allow writting to a different buffer than the current one.
The proposed command here allow that and save some consing.

Writing the result into another buffer sounds quite arbitrary. Is it really such a common scenario that this function should do it? Those who needs to have the result in another buffer can easily add a few lines in the calling code to make it so.

Out of curiosity I had a look at the current implementation of `reverse-region' and found it quite complicated and hard to understand what it does. I wrote my own naïve version that I think is much simpler to understand and which seems to be as quick as the current version (I tested on a buffer with 100 000 lines.) I have no idea on how much memory it consumes compared to the current version though. It's so small that I am including it here:

(defun reverse-region (beg end)
  "Reverse the order of lines in a region.
From a program takes two point or marker arguments, BEG and END."
  (interactive "r")
  (let ((lines (nreverse (split-string (buffer-substring beg end) "\n")))
        (cnt 0))
    (delete-region beg end)
    (dolist (line lines)
      ;; Try to do the right thing when the end of the region is on an
      ;; empty line, which should be a common use case.
      (unless (and (= cnt 0) (string= "" line))
        (setq cnt (1+ cnt))
        (insert line "\n")))))

If case people prefer this version and want to replace the current implementation with it, I should have papers on file already.

/Mathias


reply via email to

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