[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[elpa] master b9e93b5 6/7: * mines.el (mines-mode-map): Add mouse bindin
From: |
Stefan Monnier |
Subject: |
[elpa] master b9e93b5 6/7: * mines.el (mines-mode-map): Add mouse bindings |
Date: |
Wed, 27 Mar 2019 00:34:16 -0400 (EDT) |
branch: master
commit b9e93b5207d5ccc711a13c08cf3b098231f5a2d8
Author: Stefan Monnier <address@hidden>
Commit: Stefan Monnier <address@hidden>
* mines.el (mines-mode-map): Add mouse bindings
(mines--insert): Add mouse-face to the cells.
(mines-flag-cell): Make it work when bound to a mouse event.
(mines-dig): Make it work when bound to a mouse event.
Straighten out the control flow.
---
packages/mines/mines.el | 70 ++++++++++++++++++++++++-------------------------
1 file changed, 34 insertions(+), 36 deletions(-)
diff --git a/packages/mines/mines.el b/packages/mines/mines.el
index fce9f64..7ca2c65 100644
--- a/packages/mines/mines.el
+++ b/packages/mines/mines.el
@@ -327,6 +327,7 @@ Each cell can be either:
(pos (point))
(inhibit-read-only t))
(insert (format " %c " char))
+ (add-text-properties pos (point) `(mouse-face ,(list 'highlight)))
(when (= (cadr (mines-index-2-matrix idx)) (1- mines-number-cols))
(backward-delete-char 1)
(insert "\n"))
@@ -465,10 +466,11 @@ After sorting, games completed with shorter times appear
first."
(message (format "Well done %s, you have completed it in %s!"
user-login-name elapsed-time))))
-(defun mines-flag-cell ()
+(defun mines-flag-cell (&optional event)
"Flag current cell as having a mine.
If called again then unflag it."
- (interactive)
+ (interactive (list last-nonmenu-event))
+ (if event (posn-set-point (event-end event)))
(let* ((idx (mines-current-pos))
(state (aref mines-state idx)))
(if (null state)
@@ -518,45 +520,39 @@ If called again then unflag it."
;; Update the numbers on neighbour cells.
(mines-set-numbers))))
-(defun mines-dig ()
+(defun mines-dig (&optional event)
"Reveal the content of the cell at point."
- (interactive)
+ (interactive (list last-nonmenu-event))
+ (if event (posn-set-point (event-end event)))
(if mines-game-over
(user-error "Current game is over. Try `%s' to start a new one"
(substitute-command-keys "\\[mines]"))
(mines-goto (mines-current-pos)) ; Set point in the center of the cell.
- (cl-labels ((uncover-fn
- ()
- (let* ((idx (mines-current-pos))
- (inhibit-read-only t)
- (state (aref mines-state idx))
- (done (null state)))
- (cond (done (message "Nothing new here")) ; Already updated.
- (t
- (let ((elt (aref mines-grid idx)))
- (cl-flet ((game-end-fn
- ()
- ;; Check for end of game.
- (cond ((eq elt 'bomb)
- ;; We lost the game; show all
the mines.
- (mines-game-over))
- (t
- (when (mines-end-p)
- (mines-game-completed))))))
- ;; Don't end the game in the first trial when
- ;; `mines-protect-first-move' is non-nil.
- (when (and mines-protect-first-move
- (mines-first-move-p)
- elt)
- (mines--clear-first-move idx)
- (setq elt nil))
- (cond ((and (eq 'flag state)
- ;; If the cell is flagged ask for
confirmation.
- (not (yes-or-no-p "This cell is
flagged as having a bomb. Uncover it? ")))
- (message "OK, canceled"))
- (t
- (mines--update-cell idx nil)
- (game-end-fn))))))))))
+ (cl-flet ((uncover-fn
+ ()
+ (let* ((idx (mines-current-pos))
+ (inhibit-read-only t)
+ (state (aref mines-state idx)))
+ (cond ((null state)
+ (message "Nothing new here")) ; Already updated.
+ ((and (eq 'flag state)
+ ;; If the cell is flagged ask for confirmation.
+ ;; FIXME: I personally find this prompt annoying.
+ (not (yes-or-no-p "This cell is flagged as having
a bomb. Uncover it? ")))
+ (message "OK, canceled"))
+ (t
+ (let ((elt (aref mines-grid idx)))
+ ;; Don't end the game in the first trial when
+ ;; `mines-protect-first-move' is non-nil.
+ (when (and mines-protect-first-move
+ (mines-first-move-p)
+ elt)
+ (mines--clear-first-move idx)
+ (setq elt nil))
+ (mines--update-cell idx nil)
+ ;; Check for end of game.
+ (cond ((eq elt 'bomb) (mines-game-over))
+ ((mines-end-p) (mines-game-completed)))))))))
(uncover-fn)
(when mines-undone-neighbours
(while mines-undone-neighbours
@@ -638,6 +634,8 @@ Called with a prefix prompt for the difficulty level."
(define-key map "x" 'mines-dig)
;; FIXME: I think SPC would be a natural binding for `mines-dig'.
(define-key map "c" 'mines-dig)
+ (define-key map [mouse-1] 'mines-dig)
+ (define-key map [mouse-3] 'mines-flag-cell)
;; (define-key map "a" 'mines-flag-cell)
(define-key map "1" 'mines-flag-cell)
(define-key map "m" 'mines-flag-cell)
- [elpa] master updated (2915039 -> 5095d58), Stefan Monnier, 2019/03/27
- [elpa] master 5095d58 7/7: * packages/mines/mines.el (mines-auto-flag): New custom var, Stefan Monnier, 2019/03/27
- [elpa] master 25974d9 4/7: * packages/mines/mines.el: Streamline mines--insert, Stefan Monnier, 2019/03/27
- [elpa] master b9e93b5 6/7: * mines.el (mines-mode-map): Add mouse bindings,
Stefan Monnier <=
- [elpa] master 5456869 5/7: * mines.el: Make sure the first move is successful, Stefan Monnier, 2019/03/27
- [elpa] master 3c7f8ca 3/7: * packages/mines/mines.el: Keep flag in mines-state, Stefan Monnier, 2019/03/27
- [elpa] master 717e6b2 1/7: * mines/mines.el: Various minor changes, wave 1, Stefan Monnier, 2019/03/27
- [elpa] master eb0a053 2/7: * mines/mines.el (mines-grid): Use `bomb` instead of t, Stefan Monnier, 2019/03/27