args.el 7.1 KB

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