Mercurial > emacs
view lisp/sort.el @ 96044:c1ef445563bb
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-colview.el (org-columns-next-allowed-value): Bug fix.
* org-colview-xemacs.el (org-columns-next-allowed-value): Bug fix.
* org-agenda.el (org-agenda-get-closed): Get the end time into the
agenda prefix as well.
* org-publish.el (org-publish-org-index): Make a properly indented
list.
* org.el (org-calendar-agenda-action-key): New option.
(org-get-cursor-date): New function.
(org-mark-entry-for-agenda-action): New command.
(org-overriding-default-time): New variable.
(org-read-date): Respect `org-overriding-default-time'.
* org-remember.el (org-remember-apply-template): Respect the
ovverriding default time.
* org-agenda.el (org-agenda-action-marker): New variable.
(org-agenda-action): New command.
(org-agenda-do-action): New function.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-schedule, org-deadline): Protect scheduled and
deadline tasks against changes that accidently remove the
repeater. Also show a message with the new date when done.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-beginning-of-line): Cater for the case when there
are tags but no headline text.
(org-align-tags-here): Convert to tabs only when indent-tabs-mode
it set.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-mhe.el (org-mhe-get-message-folder-from-index): Make sure
the return value is nil instead of "nil" when there is no match.
* org-exp.el (org-insert-centered): Use fill-column instead of
80.
(org-export-as-ascii): Use string-width to measure the width of
the heading.
* org.el (org-diary-to-ical-string): No longer kill buffer
FROMBUF, this is now done by the caller.
* org-exp.el (org-print-icalendar-entries): Move the call to
`org-diary-to-ical-string' out of the loop, and kill the buffer
afterwords.
* org-remember.el (org-remember-visit-immediately): Position
cursor after moving to the note.
(org-remember-apply-template): Use a text property to record the
cursor position.
(org-remember-handler): Align tags after pasting the note.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-bbdb.el (org-bbdb-follow-anniversary-link): New function.
* org-agenda.el (org-agenda-open-link): If there is an
org-bbdb-name property in the current line, jump to that bbdb
entry.
* org-bbdb.el (org-bbdb-anniversaries): Add the bbdb-name as a
text property, so that the agenda knows where this entry comes
from.
* org-agenda.el (org-agenda-clock-in): Fixed bug in the
interaction between clocking-in from the agenda, and automatic
task state switching.
* org-macs.el (org-with-point-at): Bug fix in macro defintion.
* org.el (org-beginning-of-line, org-end-of-line): Make sure the
zmacs-region stays after this command in XEmacs.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-scan-tags): Allow new values for ACTION parameter.
* org-remember.el (org-remember-templates): Fix bug in
customization type definition.
* org.el (org-map-entries): New function.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-agenda.el (org-agenda-skip-comment-trees): New option.
(org-agenda-skip): Respect `org-agenda-skip-comment-trees'.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-remember.el (org-jump-to-target-location): New variable.
(org-remember-apply-template): Set
`org-remember-apply-template' if requested by template.
(org-remember-handler): Start an idle timer to jump to
remember location.
* org-exp.el (org-get-current-options): Add the FILETAGS setting.
* org.el (org-set-regexps-and-options): Fix bug with parsing of
file tags.
(org-get-tags-at): Add the content of `org-file-tags'.
* org-exp.el (org-export-handle-comments): Fix bug with several
comment lines after each other.
(org-number-to-roman, org-number-to-counter): New functions.
(org-export-section-number-format): New option.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-exp.el (org-export-protect-examples): Catch the case of a
missing end_example line.
* org.el (org-set-regexps-and-options): Set `org-file-properties' and
`org-file-tags' to nil.
* org-colview.el (org-columns-next-allowed-value): Handle next
argument NTH to directly select a value.
* org-colview-xemacs.el (org-columns-next-allowed-value): Handle next
argument NTH to directly select a value.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-agenda.el (org-agenda-scheduled-leaders): Fix docstring.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-columns-ellipses): New option.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-colview.el (org-columns-add-ellipses): New function.
(org-columns-compact-links): New function.
(org-columns-cleanup-item): Call `org-columns-compact-links'.
(org-columns-display-here): Call `org-agenda-columns-cleanup-item'
when in agenda.
(org-columns-edit-value): Fixed bug with editing values from
agenda column view.
(org-columns-redo): Also redo the agenda itself.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-agenda.el (org-agenda-columns-remove-prefix-from-item): New
option.
* org-colview.el (org-agenda-columns-cleanup-item): New function.
* org-exp.el (org-export-ascii-preprocess): Renamed from
`org-export-ascii-clean-string'.
(org-export-kill-licensed-text)
(org-export-define-heading-targets)
(org-export-handle-invisible-targets)
(org-export-target-internal-links)
(org-export-remove-or-extract-drawers)
(org-export-remove-archived-trees)
(org-export-protect-quoted-subtrees)
(org-export-protect-verbatim, org-export-protect-examples)
(org-export-select-backend-specific-text)
(org-export-mark-blockquote-and-verse)
(org-export-remove-comment-blocks-and-subtrees)
(org-export-handle-comments, org-export-mark-radio-links)
(org-export-remove-special-table-lines)
(org-export-normalize-links)
(org-export-concatenate-multiline-links)
(org-export-concatenate-multiline-emphasis): New functions,
obtained from spliiting the export preprocessor.
* org-table.el (org-table-recalculate): Improve error message if
the row number is invalid.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-archive.el (org-archive-save-context-info): Fix bugs in
customization setup and docstring.
* org-exp.el (org-export-html-style): Changed the size of in the
<pre> element to 90%.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-find-src-example-start): Function removed.
(org-edit-src-find-region-and-lang): New function.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-edit-src-exit): New function.
(org-exit-edit-mode): New minor mode.
* org-exp.el (org-export-preprocess-string): Fix bug with removing
comment-like lines from protected examples.
* org.el (org-edit-src-example, org-find-src-example-start)
(org-protect-source-example, org-edit-special): New functions.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-publish.el (org-publish-project-alist): Fix typo in
docstring.
(org-publish-project-alist): Handle :index-title property.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-export-latex.el (org-export-as-latex): Make sure region
bounds are correct. Parse subtree properties relating to export.
* org-exp.el (org-export-add-options-to-plist): New function.
(org-infile-export-plist): Use `org-export-add-options-to-plist'.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-default-properties): Add EXPORT_FILE_NAME and
EXPORT_TITLE.
* org-exp.el (org-export-get-title-from-subtree)
(org-export-as-ascii, org-export-as-html): Make sure the original
region-beginning and region-end are used, even after moving
point.
(org-export-get-title-from-subtree): Also try the EXPORT_TITLE
property.
* org-remember.el (org-remember-last-stored-marker): New variable.
(org-remember-goto-last-stored): Use `org-goto-marker-or-bmk'.
(org-remember-handler): Also use marker to remember
last-stored position.
* org.el (org-goto-marker-or-bmk): New function.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-file-properties): Renamed from `org-local-properties'.
(org-scan-tags): Take file tags into account.
(org-tags-match-list-sublevels): Default changed to t.
* org-exp.el (org-export-as-html): Close paragraph after a
footnote.
* org.el (org-update-parent-todo-statistics): New function.
* org-exp.el (org-icalendar-store-UID): New option.
(org-icalendar-force-UID): Option removed.
(org-print-icalendar-entries): IMplement UIDs.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-mhe.el (org-mhe-follow-link): Fix bug in mhe searches.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-faces.el (org-column): Document how this face is being used
and why sometimes the background faces shine through.
* org-mhe.el (org-mhe-follow-link): Improve handling of searches.
* org-publish.el (org-publish-attachment): Create publishing
directory if it does not yet exist.
* org-table.el (org-calc-default-modes): Change default number
format to (float 8).
* org.el (org-olpath-completing-read): New function.
(org-time-clocksum-format): New option.
(org-minutes-to-hh:mm-string): Use `org-time-clocksum-format'.
* org-clock.el (org-clock-display, org-clock-out)
(org-update-mode-line): Use `org-time-clocksum-format'.
* org-colview-xemacs.el (org-columns-number-to-string): Use
`org-time-clocksum-format'.
* org-colview.el (org-columns-number-to-string): Use
`org-time-clocksum-format'.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-id.el: New file, move from contrib to core.
* org-exp.el (org-icalendar-force-UID): New option.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-exp.el (org-print-icalendar-entries): Make sure DTEND is
shifted by one day if theere is a date range without an end
time.
* org.el (org-try-structure-completion): New function.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-set-font-lock-defaults): Improve fontification of
description lists.
(org-insert-item): Handle description lists.
(org-adaptive-fill-function): Improve auto indentation in
description lists.
* org-exp.el (org-export-as-html, org-export-preprocess-string):
Implement VERSE environment.
(org-export-preprocess-string): Implement the COMMENT
environment.
* org-export-latex.el (org-export-latex-preprocess): Implement
VERSE environment.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-jsinfo.el (org-infojs-opts-table): Add entry for FIXED_TOC
option.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-table.el (orgtbl-to-tsv, orgtbl-to-csv): New functions.
* org.el (org-quote-csv-field): New functions.
* org-table.el (org-table-export-default-format): Remove :splice
from default format, we get the same effect by not specifying
:tstart and :tend.
(org-table-export): Improve setup, distinguish better between
interactive and non-interactive use, allow specifying the format
on the fly, better protection against wrong file names.
(orgtbl-to-generic): Fix documentation. Do not require :tstart
and :tend when :splice is omitted.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-clock.el (org-clock-select-task): Make sure the selection
letters are 1-9 and A-Z, no special characters.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-exp.el (org-export-htmlize): New group.
(org-export-htmlize-output-type)
(org-export-htmlize-css-font-prefix): New options.
(org-export-htmlize-region-for-paste): New function.
(org-export-htmlize-generate-css): New command.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-set-visibility-according-to-property): New function.
(org-ctrl-c-ctrl-c): Do not restart org-mode, just get the options
and compute the regular expressions, and update font-lock.
(org-property-re): Allow a dash in property names.
* org-archive.el (org-extract-archive-file): Insert the file name
without the path into the format, to allow the location format to
contain a subdirectory.
* org-agenda.el (org-agenda-post-command-hook): If point is at end
of buffer, and the `org-agenda-type' property undefined, use the
value from the character before.
* org.el (org-add-planning-info): Don't let indentation for
would-be timestamp become extra whitespace at the end of headline.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-remove-double-quotes, org-file-contents): New
functions.
* org-exp.el (org-infile-export-plist): Also parse the
contents of #+SETUPFILE files, recursively.
* org.el (org-set-regexps-and-options): Also parse the
contents of #+SETUPFILE files, recursively.
* org-exp.el (org-export-handle-include-files): New function.
(org-export-preprocess-string): Call
`org-export-handle-include-files'.
* org.el (org-delete-property-globally)
(org-delete-property, org-set-property): Ignore case during
completion.
(org-set-property): Use `org-completing-read' instead of
`completing-read'.
* org.el (org-complete-expand-structure-template): New,
experimental function.
(org-structure-template-alist): New, experimental option.
(org-complete): Call `org-complete-expand-structure-template'.
2008-06-17 Bastien Guerry <bzg@altern.org>
* org-export-latex.el (org-export-latex-preprocess): Added
support for blockquotes.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-read-date-analyze): Catch the case where only a
weekday is given.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-set-font-lock-defaults): Make the description
tag bold.
* org-exp.el (org-export-as-html, org-close-li): Implement
description lists.
2008-06-17 Jason Riedy <jason@acm.org>
* org-table.el (*orgtbl-default-fmt*): New variable.
(orgtbl-format-line): Use the value of *orgtbl-default-fmt*
when there is no other fmt available.
(orgtbl-to-generic): Allow an explicitly nil :tstart or
:tend to suppress the appropriate string.
(orgtbl-to-orgtbl): New function for translating to another orgtbl
table.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.el (org-read-date-analyze): "." as an alias for "+0" in
read date.
* org-clock.el (org-clock-save-markers-for-cut-and-paste):
New function.
* org-agenda.el (org-agenda-save-markers-for-cut-and-paste):
New function.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-clock.el (org-clock-find-position): Don't include notes
into clock drawer.
* org-archive.el (org-archive-subtree): No longer remove an
extra line after cutting the subtree. `org-cut-subtree' already
takes care of this.
* org-remember.el (org-remember-handler): Only kill the target
buffer if it does not contain the running clock.
* org.el (org-markers-to-move): New variable.
(org-save-markers-in-region, org-check-and-save-marker)
(org-reinstall-markers-in-region): New function.
(org-move-subtree-down, org-copy-subtree): Remember relative
marker positions before cutting.
(org-move-subtree-down, org-paste-subtree): Restore relative
marker positions after pasting.
* org-remember.el (org-remember-clock-out-on-exit): New option.
(org-remember-finalize): Clock out only if the setting in
`org-remember-clock-out-on-exit' requires it.
(org-remember-handler): Do the cleanup in the buffer, to make sure
that the clock marker remains in tact.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-clock.el (org-clock-goto): Widen buffer if necessary.
(org-clock-in): Make sure that also tasks outside the narrowed
region will be clocked in correctly.
(org-clock-insert-selection-line): Widen the buffer so that we can
find the correct task heading.
* org.el (org-base-buffer): New function.
* org-exp.el (org-icalendar-cleanup-string): Make sure ',"
and ";" are escaped.
(org-print-icalendar-entries): Also apply
`org-icalendar-cleanup-string' to the headline, not only to the
summary property.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org-exp.el (org-export-preprocess-hook): New hook.
(org-export-preprocess-string): Call
`org-export-preprocess-hook'.
* org.el (org-font-lock-hook): New variable.
(org-font-lock-hook): New function.
(org-set-font-lock-defaults): Call `org-font-lock-hook'.
2008-06-17 Carsten Dominik <dominik@science.uva.nl>
* org.texi: Modify license to no longer include back- and front
cover matters.
(Using the mapping API): New section.
(Agenda column view): New section.
(Moving subtrees): Document archiving to the archive
sibling.
(Agenda commands): Document columns view in the agenda.
(Using the property API): Document the API for
multi-valued properties.
author | Carsten Dominik <dominik@science.uva.nl> |
---|---|
date | Tue, 17 Jun 2008 15:22:00 +0000 |
parents | ee5932bf781d |
children | c3512b2085a0 |
line wrap: on
line source
;;; sort.el --- commands to sort text in an Emacs buffer ;; Copyright (C) 1986, 1987, 1994, 1995, 2001, 2002, 2003, ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. ;; Author: Howie Kaye ;; Maintainer: FSF ;; Keywords: unix ;; This file is part of GNU Emacs. ;; GNU Emacs is free software: you can redistribute it and/or modify ;; it under the terms of the GNU General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; GNU Emacs is distributed in the hope that it will be useful, ;; but WITHOUT ANY WARRANTY; without even the implied warranty of ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ;; GNU General Public License for more details. ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. ;;; Commentary: ;; This package provides the sorting facilities documented in the Emacs ;; user's manual. ;;; Code: (defgroup sort nil "Commands to sort text in an Emacs buffer." :group 'data) (defcustom sort-fold-case nil "*Non-nil if the buffer sort functions should ignore case." :group 'sort :type 'boolean) ;;;###autoload(put 'sort-fold-case 'safe-local-variable 'booleanp) ;;;###autoload (defun sort-subr (reverse nextrecfun endrecfun &optional startkeyfun endkeyfun predicate) "General text sorting routine to divide buffer into records and sort them. We divide the accessible portion of the buffer into disjoint pieces called sort records. A portion of each sort record (perhaps all of it) is designated as the sort key. The records are rearranged in the buffer in order by their sort keys. The records may or may not be contiguous. Usually the records are rearranged in order of ascending sort key. If REVERSE is non-nil, they are rearranged in order of descending sort key. The variable `sort-fold-case' determines whether alphabetic case affects the sort order. The next four arguments are functions to be called to move point across a sort record. They will be called many times from within sort-subr. NEXTRECFUN is called with point at the end of the previous record. It moves point to the start of the next record. It should move point to the end of the buffer if there are no more records. The first record is assumed to start at the position of point when sort-subr is called. ENDRECFUN is called with point within the record. It should move point to the end of the record. STARTKEYFUN moves from the start of the record to the start of the key. It may return either a non-nil value to be used as the key, or else the key is the substring between the values of point after STARTKEYFUN and ENDKEYFUN are called. If STARTKEYFUN is nil, the key starts at the beginning of the record. ENDKEYFUN moves from the start of the sort key to the end of the sort key. ENDKEYFUN may be nil if STARTKEYFUN returns a value or if it would be the same as ENDRECFUN. PREDICATE is the function to use to compare keys. If keys are numbers, it defaults to `<', otherwise it defaults to `string<'." ;; Heuristically try to avoid messages if sorting a small amt of text. (let ((messages (> (- (point-max) (point-min)) 50000))) (save-excursion (if messages (message "Finding sort keys...")) (let* ((sort-lists (sort-build-lists nextrecfun endrecfun startkeyfun endkeyfun)) (old (reverse sort-lists)) (case-fold-search sort-fold-case)) (if (null sort-lists) () (or reverse (setq sort-lists (nreverse sort-lists))) (if messages (message "Sorting records...")) (setq sort-lists (sort sort-lists (cond (predicate `(lambda (a b) (,predicate (car a) (car b)))) ((numberp (car (car sort-lists))) 'car-less-than-car) ((consp (car (car sort-lists))) (lambda (a b) (> 0 (compare-buffer-substrings nil (car (car a)) (cdr (car a)) nil (car (car b)) (cdr (car b)))))) (t (lambda (a b) (string< (car a) (car b))))))) (if reverse (setq sort-lists (nreverse sort-lists))) (if messages (message "Reordering buffer...")) (sort-reorder-buffer sort-lists old))) (if messages (message "Reordering buffer... Done")))) nil) ;; Parse buffer into records using the arguments as Lisp expressions; ;; return a list of records. Each record looks like (KEY STARTPOS . ENDPOS) ;; where KEY is the sort key (a number or string), ;; and STARTPOS and ENDPOS are the bounds of this record in the buffer. ;; The records appear in the list lastmost first! (defun sort-build-lists (nextrecfun endrecfun startkeyfun endkeyfun) (let ((sort-lists ()) (start-rec nil) done key) ;; Loop over sort records. ;(goto-char (point-min)) -- it is the caller's responsibility to ;arrange this if necessary (while (not (eobp)) (setq start-rec (point)) ;save record start (setq done nil) ;; Get key value, or move to start of key. (setq key (catch 'key (or (and startkeyfun (funcall startkeyfun)) ;; If key was not returned as value, ;; move to end of key and get key from the buffer. (let ((start (point))) (funcall (or endkeyfun (prog1 endrecfun (setq done t)))) (cons start (point)))))) ;; Move to end of this record (start of next one, or end of buffer). (cond ((prog1 done (setq done nil))) (endrecfun (funcall endrecfun)) (nextrecfun (funcall nextrecfun) (setq done t))) (if key (push ;; consing optimization in case in which key is same as record. (if (and (consp key) (equal (car key) start-rec) (equal (cdr key) (point))) (cons key key) (cons key (cons start-rec (point)))) sort-lists)) (and (not done) nextrecfun (funcall nextrecfun))) sort-lists)) (defun sort-reorder-buffer (sort-lists old) (let ((last (point-min)) (min (point-min)) (max (point-max)) (old-buffer (current-buffer)) (mb enable-multibyte-characters) temp-buffer) (with-temp-buffer (set-buffer-multibyte mb) ;; Record the temporary buffer. (setq temp-buffer (current-buffer)) ;; Copy the sorted text into the temporary buffer. (while sort-lists (goto-char (point-max)) (insert-buffer-substring old-buffer last (nth 1 (car old))) (goto-char (point-max)) (insert-buffer-substring old-buffer (nth 1 (car sort-lists)) (cdr (cdr (car sort-lists)))) (setq last (cdr (cdr (car old))) sort-lists (cdr sort-lists) old (cdr old))) (goto-char (point-max)) (insert-buffer-substring old-buffer last max) ;; Copy the reordered text from the temporary buffer ;; to the buffer we sorted (OLD-BUFFER). (set-buffer old-buffer) (let ((inhibit-quit t)) ;; Make sure insertions done for reordering ;; saves any markers at the end of the sorted region, ;; by leaving the last character of the region. (delete-region min (1- max)) ;; Now replace the one remaining old character with the sorted text. (goto-char (point-min)) (insert-buffer-substring temp-buffer) (delete-region max (1+ max)))))) ;;;###autoload (defun sort-lines (reverse beg end) "Sort lines in region alphabetically; argument means descending order. Called from a program, there are three arguments: REVERSE (non-nil means reverse order), BEG and END (region to sort). The variable `sort-fold-case' determines whether alphabetic case affects the sort order." (interactive "P\nr") (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (let ;; To make `end-of-line' and etc. to ignore fields. ((inhibit-field-text-motion t)) (sort-subr reverse 'forward-line 'end-of-line))))) ;;;###autoload (defun sort-paragraphs (reverse beg end) "Sort paragraphs in region alphabetically; argument means descending order. Called from a program, there are three arguments: REVERSE (non-nil means reverse order), BEG and END (region to sort). The variable `sort-fold-case' determines whether alphabetic case affects the sort order." (interactive "P\nr") (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (sort-subr reverse (function (lambda () (while (and (not (eobp)) (looking-at paragraph-separate)) (forward-line 1)))) 'forward-paragraph)))) ;;;###autoload (defun sort-pages (reverse beg end) "Sort pages in region alphabetically; argument means descending order. Called from a program, there are three arguments: REVERSE (non-nil means reverse order), BEG and END (region to sort). The variable `sort-fold-case' determines whether alphabetic case affects the sort order." (interactive "P\nr") (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (sort-subr reverse (function (lambda () (skip-chars-forward "\n"))) 'forward-page)))) (defvar sort-fields-syntax-table nil) (if sort-fields-syntax-table nil (let ((table (make-syntax-table)) (i 0)) (while (< i 256) (modify-syntax-entry i "w" table) (setq i (1+ i))) (modify-syntax-entry ?\s " " table) (modify-syntax-entry ?\t " " table) (modify-syntax-entry ?\n " " table) (modify-syntax-entry ?\. "_" table) ; for floating pt. numbers. -wsr (setq sort-fields-syntax-table table))) (defcustom sort-numeric-base 10 "*The default base used by `sort-numeric-fields'." :group 'sort :type 'integer) ;;;###autoload(put 'sort-numeric-base 'safe-local-variable 'integerp) ;;;###autoload (defun sort-numeric-fields (field beg end) "Sort lines in region numerically by the ARGth field of each line. Fields are separated by whitespace and numbered from 1 up. Specified field must contain a number in each line of the region, which may begin with \"0x\" or \"0\" for hexadecimal and octal values. Otherwise, the number is interpreted according to sort-numeric-base. With a negative arg, sorts by the ARGth field counted from the right. Called from a program, there are three arguments: FIELD, BEG and END. BEG and END specify region to sort." (interactive "p\nr") (let ;; To make `end-of-line' and etc. to ignore fields. ((inhibit-field-text-motion t)) (sort-fields-1 field beg end (lambda () (sort-skip-fields field) (let* ((case-fold-search t) (base (if (looking-at "\\(0x\\)[0-9a-f]\\|\\(0\\)[0-7]") (cond ((match-beginning 1) (goto-char (match-end 1)) 16) ((match-beginning 2) (goto-char (match-end 2)) 8) (t nil))))) (string-to-number (buffer-substring (point) (save-excursion (forward-sexp 1) (point))) (or base sort-numeric-base)))) nil))) ;;;;;###autoload ;;(defun sort-float-fields (field beg end) ;; "Sort lines in region numerically by the ARGth field of each line. ;;Fields are separated by whitespace and numbered from 1 up. Specified field ;;must contain a floating point number in each line of the region. With a ;;negative arg, sorts by the ARGth field counted from the right. Called from a ;;program, there are three arguments: FIELD, BEG and END. BEG and END specify ;;region to sort." ;; (interactive "p\nr") ;; (sort-fields-1 field beg end ;; (function (lambda () ;; (sort-skip-fields field) ;; (string-to-number ;; (buffer-substring ;; (point) ;; (save-excursion ;; (re-search-forward ;; "[+-]?[0-9]*\.?[0-9]*\\([eE][+-]?[0-9]+\\)?") ;; (point)))))) ;; nil)) ;;;###autoload (defun sort-fields (field beg end) "Sort lines in region lexicographically by the ARGth field of each line. Fields are separated by whitespace and numbered from 1 up. With a negative arg, sorts by the ARGth field counted from the right. Called from a program, there are three arguments: FIELD, BEG and END. BEG and END specify region to sort. The variable `sort-fold-case' determines whether alphabetic case affects the sort order." (interactive "p\nr") (let ;; To make `end-of-line' and etc. to ignore fields. ((inhibit-field-text-motion t)) (sort-fields-1 field beg end (function (lambda () (sort-skip-fields field) nil)) (function (lambda () (skip-chars-forward "^ \t\n")))))) (defun sort-fields-1 (field beg end startkeyfun endkeyfun) (let ((tbl (syntax-table))) (if (zerop field) (setq field 1)) (unwind-protect (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (set-syntax-table sort-fields-syntax-table) (sort-subr nil 'forward-line 'end-of-line startkeyfun endkeyfun))) (set-syntax-table tbl)))) ;; Position at the beginning of field N on the current line, ;; assuming point is initially at the beginning of the line. (defun sort-skip-fields (n) (if (> n 0) ;; Skip across N - 1 fields. (let ((i (1- n))) (while (> i 0) (skip-chars-forward " \t") (skip-chars-forward "^ \t\n") (setq i (1- i))) (skip-chars-forward " \t") (if (eolp) (error "Line has too few fields: %s" (buffer-substring (save-excursion (beginning-of-line) (point)) (save-excursion (end-of-line) (point)))))) (end-of-line) ;; Skip back across - N - 1 fields. (let ((i (1- (- n)))) (while (> i 0) (skip-chars-backward " \t") (skip-chars-backward "^ \t\n") (setq i (1- i))) (skip-chars-backward " \t")) (if (bolp) (error "Line has too few fields: %s" (buffer-substring (save-excursion (beginning-of-line) (point)) (save-excursion (end-of-line) (point))))) ;; Position at the front of the field ;; even if moving backwards. (skip-chars-backward "^ \t\n"))) (defvar sort-regexp-fields-regexp) (defvar sort-regexp-record-end) ;; Move to the beginning of the next match for record-regexp, ;; and set sort-regexp-record-end to the end of that match. ;; If the next match is empty and does not advance point, ;; skip one character and try again. (defun sort-regexp-fields-next-record () (let ((oldpos (point))) (and (re-search-forward sort-regexp-fields-regexp nil 'move) (setq sort-regexp-record-end (match-end 0)) (if (= sort-regexp-record-end oldpos) (progn (forward-char 1) (re-search-forward sort-regexp-fields-regexp nil 'move) (setq sort-regexp-record-end (match-end 0))) t) (goto-char (match-beginning 0))))) ;;;###autoload (defun sort-regexp-fields (reverse record-regexp key-regexp beg end) "Sort the region lexicographically as specified by RECORD-REGEXP and KEY. RECORD-REGEXP specifies the textual units which should be sorted. For example, to sort lines RECORD-REGEXP would be \"^.*$\" KEY specifies the part of each record (ie each match for RECORD-REGEXP) is to be used for sorting. If it is \"\\\\digit\" then the digit'th \"\\\\(...\\\\)\" match field from RECORD-REGEXP is used. If it is \"\\\\&\" then the whole record is used. Otherwise, it is a regular-expression for which to search within the record. If a match for KEY is not found within a record then that record is ignored. With a negative prefix arg sorts in reverse order. The variable `sort-fold-case' determines whether alphabetic case affects the sort order. For example: to sort lines in the region by the first word on each line starting with the letter \"f\", RECORD-REGEXP would be \"^.*$\" and KEY would be \"\\\\=\\<f\\\\w*\\\\>\"" ;; using negative prefix arg to mean "reverse" is now inconsistent with ;; other sort-.*fields functions but then again this was before, since it ;; didn't use the magnitude of the arg to specify anything. (interactive "P\nsRegexp specifying records to sort: sRegexp specifying key within record: \nr") (cond ((or (equal key-regexp "") (equal key-regexp "\\&")) (setq key-regexp 0)) ((string-match "\\`\\\\[1-9]\\'" key-regexp) (setq key-regexp (- (aref key-regexp 1) ?0)))) (save-excursion (save-restriction (narrow-to-region beg end) (goto-char (point-min)) (let (sort-regexp-record-end (sort-regexp-fields-regexp record-regexp)) (re-search-forward sort-regexp-fields-regexp nil t) (setq sort-regexp-record-end (point)) (goto-char (match-beginning 0)) (sort-subr reverse 'sort-regexp-fields-next-record (function (lambda () (goto-char sort-regexp-record-end))) (function (lambda () (let ((n 0)) (cond ((numberp key-regexp) (setq n key-regexp)) ((re-search-forward key-regexp sort-regexp-record-end t) (setq n 0)) (t (throw 'key nil))) (condition-case () (cons (match-beginning n) (match-end n)) ;; if there was no such register (error (throw 'key nil))))))))))) (defvar sort-columns-subprocess t) ;;;###autoload (defun sort-columns (reverse &optional beg end) "Sort lines in region alphabetically by a certain range of columns. For the purpose of this command, the region BEG...END includes the entire line that point is in and the entire line the mark is in. The column positions of point and mark bound the range of columns to sort on. A prefix argument means sort into REVERSE order. The variable `sort-fold-case' determines whether alphabetic case affects the sort order. Note that `sort-columns' rejects text that contains tabs, because tabs could be split across the specified columns and it doesn't know how to handle that. Also, when possible, it uses the `sort' utility program, which doesn't understand tabs. Use \\[untabify] to convert tabs to spaces before sorting." (interactive "P\nr") (save-excursion (let ;; To make `end-of-line' and etc. to ignore fields. ((inhibit-field-text-motion t) beg1 end1 col-beg1 col-end1 col-start col-end) (goto-char (min beg end)) (setq col-beg1 (current-column)) (beginning-of-line) (setq beg1 (point)) (goto-char (max beg end)) (setq col-end1 (current-column)) (forward-line) (setq end1 (point)) (setq col-start (min col-beg1 col-end1)) (setq col-end (max col-beg1 col-end1)) (if (search-backward "\t" beg1 t) (error "sort-columns does not work with tabs -- use M-x untabify")) (if (not (or (memq system-type '(vax-vms windows-nt)) (let ((pos beg1) plist fontified) (catch 'found (while (< pos end1) (setq plist (text-properties-at pos)) (setq fontified (plist-get plist 'fontified)) (while (consp plist) (unless (or (eq (car plist) 'fontified) (and (eq (car plist) 'face) fontified)) (throw 'found t)) (setq plist (cddr plist))) (setq pos (next-property-change pos nil end1))))))) ;; Use the sort utility if we can; it is 4 times as fast. ;; Do not use it if there are any non-font-lock properties ;; in the region, since the sort utility would lose the ;; properties. Tabs are used as field separator; on NetBSD, ;; sort complains if "\n" is used as field separator. (let ((sort-args (list (if reverse "-rt\t" "-t\t") (format "-k1.%d,1.%d" (1+ col-start) (1+ col-end))))) (when sort-fold-case (push "-f" sort-args)) (apply #'call-process-region beg1 end1 "sort" t t nil sort-args)) ;; On VMS and ms-windows, use Emacs's own facilities. (save-excursion (save-restriction (narrow-to-region beg1 end1) (goto-char beg1) (sort-subr reverse 'forward-line 'end-of-line #'(lambda () (move-to-column col-start) nil) #'(lambda () (move-to-column col-end) nil)))))))) ;;;###autoload (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") (if (> beg end) (let (mid) (setq mid end end beg beg mid))) (save-excursion ;; put beg at the start of a line and end and the end of one -- ;; the largest possible region which fits this criteria (goto-char beg) (or (bolp) (forward-line 1)) (setq beg (point)) (goto-char end) ;; the test for bolp is for those times when end is on an empty line; ;; it is probably not the case that the line should be included in the ;; reversal; it isn't difficult to add it afterward. (or (and (eolp) (not (bolp))) (progn (forward-line -1) (end-of-line))) (setq end (point-marker)) ;; the real work. this thing cranks through memory on large regions. (let (ll (do t)) (while do (goto-char beg) (setq ll (cons (buffer-substring (point) (progn (end-of-line) (point))) ll)) (setq do (/= (point) end)) (delete-region beg (if do (1+ (point)) (point)))) (while (cdr ll) (insert (car ll) "\n") (setq ll (cdr ll))) (insert (car ll))))) (provide 'sort) ;; arch-tag: fbac12be-2a7b-4c8a-9665-264d61f70bd9 ;;; sort.el ends here