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

Re: [BUGFIX] w3m-session-delete [PATCH]



In [emacs-w3m:12853]
On Thu, 14 Dec 2017 08:04:30 -0500, Boruch Baum wrote:
> On 2017-12-14 18:59, Katsumi Yamaoka wrote:
>> Sorry for the noise.

> No need to apologize to anyone who has read the code. This is a case
> where we both fell victim to bad design. The code is written in lisp,
> inherits lisp structures, but persists in performing its functions in
> very un-lisp ways, going off in what I imagine is a very inefficient
> manner searching incrementally in strings for the next occurrence of a
> text property, when there is a very efficient lisp alternative of going
> to the next or nth list element. I found it frustrating and
> disheartening to read the code in this part of the project, especially
> in view of how other parts of the project show a mastery of the power of
> lisp.

Thanks.  A revised patch is below.  The way I introduced to avoid
using the INITIAL-INPUT argument passed to `completing-read' and
`read-from-minibuffer' is obviously devious, though.

(BTW, we Japanese often say すみません (su-mi-ma-sen) that literally
 means `sorry', but it is not necessarily intended to apologize.
 It may be just a decorative word as the case may be. ;-))


* w3m-session.el (w3m-session-save): Update selection buffer.
(w3m-session-select-rename, w3m-session-select-delete):
Make the selected session highlighted after renaming or deletion.
(w3m-session-select): Allow specifying the session number;
close selection window when no session remains after deletion.
(w3m-session-save, w3m-session-rename): Emulate INITIAL-INPUT.

--- w3m-session.el~	2017-12-07 06:36:37.848393500 +0000
+++ w3m-session.el	2017-12-15 01:52:10.930297600 +0000
@@ -216,7 +216,9 @@
      (setq titles (cons (cons title title) titles))
      (catch 'loop
        (while t
-	 (setq title (completing-read prompt titles nil nil title))
+	 ;; A devious way to emulate INITIAL-INPUT that is deprecated.
+	 (let ((minibuffer-setup-hook (lambda nil (insert title))))
+	   (setq title (completing-read prompt titles nil nil nil nil title)))
 	 (if (or (string= title "")
 		 (and (assoc title sessions)
 		      (not (y-or-n-p (format "\"%s\" is exist. Overwrite? "
@@ -247,7 +249,10 @@
        (w3m-save-list w3m-session-file sessions)
        (if (= len 1)
 	   (message "%s: 1 session save...done" title)
-	 (message "%s: %d sessions save...done" title len))))))
+	 (message "%s: %d sessions save...done" title len))
+       (when (and (setq buf (get-buffer " *w3m-session select*"))
+		  (get-buffer-window buf 'visible))
+	 (save-selected-window (w3m-session-select)))))))
 
 (defun w3m-session-automatic-save ()
   "Save list of displayed session automatically."
@@ -631,10 +636,8 @@
   (let ((num (get-text-property
 	      (point) 'w3m-session-number))
 	(sessions w3m-session-select-sessions))
-    (w3m-session-select-quit)
     (w3m-session-rename sessions num)
-    (w3m-session-select)
-    (forward-line num)))
+    (w3m-session-select num)))
 
 (defun w3m-session-select-delete ()
   "Delete the session."
@@ -644,43 +647,52 @@
     (let ((num (get-text-property
 		(point) 'w3m-session-number))
 	  (sessions w3m-session-select-sessions))
-      (w3m-session-select-quit)
       (w3m-session-delete sessions num)
-      (w3m-session-select)
-      (forward-line num))))
+      (w3m-session-select (min num (1- (length sessions)))))))
 
 ;;;###autoload
-(defun w3m-session-select ()
-  "Select session from session list."
+(defun w3m-session-select (&optional n)
+  "Select session from session list.
+Position point at N-th session if N is given."
   (interactive)
   (w3m-session-ignore-errors
-   (let* ((sessions (w3m-load-list w3m-session-file))
-	  (showbuf (w3m-get-buffer-create " *w3m-session select*"))
-	  (wheight (max (+ (length sessions) 5) window-min-height))
-	  (wincfg (current-window-configuration))
-	  window last-window)
-     (setq last-window (previous-window
-			(w3m-static-if (fboundp 'frame-highest-window)
-			    (frame-highest-window)
-			  (frame-first-window))))
-     (while (minibuffer-window-active-p last-window)
-       (setq last-window (previous-window last-window)))
-     (while (and
-	     (not (one-window-p))
-	     (or (< (window-width last-window)
-		    (frame-width))
-		 (< (window-height last-window)
-		    (+ wheight window-min-height))))
-       (setq window last-window)
-       (setq last-window (previous-window window))
-       (delete-window window))
-     (select-window (split-window last-window))
-     (condition-case nil
-	 (shrink-window (- (window-height) wheight))
-       (error nil))
-     (switch-to-buffer showbuf)
-     (setq w3m-session-select-wincfg wincfg)
-     (w3m-session-select-mode sessions))))
+   (let ((sessions (w3m-load-list w3m-session-file))
+	 (showbuf " *w3m-session select*")
+	 window)
+     (if sessions
+	 (let ((wheight (max (+ (length sessions) 5) window-min-height))
+	       (wincfg (current-window-configuration))
+	       last-window)
+	   (setq last-window (previous-window
+			      (w3m-static-if (fboundp 'frame-highest-window)
+				  (frame-highest-window)
+				(frame-first-window))))
+	   (while (minibuffer-window-active-p last-window)
+	     (setq last-window (previous-window last-window)))
+	   (while (and
+		   (not (one-window-p))
+		   (or (< (window-width last-window)
+			  (frame-width))
+		       (< (window-height last-window)
+			  (+ wheight window-min-height))))
+	     (setq window last-window)
+	     (setq last-window (previous-window window))
+	     (delete-window window))
+	   (select-window (split-window last-window))
+	   (condition-case nil
+	       (shrink-window (- (window-height) wheight))
+	     (error nil))
+	   (switch-to-buffer (w3m-get-buffer-create showbuf))
+	   (setq w3m-session-select-wincfg wincfg)
+	   (w3m-session-select-mode sessions)
+	   (when n (w3m-session-select-next n)))
+       (when (setq showbuf (get-buffer showbuf))
+	 (when (setq window (prog1 (get-buffer-window showbuf t)
+			      (kill-buffer showbuf)))
+	   (save-selected-window
+	     (select-window window)
+	     (or (one-window-p t t) (delete-window window)))))
+       (message "No saved session")))))
 
 (defun w3m-session-goto-session (session)
   "Goto URLs."
@@ -723,7 +735,9 @@
 	   (tmp (nth num sessions))
 	   (otitle (car tmp)))
       (while (not title)
-	(setq title (read-from-minibuffer prompt nil nil nil nil otitle))
+	;; A devious way to emulate INITIAL-INPUT that is deprecated.
+	(let ((minibuffer-setup-hook (lambda nil (insert otitle))))
+	  (setq title (read-from-minibuffer prompt nil nil nil nil otitle)))
 	(cond
 	 ((string= title "")
 	  (setq title nil