Function: todo-delete-file

todo-delete-file is an interactive and byte-compiled function defined in todo-mode.el.gz.

Signature

(todo-delete-file)

Documentation

Delete the current todo, archive or filtered items file.

If the todo file has a corresponding archive file, or vice versa, prompt whether to delete that as well. Also kill the buffers visiting the deleted files.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/calendar/todo-mode.el.gz
(defun todo-delete-file ()
  "Delete the current todo, archive or filtered items file.
If the todo file has a corresponding archive file, or vice versa,
prompt whether to delete that as well.  Also kill the buffers
visiting the deleted files."
  (interactive)
  (let* ((file1 (buffer-file-name))
	 (todo (eq major-mode 'todo-mode))
	 (archive (eq major-mode 'todo-archive-mode))
	 (filtered (eq major-mode 'todo-filtered-items-mode))
	 (file1-sn (todo-short-file-name file1))
	 (file2 (concat todo-directory file1-sn (cond (todo ".toda")
						      (archive ".todo"))))
	 (buf1 (current-buffer))
	 (buf2 (when file2 (find-buffer-visiting file2)))
	 (prompt1 (concat "Delete " (cond (todo "todo")
					  (archive "archive")
					  (filtered "filtered items"))
	                  " file \"%s\"? "))
	 (prompt2 (concat "Also delete the corresponding "
			  (cond (todo "archive") (archive "todo")) " file "
			  (when buf2 "and kill the buffer visiting it? ")))
	 (delete1 (yes-or-no-p (format prompt1 file1-sn)))
	 (delete2 (when (and delete1 (or (file-exists-p file2) buf2))
		    (yes-or-no-p prompt2))))
    (when delete1
      (when (file-exists-p file1) (delete-file file1))
      (setq todo-visited (delete file1 todo-visited))
      (kill-buffer buf1)
      (if delete2
	  (progn
	    (when (file-exists-p file2) (delete-file file2))
	    (setq todo-visited (delete file2 todo-visited))
	    (and buf2 (kill-buffer buf2)))
	;; If we deleted an archive but not its todo file, update the
	;; latter's category sexp.
	(when (equal (file-name-extension file2) "todo")
	  (with-current-buffer (or buf2 (find-file-noselect file2))
	    (save-excursion
	      (save-restriction
		(widen)
		(goto-char (point-min))
		(let ((sexp (read (buffer-substring-no-properties
				   (line-beginning-position)
				   (line-end-position))))
		      (inhibit-read-only t)
		      (print-length nil)
		      (print-level nil))
		  (mapc (lambda (x) (aset (cdr x) 3 0)) sexp)
		  (delete-region (line-beginning-position) (line-end-position))
		  (prin1 sexp (current-buffer)))))
	    (todo-set-categories)
	    (unless buf2 (kill-buffer)))))
      (setq todo-files (funcall todo-files-function)
	    todo-archives (funcall todo-files-function t))
      (when (or (string=  file1-sn todo-default-todo-file)
		(and delete2 (string= file1-sn todo-default-todo-file)))
	(setq todo-default-todo-file (todo-short-file-name (car todo-files))))
      (when (or (string= file1 todo-global-current-todo-file)
		(and delete2 (string= file2 todo-global-current-todo-file)))
	(setq todo-global-current-todo-file nil))
      (todo-update-filelist-defcustoms)
      (message (concat (cond (todo "Todo") (archive "Archive")) " file \"%s\" "
		       (when delete2
			 (concat "and its "
				 (cond (todo "archive") (archive "todo"))
				 " file "))
		       "deleted")
	       file1-sn))))