Skip to content

Commit

Permalink
Fall back to general fontification of source blocks if lang attr is n… (
Browse files Browse the repository at this point in the history
#55)

* Fall back to general fontification of source blocks if lang attr is not given

* Add test case for source blocks without language attribute

Restructure adoc-fontify-code-blocks and adoc-fontify-code-block-natively
such that things that are needed for blocks with and without language attribute
are done in adoc-fontify-code-blocks.

---------

Co-authored-by: Tobias Zawada <TOZ@esi-group.com>
  • Loading branch information
TobiasZawada and Tobias Zawada authored Feb 18, 2024
1 parent 524e3ae commit 2c2eb80
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
40 changes: 19 additions & 21 deletions adoc-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -2066,12 +2066,6 @@ START-SRC and END-SRC delimit the actual source code."
(put-text-property
(+ start-src (1- pos)) (1- (+ start-src next)) 'face
val adoc-buffer))))
(add-text-properties start-block start-src '(face adoc-meta-face))
(add-text-properties end-src end-block '(face adoc-meta-face))
(add-text-properties
start-block end-block
'(font-lock-fontified t fontified t font-lock-multiline t
adoc-code-block t adoc-reserved t))
(set-buffer-modified-p modified)))))

(defconst adoc-code-block-begin-regexp
Expand Down Expand Up @@ -2109,9 +2103,10 @@ Group 2 contains the block delimiter.")
"Search for next adoc-code block up to LAST.
NOERROR is the same as for `search-forward'.
Return the source block language and
Return a string with the source block language and
set match data if a source block is found.
Otherwise return nil.
If the source block is given without the language attribute return t.
If no source block is found return nil.
The overall match data begins at the
header of the code block and ends at the end of the
Expand Down Expand Up @@ -2167,19 +2162,22 @@ Use this function as matching function MATCHER in `font-lock-keywords'."
(end-src (match-end 1))
(end-src+nl (if (eq (char-after end-src) ?\n) (1+ end-src) end-src))
(size (1+ (- end-src start-src))))
(if (if (numberp adoc-fontify-code-blocks-natively)
(<= size adoc-fontify-code-blocks-natively)
adoc-fontify-code-blocks-natively)
(adoc-fontify-code-block-natively lang start-block end-block start-src end-src)
(add-text-properties
start-src
end-src
'(font-lock-face adoc-verbatim-face)))
;; Set background for block as well as opening and closing lines.
(font-lock-append-text-property
start-src end-src+nl 'face 'adoc-native-code-face)
(add-text-properties
start-src end-src+nl '(font-lock-fontified t font-lock-multiline t adoc-code-block t))
(if (and
(stringp lang)
(if (numberp adoc-fontify-code-blocks-natively)
(<= size adoc-fontify-code-blocks-natively)
adoc-fontify-code-blocks-natively))
(progn
(adoc-fontify-code-block-natively lang start-block end-block start-src end-src)
(font-lock-append-text-property start-src end-src 'face 'adoc-native-code-face)
(add-text-properties end-src end-src+nl '(face adoc-native-code-face)))
;; code block without language attribute or too large
(add-text-properties start-src end-src '(face (adoc-verbatim-face adoc-code-face)))
(add-text-properties end-src end-src+nl '(face adoc-code-face)))
(add-text-properties start-block start-src '(face adoc-meta-face))
(put-text-property end-src+nl end-block 'face adoc-meta-face)
(add-text-properties start-src end-src+nl '(adoc-code-block t))
(add-text-properties start-block end-block '(font-lock-fontified t font-lock-multiline t adoc-reserved t))
)))
t)))

Expand Down
14 changes: 10 additions & 4 deletions test/adoc-mode-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -349,26 +349,32 @@ Don't use it for anything real.")
"\n" nil
"[source,adoctest-lang]\n----\n" 'adoc-meta-face
source-code
"\n" '(adoc-meta-face adoc-native-code-face)
"\n" 'adoc-native-code-face
"----" 'adoc-meta-face
"\n" nil
;; Code blocks without language attribute
"[source]\n----\n" 'adoc-meta-face
(apply #'concat (cl-loop for str in source-code by #'cddr collect str)) '(adoc-verbatim-face adoc-code-face)
"\n" 'adoc-code-face
"----" 'adoc-meta-face
"\n" nil
;; Code block as OPEN BLOCK
"\n" nil
"[source,adoctest-lang]\n--\n" 'adoc-meta-face
source-code
"\n" '(adoc-meta-face adoc-native-code-face)
"\n" 'adoc-native-code-face
"--" 'adoc-meta-face
"\n" nil
;; Code block as Literal block
"[source,adoctest-lang]\n....\n" 'adoc-meta-face
source-code
"\n" '(adoc-meta-face adoc-native-code-face)
"\n" 'adoc-native-code-face
"...." 'adoc-meta-face
"\n" nil
;; Test ignored spaces
"[source,\t adoctest-lang]\t \n....\n" 'adoc-meta-face
source-code
"\n" '(adoc-meta-face adoc-native-code-face)
"\n" 'adoc-native-code-face
"...." 'adoc-meta-face
"\n" nil
))))
Expand Down

0 comments on commit 2c2eb80

Please sign in to comment.