emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Arthur Miller <arthur.miller@live.com>
To: Nick Dokos <ndokos@gmail.com>
Cc: emacs-orgmode@gnu.org
Subject: Re: Programmatically set TODO labels per file?
Date: Fri, 30 Apr 2021 16:35:56 +0200	[thread overview]
Message-ID: <AM9PR09MB497783A3C69C6DA0D25DD936965E9@AM9PR09MB4977.eurprd09.prod.outlook.com> (raw)
In-Reply-To: <AM9PR09MB497713FFAE5BBD98D85D4725965E9@AM9PR09MB4977.eurprd09.prod.outlook.com> (Arthur Miller's message of "Fri, 30 Apr 2021 12:13:12 +0200")

[-- Attachment #1: Type: text/plain, Size: 1244 bytes --]

Arthur Miller <arthur.miller@live.com> writes:

If anyone is interested, this is how I understand the org TODO per file
parsing:

The file is parsed in org-collect-keywords-1 in org.el.

Each #+TODO: line is lumped into one single string, which is a problem
when strings with spaces a concerned. Multiple #+TODO: lines will end up
in final multiple strings added into an alist which has a first element
a "TODO" string. The right thing would be to parse multiple strings per
each #+TODO: line. Now the org-collect-keyword-1 is not a trivial one,
so it would take me quite some time to understand, so I'll pass.

The thing I did that worked for me is wrong super-hackish thing, just an
experiment, so to say.

I have simply refactored the code where the string obtained from
org-collect-keywords is parsed, which is the very last part of
org-set-regexps-and-options, where main action seems to take place. That
let's me do what I wanted. It will completely replace whatever was
specified with #+ syntax, and the fontification won't be done
either. Also if file is reverted than #+ will completely replace what
was set with lisp. So not a clean thing, probably nothing to be used by
anyone, but it does what I roughly need for my init file :-).


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: test.org --]
[-- Type: text/x-org, Size: 3888 bytes --]

#+TODO: one two
#+TODO: three
#+TODO: four

