Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / cedet / srecode / args.el
blobb4977f0882ed0a1c4ba809006f6f5b3939de31fd
1 ;;; srecode/args.el --- Provide some simple template arguments
3 ;; Copyright (C) 2007-2014 Free Software Foundation, Inc.
5 ;; Author: Eric M. Ludlam <eric@siege-engine.com>
7 ;; This file is part of GNU Emacs.
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.
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.
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/>.
22 ;;; Commentary:
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.
28 (require 'srecode/dictionary)
29 (require 'ede)
31 ;;; Code:
33 ;;; :blank
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)
47 ;;; :indent ARGUMENT HANDLING
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)
56 ;;; :region ARGUMENT HANDLING
58 ;; When a :region argument is required, provide macros that
59 ;; deal with that active region.
61 ;; Regions allow a macro to wrap the region text within the
62 ;; template bounds.
64 (defvar srecode-handle-region-when-non-active-flag nil
65 "Non-nil means do region handling w/out the region being active.")
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))))
84 ;;; :user ARGUMENT HANDLING
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))
97 ;;; :time ARGUMENT HANDLING
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 (srecode-dictionary-set-value
105 dict "YEAR" (format-time-string "%Y" (current-time)))
106 (srecode-dictionary-set-value
107 dict "MONTHNAME" (format-time-string "%B" (current-time)))
108 (srecode-dictionary-set-value
109 dict "MONTH" (format-time-string "%m" (current-time)))
110 (srecode-dictionary-set-value
111 dict "DAY" (format-time-string "%d" (current-time)))
112 (srecode-dictionary-set-value
113 dict "WEEKDAY" (format-time-string "%a" (current-time)))
114 ;; Time Values
115 (srecode-dictionary-set-value
116 dict "HOUR" (format-time-string "%H" (current-time)))
117 (srecode-dictionary-set-value
118 dict "HOUR12" (format-time-string "%l" (current-time)))
119 (srecode-dictionary-set-value
120 dict "AMPM" (format-time-string "%p" (current-time)))
121 (srecode-dictionary-set-value
122 dict "MINUTE" (format-time-string "%M" (current-time)))
123 (srecode-dictionary-set-value
124 dict "SECOND" (format-time-string "%S" (current-time)))
125 (srecode-dictionary-set-value
126 dict "TIMEZONE" (format-time-string "%Z" (current-time)))
127 ;; Convenience pre-packed date/time
128 (srecode-dictionary-set-value
129 dict "DATE" (format-time-string "%D" (current-time)))
130 (srecode-dictionary-set-value
131 dict "TIME" (format-time-string "%X" (current-time)))
134 ;;; :file ARGUMENT HANDLING
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")
161 ;;; :project ARGUMENT HANDLING
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))
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))
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"))))
185 ;;; :system ARGUMENT HANDLING
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)))
198 ;;; :kill ARGUMENT HANDLING
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))
210 (provide 'srecode/args)
212 ;;; srecode/args.el ends here