reddit / emacs

Org mode autocomplete custom tag

wiyomay249 · 2022-02-24 19:29
I want to register what books I have read, and I would like to have a template with the following: * Name of the book :Read: [2022-02-24 Thu] :Author: :Categories: I want the author to be contain suggestions from authors I already have in my document. How can I add this functionality? And is it possible to also get some form of auto-complete for categories, but with the categories being a list?
Illiamen · 2022-02-24 21:41
I don't know about an Org-specific solution, but more generally the command `hippie-expand` can optionally complete existing lines in the buffer. That might be enough for completing authors. See https://www.gnu.org/software/emacs/manual/html_node/autotype/Hippie-Expand.html.
Drfiresign · 2022-02-25 02:57
Take a look at the properties chapter in the org manual. https://orgmode.org/manual/Property-Syntax.html You can define a set of properties for the whole file, define a list of allowed values for a property, set all child entries in a subtree to inherit from the parent (or not). It's pretty versatile. But more specifically, the behavior you just described wanting: > I want the author to be contain suggestions from authors I already have in my document. > And is it possible to also get some form of auto-complete for categories, but with the categories being a list? Is exactly the behavior I have in my org buffers. Once I've added a property in a file, I see it appear in the completion candidates when using `(org-set-property)` (bound to `C-c C-x p` by default). After selecting a property key, I see candidates for the key taken from values I've previously given to that particular key in other entries in the file. I looked through my init files and I don't appear to have customized anything that turns this on by default. You can also define [a capture template](https://orgmode.org/manual/Capture-templates.html) and create specific prompts for each property. I'd really recommend reading the whole chapter front to back. I used to skim through sections looking just from one thing or another, but there's really a lot to cover that you might not even think about using. Have fun!
akirakom · 2022-02-25 12:20
I would set up `org-todo-keywords` to log state changes. You can create a reading list in org-mode and change the state of a book you have read. I was also working on a package for querying book database: https://github.com/akirak/org-volume
xenodium · 2022-02-25 16:23
I'm not an expert in any of these APIs, but I get by... For a structure like: * First book :PROPERTIES: :Author: wiyomay249 :END: Here's a quick and dirty function to extract all authors in current buffer, offer basic completion, and insert selection: (defun ar/org-insert-author () (interactive) (require 'org-element) (let ((authors '())) (save-mark-and-excursion (org-element-map (org-element-parse-buffer) 'node-property (lambda (prop) (when (string-equal (org-element-property :key prop) "Author") (add-to-list 'authors (org-element-property :value prop))) nil))) (insert (completing-read "author: " authors)))) If you want to get fancier with something like company completion, you can peek at [company-org-block](https://github.com/xenodium/company-org-block) source (disclosure, I wrote that).