blob: 4c1c7cab3fbc59d0f846e5803ebc989400ec05b0 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
|
;;; w3m-session-backup.el --- Backup the current W3m session to a file
;; Copyright (c) 2018 Teddy Wing
;; Author: Teddy Wing
;; Version: 0.0.1
;; Package-Requires: ((w3m "1.4.609"))
;; Keywords: tools
;; URL:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Saves a YAML file backup of the current W3m "Crash recovery session".
;;
;; Tabs are saved in the following format:
;;
;; - page_title: 'Title'
;; url: 'http://example.com'
;;; Code:
(defgroup w3m-session-backup nil
"w3m-session-backup customisations.")
;; Configurable save directory, default to current path
(defcustom w3m-session-backup-save-directory "."
"Directory where backup files are saved."
:type 'directory
:group 'w3m-session-backup
:package-version '(w3m-session-backup . "0.0.1"))
(defcustom w3m-session-backup-filename-function 'w3m-session-backup--filename
"Function that generates a filename for the session backup."
:type 'function
:group 'w3m-session-backup
:package-version '(w3m-session-backup . "0.0.1"))
(defun w3m-session-backup--buffers ()
"Crash recovery session list from `~/.w3m/.sessions`."
(nth 2
(first
(w3m-load-list w3m-session-file))))
(defun w3m-session-backup--page-list ()
"List of URL and page title tuples."
(mapcar
(lambda (buffer)
(cons
;; URL
(first buffer)
;; Page title
(last buffer)))
(w3m-session-backup--buffers)))
;; Write to file
;; https://stackoverflow.com/questions/2321904/elisp-how-to-save-data-in-a-file#2322164
;; Format some YAML text to write to the file
;; Configurable dynamic filename based on date-time
(defun w3m-session-backup--yml-escape (str)
"YAML escape single quotes by doubling them."
(replace-regexp-in-string
(regexp-quote "'")
"''"
str
'fixedcase
'literal))
(defun w3m-session-backup--save-backup ()
"Save the current w3m crash recovery session to a new YAML file."
(with-temp-file
(concat
(file-name-as-directory w3m-session-backup-save-directory)
(funcall w3m-session-backup-filename-function))
(insert
(string-join
(mapcar
(lambda (page)
(format "- page_title: '%s'
url: '%s'"
(w3m-session-backup--yml-escape (first (last page)))
(first page)))
(w3m-session-backup--page-list))
"\n"))))
(defun w3m-session-backup--filename ()
"Generates a default filename using the current date & time."
(format "w3m-tabs-%s.yml"
(format-time-string "%Y%m%d-%Hh%Mm%S")))
;; Make filename customisable
;; Make M-x command to write session backup
;;;###autoload
(defun w3m-session-backup ()
"Save the current w3m crash recovery session to a new YAML file."
(interactive)
(w3m-session-backup--save-backup))
(provide 'w3m-session-backup)
;;; w3m-session-backup.el ends here
|