-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathvhdl-ext-hs.el
74 lines (58 loc) · 2.68 KB
/
vhdl-ext-hs.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
;;; vhdl-ext-hs.el --- Vhdl-ext Hideshow -*- lexical-binding: t -*-
;; Copyright (C) 2022-2025 Gonzalo Larumbe
;; Author: Gonzalo Larumbe <gonzalomlarumbe@gmail.com>
;; URL: https://github.com/gmlarumbe/vhdl-ext
;; 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:
;; `hideshow' configuration for code folding:
;; - Add support for code folding of if/else blocks
;;; Code:
(require 'vhdl-mode)
(require 'hideshow)
(defconst vhdl-ext-hs-start-regexp
(concat "\\(\\_<\\(then\\|else\\)\\_>\\|" vhdl-hs-start-regexp "\\)"))
(defun vhdl-ext-hs-forward-sexp-func (count)
"Wrapper for `vhdl-hs-forward-sexp-func' adding hideshow for if/else blocks.
Move forward COUNT s-expressions."
(cond ((looking-at "\\_<then\\_>") ; if/elsif
(vhdl-forward-sexp)
(cond ((looking-at "\\s-*;") ; if w/o else/elsif
(goto-char (match-end 0)))
((looking-at "\\_<else\\_>") ; if with else
(point))
((save-excursion
(backward-word)
(looking-at "\\_<elsif\\_>")) ; if with elsif
(backward-word)
(point))))
((looking-at "\\_<else\\_>") ; else
(vhdl-forward-sexp)
(vhdl-re-search-forward "if\\s-*;" nil t))
(t
(vhdl-hs-forward-sexp-func count))))
(defun vhdl-ext-hs-setup ()
"Configure `hideshow'."
(dolist (mode '((vhdl-mode . vhdl-ext-hs-forward-sexp-func)
(vhdl-ts-mode . vhdl-ts-forward-sexp)))
(add-to-list 'hs-special-modes-alist `(,(car mode)
,vhdl-ext-hs-start-regexp
nil
"--\\( \\|$\\)"
,(cdr mode))))
(dolist (hook '(vhdl-mode-hook vhdl-ts-mode-hook))
(add-hook hook #'hs-minor-mode))
;; Workaround to enable `hideshow' on first file visit with lazy loading using
;; :config section with `use-package'
(when (member major-mode '(vhdl-mode vhdl-ts-mode))
(hs-minor-mode 1)))
(provide 'vhdl-ext-hs)
;;; vhdl-ext-hs.el ends here