In [emacs-w3m : No.12595] On Tue, 06 Sep 2016 17:44:30 -0400, Boruch Baum wrote: > Here is a proposal for a one-line change to function `w3m-save-buffer' > to make life easier for those of us who view web pages without images, > or who never want to save images along with html. > The diff is considerably longer than a single line because: Long diff is ok, but please use the ways like the following to generate a diff and send it as a MIME part from the next time. % diff -au file-OLD file-NEW > DIFF % diff -aruN directory-OLD directory-NEW > DIFF Using a unified diff is a convention in at least the Emacs world today, see: <http://lists.gnu.org/archive/html/emacs-diffs/>. Using a MIME part keeps a diff unbreakable by the email encoding, that uses utf-8 or something whereas many emacs-w3m files use iso-2022-7bit. > * the change includes defining a new customizable variable > * documentation > * typo corrections of functions caddr cadar I believe caddr is not a typo: (setq bname (cadr (assoc img imgs)) ext (caddr (assoc img imgs))) Where `imgs' includes: (("url" "file-basename" "file-extention")...) cadar is not a typo either: (cadar w3m-history) See the variable documentation of `w3m-history'. > * change of parameter name `no-image' to `no-images' > * there seems to be a bug in the function call to > `w3m-history-set-current', so that function call was removed in > order to illustrate an error-free test. Why bug? After saving a w3m buffer, `w3m-view-next-page' in the buffer shows the saved one as designed. > diff appended in-line and attached. Replaced it with the unified version (the full text is attached). -(defun w3m-save-buffer (name &optional no-image) - "Save the current page and its image data in NAME.html and NAME-files/. +(defun w3m-save-buffer (name &optional no-images) + "Save the current buffer as `w3m-save-buffer-directory'/NAME.html, +and... +(defcustom w3m-save-buffer-html-only nil + "If nil, `w3m-save-buffer' will save the images of a buffer in The docstring should mention what it is in one line: ,---- (info "(elisp)Documentation Tips") | • Format the documentation string so that it fits in an Emacs window | on an 80-column screen. It is a good idea for most lines to be no | wider than 60 characters. The first line should not be wider than | 67 characters or it will look bad in the output of ‘apropos’. `---- Not a few emacs-w3m objects violate it though. The idea of `w3m-save-buffer-html-only' is acceptable, but the variable name and its docstring don't mention that it is a complement operator: + (no-images (if no-images + (not w3m-save-buffer-html-only) + w3m-save-buffer-html-only)) And `no-images' is not necessary to be `let'-bound because it is already bound as a function argument. BTW, there seems to be another way to achieve your personal purpose in a simple way, e.g.:
Attachment:
bin54qsCksC5R.bin
Description: application/emacs-lisp
Regards,
--- w3m-save.el~ 2016-08-18 23:15:23.920675800 +0000 +++ w3m-save.el 2016-09-07 00:26:10.026789100 +0000 @@ -43,12 +43,25 @@ :group 'w3m :type 'boolean) -(defun w3m-save-buffer (name &optional no-image) - "Save the current page and its image data in NAME.html and NAME-files/. -Those files will be saved in `w3m-save-buffer-directory' by default. -No image will be saved if the prefix argument (the optional NO-IMAGE) -is given. The saved page will be added to the history list, and be -viewable using `w3m-next-page'." +(defcustom w3m-save-buffer-html-only nil + "If nil, `w3m-save-buffer' will save the images of a buffer in +addition to the buffer's html. If the buffer was originally +loaded without images, the images will now be retrieved. The +value of this variable may be over-ridden at run-time by passing +a prefix argument to function `w3m-save-buffer'." + :group 'w3m + :type 'boolean) + +(defun w3m-save-buffer (name &optional no-images) + "Save the current buffer as `w3m-save-buffer-directory'/NAME.html, +and optionally save the buffer's associated image files in +folder `w3m-save-buffer-directory'/NAME-files/. Use of +`w3m-save-buffer-directory' may be over-ridden by including a +folder path in NAME. Variable `w3m-save-buffer-html-only' +determines whether images will be save by default, but that +setting may be over-ridden using the prefix argument (the +optional NO-IMAGES). The saved page will be added to the +history list, and be viewable using `w3m-next-page'." (interactive (if (and w3m-current-url (or (not (string-match "\\`[\C-@- ]*\\'\\|\\`about:\\|\\`file:" @@ -74,6 +87,9 @@ (let ((url w3m-current-url) (w3m-prefer-cache w3m-save-buffer-use-cache) (case-fold-search t) + (no-images (if no-images + (not w3m-save-buffer-html-only) + w3m-save-buffer-html-only)) subdir type st base regexp sdir charset ibuf imgs nd img bads bname ext num bn) (unless (and url @@ -93,7 +109,7 @@ (unless (file-name-directory name) (setq name (expand-file-name name w3m-save-buffer-directory))) (setq subdir (concat (file-name-sans-extension name) "-files")) - (cond ((and (not no-image) (file-exists-p name) (file-exists-p subdir)) + (cond ((and (not no-images) (file-exists-p name) (file-exists-p subdir)) (if (yes-or-no-p (format "#1=%s and #1#-files/ already exist in %s, overwrite? " (file-name-nondirectory name) @@ -106,7 +122,7 @@ (if (yes-or-no-p (format "%s already exists, overwrite? " name)) (delete-file name) (keyboard-quit))) - ((and (not no-image) (file-exists-p subdir)) + ((and (not no-images) (file-exists-p subdir)) (if (yes-or-no-p (format "%s already exists, overwrite? " subdir)) (delete-directory subdir t) (keyboard-quit)))) @@ -139,7 +155,7 @@ (delete-region (match-beginning 1) (match-end 1))))) (goto-char st)) ;; Save images into `subdir'. - (unless no-image + (unless no-images (make-directory subdir t) (setq sdir (file-name-nondirectory (directory-file-name subdir))) (when (and (setq charset (or (w3m-arrived-content-charset url) @@ -160,7 +176,7 @@ ((member img bads) nil) ((assoc img imgs) (setq bname (cadr (assoc img imgs)) - ext (caddr (assoc img imgs))) + ext (car (assoc img imgs))) t) (t (with-current-buffer @@ -206,12 +222,7 @@ (unless imgs (delete-directory subdir)))) (unless (file-exists-p (file-name-directory name)) (make-directory (file-name-directory name) t)) - (write-region (point-min) (point-max) name)) - (w3m-history-set-current - (prog1 - (cadar w3m-history) - (w3m-history-push (concat "file://" name) - (list :title (or w3m-current-title "<no-title>"))))))) + (write-region (point-min) (point-max) name)))) (provide 'w3m-save)