In org-mode, multiple Properties can be set on an entry. Some properties are built-in to org-mode and have special uses for them. Some properties are custom, used in some plug-ins or just for logging information. CUSTOM_ID belongs to the former, while the ID attribute belongs to the latter.

CUSTOM_ID

CUSTOM_ID is used to jump. Org-mode supports a variety of external link formats, one of which is an entry with a specified CUSTOM_ID that links to a specified.org file.

Such as in a. File:/Users/liutos/Dropbox/gtd/roles/writer.org org file: : # d1bdc978 – a8ce – 4266-9 ffa – b6041f818431 section of text, so that when the cursor is placed in the text, Press the shortcut key C – C – C o, Emacs will open the file/Users/liutos/Dropbox/gtd/roles/writer.org, and the cursor on the corresponding items.

ID

The ID is used to associate two entries. A third-party plug-in called org-edna implements the dependency between two items, one of which is the item ID attribute.

For example, I have a task to explain Ada (in the form of an entry) and a task to learn Ada (in the form of another entry). Obviously, you have to learn it before you can tell it to others, so the first item depends on the second item, so I first set an ID attribute for the Ada language item with the value 905fc2F4-4e28-4966-84fa-84c9e6bae96c. Then set the BLOCKER property for the Ada language entry to IDS (905fc2F4-4e28-4966-84fa-84c9e6Bae96c). In this way, when an Ada language entry appears on the *Org Agenda*, Emacs will gray it out, indicating that it is blocked and that its dependencies must be addressed first.

CUSTOM_ID and ID are automatically populated

Creating dependencies and jumps is a common feature, so I’ll set the CUSTOM_ID and ID attributes for each entry. To avoid having to manually set it up each time, I used the capture-Template feature of org-mode to do it automatically.

Capture-template is another tool of org-mode that generates common content between items, such as asterisks at the beginning of lines, keywords, and which levels of files to write to. An example is on the org-mode website

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/org/gtd.org" "Tasks")
         "* TODO %? \n %i\n %a")
        ("j" "Journal" entry (file+datetree "~/org/journal.org")
         "* %? \nEntered on %U\n %i\n %a")))
Copy the code

In addition to using pre-defined placeholders (such as %U, % I, and %a above) in the Capture-template, you can call any Elisp function — which is good for filling in attributes such as ID and CUSTOM_ID that are not repeated and have certain formatting requirements. The value of the ID attribute can be generated using the uuidgen-4 function from the third-party plug-in uuidgen

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/org/gtd.org" "Tasks")
         "* TODO %? :PROPERTIES:\n :CUSTOM_ID: %(uuidgen-4)\n :ID: %(uuidgen-4)\n :END:")))
Copy the code

The catch is that the values for CUSTOM_ID and ID are different because uuidgen-4 returns a different string each time. Is there any way to make them the same? The answer is yes.

In duplicate

Since the results of the two calls to uuidgen-4 are different, the results after the first call can be saved and reused. The idea is simple, and the implementation code is straightforward

(let (lt-org-capture--uuid)
  (defun lt-org-capture-uuidgen ()
    "Generate a UUID and populate it in a lexical scoped variable."
    (setf lt-org-capture--uuid (uuidgen-4))
    lt-org-capture--uuid)
  (defun lt-org-capture-uuidclr ()
    "Return the generated UUID and empty it."
    lt-org-capture--uuid))
Copy the code

The Capture-Template came naturally

(setq org-capture-templates
      '(("t" "Todo" entry (file+headline "~/org/gtd.org" "Tasks")
         "* TODO %? :PROPERTIES:\n :CUSTOM_ID: %(lt-org-capture-uuidgen)\n :ID: %(lt-org-capture-uuidclr)\n :END:")))
Copy the code

Afterword.

In the above function definition, I tried to use the lexical scope feature so that lt-org-capture– uUID can only be read and written by Lt-org-capture-uuidGen and lT-org-capture-uuidclr. Unfortunately, Elisp does not support lexical scope, and lT-org-capture — uUID is actually a global variable — can be viewed as c-H V.

The full text.

Read the original