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

Re: macros for w3m-attributes



On 2018-09-03 07:28, Katsumi Yamaoka wrote:
> Hi Tsuchiya-san, could you follow this subject up? (I'm not so
> versed in the asynchronous operation of emacs-w3m, sorry.)

Here's why I'm bothering, what attracted my attention, what I tried to
do, and what result I obtained:

The eventual goal is to debug two longstanding issues I'm having: one
related to displaying some large images (I've discussed this a few times
in prior emails to the list), and the second related to properly
handling links to pdf files when running emacs from the console (no
GUI).

I noticed the series of macros at about line 3200 in w3m.el in a manner
that forces a call to function `w3m-attributes' to be async, but the
first thing that function performs is the same check and action, so most
of the code of the macro *seems* unnecessary.

Further of interest was all the levels of function calling:

   w3m-content-type
     w3m-attributes
       w3m-w3m-attributes
         w3m-w3m-attributes-1

Finally, function `w3m-w3m-attributes-1' is recursive, when it would
seem to sufficient to iterate, and it doesn't seem to be signaling an
error if no header is retrieved (that should always be an error, no?).

In the attached 'good' diff, I seem to have succeeded in performing a
few minor optimization tweaks, but the attached 'bad' diff, in which I
attempted to simplify the macro call, it seems that the sequence of
elements in the handler-queue-list has been affected. In my test, when I
run function `w3m-view-image', the code seems to be performing
`w3m-download' before a temporary filename is assigned, so the function
prompts the user for a file name, and then does nothing (my guess is
that it calls the external viewer with the same nil file name).

