Playing around with planner-search-notes

(defun planner-search-notes (regexp)
  "Return a buffer with all the notes returned by the query for REGEXP."
  (interactive "MRegexp: ")
  (with-emacs-wiki-project planner-project
    (let ((results (planner-search-notes-internal regexp)))
      (if results
             (get-buffer-create (generate-new-buffer-name
                                 (concat "Planner Search: " regexp))))
            (setq emacs-wiki-current-project planner-project)
            (mapcar (lambda (item)
                      (insert (car item) "\t"
                              (cadr item) "\n"))
            (goto-char point-min)
            (display-buffer (current-buffer)))
        (message "No results found.")))))

(defun planner-search-notes-internal (regexp)
  "Return an alist of all notes in daily plan pages containing REGEXP.
The alist is of the form ((REFERENCE . TEXT) (REFERENCE . TEXT))."
  (let ((pages (sort (copy-sequence (emacs-wiki-file-alist))
                     (lambda (a b)
                       (string< (car a) (car b)))))         page start anchor text results)     (while pages       (setq page (caar pages))       (when (string-match planner-date-regexp page)         (with-temp-buffer           (insert-file-contents-literally (cdar pages))           (setq start nil)           ;; Find the first note           (when (re-search-forward "\\.\\(#[0-9]+\\)\s+\\(.*\\)" nil t)             (setq start (match-beginning 2))             (setq anchor (match-string 1))             (setq title (match-string 2)))           (while (re-search-forward "\\.\\(#[0-9]+\\)\s+\\(.*\\)" nil t)             ;; The text between start and (1- (match-beginning 0))             ;; is the note body.             (setq text (buffer-substring start (1- (match-beginning 0))))             (save-match-data               (when (string-match regexp text)                 (add-to-list 'results (list (concat page anchor)                                             title))))             (setq start (match-beginning 2))             (setq anchor (match-string 1))             (setq title (match-string 2)))           (when start             (setq text (buffer-substring start (point-max)))             (when (string-match regexp text)               (add-to-list 'results (list (concat page anchor)                                           title))))))       (setq pages (cdr pages)))     results))