add # comment, and others
@@ -1,6 +1,6 @@ | ||
1 | -;;; applescript-mode.el --- major mode for editing AppleScript source | |
1 | +;;; applescript-mode.el --- Major mode for editing AppleScript source | |
2 | 2 | |
3 | -;; Copyright (C) 2004 MacEmacs JP Project | |
3 | +;; Copyright (C) 2004-2012 MacEmacs JP Project | |
4 | 4 | |
5 | 5 | ;;; Credits: |
6 | 6 | ;; Copyright (C) 2003,2004 FUJIMOTO Hisakuni |
@@ -9,9 +9,12 @@ | ||
9 | 9 | ;; http://pc.2ch.net/test/read.cgi/mac/1034581863/ |
10 | 10 | ;; Copyright (C) 2004 Harley Gorrell <harley@mahalito.net> |
11 | 11 | ;; http://www.mahalito.net/~harley/elisp/osx-osascript.el |
12 | +;; Copyright (C) 2009 Ian Eure | |
13 | +;; https://github.com/ieure/applescript-mode | |
12 | 14 | |
13 | 15 | ;; Author: sakito <sakito@users.sourceforge.jp> |
14 | -;; Keywords: languages, tools | |
16 | +;; Version: $Revision$ | |
17 | +;; Keywords: AppleScript languages | |
15 | 18 | |
16 | 19 | (defconst applescript-mode-version "$Revision$" |
17 | 20 | "The current version of the AppleScript mode.") |
@@ -19,20 +22,24 @@ | ||
19 | 22 | (defconst applescript-mode-help-address "sakito@users.sourceforge.jp" |
20 | 23 | "Address accepting submission of bug reports.") |
21 | 24 | |
22 | -;; This file is free software; you can redistribute it and/or modify | |
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 | |
23 | 30 | ;; it under the terms of the GNU General Public License as published by |
24 | -;; the Free Software Foundation; either version 2, or (at your option) | |
31 | +;; the Free Software Foundation; either version 3, or (at your option) | |
25 | 32 | ;; any later version. |
26 | 33 | |
27 | -;; This file is distributed in the hope that it will be useful, | |
34 | +;; This program is distributed in the hope that it will be useful, | |
28 | 35 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of |
29 | 36 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
30 | 37 | ;; GNU General Public License for more details. |
31 | 38 | |
32 | 39 | ;; You should have received a copy of the GNU General Public License |
33 | -;; along with GNU Emacs; see the file COPYING. If not, write to | |
34 | -;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
35 | -;; Boston, MA 02111-1307, USA. | |
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. | |
36 | 43 | |
37 | 44 | ;;; Commentary: |
38 | 45 |
@@ -40,10 +47,9 @@ | ||
40 | 47 | |
41 | 48 | ;;; Usage: |
42 | 49 | ;; To use applescript-mode.el put the following line in your .emacs: |
43 | -;; (autoload 'applescript-mode "applescript-mode" "major mode for editing AppleScript source." t) | |
44 | -;; (setq auto-mode-alist | |
45 | -;; (cons '("\\.applescript$" . applescript-mode) auto-mode-alist) | |
46 | -;; ) | |
50 | +;; (autoload 'applescript-mode "applescript-mode" | |
51 | +;; "Major mode for editing AppleScript source." t) | |
52 | +;; (add-to-list 'auto-mode-alist '("\\.applescript$" . applescript-mode)) | |
47 | 53 | |
48 | 54 | ;; Please use the SourceForge MacEmacs JP Project to submit bugs or |
49 | 55 | ;; patches: |
@@ -59,11 +65,9 @@ | ||
59 | 65 | |
60 | 66 | ;;; Code: |
61 | 67 | |
62 | - | |
63 | - | |
64 | 68 | ;; user customize variables |
65 | 69 | (defgroup applescript nil |
66 | - "Support for the AppleScript, <http://www.apple.com/applescript/>" | |
70 | + "Support for AppleScript, <http://www.apple.com/applescript/>" | |
67 | 71 | :group 'languages |
68 | 72 | :prefix "as-") |
69 | 73 |
@@ -113,46 +117,39 @@ | ||
113 | 117 | (or (face-differs-from-default-p 'as-pseudo-keyword-face) |
114 | 118 | (copy-face 'font-lock-keyword-face 'as-pseudo-keyword-face)) |
115 | 119 | (or (face-differs-from-default-p 'as-command-face) |
116 | - (copy-face 'font-lock-keyword-face 'as-command-face)) | |
117 | - ) | |
120 | + (copy-face 'font-lock-keyword-face 'as-command-face))) | |
118 | 121 | (add-hook 'font-lock-mode-hook 'as-font-lock-mode-hook) |
119 | 122 | |
120 | -(defvar applescript-font-lock-keywords | |
123 | +(defconst applescript-font-lock-keywords | |
121 | 124 | (let ( |
122 | 125 | ;; expressions and control Statements |
123 | - (kw1 (mapconcat 'identity | |
124 | - '("and" "app" "application" "considering" "div" | |
125 | - "else" "end" "exit" "is" "mod" "not" "on" "or" | |
126 | - "if" "ignoring" "reopen" "repeat" | |
127 | - "tell" "then" "to" | |
128 | - "using[ \t]terms[ \t]from" | |
129 | - "with[ \t]timeout" "with[ \t]transaction" | |
130 | - ) | |
131 | - "\\|")) | |
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"))) | |
132 | 133 | ;; commands |
133 | - (kw2 (mapconcat 'identity | |
134 | - '("ASCII[ \t]character" "ASCII[ \t]number" "activate" "AGStart" | |
135 | - "beep" "copy" "count" "choose[ \t]application" | |
136 | - "choose[ \t]file" "choose[ \t]folder" "close[ \t]access" | |
137 | - "current[ \t]date" "display[ \t]dialog" "get" "get[ \t]EOF" | |
138 | - "info[ \t]for" "launch" "list[ \t]disks" "list[ \t]folder" | |
139 | - "load[ \t]script" "log" "monitor[ \t]depth" "max[ \t]monitor[ \t]depth" | |
140 | - "min[ \t]monitor[ \t]depth" "new[ \t]file" "offset" | |
141 | - "open[ \t]for[ \t]access" "path[ \t]to" "random[ \t]number" | |
142 | - "read" "round" "run" "run[ \t]script" "scripting[ \t]component" | |
143 | - "set" "set[ \t]EOF" "set[ \t]monitor[ \t]depth" "set[ \t]volume" | |
144 | - "start[ \t]log" "stop[ \t]log" "store[ \t]script" | |
145 | - "time[ \t]to[ \t]GMT" "write" | |
146 | - ) | |
147 | - "\\|")) | |
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 | 148 | ;; misc |
149 | - (kw3 (mapconcat 'identity | |
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 | - ) | |
154 | - "\\|")) | |
155 | - ) | |
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")))) | |
156 | 153 | (list |
157 | 154 | ;; keywords |
158 | 155 | (cons (concat "\\b\\(" kw1 "\\)\\b[ \n\t(]") 1) |
@@ -166,9 +163,7 @@ | ||
166 | 163 | 1 font-lock-function-name-face) |
167 | 164 | ;; pseudo-keywords |
168 | 165 | '("\\b\\(it\\|me\\|my\\|true\\|false\\)\\b" |
169 | - 1 as-pseudo-keyword-face) | |
170 | - ) | |
171 | - )) | |
166 | + 1 as-pseudo-keyword-face)))) | |
172 | 167 | (put 'applescript-mode 'font-lock-defaults '(applescript-font-lock-keywords)) |
173 | 168 | |
174 | 169 | ;; Major mode boilerplate |
@@ -181,26 +176,6 @@ | ||
181 | 176 | (defvar applescript-mode-hook nil |
182 | 177 | "*Hook called by `applescript-mode'.") |
183 | 178 | |
184 | -(defvar as-mode-map () | |
185 | - "Keymap used in `applescript-mode' buffers.") | |
186 | -(if as-mode-map | |
187 | - nil | |
188 | - (setq as-mode-map (make-sparse-keymap)) | |
189 | - ;; Key bindings | |
190 | - | |
191 | - ;; subprocess commands | |
192 | - (define-key as-mode-map "\C-c\C-c" 'as-execute-buffer) | |
193 | - (define-key as-mode-map "\C-c\C-s" 'as-execute-string) | |
194 | - (define-key as-mode-map "\C-c|" 'as-execute-region) | |
195 | - | |
196 | - ;; Miscellaneous | |
197 | - (define-key as-mode-map "\C-c;" 'comment-region) | |
198 | - (define-key as-mode-map "\C-c:" 'uncomment-region) | |
199 | - | |
200 | - ;; information | |
201 | - ;(define-key as-mode-map "\C-c\C-v" 'as-mode-version) | |
202 | - ) | |
203 | - | |
204 | 179 | (defvar as-mode-syntax-table nil |
205 | 180 | "Syntax table used in `applescript-mode' buffers.") |
206 | 181 | (when (not as-mode-syntax-table) |
@@ -226,6 +201,8 @@ | ||
226 | 201 | (modify-syntax-entry ?\\ "." as-mode-syntax-table) |
227 | 202 | (modify-syntax-entry ?\' "." as-mode-syntax-table) |
228 | 203 | |
204 | + ;; a single hash starts a comment | |
205 | + (modify-syntax-entry ?\# "<" as-mode-syntax-table) | |
229 | 206 | ;; a double hyphen starts a comment |
230 | 207 | (modify-syntax-entry ?- ". 12" as-mode-syntax-table) |
231 | 208 |
@@ -236,15 +213,51 @@ | ||
236 | 213 | ;; define parentheses to match |
237 | 214 | (modify-syntax-entry ?\( "()1" as-mode-syntax-table) |
238 | 215 | (modify-syntax-entry ?\) ")(4" as-mode-syntax-table) |
239 | - (modify-syntax-entry ?* ". 23b" as-mode-syntax-table) | |
240 | - ) | |
216 | + (modify-syntax-entry ?* ". 23b" as-mode-syntax-table)) | |
241 | 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 | + | |
242 | 255 | ;; Utilities |
243 | 256 | (defmacro as-safe (&rest body) |
244 | 257 | "Safely execute BODY, return nil if an error occurred." |
245 | - (` (condition-case nil | |
246 | - (progn (,@ body)) | |
247 | - (error nil)))) | |
258 | + `(condition-case nil | |
259 | + (progn (,@ body)) | |
260 | + (error nil))) | |
248 | 261 | |
249 | 262 | (defsubst as-keep-region-active () |
250 | 263 | "Keep the region active in XEmacs." |
@@ -276,39 +289,13 @@ | ||
276 | 289 | ((eq position 'bod) (as-beginning-of-handler 'either)) |
277 | 290 | ((eq position 'eod) (as-end-of-handler 'either)) |
278 | 291 | ;; Kind of funny, I know, but useful for as-up-exception. |
279 | - ((eq position 'bob) (beginning-of-buffer)) | |
280 | - ((eq position 'eob) (end-of-buffer)) | |
292 | + ((eq position 'bob) (goto-char (point-min))) | |
293 | + ((eq position 'eob) (goto-char (point-max))) | |
281 | 294 | ((eq position 'boi) (back-to-indentation)) |
282 | 295 | ((eq position 'bos) (as-goto-initial-line)) |
283 | - (t (error "Unknown buffer position requested: %s" position)) | |
284 | - ) | |
285 | - (prog1 | |
286 | - (point) | |
287 | - (goto-char here)))) | |
296 | + (t (error "Unknown buffer position requested: %s" position))) | |
297 | + (prog1 (point) (goto-char here)))) | |
288 | 298 | |
289 | -;; Menu definitions, only relevent if you have the easymenu.el package | |
290 | -;; (standard in the latest Emacs 19 and XEmacs 19 distributions). | |
291 | -(defvar as-menu nil | |
292 | - "Menu for AppleScript Mode. | |
293 | -This menu will get created automatically if you have the | |
294 | -`easymenu' package. Note that the latest X/Emacs releases | |
295 | -contain this package.") | |
296 | - | |
297 | -(and (as-safe (require 'easymenu) t) | |
298 | - (easy-menu-define | |
299 | - as-menu as-mode-map "AppleScript Mode menu" | |
300 | - '("AppleScript" | |
301 | - ["Comment Out Region" comment-region (mark)] | |
302 | - ["Uncomment Region" uncomment-region (mark)] | |
303 | - "-" | |
304 | - ["Execute buffer" as-execute-buffer t] | |
305 | - ["Execute region" as-execute-region (mark)] | |
306 | - ["Execute string" as-execute-string t] | |
307 | - "-" | |
308 | - ["Mode Version" as-mode-version t] | |
309 | - ["AppleScript Version" as-language-version t] | |
310 | - ))) | |
311 | - | |
312 | 299 | ;;;###autoload |
313 | 300 | (defun applescript-mode () |
314 | 301 | "Major mode for editing AppleScript files." |
@@ -346,8 +333,7 @@ | ||
346 | 333 | comment-start "-- " |
347 | 334 | comment-end "" |
348 | 335 | comment-start-skip "---*[ \t]*" |
349 | - comment-column 40 | |
350 | - ) | |
336 | + comment-column 40) | |
351 | 337 | |
352 | 338 | ;; Support for outline-minor-mode |
353 | 339 | (set (make-local-variable 'outline-regexp) |
@@ -361,10 +347,8 @@ | ||
361 | 347 | ;; Run the mode hook. Note that applescript-mode-hook is deprecated. |
362 | 348 | (if applescript-mode-hook |
363 | 349 | (run-hooks 'applescript-mode-hook) |
364 | - (run-hooks 'applescript-mode-hook)) | |
350 | + (run-hooks 'applescript-mode-hook))) | |
365 | 351 | |
366 | - ) | |
367 | - | |
368 | 352 | (when (not (or (rassq 'applescript-mode auto-mode-alist) |
369 | 353 | (push '("\\.applescript$" . applescript-mode) auto-mode-alist)))) |
370 | 354 |
@@ -384,9 +368,7 @@ | ||
384 | 368 | (defun as-execute-string (string &optional async) |
385 | 369 | "Send the argument STRING to an AppleScript." |
386 | 370 | (interactive "sExecute AppleScript: ") |
387 | - (save-excursion | |
388 | - (set-buffer (get-buffer-create | |
389 | - (generate-new-buffer-name as-output-buffer))) | |
371 | + (with-temp-buffer | |
390 | 372 | (insert string) |
391 | 373 | (as-execute-region (point-min) (point-max) async))) |
392 | 374 |
@@ -397,12 +379,12 @@ | ||
397 | 379 | (as-current-win (selected-window))) |
398 | 380 | (pop-to-buffer as-output-buffer) |
399 | 381 | (insert (as-execute-code region)) |
400 | - (select-window as-current-win) | |
401 | - )) | |
382 | + (select-window as-current-win))) | |
402 | 383 | |
403 | 384 | (defun as-execute-code (code) |
404 | 385 | "pop to the AppleScript buffer, run the code and display the results." |
405 | - (as-decode-string (do-applescript (as-string-to-sjis-string-with-escape code)))) | |
386 | + (as-decode-string | |
387 | + (do-applescript (as-string-to-sjis-string-with-escape code)))) | |
406 | 388 | |
407 | 389 | (defun as-mode-version () |
408 | 390 | "Echo the current version of `applescript-mode' in the minibuffer." |
@@ -413,7 +395,8 @@ | ||
413 | 395 | (defun as-language-version() |
414 | 396 | "Echo the current version of AppleScript Version in the minibuffer." |
415 | 397 | (interactive) |
416 | - (message "Using AppleScript version %s" (as-execute-code "AppleScript's version")) | |
398 | + (message "Using AppleScript version %s" | |
399 | + (as-execute-code "AppleScript's version")) | |
417 | 400 | (as-keep-region-active)) |
418 | 401 | |
419 | 402 | ;; as-beginning-of-handler, as-end-of-handler,as-goto-initial-line not yet |
@@ -478,7 +461,7 @@ | ||
478 | 461 | |
479 | 462 | ;; as integer |
480 | 463 | ((string-match "\\`\\s-*\\([0-9]+\\)\\s-*\\'" retstr) |
481 | - (string-to-int (match-string 1 retstr))) | |
464 | + (string-to-number (match-string 1 retstr))) | |
482 | 465 | |
483 | 466 | ;; else |
484 | 467 | (t (intern retstr)))) |