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

Re: w3m-filter-delete-regions feature enhancements (patch included)



In [emacs-w3m : No.12666]
On Tue, 30 May 2017 08:47:41 -0400, Boruch Baum wrote:
[...]
>> In addition, could you show some filter examples by which the
>> revised `w3m-filter-delete-regions' is used conveniently?

> Sure, I have a bunch handy already, two of which are for sites popular
> enough to be considered for the package. The following code contains:
[...]

Thanks.

In order to avoid the overhead that interpreting the optional
arguments causes, I've committed the Lisp macro version of your
`w3m-filter-delete-regions' and `w3m-filter-replace-regex'.
The macro expansion examples are below.

The best way to use those macros is to use them in a Lisp function
and to byte-compile the function in order to avoid the overhead
caused by the macro expansion at the run-time.  So, I've replaced
every filter item that uses `w3m-filter-delete-regions', listed
in `w3m-filter-configuration' with a Lisp function in addtion to
having installed your new filters.

Regards,

;; The most simple case (the expanded form is almost equal to
;; the old `w3m-filter-delete-regions'):
(macroexpand '(w3m-filter-delete-regions URL "START" "END"))
↓
(let (p)
  (goto-char (point-min))
  (while (and (search-forward "START" nil t)
	      (setq p (match-beginning 0))
	      (search-forward "END" nil t))
    (delete-region p (match-end 0))))

;; The most complex case (you can see the expanded form doesn't
;; look so slow):
(macroexpand '(w3m-filter-delete-regions URL "START" "END" t t t 123 456 99))
↓
(let (p (i 0))
  (goto-char 123)
  (while (and (< i 99)
	      (re-search-forward "START" 456 t)
	      (setq p (match-end 0))
	      (re-search-forward "END" 456 t))
    (delete-region p (match-beginning 0))
    (setq i (1+ i))))

;; For the moment, I removed the last `(> i 0)', since I don't
;; know how to silence Emacs 26's byte-compiler, that issues
;; a warning: Warning: value returned from (> i 0) is unused


;; The most simple case:
(macroexpand '(w3m-filter-replace-regexp URL "REGEXP" "TO-STRING"))
↓
(progn
  (goto-char (point-min))
  (while (re-search-forward "REGEXP" nil t)
    (replace-match "TO-STRING" nil nil)))

;; The most complex case:
(macroexpand '(w3m-filter-replace-regexp URL "REGEXP" "TO-STRING" 123 456 99))
↓
(let ((i 0))
  (goto-char 123)
  (while (and (< i 99)
	      (re-search-forward "REGEXP" 456 t))
    (replace-match "TO-STRING" nil nil)
    (setq i (1+ i)))
  (> i 0))

;; I've replaced `incf' with `1+' for a case the macro is used
;; like a Lisp function.  In that case, `incf' required cl that
;; should not be loaded at the run-time.