Malabarba
Malabarba

Reputation: 4553

Enforce table sorting

Is it possible to enforce table sorting for a given table or file in Emacs org-mode?

When I say "enforce" I mean the table should get sorted automatically whenever it's realigned (such as when I hit <TAB> or C-c C-c). I want it to be sorted alphabetically according to the first row.

Is there some property I can set to achieve this?

Upvotes: 4

Views: 712

Answers (1)

slitvinov
slitvinov

Reputation: 5768

One option is to "advice" functions you call by keys.
http://www.gnu.org/savannah-checkouts/gnu/emacs/manual/html_node/elisp/Defining-Advice.html

;; advise a functions called by <TAB>
(defadvice org-table-next-field (around auto-sort-advice)
  "Sort table alphabetically according to the first rows"
  (progn
    ad-do-it
    (let ((pos (point)))
      ;; to to the first column
      (goto-char (org-table-begin))
      ;; one can change SORTING-TYPE (?a ?A ?n ?N ?t ?T)
      (org-table-sort-lines nil ?a)
      ;; restore position
      (goto-char pos))))

(ad-activate 'org-table-next-field)

Upvotes: 4

Related Questions