forked from rnkn/fountain-mode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fountain-mode.el
3522 lines (3115 loc) · 128 KB
/
fountain-mode.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
;;; fountain-mode.el --- Major mode for screenwriting in Fountain markup -*- lexical-binding: t; -*-
;; Copyright (c) 2014-2018 Paul William Rankin
;; Copyright (c) 2019 Free Software Foundation, Inc.
;; Copyright (c) 2019-2020 Paul William Rankin
;; Author: William Rankin <william@bydasein.com>
;; Keywords: wp, text
;; Version: 3.2.1
;; Package-Requires: ((emacs "24.4") (seq "2.20"))
;; URL: https://github.com/rnkn/fountain-mode
;; This file is not part of GNU Emacs.
;; This program 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 program 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 program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; # Fountain Mode #
;; Fountain Mode is a scriptwriting program for GNU Emacs using the
;; Fountain plain text markup format.
;; For more information on the Fountain format, visit <https://fountain.io>.
;; Community screenshots: <https://github.com/rnkn/fountain-mode/issues/114>
;; ## Features ##
;; - Support for Fountain 1.1 specification
;; - WYSIWYG auto-align elements (display only, does not modify file
;; contents) specific to script format, e.g. screenplay, stageplay or
;; user-defined format
;; - Traditional TAB auto-completion writing style
;; - Navigation by section, scene, character name, or page
;; - Integration with outline to fold/cycle visibility of sections and
;; scenes
;; - Integration with electric-pair-mode to insert emphasis delimiters
;; with single key (i.e. * or _)
;; - Integration with imenu (sections, scene headings, notes)
;; - Intergration with auto-insert for title page metadata
;; - Automatically add/remove character (CONT'D)
;; - Toggle syntax highlighting of each element
;; - Toggle visibility of emphasis and syntax markup
;; - Optionally display scene numbers in the right margin
;; - Intelligent insertion of a page breaks
;; Most common features are accessible from the menu. For a full list of
;; functions and key-bindings, type C-h m.
;; ## Requirements ##
;; - Emacs 24.5
;; ## Exporting ##
;; Earlier versions of Fountain Mode had export functionality, but this was
;; not very good and is better handled via interfacing with external shell
;; tools, such as:
;; - [afterwriting](https://github.com/ifrost/afterwriting-labs/blob/master/docs/clients.md) (JavaScript)
;; - [Wrap](https://github.com/Wraparound/wrap) (Go)
;; - [screenplain](https://github.com/vilcans/screenplain) (Python 2)
;; - [Textplay](https://github.com/olivertaylor/Textplay) (Ruby, requires PrinceXML for PDF)
;; The option fountain-export-command-profiles provides some shell
;; commands to interface with these tools, but you are encouraged to edit
;; or completely replace these to suit your own needs. The format is simple
;; while still allowing for a lot of flexibility.
;; ## Installation ##
;; The latest stable release of Fountain Mode is available via
;; [MELPA-stable]. First, add MELPA-stable to your package archives:
;; M-x customize-option RET package-archives RET
;; Insert an entry named melpa-stable with the URL https://stable.melpa.org/packages/.
;; You can then find the latest stable version of fountain-mode in the
;; list returned by:
;; M-x list-packages RET
;; If you prefer the latest but perhaps unstable version, do the above
;; using [MELPA].
;; ## Advanced Installation ##
;; Download the [latest release], move this file into your load-path and
;; add to your init.el file:
;; (require 'fountain-mode)
;; If you wish to contribute to or alter Fountain Mode's code, clone the
;; repository into your load-path and require as above:
;; git clone https://github.com/rnkn/fountain-mode.git
;; [melpa]: https://melpa.org/#/fountain-mode "MELPA"
;; [melpa-stable]: https://stable.melpa.org/#/fountain-mode "MELPA-stable"
;; [latest release]: https://github.com/rnkn/fountain-mode/releases/latest "Fountain Mode latest release"
;; ## History ##
;; See: <https://github.com/rnkn/fountain-mode/releases>
;; ## Bugs and Feature Requests ##
;; Report bugs and feature requests at: <https://github.com/rnkn/fountain-mode/issues>
;;; Code:
(eval-when-compile (require 'subr-x))
(eval-when-compile (require 'cl-lib))
(require 'seq)
(eval-when-compile
(require 'lisp-mnt)
(defconst fountain-version
(lm-version load-file-name)))
(defun fountain-version ()
"Return `fountain-mode' version."
(interactive)
(message "Fountain Mode %s" fountain-version))
;;; Top-Level Options
(defgroup fountain ()
"Major mode for screenwriting in Fountain markup."
:prefix "fountain-"
:group 'text)
(defun fountain--set-and-refresh-font-lock (symbol value)
"Set SYMBOL to VALUE and refresh defaults.
Cycle buffers and call `font-lock-refresh-defaults' when
`fountain-mode' is active."
(set-default symbol value)
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'fountain-mode)
(font-lock-refresh-defaults))))
(buffer-list)))
(defcustom fountain-mode-hook
'(visual-line-mode)
"Mode hook for `fountain-mode', run after the mode is turned on."
:group 'fountain
:type 'hook
:options '(visual-line-mode
electric-pair-local-mode
imenu-add-menubar-index
fountain-completion-update
fountain-outline-hide-custom-level
flyspell-mode))
(define-obsolete-variable-alias 'fountain-script-format
'fountain-default-script-format "`fountain-mode' 3.0")
(defcustom fountain-default-script-format
"screenplay"
"Default script format.
Can be overridden in metadata with, e.g.
format: teleplay"
:group 'fountain
:type 'string
:safe 'string)
(defcustom fountain-add-continued-dialog
t
"\\<fountain-mode-map>If non-nil, \\[fountain-continued-dialog-refresh] will mark continued dialogue.
When calling `fountain-continued-dialog-refresh', append
`fountain-continued-dialog-string' to characters speaking in
succession, or if nil, remove this string."
:group 'fountain
:type 'boolean
:safe 'booleanp)
(defcustom fountain-continued-dialog-string
"(CONT'D)"
"String to append to character name speaking in succession.
If `fountain-add-continued-dialog' is non-nil, append this string
to characters speaking in succession when calling
`fountain-continued-dialog-refresh'.
n.b. if you change this option then call
`fountain-continued-dialog-refresh', strings matching the
previous value will not be recognized. First remove all instances
in your script by setting `fountain-add-continued-dialog' to nil
and calling `fountain-continued-dialog-refresh', then customize
this option."
:group 'fountain
:type 'string
:safe 'stringp)
(defcustom fountain-more-dialog-string
"(MORE)"
"String to append to dialog when breaking across pages."
:type 'string
:safe 'stringp)
(define-obsolete-variable-alias 'fountain-hide-emphasis-delim
'fountain-hide-emphasis-markup "`fountain-mode' 3.0")
(defcustom fountain-hide-emphasis-markup
nil
"If non-nil, make emphasis delimiters invisible."
:group 'fountain
:type 'boolean
:safe 'booleanp
:set (lambda (symbol value)
(set-default symbol value)
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'fountain-mode)
(if fountain-hide-emphasis-markup
(add-to-invisibility-spec 'fountain-emphasis-delim)
(remove-from-invisibility-spec 'fountain-emphasis-delim))
(font-lock-refresh-defaults))))
(buffer-list))))
(define-obsolete-variable-alias 'fountain-hide-syntax-chars
'fountain-hide-element-markup "`fountain-mode' 3.0")
(defcustom fountain-hide-element-markup
nil
"If non-nil, make syntax characters invisible."
:group 'fountain
:type 'boolean
:safe 'booleanp
:set (lambda (symbol value)
(set-default symbol value)
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'fountain-mode)
(if fountain-hide-element-markup
(add-to-invisibility-spec 'fountain-syntax-chars)
(remove-from-invisibility-spec 'fountain-syntax-chars))
(font-lock-refresh-defaults))))
(buffer-list))))
(defcustom fountain-auto-upcase-scene-headings
t
"If non-nil, automatically upcase scene headings as you type.
Upcases lines matching `fountain-scene-heading-regexp'."
:type 'boolean
:safe 'booleanp
:group 'fountain)
(defcustom fountain-dwim-insert-next-character
nil
"When non-nil `fountain-dwim' inserts next character after dialogue."
:type 'boolean
:safe 'booleanp
:group 'fountain)
(defcustom fountain-note-template
" %x - %n: "
"\\<fountain-mode-map>Template for inserting notes with \\[fountain-insert-note].
Passed to `format-spec' with the following specification:
%u `user-login-name'
%n `user-full-name'
%e `user-mail-address'
%x date in locale's preferred format
%F date in ISO format
The default \" %x - %n:\" inserts something like:
[[ 12/31/2017 - Alan Smithee: ]]"
:group 'fountain
:type 'string
:safe 'stringp)
(defcustom fountain-highlight-elements
'(section-heading scene-heading character dialog synopsis note
metadata page-break)
"List of elements highlighted with `font-lock-mode'."
:type '(choice (const :tag "No Highlighting" nil)
(set (const :tag "Section Headings" section-heading)
(const :tag "Scene Headings" scene-heading)
(const :tag "Action" action)
(const :tag "Character Names" character)
(const :tag "Dialogue" dialog)
(const :tag "Parentheticals" paren)
(const :tag "Transitions" trans)
(const :tag "Synopses" synopsis)
(const :tag "Notes" note)
(const :tag "Metadata" metadata)
(const :tag "Center Text" center)
(const :tag "Page Breaks" page-break)))
:group 'fountain
:set 'fountain--set-and-refresh-font-lock)
(defvar fountain-highlight-elements-always
'(underline italic bold bold-italic lyrics)
"List of elements always highlighted with `font-lock-mode'.")
;;; Faces
(defgroup fountain-faces ()
"Faces used in `fountain-mode'
Control which elements are highlighted with
`fountain-highlight-elements'."
:prefix "fountain-"
:link '(info-link "(emacs) Font Lock")
:group 'fountain)
(defface fountain
'((t nil))
"Default base-level face for `fountain-mode' buffers.")
(defface fountain-action
'((t nil))
"Default face for action.")
(defface fountain-comment
'((t (:inherit shadow)))
"Default face for comments (boneyard).")
(defface fountain-non-printing
'((t (:inherit fountain-comment)))
"Default face for element and emphasis markup.")
(defface fountain-metadata-key
'((t (:inherit font-lock-constant-face)))
"Default face for metadata keys.")
(defface fountain-metadata-value
'((t (:inherit font-lock-keyword-face)))
"Default face for metadata values.")
(defface fountain-page-break
'((t (:inherit font-lock-constant-face)))
"Default face for page breaks.")
(defface fountain-page-number
'((t (:inherit font-lock-warning-face)))
"Default face for page numbers.")
(defface fountain-scene-heading
'((t (:inherit font-lock-function-name-face)))
"Default face for scene headings.")
(defface fountain-paren
'((t (:inherit font-lock-string-face)))
"Default face for parentheticals.")
(defface fountain-center
'((t nil))
"Default face for centered text.")
(defface fountain-note
'((t (:inherit font-lock-comment-face)))
"Default face for notes.")
(define-obsolete-face-alias 'fountain-section-heading
'fountain-section-heading-1 "`fountain-mode' 3.0")
(defface fountain-section-heading-1
'((t (:inherit outline-1)))
"Default face for section level 1 headings.")
(defface fountain-section-heading-2
'((t (:inherit outline-2)))
"Default face for section level 2 headings.")
(defface fountain-section-heading-3
'((t (:inherit outline-3)))
"Default face for section level 3 headings.")
(defface fountain-section-heading-4
'((t (:inherit outline-4)))
"Default face for section level 4 headings.")
(defface fountain-section-heading-5
'((t (:inherit outline-5)))
"Default face for section level 5 headings.")
(defface fountain-synopsis
'((t (:inherit font-lock-type-face)))
"Default face for synopses.")
(defface fountain-character
'((t (:inherit font-lock-variable-name-face)))
"Default face for characters.")
(defface fountain-dialog
'((t (:inherit font-lock-string-face)))
"Default face for dialog.")
(defface fountain-trans
'((t nil))
"Default face for transitions.")
;;; Regular Expressions
(defvar fountain-scene-heading-regexp
nil
"Regular expression for matching scene headings.
Group 1: match leading . for forced scene heading
Group 2: match whole scene heading without scene number
Group 3: match INT/EXT
Group 4: match location
Group 5: match suffix separator
Group 6: match suffix
Group 7: match space between scene heading and scene number
Group 8: match first # delimiter
Group 9: match scene number
Group 10: match last # delimiter
Contructed with `fountain-init-scene-heading-regexp'. Requires
`fountain-match-scene-heading' for preceding blank line.")
(defcustom fountain-scene-heading-prefix-list
'("INT" "EXT" "EST" "INT./EXT." "INT/EXT" "I/E")
"List of scene heading prefixes (case insensitive).
Any scene heading prefix can be followed by a dot and/or a space,
so the following are equivalent:
INT HOUSE - DAY
INT. HOUSE - DAY"
:type '(repeat (string :tag "Prefix"))
:group 'fountain
:set (lambda (symbol value)
(set-default symbol value)
(when (featurep 'fountain-mode)
(fountain-init-scene-heading-regexp)
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'fountain-mode)
(fountain-init-outline-regexp)
(font-lock-refresh-defaults))))
(buffer-list)))))
(define-obsolete-variable-alias 'fountain-scene-heading-suffix-sep
'fountain-scene-heading-suffix-separator "`fountain-mode' 3.0")
(defcustom fountain-scene-heading-suffix-separator
" --? "
"Regular expression separating scene heading location from suffix.
n.b. If you change this any existing scene headings may not
be parsed correctly."
:group 'fountain
:type 'regexp
:safe 'regexp
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-scene-heading-suffix-list
'("DAY" "NIGHT" "CONTINUOUS" "LATER" "MOMENTS LATER")
"List of scene heading suffixes (case insensitive).
These are only used for auto-completion. Any scene headings can
have whatever suffix you like.
Separated from scene heading locations with
`fountain-scene-heading-suffix-separator'."
:group 'fountain
:type '(repeat (string :tag "Suffix"))
:set #'fountain--set-and-refresh-font-lock)
(defvar fountain-trans-regexp
nil
"Regular expression for matching transitions.
Group 1: match forced transition mark
Group 2: match transition
Constructed with `fountain-init-trans-regexp'. Requires
`fountain-match-trans' for preceding and succeeding blank
lines.")
(defcustom fountain-trans-suffix-list
'("TO:" "WITH:" "FADE OUT" "TO BLACK")
"List of transition suffixes (case insensitive).
This list is used to match the endings of transitions,
e.g. `TO:' will match both the following:
CUT TO:
DISSOLVE TO:"
:type '(repeat (string :tag "Suffix"))
:group 'fountain
:set (lambda (symbol value)
(set-default symbol value)
(when (featurep 'fountain-mode)
(fountain-init-trans-regexp)
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'fountain-mode)
(font-lock-refresh-defaults))))
(buffer-list)))))
(defcustom fountain-character-extension-list
'("(V.O.)" "(O.S.)" "(O.C.)")
"List of extensions after character names (case sensitive).
`fountain-continued-dialog-string' is automatically added to this
list.
These are only used for auto-completion. Any character can have
whatever extension you like."
:group 'fountain
:type '(repeat (string :tag "Extension")))
(defconst fountain-action-regexp
"^\\(!\\)?\\(.*\\)[\s\t]*$"
"Regular expression for forced action.
Group 1: match forced action mark
Group 2: match trimmed whitespace")
(defconst fountain-comment-regexp
(concat "\\(?://[\s\t]*\\(?:.*\\)\\)"
"\\|"
"\\(?:\\(?:/\\*\\)[\s\t]*\\(?:\\(?:.\\|\n\\)*?\\)[\s\t]*\\*/\\)")
"Regular expression for matching comments.")
(defconst fountain-metadata-regexp
(concat "^\\([^:\s\t\n][^:\n]*\\):[\s\t]*\\(.+\\)?"
"\\|"
"^[\s\t]+\\(?2:.+\\)")
"Regular expression for matching multi-line metadata values.
Requires `fountain-match-metadata' for `bobp'.")
(defconst fountain-character-regexp
(concat "^[\s\t]*"
"\\(?:\\(?1:@\\)\\(?2:[^<>\n]+?\\)"
"\\|"
"\\(?2:[^<>\n[:lower:]]*?[[:upper:]]+[^<>\n[:lower:]]*?\\)"
"\\)"
"\\(?3:[\s\t]*\\(?4:(\\).*?)\\)*?"
"\\(?5:[\s\t]*^\\)?"
"[\s\t]*$")
"Regular expression for matching character names.
Group 1: match leading @ for forced character
Group 2: match character name
Group 3: match parenthetical extension
Group 4: match opening parenthetical
Group 5: match trailing ^ for dual dialog
Requires `fountain-match-character' for preceding blank line.")
(defconst fountain-dialog-regexp
"^\\(\s\s\\)$\\|^[\s\t]*\\([^<>\n]+?\\)[\s\t]*$"
"Regular expression for matching dialogue.
Group 1: match trimmed whitespace
Requires `fountain-match-dialog' for preceding character,
parenthetical or dialogue.")
(defconst fountain-paren-regexp
"^[\s\t]*([^)\n]*)[\s\t]*$"
"Regular expression for matching parentheticals.
Requires `fountain-match-paren' for preceding character or
dialogue.")
(defconst fountain-page-break-regexp
"^[\s\t]*\\(=\\{3,\\}\\)[\s\t]*\\([a-z0-9\\.-]+\\)?.*$"
"Regular expression for matching page breaks.
Group 1: leading ===
Group 2: forced page number")
(defconst fountain-note-regexp
"\\[\\[[\s\t]*\\(\\(?:.\\|\n\\)*?\\)[\s\t]*]]"
"Regular expression for matching notes.
Group 1: note text")
(defconst fountain-section-heading-regexp
"^\\(?1:\\(?2:#\\{1,5\\}\\)[\s\t]*\\)\\(?3:[^#\n].*?\\)[\s\t]*$"
"Regular expression for matching section headings.
Group 1: match leading #'s and following whitespace
Group 2: match leading #'s
Group 3: match heading text")
(defconst fountain-synopsis-regexp
"^\\(\\(=\\)[\s\t]*\\)\\([^=\n].*?\\)$"
"Regular expression for matching synopses.
Group 1: leading = and following whitespace
Group 2: leading =
Group 3: synopsis text")
(defconst fountain-center-regexp
"^[\s\t]*\\(>\\)[\s\t]*\\(.+?\\)[\s\t]*\\(<\\)[\s\t]*$"
"Regular expression for matching centered text.
Group 1: match leading >
Group 2: match center text
Group 3: match trailing <")
(defconst fountain-underline-regexp
"\\(?:^\\|[^\\]\\)\\(\\(_\\)\\([^\n\s\t_][^_\n]*?\\)\\(\\2\\)\\)"
"Regular expression for matching underlined text.")
(defconst fountain-italic-regexp
"\\(?:^\\|[^\\*\\]\\)\\(\\(\\*\\)\\([^\n\s\t\\*][^\\*\n]*?\\)\\(\\2\\)\\)"
"Regular expression for matching italic text.")
(defconst fountain-bold-regexp
"\\(?:^\\|[^\\]\\)\\(\\(\\*\\*\\)\\([^\n\s\t\\*][^\\*\n]*?\\)\\(\\2\\)\\)"
"Regular expression for matching bold text.")
(defconst fountain-bold-italic-regexp
"\\(?:^\\|[^\\]\\)\\(\\(\\*\\*\\*\\)\\([^\n\s\t\\*][^\\*\n]*?\\)\\(\\2\\)\\)"
"Regular expression for matching bold-italic text.
Due to the nature of the syntax, bold-italic-underlined text must
be specified with the bold-italic delimiters together, e.g.
This text is _***ridiculously important***_.
This text is ***_stupendously significant_***.")
(defconst fountain-lyrics-regexp
"^\\(~[\s\t]*\\)\\(.+\\)"
"Regular expression for matching lyrics.")
;;; Aligning
(defgroup fountain-align ()
"Options for element alignment.
Each Fountain element align option can be an integer representing
the align column for all formats, or a list where each element
takes the form:
(FORMAT . INT)
Where FORMAT is a script format string and INT is the align
column for that format.
To disable element alignment, see `fountain-align-elements'."
:prefix "fountain-align-"
:group 'fountain)
(defcustom fountain-align-elements
t
"If non-nil, elements will be displayed auto-aligned.
This option does not affect file contents."
:group 'fountain-align
:type 'boolean
:safe 'booleanp
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-section-heading
'(("screenplay" . 0)
("stageplay" . 30))
"Column integer to which section headings should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-scene-heading
'(("screenplay" . 0)
("stageplay" . 30))
"Column integer to which scene headings should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-synopsis
'(("screenplay" . 0)
("stageplay" . 30))
"Column integer to which synopses should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-action
'(("screenplay" . 0)
("stageplay" . 20))
"Column integer to which action should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-character
'(("screenplay" . 20)
("stageplay" . 30))
"Column integer to which characters names should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-dialog
'(("screenplay" . 10)
("stageplay" . 0))
"Column integer to which dialog should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-paren
'(("screenplay" . 15)
("stageplay" . 20))
"Column integer to which parentheticals should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-trans
'(("screenplay" . 45)
("stageplay" . 30))
"Column integer to which transitions should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
(defcustom fountain-align-center
'(("screenplay" . 20)
("stageplay" . 20))
"Column integer to which centered text should be aligned.
This option does not affect file contents."
:group 'fountain-align
:type '(choice (integer :tag "Column")
(repeat (cons (string :tag "Format")
(integer :tag "Column"))))
:set #'fountain--set-and-refresh-font-lock)
;;; Autoinsert
(require 'autoinsert)
(defvar fountain-metadata-skeleton
'(nil
"title: " (skeleton-read "Title: " (file-name-base (buffer-name))) | -7 "\n"
"credit: " (skeleton-read "Credit: " "written by") | -9 "\n"
"author: " (skeleton-read "Author: " user-full-name) | -9 "\n"
"format: " (skeleton-read "Script format: " fountain-default-script-format) | -9 "\n"
"source: " (skeleton-read "Source: ") | -9 "\n"
"date: " (skeleton-read "Date: " (format-time-string "%x")) | -7 "\n"
"contact:\n" ("Contact details, %s: " " " str | -4 "\n") | -9))
(define-auto-insert '(fountain-mode . "Fountain metadata skeleton")
fountain-metadata-skeleton)
;;; Initializing
(defun fountain-init-scene-heading-regexp ()
"Initialize scene heading regular expression.
Uses `fountain-scene-heading-prefix-list' to create non-forced
scene heading regular expression."
(setq fountain-scene-heading-regexp
(concat
"^\\(?:"
;; Group 1: match leading . (for forced scene heading)
"\\(?1:\\.\\)"
;; Group 2: match scene heading without scene number
"\\(?2:\\<"
;; Group 4: match location
"\\(?4:.+?\\)"
;; Group 5: match suffix separator
"\\(?:\\(?5:" fountain-scene-heading-suffix-separator "\\)"
;; Group 6: match suffix
"\\(?6:.+?\\)?\\)?"
"\\)\\|"
;; Group 2: match scene heading without scene number
"^\\(?2:"
;; Group 3: match INT/EXT
"\\(?3:" (regexp-opt fountain-scene-heading-prefix-list) "\\.?\s+\\)"
;; Group 4: match location
"\\(?4:.+?\\)?"
;; Group 5: match suffix separator
"\\(?:\\(?5:" fountain-scene-heading-suffix-separator "\\)"
;; Group 6: match suffix
"\\(?6:.+?\\)?\\)?"
"\\)\\)"
;;; Match scene number
"\\(?:"
;; Group 7: match space between scene heading and scene number
"\\(?7:\s+\\)"
;; Group 8: match first # delimiter
"\\(?8:#\\)"
;; Group 9: match scene number
"\\(?9:[0-9a-z\\.-]+\\)"
;; Group 10: match last # delimiter
"\\(?10:#\\)\\)?"
"\s*$")))
(defun fountain-init-trans-regexp ()
"Initialize transition regular expression.
Uses `fountain-trans-suffix-list' to create non-forced tranistion
regular expression."
(setq fountain-trans-regexp
(concat
"^\\(?:[\s\t]*"
;; Group 1: match forced transition mark
"\\(>\\)[\s\t]*"
;; Group 2: match forced transition
"\\([^<>\n]*?\\)"
"\\|"
;; Group 2: match transition
"\\(?2:[[:upper:]\s\t]*"
(upcase (regexp-opt fountain-trans-suffix-list))
"\\)"
"\\)[\s\t]*$")))
(defun fountain-init-outline-regexp ()
"Initialize `outline-regexp'."
(setq-local outline-regexp
(concat fountain-section-heading-regexp
"\\|"
fountain-scene-heading-regexp)))
(require 'imenu)
(defcustom fountain-imenu-elements
'(section-heading scene-heading synopsis note)
"List of elements to include in `imenu'."
:type '(set (const :tag "Section Headings" section-heading)
(const :tag "Scene Headings" scene-heading)
(const :tag "Synopses" synopsis)
(const :tag "Notes" note))
:set (lambda (symbol value)
(set-default symbol value)
(mapc (lambda (buffer)
(with-current-buffer buffer
(when (derived-mode-p 'fountain-mode)
(fountain-init-imenu))))
(buffer-list))))
(defun fountain-init-imenu ()
"Initialize `imenu-generic-expression'."
(setq imenu-generic-expression nil)
(when (memq 'section-heading fountain-imenu-elements)
(push (list "Section Headings" fountain-section-heading-regexp 3)
imenu-generic-expression))
(when (memq 'scene-heading fountain-imenu-elements)
(push (list "Scene Headings" fountain-scene-heading-regexp 2)
imenu-generic-expression))
(when (memq 'synopsis fountain-imenu-elements)
(push (list "Synopses" fountain-synopsis-regexp 3)
imenu-generic-expression))
(when (memq 'note fountain-imenu-elements)
(push (list "Notes" fountain-note-regexp 1)
imenu-generic-expression))
(when (featurep 'imenu) (imenu-update-menubar)))
(require 'elec-pair)
(defun fountain-electric-pair-skip-self (char)
"Return non-nil if syntax before that of CHAR is word syntax."
(and electric-pair-preserve-balance
(save-excursion
(skip-syntax-backward (char-to-string (char-syntax char))
(line-beginning-position))
(= (char-syntax (char-before)) ?w))))
(defun fountain-init-vars ()
"Initialize important variables.
Needs to be called for every Fountain buffer because some
variatbles are required for functions to operate with temporary
buffers."
(fountain-init-scene-heading-regexp)
(fountain-init-trans-regexp)
(fountain-init-outline-regexp)
(fountain-init-imenu)
(modify-syntax-entry (string-to-char "/") ". 14" nil)
(modify-syntax-entry (string-to-char "*") "$ 23" nil)
(modify-syntax-entry (string-to-char "_") "$" nil)
(setq-local comment-start "/*")
(setq-local comment-end "*/")
(setq-local comment-use-syntax t)
(setq-local electric-pair-skip-self #'fountain-electric-pair-skip-self)
(setq-local font-lock-comment-face 'fountain-comment)
(setq-local page-delimiter fountain-page-break-regexp)
(setq-local outline-level #'fountain-outline-level)
(setq-local require-final-newline mode-require-final-newline)
(setq-local completion-cycle-threshold t)
(setq-local completion-at-point-functions
'(fountain-completion-at-point))
(setq-local font-lock-extra-managed-props
'(line-prefix wrap-prefix invisible))
;; This should be temporary. Feels better to ensure appropriate
;; case-fold within each function.
(setq case-fold-search t)
(setq imenu-case-fold-search nil)
(setq font-lock-multiline 'undecided)
(setq font-lock-defaults '(fountain-init-font-lock))
(add-to-invisibility-spec (cons 'outline t))
(when fountain-hide-emphasis-markup
(add-to-invisibility-spec 'fountain-emphasis-delim))
(when fountain-hide-element-markup
(add-to-invisibility-spec 'fountain-syntax-chars)))
;;; Element Matching
(defun fountain-blank-before-p ()
"Return non-nil if preceding line is blank or a comment."
(save-excursion
(save-restriction
(widen)
(beginning-of-line)
(or (bobp)
(progn (forward-line -1)
(or (and (bolp) (eolp))
(progn (end-of-line)
(forward-comment -1))))))))
(defun fountain-blank-after-p ()
"Return non-nil if following line is blank or a comment."
(save-excursion
(save-restriction
(widen)
(forward-line)
(or (eobp)
(and (bolp) (eolp))
(forward-comment 1)))))
(defun fountain-maybe-in-dialog-p ()
"Return non-nil if point may be in dialogue."
(save-excursion
(or (fountain-match-dialog)
(fountain-match-paren)
(and (save-restriction
(widen)
(forward-line -1)
(or (fountain-match-character 'loose)
(fountain-match-dialog)))))))
(defun fountain-match-metadata ()
"Match metadata if point is at metadata, nil otherwise."
(save-excursion
(beginning-of-line)
(and (looking-at fountain-metadata-regexp)
(save-match-data
(save-restriction
(widen)
(or (bobp)
(and (forward-line -1)
(fountain-match-metadata))))))))
(defun fountain-match-page-break ()
"Match page break if point is at page break, nil otherwise."
(save-excursion
(beginning-of-line)
(looking-at fountain-page-break-regexp)))
(defun fountain-match-section-heading ()
"Match section heading if point is at section heading, nil otherwise."