-
Notifications
You must be signed in to change notification settings - Fork 4
/
crossword.el
3071 lines (2729 loc) · 120 KB
/
crossword.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; crossword.el --- Download and play crossword puzzles -*- lexical-binding: t; coding: utf-8; -*-
;; Copyright (C) 2018-2021 Boruch Baum <boruch-baum@gmx.com>
;; Author/Maintainer: Boruch Baum <boruch-baum@gmx.com>
;; Homepage: https://github.com/Boruch-Baum/emacs-crossword
;; License: GPL3+
;; Keywords: games
;; Package: crossword
;; Version: 1.0
;; Package-Requires: ((emacs "26.1"))
;; This file is NOT part of GNU Emacs.
;; This is free software: you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This software is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this software. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; * Download and play crossword puzzles in Emacs.
;;
;; * Includes a browser to view puzzles' detailed metadata, including
;; progress of partially played puzzles.
;; * Optionally, play against the clock, with the built-in timer.
;;
;;; Dependencies: (all already part of core Emacs)
;;
;; seq - for `seq--into-vector'
;; tabulated-list - for `tabulated-list-mode', etal.
;; hl-line - for `hl-line-mode'
;; calendar - for `calendar-read' etal.
;;
;;; Dependencies: (external to Emacs)
;;
;; The package uses either `wget' or `curl' to download packages from
;; the network. These are both long-established standard programs and
;; at least one is probably already installed on your computer.
;;
;;; Installation:
;;
;; 1) Evaluate or load or install this file.
;;
;; 2) Optionally, define a global keybinding or defalias for functions
;; `crossword'
;;
;; (global-set-key (kbd "foo") 'crossword))
;; (defalias 'crossword 'foo)
;;
;; Depending on your temperament and style, you might also want to
;; set direct keybindings and aliases for functions
;; `crossword-download', `crossword-display', and `crossword-load';
;; however, they're just one menu-selection away from function
;; `crossword'.
;;
;;; Configuration:
;;
;; M-x `customize-group' <RET> crossword <RET>
;;
;; Initially, you may want to change the default download path
;; `crossword-save-path', but otherwise, try using the mode first
;; without any customization.
;; There exist four customizations for how POINT advances after you
;; fill-in a square or navigate: `crossword-arrow-changes-direction'
;; `crossword-wrap-on-entry-or-nav', `crossword-tab-to-next-unfilled'
;; `crossword-auto-nav-only-within-clue'.
;; If you don't usually play more than one crossword in a sitting,
;; you may want to set `crossword-quit-to-browser' to NIL to save
;; yourself a keystroke on exit.
;; You can also customize the download sources to be used for network
;; downloading (do share, please).
;;
;; There are also several 'faces' defined to allow custom colorization
;; and fontification. Knock yourself out.
;;
;;; Operation:
;;
;; M-x `crossword' presents a menu with three options. Unless you
;; already have local puzzle files, you'll want to connect to the
;; network, and decide what puzzle to download and for what date.
;; Select a download source from the download menu, noting that the
;; label for each download source includes the days of the week on
;; which puzzles are published. Then enter the date of the puzzle you
;; want. Different download sources have different archive retention
;; policies, so you can try downloading 'old' puzzles. The puzzle files
;; are tiny, so the download should be instantaneous for most, but
;; YMMV.
;;
;; M-x `crossword' a second time. If you choose to directly load a
;; puzzle, you will be prompted to navigate to it. If you choose the
;; browser, you're in for a small treat, as you'll be presented with a
;; handy-dandy nifty screenshot-suitable metadata browser of all your
;; known puzzle files, courtesy of Emacs' built-in
;; `tabulated-list-mode'. Entries can be sorted by any column by
;; navigating POINT there and pressing 'S' once or twice. Press <RET>
;; to play the puzzle at point. You can also delete files here ('d').
;;
;; IMPORTANT: The mode supports a single save file for each puzzle.
;; These save files do not over-write the original file, so the
;; partially- or completely- played puzzles appear in the browser
;; alongside the untouched original. Save files can be identified in
;; the browser and on youor compuer disk by their file-name extension
;; `puz-emacs'. You can at any time start a puzzle over from scratch
;; by selecting the untouched copy. BUT... If you don't want to lose
;; your partially played session, you will need to back-up the save
;; file.
;;
;; Play occurs in a single window of single dedicated frame, although
;; that frame is divided into three dedicated windows, each displaying
;; its own dedicated buffer. You should never need to leave the playable
;; 'Crossword grid' buffer.
;;
;; At any time, you can save your current state-of-play or restore your
;; saved stated. Quitting a game will prompt you to save or discard it:
;;
;; M-q crossword-quit
;; C-x C-s crossword-backup
;; C-c C-x C-f crossword-restore
;;
;; If, while playing, you delete the crossword frame or one of its
;; windows, you can use command M-x `crossword-recover-game-in-progress'.
;; If you kill a clue buffer, you'll need to save, quit, and restore
;; the saved game.
;;
;; General navigation within the grid buffer should be intuitive, using
;; all the usual keys. Additionally, filling in a square will advance
;; POINT to the next sensible one. Grids begin with an 'across'
;; navigation for solving across clues, but that's easily changed:
;;
;; M-a crossword-nav-dir-across
;; M-d crossword-nav-dir-down
;; M-<SPC> crossword-nav-dir-toggle
;;
;; Notice that when you change direction, the font of the current clue
;; changes accordingly, within the grid, and for the clues' text below
;; the grid and in their dedicated buffers.
;;
;; Additionally, you can always navigate directly to any specific clue
;; by its number, using the '/' key:
;;
;; / crossword-goto-clue
;;
;; You can enter pretty much any character I can think of into any
;; square, but any non-alphabetic characters of the puzzle's language
;; will be ignored (ie. you can make signs for yourself). All lower case
;; alphabetic characters are immediately converted to uppercase.
;;
;; At some point, you'll want to check your work. You have three options
;; for this:
;;
;; M-c l crossword-check-letter
;; M-c w crossword-check-word
;; M-c p crossword-check-puzzle
;;
;; All correctly and incorrectly solved squares will be fontified
;; accordingly. Correctly solved square can no longer be edited. Errors
;; for each square are logged and you are reminded of them at the bottom
;; of the grid buffer when you visit the square in the future.
;;
;; You can also ask the program to give you a break and solve parts of
;; the puzzle for you:
;;
;; M-s l crossword-solve-letter
;; M-s w crossword-solve-word
;; M-s p crossword-solve-puzzle
;;
;; You can browse the list of clues in the other two buffers by setting
;; your current navigation direction (ie. across or down) and using an
;; intuitive keybinding with a 'Meta' prefix. I don't expect that you'll
;; need to use these features much because as you navigate within the
;; grid, the program auto-magically re-centers the other buffers around
;; the whatever are the current clues. Anyway, here are the command
;; names ad default key-bindings:
;;
;; M-up> crossword-clue-scroll-up
;; M-down> crossword-clue-scroll-down
;; M-<next> crossword-clue-scroll-page-up
;; M-<prior> crossword-clue-scroll-page-down
;; M-<home> crossword-clue-scroll-page-home
;; M-<end> crossword-clue-scroll-page-end
;;
;; If you want to play against the clock, toggle the timer on (and off):
;;
;; M-p crossword-pause-unpause-time
;; M-t crossword-pause-unpause-timer
;;
;; That about does it, doesn't it? Here are some of the 'intuitive'
;; navigation keybindings given special attentionL
;;
;; <left> crossword-prior-char
;; <right> crossword-next-char
;; <RET> crossword-next-char
;; <delete> crossword-del-char
;; SPC crossword-del-char
;; <baskspace> crossword-bsp-char
;; C-a crossword-begin-field
;; C-e crossword-end-field
;; <TAB> crossword-next-field
;; <backtab> crossword-prior-field
;;
;;; Feedback:
;;
;; It's best to contact me by opening an 'issue' on the program's github
;; repository (see above) or, distant second-best, by direct e-mail.
;;
;; Code contributions are welcome and github starring is appreciated.
;;
;; Please share your knowledge of other download resources, free
;; software resources, and puzzle formats.
;;
;;; Compatibility:
;;
;; The software has been tested on emacs version 26.1 and and emacs 28
;; snapshot, both in debian on a linux terminal (non-GUI).
;; Several puzzle file formats exist. This software, as currently
;; written, parses the `.puz' file format created sometime in the 1990's
;; by Literate Software LLC[1] and used initially by them for their
;; `across' line of software for creating, solving and sharing crossword
;; puzzles. This format has supposedly since become the de facto
;; standard for the genre[2].
;;
;; [1] http://www.litsoft.com/
;; [2] http://fileformats.archiveteam.org/wiki/PUZ_%28crossword_puzzles%29
;; [3] https://code.google.com/archive/p/puz/wikis/FileFormat.wiki
;; The contents at this URL were mangled, so I reformatted it to
;; properly display its tables and code blocks in an `org-mode`
;; file. See `docs/crossword_puz_format.org`.
;;
;;; Download resources:
;; This project wouldn't be useful without people sharing puzzles for
;; free network download. A big thanks are due to `Martin_Herbach' for
;; being the provider of all the default download resources
;; pre-configured in this sotware.
;;
;; Please let me know of other resources that can be added.
;;
;; Check these URLs for links to other puzzles. In many cases, you
;; need to look for a link labeled something like 'across-lite format'
;; or 'puz format':
;;
;; * https://crosswordlinks.substack.com/?no_cover=true
;; * aggregator, pointing to pages which have download links
;; * RSS: https://crosswordlinks.substack.com/feed
;;
;; * https://crosswordfiend.com/download/
;; * The download links appear only with javascript enabled
;;
;;; Comparable software:
;; I'm not aware of any other similar software for the linux terminal.
;; The following may be of interest to readers. Please let me know of
;; other projects.
;;
;; * xword - Linux GUI package
;; https://sourceforge.net/projects/wx-xword/
;;
;; * shortyz - Android GUI package
;; https://f-droid.org/en/packages/com.totsp.crossword.shortyz/
;;
;;; Code:
;;
;;; Dependencies:
(require 'seq) ;; for seq--into-vector
(require 'tabulated-list) ;; for tabulated list mode
(require 'hl-line) ;; for hl-line-mode
(require 'calendar) ;; for calendar-read and related functions
;;
;;; Customization variables
(defgroup crossword nil
"Settings for the Emacs crossword puzzle game/downloader."
:group 'games
:prefix "crossword-")
(defcustom crossword-save-path "~/Crosswords/"
"Directory where crossword data is to be stored."
:type 'directory)
(defcustom crossword-quit-to-browser t
"Whether quitting a puzzle opens a puzzle browser.
Set this NIL to totally exit 'crossword' immediately."
:type 'boolean)
(defcustom crossword-auto-check-completed t
"Whether to automatically check a puzzle when completely filled.
Set this NIL to be able to self-check first and avoid registering
any errors."
:type 'boolean)
(defcustom crossword-empty-position-char "▦"
"Character denoting non-insertion squares.
The single character to use to denote a positon on the board
that is not for data entry, ie. not part of the solutions for
clues. If NIL or empty, a period (full-stop) is used, since
that is what is used in the .puz standard format. Ideas tried:
⊞ 00229E SQUARED PLUS
🞖 01F796 SQUARE TARGET
⬚ 002B1A DOTTED SQUARE
⁜ 00205C DOTTED CROSS
🗵01F5F5 BALLOT BOX WITH SCRIPT X
🗷 01F5F7 BALLOT BOX WITH BOLD SCRIPT X
╳ 002573 BOX DRAWINGS LIGHT DIAGONAL CROSS
▦ 0025A6 SQUARE WITH ORTHOGONAL CROSSHATCH FILL"
:type 'string)
(defcustom crossword-wrap-on-entry-or-nav t
"How to advance at ends of puzzle.
When non-NIL, automatically return to top-left when at final
lower-right position and either entering data or navigating
forward. This value will also be used for navigating backward
from the top-left position to wrap to lower-right. When NIL,
POINT is not advanced in such circumstances."
:type 'boolean)
(defcustom crossword-auto-nav-only-within-clue t
"Stay within a clue when entering or deleting data.
Don't ever automatically proceed to the first square of the next
clue. If all squares of the current clue have alpha characters,
then stay at the current square. Otherwise, auto advance to the
next non-alpha character square of the current clue,
wrapping-around if necessary."
:type 'boolean)
(defcustom crossword-arrow-changes-direction t
"Arrow keys also change current clue direction across/down.
When nil, arrow keys always navigate to next square in their
direction. When non-nil, arrow keys only navigate in the current
clue direction, and if that doesn't match the arrow direction,
the clue direction is changed."
:type 'boolean)
(defcustom crossword-tab-to-next-unfilled t
"Tabbing navigates to first empty square in next field.
When NIL, functions `crossword-next-field' and
`crossword-prior-field' always navigate to the first square of
the next/prior field. When non-NIL, they navigate to the first
empty square in the next/prior that has an empty square, which
could be several fields distant."
:type 'boolean)
(defcustom crossword-download-puz-alist '(
("Universal Daily (Daily 15x15)"
"http://herbach.dnsalias.com/uc/uc%y%m%d.puz")
("Universal Daily (Sunday bonus, 21x21)"
"http://herbach.dnsalias.com/uc/ucs%y%m%d.puz")
("Wall Street Journal (Mondays-Saturdays)"
"http://herbach.dnsalias.com/wsj/wsj%y%m%d.puz")
("Washington Post (Sundays)"
"http://herbach.dnsalias.com/WaPo/wp%y%m%d.puz")
("Matt Jones' (Thursdays)"
"http://herbach.dnsalias.com/Jonesin/jz%y%m%d.puz"))
"Download resources for .puz files."
:type '(repeat (list (string :tag "Resource description")
(string :tag "URL"))))
(defcustom crossword-download-xml-alist '(
("Los Angeles Times" .
"http://cdn.games.arkadiumhosted.com/latimes/assets/DailyCrossword/la%y%m%d.xml")
("Newsday" .
"http://picayune.uclick.com/comics/crnet/data/crnet%y%m%d-data.xml")
("USA Today (Monday-Saturday?)" .
"http://picayune.uclick.com/comics/usaon/data/usaon%y%m%d-data.xml")
("Universal" .
"http://picayune.uclick.com/comics/fcx/data/fcx%y%m%d-data.xml")
("LA Times Sunday" .
"http://picayune.uclick.com/comics/lacal/data/lacal%y%m%d-data.xml"))
"Download resources for .xml file.
NOTE: Support for this file format has not yet been written!"
:type '(repeat (cons (string :tag "Resource description")
(string :tag "URL"))))
(defcustom crossword-puzzle-file-coding 'iso-8859-1
"Coding system for reading puz files.
Only change this if you are having problems reading a puz file."
:type 'coding-system)
;;
;;; Faces
(defface crossword-current-face
'((((class color) (background light))
(:background "lightgreen" :foreground "black" :inherit 'normal))
(((class color) (background dark))
(:background "darkgreen" :foreground "black" :inherit 'normal))
(t (:background "darkgreen" :foreground "black" :inherit 'normal)))
"For the current clue and word.")
(defface crossword-other-dir-face
'((((class color) (background light))
(:background "darkgrey" :foreground "black" :inherit 'normal))
(((class color) (background dark))
(:background "brightblack" :foreground "black" :inherit 'normal))
(t (:background "brightblack" :foreground "black" :inherit 'normal)))
"For the current clue and word.")
(defface crossword-error-face
'((t (:background "red" :foreground "black" :inherit 'normal)))
"For a letter that has been checked and is wrong.")
(defface crossword-error-inverse-face
'((t (:inverse-video t :inherit 'crossword-error-face)))
"For a letter that has been checked and is wrong.")
(defface crossword-checked-face
'((t (:foreground "cyan" :inherit 'normal)))
"For a letter that has been checked and is correct.")
(defface crossword-solved-face
'((((class color) (background light))
(:background "cyan" :inherit 'normal))
(((class color) (background dark))
(:foreground "brightyellow" :inherit 'normal))
(t (:foreground "brightyellow" :inherit 'normal)))
"For a letter that the user has asked to be solved.")
(defface crossword-grid-face
'((((class color) (background light))
(:foreground "blue" :inherit 'normal))
(((class color) (background dark))
(:foreground "blue" :inherit 'normal))
(t (:foreground "blue" :inherit 'normal)))
"For un-writable squares and grid-lines.")
;;
;;; Constants
(defconst crossword--max-width 30
"Arbitrarily set, for sanity checking.")
(defconst crossword--max-height 30
"Arbitrarily set, for sanity checking.")
(defconst crossword--grid-characters
(format "[^\n|%s]" crossword-empty-position-char)
"Characters not part of the grid layout.
These mark un-writable positions of the grid. This variable is
used to auto-advance to the next across position when inserting a
character.")
;;
;;; Buffer-local variables (only for crossword-grid buffer)
(defvar-local crossword--version nil
"Version used to create puz-emacs file.
For format and data structure compatability purposes.")
(defvar-local crossword--filename nil)
(defvar-local crossword--hash nil)
(defvar-local crossword--date nil)
(defvar-local crossword--across-buffer nil)
(defvar-local crossword--down-buffer nil)
;; Useful static positions within a "Crossword grid" buffer.
(defvar-local crossword--first-square nil)
(defvar-local crossword--last-square nil)
(defvar-local crossword--timer-state-pos 0)
(defvar-local crossword--timer-value-pos 0)
(defvar-local crossword--checked-count-pos 0)
(defvar-local crossword--error-count-pos 0)
(defvar-local crossword--cheat-count-pos 0)
(defvar-local crossword--solved-percent-pos 0)
(defvar-local crossword--completion-percent-pos 0)
(defvar-local crossword--grid-end 0)
(defvar-local crossword--prior-point 1
"Used by function `crossword--update-faces'.")
(defvar-local crossword--first-column 3
"Really a constant. Placed here among similar symbols.")
(defvar-local crossword--last-column 0)
;; Tallies for progress statistics feedback
(defvar-local crossword--total-count 0
"Total number of puzzles squares to be solved.")
(defvar-local crossword--completed-count 0
"Number of squares filled.")
(defvar-local crossword--solved-count 0
"Number of puzzle squares verified as correctly solved.")
(defvar-local crossword--error-count 0
"Number of unique letters checked and found incorrect.")
(defvar-local crossword--checked-count 0
"Number of unique letters checked.")
(defvar-local crossword--cheat-count 0
"Number of unique letters asked for as hints.")
;; Timer-related
(defvar-local crossword--timer-elapsed 0)
(defvar-local crossword--timer-object nil)
(defvar-local crossword--across-clue-list nil
"Bounds data structure for across clues.
A list, each member of which comprises six elements: The clue
number; The clue string; The first buffer position of the clue;
The final buffer position of the clue, and; The beginning and end
positions of the clue in the `crossword--across-buffer'..")
(defvar-local crossword--down-clue-list nil
"Bounds data structure for down clues.
A list, each member of which comprises five elements: The clue
number; The clue string; a list of buffer positions of the clue,
and; The beginning and end positions of the clue in the
`crossword--down-buffer'.")
(defvar-local crossword--nav-dir 'across
"Current navigation direction within the puzzle,
either 'across or 'down.")
(defvar-local crossword--downloading-available
(load "crossword-download" t)
"The crossword downloader is optional and independent.")
(defvar-local crossword--local-proc nil
"Stores a process object for a download.")
(defvar-local crossword--called-interactively-p nil
"Record if containing function was called interactively.")
;;
;;; Buffer-local variables (only for crossword-download buffer)
(defvar-local crossword--download-processes-list nil)
;;
;;; Keymap and Mode definitions
(defvar crossword-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "C-x C-s") 'crossword-backup)
(define-key map (kbd "C-c C-x C-f") 'crossword-restore)
(define-key map (kbd "<left>") 'crossword-prior-char)
(define-key map (kbd "<right>") 'crossword-next-char)
(define-key map (kbd "<RET>") 'crossword-next-char)
(define-key map (kbd "<deletechar>") 'crossword-del-char)
(define-key map (kbd "SPC") 'crossword-del-char)
(define-key map (kbd "DEL") 'crossword-bsp-char)
(define-key map (kbd "M-a") 'crossword-nav-dir-across)
(define-key map (kbd "M-d") 'crossword-nav-dir-down)
(define-key map (kbd "M-<SPC>") 'crossword-nav-dir-toggle)
(define-key map (kbd "C-a") 'crossword-begin-field)
(define-key map (kbd "C-e") 'crossword-end-field)
(define-key map "\t" 'crossword-next-field)
(define-key map (kbd "<backtab>") 'crossword-prior-field)
(define-key map (kbd "/") 'crossword-goto-clue)
(define-key map (kbd "M-<") 'crossword-first-square)
(define-key map (kbd "M->") 'crossword-last-square)
(define-key map (kbd "M-c l") 'crossword-check-letter)
(define-key map (kbd "M-c w") 'crossword-check-word)
(define-key map (kbd "M-c p") 'crossword-check-puzzle)
(define-key map (kbd "M-s l") 'crossword-solve-letter)
(define-key map (kbd "M-s w") 'crossword-solve-word)
(define-key map (kbd "M-s p") 'crossword-solve-puzzle)
(define-key map (kbd "M-p") 'crossword-pause-unpause-timer)
(define-key map (kbd "M-t") 'crossword-pause-unpause-timer)
(define-key map (kbd "M-q") 'crossword-quit)
(define-key map [remap next-line] 'crossword-next-line)
(define-key map [remap previous-line] 'crossword-previous-line)
(define-key map (kbd "<M-up>") 'crossword-clue-scroll-up)
(define-key map (kbd "<M-down>") 'crossword-clue-scroll-down)
(define-key map (kbd "M-<next>") 'crossword-clue-scroll-page-up)
(define-key map (kbd "M-<prior>") 'crossword-clue-scroll-page-down)
(define-key map (kbd "M-<home>") 'crossword-clue-scroll-page-home)
(define-key map (kbd "M-<end>") 'crossword-clue-scroll-page-end)
map))
(defvar crossword-summary-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "q") 'crossword-quit)
(define-key map (kbd "M-q") 'crossword-quit)
(define-key map (kbd "<RET>") 'crossword-summary-select)
(define-key map (kbd "d") 'crossword-summary-delete)
(define-key map "\t" 'crossword-summary-tab-forward)
(define-key map (kbd "<backtab>") 'crossword-summary-tab-backward)
(define-key map (kbd "g") 'crossword-summary-revert-buffer)
(define-key map (kbd "S") 'crossword-summary-sort)
map))
(define-derived-mode crossword-mode fundamental-mode "Crossword"
"Operate on puz file format crossword puzzles.
\\{crossword-mode-map}"
(add-hook 'post-command-hook #'crossword--update-faces t t)
(overwrite-mode)
(face-remap-add-relative 'default :family "Monospace")
(advice-add #'self-insert-command
:around #'crossword--advice-around-self-insert-command)
(advice-add #'call-interactively
:around #'crossword--advice-around-call-interactively))
(define-derived-mode crossword-summary-mode tabulated-list-mode
"Known crossword puzzles"
"Mode for displayed crossword summary lists."
(setq tabulated-list-format
`[("filename" 10 t)
("extension" 9 t)
("date" 10 t)
("title" 30 t)
("author" 30 t)
("publisher/copyright" 25 t)
("%%filled" 7 t :right-align t)
("%%solved" 7 t :right-align t)
("checked" 7 t :right-align t)
("errors" 7 t :right-align t)
("cheats" 7 t :right-align t)
("time" 5 t)])
(setq tabulated-list-sort-key (cons "date" 'flip))
(setq tabulated-list-padding 2)
(setq tabulated-list-entries #'crossword--summary-list-entries)
(add-hook 'tabulated-list-revert-hook #'crossword--summary-revert-hook-function nil t)
(hl-line-mode)
(tabulated-list-init-header))
;;
;;; Macros
(defmacro crossword--get-date (file)
"Return a consistent date string for FILE."
`(format-time-string "%Y/%m/%d" (nth 6 (file-attributes ,file))))
(defmacro crossword--summary-file ()
"Return the path to the expected crossword summary file."
`(concat (file-name-as-directory crossword-save-path) "puz-emacs.data"))
(defmacro crossword--puzzle-file-list ()
"Return list of known puzzle files."
`(directory-files crossword-save-path 'full "\\.puz\\(-emacs\\)?$"))
;;
;;; Hook functions
(defun crossword--kill-grid-buffer-hook-function ()
"Hook function for `kill-buffer-hook'."
(when crossword--timer-object
(cancel-timer crossword--timer-object))
(let ((crossword-quit-to-browser nil))
(crossword-quit)))
(defun crossword--kill-all-asociated-processes ()
"Crossword-download buffer hook function for `kill-buffer-hook'."
(let (x)
(while (setq x (pop crossword--download-processes-list))
(when (processp (car x))
(delete-process (car x)))
(when (bufferp (cdr x))
(kill-buffer (cdr x))))))
;;(defun crossword--kill-associated-process () t)
(defun crossword--kill-associated-process ()
"Crossword-download buffer hook function for `kill-buffer-hook'.
PROC should have been set as a local variable at buffer creation."
(if (not (processp crossword--local-proc))
(message "Warning: no process found to kill (crossword-download).")
(delete-process crossword--local-proc)
(assq-delete-all crossword--local-proc crossword--download-processes-list)))
(defun crossword--face-current-remove (start end)
"Remove just `crossword-current-face' from region START END.
Leave alone any other faces."
(let ((pos start)
this-face)
(while (< pos end)
(when (setq this-face (get-text-property pos 'face))
(if (eq this-face 'crossword-current-face)
(remove-text-properties pos (1+ pos) '(face))
(when (listp this-face)
(put-text-property pos (1+ pos)
'face (delq 'crossword-current-face this-face)))))
(cl-incf pos))))
(defun crossword--face-current-add (start end)
"Conditionally apply `crossword-current-face' to region START END.
Only do so at points that have no other face."
;; FIXME: This seems no longer necessary
(add-face-text-property start end 'crossword-current-face t))
(defun crossword--update-faces (&optional force)
"Called mainly as a `post-command-hook' function for `crossword-mode'.
Called also by `crossword-restore', in which case the optional FORCE
arg is non-nil in order to ensure that clue buffers are properly
fontified. Also called by `crossword--start-game'.
This function operates upon all three crossword buffers, and
expects the current buffer to be the crossword grid-buffer."
(when (or (not (equal (point) crossword--prior-point))
force)
(let ((grid-buffer (current-buffer))
(across-buffer crossword--across-buffer)
(down-buffer crossword--down-buffer)
(nav-dir crossword--nav-dir)
(inhibit-read-only t)
(return-pos (point))
(this-clue-across (get-text-property (point) 'clue-across))
(this-clue-down (get-text-property (point) 'clue-down))
clue prior-clue-num pos pos-max pos-list)
;; Grid buffer: across calculations
(setq prior-clue-num (get-text-property crossword--prior-point 'clue-across))
(when (or (not (eq prior-clue-num this-clue-across))
force)
(when prior-clue-num
(setq clue (assq prior-clue-num crossword--across-clue-list)
pos (nth 2 clue)
pos-max (nth 3 clue))
(while (< pos pos-max)
(unless (and (eq nav-dir 'down) this-clue-down
(eq this-clue-down
(get-text-property pos 'clue-down)))
(crossword--face-current-remove pos (1+ pos)))
(cl-incf pos))
(with-current-buffer across-buffer
(remove-text-properties (nth 4 clue) (nth 5 clue) '(face))))
(when this-clue-across
(setq clue (assq this-clue-across crossword--across-clue-list))
(when (eq nav-dir 'across)
(crossword--face-current-add (nth 2 clue) (nth 3 clue)))))
;; Grid buffer: down calculations
(setq prior-clue-num (get-text-property crossword--prior-point 'clue-down))
(when (or (not (eq prior-clue-num this-clue-down))
force)
(when prior-clue-num
(setq clue (assq prior-clue-num crossword--down-clue-list))
(setq pos-list (nth 2 clue))
(while (setq pos (pop pos-list))
(unless (and (eq nav-dir 'across)
(eq this-clue-across
(get-text-property pos 'clue-across)))
(crossword--face-current-remove pos (1+ pos))))
(with-current-buffer down-buffer
(remove-text-properties (nth 3 clue) (nth 4 clue) '(face))))
(when this-clue-down
(setq clue (assq this-clue-down crossword--down-clue-list))
(when (eq nav-dir 'down)
(setq pos-list (nth 2 clue))
(while (setq pos (pop pos-list))
(crossword--face-current-add pos (1+ pos))))))
;; Across-clues buffer
(when this-clue-across
(setq clue (assq this-clue-across crossword--across-clue-list))
(pop-to-buffer across-buffer)
(goto-char (nth 4 clue))
(put-text-property (point) (nth 5 clue)
'face (if (eq nav-dir 'across)
'crossword-current-face
'crossword-other-dir-face))
(recenter)
(while (pos-visible-in-window-p (point-max))
(scroll-down 1)))
(pop-to-buffer grid-buffer) ;; to access buffer-local variable
;; Down-clues buffer
(when this-clue-down
(setq clue (assq this-clue-down crossword--down-clue-list))
(pop-to-buffer down-buffer)
(goto-char (nth 3 clue))
(put-text-property (point) (nth 4 clue)
'face (if (eq nav-dir 'down)
'crossword-current-face
'crossword-other-dir-face))
(recenter)
(while (pos-visible-in-window-p (point-max))
(scroll-down 1)))
;; Grid clues (bottom of the grid buffer)
(pop-to-buffer grid-buffer)
(crossword--update-grid-clues this-clue-across this-clue-down)
(goto-char (setq crossword--prior-point (min return-pos (point-max)))))))
(defun crossword--update-grid-clues (this-clue-across this-clue-down)
"Update the clue text appearing below the crossword grid.
THIS-CLUE-ACROSS and THIS-CLUE-DOWN are integers values."
(let ((return-pos (point))
elem)
(goto-char crossword--grid-end)
(delete-region (point) (point-max))
(setq return-pos (min return-pos (point-max)))
(insert
(format " Across:\n\n%s\n Down:\n\n%s%s"
(if (not this-clue-across) ""
(setq elem (assq this-clue-across crossword--across-clue-list))
(with-current-buffer crossword--across-buffer
(buffer-substring (nth 4 elem) (nth 5 elem))))
(if (not this-clue-down) ""
(setq elem (assq this-clue-down crossword--down-clue-list))
(with-current-buffer crossword--down-buffer
(buffer-substring (nth 3 elem) (nth 4 elem))))
(if (not (setq elem (get-text-property return-pos 'errors))) ""
(format "\n Errors:\n\n %s"
(propertize (substring (format "%s" elem) 1 -1)
'face 'crossword-error-inverse-face)))))
(goto-char return-pos)))
(defun crossword--summary-revert-hook-function ()
"Hook function for `tabulated-list-revert-hook'.
See mode `crossword-summary-mode'."
(crossword-summary-rebuild-data)
(tabulated-list-init-header)
(tabulated-list-print t)
(recenter))
;;
;;; Advice functions
(defun crossword--advice-around-self-insert-command (oldfun &rest args)
"Advice for handling insertions in the 'Crossword grid' buffer.
While this function needs no arguments for itself, the advised
function does: OLDFUN is the advised function, per function
`advice-add' and ARGS are per function `self-insert-command'.
This function is a central part of the package, as it controls
data-entry, fontification, advances POINT to the next grid
position, and updates the puzzle's completion statistics."
(if (not (and crossword--called-interactively-p
(eq major-mode 'crossword-mode)
(get-text-property (point) 'answer)
(not (get-text-property (point) 'solved))))
(apply oldfun args)
(setq crossword--called-interactively-p nil)
(crossword--insert-char)
(if crossword-auto-nav-only-within-clue
(crossword--next-square-in-clue 'wrap)
(crossword--next-logical-square))))
(defun crossword--advice-around-call-interactively (oldfun func &rest args)
"Possibly set variable `crossword--called-interactively-p'.
This is necessary to enable advising around
`self-insert-command'. OLDFUN is the advised function, per function
`advice-add' and FUNC and ARGS are per function `call-interactively'."
(when (and (eq func 'self-insert-command)
(eq major-mode 'crossword-mode)
(get-text-property (point) 'answer)
(not (get-text-property (point) 'solved)))
(setq crossword--called-interactively-p t))
(apply oldfun func args))
;;
;;; Internal functions
(defun crossword--check-and-create-save-path ()
"Check that a crossword save directory exists.
If not, prompts the user to create one. Return non-nil on
sucess."
(let ((save-dir crossword-save-path))
(cond
((file-directory-p save-dir) t)
((yes-or-no-p (format "Save directory '%s' does not exist.\nWe can create it now, or you can choose another save-path.\nDo so now? " save-dir))
(while (not (setq save-dir (read-file-name "? " nil "Crosswords"))))
(make-directory save-dir t)
(setq crossword-save-path (file-name-as-directory save-dir)))
(t nil))))
(defun crossword--calendar-read-date ()
"Prompt for Gregorian date. Return a list (day month year).
This function is based upon function `calendar-read-date' of
package calendar.el. See Emacs bug report 32105 for the relevant
discussion and patch submission. Additionally, this version
orders its return list to be consistent with function
`encode-time'."
(let* ((today (calendar-current-date))
(year (calendar-read
"Year (>0): "
(lambda (x) (> x 0))
(number-to-string (calendar-extract-year today))))
(month-array calendar-month-name-array)
(completion-ignore-case t)
(month (cdr (assoc-string
(completing-read
"Month name: "
(mapcar #'list (append month-array nil))
nil t nil nil
(aref month-array (1- (calendar-extract-month today))))
(calendar-make-alist month-array 1) t)))
(last (calendar-last-day-of-month month year)))
(list (calendar-read (format "Day (1-%d): " last)
(lambda (x) (and (< 0 x) (<= x last)))
(number-to-string (calendar-extract-day today)))
month
year)))
(defun crossword--window-resize-function (frame)
"How to respond to window/frame resize events.
FRAME is expected to be the `selected-frame'. This function is
meant for variable `window-size-change-functions'. It rebalances
the frame's three windows, auto-fills the contents of the two
clue listing buffers, and updates the clue data-structures."
(when (and (equal "Crossword"
(cdr (assq 'name (frame-parameters frame))))
(not (minibuffer-window-active-p (active-minibuffer-window))))
(balance-windows)
;; snippet based upon part of function `crossword--start-game-puz'
(let (grid-buffer
(across-buffer crossword--across-buffer)
(down-buffer crossword--down-buffer)
(across-clue-list crossword--across-clue-list)
(down-clue-list crossword--down-clue-list)
(inhibit-read-only t))
(when (setq grid-buffer (get-buffer "Crossword grid"))
(set-buffer grid-buffer)
(cl-flet ((strip2 (x) (mapcar (lambda (elem) (butlast elem 2)) x)))
(setq across-clue-list
(crossword--insert-clues across-buffer
'clue-across
(strip2 across-clue-list)
"--- Across clues for crossword"))
(setq down-clue-list
(crossword--insert-clues down-buffer
'clue-down
(strip2 down-clue-list)
"--- Down clues for crossword")))
;; ** Finish in grid buffer
(set-buffer grid-buffer)