📄 gtags.el
字号:
(defun gtags-find-with-idutils () "Input pattern, search with id-utils(1) and move to the locations." (interactive) (gtags-find-with "I"))(defun gtags-find-file () "Input pattern and move to the top of the file." (interactive) (let (tagname prompt input) (setq prompt "Find files: ") (setq input (read-string prompt)) (if (not (equal "" input)) (setq tagname input)) (gtags-push-context) (gtags-goto-tag tagname "P")))(defun gtags-parse-file () "Input file name, parse it and show object list." (interactive) (let (tagname prompt input) (setq input (read-file-name "Parse file: " nil nil t (file-name-nondirectory buffer-file-name))) (if (not (equal "" input)) (setq tagname input)) (gtags-push-context) (gtags-goto-tag tagname "f")))(defun gtags-find-tag-from-here () "Get the expression as a tagname around here and move there." (interactive) (let (tagname flag) (setq tagname (gtags-current-token)) (if (gtags-is-function) (if (gtags-is-definition) (setq flag "r") (setq flag "")) (setq flag "s")) (if (not tagname) nil (gtags-push-context) (gtags-goto-tag tagname flag)))); This function doesn't work with mozilla.; But I will support it in the near future.(defun gtags-display-browser () "Display current screen on hypertext browser." (interactive) (let (lno) (if (= 0 (count-lines (point-min) (point-max))) nil (save-excursion (end-of-line) (if (equal (point-min) (point)) (setq lno 1) (setq lno (count-lines (point-min) (point))))) (call-process "gozilla" nil t nil (concat "+" (number-to-string lno)) buffer-file-name))))(defun gtags-find-tag-by-event (event) "Get the expression as a tagname around here and move there." (interactive "e") (let (tagname flag) (if (= 0 (count-lines (point-min) (point-max))) (progn (setq tagname "main") (setq flag "")) (if gtags-running-xemacs (goto-char (event-point event)) (select-window (posn-window (event-end event))) (set-buffer (window-buffer (posn-window (event-end event)))) (goto-char (posn-point (event-end event)))) (setq tagname (gtags-current-token)) (if (gtags-is-function) (if (gtags-is-definition) (setq flag "r") (setq flag "")) (setq flag "s"))) (if (not tagname) nil (gtags-push-context) (gtags-goto-tag tagname flag))))(defun gtags-select-tag () "Select a tag in [GTAGS SELECT MODE] and move there." (interactive) (gtags-push-context) (gtags-select-it nil))(defun gtags-select-tag-by-event (event) "Select a tag in [GTAGS SELECT MODE] and move there." (interactive "e") (if gtags-running-xemacs (goto-char (event-point event)) (select-window (posn-window (event-end event))) (set-buffer (window-buffer (posn-window (event-end event)))) (goto-char (posn-point (event-end event)))) (gtags-push-context) (gtags-select-it nil))(defun gtags-pop-stack () "Move to previous point on the stack." (interactive) (let (delete context buffer) (if (and (not (equal gtags-current-buffer nil)) (not (equal gtags-current-buffer (current-buffer)))) (switch-to-buffer gtags-current-buffer) (if (not (gtags-exist-in-stack (current-buffer))) (setq delete t)) (setq context (gtags-pop-context)) (if (not context) (message "The tags stack is empty.") (if delete (kill-buffer (current-buffer))) (switch-to-buffer (nth 0 context)) (setq gtags-current-buffer (current-buffer)) (goto-char (nth 1 context))))));;;; common function;;;; find with grep or id-utils.(defun gtags-find-with (flag) (let (tagname prompt input) (setq tagname (gtags-current-token)) (if tagname (setq prompt (concat "Find pattern: (default " tagname ") ")) (setq prompt "Find pattern: ")) (setq input (completing-read prompt gtags-complete-list nil nil nil gtags-history-list)) (if (not (equal "" input)) (setq tagname input)) (gtags-push-context) (gtags-goto-tag tagname flag)));; goto tag's point(defun gtags-goto-tag (tagname flag) (let (save prefix buffer lines) (setq save (current-buffer)) (cond ((equal flag "P") (setq prefix "(P)")) ((equal flag "g") (setq prefix "(GREP)")) ((equal flag "I") (setq prefix "(IDUTILS)")) ((equal flag "s") (setq prefix "(S)")) ((equal flag "r") (setq prefix "(R)")) (t (setq prefix "(D)"))) ;; load tag (setq buffer (generate-new-buffer (generate-new-buffer-name (concat "*GTAGS SELECT* " prefix tagname)))) (set-buffer buffer) (message "Searching %s ..." tagname) (if (not (= 0 (call-process "global" nil t nil (concat "-ax" flag) tagname))) (progn (message (buffer-substring (point-min)(1- (point-max)))) (gtags-pop-context)) (goto-char (point-min)) (setq lines (count-lines (point-min) (point-max))) (cond ((= 0 lines) (cond ((equal flag "P") (message "%s: path not found" tagname)) ((equal flag "g") (message "%s: pattern not found" tagname)) ((equal flag "I") (message "%s: token not found" tagname)) ((equal flag "s") (message "%s: symbol not found" tagname)) (t (message "%s: tag not found" tagname))) (gtags-pop-context) (kill-buffer buffer) (set-buffer save)) ((= 1 lines) (message "Searching %s ... Done" tagname) (gtags-select-it t)) (t (switch-to-buffer buffer) (gtags-select-mode))))));; select a tag line from lines(defun gtags-select-it (delete) (let (line file) ;; get context from current tag line (beginning-of-line) (if (not (looking-at "[^ \t]+[ \t]+\\([0-9]+\\)[ \t]\\([^ \t]+\\)[ \t]")) (gtags-pop-context) (setq line (string-to-number (gtags-match-string 1))) (setq file (gtags-match-string 2)) (if delete (kill-buffer (current-buffer))) ;; move to the context (if gtags-read-only (find-file-read-only file) (find-file file)) (setq gtags-current-buffer (current-buffer)) (goto-line line) (gtags-mode 1))));; make complete list(defun gtags-make-complete-list () "Make tag name list for completion." (interactive) (save-excursion (message "Making completion list ...") (setq gtags-complete-list (make-vector 63 0)) (set-buffer (generate-new-buffer "*Completions*")) (call-process "global" nil t nil "-c") (goto-char (point-min)) (while (looking-at gtags-symbol-regexp) (intern (gtags-match-string 0) gtags-complete-list) (forward-line)) (message "Making completion list ... Done") (kill-buffer (current-buffer))));;;###autoload(defun gtags-mode (&optional forces) "Toggle Gtags mode, a minor mode for browsing source code using GLOBAL.Input tag name and move to the definition. \\[gtags-find-tag]Input tag name and move to the referenced point. \\[gtags-find-rtag]Input symbol and move to the locations. \\[gtags-find-symbol]Input pattern, search with grep(1) and move to the locations. \\[gtags-find-with-grep]Input pattern, search with id-utils(1) and move to the locations. \\[gtags-find-with-idutils]Input pattern and move to the top of the file. \\[gtags-find-file]Get the expression as a tagname around here and move there. \\[gtags-find-tag-from-here]Display current screen on hypertext browser. \\[gtags-display-browser]Get the expression as a tagname around here and move there. \\[gtags-find-tag-by-event]Move to previous point on the stack. \\[gtags-pop-stack]Make tag name list for completion. \\[gtags-make-complete-list]Key definitions:\\{gtags-mode-map}Turning on Gtags mode calls the value of the variable `gtags-mode-hook'with no args, if that value is non-nil." (interactive) (or (assq 'gtags-mode minor-mode-alist) (setq minor-mode-alist (cons '(gtags-mode " Gtags") minor-mode-alist))) (or (assq 'gtags-mode minor-mode-map-alist) (setq minor-mode-map-alist (cons (cons 'gtags-mode gtags-mode-map) minor-mode-map-alist))) (setq gtags-mode (if (null forces) (not gtags-mode) (> (prefix-numeric-value forces) 0))) (run-hooks 'gtags-mode-hook));; make gtags select-mode(defun gtags-select-mode () "Major mode for choosing a tag from tags list.Select a tag in tags list and move there. \\[gtags-select-tag]Move to previous point on the stack. \\[gtags-pop-stack]Key definitions:\\{gtags-select-mode-map}Turning on Gtags-Select mode calls the value of the variable`gtags-select-mode-hook' with no args, if that value is non-nil." (interactive) (kill-all-local-variables) (use-local-map gtags-select-mode-map) (setq buffer-read-only t truncate-lines t major-mode 'gtags-select-mode mode-name "Gtags-Select") (setq gtags-current-buffer (current-buffer)) (goto-char (point-min)) (message "[GTAGS SELECT MODE] %d lines" (count-lines (point-min) (point-max))) (run-hooks 'gtags-select-mode-hook));;; gtags.el ends here
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -