From 17a35e2762a670c3475612c3a36ee1d6a8bf0e93 Mon Sep 17 00:00:00 2001 From: "Dodge W. Coates" Date: Fri, 24 Feb 2017 15:22:33 -0500 Subject: Add default puzzle file functionality to chess-puzzle --- chess-puzzle.el | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/chess-puzzle.el b/chess-puzzle.el index 8b4da35..d581185 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) @@ -59,7 +78,25 @@ "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 + " (" + (abbreviate-file-name 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)) -- cgit v1.2.3 From 588a36381106f8162fec8bba0993644a7f219968 Mon Sep 17 00:00:00 2001 From: "Dodge W. Coates" Date: Sat, 25 Feb 2017 15:03:19 -0500 Subject: Add chess-puzzle-set-default-file --- chess-puzzle.el | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/chess-puzzle.el b/chess-puzzle.el index d581185..1274746 100644 --- a/chess-puzzle.el +++ b/chess-puzzle.el @@ -73,6 +73,26 @@ it. Useful if you have all of your puzzles in a single file." '((bad-game-read . "Error reading game at position %d") (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. @@ -89,7 +109,7 @@ making it easy to go on to the next puzzle once you've solved one." (if file-p (concat " (" - (abbreviate-file-name file-name) + (file-name-nondirectory file-name) ")") "")) (file-name-directory file-name) -- cgit v1.2.3