Function: todo-category-completions

todo-category-completions is a byte-compiled function defined in todo-mode.el.gz.

Signature

(todo-category-completions &optional ARCHIVE)

Documentation

Return a list of completions for todo-read-category.

Each element of the list is a cons of a category name and the file or list of files (as short file names) it is in. The files are either the current (or if there is none, the default) todo file plus the files listed in todo-category-completions-files, or, with non-nil ARCHIVE, the current archive file.

Before calculating the completions, update the value of todo-category-completions-files in case any files named in it have been removed.

Source Code

;; Defined in /usr/src/emacs/lisp/calendar/todo-mode.el.gz
(defun todo-category-completions (&optional archive)
  "Return a list of completions for `todo-read-category'.
Each element of the list is a cons of a category name and the
file or list of files (as short file names) it is in.  The files
are either the current (or if there is none, the default) todo
file plus the files listed in `todo-category-completions-files',
or, with non-nil ARCHIVE, the current archive file.

Before calculating the completions, update the value of
`todo-category-completions-files' in case any files named in it
have been removed."
  (let (deleted)
    (dolist (f todo-category-completions-files)
      (unless (file-exists-p (todo-absolute-file-name f))
	(setq todo-category-completions-files
	      (delete f todo-category-completions-files))
	(push f deleted)))
    (when deleted
      (let ((ndeleted (length deleted))
	    (names (mapconcat (lambda (f) (concat "\"" f "\"")) deleted ", ")))
	(message (concat
                  (ngettext "File %s has been deleted and removed from\n"
                            "Files %s have been deleted and removed from\n"
                            ndeleted)
		  "the list of category completion files")
		 names))
      (put 'todo-category-completions-files 'custom-type
           `(set ,@(todo--files-type-list)))
      (custom-set-default 'todo-category-completions-files
			  (symbol-value 'todo-category-completions-files))
      (sleep-for 1.5)))
  (let* ((curfile (or todo-current-todo-file
		      (and todo-show-current-file
			   todo-global-current-todo-file)
		      (todo-absolute-file-name todo-default-todo-file)))
	 (files (or (unless archive
		      (mapcar #'todo-absolute-file-name
			      todo-category-completions-files))
		    (list curfile)))
	 listall listf)
    ;; If file was just added, it has no category completions.
    (unless (zerop (buffer-size (find-buffer-visiting curfile)))
      (unless (member curfile todo-archives)
	(cl-pushnew curfile files :test #'equal))
      (dolist (f files listall)
	(with-current-buffer (find-file-noselect f 'nowarn)
	  (if archive
	      (unless (derived-mode-p 'todo-archive-mode) (todo-archive-mode))
	    (unless (derived-mode-p 'todo-mode) (todo-mode)))
	  ;; Ensure category is properly displayed in case user
	  ;; switches to file via a non-Todo mode command.  And if
	  ;; done items in category are visible, keep them visible.
	  (let ((done todo-show-with-done))
	    (when (> (buffer-size) (- (point-max) (point-min)))
	      (save-excursion
		(goto-char (point-min))
		(setq done (re-search-forward todo-done-string-start nil t))))
	    (let ((todo-show-with-done done))
	      (save-excursion (todo-category-select))))
	  (save-excursion
	    (save-restriction
	      (widen)
	      (goto-char (point-min))
	      (setq listf (read (buffer-substring-no-properties
				 (line-beginning-position)
				 (line-end-position)))))))
	(mapc (lambda (elt) (let* ((cat (car elt))
				   (la-elt (assoc cat listall)))
			      (if la-elt
				  (setcdr la-elt (append (list (cdr la-elt))
							 (list f)))
				(push (cons cat f) listall))))
	      listf)))))