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