]> code.delx.au - gnu-emacs/blob - lisp/cedet/srecode/args.el
Update copyright year to 2015
[gnu-emacs] / lisp / cedet / srecode / args.el
1 ;;; srecode/args.el --- Provide some simple template arguments
2
3 ;; Copyright (C) 2007-2015 Free Software Foundation, Inc.
4
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
21
22 ;;; Commentary:
23 ;;
24 ;; Srecode templates can accept arguments. These arguments represent
25 ;; sets of dictionary words that need to be derived. This file contains
26 ;; a set of simple arguments for srecode templates.
27
28 (require 'srecode/dictionary)
29 (require 'ede)
30
31 ;;; Code:
32
33 ;;; :blank
34 ;;
35 ;; Using :blank means that the template should force blank lines
36 ;; before and after the template, regardless of where the insertion
37 ;; is occurring.
38 (defun srecode-semantic-handle-:blank (dict)
39 "Add macros into the dictionary DICT specifying blank line spacing.
40 The wrapgap means make sure the first and last lines of the macro
41 do not contain any text from preceding or following text."
42 ;; This won't actually get used, but it might be nice
43 ;; to know about it.
44 (srecode-dictionary-set-value dict "BLANK" t)
45 )
46
47 ;;; :indent ARGUMENT HANDLING
48 ;;
49 ;; When a :indent argument is required, the default is to indent
50 ;; for the current major mode.
51 (defun srecode-semantic-handle-:indent (dict)
52 "Add macros into the dictionary DICT for indentation."
53 (srecode-dictionary-set-value dict "INDENT" t)
54 )
55
56 ;;; :region ARGUMENT HANDLING
57 ;;
58 ;; When a :region argument is required, provide macros that
59 ;; deal with that active region.
60 ;;
61 ;; Regions allow a macro to wrap the region text within the
62 ;; template bounds.
63 ;;
64 (defvar srecode-handle-region-when-non-active-flag nil
65 "Non-nil means do region handling w/out the region being active.")
66
67 (defun srecode-semantic-handle-:region (dict)
68 "Add macros into the dictionary DICT based on the current :region."
69 ;; Only enable the region section if we can clearly show that
70 ;; the user is intending to do something with the region.
71 (when (or srecode-handle-region-when-non-active-flag
72 (eq last-command 'mouse-drag-region)
73 (and transient-mark-mode mark-active))
74 ;; Show the region section
75 (srecode-dictionary-show-section dict "REGION")
76 (srecode-dictionary-set-value
77 dict "REGIONTEXT" (buffer-substring-no-properties (point) (mark)))
78 ;; Only whack the region if our template output
79 ;; is also destined for the current buffer.
80 (when (eq standard-output (current-buffer))
81 (kill-region (point) (mark))))
82 )
83
84 ;;; :user ARGUMENT HANDLING
85 ;;
86 ;; When a :user argument is required, fill the dictionary with
87 ;; information about the current Emacs user.
88 (defun srecode-semantic-handle-:user (dict)
89 "Add macros into the dictionary DICT based on the current :user."
90 (srecode-dictionary-set-value dict "AUTHOR" (user-full-name))
91 (srecode-dictionary-set-value dict "LOGIN" (user-login-name))
92 (srecode-dictionary-set-value dict "EMAIL" user-mail-address)
93 (srecode-dictionary-set-value dict "EMACSINITFILE" user-init-file)
94 (srecode-dictionary-set-value dict "UID" (user-uid))
95 )
96
97 ;;; :time ARGUMENT HANDLING
98 ;;
99 ;; When a :time argument is required, fill the dictionary with
100 ;; information about the current Emacs time.
101 (defun srecode-semantic-handle-:time (dict)
102 "Add macros into the dictionary DICT based on the current :time."
103 ;; DATE Values
104 (let ((now (current-time)))
105 (srecode-dictionary-set-value
106 dict "YEAR" (format-time-string "%Y" now))
107 (srecode-dictionary-set-value
108 dict "MONTHNAME" (format-time-string "%B" now))
109 (srecode-dictionary-set-value
110 dict "MONTH" (format-time-string "%m" now))
111 (srecode-dictionary-set-value
112 dict "DAY" (format-time-string "%d" now))
113 (srecode-dictionary-set-value
114 dict "WEEKDAY" (format-time-string "%a" now))
115 ;; Time Values
116 (srecode-dictionary-set-value
117 dict "HOUR" (format-time-string "%H" now))
118 (srecode-dictionary-set-value
119 dict "HOUR12" (format-time-string "%l" now))
120 (srecode-dictionary-set-value
121 dict "AMPM" (format-time-string "%p" now))
122 (srecode-dictionary-set-value
123 dict "MINUTE" (format-time-string "%M" now))
124 (srecode-dictionary-set-value
125 dict "SECOND" (format-time-string "%S" now))
126 (srecode-dictionary-set-value
127 dict "TIMEZONE" (format-time-string "%Z" now))
128 ;; Convenience pre-packed date/time
129 (srecode-dictionary-set-value
130 dict "DATE" (format-time-string "%D" now))
131 (srecode-dictionary-set-value
132 dict "TIME" (format-time-string "%X" now))))
133
134 ;;; :file ARGUMENT HANDLING
135 ;;
136 ;; When a :file argument is required, fill the dictionary with
137 ;; information about the file Emacs is editing at the time of
138 ;; insertion.
139 (defun srecode-semantic-handle-:file (dict)
140 "Add macros into the dictionary DICT based on the current :file."
141 (let* ((bfn (buffer-file-name))
142 (file (file-name-nondirectory bfn))
143 (dir (file-name-directory bfn)))
144 (srecode-dictionary-set-value dict "FILENAME" file)
145 (srecode-dictionary-set-value dict "FILE" (file-name-sans-extension file))
146 (srecode-dictionary-set-value dict "EXTENSION" (file-name-extension file))
147 (srecode-dictionary-set-value dict "DIRECTORY" dir)
148 (srecode-dictionary-set-value dict "MODE" (symbol-name major-mode))
149 (srecode-dictionary-set-value
150 dict "SHORTMODE"
151 (let* ((mode-name (symbol-name major-mode))
152 (match (string-match "-mode" mode-name)))
153 (if match
154 (substring mode-name 0 match)
155 mode-name)))
156 (if (or (file-exists-p "CVS")
157 (file-exists-p "RCS"))
158 (srecode-dictionary-show-section dict "RCS")
159 )))
160
161 ;;; :project ARGUMENT HANDLING
162 ;;
163 ;; When the :project argument is required, fill the dictionary with
164 ;; information that the current project (from EDE) might know
165 (defun srecode-semantic-handle-:project (dict)
166 "Add macros into the dictionary DICT based on the current ede project."
167 (let* ((bfn (buffer-file-name))
168 (dir (file-name-directory bfn)))
169 (if (ede-toplevel)
170 (let* ((projecttop (ede-toplevel-project default-directory))
171 (relfname (file-relative-name bfn projecttop))
172 (reldir (file-relative-name dir projecttop))
173 )
174 (srecode-dictionary-set-value dict "PROJECT_FILENAME" relfname)
175 (srecode-dictionary-set-value dict "PROJECT_DIRECTORY" reldir)
176 (srecode-dictionary-set-value dict "PROJECT_NAME" (ede-name (ede-toplevel)))
177 (srecode-dictionary-set-value dict "PROJECT_VERSION" (oref (ede-toplevel) :version))
178 )
179 ;; If there is no EDE project, then put in some base values.
180 (srecode-dictionary-set-value dict "PROJECT_FILENAME" bfn)
181 (srecode-dictionary-set-value dict "PROJECT_DIRECTORY" dir)
182 (srecode-dictionary-set-value dict "PROJECT_NAME" "N/A")
183 (srecode-dictionary-set-value dict "PROJECT_VERSION" "1.0"))))
184
185 ;;; :system ARGUMENT HANDLING
186 ;;
187 ;; When a :system argument is required, fill the dictionary with
188 ;; information about the computer Emacs is running on.
189 (defun srecode-semantic-handle-:system (dict)
190 "Add macros into the dictionary DICT based on the current :system."
191 (srecode-dictionary-set-value dict "SYSTEMCONF" system-configuration)
192 (srecode-dictionary-set-value dict "SYSTEMTYPE" system-type)
193 (srecode-dictionary-set-value dict "SYSTEMNAME" (system-name))
194 (srecode-dictionary-set-value dict "MAILHOST" (or mail-host-address
195 (system-name)))
196 )
197
198 ;;; :kill ARGUMENT HANDLING
199 ;;
200 ;; When a :kill argument is required, fill the dictionary with
201 ;; information about the current kill ring.
202 (defun srecode-semantic-handle-:kill (dict)
203 "Add macros into the dictionary DICT based on the kill ring."
204 (srecode-dictionary-set-value dict "KILL" (car kill-ring))
205 (srecode-dictionary-set-value dict "KILL2" (nth 1 kill-ring))
206 (srecode-dictionary-set-value dict "KILL3" (nth 2 kill-ring))
207 (srecode-dictionary-set-value dict "KILL4" (nth 3 kill-ring))
208 )
209
210 (provide 'srecode/args)
211
212 ;;; srecode/args.el ends here