#+begin_src emacs-lisp
(defun org-todo-per-file-keywords (kwds)
  "Sets per file TODO labels. Takes as argument a list of strings to be used as
labels."
  (let (alist)
    (push "TODO" alist)
    (dolist (kwd kwds)
      (push kwd alist))
    (setq alist (list (nreverse alist)))
    ;; TODO keywords.
    (setq-local org-todo-kwd-alist nil)
    (setq-local org-todo-key-alist nil)
    (setq-local org-todo-key-trigger nil)
    (setq-local org-todo-keywords-1 nil)
    (setq-local org-done-keywords nil)
    (setq-local org-todo-heads nil)
    (setq-local org-todo-sets nil)
    (setq-local org-todo-log-states nil)
    (let ((todo-sequences alist))
      (dolist (sequence todo-sequences)
	(let* ((sequence (or (run-hook-with-args-until-success
			      'org-todo-setup-filter-hook sequence)
			     sequence))
	       (sequence-type (car sequence))
	       (keywords (cdr sequence))
	       (sep (member "|" keywords))
	       names alist)
	  (dolist (k (remove "|" keywords))
	    (unless (string-match "^\\(.*?\\)\\(?:(\\([^!@/]\\)?.*?)\\)?$"
				  k)
	      (error "Invalid TODO keyword %s" k))
	    (let ((name (match-string 1 k))
		  (key (match-string 2 k))
		  (log (org-extract-log-state-settings k)))
	      (push name names)
	      (push (cons name (and key (string-to-char key))) alist)
	      (when log (push log org-todo-log-states))))
	  (let* ((names (nreverse names))
		 (done (if sep (org-remove-keyword-keys (cdr sep))
			 (last names)))
		 (head (car names))
		 (tail (list sequence-type head (car done) (org-last done))))
	    (add-to-list 'org-todo-heads head 'append)
	    (push names org-todo-sets)
	    (setq org-done-keywords (append org-done-keywords done nil))
	    (setq org-todo-keywords-1 (append org-todo-keywords-1 names nil))
	    (setq org-todo-key-alist
		  (append org-todo-key-alist
			  (and alist
			       (append '((:startgroup))
				       (nreverse alist)
				       '((:endgroup))))))
	    (dolist (k names) (push (cons k tail) org-todo-kwd-alist))))))
    (setq org-todo-sets (nreverse org-todo-sets)
	  org-todo-kwd-alist (nreverse org-todo-kwd-alist)
	  org-todo-key-trigger (delq nil (mapcar #'cdr org-todo-key-alist))
	  org-todo-key-alist (org-assign-fast-keys org-todo-key-alist))
    ;; Compute the regular expressions and other local variables.
    ;; Using `org-outline-regexp-bol' would complicate them much,
    ;; because of the fixed white space at the end of that string.
    (unless org-done-keywords
      (setq org-done-keywords
	    (and org-todo-keywords-1 (last org-todo-keywords-1))))
    (setq org-not-done-keywords
	  (org-delete-all org-done-keywords
			  (copy-sequence org-todo-keywords-1))
	  org-todo-regexp (regexp-opt org-todo-keywords-1 t)
	  org-not-done-regexp (regexp-opt org-not-done-keywords t)
	  org-not-done-heading-regexp
	  (format org-heading-keyword-regexp-format org-not-done-regexp)
	  org-todo-line-regexp
	  (format org-heading-keyword-maybe-regexp-format org-todo-regexp)
	  org-complex-heading-regexp
	  (concat "^\\(\\*+\\)"
		  "\\(?: +" org-todo-regexp "\\)?"
		  "\\(?: +\\(\\[#.\\]\\)\\)?"
		  "\\(?: +\\(.*?\\)\\)??"
		  "\\(?:[ \t]+\\(:[[:alnum:]_@#%:]+:\\)\\)?"
		  "[ \t]*$")
	  org-complex-heading-regexp-format
	  (concat "^\\(\\*+\\)"
		  "\\(?: +" org-todo-regexp "\\)?"
		  "\\(?: +\\(\\[#.\\]\\)\\)?"
		  "\\(?: +"
		  ;; Stats cookies can be stuck to body.
		  "\\(?:\\[[0-9%%/]+\\] *\\)*"
		  "\\(%s\\)"
		  "\\(?: *\\[[0-9%%/]+\\]\\)*"
		  "\\)"
		  "\\(?:[ \t]+\\(:[[:alnum:]_@#%%:]+:\\)\\)?"
		  "[ \t]*$")
	  org-todo-line-tags-regexp
	  (concat "^\\(\\*+\\)"
		  "\\(?: +" org-todo-regexp "\\)?"
		  "\\(?: +\\(.*?\\)\\)??"
		  "\\(?:[ \t]+\\(:[[:alnum:]:_@#%]+:\\)\\)?"
		  "[ \t]*$"))
    (org-compute-latex-and-related-regexp)))

(org-todo-per-file-keywords '("label 1" "label 2" "something" "last one"))
#+end_src

* Test

      reply	other threads:[~2021-04-30 14:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-29 20:49 Programmatically set TODO labels per file? Arthur Miller
2021-04-29 21:09 ` tomas
2021-04-29 21:58   ` Arthur Miller
2021-04-29 21:20 ` Russell Adams
2021-04-29 22:01   ` Arthur Miller
2021-04-29 22:37     ` Samuel Wales
2021-04-29 22:48       ` Arthur Miller
2021-04-30  1:08 ` Nick Dokos
2021-04-30 10:13   ` Arthur Miller
2021-04-30 14:35     ` Arthur Miller [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AM9PR09MB497783A3C69C6DA0D25DD936965E9@AM9PR09MB4977.eurprd09.prod.outlook.com \
    --to=arthur.miller@live.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=ndokos@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).