Add guard clauses for file existence and initialize variables
This commit is contained in:
51
nob.el
51
nob.el
@@ -5,7 +5,7 @@
|
||||
;; Author: Robert Rose <overclucker@gmail.com>
|
||||
;; Keywords: lisp eww novel bookmark
|
||||
;; Version: 0.0.1
|
||||
;; Package-Requires ((dash "2.15.0) (emacs "24") (ivy "20190214.1032")
|
||||
;; Package-Requires ((dash "2.15.0") (emacs "24") (ivy "20190214.1032"))
|
||||
|
||||
;; 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
|
||||
@@ -25,9 +25,9 @@
|
||||
;; This package provides functions for saving and selecting novels for
|
||||
;; for reading in eww. I wrote nob because bookmarking a chapter in eww
|
||||
;; and then always having to remove old bookmarks is ridiculous. nov-save
|
||||
;; works by queryinh the bookmark list a pattern that matches the current
|
||||
;; works by querying the bookmark list a pattern that matches the current
|
||||
;; url. If it finds a match, it updates the title and url and moves it to
|
||||
;; the front of the list. This makes shoosing what to to read next, and
|
||||
;; the front of the list. This makes choosing what to read next, and
|
||||
;; picking up where you left off much easier. The nob-select function
|
||||
;; provides an easy to navigate pop up list.
|
||||
|
||||
@@ -44,6 +44,7 @@
|
||||
|
||||
(defcustom nob-bookmarks-file (concat user-emacs-directory "nob-bookmarks.el")
|
||||
"Location of the file nob uses for bookmark storage."
|
||||
:type 'file
|
||||
:group 'nob)
|
||||
|
||||
(defcustom nob-autosave-on-render t
|
||||
@@ -54,9 +55,11 @@
|
||||
(defmacro with-nob (&rest body)
|
||||
"Execute body with variable: `novels' set."
|
||||
`(let ((novels
|
||||
(with-temp-buffer
|
||||
(insert-file-contents nob-bookmarks-file)
|
||||
(read (buffer-string)))))
|
||||
(if (file-exists-p nob-bookmarks-file)
|
||||
(with-temp-buffer
|
||||
(insert-file-contents nob-bookmarks-file)
|
||||
(read (buffer-string)))
|
||||
'())))
|
||||
(unwind-protect ,@body)))
|
||||
|
||||
(defmacro with-eww-buffer (&rest body)
|
||||
@@ -68,7 +71,7 @@
|
||||
;;; I will need to explore options for replacing `--map-first'
|
||||
;;; Maybe it's as simple as popping and pushing. That would be nice.
|
||||
(defun nob-update-bookmark (novels title url)
|
||||
"Update an existing bookmark. Returns updated novels list.'
|
||||
"Update an existing bookmark. Returns updated novels list.
|
||||
This function has no side affects."
|
||||
(--map-first (string-match (car it) url) `(,(car it) ,title ,url)
|
||||
(--sort (string-match (car it) url) novels)))
|
||||
@@ -84,17 +87,19 @@ This function has no side affects."
|
||||
(cons pattern (cons title url))
|
||||
As I've started hacking away at nob mode, I've come
|
||||
to realize that I can't simply rewrite the functions.
|
||||
I'll need some scafolding in place so that nob remains
|
||||
I'll need some scaffolding in place so that nob remains
|
||||
functional while I continue working on it."
|
||||
(interactive))
|
||||
(with-temp-file (concat user-emacs-directory "nob-bookmarks")
|
||||
(with-nob
|
||||
(let*
|
||||
((n (mapcar
|
||||
(lambda (x)
|
||||
(cons (car x) (cons (cadr x) (caddr x))))
|
||||
novels)))
|
||||
(insert (pp n)) )))
|
||||
(interactive)
|
||||
(let ((old-file (concat user-emacs-directory "nob-bookmarks")))
|
||||
(when (file-exists-p old-file)
|
||||
(with-temp-buffer
|
||||
(insert-file-contents old-file)
|
||||
(let ((novels (read (buffer-string)))
|
||||
(n (mapcar
|
||||
(lambda (x)
|
||||
(cons (car x) (cons (cadr x) (caddr x))))
|
||||
novels)))
|
||||
(insert (pp n))))))
|
||||
|
||||
(defun nob-save ()
|
||||
"Saves current novel."
|
||||
@@ -112,12 +117,12 @@ functional while I continue working on it."
|
||||
(progn
|
||||
(nob-write-file
|
||||
(push `(,(read-string "Match: " url) ,title ,url) novels))
|
||||
(message "Bookmarked: %s" title))) )) )) )
|
||||
(message "Bookmarked: %s" title))) )) ))
|
||||
|
||||
(defun nob-select ()
|
||||
"Select novel for reading in `eww'."
|
||||
(interactive)
|
||||
;;
|
||||
;;
|
||||
;; version two
|
||||
;; `assoc' is a wonderful thing.
|
||||
;; originally required `--map' and `--first' from `dash.el'
|
||||
@@ -125,7 +130,7 @@ functional while I continue working on it."
|
||||
(with-nob ; novels: ((pattern title url))
|
||||
(let*
|
||||
((n (--map (cons (cadr it) (caddr it)) novels))
|
||||
;; the mapcar version is bigger.
|
||||
;; mapcar version is bigger.
|
||||
;; (n (mapcar
|
||||
;; (lambda (x)
|
||||
;; (cons (cadr x) (caddr x)))
|
||||
@@ -140,14 +145,14 @@ functional while I continue working on it."
|
||||
(eww-browse-url s))))
|
||||
|
||||
(defun nob-remove ()
|
||||
"Remove an novel."
|
||||
"Remove a novel."
|
||||
(interactive)
|
||||
(with-nob
|
||||
(let ((s (ivy-read "Remove Novel: " (--map (cdr it) novels))))
|
||||
(when (y-or-n-p (format "Remove \"%s\"?" s))
|
||||
(nob-write-file
|
||||
(--remove (string-equal s (car (cdr it))) novels))
|
||||
(message "Removed: %s" s)) )) )
|
||||
(message "Removed: %s" s)) ))
|
||||
|
||||
(defun nob-eww-after-render-hook ()
|
||||
"Eww render hook for nob autosave"
|
||||
@@ -158,7 +163,7 @@ functional while I continue working on it."
|
||||
(let ((n (--first (string-match (car it) url) novels)))
|
||||
(if n
|
||||
(if (not (string-equal (nth 2 n) url))
|
||||
(nob-write-file (nob-update-bookmark novels title url))))) )))) )
|
||||
(nob-write-file (nob-update-bookmark novels title url))))) ))))
|
||||
|
||||
(setq nob-mode-map (make-sparse-keymap))
|
||||
(define-key nob-mode-map [?b] 'nob-save)
|
||||
|
||||
Reference in New Issue
Block a user