From mboxrd@z Thu Jan 1 00:00:00 1970 From: Christian Moe Subject: Re: Idea: insert "current sort order" property Date: Tue, 11 Oct 2011 21:46:27 +0200 Message-ID: <4E949D13.6060505@christianmoe.com> References: Reply-To: mail@christianmoe.com Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Return-path: Received: from eggs.gnu.org ([140.186.70.92]:54557) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RDiFL-0001bF-Q4 for emacs-orgmode@gnu.org; Tue, 11 Oct 2011 15:44:17 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RDiFK-0000LE-ID for emacs-orgmode@gnu.org; Tue, 11 Oct 2011 15:44:15 -0400 Received: from mars.hitrost.net ([91.185.211.18]:50357) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RDiFK-0000HO-6H for emacs-orgmode@gnu.org; Tue, 11 Oct 2011 15:44:14 -0400 In-Reply-To: List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: suleika@gmail.com Cc: emacs-orgmode@gnu.org Hi, Gez, On 10/7/11 5:02 PM, Gez wrote: > What I'm imagining is a command executed on a headline to insert a > property into each of its children "fixing" the current order; > something like ":sorted:01", ":sorted:02" etc. I think this is a neat idea, and can see some uses for it for my own stuff. I've made a first pass below. Please test it and let me know how it works for you (and please *don't* test it on anything valuable without backing up first!). > Ideally there would > be a prompt for the property key (or part of it) so that there could > be more than one such property for a particular headline, but with the > option to simplify the usage and consistency by having just one > property key as a default and no prompt. More or less done. There'll be a prompt anyway, but just hit RET to select the default ("OutlineIndex"). > If a sorted headline got moved to a new parent its sort-order might be > a duplicate one of its siblings, but I don't really see that as a > problem - the user knows it's "only text" after all. The below will just do sequential numbering, as you suggested (01, 02, 03...) Then, as long as you've only changed the order of siblings at each level, the order can be restored by sorting on the outline-index property (C-c ^ r) for each parent. Irretrievable chaos may and probably will result if headlines are demoted, promoted, or moved to new parents. As long as the user's fine with that, everything's fine. An obvious refinement would be to do true outline numbering (e.g. 01, 01-01, 01-01-01, 01-01-02, 01-02-01, 01-02-02 ...). Then you could restore the original outline entirely no matter how headings have been moved around. But you couldn't do that by sorting; making that useful would take additional code, so I haven't implemented it (yet). > I don't know > whether it might be desired to similarly fix the sort-order of 1st > level headlines; perhaps the command could act per current file, with > a < to narrow down to the current tree. The below will not number the entry at point, because that could break a numbering already set at that level. It will number its direct children. With prefix, it will number all its descendants. See how it works for you. I made it the default behavior to number only the direct children of the entry at point, since littering the whole tree with property drawers is not necessarily what you want to do. > I'm also not sure how to deal > with larger numbers - 01-99 seems like a good default but could one > have an option for 001-999? I also wonder about adding the property > to more than one level at a time, but might that be a bit much in > terms of affecting performance? Solved; the below counts the total number of headings first, then adjusts the zero padding accordingly. Code follows. Yours, Christian #+begin_src emacs-lisp (defun cm/org-store-outline-order (arg prop) "Store the outline of the subtree of the entry at point by setting the property PROP of each direct child entry to its current position in the tree. With prefix ARG, store the position of the whole subtree. The tree can be restored to the stored outline by sorting on the property with `C-c ^ r'. Note that this will only work properly on the order of each subtree; if headings are demoted, promoted, or moved into different subtrees, the result may or may not be nonsense, but it will be impossible to restore the original order by sorting." (interactive "P\nsProperty key (default OutlineIndex): ") (if (string= prop "") (setq prop "OutlineIndex")) (if (or (not (org-map-entries t (concat prop "={.}") 'tree)) (y-or-n-p "Property exists; overwrite? ")) (let* ((match (format "LEVEL%s%s" (if arg ">=" "=") (1+ (org-current-level)))) (counter 1) (width (1+ (floor (log10 (length (org-map-entries t match 'tree)))))) (fstr (concat "%0" (number-to-string width) "d"))) (org-map-entries '(progn (org-set-property prop (format fstr counter)) (setq counter (1+ counter))) match 'tree) (message "")))) #+end_src