summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Wiegley <jwiegley@gmail.com>2017-02-25 16:07:18 -0500
committerGitHub <noreply@github.com>2017-02-25 16:07:18 -0500
commite21d9a389d8d73329ce3786b5ed8ab0c3f9e8f63 (patch)
tree68429039544270364448551ff2e020d36ffa94a4
parent8257a9d47fa617851b977c0303794a520e51e1f3 (diff)
parent588a36381106f8162fec8bba0993644a7f219968 (diff)
Merge pull request #14 from dwcoates/master
Add default directory for puzzles
-rw-r--r--chess-puzzle.el59
1 files changed, 58 insertions, 1 deletions
diff --git a/chess-puzzle.el b/chess-puzzle.el
index 8b4da35..1274746 100644
--- a/chess-puzzle.el
+++ b/chess-puzzle.el
@@ -44,6 +44,25 @@
:type 'boolean
:group 'chess-puzzle)
+(defcustom chess-puzzle-default-file nil
+ "Default file in which to search for chess puzzles.
+
+If non-nil, `chess-puzzle' will interpret the value as either a
+puzzle file to load or a directory in which to look for puzzle
+file to load. When nil, `chess-puzzle' will read files from
+current directory."
+ :type 'file
+ :group 'chess-puzzle)
+
+(defcustom chess-puzzle-autoload-file nil
+ "Avoid prompting for puzzle file if `chess-puzzle-default-file' is a pgn file.
+
+If non-nil, don't use `chess-puzzle-default-file' as the default
+in the read file prompt for `chess-puzzle', and instead simply load
+it. Useful if you have all of your puzzles in a single file."
+ :type 'boolean
+ :group 'chess-puzzle)
+
(defvar chess-puzzle-indices nil)
(defvar chess-puzzle-position nil)
@@ -55,11 +74,49 @@
(end-of-puzzles . "There are no more puzzles in this collection")))
;;;###autoload
+(defun chess-puzzle-set-defualt-file (file)
+ "Set the default puzzle file to FILE for the current session."
+ (interactive
+ (list (let* ((file-name (or chess-puzzle-default-file
+ (file-name-directory (buffer-file-name))))
+ (file-p (not (file-directory-p file-name)))
+ (def-file (read-file-name
+ "Set puzzle file to: "
+ (file-name-directory file-name)
+ (when file-p file-name) t)))
+ (if (file-directory-p def-file)
+ (file-name-as-directory def-file)
+ def-file))))
+ (setq chess-puzzle-default-file file)
+ (when (yes-or-no-p "Load a chess puzzle?: ")
+ (let ((chess-puzzle-autoload-file t))
+ (unless (call-interactively 'chess-puzzle))))
+ (message "chess-puzzle-default-file set to '%s'" file))
+
+;;;###autoload
(defun chess-puzzle (file &optional index) ;FIXME: index not used!
"Pick a random puzzle from FILE, and solve it against the default engine.
The spacebar in the display buffer is bound to `chess-puzzle-next',
making it easy to go on to the next puzzle once you've solved one."
- (interactive "fRead chess puzzles from: ")
+ (interactive
+ (list (let* ((file-name (or chess-puzzle-default-file
+ (file-name-directory (buffer-file-name))))
+ (file-p (not (file-directory-p file-name)))
+ (auto-load (and file-p chess-puzzle-autoload-file)))
+ (if (not auto-load)
+ (read-file-name
+ (format "Read chess puzzles from%s: "
+ (if file-p
+ (concat
+ " ("
+ (file-name-nondirectory file-name)
+ ")")
+ ""))
+ (file-name-directory file-name)
+ (when file-p file-name) t)
+ file-name))))
+
+ (abbreviate-file-name (buffer-file-name))
(let* ((database (chess-database-open file))
(objects (and database (chess-session)))
(engine (car objects))