1;;; semantic/util-modes.el --- Semantic minor modes
2
3;; Copyright (C) 2000-2005, 2007-2021 Free Software Foundation, Inc.
4
5;; Authors: Eric M. Ludlam <zappo@gnu.org>
6;;          David Ponce <david@dponce.com>
7;; Keywords: syntax
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software: you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
23
24;;; Commentary:
25;;
26;;  Semantic utility minor modes.
27;;
28
29;;; Code:
30
31;; FIXME: compiling util-modes.el seems to require loading util-modes.el,
32;; so if the previous compilation generated a file that fails to load,
33;; recompiling fails to fix the problem.
34(require 'semantic)
35
36;;; Group for all semantic enhancing modes
37(defgroup semantic-modes nil
38  "Minor modes associated with the Semantic architecture."
39  :group 'semantic)
40
41;;;;
42;;;; Semantic minor modes stuff
43;;;;
44(defcustom semantic-update-mode-line t
45  "If non-nil, show enabled minor modes in the mode line.
46Only minor modes that are not turned on globally are shown in the mode
47line."
48  :group 'semantic
49  :type 'boolean
50  :require 'semantic/util-modes
51  :initialize 'custom-initialize-default
52  :set (lambda (sym val)
53         (set-default sym val)
54         ;; Update status of all Semantic enabled buffers
55         (semantic-mode-line-update)))
56
57(defcustom semantic-mode-line-prefix
58  (propertize "S" 'face 'bold)
59  "Prefix added to minor mode indicators in the mode line."
60  :group 'semantic
61  :type 'string
62  :require 'semantic/util-modes
63  :initialize 'custom-initialize-default)
64
65(defvar semantic-minor-modes-format nil
66  "Mode line format showing Semantic minor modes which are locally enabled.
67It is displayed in the mode line.")
68(put 'semantic-minor-modes-format 'risky-local-variable t)
69
70(defvar semantic-minor-mode-alist nil
71  "Alist saying how to show Semantic minor modes in the mode line.
72Like variable `minor-mode-alist'.")
73
74(defun semantic-mode-line-update ()
75  "Update mode line format of Semantic minor modes.
76Only minor modes that are locally enabled are shown in the mode line."
77  (setq semantic-minor-modes-format nil)
78  (dolist (x semantic-minor-mode-alist)
79    (setq minor-mode-alist (delq (assq (car x) minor-mode-alist)
80                                 minor-mode-alist)))
81  (when semantic-update-mode-line
82    (let ((locals '()))
83      ;; Select the minor modes that aren't enabled globally and who
84      ;; have a non-empty "name".
85      (dolist (x semantic-minor-mode-alist)
86        (unless (or (memq (car x) semantic-init-hook)
87                    (not (string-match "^[ ]*\\(.+\\)" (cadr x))))
88          (push (list (car x) (concat "/" (match-string 1 (cadr x)))) locals)))
89      ;; Then build the format spec.
90      (when locals
91        (let ((prefix (if (string-match "^[ ]*\\(.+\\)"
92                                        semantic-mode-line-prefix)
93                          (match-string 1 semantic-mode-line-prefix)
94                        "S")))
95          (setq semantic-minor-modes-format
96                `((:eval (if (or ,@(mapcar 'car locals))
97                             ,(concat " " prefix)))))
98          ;; It would be easier to just put `locals' inside
99          ;; semantic-minor-modes-format, but then things like
100          ;; mode-line-minor-mode-help can't find the right major mode
101          ;; any more.  So instead, we carefully put the minor modes
102          ;; in minor-mode-alist.
103          (let* ((elem (or (assq 'semantic-minor-modes-format
104                                 minor-mode-alist)
105                           ;; FIXME: This entry is meaningless for
106                           ;; mode-line-minor-mode-help.
107                           '(semantic-minor-modes-format
108                           semantic-minor-modes-format)))
109                 (tail (or (memq elem minor-mode-alist)
110                           (setq minor-mode-alist
111                                 (cons elem minor-mode-alist)))))
112            (setcdr tail (nconc locals (cdr tail)))))))))
113
114(defun semantic-desktop-ignore-this-minor-mode (buffer)
115  "Installed as a minor-mode initializer for Desktop mode.
116BUFFER is the buffer to not initialize a Semantic minor mode in."
117  nil)
118
119(defun semantic-add-minor-mode (toggle name)
120  "Register a new Semantic minor mode.
121TOGGLE is a symbol which is the name of a buffer-local variable that
122is toggled on or off to say whether the minor mode is active or not.
123It is also an interactive function to toggle the mode.
124
125NAME specifies what will appear in the mode line when the minor mode
126is active.  NAME should be either a string starting with a space, or a
127symbol whose value is such a string."
128  ;; Record how to display this minor mode in the mode line
129  (let ((mm (assq toggle semantic-minor-mode-alist)))
130    (if mm
131        (setcdr mm (list name))
132      (setq semantic-minor-mode-alist (cons (list toggle name)
133                                       semantic-minor-mode-alist))))
134  (semantic-mode-line-update)
135
136  ;; Semantic minor modes don't work w/ Desktop restore.
137  ;; This line will disable this minor mode from being restored
138  ;; by Desktop.
139  (when (boundp 'desktop-minor-mode-handlers)
140    (add-to-list 'desktop-minor-mode-handlers
141		 (cons toggle 'semantic-desktop-ignore-this-minor-mode))))
142
143(defun semantic-toggle-minor-mode-globally (mode &optional arg)
144  "Toggle minor mode MODE in every Semantic enabled buffer.
145Return non-nil if MODE is turned on in every Semantic enabled buffer.
146If ARG is positive, enable, if it is negative, disable.
147MODE must be a valid minor mode defined in `minor-mode-alist' and must be
148too an interactive function used to toggle the mode."
149  ;; FIXME: All callers should pass a -1 or +1 argument.
150  (or (and (fboundp mode) (or (assq mode minor-mode-alist) ;Needed?
151			      (assq mode semantic-minor-mode-alist)))
152      (error "Semantic minor mode %s not found" mode))
153  ;; Add or remove the MODE toggle function from `semantic-init-hook'.
154  (cond
155   ;; Turn off if ARG < 0
156   ((< arg 0) (remove-hook 'semantic-init-hook mode))
157   ;; Turn on if ARG > 0
158   ((> arg 0) (add-hook 'semantic-init-hook mode))
159   ;; Otherwise just check MODE state
160   (t
161    (error "semantic-toggle-minor-mode-globally: arg should be -1 or 1")))
162  ;; Update the minor mode format.
163  (semantic-mode-line-update)
164  ;; Then turn MODE on or off in every Semantic enabled buffer.
165  (semantic-map-buffers #'(lambda () (funcall mode arg))))
166
167;;;;
168;;;; Minor mode to highlight areas that a user edits.
169;;;;
170
171;;;###autoload
172(define-minor-mode global-semantic-highlight-edits-mode
173  "Toggle global use of option `semantic-highlight-edits-mode'."
174  :global t :group 'semantic :group 'semantic-modes
175  (semantic-toggle-minor-mode-globally
176   'semantic-highlight-edits-mode
177   (if global-semantic-highlight-edits-mode 1 -1)))
178
179(defcustom semantic-highlight-edits-mode-hook nil
180  "Hook run at the end of function `semantic-highlight-edits-mode'."
181  :group 'semantic
182  :type 'hook)
183
184(defface semantic-highlight-edits-face
185  '((((class color) (background dark))
186     ;; Put this back to something closer to black later.
187     (:background "gray20"))
188    (((class color) (background light))
189     (:background "gray90")))
190  "Face used to show dirty tokens in `semantic-highlight-edits-mode'."
191  :group 'semantic-faces)
192
193(defun semantic-highlight-edits-new-change-hook-fcn (overlay)
194  "Function set into `semantic-edits-new-change-hook'.
195Argument OVERLAY is the overlay created to mark the change.
196This function will set the face property on this overlay."
197  (overlay-put overlay 'face 'semantic-highlight-edits-face))
198
199(defvar semantic-highlight-edits-mode-map
200  (let ((km (make-sparse-keymap)))
201    km)
202  "Keymap for highlight-edits minor mode.")
203
204;;;###autoload
205(define-minor-mode semantic-highlight-edits-mode
206  "Minor mode for highlighting changes made in a buffer.
207Changes are tracked by semantic so that the incremental parser can work
208properly.
209This mode will highlight those changes as they are made, and clear them
210when the incremental parser accounts for those edits.
211
212The minor mode can be turned on only if semantic feature is
213available and the current buffer was set up for parsing.  Return
214non-nil if the minor mode is enabled."
215  :keymap semantic-highlight-edits-mode-map
216  (if semantic-highlight-edits-mode
217      (if (not (and (featurep 'semantic) (semantic-active-p)))
218	  (progn
219	    ;; Disable minor mode if semantic stuff not available
220	    (setq semantic-highlight-edits-mode nil)
221	    (error "Buffer %s was not set up for parsing"
222		   (buffer-name)))
223	(add-hook 'semantic-edits-new-change-functions
224		  'semantic-highlight-edits-new-change-hook-fcn nil t))
225    ;; Remove hooks
226    (remove-hook 'semantic-edits-new-change-functions
227		 'semantic-highlight-edits-new-change-hook-fcn t)))
228
229(semantic-add-minor-mode 'semantic-highlight-edits-mode
230                         "e")
231
232;;;;
233;;;; Minor mode to show unmatched-syntax elements
234;;;;
235
236;;;###autoload
237(define-minor-mode global-semantic-show-unmatched-syntax-mode
238  "Toggle global use of option `semantic-show-unmatched-syntax-mode'."
239  :global t :group 'semantic :group 'semantic-modes
240  ;; Not needed because it's autoloaded instead.
241  ;; :require 'semantic/util-modes
242  (semantic-toggle-minor-mode-globally
243   'semantic-show-unmatched-syntax-mode
244   (if global-semantic-show-unmatched-syntax-mode 1 -1)))
245
246(defcustom semantic-show-unmatched-syntax-mode-hook nil
247  "Hook run at the end of function `semantic-show-unmatched-syntax-mode'."
248  :group 'semantic
249  :type 'hook)
250
251(defface semantic-unmatched-syntax-face
252  '((((class color) (background dark))
253     (:underline "red"))
254    (((class color) (background light))
255     (:underline "red")))
256  "Face used to show unmatched syntax in.
257The face is used in `semantic-show-unmatched-syntax-mode'."
258  :group 'semantic-faces)
259
260(defsubst semantic-unmatched-syntax-overlay-p (overlay)
261  "Return non-nil if OVERLAY is an unmatched syntax one."
262  (eq (overlay-get overlay 'semantic) 'unmatched))
263
264(defun semantic-showing-unmatched-syntax-p ()
265  "Return non-nil if an unmatched syntax overlay was found in buffer."
266  (let ((ol (overlays-in (point-min) (point-max)))
267        found)
268    (while (and ol (not found))
269      (setq found (semantic-unmatched-syntax-overlay-p (car ol))
270            ol    (cdr ol)))
271    found))
272
273(defun semantic-show-unmatched-lex-tokens-fetch ()
274  "Fetch a list of unmatched lexical tokens from the current buffer.
275Uses the overlays which have accurate bounds, and rebuilds what was
276originally passed in."
277  (let ((ol (overlays-in (point-min) (point-max)))
278	(ustc nil))
279    (while ol
280      (if (semantic-unmatched-syntax-overlay-p (car ol))
281	  (setq ustc (cons (cons 'thing
282				 (cons (overlay-start (car ol))
283				       (overlay-end (car ol))))
284			   ustc)))
285      (setq ol (cdr ol)))
286    (nreverse ustc))
287  )
288
289(defun semantic-clean-unmatched-syntax-in-region (beg end)
290  "Remove all unmatched syntax overlays between BEG and END."
291  (let ((ol (overlays-in beg end)))
292    (while ol
293      (if (semantic-unmatched-syntax-overlay-p (car ol))
294	  (delete-overlay (car ol)))
295      (setq ol (cdr ol)))))
296
297(defsubst semantic-clean-unmatched-syntax-in-buffer ()
298  "Remove all unmatched syntax overlays found in current buffer."
299  (semantic-clean-unmatched-syntax-in-region
300   (point-min) (point-max)))
301
302(defsubst semantic-clean-token-of-unmatched-syntax (token)
303  "Clean the area covered by TOKEN of unmatched syntax markers."
304  (semantic-clean-unmatched-syntax-in-region
305   (semantic-tag-start token) (semantic-tag-end token)))
306
307(defun semantic-show-unmatched-syntax (syntax)
308  "Function set into `semantic-unmatched-syntax-hook'.
309This will highlight elements in SYNTAX as unmatched syntax."
310  ;; This is called when `semantic-show-unmatched-syntax-mode' is
311  ;; enabled.  Highlight the unmatched syntax, and then add a semantic
312  ;; property to that overlay so we can add it to the official list of
313  ;; semantic supported overlays.  This gets it cleaned up for errors,
314  ;; buffer cleaning, and the like.
315  (semantic-clean-unmatched-syntax-in-buffer) ;Clear previous highlighting
316  (if syntax
317      (let (o)
318        (while syntax
319          (setq o (make-overlay (semantic-lex-token-start (car syntax))
320                                (semantic-lex-token-end (car syntax))))
321          (overlay-put o 'semantic 'unmatched)
322          (overlay-put o 'face 'semantic-unmatched-syntax-face)
323          (setq syntax (cdr syntax))))
324    ))
325
326(defun semantic-next-unmatched-syntax (point &optional bound)
327  "Find the next overlay for unmatched syntax after POINT.
328Do not search past BOUND if non-nil."
329  (save-excursion
330    (goto-char point)
331    (let ((os point) (ol nil))
332      (while (and os (< os (or bound (point-max))) (not ol))
333	(setq os (next-overlay-change os))
334	(when os
335	  ;; Get overlays at position
336	  (setq ol (overlays-at os))
337	  ;; find the overlay that belongs to semantic
338	  ;; and starts at the found position.
339	  (while (and ol (listp ol))
340	    (and (semantic-unmatched-syntax-overlay-p (car ol))
341                 (setq ol (car ol)))
342	    (if (listp ol)
343                (setq ol (cdr ol))))))
344      ol)))
345
346(defvar semantic-show-unmatched-syntax-mode-map
347  (let ((km (make-sparse-keymap)))
348    (define-key km "\C-c,`" 'semantic-show-unmatched-syntax-next)
349    km)
350  "Keymap for command `semantic-show-unmatched-syntax-mode'.")
351
352;;;###autoload
353(define-minor-mode semantic-show-unmatched-syntax-mode
354  "Minor mode to highlight unmatched lexical syntax tokens.
355When a parser executes, some elements in the buffer may not match any
356parser rules.  These text characters are considered unmatched syntax.
357Often time, the display of unmatched syntax can expose coding
358problems before the compiler is run.
359
360The minor mode can be turned on only if semantic feature is
361available and the current buffer was set up for parsing.  Return
362non-nil if the minor mode is enabled.
363
364\\{semantic-show-unmatched-syntax-mode-map}"
365  :keymap semantic-show-unmatched-syntax-mode-map
366  (if semantic-show-unmatched-syntax-mode
367      (if (not (and (featurep 'semantic) (semantic-active-p)))
368          (progn
369            ;; Disable minor mode if semantic stuff not available
370            (setq semantic-show-unmatched-syntax-mode nil)
371            (error "Buffer %s was not set up for parsing"
372                   (buffer-name)))
373        ;; Add hooks
374        (add-hook 'semantic-unmatched-syntax-hook
375                  'semantic-show-unmatched-syntax nil t)
376	(add-hook 'semantic-pre-clean-token-hooks
377		  'semantic-clean-token-of-unmatched-syntax nil t)
378        ;; Show unmatched syntax elements
379	(if (not (semantic--umatched-syntax-needs-refresh-p))
380	    (semantic-show-unmatched-syntax
381	     (semantic-unmatched-syntax-tokens))))
382    ;; Remove hooks
383    (remove-hook 'semantic-unmatched-syntax-hook
384                 'semantic-show-unmatched-syntax t)
385    (remove-hook 'semantic-pre-clean-token-hooks
386		 'semantic-clean-token-of-unmatched-syntax t)
387    ;; Cleanup unmatched-syntax highlighting
388    (semantic-clean-unmatched-syntax-in-buffer)))
389
390(semantic-add-minor-mode 'semantic-show-unmatched-syntax-mode
391                         "u")
392
393(defun semantic-show-unmatched-syntax-next ()
394  "Move forward to the next occurrence of unmatched syntax."
395  (interactive)
396  (let ((o (semantic-next-unmatched-syntax (point))))
397    (if o
398	(goto-char (overlay-start o)))))
399
400
401;;;;
402;;;; Minor mode to display the parser state in the modeline.
403;;;;
404
405;;;###autoload
406(define-minor-mode global-semantic-show-parser-state-mode
407  "Toggle global use of option `semantic-show-parser-state-mode'."
408  :global t :group 'semantic
409  ;; Not needed because it's autoloaded instead.
410  ;; :require 'semantic/util-modes
411  (semantic-toggle-minor-mode-globally
412   'semantic-show-parser-state-mode
413   (if global-semantic-show-parser-state-mode 1 -1)))
414
415(defcustom semantic-show-parser-state-mode-hook nil
416  "Hook run at the end of function `semantic-show-parser-state-mode'."
417  :group 'semantic
418  :type 'hook)
419
420(defvar semantic-show-parser-state-mode-map
421  (let ((km (make-sparse-keymap)))
422    km)
423  "Keymap for show-parser-state minor mode.")
424
425;;;###autoload
426(define-minor-mode semantic-show-parser-state-mode
427  "Minor mode for displaying parser cache state in the modeline.
428The cache can be in one of three states.  They are
429Up to date, Partial reparse needed, and Full reparse needed.
430The state is indicated in the modeline with the following characters:
431 `-'  ->  The cache is up to date.
432 `!'  ->  The cache requires a full update.
433 `~'  ->  The cache needs to be incrementally parsed.
434 `%'  ->  The cache is not currently parsable.
435 `@'  ->  Auto-parse in progress (not set here.)
436
437The minor mode can be turned on only if semantic feature is
438available and the current buffer was set up for parsing.  Return
439non-nil if the minor mode is enabled."
440  :keymap semantic-show-parser-state-mode-map
441  (if semantic-show-parser-state-mode
442      (if (not (and (featurep 'semantic) (semantic-active-p)))
443          (progn
444            ;; Disable minor mode if semantic stuff not available
445            (setq semantic-show-parser-state-mode nil)
446            (error "Buffer %s was not set up for parsing"
447                   (buffer-name)))
448	;; Set up mode line
449
450	(when (not
451	       (memq 'semantic-show-parser-state-string mode-line-modified))
452	  (setq mode-line-modified
453		(append mode-line-modified
454			'(semantic-show-parser-state-string))))
455	;; Add hooks
456        (add-hook 'semantic-edits-new-change-functions
457                  'semantic-show-parser-state-marker nil t)
458	(add-hook 'semantic-edits-incremental-reparse-failed-hook
459		  'semantic-show-parser-state-marker nil t)
460	(add-hook 'semantic-after-partial-cache-change-hook
461		  'semantic-show-parser-state-marker nil t)
462	(add-hook 'semantic-after-toplevel-cache-change-hook
463		  'semantic-show-parser-state-marker nil t)
464	(semantic-show-parser-state-marker)
465
466	(add-hook 'semantic-before-auto-parse-hooks
467		  'semantic-show-parser-state-auto-marker nil t)
468	(add-hook 'semantic-after-auto-parse-hooks
469		  'semantic-show-parser-state-marker nil t)
470
471	(add-hook 'semantic-before-idle-scheduler-reparse-hook
472		  'semantic-show-parser-state-auto-marker nil t)
473	(add-hook 'semantic-after-idle-scheduler-reparse-hook
474		  'semantic-show-parser-state-marker nil t))
475    ;; Remove parts of mode line
476    (setq mode-line-modified
477	  (delq 'semantic-show-parser-state-string mode-line-modified))
478    ;; Remove hooks
479    (remove-hook 'semantic-edits-new-change-functions
480		 'semantic-show-parser-state-marker t)
481    (remove-hook 'semantic-edits-incremental-reparse-failed-hook
482		 'semantic-show-parser-state-marker t)
483    (remove-hook 'semantic-after-partial-cache-change-hook
484		 'semantic-show-parser-state-marker t)
485    (remove-hook 'semantic-after-toplevel-cache-change-hook
486		 'semantic-show-parser-state-marker t)
487
488    (remove-hook 'semantic-before-auto-parse-hooks
489		 'semantic-show-parser-state-auto-marker t)
490    (remove-hook 'semantic-after-auto-parse-hooks
491		 'semantic-show-parser-state-marker t)
492
493    (remove-hook 'semantic-before-idle-scheduler-reparse-hook
494		 'semantic-show-parser-state-auto-marker t)
495    (remove-hook 'semantic-after-idle-scheduler-reparse-hook
496		 'semantic-show-parser-state-marker t)))
497
498(semantic-add-minor-mode 'semantic-show-parser-state-mode
499                         "")
500
501(defvar semantic-show-parser-state-string nil
502  "String showing the parser state for this buffer.
503See `semantic-show-parser-state-marker' for details.")
504(make-variable-buffer-local 'semantic-show-parser-state-string)
505
506(defun semantic-show-parser-state-marker (&rest ignore)
507  "Set `semantic-show-parser-state-string' to indicate parser state.
508This marker is one of the following:
509 `-'  ->  The cache is up to date.
510 `!'  ->  The cache requires a full update.
511 `~'  ->  The cache needs to be incrementally parsed.
512 `%'  ->  The cache is not currently parsable.
513 `@'  ->  Auto-parse in progress (not set here.)
514Arguments IGNORE are ignored, and accepted so this can be used as a hook
515in many situations."
516  (setq semantic-show-parser-state-string
517	(cond ((semantic-parse-tree-needs-rebuild-p)
518	       "!")
519	      ((semantic-parse-tree-needs-update-p)
520	       "^")
521	      ((semantic-parse-tree-unparseable-p)
522	       "%")
523	      (t
524               "-")))
525  ;;(message "Setup mode line indicator to [%s]" semantic-show-parser-state-string)
526  )
527
528(defun semantic-show-parser-state-auto-marker ()
529  "Hook function run before an autoparse.
530Set up `semantic-show-parser-state-marker' to show `@'
531to indicate a parse in progress."
532  (unless (semantic-parse-tree-up-to-date-p)
533    (setq semantic-show-parser-state-string "@")
534    ;; For testing.
535    ;;(sit-for 1)
536    ))
537
538
539;;;;
540;;;; Minor mode to make function decls sticky.
541;;;;
542
543;;;###autoload
544(define-minor-mode global-semantic-stickyfunc-mode
545  "Toggle global use of option `semantic-stickyfunc-mode'."
546  :global t :group 'semantic :group 'semantic-modes
547  ;; Not needed because it's autoloaded instead.
548  ;; :require 'semantic/util-modes
549  (semantic-toggle-minor-mode-globally
550   'semantic-stickyfunc-mode (if global-semantic-stickyfunc-mode 1 -1)))
551
552(defcustom semantic-stickyfunc-mode-hook nil
553  "Hook run at the end of function `semantic-stickyfunc-mode'."
554  :group 'semantic
555  :type 'hook)
556
557(defvar semantic-stickyfunc-mode-map
558  (let ((km (make-sparse-keymap)))
559    (define-key km [ header-line down-mouse-1 ] 'semantic-stickyfunc-menu)
560    km)
561  "Keymap for stickyfunc minor mode.")
562
563(defvar semantic-stickyfunc-popup-menu nil
564  "Menu used if the user clicks on the header line used by stickyfunc mode.")
565
566(easy-menu-define
567  semantic-stickyfunc-popup-menu
568  semantic-stickyfunc-mode-map
569  "Stickyfunc Menu"
570  '("Stickyfunc Mode"  :visible (progn nil)
571    [ "Copy Headerline Tag" senator-copy-tag
572      :active (semantic-current-tag)
573      :help "Copy the current tag to the tag ring"]
574    [ "Kill Headerline Tag" senator-kill-tag
575      :active (semantic-current-tag)
576      :help "Kill tag text to the kill ring, and copy the tag to the tag ring"
577      ]
578    [ "Copy Headerline Tag to Register" senator-copy-tag-to-register
579      :active (semantic-current-tag)
580      :help "Copy the current tag to a register"
581      ]
582    [ "Narrow To Headerline Tag" senator-narrow-to-defun
583      :active (semantic-current-tag)
584      :help "Narrow to the bounds of the current tag"]
585    [ "Fold Headerline Tag" senator-fold-tag-toggle
586      :active (semantic-current-tag)
587      :style toggle
588      :selected (let ((tag (semantic-current-tag)))
589		  (and tag (semantic-tag-folded-p tag)))
590      :help "Fold the current tag to one line"
591      ]
592    "---"
593    [ "About This Header Line"
594      (lambda () (interactive)
595	(describe-function 'semantic-stickyfunc-mode)) t])
596  )
597
598(defcustom semantic-stickyfunc-indent-string
599  (if window-system
600      (concat
601       (condition-case nil
602	   ;; Test scroll bar location
603	   (let ((charwidth (frame-char-width))
604		 (scrollpos (frame-parameter (selected-frame)
605					     'vertical-scroll-bars))
606		 )
607	     (if (or (eq scrollpos 'left)
608		     ;; Now wait a minute.  If you turn scroll-bar-mode
609		     ;; on, then off, the new value is t, not left.
610		     ;; Will this mess up older emacs where the default
611		     ;; was on the right?  I don't think so since they don't
612		     ;; support a header line.
613		     (eq scrollpos t))
614		 (let ((w (when (boundp 'scroll-bar-width)
615			    (symbol-value 'scroll-bar-width))))
616
617		   (if (not w)
618		       (setq w (frame-parameter (selected-frame)
619						'scroll-bar-width)))
620
621		   ;; in 21.2, the frame parameter is sometimes empty
622		   ;; so we need to get the value here.
623		   (if (not w)
624		       (setq w (+ (get 'scroll-bar-width 'x-frame-parameter)
625				  ;; In 21.4, or perhaps 22.1 the x-frame
626				  ;; parameter is different from the frame
627				  ;; parameter by only 1 pixel.
628				  1)))
629
630		   (if (not w)
631		       "  "
632		     (setq w (+ 2 w))   ; Some sort of border around
633					; the scrollbar.
634		     (make-string (/ w charwidth) ? )))
635	       ""))
636	 (error ""))
637       (condition-case nil
638	   ;; Test fringe size.
639	   (let* ((f (window-fringes))
640		  (fw (car f))
641		  (numspace (/ fw (frame-char-width)))
642		  )
643	     (make-string numspace ? ))
644	 (error
645	  ;; Well, the fancy new Emacs functions failed.  Try older
646	  ;; tricks.
647	  (condition-case nil
648	      ;; I'm not so sure what's up with the 21.1-21.3 fringe.
649	      ;; It looks to be about 1 space wide.
650	      (if (get 'fringe 'face)
651		  " "
652		"")
653	    (error ""))))
654       )
655    ;; Not Emacs or a window system means no scrollbar or fringe,
656    ;; and perhaps not even a header line to worry about.
657    "")
658  "String used to indent the stickyfunc header.
659Customize this string to match the space used by scrollbars and
660fringe so it does not appear that the code is moving left/right
661when it lands in the sticky line."
662  :group 'semantic
663  :type 'string)
664
665(defvar semantic-stickyfunc-old-hlf nil
666  "Value of the header line when entering stickyfunc mode.")
667
668(defconst semantic-stickyfunc-header-line-format
669  '(:eval (list
670	   ;; Magic bit I found on emacswiki.
671	   (propertize " " 'display '((space :align-to 0)))
672	   (semantic-stickyfunc-fetch-stickyline)))
673  "The header line format used by stickyfunc mode.")
674
675;;;###autoload
676(define-minor-mode semantic-stickyfunc-mode
677  "Minor mode to show the title of a tag in the header line.
678Enables/disables making the header line of functions sticky.
679A function (or other tag class specified by
680`semantic-stickyfunc-sticky-classes') has a header line, meaning the
681first line which describes the rest of the construct.  This first
682line is what is displayed in the header line.
683
684The minor mode can be turned on only if semantic feature is
685available and the current buffer was set up for parsing.  Return
686non-nil if the minor mode is enabled."
687  ;; Don't need indicator.  It's quite visible
688  :keymap semantic-stickyfunc-mode-map
689  (if semantic-stickyfunc-mode
690      (progn
691	(unless (and (featurep 'semantic) (semantic-active-p))
692	  ;; Disable minor mode if semantic stuff not available
693	  (setq semantic-stickyfunc-mode nil)
694	  (error "Buffer %s was not set up for parsing" (buffer-name)))
695	(unless (boundp 'header-line-format)
696	  ;; Disable if there are no header lines to use.
697	  (setq semantic-stickyfunc-mode nil)
698	  (error "Sticky Function mode requires Emacs"))
699	;; Enable the mode
700	;; Save previous buffer local value of header line format.
701	(when (and (local-variable-p 'header-line-format (current-buffer))
702		   (not (eq header-line-format
703			    semantic-stickyfunc-header-line-format)))
704	  (set (make-local-variable 'semantic-stickyfunc-old-hlf)
705	       header-line-format))
706	(setq header-line-format semantic-stickyfunc-header-line-format))
707    ;; Disable sticky func mode
708    ;; Restore previous buffer local value of header line format if
709    ;; the current one is the sticky func one.
710    (when (eq header-line-format semantic-stickyfunc-header-line-format)
711      (kill-local-variable 'header-line-format)
712      (when (local-variable-p 'semantic-stickyfunc-old-hlf (current-buffer))
713	(setq header-line-format semantic-stickyfunc-old-hlf)
714	(kill-local-variable 'semantic-stickyfunc-old-hlf)))))
715
716(defvar semantic-stickyfunc-sticky-classes
717  '(function type)
718  "List of tag classes which stickyfunc will display in the header line.")
719(make-variable-buffer-local 'semantic-stickyfunc-sticky-classes)
720
721(defcustom semantic-stickyfunc-show-only-functions-p nil
722  "Non-nil means don't show lines that aren't part of a tag.
723If this is nil, then comments or other text between tags that is
7241 line above the top of the current window will be shown."
725  :group 'semantic
726  :type 'boolean)
727
728(defun semantic-stickyfunc-tag-to-stick ()
729  "Return the tag to stick at the current point."
730  (let ((tags (nreverse (semantic-find-tag-by-overlay (point)))))
731    ;; Get rid of non-matching tags.
732    (while (and tags
733		(not (member
734		      (semantic-tag-class (car tags))
735		      semantic-stickyfunc-sticky-classes))
736		)
737      (setq tags (cdr tags)))
738    (car tags)))
739
740(defun semantic-stickyfunc-fetch-stickyline ()
741  "Make the function at the top of the current window sticky.
742Capture its function declaration, and place it in the header line.
743If there is no function, disable the header line."
744  (save-excursion
745    (goto-char (window-start (selected-window)))
746    (let* ((noshow (bobp))
747	   (str
748	    (progn
749	      (forward-line -1)
750	      (end-of-line)
751	      ;; Capture this function
752	      (let* ((tag (semantic-stickyfunc-tag-to-stick)))
753		;; TAG is nil if there was nothing of the appropriate type there.
754		(if (not tag)
755		    ;; Set it to be the text under the header line
756		    (if noshow
757			""
758		      (if semantic-stickyfunc-show-only-functions-p ""
759			(buffer-substring (point-at-bol) (point-at-eol))
760			))
761		  ;; Go get the first line of this tag.
762		  (goto-char (semantic-tag-start tag))
763		  ;; Klaus Berndl <klaus.berndl@sdm.de>:
764		  ;; goto the tag name; this is especially needed for languages
765		  ;; like c++ where an often used style is like:
766		  ;;     void
767		  ;;     ClassX::methodM(arg1...)
768		  ;;     {
769		  ;;       ...
770		  ;;     }
771		  ;; Without going to the tag-name we would get"void" in the
772		  ;; header line which is IMHO not really useful
773		  (search-forward (semantic-tag-name tag) nil t)
774		  (buffer-substring (point-at-bol) (point-at-eol))
775		  ))))
776	   (start 0))
777      (while (string-match "%" str start)
778	(setq str (replace-match "%%" t t str 0)
779	      start (1+ (match-end 0)))
780	)
781      ;; In 21.4 (or 22.1) the header doesn't expand tabs.  Hmmmm.
782      ;; We should replace them here.
783      ;;
784      ;; This hack assumes that tabs are kept smartly at tab boundaries
785      ;; instead of in a tab boundary where it might only represent 4 spaces.
786      (while (string-match "\t" str start)
787	(setq str (replace-match "        " t t str 0)))
788      str)))
789
790(defun semantic-stickyfunc-menu (event)
791  "Popup a menu that can help a user understand stickyfunc-mode.
792Argument EVENT describes the event that caused this function to be called."
793  (interactive "e")
794  (let* ((startwin (selected-window))
795	 (win (car (car (cdr event))))
796	 )
797    (select-window win t)
798    (save-excursion
799      (goto-char (window-start win))
800      (sit-for 0)
801      (popup-menu semantic-stickyfunc-popup-menu event)
802      )
803    (select-window startwin)))
804
805
806(semantic-add-minor-mode 'semantic-stickyfunc-mode
807                         "") ;; Don't need indicator.  It's quite visible
808
809
810
811;;;;
812;;;; Minor mode to make highlight the current function
813;;;;
814
815;; Highlight the first like of the function we are in if it is different
816;; from the tag going off the top of the screen.
817
818;;;###autoload
819(define-minor-mode global-semantic-highlight-func-mode
820  "Toggle global use of option `semantic-highlight-func-mode'."
821  :global t :group 'semantic :group 'semantic-modes
822  ;; Not needed because it's autoloaded instead.
823  ;; :require 'semantic/util-modes
824  (semantic-toggle-minor-mode-globally
825   'semantic-highlight-func-mode
826   (if global-semantic-highlight-func-mode 1 -1)))
827
828(defcustom semantic-highlight-func-mode-hook nil
829  "Hook run at the end of function `semantic-highlight-func-mode'."
830  :group 'semantic
831  :type 'hook)
832
833(defvar semantic-highlight-func-mode-map
834  (let ((km (make-sparse-keymap)))
835    (define-key km [mouse-3] 'semantic-highlight-func-menu)
836    km)
837  "Keymap for highlight-func minor mode.")
838
839(defvar semantic-highlight-func-popup-menu nil
840  "Menu used if the user clicks on the header line used by `semantic-highlight-func-mode'.")
841
842(easy-menu-define
843  semantic-highlight-func-popup-menu
844  semantic-highlight-func-mode-map
845  "Highlight-Func Menu"
846  '("Highlight-Func Mode"  :visible (progn nil)
847    [ "Copy Tag" senator-copy-tag
848      :active (semantic-current-tag)
849      :help "Copy the current tag to the tag ring"]
850    [ "Kill Tag" senator-kill-tag
851      :active (semantic-current-tag)
852      :help "Kill tag text to the kill ring, and copy the tag to the tag ring"
853      ]
854    [ "Copy Tag to Register" senator-copy-tag-to-register
855      :active (semantic-current-tag)
856      :help "Copy the current tag to a register"
857      ]
858    [ "Narrow To Tag" senator-narrow-to-defun
859      :active (semantic-current-tag)
860      :help "Narrow to the bounds of the current tag"]
861    [ "Fold Tag" senator-fold-tag-toggle
862      :active (semantic-current-tag)
863      :style toggle
864      :selected (let ((tag (semantic-stickyfunc-tag-to-stick)))
865		  (and tag (semantic-tag-folded-p tag)))
866      :help "Fold the current tag to one line"
867      ]
868    "---"
869    [ "About This Tag" semantic-describe-tag t])
870  )
871
872(defun semantic-highlight-func-menu (event)
873  "Popup a menu that displays things to do to the current tag.
874Argument EVENT describes the event that caused this function to be called."
875  (interactive "e")
876  (let* ((startwin (selected-window))
877	 (win (semantic-event-window event))
878	 )
879    (select-window win t)
880    (save-excursion
881      ;(goto-char (window-start win))
882      (mouse-set-point event)
883      (sit-for 0)
884      (popup-menu semantic-highlight-func-popup-menu)
885      )
886    (select-window startwin)))
887
888(defvar semantic-highlight-func-ct-overlay nil
889  "Overlay used to highlight the tag the cursor is in.")
890(make-variable-buffer-local 'semantic-highlight-func-ct-overlay)
891
892(defface semantic-highlight-func-current-tag-face
893  '((((class color) (background dark))
894     ;; Put this back to something closer to black later.
895     (:background "gray20"))
896    (((class color) (background light))
897     (:background "gray90")))
898  "Face used to show the top of current function."
899  :group 'semantic-faces)
900
901;;;###autoload
902(define-minor-mode semantic-highlight-func-mode
903  "Minor mode to highlight the first line of the current tag.
904Enables/disables making the current function's first line light up.
905A function (or other tag class specified by
906`semantic-stickyfunc-sticky-classes') is highlighted, meaning the
907first line which describes the rest of the construct.
908
909See `semantic-stickyfunc-mode' for putting a function in the
910header line.  This mode recycles the stickyfunc configuration
911classes list.
912
913The minor mode can be turned on only if semantic feature is
914available and the current buffer was set up for parsing.  Return
915non-nil if the minor mode is enabled."
916  :lighter nil ;; Don't need indicator.  It's quite visible.
917  (if semantic-highlight-func-mode
918      (progn
919	(unless (and (featurep 'semantic) (semantic-active-p))
920	  ;; Disable minor mode if semantic stuff not available
921	  (setq semantic-highlight-func-mode nil)
922	  (error "Buffer %s was not set up for parsing" (buffer-name)))
923	;; Setup our hook
924	(add-hook 'post-command-hook
925                  'semantic-highlight-func-highlight-current-tag nil t))
926    ;; Disable highlight func mode
927    (remove-hook 'post-command-hook
928                 'semantic-highlight-func-highlight-current-tag t)
929    (semantic-highlight-func-highlight-current-tag t)))
930
931(defun semantic-highlight-func-highlight-current-tag (&optional disable)
932  "Highlight the current tag under point.
933Optional argument DISABLE will turn off any active highlight.
934If the current tag for this buffer is different from the last time this
935function was called, move the overlay."
936  (when (and (not (minibufferp))
937	     (or (not semantic-highlight-func-ct-overlay)
938		 (eq (overlay-buffer
939		      semantic-highlight-func-ct-overlay)
940		     (current-buffer))))
941    (let* ((tag (semantic-stickyfunc-tag-to-stick))
942	   (ol semantic-highlight-func-ct-overlay))
943      (when (not ol)
944	;; No overlay in this buffer.  Make one.
945	(setq ol (make-overlay (point-min) (point-min)
946			       (current-buffer) t nil))
947	(overlay-put ol 'highlight-func t)
948	(overlay-put ol 'face 'semantic-highlight-func-current-tag-face)
949	(overlay-put ol 'keymap semantic-highlight-func-mode-map)
950	(overlay-put ol 'help-echo
951			      "Current Function : mouse-3 - Context menu")
952	(setq semantic-highlight-func-ct-overlay ol)
953	)
954
955      ;; TAG is nil if there was nothing of the appropriate type there.
956      (if (or (not tag) disable)
957	  ;; No tag, make the overlay go away.
958	  (progn
959	    (overlay-put ol 'tag nil)
960	    (move-overlay ol (point-min) (point-min) (current-buffer)))
961
962	;; We have a tag, if it is the same, do nothing.
963	(unless (eq (overlay-get ol 'tag) tag)
964	  (save-excursion
965	    (goto-char (semantic-tag-start tag))
966	    (search-forward (semantic-tag-name tag) nil t)
967	    (overlay-put ol 'tag tag)
968	    (move-overlay ol (point-at-bol) (point-at-eol)))))))
969  nil)
970
971(semantic-add-minor-mode 'semantic-highlight-func-mode
972                         "") ;; Don't need indicator.  It's quite visible
973
974(provide 'semantic/util-modes)
975
976;; Local variables:
977;; generated-autoload-file: "loaddefs.el"
978;; generated-autoload-load-name: "semantic/util-modes"
979;; End:
980
981;;; semantic/util-modes.el ends here
982