about summary refs log tree commit diff
path: root/configs/shared/emacs/.emacs.d/elpa/magit-20180719.1904/magit-imenu.el
blob: 6434f9f3c826e6786321e87e9ae525ace303e114 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
;;; magit-imenu.el --- Integrate Imenu in magit major modes  -*- lexical-binding: t -*-

;; Copyright (C) 2010-2018  The Magit Project Contributors
;;
;; You should have received a copy of the AUTHORS.md file which
;; lists all contributors.  If not, see http://magit.vc/authors.

;; Author: Damien Cassou <damien@cassou.me>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>

;; Magit 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, or (at your option)
;; any later version.
;;
;; Magit 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 Magit.  If not, see http://www.gnu.org/licenses.

;;; Commentary:

;; Emacs' major modes can facilitate navigation in their buffers by
;; supporting Imenu.  In such major modes, launching Imenu (M-x imenu)
;; makes Emacs display a list of items (e.g., function definitions in
;; a programming major mode).  Selecting an item from this list moves
;; point to this item.

;; magit-imenu.el adds Imenu support to every major mode in Magit

;;; Code:

(eval-when-compile (require 'subr-x))

(require 'magit)
(require 'git-rebase)

(defun magit-imenu--index-function (entry-types menu-types)
  "Return an alist of imenu entries in current buffer.

ENTRY-TYPES is a list of section types to be selected through
`imenu'.

MENU-TYPES is a list of section types containing elements of
ENTRY-TYPES.  Elements of MENU-TYPES are are used to categories
elements of ENTRY-TYPES.

This function is used as a helper for functions set as
`imenu-create-index-function'."
  (let ((entries (make-hash-table :test 'equal)))
    (goto-char (point-max))
    (while (magit-section--backward-find
            (lambda ()
              (let* ((section (magit-current-section))
                     (type (oref section type))
                     (parent (oref section parent))
                     (parent-type (oref parent type)))
                (and (-contains-p entry-types type)
                     (-contains-p menu-types parent-type)))))
      (let* ((section (magit-current-section))
             (name (buffer-substring-no-properties
                    (line-beginning-position)
                    (line-end-position)))
             (parent (oref section parent))
             (parent-title (buffer-substring-no-properties
                            (oref parent start)
                            (1- (oref parent content)))))
        (puthash parent-title
                 (cons (cons name (point))
                       (gethash parent-title entries (list)))
                 entries)))
    (mapcar (lambda (menu-title)
              (cons menu-title (gethash menu-title entries)))
            (hash-table-keys entries))))

;;; Log mode

;;;###autoload
(defun magit-imenu--log-prev-index-position-function ()
  "Move point to previous line in current buffer.
This function is used as a value for
`imenu-prev-index-position-function'."
  (magit-section--backward-find
   (lambda ()
     (-contains-p '(commit stash)
                  (oref (magit-current-section) type)))))

;;;###autoload
(defun magit-imenu--log-extract-index-name-function ()
  "Return imenu name for line at point.
This function is used as a value for
`imenu-extract-index-name-function'.  Point should be at the
beginning of the line."
  (save-match-data
    (looking-at "\\([^ ]+\\)[ *|]+\\(.+\\)$")
    (format "%s: %s"
            (match-string-no-properties 1)
            (match-string-no-properties 2))))

;;; Diff mode

;;;###autoload
(defun magit-imenu--diff-prev-index-position-function ()
  "Move point to previous file line in current buffer.
This function is used as a value for
`imenu-prev-index-position-function'."
  (magit-section--backward-find
   (lambda ()
     (let ((section (magit-current-section)))
       (and (magit-file-section-p section)
            (not (equal (oref (oref section parent) type)
                        'diffstat)))))))

;;;###autoload
(defun magit-imenu--diff-extract-index-name-function ()
  "Return imenu name for line at point.
This function is used as a value for
`imenu-extract-index-name-function'.  Point should be at the
beginning of the line."
  (buffer-substring-no-properties (line-beginning-position)
                                  (line-end-position)))

;;; Status mode

;;;###autoload
(defun magit-imenu--status-create-index-function ()
  "Return an alist of all imenu entries in current buffer.
This function is used as a value for
`imenu-create-index-function'."
  (magit-imenu--index-function
   '(file commit stash)
   '(unpushed unstaged unpulled untracked staged stashes)))

;;;; Refs mode

;;;###autoload
(defun magit-imenu--refs-create-index-function ()
  "Return an alist of all imenu entries in current buffer.
This function is used as a value for
`imenu-create-index-function'."
  (magit-imenu--index-function
   '(branch commit tag)
   '(local remote tags)))

;;;; Cherry mode

;;;###autoload
(defun magit-imenu--cherry-create-index-function ()
  "Return an alist of all imenu entries in current buffer.
This function is used as a value for
`imenu-create-index-function'."
  (magit-imenu--index-function
   '(commit)
   '(cherries)))

;;;; Submodule list mode

;;;###autoload
(defun magit-imenu--submodule-prev-index-position-function ()
  "Move point to previous line in magit-submodule-list buffer.
This function is used as a value for
`imenu-prev-index-position-function'."
  (unless (bobp)
    (forward-line -1)))

;;;###autoload
(defun magit-imenu--submodule-extract-index-name-function ()
  "Return imenu name for line at point.
This function is used as a value for
`imenu-extract-index-name-function'.  Point should be at the
beginning of the line."
  (elt (tabulated-list-get-entry) 0))

;;;; Repolist mode

;;;###autoload
(defun magit-imenu--repolist-prev-index-position-function ()
  "Move point to previous line in magit-repolist buffer.
This function is used as a value for
`imenu-prev-index-position-function'."
  (unless (bobp)
    (forward-line -1)))

;;;###autoload
(defun magit-imenu--repolist-extract-index-name-function ()
  "Return imenu name for line at point.
This function is used as a value for
`imenu-extract-index-name-function'.  Point should be at the
beginning of the line."
  (let ((entry (tabulated-list-get-entry)))
    (format "%s (%s)"
            (elt entry 0)
            (elt entry (1- (length entry))))))

;;;; Process mode

;;;###autoload
(defun magit-imenu--process-prev-index-position-function ()
  "Move point to previous process in magit-process buffer.
This function is used as a value for
`imenu-prev-index-position-function'."
  (magit-section--backward-find
   (lambda ()
     (eq (oref (magit-current-section) type) 'process))))

;;;###autoload
(defun magit-imenu--process-extract-index-name-function ()
  "Return imenu name for line at point.
This function is used as a value for
`imenu-extract-index-name-function'.  Point should be at the
beginning of the line."
  (buffer-substring-no-properties (line-beginning-position)
                                  (line-end-position)))

;;;; Rebase mode

;;;###autoload
(defun magit-imenu--rebase-prev-index-position-function ()
  "Move point to previous commit in git-rebase buffer.
This function is used as a value for
`imenu-prev-index-position-function'."
  (catch 'found
    (while (not (bobp))
      (git-rebase-backward-line)
      (when (git-rebase-line-p)
        (throw 'found t)))))

;;;###autoload
(defun magit-imenu--rebase-extract-index-name-function ()
  "Return imenu name for line at point.
This function is used as a value for
`imenu-extract-index-name-function'.  Point should be at the
beginning of the line."
  (buffer-substring-no-properties (line-beginning-position)
                                  (line-end-position)))

(provide 'magit-imenu)
;;; magit-imenu.el ends here