| 1 |
;; easydoctex-mode.el 0.2.0 |
| 2 |
;; |
| 3 |
;; This file is a part of TeX package EasyDTX, available at |
| 4 |
;; https://ctan.org/pkg/easydtx and https://github.com/sasozivanovic/easydtx. |
| 5 |
;; |
| 6 |
;; Copyright (c) 2023- Saso Zivanovic <[email protected]> |
| 7 |
;; |
| 8 |
;; This program is free software: you can redistribute it and/or modify |
| 9 |
;; it under the terms of the GNU General Public License as published by |
| 10 |
;; the Free Software Foundation, either version 3 of the License, or |
| 11 |
;; (at your option) any later version. |
| 12 |
;; |
| 13 |
;; This program is distributed in the hope that it will be useful, |
| 14 |
;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
;; GNU General Public License for more details. |
| 17 |
;; |
| 18 |
;; You should have received a copy of the GNU General Public License |
| 19 |
;; along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 20 |
;; |
| 21 |
;; The files belonging to this work and covered by GPL are listed in |
| 22 |
;; <texmf>/doc/support/easydtx/FILES. |
| 23 |
|
| 24 |
;; Add this to .emacs to (auto)load EasyDocTeX mode and associate it with .edtx files: |
| 25 |
;; (autoload 'easydoctex-mode "easydoctex-mode" "Add DocTeX fontification (and only fontification) to LaTeX mode." t) |
| 26 |
;; (add-to-list 'auto-mode-alist '("\\.edtx\\'" . easydoctex-mode)) |
| 27 |
|
| 28 |
(define-derived-mode easydoctex-mode TeX-latex-mode "EasyDocTeX" |
| 29 |
"Add DocTeX fontification (and only fontification) to LaTeX mode." |
| 30 |
(setq major-mode 'easydoctex-mode) |
| 31 |
(set (make-local-variable 'LaTeX-insert-into-comments) t) |
| 32 |
(set (make-local-variable 'LaTeX-syntactic-comments) t) |
| 33 |
(setq paragraph-start (concat paragraph-start "\\| *%<") |
| 34 |
paragraph-separate (concat paragraph-separate "\\|%<") |
| 35 |
font-latex-doctex-keywords (append (butlast font-latex-doctex-keywords) '(("^ *%<[^>]*>" (0 font-latex-doctex-preprocessor-face t)))) |
| 36 |
) |
| 37 |
(let ((major-mode 'doctex-mode)) |
| 38 |
(funcall TeX-install-font-lock))) |
| 39 |
|
| 40 |
(defun easydoctex-wrapper (ORIG-FUNC &rest args) |
| 41 |
(if (eq major-mode 'easydoctex-mode) |
| 42 |
(let ((major-mode 'doctex-mode)) |
| 43 |
(apply ORIG-FUNC args)) |
| 44 |
(apply ORIG-FUNC args))) |
| 45 |
|
| 46 |
(advice-add 'font-latex-find-matching-close :around #'easydoctex-wrapper) |
| 47 |
(advice-add 'font-latex-commented-outp :around #'easydoctex-wrapper) |
| 48 |
(advice-add 'font-latex-forward-comment :around #'easydoctex-wrapper) |
| 49 |
(advice-add 'font-latex-match-simple-command :around #'easydoctex-wrapper) |
| 50 |
|
| 51 |
(defun easydoctex-no-comment-padding (ORIG-FUNC &rest args) |
| 52 |
(if (and |
| 53 |
(eq major-mode 'easydoctex-mode) |
| 54 |
(save-excursion |
| 55 |
(beginning-of-line) |
| 56 |
(looking-at " *%<") |
| 57 |
)) |
| 58 |
"" |
| 59 |
(apply ORIG-FUNC args))) |
| 60 |
(advice-add 'TeX-comment-padding-string :around #'easydoctex-no-comment-padding) |
| 61 |
|
| 62 |
(defun easydoctex-guard-is-code (ORIG-FUNC &rest args) |
| 63 |
(if (and |
| 64 |
(eq major-mode 'easydoctex-mode) |
| 65 |
(save-excursion |
| 66 |
(beginning-of-line) |
| 67 |
(looking-at " *%<"))) |
| 68 |
nil |
| 69 |
(apply ORIG-FUNC args))) |
| 70 |
(advice-add 'TeX-in-commented-line :around #'easydoctex-guard-is-code) |
| 71 |
|
| 72 |
(defun font-latex-easydoctex-syntactic-face-function (ORIG-FUNC state &rest args) |
| 73 |
"In EasyDTC (.edtx), % can start after any blanks, but it should not be followed by another % or a guard." |
| 74 |
(if (eq major-mode 'easydoctex-mode) |
| 75 |
(if (or |
| 76 |
(nth 3 state) |
| 77 |
(nth 7 state) |
| 78 |
(save-excursion |
| 79 |
(goto-char (nth 8 state)) |
| 80 |
(skip-chars-backward " \t") |
| 81 |
(not (bolp))) |
| 82 |
(char-equal (char-after (1+ (nth 8 state))) ?%)) |
| 83 |
(apply ORIG-FUNC state args) |
| 84 |
font-latex-doctex-documentation-face) |
| 85 |
(apply ORIG-FUNC state args))) |
| 86 |
(advice-add |
| 87 |
'font-latex-doctex-syntactic-face-function :around |
| 88 |
#'font-latex-easydoctex-syntactic-face-function) |
| 89 |
|
| 90 |
(defun LaTeX-current-environment-no-macrocode (env) |
| 91 |
"The only (and obligatory) macrocode environment does not count as an environment." |
| 92 |
(if (member env '("macrocode" "macrocode*")) "document" env)) |
| 93 |
(advice-add 'LaTeX-current-environment :filter-return #'LaTeX-current-environment-no-macrocode) |
| 94 |
;; (advice-remove 'LaTeX-current-environment#'LaTeX-current-environment-no-macrocode) |
| 95 |
|
| 96 |
(defun server-visit-files-forward-to-edtx (args) |
| 97 |
"When inverse search points to a .dtx generated from .edtx, visit |
| 98 |
the original .edtx file instead." |
| 99 |
(dolist (file-pos (nth 0 args)) |
| 100 |
(let* ((file (car file-pos)) |
| 101 |
(pos (cdr file-pos)) |
| 102 |
(edtx (concat (file-name-sans-extension file) ".edtx")) |
| 103 |
) |
| 104 |
(when (and pos |
| 105 |
(string-match "dtx" (file-name-extension file)) |
| 106 |
(file-readable-p edtx)) |
| 107 |
(setcar file-pos edtx) |
| 108 |
(let* ((line (car pos)) |
| 109 |
(col (cdr pos)) |
| 110 |
(dtx-buffer (find-file-noselect file nil t)) |
| 111 |
(line-offset |
| 112 |
(with-current-buffer dtx-buffer |
| 113 |
(widen) |
| 114 |
(goto-char 1) |
| 115 |
(forward-line (1- line)) |
| 116 |
(move-to-column col) |
| 117 |
(count-matches "^% \\\\\\(begin\\|end\\){macrocode}" 0 (point))))) |
| 118 |
(when (> line-offset 1) |
| 119 |
(set 'line-offset (1- line-offset))) |
| 120 |
(setcdr file-pos (cons (- line line-offset) col)) |
| 121 |
)))) |
| 122 |
args) |
| 123 |
(advice-add 'server-visit-files :filter-args #'server-visit-files-forward-to-edtx) |
| 124 |
;; (advice-remove 'server-visit-files #'server-visit-files-forward-to-edtx) |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
;; Forward search for .edtx |
| 129 |
(defun TeX-current-file-name-master-relative--edtx (r) |
| 130 |
(if (equal (file-name-extension r) "edtx") |
| 131 |
(concat (file-name-sans-extension r) ".dtx") |
| 132 |
r)) |
| 133 |
(advice-add 'TeX-current-file-name-master-relative :filter-return |
| 134 |
#'TeX-current-file-name-master-relative--edtx) |
| 135 |
(defun TeX-current-line--edtx (line) |
| 136 |
(when (equal (file-name-extension (buffer-file-name)) "edtx") |
| 137 |
(let ((remaining (string-to-number line)) |
| 138 |
(dtx-buffer (find-file-noselect (concat (file-name-sans-extension (buffer-file-name)) ".dtx") nil t))) |
| 139 |
(with-current-buffer dtx-buffer |
| 140 |
(widen) |
| 141 |
(goto-char 1) |
| 142 |
(setq line 0) |
| 143 |
(while (> (setq remaining (1- remaining)) 0) |
| 144 |
(while (progn (forward-line 1) |
| 145 |
(setq line (1+ line)) |
| 146 |
(looking-at "% \\\\\\(begin\\|end\\){macrocode}"))))) |
| 147 |
(setq line (format "%d" line)))) |
| 148 |
line) |
| 149 |
(advice-add 'TeX-current-line :filter-return #'TeX-current-line--edtx) |