Develop and Download Open Source Software

Browse Subversion Repository

Contents of /applescript-mode/trunk/applescript-mode.el

Parent Directory Parent Directory | Revision Log Revision Log


Revision 580 - (show annotations) (download)
Sun Feb 5 11:07:07 2012 UTC (12 years, 3 months ago) by sakito
File size: 16776 byte(s)
add # comment, and others

1 ;;; applescript-mode.el --- Major mode for editing AppleScript source
2
3 ;; Copyright (C) 2004-2012 MacEmacs JP Project
4
5 ;;; Credits:
6 ;; Copyright (C) 2003,2004 FUJIMOTO Hisakuni
7 ;; http://www.fobj.com/~hisa/w/applescript.el.html
8 ;; Copyright (C) 2003 443,435 (Public Domain?)
9 ;; http://pc.2ch.net/test/read.cgi/mac/1034581863/
10 ;; Copyright (C) 2004 Harley Gorrell <harley@mahalito.net>
11 ;; http://www.mahalito.net/~harley/elisp/osx-osascript.el
12 ;; Copyright (C) 2009 Ian Eure
13 ;; https://github.com/ieure/applescript-mode
14
15 ;; Author: sakito <sakito@users.sourceforge.jp>
16 ;; Version: $Revision$
17 ;; Keywords: AppleScript languages
18
19 (defconst applescript-mode-version "$Revision$"
20 "The current version of the AppleScript mode.")
21
22 (defconst applescript-mode-help-address "sakito@users.sourceforge.jp"
23 "Address accepting submission of bug reports.")
24
25 ;;; This file is NOT part of GNU Emacs
26
27 ;;; License
28 ;;
29 ;; This program is free software; you can redistribute it and/or modify
30 ;; it under the terms of the GNU General Public License as published by
31 ;; the Free Software Foundation; either version 3, or (at your option)
32 ;; any later version.
33
34 ;; This program is distributed in the hope that it will be useful,
35 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
36 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
37 ;; GNU General Public License for more details.
38
39 ;; You should have received a copy of the GNU General Public License
40 ;; along with this program; see the file COPYING. If not, write to
41 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
42 ;; Floor, Boston, MA 02110-1301, USA.
43
44 ;;; Commentary:
45
46 ;; AppleScript Mode
47
48 ;;; Usage:
49 ;; To use applescript-mode.el put the following line in your .emacs:
50 ;; (autoload 'applescript-mode "applescript-mode"
51 ;; "Major mode for editing AppleScript source." t)
52 ;; (add-to-list 'auto-mode-alist '("\\.applescript$" . applescript-mode))
53
54 ;; Please use the SourceForge MacEmacs JP Project to submit bugs or
55 ;; patches:
56 ;;
57 ;; http://sourceforge.jp/projects/macemacsjp
58
59 ;; FOR MORE INFORMATION:
60
61 ;; There is some information on applescript-mode.el at
62
63 ;; http://macemacsjp.sourceforge.jp/documents/applescript-mode/
64
65
66 ;;; Code:
67
68 ;; user customize variables
69 (defgroup applescript nil
70 "Support for AppleScript, <http://www.apple.com/applescript/>"
71 :group 'languages
72 :prefix "as-")
73
74 (defcustom as-osascript-command "osascript"
75 "*execute AppleScripts and other OSA language scripts."
76 :type 'string
77 :group 'applescript)
78
79 (defcustom as-osacompile-command "osacompile"
80 "*compile AppleScripts and other OSA language scripts."
81 :type 'string
82 :group 'applescript)
83
84 (defcustom as-osascript-command-args '("-ss")
85 "*List of string arguments to be used when starting a osascript."
86 :type '(repeat string)
87 :group 'applescript)
88
89 (defcustom as-indent-offset 4
90 "*Amount of offset per level of indentation.
91 `\\[as-guess-indent-offset]' can usually guess a good value when
92 you're editing someone else's AppleScript code."
93 :type 'integer
94 :group 'applescript)
95
96 (defcustom as-continuation-offset 4
97 "*Additional amount of offset to give for some continuation lines.
98 Continuation lines are those that immediately follow a
99 Continuation sign terminated line. Only those continuation lines
100 for a block opening statement are given this extra offset."
101 :type 'integer
102 :group 'applescript)
103
104 ;; Face Setting
105
106 ;; Face for true, false, me, it
107 (defvar as-pseudo-keyword-face 'as-pseudo-keyword-face
108 "Face for pseudo keywords in AppleScript mode, like me, true, false.")
109 (make-face 'as-pseudo-keyword-face)
110
111 ;; Face for command
112 (defvar as-command-face 'as-command-face
113 "Face for command like copy, get, set, and error.")
114 (make-face 'as-command-face)
115
116 (defun as-font-lock-mode-hook ()
117 (or (face-differs-from-default-p 'as-pseudo-keyword-face)
118 (copy-face 'font-lock-keyword-face 'as-pseudo-keyword-face))
119 (or (face-differs-from-default-p 'as-command-face)
120 (copy-face 'font-lock-keyword-face 'as-command-face)))
121 (add-hook 'font-lock-mode-hook 'as-font-lock-mode-hook)
122
123 (defconst applescript-font-lock-keywords
124 (let (
125 ;; expressions and control Statements
126 (kw1 (regexp-opt
127 '("and" "app" "application" "considering" "div"
128 "else" "end" "exit" "is" "mod" "not" "on" "or"
129 "if" "ignoring" "reopen" "repeat" "return"
130 "tell" "then" "to" "try"
131 "using[ \t]terms[ \t]from"
132 "with[ \t]timeout" "with[ \t]transaction")))
133 ;; commands
134 (kw2 (regexp-opt
135 '("ASCII[ \t]character" "ASCII[ \t]number" "activate" "AGStart"
136 "beep" "copy" "count" "choose[ \t]application"
137 "choose[ \t]file" "choose[ \t]folder" "close[ \t]access"
138 "current[ \t]date" "display[ \t]dialog" "get" "get[ \t]EOF"
139 "info[ \t]for" "launch" "list[ \t]disks" "list[ \t]folder"
140 "load[ \t]script" "log" "monitor[ \t]depth" "max[ \t]monitor[ \t]depth"
141 "min[ \t]monitor[ \t]depth" "new[ \t]file" "offset"
142 "open[ \t]for[ \t]access" "path[ \t]to" "random[ \t]number"
143 "read" "round" "run" "run[ \t]script" "scripting[ \t]component"
144 "set" "set[ \t]EOF" "set[ \t]monitor[ \t]depth" "set[ \t]volume"
145 "start[ \t]log" "stop[ \t]log" "store[ \t]script"
146 "time[ \t]to[ \t]GMT" "write"
147 )))
148 ;; misc
149 (kw3 (regexp-opt
150 '("buttons" "default[ \t]answer" "default[ \t]button"
151 "to[ \t]begining[ \t]of" "to[ \t]word" "starting[ \t]at"
152 "with[ \t]icon" "write[ \t]permission"))))
153 (list
154 ;; keywords
155 (cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1)
156 (cons (concat "\\b\\(" kw2 "\\)\\b[ \n\t(]") 1)
157 ;; kw3 not yet..
158 (cons (concat "\\b\\(" kw3 "\\)\\b[ \n\t(]") 1)
159 ;; functions
160 '("\\bon[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
161 1 font-lock-function-name-face)
162 '("\\bto[ \t]+\\([a-zA-Z_]+[a-zA-Z0-9_]*\\)"
163 1 font-lock-function-name-face)
164 ;; pseudo-keywords
165 '("\\b\\(it\\|me\\|my\\|true\\|false\\)\\b"
166 1 as-pseudo-keyword-face))))
167 (put 'applescript-mode 'font-lock-defaults '(applescript-font-lock-keywords))
168
169 ;; Major mode boilerplate
170
171 ;; define a mode-specific abbrev table for those who use such things
172 (defvar applescript-mode-abbrev-table nil
173 "Abbrev table in use in `applescript-mode' buffers.")
174 (define-abbrev-table 'applescript-mode-abbrev-table nil)
175
176 (defvar applescript-mode-hook nil
177 "*Hook called by `applescript-mode'.")
178
179 (defvar as-mode-syntax-table nil
180 "Syntax table used in `applescript-mode' buffers.")
181 (when (not as-mode-syntax-table)
182 (setq as-mode-syntax-table (make-syntax-table))
183
184 (modify-syntax-entry ?\" "\"" as-mode-syntax-table)
185
186 (modify-syntax-entry ?: "." as-mode-syntax-table)
187 (modify-syntax-entry ?\; "." as-mode-syntax-table)
188 (modify-syntax-entry ?& "." as-mode-syntax-table)
189 (modify-syntax-entry ?\| "." as-mode-syntax-table)
190 (modify-syntax-entry ?+ "." as-mode-syntax-table)
191 (modify-syntax-entry ?/ "." as-mode-syntax-table)
192 (modify-syntax-entry ?= "." as-mode-syntax-table)
193 (modify-syntax-entry ?< "." as-mode-syntax-table)
194 (modify-syntax-entry ?> "." as-mode-syntax-table)
195 (modify-syntax-entry ?$ "." as-mode-syntax-table)
196 (modify-syntax-entry ?\[ "." as-mode-syntax-table)
197 (modify-syntax-entry ?\] "." as-mode-syntax-table)
198 (modify-syntax-entry ?\{ "." as-mode-syntax-table)
199 (modify-syntax-entry ?\} "." as-mode-syntax-table)
200 (modify-syntax-entry ?. "." as-mode-syntax-table)
201 (modify-syntax-entry ?\\ "." as-mode-syntax-table)
202 (modify-syntax-entry ?\' "." as-mode-syntax-table)
203
204 ;; a single hash starts a comment
205 (modify-syntax-entry ?\# "<" as-mode-syntax-table)
206 ;; a double hyphen starts a comment
207 (modify-syntax-entry ?- ". 12" as-mode-syntax-table)
208
209 ;; comment delimiters
210 (modify-syntax-entry ?\f "> " as-mode-syntax-table)
211 (modify-syntax-entry ?\n "> " as-mode-syntax-table)
212
213 ;; define parentheses to match
214 (modify-syntax-entry ?\( "()1" as-mode-syntax-table)
215 (modify-syntax-entry ?\) ")(4" as-mode-syntax-table)
216 (modify-syntax-entry ?* ". 23b" as-mode-syntax-table))
217
218 (defvar as-mode-map ()
219 "Keymap used in `applescript-mode' buffers.")
220 ;; Menu definitions, only relevent if you have the easymenu.el package
221 ;; (standard in the latest Emacs 19 and XEmacs 19 distributions).
222 (defvar as-menu nil
223 "Menu for AppleScript Mode.
224 This menu will get created automatically if you have the
225 `easymenu' package. Note that the latest X/Emacs releases
226 contain this package.")
227 (if as-mode-map
228 nil
229 (setq as-mode-map
230 (let ((map (make-sparse-keymap)))
231 ;; Key bindings
232
233 ;; subprocess commands
234 (define-key map "\C-c\C-c" 'as-execute-buffer)
235 (define-key map "\C-c\C-s" 'as-execute-string)
236 (define-key map "\C-c|" 'as-execute-region)
237
238 ;; Miscellaneous
239 (define-key map "\C-c;" 'comment-region)
240 (define-key map "\C-c:" 'uncomment-region)
241
242 (easy-menu-define as-menu map "AppleScript Mode menu"
243 '("AppleScript"
244 ["Comment Out Region" comment-region (mark)]
245 ["Uncomment Region" uncomment-region (mark)]
246 "-"
247 ["Execute buffer" as-execute-buffer t]
248 ["Execute region" as-execute-region (mark)]
249 ["Execute string" as-execute-string t]
250 "-"
251 ["Mode Version" as-mode-version t]
252 ["AppleScript Version" as-language-version t]))
253 map)))
254
255 ;; Utilities
256 (defmacro as-safe (&rest body)
257 "Safely execute BODY, return nil if an error occurred."
258 `(condition-case nil
259 (progn (,@ body))
260 (error nil)))
261
262 (defsubst as-keep-region-active ()
263 "Keep the region active in XEmacs."
264 ;; Ignore byte-compiler warnings you might see. Also note that
265 ;; FSF's Emacs 19 does it differently; its policy doesn't require us
266 ;; to take explicit action.
267 (when (featurep 'xemacs)
268 (and (boundp 'zmacs-region-stays)
269 (setq zmacs-region-stays t))))
270
271 (defsubst as-point (position)
272 "Returns the value of point at certain commonly referenced POSITIONs.
273 POSITION can be one of the following symbols:
274
275 bol -- beginning of line
276 eol -- end of line
277 bod -- beginning of on or to
278 eod -- end of on or to
279 bob -- beginning of buffer
280 eob -- end of buffer
281 boi -- back to indentation
282 bos -- beginning of statement
283
284 This function does not modify point or mark."
285 (let ((here (point)))
286 (cond
287 ((eq position 'bol) (beginning-of-line))
288 ((eq position 'eol) (end-of-line))
289 ((eq position 'bod) (as-beginning-of-handler 'either))
290 ((eq position 'eod) (as-end-of-handler 'either))
291 ;; Kind of funny, I know, but useful for as-up-exception.
292 ((eq position 'bob) (goto-char (point-min)))
293 ((eq position 'eob) (goto-char (point-max)))
294 ((eq position 'boi) (back-to-indentation))
295 ((eq position 'bos) (as-goto-initial-line))
296 (t (error "Unknown buffer position requested: %s" position)))
297 (prog1 (point) (goto-char here))))
298
299 ;;;###autoload
300 (defun applescript-mode ()
301 "Major mode for editing AppleScript files."
302 (interactive)
303 ;; set up local variables
304 (kill-all-local-variables)
305 (make-local-variable 'font-lock-defaults)
306 (make-local-variable 'paragraph-separate)
307 (make-local-variable 'paragraph-start)
308 (make-local-variable 'require-final-newline)
309 (make-local-variable 'comment-start)
310 (make-local-variable 'comment-end)
311 (make-local-variable 'comment-start-skip)
312 (make-local-variable 'comment-column)
313 (make-local-variable 'comment-indent-function)
314 (make-local-variable 'indent-region-function)
315 (make-local-variable 'indent-line-function)
316 (make-local-variable 'add-log-current-defun-function)
317 (make-local-variable 'fill-paragraph-function)
318 ;;
319
320 ;; Maps and tables
321 (use-local-map as-mode-map)
322
323 (set-syntax-table as-mode-syntax-table)
324
325 ;; Names
326 (setq major-mode 'applescript-mode
327 mode-name "AppleScript"
328 local-abbrev-table applescript-mode-abbrev-table
329 font-lock-defaults '(applescript-font-lock-keywords)
330 paragraph-separate "[ \t\n\f]*$"
331 paragraph-start "[ \t\n\f]*$"
332 require-final-newline t
333 comment-start "-- "
334 comment-end ""
335 comment-start-skip "---*[ \t]*"
336 comment-column 40)
337
338 ;; Support for outline-minor-mode
339 (set (make-local-variable 'outline-regexp)
340 "\\([ \t]*\\(on\\|to\\|if\\|repeat\\|tell\\|end\\)\\|--\\)")
341 (set (make-local-variable 'outline-level) 'as-outline-level)
342
343 ;; add the menu
344 (if as-menu
345 (easy-menu-add as-menu))
346
347 ;; Run the mode hook. Note that applescript-mode-hook is deprecated.
348 (if applescript-mode-hook
349 (run-hooks 'applescript-mode-hook)
350 (run-hooks 'applescript-mode-hook)))
351
352 (when (not (or (rassq 'applescript-mode auto-mode-alist)
353 (push '("\\.applescript$" . applescript-mode) auto-mode-alist))))
354
355 ;;; Subprocess commands
356
357 ;; function use variables
358 (defconst as-output-buffer "*AppleScript Output*"
359 "Name for the buffer to display AppleScript output.")
360 (make-variable-buffer-local 'as-output-buffer)
361
362 ;; Code execution commands
363 (defun as-execute-buffer (&optional async)
364 "Execute the whole buffer as an Applescript"
365 (interactive "P")
366 (as-execute-region (point-min) (point-max) async))
367
368 (defun as-execute-string (string &optional async)
369 "Send the argument STRING to an AppleScript."
370 (interactive "sExecute AppleScript: ")
371 (with-temp-buffer
372 (insert string)
373 (as-execute-region (point-min) (point-max) async)))
374
375 (defun as-execute-region (start end &optional async)
376 "Execute the region as an Applescript"
377 (interactive "r\nP")
378 (let ((region (buffer-substring start end))
379 (as-current-win (selected-window)))
380 (pop-to-buffer as-output-buffer)
381 (insert (as-execute-code region))
382 (select-window as-current-win)))
383
384 (defun as-execute-code (code)
385 "pop to the AppleScript buffer, run the code and display the results."
386 (as-decode-string
387 (do-applescript (as-string-to-sjis-string-with-escape code))))
388
389 (defun as-mode-version ()
390 "Echo the current version of `applescript-mode' in the minibuffer."
391 (interactive)
392 (message "Using `applescript-mode' version %s" applescript-mode-version)
393 (as-keep-region-active))
394
395 (defun as-language-version()
396 "Echo the current version of AppleScript Version in the minibuffer."
397 (interactive)
398 (message "Using AppleScript version %s"
399 (as-execute-code "AppleScript's version"))
400 (as-keep-region-active))
401
402 ;; as-beginning-of-handler, as-end-of-handler,as-goto-initial-line not yet
403
404 ;; Support for outline.el
405 (defun as-outline-level ()
406 "This is so that `current-column` DTRT in otherwise-hidden text"
407 (let (buffer-invisibility-spec)
408 (save-excursion
409 (back-to-indentation)
410 (current-column))))
411
412 ;; for Japanese
413 (defun as-unescape-string (str)
414 "delete escape char '\'"
415 (replace-regexp-in-string "\\\\\\(.\\)" "\\1" str))
416
417 (defun as-escape-string (str)
418 "add escape char '\'"
419 (replace-regexp-in-string "\\\\" "\\\\\\\\" str))
420
421 (defun as-sjis-byte-list-escape (lst)
422 (cond
423 ((null lst) nil)
424 ((= (car lst) 92)
425 (append (list 92 (car lst)) (as-sjis-byte-list-escape (cdr lst))))
426 (t (cons (car lst) (as-sjis-byte-list-escape (cdr lst))))))
427
428 (defun as-string-to-sjis-string-with-escape (str)
429 "String convert to SJIS, and escape \"\\\" "
430 (apply 'string
431 (as-sjis-byte-list-escape
432 (string-to-list
433 (as-encode-string str)))))
434
435 (defun as-decode-string (str)
436 "do-applescript return values decode sjis-mac"
437 (decode-coding-string str 'sjis-mac))
438
439 (defun as-encode-string (str)
440 "do-applescript return values encode sjis-mac"
441 (encode-coding-string str 'sjis-mac))
442
443 (defun as-parse-result (retstr)
444 "String convert to Emacs Lisp Object"
445 (cond
446 ;; as list
447 ((string-match "\\`\\s-*{\\(.*\\)}\\s-*\\'" retstr)
448 (let ((mstr (match-string 1 retstr)))
449 (mapcar (lambda (item) (as-parse-result item))
450 (split-string mstr ","))))
451
452 ;; AERecord item as cons
453 ((string-match "\\`\\s-*\\([^:\\\"]+\\):\\(.*\\)\\s-*\\'" retstr)
454 (cons (intern (match-string 1 retstr))
455 (as-parse-result (match-string 2 retstr))))
456
457 ;; as string
458 ((string-match "\\`\\s-*\\\"\\(.*\\)\\\"\\s-*\\'" retstr)
459 (as-decode-string
460 (as-unescape-string (match-string 1 retstr))))
461
462 ;; as integer
463 ((string-match "\\`\\s-*\\([0-9]+\\)\\s-*\\'" retstr)
464 (string-to-number (match-string 1 retstr)))
465
466 ;; else
467 (t (intern retstr))))
468
469 (provide 'applescript-mode)
470 ;;; applescript-mode.el ends here

Properties

Name Value
svn:eol-style native
svn:keywords Author Date Id Revision

Back to OSDN">Back to OSDN
ViewVC Help
Powered by ViewVC 1.1.26