emacs-wiki-discuss
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[emacs-wiki-discuss] planner-cyclic leaves diary file buffer with select


From: Sergey Vlasov
Subject: [emacs-wiki-discuss] planner-cyclic leaves diary file buffer with selective-display
Date: Tue, 23 Aug 2005 22:14:45 +0400

Hello!

When using planner-cyclic, I have encountered an annoying thing: after
planner-cyclic-create-tasks-maybe reads planner-cyclic-diary-file, it
leaves a buffer for that file, and that buffer is left in a weird
selective-display mode.  Then later, when I try to edit that diary
file, Emacs happily gives me that buffer, which is unusable for
editing (most of text is hidden, and it is very easy to delete some of
that hidden text accidentally; also, several times I ended up with a
mix of ^M and proper newline characters in the file, even though the
selective-display documentation says that those characters will be
converted when saving).  So I need to kill the broken buffer and open
the file again.

The actual problem is that the Emacs diary support code is ancient and
therefore uses old and inconvenient features like selective-display.
However, planner-cyclic could at least try to handle this situation
better.

I have tried to fix planner-cyclic-get-cyclic-tasks so that it would
clean up after list-diary-entries and restore the diary buffer to a
usable state.  In the process, I decided to make a separate function
for this cleanup - planner-cyclic-list-diary-entries.  This turned out
to be useful later, when I discovered that planner-appt has a copy of
the code from planner-cyclic and therefore has the same problem with
the diary buffers.  So I used that function in planner-appt too (that
part of planner-appt is useless without planner-cyclic anyway).

Probably planner-cyclic-list-diary-entries should really be moved to
planner-diary, but there is much more code in there, and I don't use
it (at least for now).  However, seems that planner-diary-get-entries
has the same problem (it is somewhat mitigated by the fact that
planner-diary-get-entries does not change diary-display-hook, and I
have fancy-diary-display there, which does similar cleanup).
planner-diary-get-diary-entries seems to be OK (again because of
fancy-diary-display).

What do you think about this patch?

--- planner/planner-cyclic.el.diary-fix 2005-08-22 09:41:06 +0400
+++ planner/planner-cyclic.el   2005-08-23 21:25:32 +0400
@@ -76,23 +76,44 @@ tasks to the new format manually."
   :group 'planner)
 
 ;;; functions
-(defun planner-cyclic-get-cyclic-tasks (date &optional no-of-days)
-  "For DATE, get the cyclic tasks."
+
+(defun planner-cyclic-list-diary-entries (file date &optional number)
+  "\
+Get list of diary entries in FILE for NUMBER days starting with DATE.
+The list has the same form as returned by `list-diary-entries', but
+this function tries to undo the changes which `list-diary-entries'
+does to the diary buffer."
+  ;; The code to restore the buffer is copied from `include-other-diary-files'
   (save-window-excursion
     (save-excursion
-      (unwind-protect
-          (delq nil
-                (let* ((diary-display-hook nil)
-                       (diary-file planner-cyclic-diary-file)
-                       (list-diary-entries-hook '(include-other-diary-files))
-                       (entries (list-diary-entries
-                                 (if (stringp date)
-                                     (planner-filename-to-calendar-date date)
-                                   date) 1)))
-                  (mapcar (lambda (item)
-                            (when (string-match "#[A-C].+" (elt item 1))
-                              (match-string 0 (elt item 1))))
-                          entries)))))))
+      (let* ((diary-file file)
+            (list-diary-entries-hook '(include-other-diary-files))
+            (diary-display-hook 'ignore)
+            (diary-hook nil)
+            (d-buffer (find-buffer-visiting diary-file))
+            (diary-modified (when d-buffer
+                              (set-buffer d-buffer)
+                              (buffer-modified-p))))
+       (unwind-protect
+           (list-diary-entries date (or number 1))
+         (let ((d-buffer (find-buffer-visiting diary-file)))
+           (when d-buffer
+             (set-buffer d-buffer)
+             (subst-char-in-region (point-min) (point-max) ?\^M ?\n t)
+             (setq selective-display nil)
+             (set-buffer-modified-p diary-modified))))))))
+
+(defun planner-cyclic-get-cyclic-tasks (date &optional no-of-days)
+  "For DATE, get the cyclic tasks."
+  (let ((date (if (stringp date)
+                 (planner-filename-to-calendar-date date)
+               date)))
+    (delq nil
+         (mapcar (lambda (item)
+                   (when (string-match "#[A-C].+" (elt item 1))
+                     (match-string 0 (elt item 1))))
+                 (planner-cyclic-list-diary-entries planner-cyclic-diary-file
+                                                    date 1)))))
 
 (defun planner-cyclic-generate-task (date task-string)
   "For DATE, generate a cyclic task based on TASK-STRING."
--- planner/planner-appt.el.diary-fix   2005-08-21 22:51:50 +0400
+++ planner/planner-appt.el     2005-08-23 21:20:34 +0400
@@ -1260,22 +1260,17 @@ handling is in advice to `planner-create
   "For DATE, get the cyclic tasks.
 Optional argument get tasks for NO-OF-DAYS from DATE, the default is 1
 day [i.e., only for DATE]."
-  (save-window-excursion
-    (save-excursion
-      (delq nil
-           (let* ((diary-display-hook nil)
-                  (diary-file planner-cyclic-diary-file)
-                  (list-diary-entries-hook '(include-other-diary-files))
-                  (entries (list-diary-entries
-                            (if (stringp date)
-                                (planner-filename-to-calendar-date date)
-                              date) (or no-of-days 1))))
-             (mapcar #'(lambda (item)
-                         (when (string-match
-                                planner-appt-schedule-regexp
-                                (elt item 1))
-                           (match-string 0 (elt item 1))))
-                     entries))))))
+  (require 'planner-cyclic)
+  (let ((date (if (stringp date)
+                 (planner-filename-to-calendar-date date)
+               date)))
+    (delq nil
+         (mapcar #'(lambda (item)
+                     (when (string-match planner-appt-schedule-regexp
+                                         (elt item 1))
+                       (match-string 0 (elt item 1))))
+                 (planner-cyclic-list-diary-entries planner-cyclic-diary-file
+                                                    date no-of-days)))))
 
 (defun planner-appt-schedule-add-cyclic ()
   "Add cylical tasks to the schedule if the current buffer is a day page."

-- 
Sergey Vlasov

Attachment: pgpWpmDH34wu8.pgp
Description: PGP signature


reply via email to

[Prev in Thread] Current Thread [Next in Thread]