emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] Idea: insert "current sort order" property


From: Christian Moe
Subject: Re: [O] Idea: insert "current sort order" property
Date: Wed, 12 Oct 2011 21:51:16 +0200
User-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:5.0) Gecko/20110624 Thunderbird/5.0

On 10/12/11 5:59 PM, Gez wrote:

I do use revisioning, but Is there a protocol I should use for testing
code snippets before using them on my original data or can they be
considered safe after being run once?

Save often; backup/commit often; be ready to hit 'undo'...? Do the first test run on mock data in a test.org file?

One thing that might be useful to add to the code please ...   If when
prompting for the property key, TAB brought up the list of
auto-complete keys (like when using C-c C-x p) that would make for
easier maintenance and consistency; e.g. when I deliberately want to
overwrite a previous outline index it would avoid a second set of
properties being written accidentally if I made a typo.

Then we need a way to keep track of what property keys are used to store outlines in, so we can offer those and only those for completion. (We don't want to offer all the property keys used in the buffer -- that's an invitation to overwrite data.)

So in my second pass (below), when you use a :foo: property to store the outline index, `foo' gets added to a list of keys in the :Stored_outlines: property of the parent. When you store an outline, the prompt for a property key offers completion on all the keys stored in :Stored_outlines:. See how this works for you.

Yours,
Christian



#+BEGIN_SRC emacs-lisp
  (defun cm/org-store-outline-order (arg prop)
    "Store the heading order 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.

  You can store different heading orders in the same subtree by
  using different keys as PROP; these will be listed in the
  Stored_Outlines property of the parent entry. The tree can be
  restored to a given stored outline by sorting on the appropriate
  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\ni")
        (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"))
               (defaultprop "OutlineIndex")
               (keychain "Stored_Outlines")
               (keys (org-entry-get-multivalued-property
                       (point) keychain)))
          (unless prop
            (setq prop
                  (org-icompleting-read
                   (concat "Property to use [" defaultprop "]: ")
                   (mapcar 'list keys) nil nil nil nil defaultprop)))
          (when (or (not (org-map-entries t (concat prop "={.}") 'tree))
                    (y-or-n-p "Property exists; overwrite? "))
            (org-entry-add-to-multivalued-property
             (point) "Stored_Outlines" prop)
            (org-map-entries
             '(progn
                (org-set-property prop
                                  (format fstr counter))
                (setq counter (1+ counter)))
             match 'tree)
            (message ""))))
#+END_SRC




Gez


On 10/12/11 1:35 PM, Gez wrote:

Thanks, Christian.  Please forgive my ignorance but what should I do
with the code?  I've not done anything more advanced than
org-customize before.

Gez






reply via email to

[Prev in Thread] Current Thread [Next in Thread]