At one point I also tried replacing the recursion in
`w3m-w3m-attributes-1' with iteration, which seemed straightforward, but
also led to trouble.

To complicate (or simplify?) matters, I'm using the 'refactor' patch
that I pushed a few weeks ago, which makes it easier to read the
contents of the process-handler-function-queue ...

Any advice, techniques, methodology, pointers in dealing with these
process handler queues would be greatly appreciated, Tsuchiya-san.

-- 
hkp://keys.gnupg.net
CA45 09B5 5351 7C11 A9D1  7286 0036 9E45 1595 8BC0
--- ../w3m.el_before_attributes_work	2018-09-02 10:04:42.171166125 -0400
+++ ../w3m.el_attributes_working	2018-09-02 12:03:58.998563187 -0400
@@ -3195,7 +3195,7 @@
      ((w3m-url-local-p url)
       (w3m-local-attributes url))
      (t
-      (w3m-w3m-attributes url no-cache handler)))))
+      (w3m-w3m-attributes url no-cache (or w3m-follow-redirection 0) handler)))))
 
 (defmacro w3m-content-type (url &optional no-cache handler)
   (if handler
@@ -5506,7 +5506,7 @@
       (concat url "/")
     url))
 
-(defun w3m-w3m-attributes (url no-cache handler)
+(defun w3m-w3m-attributes (url no-cache counter handler)
   "Return a list of attributes corresponding to URL.
 Return nil if it failed in retrieving of the header.
 Otherwise, return a list which includes the following elements:
@@ -5519,16 +5519,8 @@
  5. Real URL.
 
 If the optional argument NO-CACHE is non-nil, cache is not used."
-  (w3m-w3m-attributes-1 (w3m-w3m-canonicalize-url url)
-                        no-cache
-                        (or w3m-follow-redirection 0)
-                        handler))
 
-(defun w3m-w3m-attributes-1 (url no-cache counter handler)
-  "A subroutine for `w3m-w3m-attributes'."
-  (let ((url url)
-                (no-cache no-cache)
-                (counter counter))
+  (let ((url  (w3m-w3m-canonicalize-url url)))
     (w3m-process-do
         (header (or (unless no-cache
                       (w3m-cache-request-header url))
@@ -5541,7 +5533,7 @@
                   ;; Redirect counter exceeds `w3m-follow-redirection'.
                   (list "text/html" "us-ascii" nil nil nil url)
                 ;; Follow redirection.
-                (w3m-w3m-attributes-1 (nth 6 attr) no-cache
+                (w3m-w3m-attributes (nth 6 attr) no-cache
                                       (1- counter) handler))
             (cdr attr)))))))
 
--- ../w3m.el_before_attributes_work	2018-09-02 10:04:42.171166125 -0400
+++ w3m.el	2018-09-02 19:38:01.755671450 -0400
@@ -3195,15 +3195,11 @@
      ((w3m-url-local-p url)
       (w3m-local-attributes url))
      (t
-      (w3m-w3m-attributes url no-cache handler)))))
+      (w3m-w3m-attributes url no-cache (or w3m-follow-redirection 0) handler)))))
+
+(defmacro w3m-content-type (url no-cache handler)
+  `(car (w3m-attributes ,url ,no-cache ,handler)))

-(defmacro w3m-content-type (url &optional no-cache handler)
-  (if handler
-      `(let ((handler ,handler))
-         (w3m-process-do
-             (attrs (w3m-attributes ,url ,no-cache handler))
-           (car attrs)))
-    `(car (w3m-attributes ,url ,no-cache))))
 (defmacro w3m-content-charset (url &optional no-cache handler)
   (if handler
       `(let ((handler ,handler))
@@ -5161,7 +5150,7 @@
          cs)
     (unless (>= level 4)
       (unless content-type
-        (setq content-type (w3m-content-type url)))
+        (setq content-type (w3m-content-type url nil nil)))
       (unless content-charset
         (setq content-charset
               (or (w3m-content-charset url)
@@ -5506,7 +5495,7 @@
       (concat url "/")
     url))

-(defun w3m-w3m-attributes (url no-cache handler)
+(defun w3m-w3m-attributes (url no-cache counter handler)
   "Return a list of attributes corresponding to URL.
 Return nil if it failed in retrieving of the header.
 Otherwise, return a list which includes the following elements:
@@ -5519,16 +5508,8 @@
  5. Real URL.

 If the optional argument NO-CACHE is non-nil, cache is not used."
-  (w3m-w3m-attributes-1 (w3m-w3m-canonicalize-url url)
-                        no-cache
-                        (or w3m-follow-redirection 0)
-                        handler))

-(defun w3m-w3m-attributes-1 (url no-cache counter handler)
-  "A subroutine for `w3m-w3m-attributes'."
-  (let ((url url)
-                (no-cache no-cache)
-                (counter counter))
+  (let ((url  (w3m-w3m-canonicalize-url url)))
     (w3m-process-do
         (header (or (unless no-cache
                       (w3m-cache-request-header url))
@@ -5541,7 +5522,7 @@
                   ;; Redirect counter exceeds `w3m-follow-redirection'.
                   (list "text/html" "us-ascii" nil nil nil url)
                 ;; Follow redirection.
-                (w3m-w3m-attributes-1 (nth 6 attr) no-cache
+                (w3m-w3m-attributes (nth 6 attr) no-cache
                                       (1- counter) handler))
             (cdr attr)))))))

@@ -6704,7 +6685,7 @@
 (defsubst w3m-image-page-displayed-p ()
   (and (fboundp 'image-mode-setup-winprops)
        w3m-current-url
-       (string-match "\\`image/" (w3m-content-type w3m-current-url))
+       (string-match "\\`image/" (w3m-content-type w3m-current-url nil nil))
        (eq (get-text-property (point-min) 'w3m-image-status) 'on)))

 (defun w3m-create-image-page (url type charset page-buffer)
@@ -9897,7 +9878,7 @@
          (or (string-match
               "\\`about://\\(?:header\\|source\\)/"
               w3m-current-url)
-             (equal (w3m-content-type w3m-current-url)
+             (equal (w3m-content-type w3m-current-url nil nil)
                     "text/plain"))
          (setq truncate-lines nil))
     ;; restore position must call after hooks for localcgi.
@@ -10439,11 +10420,12 @@
   (interactive "P")
   (if (null w3m-current-url)
       (w3m-message "Can't execute this page")
+    ; BUG: This would permannently changes the associated, wouldn't it?
     (setf (w3m-arrived-content-type w3m-current-url)
           (let ((type (completing-read
                        (format "Content-type (current %s, default reset): "
                                (or (w3m-arrived-content-type w3m-current-url)
-                                   (w3m-content-type w3m-current-url)))
+                                   (w3m-content-type w3m-current-url nil nil)))
                        w3m-content-type-alist nil t)))
             (unless (string= type "") type)))
     (w3m-redisplay-this-page arg)))
@@ -10739,7 +10721,7 @@
      "\nTitle: " (or (w3m-arrived-title
                               (w3m-url-strip-authinfo url)) "")
      "\nURL: " url
-     "\nDocument,A (BType: " (or (w3m-content-type url) "")
+     "\nDocument,A (BType: " (or (w3m-content-type url no-cache nil) "")
      "\nLast,A (BModified: "
           (let ((time (w3m-last-modified url)))
             (if time (current-time-string time) "")))