comparison lisp/sort.el @ 4238:6a22eb586080

(sort-skip-fields): Really implement fields as runs of nonwhitespace chars. (sort-fields, sort-float-fields, sort-numeric-fields): Don't subtract 1 when calling sort-skip-fields.
author Richard M. Stallman <rms@gnu.org>
date Fri, 23 Jul 1993 04:44:06 +0000
parents 507f64624555
children 2b0671553d8c
comparison
equal deleted inserted replaced
4237:589ab12dbe3d 4238:6a22eb586080
251 FIELD, BEG and END. BEG and END specify region to sort. 251 FIELD, BEG and END. BEG and END specify region to sort.
252 If you want to sort floating-point numbers, try `sort-float-fields'." 252 If you want to sort floating-point numbers, try `sort-float-fields'."
253 (interactive "p\nr") 253 (interactive "p\nr")
254 (sort-fields-1 field beg end 254 (sort-fields-1 field beg end
255 (function (lambda () 255 (function (lambda ()
256 (sort-skip-fields (1- field)) 256 (sort-skip-fields field)
257 (string-to-number 257 (string-to-number
258 (buffer-substring 258 (buffer-substring
259 (point) 259 (point)
260 (save-excursion 260 (save-excursion
261 ;; This is just wrong! Even without floats... 261 ;; This is just wrong! Even without floats...
273 program, there are three arguments: FIELD, BEG and END. BEG and END specify 273 program, there are three arguments: FIELD, BEG and END. BEG and END specify
274 region to sort." 274 region to sort."
275 (interactive "p\nr") 275 (interactive "p\nr")
276 (sort-fields-1 field beg end 276 (sort-fields-1 field beg end
277 (function (lambda () 277 (function (lambda ()
278 (sort-skip-fields (1- field)) 278 (sort-skip-fields field)
279 (string-to-number 279 (string-to-number
280 (buffer-substring 280 (buffer-substring
281 (point) 281 (point)
282 (save-excursion 282 (save-excursion
283 (re-search-forward 283 (re-search-forward
293 Called from a program, there are three arguments: 293 Called from a program, there are three arguments:
294 FIELD, BEG and END. BEG and END specify region to sort." 294 FIELD, BEG and END. BEG and END specify region to sort."
295 (interactive "p\nr") 295 (interactive "p\nr")
296 (sort-fields-1 field beg end 296 (sort-fields-1 field beg end
297 (function (lambda () 297 (function (lambda ()
298 (sort-skip-fields (1- field)) 298 (sort-skip-fields field)
299 nil)) 299 nil))
300 (function (lambda () (skip-chars-forward "^ \t\n"))))) 300 (function (lambda () (skip-chars-forward "^ \t\n")))))
301 301
302 (defun sort-fields-1 (field beg end startkeyfun endkeyfun) 302 (defun sort-fields-1 (field beg end startkeyfun endkeyfun)
303 (let ((tbl (syntax-table))) 303 (let ((tbl (syntax-table)))
311 (sort-subr nil 311 (sort-subr nil
312 'forward-line 'end-of-line 312 'forward-line 'end-of-line
313 startkeyfun endkeyfun))) 313 startkeyfun endkeyfun)))
314 (set-syntax-table tbl)))) 314 (set-syntax-table tbl))))
315 315
316 ;; Position at the beginning of field N on the current line,
317 ;; assuming point is initially at the beginning of the line.
316 (defun sort-skip-fields (n) 318 (defun sort-skip-fields (n)
317 (let ((bol (point)) 319 (if (> n 0)
318 (eol (save-excursion (end-of-line 1) (point)))) 320 ;; Skip across N - 1 fields.
319 (if (> n 0) (forward-word n) 321 (let ((i (1- n)))
320 (end-of-line) 322 (while (> i 0)
321 (forward-word (1+ n))) 323 (skip-chars-forward " \t")
322 (if (or (and (>= (point) eol) (> n 0)) 324 (skip-chars-forward "^ \t\n")
323 ;; this is marginally wrong; if the first line of the sort 325 (setq i (1- i)))
324 ;; at bob has the wrong number of fields the error won't be 326 (skip-chars-forward " \t")
325 ;; reported until the next short line. 327 (recursive-edit)
326 (and (< (point) bol) (< n 0))) 328 (if (eolp)
329 (error "Line has too few fields: %s"
330 (buffer-substring
331 (save-excursion (beginning-of-line) (point))
332 (save-excursion (end-of-line) (point))))))
333 (end-of-line)
334 ;; Skip back across - N - 1 fields.
335 (let ((i (1- (- n))))
336 (while (> i 0)
337 (skip-chars-backward " \t")
338 (skip-chars-backward "^ \t\n")
339 (setq i (1- i)))
340 (skip-chars-backward " \t"))
341 (if (bolp)
327 (error "Line has too few fields: %s" 342 (error "Line has too few fields: %s"
328 (buffer-substring bol eol))) 343 (buffer-substring
329 (skip-chars-forward " \t"))) 344 (save-excursion (beginning-of-line) (point))
330 345 (save-excursion (end-of-line) (point)))))
346 ;; Position at the front of the field
347 ;; even if moving backwards.
348 (skip-chars-backward "^ \t\n")))
331 349
332 ;;;###autoload 350 ;;;###autoload
333 (defun sort-regexp-fields (reverse record-regexp key-regexp beg end) 351 (defun sort-regexp-fields (reverse record-regexp key-regexp beg end)
334 "Sort the region lexicographically as specified by RECORD-REGEXP and KEY. 352 "Sort the region lexicographically as specified by RECORD-REGEXP and KEY.
335 RECORD-REGEXP specifies the textual units which should be sorted. 353 RECORD-REGEXP specifies the textual units which should be sorted.