| 1 | ;;; sort.el --- commands to sort text in an Emacs buffer |
| 2 | |
| 3 | ;; Copyright (C) 1986-1987, 1994-1995, 2001-2014 Free Software |
| 4 | ;; Foundation, Inc. |
| 5 | |
| 6 | ;; Author: Howie Kaye |
| 7 | ;; Maintainer: FSF |
| 8 | ;; Keywords: unix |
| 9 | |
| 10 | ;; This file is part of GNU Emacs. |
| 11 | |
| 12 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
| 13 | ;; it under the terms of the GNU General Public License as published by |
| 14 | ;; the Free Software Foundation, either version 3 of the License, or |
| 15 | ;; (at your option) any later version. |
| 16 | |
| 17 | ;; GNU Emacs is distributed in the hope that it will be useful, |
| 18 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | ;; GNU General Public License for more details. |
| 21 | |
| 22 | ;; You should have received a copy of the GNU General Public License |
| 23 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
| 24 | |
| 25 | ;;; Commentary: |
| 26 | |
| 27 | ;; This package provides the sorting facilities documented in the Emacs |
| 28 | ;; user's manual. |
| 29 | |
| 30 | ;;; Code: |
| 31 | |
| 32 | (defgroup sort nil |
| 33 | "Commands to sort text in an Emacs buffer." |
| 34 | :group 'data) |
| 35 | |
| 36 | (defcustom sort-fold-case nil |
| 37 | "Non-nil if the buffer sort functions should ignore case." |
| 38 | :group 'sort |
| 39 | :type 'boolean) |
| 40 | ;;;###autoload(put 'sort-fold-case 'safe-local-variable 'booleanp) |
| 41 | |
| 42 | ;;;###autoload |
| 43 | (defun sort-subr (reverse nextrecfun endrecfun |
| 44 | &optional startkeyfun endkeyfun predicate) |
| 45 | "General text sorting routine to divide buffer into records and sort them. |
| 46 | |
| 47 | We divide the accessible portion of the buffer into disjoint pieces |
| 48 | called sort records. A portion of each sort record (perhaps all of |
| 49 | it) is designated as the sort key. The records are rearranged in the |
| 50 | buffer in order by their sort keys. The records may or may not be |
| 51 | contiguous. |
| 52 | |
| 53 | Usually the records are rearranged in order of ascending sort key. |
| 54 | If REVERSE is non-nil, they are rearranged in order of descending sort key. |
| 55 | The variable `sort-fold-case' determines whether alphabetic case affects |
| 56 | the sort order. |
| 57 | |
| 58 | The next four arguments are functions to be called to move point |
| 59 | across a sort record. They will be called many times from within sort-subr. |
| 60 | |
| 61 | NEXTRECFUN is called with point at the end of the previous record. |
| 62 | It moves point to the start of the next record. |
| 63 | It should move point to the end of the buffer if there are no more records. |
| 64 | The first record is assumed to start at the position of point when sort-subr |
| 65 | is called. |
| 66 | |
| 67 | ENDRECFUN is called with point within the record. |
| 68 | It should move point to the end of the record. |
| 69 | |
| 70 | STARTKEYFUN moves from the start of the record to the start of the key. |
| 71 | It may return either a non-nil value to be used as the key, or |
| 72 | else the key is the substring between the values of point after |
| 73 | STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key |
| 74 | starts at the beginning of the record. |
| 75 | |
| 76 | ENDKEYFUN moves from the start of the sort key to the end of the sort key. |
| 77 | ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the |
| 78 | same as ENDRECFUN. |
| 79 | |
| 80 | PREDICATE, if non-nil, is the predicate function for comparing |
| 81 | keys; it is called with two arguments, the keys to compare, and |
| 82 | should return non-nil if the first key should sort before the |
| 83 | second key. If PREDICATE is nil, comparison is done with `<' if |
| 84 | the keys are numbers, with `compare-buffer-substrings' if the |
| 85 | keys are cons cells (the car and cdr of each cons cell are taken |
| 86 | as start and end positions), and with `string<' otherwise." |
| 87 | ;; Heuristically try to avoid messages if sorting a small amt of text. |
| 88 | (let ((messages (> (- (point-max) (point-min)) 50000))) |
| 89 | (save-excursion |
| 90 | (if messages (message "Finding sort keys...")) |
| 91 | (let* ((sort-lists (sort-build-lists nextrecfun endrecfun |
| 92 | startkeyfun endkeyfun)) |
| 93 | (old (reverse sort-lists)) |
| 94 | (case-fold-search sort-fold-case)) |
| 95 | (if (null sort-lists) |
| 96 | () |
| 97 | (or reverse (setq sort-lists (nreverse sort-lists))) |
| 98 | (if messages (message "Sorting records...")) |
| 99 | (setq sort-lists |
| 100 | (sort sort-lists |
| 101 | (cond (predicate |
| 102 | `(lambda (a b) (,predicate (car a) (car b)))) |
| 103 | ((numberp (car (car sort-lists))) |
| 104 | 'car-less-than-car) |
| 105 | ((consp (car (car sort-lists))) |
| 106 | (lambda (a b) |
| 107 | (> 0 (compare-buffer-substrings |
| 108 | nil (car (car a)) (cdr (car a)) |
| 109 | nil (car (car b)) (cdr (car b)))))) |
| 110 | (t |
| 111 | (lambda (a b) (string< (car a) (car b))))))) |
| 112 | (if reverse (setq sort-lists (nreverse sort-lists))) |
| 113 | (if messages (message "Reordering buffer...")) |
| 114 | (sort-reorder-buffer sort-lists old))) |
| 115 | (if messages (message "Reordering buffer... Done")))) |
| 116 | nil) |
| 117 | |
| 118 | ;; Parse buffer into records using the arguments as Lisp expressions; |
| 119 | ;; return a list of records. Each record looks like (KEY STARTPOS . ENDPOS) |
| 120 | ;; where KEY is the sort key (a number or string), |
| 121 | ;; and STARTPOS and ENDPOS are the bounds of this record in the buffer. |
| 122 | |
| 123 | ;; The records appear in the list lastmost first! |
| 124 | |
| 125 | (defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun) |
| 126 | (let ((sort-lists ()) |
| 127 | (start-rec nil) |
| 128 | done key) |
| 129 | ;; Loop over sort records. |
| 130 | ;(goto-char (point-min)) -- it is the caller's responsibility to |
| 131 | ;arrange this if necessary |
| 132 | (while (not (eobp)) |
| 133 | (setq start-rec (point)) ;save record start |
| 134 | (setq done nil) |
| 135 | ;; Get key value, or move to start of key. |
| 136 | (setq key (catch 'key |
| 137 | (or (and startkeyfun (funcall startkeyfun)) |
| 138 | ;; If key was not returned as value, |
| 139 | ;; move to end of key and get key from the buffer. |
| 140 | (let ((start (point))) |
| 141 | (funcall (or endkeyfun |
| 142 | (prog1 endrecfun (setq done t)))) |
| 143 | (cons start (point)))))) |
| 144 | ;; Move to end of this record (start of next one, or end of buffer). |
| 145 | (cond ((prog1 done (setq done nil))) |
| 146 | (endrecfun (funcall endrecfun)) |
| 147 | (nextrecfun (funcall nextrecfun) (setq done t))) |
| 148 | (if key (push |
| 149 | ;; consing optimization in case in which key is same as record. |
| 150 | (if (and (consp key) |
| 151 | (equal (car key) start-rec) |
| 152 | (equal (cdr key) (point))) |
| 153 | (cons key key) |
| 154 | (cons key (cons start-rec (point)))) |
| 155 | sort-lists)) |
| 156 | (and (not done) nextrecfun (funcall nextrecfun))) |
| 157 | sort-lists)) |
| 158 | |
| 159 | (defun sort-reorder-buffer (sort-lists old) |
| 160 | (let ((last (point-min)) |
| 161 | (min (point-min)) (max (point-max)) |
| 162 | (old-buffer (current-buffer)) |
| 163 | (mb enable-multibyte-characters) |
| 164 | temp-buffer) |
| 165 | (with-temp-buffer |
| 166 | (set-buffer-multibyte mb) |
| 167 | ;; Record the temporary buffer. |
| 168 | (setq temp-buffer (current-buffer)) |
| 169 | |
| 170 | ;; Copy the sorted text into the temporary buffer. |
| 171 | (while sort-lists |
| 172 | (goto-char (point-max)) |
| 173 | (insert-buffer-substring old-buffer |
| 174 | last |
| 175 | (nth 1 (car old))) |
| 176 | (goto-char (point-max)) |
| 177 | (insert-buffer-substring old-buffer |
| 178 | (nth 1 (car sort-lists)) |
| 179 | (cdr (cdr (car sort-lists)))) |
| 180 | (setq last (cdr (cdr (car old))) |
| 181 | sort-lists (cdr sort-lists) |
| 182 | old (cdr old))) |
| 183 | (goto-char (point-max)) |
| 184 | (insert-buffer-substring old-buffer last max) |
| 185 | |
| 186 | ;; Copy the reordered text from the temporary buffer |
| 187 | ;; to the buffer we sorted (OLD-BUFFER). |
| 188 | (set-buffer old-buffer) |
| 189 | (let ((inhibit-quit t)) |
| 190 | ;; Make sure insertions done for reordering |
| 191 | ;; saves any markers at the end of the sorted region, |
| 192 | ;; by leaving the last character of the region. |
| 193 | (delete-region min (1- max)) |
| 194 | ;; Now replace the one remaining old character with the sorted text. |
| 195 | (goto-char (point-min)) |
| 196 | (insert-buffer-substring temp-buffer) |
| 197 | (delete-region max (1+ max)))))) |
| 198 | |
| 199 | ;;;###autoload |
| 200 | (defun sort-lines (reverse beg end) |
| 201 | "Sort lines in region alphabetically; argument means descending order. |
| 202 | Called from a program, there are three arguments: |
| 203 | REVERSE (non-nil means reverse order), BEG and END (region to sort). |
| 204 | The variable `sort-fold-case' determines whether alphabetic case affects |
| 205 | the sort order." |
| 206 | (interactive "P\nr") |
| 207 | (save-excursion |
| 208 | (save-restriction |
| 209 | (narrow-to-region beg end) |
| 210 | (goto-char (point-min)) |
| 211 | (let ;; To make `end-of-line' and etc. to ignore fields. |
| 212 | ((inhibit-field-text-motion t)) |
| 213 | (sort-subr reverse 'forward-line 'end-of-line))))) |
| 214 | |
| 215 | ;;;###autoload |
| 216 | (defun sort-paragraphs (reverse beg end) |
| 217 | "Sort paragraphs in region alphabetically; argument means descending order. |
| 218 | Called from a program, there are three arguments: |
| 219 | REVERSE (non-nil means reverse order), BEG and END (region to sort). |
| 220 | The variable `sort-fold-case' determines whether alphabetic case affects |
| 221 | the sort order." |
| 222 | (interactive "P\nr") |
| 223 | (save-excursion |
| 224 | (save-restriction |
| 225 | (narrow-to-region beg end) |
| 226 | (goto-char (point-min)) |
| 227 | (sort-subr reverse |
| 228 | (function |
| 229 | (lambda () |
| 230 | (while (and (not (eobp)) (looking-at paragraph-separate)) |
| 231 | (forward-line 1)))) |
| 232 | 'forward-paragraph)))) |
| 233 | |
| 234 | ;;;###autoload |
| 235 | (defun sort-pages (reverse beg end) |
| 236 | "Sort pages in region alphabetically; argument means descending order. |
| 237 | Called from a program, there are three arguments: |
| 238 | REVERSE (non-nil means reverse order), BEG and END (region to sort). |
| 239 | The variable `sort-fold-case' determines whether alphabetic case affects |
| 240 | the sort order." |
| 241 | (interactive "P\nr") |
| 242 | (save-excursion |
| 243 | (save-restriction |
| 244 | (narrow-to-region beg end) |
| 245 | (goto-char (point-min)) |
| 246 | (sort-subr reverse |
| 247 | (function (lambda () (skip-chars-forward "\n"))) |
| 248 | 'forward-page)))) |
| 249 | \f |
| 250 | (defvar sort-fields-syntax-table nil) |
| 251 | (if sort-fields-syntax-table nil |
| 252 | (let ((table (make-syntax-table)) |
| 253 | (i 0)) |
| 254 | (while (< i 256) |
| 255 | (modify-syntax-entry i "w" table) |
| 256 | (setq i (1+ i))) |
| 257 | (modify-syntax-entry ?\s " " table) |
| 258 | (modify-syntax-entry ?\t " " table) |
| 259 | (modify-syntax-entry ?\n " " table) |
| 260 | (modify-syntax-entry ?\. "_" table) ; for floating pt. numbers. -wsr |
| 261 | (setq sort-fields-syntax-table table))) |
| 262 | |
| 263 | (defcustom sort-numeric-base 10 |
| 264 | "The default base used by `sort-numeric-fields'." |
| 265 | :group 'sort |
| 266 | :type 'integer) |
| 267 | ;;;###autoload(put 'sort-numeric-base 'safe-local-variable 'integerp) |
| 268 | |
| 269 | ;;;###autoload |
| 270 | (defun sort-numeric-fields (field beg end) |
| 271 | "Sort lines in region numerically by the ARGth field of each line. |
| 272 | Fields are separated by whitespace and numbered from 1 up. |
| 273 | Specified field must contain a number in each line of the region, |
| 274 | which may begin with \"0x\" or \"0\" for hexadecimal and octal values. |
| 275 | Otherwise, the number is interpreted according to sort-numeric-base. |
| 276 | With a negative arg, sorts by the ARGth field counted from the right. |
| 277 | Called from a program, there are three arguments: |
| 278 | FIELD, BEG and END. BEG and END specify region to sort." |
| 279 | (interactive "p\nr") |
| 280 | (let ;; To make `end-of-line' and etc. to ignore fields. |
| 281 | ((inhibit-field-text-motion t)) |
| 282 | (sort-fields-1 field beg end |
| 283 | (lambda () |
| 284 | (sort-skip-fields field) |
| 285 | (let* ((case-fold-search t) |
| 286 | (base |
| 287 | (if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]") |
| 288 | (cond ((match-beginning 1) |
| 289 | (goto-char (match-end 1)) |
| 290 | 16) |
| 291 | ((match-beginning 2) |
| 292 | (goto-char (match-end 2)) |
| 293 | 8) |
| 294 | (t nil))))) |
| 295 | (string-to-number (buffer-substring (point) |
| 296 | (save-excursion |
| 297 | (forward-sexp 1) |
| 298 | (point))) |
| 299 | (or base sort-numeric-base)))) |
| 300 | nil))) |
| 301 | |
| 302 | ;;;;;###autoload |
| 303 | ;;(defun sort-float-fields (field beg end) |
| 304 | ;; "Sort lines in region numerically by the ARGth field of each line. |
| 305 | ;;Fields are separated by whitespace and numbered from 1 up. Specified field |
| 306 | ;;must contain a floating point number in each line of the region. With a |
| 307 | ;;negative arg, sorts by the ARGth field counted from the right. Called from a |
| 308 | ;;program, there are three arguments: FIELD, BEG and END. BEG and END specify |
| 309 | ;;region to sort." |
| 310 | ;; (interactive "p\nr") |
| 311 | ;; (sort-fields-1 field beg end |
| 312 | ;; (function (lambda () |
| 313 | ;; (sort-skip-fields field) |
| 314 | ;; (string-to-number |
| 315 | ;; (buffer-substring |
| 316 | ;; (point) |
| 317 | ;; (save-excursion |
| 318 | ;; (re-search-forward |
| 319 | ;; "[+-]?[0-9]*\.?[0-9]*\\([eE][+-]?[0-9]+\\)?") |
| 320 | ;; (point)))))) |
| 321 | ;; nil)) |
| 322 | |
| 323 | ;;;###autoload |
| 324 | (defun sort-fields (field beg end) |
| 325 | "Sort lines in region lexicographically by the ARGth field of each line. |
| 326 | Fields are separated by whitespace and numbered from 1 up. |
| 327 | With a negative arg, sorts by the ARGth field counted from the right. |
| 328 | Called from a program, there are three arguments: |
| 329 | FIELD, BEG and END. BEG and END specify region to sort. |
| 330 | The variable `sort-fold-case' determines whether alphabetic case affects |
| 331 | the sort order." |
| 332 | (interactive "p\nr") |
| 333 | (let ;; To make `end-of-line' and etc. to ignore fields. |
| 334 | ((inhibit-field-text-motion t)) |
| 335 | (sort-fields-1 field beg end |
| 336 | (function (lambda () |
| 337 | (sort-skip-fields field) |
| 338 | nil)) |
| 339 | (function (lambda () (skip-chars-forward "^ \t\n")))))) |
| 340 | |
| 341 | (defun sort-fields-1 (field beg end startkeyfun endkeyfun) |
| 342 | (let ((tbl (syntax-table))) |
| 343 | (if (zerop field) (setq field 1)) |
| 344 | (unwind-protect |
| 345 | (save-excursion |
| 346 | (save-restriction |
| 347 | (narrow-to-region beg end) |
| 348 | (goto-char (point-min)) |
| 349 | (set-syntax-table sort-fields-syntax-table) |
| 350 | (sort-subr nil |
| 351 | 'forward-line 'end-of-line |
| 352 | startkeyfun endkeyfun))) |
| 353 | (set-syntax-table tbl)))) |
| 354 | |
| 355 | ;; Position at the beginning of field N on the current line, |
| 356 | ;; assuming point is initially at the beginning of the line. |
| 357 | (defun sort-skip-fields (n) |
| 358 | (if (> n 0) |
| 359 | ;; Skip across N - 1 fields. |
| 360 | (let ((i (1- n))) |
| 361 | (while (> i 0) |
| 362 | (skip-chars-forward " \t") |
| 363 | (skip-chars-forward "^ \t\n") |
| 364 | (setq i (1- i))) |
| 365 | (skip-chars-forward " \t") |
| 366 | (if (eolp) |
| 367 | (error "Line has too few fields: %s" |
| 368 | (buffer-substring |
| 369 | (line-beginning-position) |
| 370 | (line-end-position))))) |
| 371 | (end-of-line) |
| 372 | ;; Skip back across - N - 1 fields. |
| 373 | (let ((i (1- (- n)))) |
| 374 | (while (> i 0) |
| 375 | (skip-chars-backward " \t") |
| 376 | (skip-chars-backward "^ \t\n") |
| 377 | (setq i (1- i))) |
| 378 | (skip-chars-backward " \t")) |
| 379 | (if (bolp) |
| 380 | (error "Line has too few fields: %s" |
| 381 | (buffer-substring |
| 382 | (line-beginning-position) |
| 383 | (line-end-position)))) |
| 384 | ;; Position at the front of the field |
| 385 | ;; even if moving backwards. |
| 386 | (skip-chars-backward "^ \t\n"))) |
| 387 | \f |
| 388 | (defvar sort-regexp-fields-regexp) |
| 389 | (defvar sort-regexp-record-end) |
| 390 | |
| 391 | ;; Move to the beginning of the next match for record-regexp, |
| 392 | ;; and set sort-regexp-record-end to the end of that match. |
| 393 | ;; If the next match is empty and does not advance point, |
| 394 | ;; skip one character and try again. |
| 395 | (defun sort-regexp-fields-next-record () |
| 396 | (let ((oldpos (point))) |
| 397 | (and (re-search-forward sort-regexp-fields-regexp nil 'move) |
| 398 | (setq sort-regexp-record-end (match-end 0)) |
| 399 | (if (= sort-regexp-record-end oldpos) |
| 400 | (progn |
| 401 | (forward-char 1) |
| 402 | (re-search-forward sort-regexp-fields-regexp nil 'move) |
| 403 | (setq sort-regexp-record-end (match-end 0))) |
| 404 | t) |
| 405 | (goto-char (match-beginning 0))))) |
| 406 | |
| 407 | ;;;###autoload |
| 408 | (defun sort-regexp-fields (reverse record-regexp key-regexp beg end) |
| 409 | "Sort the text in the region region lexicographically. |
| 410 | If called interactively, prompt for two regular expressions, |
| 411 | RECORD-REGEXP and KEY-REGEXP. |
| 412 | |
| 413 | RECORD-REGEXP specifies the textual units to be sorted. |
| 414 | For example, to sort lines, RECORD-REGEXP would be \"^.*$\". |
| 415 | |
| 416 | KEY-REGEXP specifies the part of each record (i.e. each match for |
| 417 | RECORD-REGEXP) to be used for sorting. |
| 418 | If it is \"\\\\digit\", use the digit'th \"\\\\(...\\\\)\" |
| 419 | match field specified by RECORD-REGEXP. |
| 420 | If it is \"\\\\&\", use the whole record. |
| 421 | Otherwise, KEY-REGEXP should be a regular expression with which |
| 422 | to search within the record. If a match for KEY-REGEXP is not |
| 423 | found within a record, that record is ignored. |
| 424 | |
| 425 | With a negative prefix arg, sort in reverse order. |
| 426 | |
| 427 | The variable `sort-fold-case' determines whether alphabetic case affects |
| 428 | the sort order. |
| 429 | |
| 430 | For example: to sort lines in the region by the first word on each line |
| 431 | starting with the letter \"f\", |
| 432 | RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\\\=\\<f\\\\w*\\\\>\"" |
| 433 | ;; using negative prefix arg to mean "reverse" is now inconsistent with |
| 434 | ;; other sort-.*fields functions but then again this was before, since it |
| 435 | ;; didn't use the magnitude of the arg to specify anything. |
| 436 | (interactive "P\nsRegexp specifying records to sort: \n\ |
| 437 | sRegexp specifying key within record: \nr") |
| 438 | (cond ((or (equal key-regexp "") (equal key-regexp "\\&")) |
| 439 | (setq key-regexp 0)) |
| 440 | ((string-match "\\`\\\\[1-9]\\'" key-regexp) |
| 441 | (setq key-regexp (- (aref key-regexp 1) ?0)))) |
| 442 | (save-excursion |
| 443 | (save-restriction |
| 444 | (narrow-to-region beg end) |
| 445 | (goto-char (point-min)) |
| 446 | (let (sort-regexp-record-end |
| 447 | (sort-regexp-fields-regexp record-regexp)) |
| 448 | (re-search-forward sort-regexp-fields-regexp nil t) |
| 449 | (setq sort-regexp-record-end (point)) |
| 450 | (goto-char (match-beginning 0)) |
| 451 | (sort-subr reverse |
| 452 | 'sort-regexp-fields-next-record |
| 453 | (function (lambda () |
| 454 | (goto-char sort-regexp-record-end))) |
| 455 | (function (lambda () |
| 456 | (let ((n 0)) |
| 457 | (cond ((numberp key-regexp) |
| 458 | (setq n key-regexp)) |
| 459 | ((re-search-forward |
| 460 | key-regexp sort-regexp-record-end t) |
| 461 | (setq n 0)) |
| 462 | (t (throw 'key nil))) |
| 463 | (condition-case () |
| 464 | (cons (match-beginning n) |
| 465 | (match-end n)) |
| 466 | ;; if there was no such register |
| 467 | (error (throw 'key nil))))))))))) |
| 468 | |
| 469 | \f |
| 470 | (defvar sort-columns-subprocess t) |
| 471 | |
| 472 | ;;;###autoload |
| 473 | (defun sort-columns (reverse &optional beg end) |
| 474 | "Sort lines in region alphabetically by a certain range of columns. |
| 475 | For the purpose of this command, the region BEG...END includes |
| 476 | the entire line that point is in and the entire line the mark is in. |
| 477 | The column positions of point and mark bound the range of columns to sort on. |
| 478 | A prefix argument means sort into REVERSE order. |
| 479 | The variable `sort-fold-case' determines whether alphabetic case affects |
| 480 | the sort order. |
| 481 | |
| 482 | Note that `sort-columns' rejects text that contains tabs, |
| 483 | because tabs could be split across the specified columns |
| 484 | and it doesn't know how to handle that. Also, when possible, |
| 485 | it uses the `sort' utility program, which doesn't understand tabs. |
| 486 | Use \\[untabify] to convert tabs to spaces before sorting." |
| 487 | (interactive "P\nr") |
| 488 | (save-excursion |
| 489 | (let ;; To make `end-of-line' and etc. to ignore fields. |
| 490 | ((inhibit-field-text-motion t) |
| 491 | beg1 end1 col-beg1 col-end1 col-start col-end) |
| 492 | (goto-char (min beg end)) |
| 493 | (setq col-beg1 (current-column)) |
| 494 | (beginning-of-line) |
| 495 | (setq beg1 (point)) |
| 496 | (goto-char (max beg end)) |
| 497 | (setq col-end1 (current-column)) |
| 498 | (forward-line) |
| 499 | (setq end1 (point)) |
| 500 | (setq col-start (min col-beg1 col-end1)) |
| 501 | (setq col-end (max col-beg1 col-end1)) |
| 502 | (if (search-backward "\t" beg1 t) |
| 503 | (error "sort-columns does not work with tabs -- use M-x untabify")) |
| 504 | (if (not (or (memq system-type '(windows-nt)) |
| 505 | (let ((pos beg1) plist fontified) |
| 506 | (catch 'found |
| 507 | (while (< pos end1) |
| 508 | (setq plist (text-properties-at pos)) |
| 509 | (setq fontified (plist-get plist 'fontified)) |
| 510 | (while (consp plist) |
| 511 | (unless (or (eq (car plist) 'fontified) |
| 512 | (and (eq (car plist) 'face) |
| 513 | fontified)) |
| 514 | (throw 'found t)) |
| 515 | (setq plist (cddr plist))) |
| 516 | (setq pos (next-property-change pos nil end1))))))) |
| 517 | ;; Use the sort utility if we can; it is 4 times as fast. |
| 518 | ;; Do not use it if there are any non-font-lock properties |
| 519 | ;; in the region, since the sort utility would lose the |
| 520 | ;; properties. Tabs are used as field separator; on NetBSD, |
| 521 | ;; sort complains if "\n" is used as field separator. |
| 522 | (let ((sort-args (list (if reverse "-rt\t" "-t\t") |
| 523 | (format "-k1.%d,1.%d" |
| 524 | (1+ col-start) |
| 525 | (1+ col-end))))) |
| 526 | (when sort-fold-case |
| 527 | (push "-f" sort-args)) |
| 528 | (apply #'call-process-region beg1 end1 "sort" t t nil sort-args)) |
| 529 | ;; On ms-windows, use Emacs's own facilities. |
| 530 | (save-excursion |
| 531 | (save-restriction |
| 532 | (narrow-to-region beg1 end1) |
| 533 | (goto-char beg1) |
| 534 | (sort-subr reverse 'forward-line 'end-of-line |
| 535 | #'(lambda () (move-to-column col-start) nil) |
| 536 | #'(lambda () (move-to-column col-end) nil)))))))) |
| 537 | |
| 538 | ;;;###autoload |
| 539 | (defun reverse-region (beg end) |
| 540 | "Reverse the order of lines in a region. |
| 541 | From a program takes two point or marker arguments, BEG and END." |
| 542 | (interactive "r") |
| 543 | (if (> beg end) |
| 544 | (let (mid) (setq mid end end beg beg mid))) |
| 545 | (save-excursion |
| 546 | ;; put beg at the start of a line and end and the end of one -- |
| 547 | ;; the largest possible region which fits this criteria |
| 548 | (goto-char beg) |
| 549 | (or (bolp) (forward-line 1)) |
| 550 | (setq beg (point)) |
| 551 | (goto-char end) |
| 552 | ;; the test for bolp is for those times when end is on an empty line; |
| 553 | ;; it is probably not the case that the line should be included in the |
| 554 | ;; reversal; it isn't difficult to add it afterward. |
| 555 | (or (and (eolp) (not (bolp))) (progn (forward-line -1) (end-of-line))) |
| 556 | (setq end (point-marker)) |
| 557 | ;; the real work. this thing cranks through memory on large regions. |
| 558 | (let (ll (do t)) |
| 559 | (while do |
| 560 | (goto-char beg) |
| 561 | (setq ll (cons (buffer-substring (point) (progn (end-of-line) (point))) |
| 562 | ll)) |
| 563 | (setq do (/= (point) end)) |
| 564 | (delete-region beg (if do (1+ (point)) (point)))) |
| 565 | (while (cdr ll) |
| 566 | (insert (car ll) "\n") |
| 567 | (setq ll (cdr ll))) |
| 568 | (insert (car ll))))) |
| 569 | |
| 570 | ;;;###autoload |
| 571 | (defun delete-duplicate-lines (beg end &optional reverse adjacent keep-blanks |
| 572 | interactive) |
| 573 | "Delete duplicate lines in the region between BEG and END. |
| 574 | |
| 575 | If REVERSE is nil, search and delete duplicates forward keeping the first |
| 576 | occurrence of duplicate lines. If REVERSE is non-nil (when called |
| 577 | interactively with C-u prefix), search and delete duplicates backward |
| 578 | keeping the last occurrence of duplicate lines. |
| 579 | |
| 580 | If ADJACENT is non-nil (when called interactively with two C-u prefixes), |
| 581 | delete repeated lines only if they are adjacent. It works like the utility |
| 582 | `uniq' and is useful when lines are already sorted in a large file since |
| 583 | this is more efficient in performance and memory usage than when ADJACENT |
| 584 | is nil that uses additional memory to remember previous lines. |
| 585 | |
| 586 | If KEEP-BLANKS is non-nil (when called interactively with three C-u prefixes), |
| 587 | duplicate blank lines are preserved. |
| 588 | |
| 589 | When called from Lisp and INTERACTIVE is omitted or nil, return the number |
| 590 | of deleted duplicate lines, do not print it; if INTERACTIVE is t, the |
| 591 | function behaves in all respects as if it had been called interactively." |
| 592 | (interactive |
| 593 | (progn |
| 594 | (barf-if-buffer-read-only) |
| 595 | (list (region-beginning) (region-end) |
| 596 | (equal current-prefix-arg '(4)) |
| 597 | (equal current-prefix-arg '(16)) |
| 598 | (equal current-prefix-arg '(64)) |
| 599 | t))) |
| 600 | (let ((lines (unless adjacent (make-hash-table :weakness 'key :test 'equal))) |
| 601 | line prev-line |
| 602 | (count 0) |
| 603 | (beg (copy-marker beg)) |
| 604 | (end (copy-marker end))) |
| 605 | (save-excursion |
| 606 | (goto-char (if reverse end beg)) |
| 607 | (if (and reverse (bolp)) (forward-char -1)) |
| 608 | (while (if reverse |
| 609 | (and (> (point) beg) (not (bobp))) |
| 610 | (and (< (point) end) (not (eobp)))) |
| 611 | (setq line (buffer-substring-no-properties |
| 612 | (line-beginning-position) (line-end-position))) |
| 613 | (if (and keep-blanks (string= "" line)) |
| 614 | (forward-line 1) |
| 615 | (if (if adjacent (equal line prev-line) (gethash line lines)) |
| 616 | (progn |
| 617 | (delete-region (progn (forward-line 0) (point)) |
| 618 | (progn (forward-line 1) (point))) |
| 619 | (if reverse (forward-line -1)) |
| 620 | (setq count (1+ count))) |
| 621 | (if adjacent (setq prev-line line) (puthash line t lines)) |
| 622 | (forward-line (if reverse -1 1)))))) |
| 623 | (set-marker beg nil) |
| 624 | (set-marker end nil) |
| 625 | (when interactive |
| 626 | (message "Deleted %d %sduplicate line%s%s" |
| 627 | count |
| 628 | (if adjacent "adjacent " "") |
| 629 | (if (= count 1) "" "s") |
| 630 | (if reverse " backward" ""))) |
| 631 | count)) |
| 632 | |
| 633 | (provide 'sort) |
| 634 | |
| 635 | ;;; sort.el ends here |