Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / semantic / senator.el
blob366f4a8b7ff56672ab0337333a55f97a2f9201dc
1 ;;; semantic/senator.el --- SEmantic NAvigaTOR
3 ;; Copyright (C) 2000-2014 Free Software Foundation, Inc.
5 ;; Author: David Ponce <david@dponce.com>
6 ;; Maintainer: FSF
7 ;; Created: 10 Nov 2000
8 ;; Keywords: syntax
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; This file defines some user commands for navigating between
28 ;; Semantic tags. This is a subset of the version of senator.el in
29 ;; the upstream CEDET package; the rest is incorporated into other
30 ;; parts of Semantic or Emacs.
32 ;;; Code:
34 (require 'ring)
35 (require 'semantic)
36 (require 'semantic/ctxt)
37 (require 'semantic/decorate)
38 (require 'semantic/format)
40 (eval-when-compile (require 'semantic/find))
42 ;; (eval-when-compile (require 'hippie-exp))
44 (declare-function semantic-analyze-tag-references "semantic/analyze/refs")
45 (declare-function semantic-analyze-refs-impl "semantic/analyze/refs")
46 (declare-function semantic-analyze-find-tag "semantic/analyze")
47 (declare-function semantic-analyze-tag-type "semantic/analyze/fcn")
48 (declare-function semantic-tag-external-class "semantic/sort")
49 (declare-function imenu--mouse-menu "imenu")
51 ;;; Customization
52 (defgroup senator nil
53 "Semantic Navigator."
54 :group 'semantic)
56 ;;;###autoload
57 (defcustom senator-step-at-tag-classes nil
58 "List of tag classes recognized by Senator's navigation commands.
59 A tag class is a symbol, such as `variable', `function', or `type'.
61 As a special exception, if the value is nil, Senator's navigation
62 commands recognize all tag classes."
63 :group 'senator
64 :type '(repeat (symbol)))
65 ;;;###autoload
66 (make-variable-buffer-local 'senator-step-at-tag-classes)
68 ;;;###autoload
69 (defcustom senator-step-at-start-end-tag-classes nil
70 "List of tag classes at which Senator's navigation commands should stop.
71 A tag class is a symbol, such as `variable', `function', or `type'.
72 The navigation commands stop at the start and end of each tag
73 class in this list, provided the tag class is recognized (see
74 `senator-step-at-tag-classes').
76 As a special exception, if the value is nil, the navigation
77 commands stop at the beginning of every tag.
79 If t, the navigation commands stop at the start and end of any
80 tag, where possible."
81 :group 'senator
82 :type '(choice :tag "Identifiers"
83 (repeat :menu-tag "Symbols" (symbol))
84 (const :tag "All" t)))
85 ;;;###autoload
86 (make-variable-buffer-local 'senator-step-at-start-end-tag-classes)
88 (defcustom senator-highlight-found nil
89 "If non-nil, Senator commands momentarily highlight found tags."
90 :group 'senator
91 :type 'boolean)
92 (make-variable-buffer-local 'senator-highlight-found)
94 ;;; Faces
95 (defface senator-momentary-highlight-face
96 '((((class color) (background dark))
97 (:background "gray30"))
98 (((class color) (background light))
99 (:background "gray70")))
100 "Face used to momentarily highlight tags."
101 :group 'semantic-faces)
103 ;;; Common functions
105 (defun senator-momentary-highlight-tag (tag)
106 "Momentarily highlight TAG.
107 Does nothing if `senator-highlight-found' is nil."
108 (and senator-highlight-found
109 (semantic-momentary-highlight-tag
110 tag 'senator-momentary-highlight-face)))
112 (defun senator-step-at-start-end-p (tag)
113 "Return non-nil if must step at start and end of TAG."
114 (and tag
115 (or (eq senator-step-at-start-end-tag-classes t)
116 (memq (semantic-tag-class tag)
117 senator-step-at-start-end-tag-classes))))
119 (defun senator-skip-p (tag)
120 "Return non-nil if must skip TAG."
121 (and tag
122 senator-step-at-tag-classes
123 (not (memq (semantic-tag-class tag)
124 senator-step-at-tag-classes))))
126 (defun senator-middle-of-tag-p (pos tag)
127 "Return non-nil if POS is between start and end of TAG."
128 (and (> pos (semantic-tag-start tag))
129 (< pos (semantic-tag-end tag))))
131 (defun senator-step-at-parent (tag)
132 "Return TAG's outermost parent if must step at start/end of it.
133 Return nil otherwise."
134 (if tag
135 (let (parent parents)
136 (setq parents (semantic-find-tag-by-overlay
137 (semantic-tag-start tag)))
138 (while (and parents (not parent))
139 (setq parent (car parents)
140 parents (cdr parents))
141 (if (or (eq tag parent)
142 (senator-skip-p parent)
143 (not (senator-step-at-start-end-p parent)))
144 (setq parent nil)))
145 parent)))
147 (defun senator-previous-tag-or-parent (pos)
148 "Return the tag before POS or one of its parent where to step."
149 (let (ol tag)
150 (while (and pos (> pos (point-min)) (not tag))
151 (setq pos (semantic-overlay-previous-change pos))
152 (when pos
153 ;; Get overlays at position
154 (setq ol (semantic-overlays-at pos))
155 ;; find the overlay that belongs to semantic
156 ;; and STARTS or ENDS at the found position.
157 (while (and ol (not tag))
158 (setq tag (semantic-overlay-get (car ol) 'semantic))
159 (unless (and tag (semantic-tag-p tag)
160 (or (= (semantic-tag-start tag) pos)
161 (= (semantic-tag-end tag) pos)))
162 (setq tag nil
163 ol (cdr ol))))))
164 (or (senator-step-at-parent tag) tag)))
166 ;;; Search functions
168 (defun senator-search-tag-name (tag)
169 "Search for TAG name in current buffer.
170 Limit the search to TAG bounds.
171 If found, set point to the end of the name, and return point. The
172 beginning of the name is at (match-beginning 0).
173 Return nil if not found, that is if TAG name doesn't come from the
174 source."
175 (let ((name (semantic-tag-name tag)))
176 (setq name (if (string-match "\\`\\([^[]+\\)[[]" name)
177 (match-string 1 name)
178 name))
179 (goto-char (semantic-tag-start tag))
180 (when (re-search-forward (concat
181 ;; The tag name is expected to be
182 ;; between word delimiters, whitespace,
183 ;; or punctuation.
184 "\\(\\<\\|\\s-+\\|\\s.\\)"
185 (regexp-quote name)
186 "\\(\\>\\|\\s-+\\|\\s.\\)")
187 (semantic-tag-end tag)
189 (goto-char (match-beginning 0))
190 (search-forward name))))
192 (defcustom senator-search-ignore-tag-classes
193 '(code block)
194 "List of ignored tag classes.
195 Tags of those classes are excluded from search."
196 :group 'senator
197 :type '(repeat (symbol :tag "class")))
199 (defun senator-search-default-tag-filter (tag)
200 "Default function that filters searched tags.
201 Ignore tags of classes in `senator-search-ignore-tag-classes'"
202 (not (memq (semantic-tag-class tag)
203 senator-search-ignore-tag-classes)))
205 (defvar senator-search-tag-filter-functions
206 '(senator-search-default-tag-filter)
207 "List of functions to be called to filter searched tags.
208 Each function is passed a tag. If one of them returns nil, the tag is
209 excluded from the search.")
211 (defun senator-search (searcher text &optional bound noerror count)
212 "Use the SEARCHER function to search from point for TEXT in a tag name.
213 SEARCHER is typically the function `search-forward', `search-backward',
214 `word-search-forward', `word-search-backward', `re-search-forward', or
215 `re-search-backward'. See one of the above function to see how the
216 TEXT, BOUND, NOERROR, and COUNT arguments are interpreted."
217 (let* ((origin (point))
218 (count (or count 1))
219 (step (cond ((> count 0) 1)
220 ((< count 0) (setq count (- count)) -1)
221 (0)))
222 found next sstart send tag tstart tend)
223 (or (zerop step)
224 (while (and (not found)
225 (setq next (funcall searcher text bound t step)))
226 (setq sstart (match-beginning 0)
227 send (match-end 0))
228 (if (= sstart send)
229 (setq found t)
230 (and (setq tag (semantic-current-tag))
231 (run-hook-with-args-until-failure
232 'senator-search-tag-filter-functions tag)
233 (setq tend (senator-search-tag-name tag))
234 (setq tstart (match-beginning 0)
235 found (and (>= sstart tstart)
236 (<= send tend)
237 (zerop (setq count (1- count))))))
238 (goto-char next))))
239 (cond ((null found)
240 (setq next origin
241 send origin))
242 ((= next sstart)
243 (setq next send
244 send sstart))
246 (setq next sstart)))
247 (goto-char next)
248 ;; Setup the returned value and the `match-data' or maybe fail!
249 (funcall searcher text send noerror step)))
251 ;;; Navigation commands
253 ;;;###autoload
254 (defun senator-next-tag ()
255 "Navigate to the next Semantic tag.
256 Return the tag or nil if at end of buffer."
257 (interactive)
258 (semantic-error-if-unparsed)
259 (let ((pos (point))
260 (tag (semantic-current-tag))
261 where)
262 (if (and tag
263 (not (senator-skip-p tag))
264 (senator-step-at-start-end-p tag)
265 (or (= pos (semantic-tag-start tag))
266 (senator-middle-of-tag-p pos tag)))
268 (if (setq tag (senator-step-at-parent tag))
270 (setq tag (semantic-find-tag-by-overlay-next pos))
271 (while (and tag (senator-skip-p tag))
272 (setq tag (semantic-find-tag-by-overlay-next
273 (semantic-tag-start tag))))))
274 (if (not tag)
275 (progn
276 (goto-char (point-max))
277 (message "End of buffer"))
278 (cond ((and (senator-step-at-start-end-p tag)
279 (or (= pos (semantic-tag-start tag))
280 (senator-middle-of-tag-p pos tag)))
281 (setq where "end")
282 (goto-char (semantic-tag-end tag)))
284 (setq where "start")
285 (goto-char (semantic-tag-start tag))))
286 (senator-momentary-highlight-tag tag)
287 (message "%S: %s (%s)"
288 (semantic-tag-class tag)
289 (semantic-tag-name tag)
290 where))
291 tag))
293 ;;;###autoload
294 (defun senator-previous-tag ()
295 "Navigate to the previous Semantic tag.
296 Return the tag or nil if at beginning of buffer."
297 (interactive)
298 (semantic-error-if-unparsed)
299 (let ((pos (point))
300 (tag (semantic-current-tag))
301 where)
302 (if (and tag
303 (not (senator-skip-p tag))
304 (senator-step-at-start-end-p tag)
305 (or (= pos (semantic-tag-end tag))
306 (senator-middle-of-tag-p pos tag)))
308 (if (setq tag (senator-step-at-parent tag))
310 (setq tag (senator-previous-tag-or-parent pos))
311 (while (and tag (senator-skip-p tag))
312 (setq tag (senator-previous-tag-or-parent
313 (semantic-tag-start tag))))))
314 (if (not tag)
315 (progn
316 (goto-char (point-min))
317 (message "Beginning of buffer"))
318 (cond ((or (not (senator-step-at-start-end-p tag))
319 (= pos (semantic-tag-end tag))
320 (senator-middle-of-tag-p pos tag))
321 (setq where "start")
322 (goto-char (semantic-tag-start tag)))
324 (setq where "end")
325 (goto-char (semantic-tag-end tag))))
326 (senator-momentary-highlight-tag tag)
327 (message "%S: %s (%s)"
328 (semantic-tag-class tag)
329 (semantic-tag-name tag)
330 where))
331 tag))
333 ;;; Search commands
335 (defun senator-search-forward (string &optional bound noerror count)
336 "Search in tag names forward from point for STRING.
337 Set point to the end of the occurrence found, and return point.
338 See also the function `search-forward' for details on the BOUND,
339 NOERROR and COUNT arguments."
340 (interactive "sSemantic search: ")
341 (senator-search 'search-forward string bound noerror count))
343 (defun senator-re-search-forward (regexp &optional bound noerror count)
344 "Search in tag names forward from point for regular expression REGEXP.
345 Set point to the end of the occurrence found, and return point.
346 See also the function `re-search-forward' for details on the BOUND,
347 NOERROR and COUNT arguments."
348 (interactive "sSemantic regexp search: ")
349 (senator-search 're-search-forward regexp bound noerror count))
351 (defun senator-word-search-forward (word &optional bound noerror count)
352 "Search in tag names forward from point for WORD.
353 Set point to the end of the occurrence found, and return point.
354 See also the function `word-search-forward' for details on the BOUND,
355 NOERROR and COUNT arguments."
356 (interactive "sSemantic word search: ")
357 (senator-search 'word-search-forward word bound noerror count))
359 (defun senator-search-backward (string &optional bound noerror count)
360 "Search in tag names backward from point for STRING.
361 Set point to the beginning of the occurrence found, and return point.
362 See also the function `search-backward' for details on the BOUND,
363 NOERROR and COUNT arguments."
364 (interactive "sSemantic backward search: ")
365 (senator-search 'search-backward string bound noerror count))
367 (defun senator-re-search-backward (regexp &optional bound noerror count)
368 "Search in tag names backward from point for regular expression REGEXP.
369 Set point to the beginning of the occurrence found, and return point.
370 See also the function `re-search-backward' for details on the BOUND,
371 NOERROR and COUNT arguments."
372 (interactive "sSemantic backward regexp search: ")
373 (senator-search 're-search-backward regexp bound noerror count))
375 (defun senator-word-search-backward (word &optional bound noerror count)
376 "Search in tag names backward from point for WORD.
377 Set point to the beginning of the occurrence found, and return point.
378 See also the function `word-search-backward' for details on the BOUND,
379 NOERROR and COUNT arguments."
380 (interactive "sSemantic backward word search: ")
381 (senator-search 'word-search-backward word bound noerror count))
383 ;;; Other useful search commands (minor mode menu)
385 (defvar senator-last-search-type nil
386 "Type of last non-incremental search command called.")
388 (defun senator-nonincremental-repeat-search-forward ()
389 "Search forward for the previous search string or regexp."
390 (interactive)
391 (cond
392 ((and (eq senator-last-search-type 'string)
393 search-ring)
394 (senator-search-forward (car search-ring)))
395 ((and (eq senator-last-search-type 'regexp)
396 regexp-search-ring)
397 (senator-re-search-forward (car regexp-search-ring)))
399 (error "No previous search"))))
401 (defun senator-nonincremental-repeat-search-backward ()
402 "Search backward for the previous search string or regexp."
403 (interactive)
404 (cond
405 ((and (eq senator-last-search-type 'string)
406 search-ring)
407 (senator-search-backward (car search-ring)))
408 ((and (eq senator-last-search-type 'regexp)
409 regexp-search-ring)
410 (senator-re-search-backward (car regexp-search-ring)))
412 (error "No previous search"))))
414 (defun senator-nonincremental-search-forward (string)
415 "Search for STRING nonincrementally."
416 (interactive "sSemantic search for string: ")
417 (setq senator-last-search-type 'string)
418 (if (equal string "")
419 (senator-search-forward (car search-ring))
420 (isearch-update-ring string nil)
421 (senator-search-forward string)))
423 (defun senator-nonincremental-search-backward (string)
424 "Search backward for STRING nonincrementally."
425 (interactive "sSemantic search for string: ")
426 (setq senator-last-search-type 'string)
427 (if (equal string "")
428 (senator-search-backward (car search-ring))
429 (isearch-update-ring string nil)
430 (senator-search-backward string)))
432 (defun senator-nonincremental-re-search-forward (string)
433 "Search for the regular expression STRING nonincrementally."
434 (interactive "sSemantic search for regexp: ")
435 (setq senator-last-search-type 'regexp)
436 (if (equal string "")
437 (senator-re-search-forward (car regexp-search-ring))
438 (isearch-update-ring string t)
439 (senator-re-search-forward string)))
441 (defun senator-nonincremental-re-search-backward (string)
442 "Search backward for the regular expression STRING nonincrementally."
443 (interactive "sSemantic search for regexp: ")
444 (setq senator-last-search-type 'regexp)
445 (if (equal string "")
446 (senator-re-search-backward (car regexp-search-ring))
447 (isearch-update-ring string t)
448 (senator-re-search-backward string)))
450 (defvar senator--search-filter nil)
452 (defun senator-search-set-tag-class-filter (&optional classes)
453 "In current buffer, limit search scope to tag CLASSES.
454 CLASSES is a list of tag class symbols or nil. If nil only global
455 filters in `senator-search-tag-filter-functions' remain active."
456 (interactive "sClasses: ")
457 (setq classes
458 (cond
459 ((null classes)
460 nil)
461 ((symbolp classes)
462 (list classes))
463 ((stringp classes)
464 (mapcar 'read (split-string classes)))
466 (signal 'wrong-type-argument (list classes)))
468 ;; Clear previous filter.
469 (remove-hook 'senator-search-tag-filter-functions
470 senator--search-filter t)
471 (kill-local-variable 'senator--search-filter)
472 (if classes
473 (let ((tag (make-symbol "tag"))
474 (names (mapconcat 'symbol-name classes "', `")))
475 (set (make-local-variable 'senator--search-filter)
476 `(lambda (,tag)
477 (memq (semantic-tag-class ,tag) ',classes)))
478 (add-hook 'senator-search-tag-filter-functions
479 senator--search-filter nil t)
480 (message "Limit search to `%s' tags" names))
481 (message "Default search filter restored")))
483 ;;; Folding
485 ;; Use new folding state. It might be wise to extend the idea
486 ;; of folding for hiding all but this, or show all children, etc.
488 (defun senator-fold-tag (&optional tag)
489 "Fold the current TAG."
490 (interactive)
491 (semantic-set-tag-folded (or tag (semantic-current-tag)) t))
493 (defun senator-unfold-tag (&optional tag)
494 "Fold the current TAG."
495 (interactive)
496 (semantic-set-tag-folded (or tag (semantic-current-tag)) nil))
498 (defun senator-fold-tag-toggle (&optional tag)
499 "Fold the current TAG."
500 (interactive)
501 (let ((tag (or tag (semantic-current-tag))))
502 (if (semantic-tag-folded-p tag)
503 (senator-unfold-tag tag)
504 (senator-fold-tag tag))))
506 ;; @TODO - move this to some analyzer / refs tool
507 (define-overloadable-function semantic-up-reference (tag)
508 "Return a tag that is referred to by TAG.
509 A \"reference\" could be any interesting feature of TAG.
510 In C++, a function may have a 'parent' which is non-local.
511 If that parent which is only a reference in the function tag
512 is found, we can jump to it.
513 Some tags such as includes have other reference features.")
515 ;;;###autoload
516 (defun senator-go-to-up-reference (&optional tag)
517 "Move up one reference from the current TAG.
518 A \"reference\" could be any interesting feature of TAG.
519 In C++, a function may have a 'parent' which is non-local.
520 If that parent which is only a reference in the function tag
521 is found, we can jump to it.
522 Some tags such as includes have other reference features."
523 (interactive)
524 (semantic-error-if-unparsed)
525 (let ((result (semantic-up-reference (or tag (semantic-current-tag)))))
526 (if (not result)
527 (error "No up reference found")
528 (push-mark)
529 (cond
530 ;; A tag
531 ((semantic-tag-p result)
532 (semantic-go-to-tag result)
533 (switch-to-buffer (current-buffer))
534 (semantic-momentary-highlight-tag result))
535 ;; Buffers
536 ((bufferp result)
537 (switch-to-buffer result)
538 (pulse-momentary-highlight-one-line (point)))
539 ;; Files
540 ((and (stringp result) (file-exists-p result))
541 (find-file result)
542 (pulse-momentary-highlight-one-line (point)))
544 (error "Unknown result type from `semantic-up-reference'"))))))
546 (defun semantic-up-reference-default (tag)
547 "Return a tag that is referred to by TAG.
548 Makes C/C++ language like assumptions."
549 (cond ((semantic-tag-faux-p tag)
550 ;; Faux tags should have a real tag in some other location.
551 (require 'semantic/sort)
552 (let ((options (semantic-tag-external-class tag)))
553 ;; I should do something a little better than
554 ;; this. Oy!
555 (car options)
558 ;; Include always point to another file.
559 ((eq (semantic-tag-class tag) 'include)
560 (let ((file (semantic-dependency-tag-file tag)))
561 (cond
562 ((or (not file) (not (file-exists-p file)))
563 (error "Could not location include %s"
564 (semantic-tag-name tag)))
565 ((get-file-buffer file)
566 (get-file-buffer file))
567 ((stringp file)
568 file)
571 ;; Is there a parent of the function to jump to?
572 ((and (semantic-tag-of-class-p tag 'function)
573 (semantic-tag-function-parent tag))
574 (let* ((scope (semantic-calculate-scope (point))))
575 ;; @todo - it would be cool to ask the user which one if
576 ;; more than one.
577 (car (oref scope parents))
580 ;; Is there a non-prototype version of the tag to jump to?
581 ((semantic-tag-get-attribute tag :prototype-flag)
582 (require 'semantic/analyze/refs)
583 (let* ((sar (semantic-analyze-tag-references tag)))
584 (car (semantic-analyze-refs-impl sar t)))
587 ;; If this is a datatype, and we have superclasses
588 ((and (semantic-tag-of-class-p tag 'type)
589 (semantic-tag-type-superclasses tag))
590 (require 'semantic/analyze)
591 (let ((scope (semantic-calculate-scope (point)))
592 (parents (semantic-tag-type-superclasses tag)))
593 (semantic-analyze-find-tag (car parents) 'type scope)))
595 ;; Get the data type, and try to find that.
596 ((semantic-tag-type tag)
597 (require 'semantic/analyze)
598 (let ((scope (semantic-calculate-scope (point))))
599 (semantic-analyze-tag-type tag scope))
601 (t nil)))
603 (defvar senator-isearch-semantic-mode nil
604 "Non-nil if isearch does semantic search.
605 This is a buffer local variable.")
606 (make-variable-buffer-local 'senator-isearch-semantic-mode)
608 (defun senator-beginning-of-defun (&optional arg)
609 "Move backward to the beginning of a defun.
610 Use semantic tags to navigate.
611 ARG is the number of tags to navigate (not yet implemented)."
612 (semantic-fetch-tags)
613 (let* ((senator-highlight-found nil)
614 ;; Step at beginning of next tag with class specified in
615 ;; `senator-step-at-tag-classes'.
616 (senator-step-at-start-end-tag-classes t)
617 (tag (senator-previous-tag)))
618 (when tag
619 (if (= (point) (semantic-tag-end tag))
620 (goto-char (semantic-tag-start tag)))
621 (beginning-of-line))))
623 (defun senator-end-of-defun (&optional arg)
624 "Move forward to next end of defun.
625 Use semantic tags to navigate.
626 ARG is the number of tags to navigate (not yet implemented)."
627 (semantic-fetch-tags)
628 (let* ((senator-highlight-found nil)
629 ;; Step at end of next tag with class specified in
630 ;; `senator-step-at-tag-classes'.
631 (senator-step-at-start-end-tag-classes t)
632 (tag (senator-next-tag)))
633 (when tag
634 (if (= (point) (semantic-tag-start tag))
635 (goto-char (semantic-tag-end tag)))
636 (skip-chars-forward " \t")
637 (if (looking-at "\\s<\\|\n")
638 (forward-line 1)))))
640 (defun senator-narrow-to-defun ()
641 "Make text outside current defun invisible.
642 The defun visible is the one that contains point or follows point.
643 Use semantic tags to navigate."
644 (interactive)
645 (semantic-fetch-tags)
646 (save-excursion
647 (widen)
648 (senator-end-of-defun)
649 (let ((end (point)))
650 (senator-beginning-of-defun)
651 (narrow-to-region (point) end))))
653 (defun senator-mark-defun ()
654 "Put mark at end of this defun, point at beginning.
655 The defun marked is the one that contains point or follows point.
656 Use semantic tags to navigate."
657 (interactive)
658 (let ((origin (point))
659 (end (progn (senator-end-of-defun) (point)))
660 (start (progn (senator-beginning-of-defun) (point))))
661 (goto-char origin)
662 (push-mark (point))
663 (goto-char end) ;; end-of-defun
664 (push-mark (point) nil t)
665 (goto-char start) ;; beginning-of-defun
666 (re-search-backward "^\n" (- (point) 1) t)))
668 ;;; Tag Cut & Paste
670 ;; To copy a tag, means to put a tag definition into the tag
671 ;; ring. To kill a tag, put the tag into the tag ring AND put
672 ;; the body of the tag into the kill-ring.
674 ;; To retrieve a killed tag's text, use C-y (yank), but to retrieve
675 ;; the tag as a reference of some sort, use senator-yank-tag.
677 (defvar senator-tag-ring (make-ring 20)
678 "Ring of tags for use with cut and paste.")
680 ;;;###autoload
681 (defun senator-copy-tag ()
682 "Take the current tag, and place it in the tag ring."
683 (interactive)
684 (semantic-fetch-tags)
685 (let ((ft (semantic-obtain-foreign-tag)))
686 (when ft
687 (ring-insert senator-tag-ring ft)
688 (kill-ring-save (semantic-tag-start ft) (semantic-tag-end ft))
689 (when (called-interactively-p 'interactive)
690 (message "Use C-y to yank text. \
691 Use `senator-yank-tag' for prototype insert.")))
692 ft))
694 ;;;###autoload
695 (defun senator-kill-tag ()
696 "Take the current tag, place it in the tag ring, and kill it.
697 Killing the tag removes the text for that tag, and places it into
698 the kill ring. Retrieve that text with \\[yank]."
699 (interactive)
700 (let ((ct (senator-copy-tag))) ;; this handles the reparse for us.
701 (kill-region (semantic-tag-start ct)
702 (semantic-tag-end ct))
703 (when (called-interactively-p 'interactive)
704 (message "Use C-y to yank text. \
705 Use `senator-yank-tag' for prototype insert."))))
707 ;;;###autoload
708 (defun senator-yank-tag ()
709 "Yank a tag from the tag ring.
710 The form the tag takes is different depending on where it is being
711 yanked to."
712 (interactive)
713 (or (ring-empty-p senator-tag-ring)
714 (let ((ft (ring-ref senator-tag-ring 0)))
715 (semantic-foreign-tag-check ft)
716 (semantic-insert-foreign-tag ft)
717 (when (called-interactively-p 'interactive)
718 (message "Use C-y to recover the yank the text of %s."
719 (semantic-tag-name ft))))))
721 ;;;###autoload
722 (defun senator-copy-tag-to-register (register &optional kill-flag)
723 "Copy the current tag into REGISTER.
724 Optional argument KILL-FLAG will delete the text of the tag to the
725 kill ring."
726 (interactive "cTag to register: \nP")
727 (semantic-fetch-tags)
728 (let ((ft (semantic-obtain-foreign-tag)))
729 (when ft
730 (set-register
731 register (registerv-make
733 :insert-func #'semantic-insert-foreign-tag
734 :jump-func (lambda (v)
735 (switch-to-buffer (semantic-tag-buffer v))
736 (goto-char (semantic-tag-start v)))))
737 (if kill-flag
738 (kill-region (semantic-tag-start ft)
739 (semantic-tag-end ft))))))
741 ;;;###autoload
742 (defun senator-transpose-tags-up ()
743 "Transpose the current tag, and the preceding tag."
744 (interactive)
745 (semantic-fetch-tags)
746 (let* ((current-tag (semantic-current-tag))
747 (prev-tag (save-excursion
748 (goto-char (semantic-tag-start current-tag))
749 (semantic-find-tag-by-overlay-prev)))
750 (ct-parent (semantic-find-tag-parent-by-overlay current-tag))
751 (pt-parent (semantic-find-tag-parent-by-overlay prev-tag)))
752 (if (not (eq ct-parent pt-parent))
753 (error "Cannot transpose tags"))
754 (let ((txt (buffer-substring (semantic-tag-start current-tag)
755 (semantic-tag-end current-tag)))
756 (line (count-lines (semantic-tag-start current-tag)
757 (point)))
758 (insert-point nil)
760 (delete-region (semantic-tag-start current-tag)
761 (semantic-tag-end current-tag))
762 (delete-blank-lines)
763 (goto-char (semantic-tag-start prev-tag))
764 (setq insert-point (point))
765 (insert txt)
766 (if (/= (current-column) 0)
767 (insert "\n"))
768 (insert "\n")
769 (goto-char insert-point)
770 (forward-line line)
773 ;;;###autoload
774 (defun senator-transpose-tags-down ()
775 "Transpose the current tag, and the following tag."
776 (interactive)
777 (semantic-fetch-tags)
778 (let* ((current-tag (semantic-current-tag))
779 (next-tag (save-excursion
780 (goto-char (semantic-tag-end current-tag))
781 (semantic-find-tag-by-overlay-next)))
782 (end-pt (point-marker))
784 (goto-char (semantic-tag-start next-tag))
785 (forward-char 1)
786 (senator-transpose-tags-up)
787 ;; I know that the above fcn deletes the next tag, so our pt marker
788 ;; will be stable.
789 (goto-char end-pt)))
791 ;;; Using semantic search in isearch mode
793 (defun senator-lazy-highlight-update ()
794 "Force lazy highlight update."
795 (lazy-highlight-cleanup t)
796 (set 'isearch-lazy-highlight-last-string nil)
797 (setq isearch-adjusted t)
798 (isearch-update))
800 ;; Recent versions of GNU Emacs allow to override the isearch search
801 ;; function for special needs, and avoid to advice the built-in search
802 ;; function :-)
803 (defun senator-isearch-search-fun ()
804 "Return the function to use for the search.
805 Use a senator search function when semantic isearch mode is enabled."
806 (intern
807 (concat (if senator-isearch-semantic-mode
808 "senator-"
810 (cond (isearch-word "word-")
811 (isearch-regexp "re-")
812 (t ""))
813 "search-"
814 (if isearch-forward
815 "forward"
816 "backward"))))
818 (defun senator-isearch-toggle-semantic-mode ()
819 "Toggle semantic searching on or off in isearch mode."
820 (interactive)
821 (setq senator-isearch-semantic-mode
822 (not senator-isearch-semantic-mode))
823 (if isearch-mode
824 ;; force lazy highlight update
825 (senator-lazy-highlight-update)
826 (message "Isearch semantic mode %s"
827 (if senator-isearch-semantic-mode
828 "enabled"
829 "disabled"))))
831 (defvar senator-old-isearch-search-fun nil
832 "Hold previous value of `isearch-search-fun-function'.")
834 (defun senator-isearch-mode-hook ()
835 "Isearch mode hook to setup semantic searching."
836 (if (and isearch-mode senator-isearch-semantic-mode)
837 (progn
838 ;; When `senator-isearch-semantic-mode' is on save the
839 ;; previous `isearch-search-fun-function' and install the
840 ;; senator one.
841 (when (and (local-variable-p 'isearch-search-fun-function)
842 (not (local-variable-p 'senator-old-isearch-search-fun)))
843 (set (make-local-variable 'senator-old-isearch-search-fun)
844 isearch-search-fun-function))
845 (set (make-local-variable 'isearch-search-fun-function)
846 'senator-isearch-search-fun))
847 ;; When `senator-isearch-semantic-mode' is off restore the
848 ;; previous `isearch-search-fun-function'.
849 (when (eq isearch-search-fun-function 'senator-isearch-search-fun)
850 (if (local-variable-p 'senator-old-isearch-search-fun)
851 (progn
852 (set (make-local-variable 'isearch-search-fun-function)
853 senator-old-isearch-search-fun)
854 (kill-local-variable 'senator-old-isearch-search-fun))
855 (kill-local-variable 'isearch-search-fun-function)))))
857 ;; (add-hook 'isearch-mode-hook 'senator-isearch-mode-hook)
858 ;; (add-hook 'isearch-mode-end-hook 'senator-isearch-mode-hook)
860 ;; ;; Keyboard shortcut to toggle semantic search in isearch mode.
861 ;; (define-key isearch-mode-map
862 ;; [(control ?,)]
863 ;; 'senator-isearch-toggle-semantic-mode)
865 (provide 'semantic/senator)
867 ;; Local variables:
868 ;; generated-autoload-file: "loaddefs.el"
869 ;; generated-autoload-load-name: "semantic/senator"
870 ;; End:
872 ;;; semantic/senator.el ends here