* wl-draft.el (wl-draft-previous-history-element): New function.
[elisp/wanderlust.git] / wl / wl-draft.el
index 4047e7c..6df2d19 100644 (file)
@@ -706,8 +706,9 @@ Reply to author if WITH-ARG is non-nil."
 (defun wl-draft-hide (editing-buffer)
   "Hide the editing draft buffer if possible."
   (when (and editing-buffer
-            (buffer-live-p editing-buffer))
-    (set-buffer editing-buffer)
+            (buffer-live-p editing-buffer)
+            (get-buffer-window editing-buffer))
+    (select-window (get-buffer-window editing-buffer))
     (let ((sum-buf wl-draft-buffer-cur-summary-buffer)
          fld-buf sum-win fld-win)
       (if (and wl-draft-use-frame
@@ -2434,6 +2435,80 @@ Automatically applied in draft sending time."
        (set-buffer-modified-p modified))))
   (recenter n))
 
+;; insert element from history
+(defvar wl-draft-current-history-position nil)
+(defvar wl-draft-history-backup-word "")
+
+(defun wl-draft-previous-history-element (n)
+  (interactive "p")
+  (let (bol history)
+    (when (and (not (wl-draft-on-field-p))
+              (< (point)
+                 (save-excursion
+                   (goto-char (point-min))
+                   (search-forward (concat "\n" mail-header-separator "\n") nil 0)
+                   (point)))
+              (save-excursion
+                (beginning-of-line)
+                (while (and (looking-at "^[ \t]")
+                            (not (= (point) (point-min))))
+                  (forward-line -1))
+                (cond
+                 ((looking-at wl-folder-complete-header-regexp)
+                  (and (boundp 'wl-read-folder-hist)
+                       (setq history wl-read-folder-hist)))
+                 ;; ((looking-at wl-address-complete-header-regexp)
+                 ;;  (setq history .....))
+                 (t
+                  nil)))
+              (eolp))
+      (setq bol (save-excursion (beginning-of-line) (point)))
+      (cond ((and (or (eq last-command 'wl-draft-previous-history-element)
+                     (eq last-command 'wl-draft-next-history-element))
+                 wl-draft-current-history-position)
+            (setq end (point))
+            (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
+                (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
+                (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t))
+            (setq prev (match-string 1))
+            (goto-char (match-beginning 1))
+            (setq beg (point))
+            (if (cond ((< n 0)
+                       (>= (+ n wl-draft-current-history-position) 0))
+                      ((> n 0)
+                       (<= (+ n wl-draft-current-history-position)
+                           (length history))))
+                (progn
+                  (setq wl-draft-current-history-position
+                        (+ n wl-draft-current-history-position))
+                  (setq new
+                        (nth wl-draft-current-history-position
+                             (append (list wl-draft-history-backup-word)
+                                     history)))
+                  (delete-region beg end)
+                  (insert new))
+              (goto-char end)
+              (cond ((< n 0)
+                     (message "End of history; no next item"))
+                    ((> n 0)
+                     (message "Beginning of history; no preceding item")))))
+           ((and (> n 0)
+                 (save-excursion
+                   (or (search-backward-regexp ",[ \t]*\\(.*\\)" bol t)
+                       (search-backward-regexp "^[ \t]\\(.*\\)" bol t)
+                       (search-backward-regexp "^[^ \t]*: \\(.*\\)" bol t)))
+                 (car history))
+            (setq wl-draft-current-history-position 1)
+            (setq wl-draft-history-backup-word (match-string 1))
+            (delete-region (match-beginning 1) (match-end 1))
+            (insert (car history)))
+           (t
+            (setq wl-draft-current-history-position nil)))))))
+
+(defun wl-draft-next-history-element (n)
+  (interactive "p")
+  (wl-draft-previous-history-element (- n)))
+
 ;;;; user-agent support by Sen Nagata
 
 ;; this appears to be necessarily global...