For .page and .pandoc files, try to scrape the metadata to figure out which mode to use
Getty Ritter
8 years ago
10 | 10 | (setq indent-tabs-mode nil) |
11 | 11 | |
12 | 12 | (setq scheme-program-name "guile") |
13 | (setq vc-follow-symlinks t) | |
13 | 14 | |
14 | 15 | (if (and (display-graphic-p) |
15 | 16 | (not (getenv "BIG"))) |
162 | 163 | :init |
163 | 164 | (progn |
164 | 165 | (add-to-list 'auto-mode-alist '("\\.text\\'" . markdown-mode)) |
165 | (add-to-list 'auto-mode-alist '("\\.page\\'" . markdown-mode)) | |
166 | 166 | (add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode)) |
167 | 167 | (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode)) |
168 | 168 | (add-hook 'markdown-mode-hook 'visual-line-mode))) |
173 | 173 | |
174 | 174 | (use-package fountain-mode |
175 | 175 | :ensure t) |
176 | ||
177 | ;; Pandoc can begin with YAML metadata, and sometimes a pandoc file | |
178 | ;; will indicate which format it should be using. I'm not going to | |
179 | ;; write a full YAML parser in elisp! But usually, parsing simple | |
180 | ;; key-value pairs should be sufficient. | |
181 | (defun gdritter/pandoc-parse-basic-meta-block () | |
182 | (let ((here (point))) | |
183 | (goto-char 0) | |
184 | (let ((rs | |
185 | (if (re-search-forward "^---\n" nil t) | |
186 | (let ((block-start (point))) | |
187 | (if (re-search-forward "^\\(---\\|...\\)\n" nil t) | |
188 | (progn (goto-char block-start) | |
189 | (gdritter/pandoc-parse-kv-section)) | |
190 | (progn (message "no metadata block found") | |
191 | nil)))))) | |
192 | (goto-char here) | |
193 | rs))) | |
194 | ||
195 | ;; This tries to parse all the simple key: value forms it can | |
196 | (defun gdritter/pandoc-parse-kv-section () | |
197 | (if (looking-at "^\\(---\\|...\\)\n") | |
198 | '() | |
199 | (let ((rs (re-search-forward "^\\([A-Za-z0-9_]+\\):[ ]*\\(.*\\)\n" nil t))) | |
200 | (if rs | |
201 | (cons (cons (match-string 1) | |
202 | (match-string 2)) | |
203 | (gdritter/pandoc-parse-kv-section)) | |
204 | (forward-line 1))))) | |
205 | ||
206 | ;; This will try to dispatch which mode to open based on the | |
207 | ;; metadata block. If the metadata block doesn't contain a format | |
208 | ;; key, then it'll default to Markdown with Pandoc extensions. | |
209 | (defun gdritter/pandoc-choose-mode () | |
210 | (interactive) | |
211 | (let* ((meta (gdritter/pandoc-parse-basic-meta-block)) | |
212 | (format (assoc-string "format" meta)) | |
213 | (fmt (if format (cdr format) nil))) | |
214 | (cond ((equal fmt "org") (org-mode)) | |
215 | ((equal fmt "latex") (latex-mode)) | |
216 | ((equal fmt "rst") (rst-mode)) | |
217 | ((equal fmt "html") (web-mode)) | |
218 | (t (markdown-mode))))) | |
219 | ||
220 | (add-to-list 'auto-mode-alist '("\\.page\\'" . gdritter/pandoc-choose-mode)) | |
221 | (add-to-list 'auto-mode-alist '("\\.pandoc\\'" . gdritter/pandoc-choose-mode)) | |
176 | 222 | |
177 | 223 | |
178 | 224 |