diff options
| author | Alan Mackenzie | 2008-04-02 21:15:53 +0000 |
|---|---|---|
| committer | Alan Mackenzie | 2008-04-02 21:15:53 +0000 |
| commit | bf9397ef96d46d867bcb2a74105faac481d5b6a4 (patch) | |
| tree | 3d133fce695e7aab3ac3b5a4b74a58cf859a0456 | |
| parent | 3c3006795165c9341990ce8277a56b4b05f6e1f7 (diff) | |
| download | emacs-bf9397ef96d46d867bcb2a74105faac481d5b6a4.tar.gz emacs-bf9397ef96d46d867bcb2a74105faac481d5b6a4.zip | |
(c-defun-name, c-cpp-define-name): New optimised functions to get the
name of the current defun/macro.
| -rw-r--r-- | lisp/progmodes/cc-cmds.el | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/lisp/progmodes/cc-cmds.el b/lisp/progmodes/cc-cmds.el index 7afe6e6c243..ba1ce001473 100644 --- a/lisp/progmodes/cc-cmds.el +++ b/lisp/progmodes/cc-cmds.el | |||
| @@ -1679,6 +1679,72 @@ the open-parenthesis that starts a defun; see `beginning-of-defun'." | |||
| 1679 | (c-keep-region-active) | 1679 | (c-keep-region-active) |
| 1680 | (= arg 0))) | 1680 | (= arg 0))) |
| 1681 | 1681 | ||
| 1682 | (defun c-defun-name () | ||
| 1683 | "Return the name of the current defun, or NIL if there isn't one. | ||
| 1684 | \"Defun\" here means a function, or other top level construct | ||
| 1685 | with a brace block." | ||
| 1686 | (interactive) | ||
| 1687 | (c-save-buffer-state | ||
| 1688 | (beginning-of-defun-function end-of-defun-function | ||
| 1689 | where pos name-end) | ||
| 1690 | |||
| 1691 | (save-excursion | ||
| 1692 | ;; Move back out of any macro/comment/string we happen to be in. | ||
| 1693 | (c-beginning-of-macro) | ||
| 1694 | (setq pos (c-literal-limits)) | ||
| 1695 | (if pos (goto-char (car pos))) | ||
| 1696 | |||
| 1697 | (setq where (c-where-wrt-brace-construct)) | ||
| 1698 | |||
| 1699 | ;; Move to the beginning of the current defun, if any, if we're not | ||
| 1700 | ;; already there. | ||
| 1701 | (if (eq where 'outwith-function) | ||
| 1702 | nil | ||
| 1703 | (unless (eq where 'at-header) | ||
| 1704 | (c-backward-to-nth-BOF-{ 1 where) | ||
| 1705 | (c-beginning-of-decl-1)) | ||
| 1706 | |||
| 1707 | ;; Pick out the defun name, according to the type of defun. | ||
| 1708 | (cond | ||
| 1709 | ((and (looking-at c-type-prefix-key) | ||
| 1710 | (progn (c-forward-token-2 2) ; over "struct foo " | ||
| 1711 | (eq (char-after) ?\{))) | ||
| 1712 | ;; struct, union, enum, or similar: | ||
| 1713 | (c-backward-syntactic-ws) | ||
| 1714 | (setq name-end (point)) | ||
| 1715 | (buffer-substring-no-properties | ||
| 1716 | (progn | ||
| 1717 | (c-backward-token-2 2) | ||
| 1718 | (point)) | ||
| 1719 | name-end)) | ||
| 1720 | |||
| 1721 | ((looking-at "DEFUN\\_>") | ||
| 1722 | ;; DEFUN ("file-name-directory", Ffile_name_directory, Sfile_name_directory, ...) ==> Ffile_name_directory | ||
| 1723 | ;; DEFUN(POSIX::STREAM-LOCK, stream lockp &key BLOCK SHARED START LENGTH) ==> POSIX::STREAM-LOCK | ||
| 1724 | (down-list 1) | ||
| 1725 | (c-forward-syntactic-ws) | ||
| 1726 | (when (eq (char-after) ?\") | ||
| 1727 | (forward-sexp 1) | ||
| 1728 | (c-forward-token-2)) ; over the comma and following WS. | ||
| 1729 | (buffer-substring-no-properties | ||
| 1730 | (point) | ||
| 1731 | (progn | ||
| 1732 | (c-forward-token-2) | ||
| 1733 | (c-backward-syntactic-ws) | ||
| 1734 | (point)))) | ||
| 1735 | |||
| 1736 | (t | ||
| 1737 | ;; Normal function or initializer. | ||
| 1738 | (when (c-syntactic-re-search-forward "[{(]" nil t) | ||
| 1739 | (backward-char) | ||
| 1740 | (c-backward-syntactic-ws) | ||
| 1741 | (when (eq (char-before) ?\=) ; struct foo bar = {0, 0} ; | ||
| 1742 | (c-backward-token-2) | ||
| 1743 | (c-backward-syntactic-ws)) | ||
| 1744 | (setq name-end (point)) | ||
| 1745 | (c-backward-token-2) | ||
| 1746 | (buffer-substring-no-properties (point) name-end)))))))) | ||
| 1747 | |||
| 1682 | (defun c-declaration-limits (near) | 1748 | (defun c-declaration-limits (near) |
| 1683 | ;; Return a cons of the beginning and end positions of the current | 1749 | ;; Return a cons of the beginning and end positions of the current |
| 1684 | ;; top level declaration or macro. If point is not inside any then | 1750 | ;; top level declaration or macro. If point is not inside any then |
| @@ -1810,6 +1876,15 @@ function does not require the declaration to contain a brace block." | |||
| 1810 | (goto-char (car decl-limits)) | 1876 | (goto-char (car decl-limits)) |
| 1811 | (push-mark (cdr decl-limits) nil t)))) | 1877 | (push-mark (cdr decl-limits) nil t)))) |
| 1812 | 1878 | ||
| 1879 | (defun c-cpp-define-name () | ||
| 1880 | "Return the name of the current CPP macro, or NIL if we're not in one." | ||
| 1881 | (interactive) | ||
| 1882 | (save-excursion | ||
| 1883 | (and c-opt-cpp-macro-define-start | ||
| 1884 | (c-beginning-of-macro) | ||
| 1885 | (looking-at c-opt-cpp-macro-define-start) | ||
| 1886 | (match-string-no-properties 1)))) | ||
| 1887 | |||
| 1813 | 1888 | ||
| 1814 | ;; Movement by statements. | 1889 | ;; Movement by statements. |
| 1815 | (defun c-in-comment-line-prefix-p () | 1890 | (defun c-in-comment-line-prefix-p () |