gdritter repos dotfiles / ccd8911
For .page and .pandoc files, try to scrape the metadata to figure out which mode to use Getty Ritter 7 years ago
1 changed file(s) with 47 addition(s) and 1 deletion(s). Collapse all Expand all
1010 (setq indent-tabs-mode nil)
1111
1212 (setq scheme-program-name "guile")
13 (setq vc-follow-symlinks t)
1314
1415 (if (and (display-graphic-p)
1516 (not (getenv "BIG")))
162163 :init
163164 (progn
164165 (add-to-list 'auto-mode-alist '("\\.text\\'" . markdown-mode))
165 (add-to-list 'auto-mode-alist '("\\.page\\'" . markdown-mode))
166166 (add-to-list 'auto-mode-alist '("\\.markdown\\'" . markdown-mode))
167167 (add-to-list 'auto-mode-alist '("\\.md\\'" . markdown-mode))
168168 (add-hook 'markdown-mode-hook 'visual-line-mode)))
173173
174174 (use-package fountain-mode
175175 :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))
176222
177223
178224