Skip to content

Commit 8e021ce

Browse files
authored
Merge pull request #186 from dwcoates/RW/forgiving-pgn-at-pos-variant
Forgiving variant of pygn-mode-pgn-at-pos defun
2 parents d43e8cb + cb30858 commit 8e021ce

1 file changed

Lines changed: 19 additions & 9 deletions

File tree

pygn-mode.el

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1174,9 +1174,19 @@ garbage."
11741174
(forward-line 1))))
11751175
(when-let ((move-node (pygn-mode--true-containing-node '(san_move lan_move))))
11761176
(goto-char (pygn-mode--true-node-after-position move-node)))
1177-
(buffer-substring-no-properties
1178-
(pygn-mode-game-start-position)
1179-
(point))))
1177+
;; todo returning nil might not be the best behavior when pos trails a game
1178+
(when-let ((start-pos (pygn-mode-game-start-position)))
1179+
(buffer-substring-no-properties
1180+
start-pos
1181+
(point)))))
1182+
1183+
(defun pygn-mode--pgn-at-pos-or-stub (pos)
1184+
"Return a single-game PGN string inclusive of any move at POS.
1185+
1186+
Identical to `pygn-mode-pgn-at-pos' except that a stub value is returned
1187+
when POS is not inside a game."
1188+
(or (pygn-mode-pgn-at-pos pos)
1189+
"[Event \"?\"]\n\n*\n"))
11801190

11811191
;; TODO this code assumes that a variation begins with the next move,
11821192
;; which is not always the case. Detect when the variation leads with
@@ -1750,7 +1760,7 @@ When called non-interactively, display the FEN corresponding to POS.
17501760
With `prefix-arg' DO-COPY, copy the FEN to the kill ring, and to the system
17511761
clipboard when running a GUI Emacs."
17521762
(interactive "d\nP")
1753-
(let ((fen (pygn-mode-pgn-to-fen (pygn-mode-pgn-at-pos pos))))
1763+
(let ((fen (pygn-mode-pgn-to-fen (pygn-mode--pgn-at-pos-or-stub pos))))
17541764
(when do-copy
17551765
(kill-new fen)
17561766
(when (and (fboundp 'gui-set-selection)
@@ -1769,7 +1779,7 @@ clipboard when running a GUI Emacs."
17691779
17701780
When called non-interactively, display the FEN corresponding to POS."
17711781
(interactive "d")
1772-
(let* ((fen (pygn-mode-pgn-to-fen (pygn-mode-pgn-at-pos pos)))
1782+
(let* ((fen (pygn-mode-pgn-to-fen (pygn-mode--pgn-at-pos-or-stub pos)))
17731783
(buf (get-buffer-create pygn-mode-fen-buffer-name))
17741784
(win (get-buffer-window buf)))
17751785
(with-current-buffer buf
@@ -1800,7 +1810,7 @@ When called non-interactively, display the FEN corresponding to POS."
18001810
"Save the board image corresponding to POS to a file."
18011811
(let* ((pygn-mode-board-size (completing-read "Pixels per side: " nil nil nil nil nil pygn-mode-board-size))
18021812
(filename (read-file-name "SVG filename: "))
1803-
(svg-data (pygn-mode-pgn-to-board (pygn-mode-pgn-at-pos pos) 'svg)))
1813+
(svg-data (pygn-mode-pgn-to-board (pygn-mode--pgn-at-pos-or-stub pos) 'svg)))
18041814
(with-temp-buffer
18051815
(insert svg-data)
18061816
(write-file filename))))
@@ -1810,7 +1820,7 @@ When called non-interactively, display the FEN corresponding to POS."
18101820
18111821
When called non-interactively, display the board corresponding to POS."
18121822
(interactive "d")
1813-
(let* ((svg-data (pygn-mode-pgn-to-board (pygn-mode-pgn-at-pos pos) 'svg))
1823+
(let* ((svg-data (pygn-mode-pgn-to-board (pygn-mode--pgn-at-pos-or-stub pos) 'svg))
18141824
(buf (pygn-mode--get-or-create-board-buffer))
18151825
(win (get-buffer-window buf)))
18161826
(with-current-buffer buf
@@ -1829,7 +1839,7 @@ When called non-interactively, display the board corresponding to POS."
18291839
18301840
When called non-interactively, display the board corresponding to POS."
18311841
(interactive "d")
1832-
(let* ((text-data (pygn-mode-pgn-to-board (pygn-mode-pgn-at-pos pos) 'text))
1842+
(let* ((text-data (pygn-mode-pgn-to-board (pygn-mode--pgn-at-pos-or-stub pos) 'text))
18331843
(buf (pygn-mode--get-or-create-board-buffer))
18341844
(win (get-buffer-window buf)))
18351845
(with-current-buffer buf
@@ -1902,7 +1912,7 @@ When called non-interactively, display the board corresponding to POS."
19021912
19031913
When called non-interactively, display the line corresponding to POS."
19041914
(interactive "d")
1905-
(let* ((line (pygn-mode-pgn-to-line (pygn-mode-pgn-at-pos pos)))
1915+
(let* ((line (pygn-mode-pgn-to-line (pygn-mode--pgn-at-pos-or-stub pos)))
19061916
(buf (get-buffer-create pygn-mode-line-buffer-name))
19071917
(win (get-buffer-window buf)))
19081918
(with-current-buffer buf

0 commit comments

Comments
 (0)