Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 124 lines (99 sloc) 4.531 kb
2b23eea @hvr Unify Elisp boilerplate headers
hvr authored
1 ;;; haskell-navigate-imports.el --- A function for cycling through Haskell import lists
a9e9e95 @chrisdone Added import jumping/navigation.
chrisdone authored
2
2b23eea @hvr Unify Elisp boilerplate headers
hvr authored
3 ;; Copyright (C) 2010 Chris Done
a9e9e95 @chrisdone Added import jumping/navigation.
chrisdone authored
4
2b23eea @hvr Unify Elisp boilerplate headers
hvr authored
5 ;; Author: Chris Done <chrisdone@gmail.com>
a9e9e95 @chrisdone Added import jumping/navigation.
chrisdone authored
6
2b23eea @hvr Unify Elisp boilerplate headers
hvr authored
7 ;; This file is not part of GNU Emacs.
a9e9e95 @chrisdone Added import jumping/navigation.
chrisdone authored
8
9 ;; This program is free software: you can redistribute it and/or
10 ;; modify it under the terms of the GNU General Public License as
11 ;; published by the Free Software Foundation, either version 3 of
12 ;; the License, or (at your option) any later version.
13
14 ;; This program is distributed in the hope that it will be
15 ;; useful, but WITHOUT ANY WARRANTY; without even the implied
16 ;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
17 ;; PURPOSE. See the GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public
20 ;; License along with this program. If not, see
21 ;; <http://www.gnu.org/licenses/>.
22
2b23eea @hvr Unify Elisp boilerplate headers
hvr authored
23 ;;; Commentary:
24
25 ;; The cycling step will stop once at the last import list so
26 ;; that it is easy to add a new import list.
27
28 ;; This module works completely independently of any libraries
29 ;; (including haskell-mode).
30
31 ;; Exports three interactive functions:
32 ;; 1. haskell-navigate-imports
33 ;; 2. haskell-navigate-imports-go
34 ;; 3. haskell-navigate-imports-return
35
36 ;; Example usage:
37
38 ;; (require 'haskell-navigate-imports)
39 ;; (define-key haskell-mode-map [f8] 'haskell-navigate-imports)
40
41 ;;; Code:
42
a9e9e95 @chrisdone Added import jumping/navigation.
chrisdone authored
43 (defvar haskell-navigate-imports-start-point nil)
44
45 ;;;###autoload
46 (defun haskell-navigate-imports (&optional return)
47 "Cycle the Haskell import lines or return to point (with prefix arg)."
48 (interactive "P")
49 (if return
50 (haskell-navigate-imports-return)
51 (haskell-navigate-imports-go)))
52
53 ;;;###autoload
54 (defun haskell-navigate-imports-go ()
55 "Go to the first line of a list of consequtive import lines. Cycles."
56 (interactive)
57 (unless (or (haskell-navigate-imports-line)
58 (equal (line-beginning-position) (point-min))
385f299 @akamch Replace functions used only interactively
akamch authored
59 (save-excursion (forward-line -1)
a9e9e95 @chrisdone Added import jumping/navigation.
chrisdone authored
60 (haskell-navigate-imports-line)))
61 (setq haskell-navigate-imports-start-point (point)))
62 (haskell-navigate-imports-go-internal))
63
64 ;;;###autoload
65 (defun haskell-navigate-imports-return ()
66 "Return to the non-import point we were at before going to the module list.
67 If we were originally at an import list, we can just cycle through easily."
68 (interactive)
69 (when haskell-navigate-imports-start-point
70 (goto-char haskell-navigate-imports-start-point)))
71
72 (defun haskell-navigate-imports-go-internal ()
73 "Go to the first line of a list of consequtive import lines. Cycle."
74 (if (haskell-navigate-imports-line)
75 (progn (haskell-navigate-imports-goto-end)
76 (when (haskell-navigate-imports-find-forward-line)
77 (haskell-navigate-imports-go-internal)))
78 (let ((point (haskell-navigate-imports-find-forward-line)))
79 (if point
80 (goto-char point)
81 (progn (goto-char (point-min))
38e8c06 @chrisdone Make navigate-imports a bit more useful in absense of import list
chrisdone authored
82 (if (haskell-navigate-imports-find-forward-line)
83 (haskell-navigate-imports-go-internal)
84 (when (search-forward-regexp "^module" nil t 1)
85 (search-forward "\n\n" nil t 1))))))))
a9e9e95 @chrisdone Added import jumping/navigation.
chrisdone authored
86
87 (defun haskell-navigate-imports-goto-end ()
88 "Skip a bunch of consequtive import lines."
89 (while (not (or (equal (point)
90 (point-max))
91 (not (haskell-navigate-imports-line))))
92 (forward-line)))
93
94 (defun haskell-navigate-imports-find-forward-line ()
95 "Return a point with at an import line, or nothing."
96 (save-excursion
97 (while (not (or (equal (point) (point-max))
98 (haskell-navigate-imports-after-imports-p) ;; This one just speeds it up.
99 (haskell-navigate-imports-line)))
100 (forward-line))
101 (let ((point (point)))
102 (if (haskell-navigate-imports-line)
103 (point)
104 nil))))
105
106 (defun haskell-navigate-imports-line ()
107 "Try to match the current line as a regexp."
108 (let ((line (buffer-substring-no-properties (line-beginning-position)
109 (line-end-position))))
110 (if (string-match "^import " line)
111 line
112 nil)))
113
114 (defun haskell-navigate-imports-after-imports-p ()
115 "Are we after the imports list? Just for a speed boost."
116 (save-excursion
117 (goto-char (line-beginning-position))
118 (not (not (search-forward-regexp "\\( = \\|\\<instance\\>\\| :: \\)"
119 (line-end-position) t 1)))))
120
121 (provide 'haskell-navigate-imports)
2b23eea @hvr Unify Elisp boilerplate headers
hvr authored
122
123 ;;; haskell-navigate-imports.el ends here
Something went wrong with that request. Please try again.