changelog shortlog tags changeset files revisions annotate raw

c-funcs.el

changeset 66: 5b737eefe5ea
parent:93b4eae80b38
author: kim.vanwyk
date: Wed Nov 10 15:19:03 2010 +0200 (18 months ago)
permissions: -rw-r--r--
description: Adding CSharp Mode and Google Weather
1(defun define-block(start end defname)
2"Adds
3#ifdef <name>
4...
5#endif /*<name>*/
6to the selected block of text. Prompts for <name>"
7(interactive "r
8sEnter Define Name:")
9(narrow-to-region start end)
10(goto-char (point-min))
11(insert (concat "#ifdef " defname "\n"))
12(goto-char (- (point-max) 1))
13(insert (concat "\n#endif /*" defname "*/"))
14(widen)
15)
16
17(defun which-case ()
18 "Displays the label of the nearest case clause.
19Currently limited in that it will return the name of the previous case, reegardless of whether point is currently in that case or not"
20
21 (interactive)
22 (save-excursion
23 (search-backward-regexp "case \\(.*\\):")
24 (message (concat "Current case: " (match-string 1)))
25 )
26)
27
28;; Provide a list of all the functions in the current c file
29(defvar funck-command (concat "ctags -x \"--c-kinds=f\" -f -") "The command run by the funck function.")
30(defvar funck-use-search-in-buffer-name t
31"If non-nil, use the search string in the ack buffer's name.")
32(define-compilation-mode funck-mode "Funck"
33 "Specialization of compilation-mode for use with funck."
34 nil)
35(defvar funck-regexp-alist '(("^\\(.*\\) function\s *\\([0-9]*\\) \\([A-Za-z\\./_:-]*\\) \\(.*\\)"
36 3 2 nil nil nil)))
37
38;;'(("\\([a-z_-]*\\)\\s *\\(function\\)\\s *\\([0-9]*\\) \\([A-Za-z.:/]*\\)" 4 3)
39(defun funck ()
40 "Run funck, with user-specified ARGS, and collect output in a buffer.
41While funck runs asynchronously, you can use the \\[next-error] command to
42find the text that ack hits refer to. The command actually run is
43defined by the funck-command variable."
44 (interactive)
45 (let (compile-command
46 (compilation-error-regexp-alist funck-regexp-alist)
47 (compilation-directory default-directory)
48 (funck-full-buffer-name (concat "*funck-" args "*")))
49;; (save-some-buffers (not compilation-ask-about-save) nil)
50 (compilation-start (concat funck-command " " args) 'funck-mode
51 (when funck-use-search-in-buffer-name
52 (function (lambda (ignore)
53 funck-full-buffer-name)))
54 (regexp-quote args))))
55
56(defun cfuncs-toggle-define ()
57 "toggles the first #define found on the current line to #undef, or vice-versa"
58 (interactive)
59 (save-exc ursion
60 (narrow-to-region (point-at-bol) (point-at-eol))
61 (beginning-of-buffer)
62 (if (search-forward "#" nil t)
63 (if (search-forward "define" nil t)
64 (replace-match "undef")
65 (if (search-forward "undef" nil t)
66 (replace-match "define")
67 )))
68 (widen)
69 ))
70
71;; (defun find-c-function ()
72;; "Find a c function name, searching forward from current point.
73;; Depends on the function body to have a brace in column 1 underneath the line holding the function name. "
74;; (interactive)
75;; ;; Find the function name, by finding text between a space and a (possible) space-opening bracket combo, which does not have a ; at the end
76;; (if (search-forward-regexp " \\(\\w+?\\) *(.*?)[^;]*
77;; {" nil t)
78;; (progn
79;; ;; Set match strng and function beginning pos to list
80;; ((setq retlist (list (match-string 1) (line-beginning-position)))
81;; ;; find the end pos of the funct, a closing brace in col 1
82;; (if (search-forward-regexp "^}" nil t)
83;; ;; match found, use match pos
84;; (append retlist (list (match-end 0)))
85;; ;; match not found, append nil
86;; (append retlist '(nil)))
87;; ))
88;; ;; No match found, return a list of nils
89;; (setq retlist '(nil nil nil)))
90;; (message "%s %d %d" retlist)
91;; (retlist)
92;; )
93
94(defun decorate-c-functions ()
95"Decorate all c functions in active buffer , placing their names in comments at the end of each function, if there is no text there already
96Depends on the function body to have a brace in column 1 underneath the line holding the function name.
97Also currently depends on there being no comments at the end of the function line"
98(interactive)
99;; Find the function name, by finding text between a space and a (possible) space-opening bracket combo, which does not have a ; at the end
100(while (search-forward-regexp " \\([A-Za-z0-9_]+?\\) *(.*?) *?
101{" nil t)
102 (message (match-string 1))
103 ;; Set match strng and function beginning pos to list
104 ;; find the end pos of the funct, a closing brace in col 1
105 (setq func (match-string 1))
106 (if (search-forward-regexp "^} *$" nil t)
107 ;; match found, use match pos
108 (progn
109 (end-of-line)
110 (insert (concat " /* " func " */")))
111 )
112))
113
114(defun decorate-c-functions-in-file (fpath)
115"Decorate all the c functions in the provided file path"
116(interactive "f")
117(save-excursion)
118(let (fname buf opened oldpoint)
119 (setq fname (file-name-nondirectory fpath))
120 (setq buf (find-file fpath))
121 ;; determine in buf already open
122 (if (equal (point) (point-min))
123 ;; buffer not opened before
124 (setq opened nil)
125 (progn
126 ;; buffer already open, mark as such by storing point and move to beginning of buffer
127 (setq opened t)
128 (setq oldpoint (point))
129 (goto-char (point-min))
130 ))
131 (decorate-c-functions)
132 (save-buffer)
133 (if (equal opened t)
134 ;; buffer was opened before, restore char
135 (goto-char oldpoint)
136 ;; Buffer was created for this purpose, kill it
137 (kill-buffer buf))
138 )
139)
140
141(defun indent-c-functions-in-file (fpath)
142"Indent all the c functions in the provided file path"
143(interactive "f")
144(save-excursion)
145(let (fname buf opened oldpoint)
146 (setq fname (file-name-nondirectory fpath))
147 (setq buf (find-file fpath))
148 ;; determine in buf already open
149 (if (equal (point) (point-min))
150 ;; buffer not opened before
151 (setq opened nil)
152 (progn
153 ;; buffer already open, mark as such by storing point and move to beginning of buffer
154 (setq opened t)
155 (setq oldpoint (point))
156 (goto-char (point-min))
157 ))
158 (c-mode)
159 (indent-region (point-min) (point-max) nil)
160 (save-buffer)
161 (if (equal opened t)
162 ;; buffer was opened before, restore char
163 (goto-char oldpoint)
164 ;; Buffer was created for this purpose, kill it
165 (kill-buffer buf))
166 )
167)
168
169(provide 'c-funcs)