aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorYAMAMOTO Mitsuharu2005-12-10 01:48:21 +0000
committerYAMAMOTO Mitsuharu2005-12-10 01:48:21 +0000
commitea1f60515f07762e5f12b7204a9f7954c99b0109 (patch)
treedf239d326b7aec10e342d8eca51d96904d12454b
parent97c688edf844fa4688c26f69dff7ed19729a0365 (diff)
downloademacs-ea1f60515f07762e5f12b7204a9f7954c99b0109.tar.gz
emacs-ea1f60515f07762e5f12b7204a9f7954c99b0109.zip
Require url when compiling. Call
mac-process-deferred-apple-events after loading init files. (mac-apple-event-map): New defvar. Define event handlers in it. (core-event, internet-event): New Apple event class symbols. (open-application, reopen-application, open-documents) (print-documents, open-contents, quit-application) (application-died, show-preferences, autosave-now, get-url): New Apple event ID symbols. (about): New HICommand ID symbol. (mac-event-spec, mac-event-ae): New macros. (mac-ae-parameter, mac-ae-list, mac-bytes-to-integer) (mac-ae-selection-range, mac-ae-text-for-search) (mac-ae-open-documents, mac-ae-text, mac-ae-get-url): New functions. (mac-application-menu-map): Remove keymap. Handlers for HICommand and Services menu events are now defined in mac-apple-event-map. (mac-drag-n-drop): Remove selection range handling.
-rw-r--r--lisp/term/mac-win.el214
1 files changed, 180 insertions, 34 deletions
diff --git a/lisp/term/mac-win.el b/lisp/term/mac-win.el
index e83734bfe33..064f9bd12bf 100644
--- a/lisp/term/mac-win.el
+++ b/lisp/term/mac-win.el
@@ -76,10 +76,12 @@
76(require 'menu-bar) 76(require 'menu-bar)
77(require 'fontset) 77(require 'fontset)
78(require 'dnd) 78(require 'dnd)
79(eval-when-compile (require 'url))
79 80
80(defvar mac-charset-info-alist) 81(defvar mac-charset-info-alist)
81(defvar mac-services-selection) 82(defvar mac-services-selection)
82(defvar mac-system-script-code) 83(defvar mac-system-script-code)
84(defvar mac-apple-event-map)
83(defvar x-invocation-args) 85(defvar x-invocation-args)
84 86
85(defvar x-command-line-resources nil) 87(defvar x-command-line-resources nil)
@@ -1148,7 +1150,7 @@ correspoinding TextEncodingBase value."
1148 1150
1149(define-key special-event-map [language-change] 'mac-handle-language-change) 1151(define-key special-event-map [language-change] 'mac-handle-language-change)
1150 1152
1151;;;; Selections and Services menu 1153;;;; Selections
1152 1154
1153;; Setup to use the Mac clipboard. 1155;; Setup to use the Mac clipboard.
1154(set-selection-coding-system mac-system-coding-system) 1156(set-selection-coding-system mac-system-coding-system)
@@ -1386,6 +1388,155 @@ in `selection-converter-alist', which see."
1386 (public.file-url . mac-select-convert-to-file-url) 1388 (public.file-url . mac-select-convert-to-file-url)
1387 ) 1389 )
1388 selection-converter-alist)) 1390 selection-converter-alist))
1391
1392;;;; Apple events, HICommand events, and Services menu
1393
1394;;; Event classes
1395(put 'core-event 'mac-apple-event-class "aevt") ; kCoreEventClass
1396(put 'internet-event 'mac-apple-event-class "GURL") ; kAEInternetEventClass
1397
1398;;; Event IDs
1399;; kCoreEventClass
1400(put 'open-application 'mac-apple-event-id "oapp") ; kAEOpenApplication
1401(put 'reopen-application 'mac-apple-event-id "rapp") ; kAEReopenApplication
1402(put 'open-documents 'mac-apple-event-id "odoc") ; kAEOpenDocuments
1403(put 'print-documents 'mac-apple-event-id "pdoc") ; kAEPrintDocuments
1404(put 'open-contents 'mac-apple-event-id "ocon") ; kAEOpenContents
1405(put 'quit-application 'mac-apple-event-id "quit") ; kAEQuitApplication
1406(put 'application-died 'mac-apple-event-id "obit") ; kAEApplicationDied
1407(put 'show-preferences 'mac-apple-event-id "pref") ; kAEShowPreferences
1408(put 'autosave-now 'mac-apple-event-id "asav") ; kAEAutosaveNow
1409;; kAEInternetEventClass
1410(put 'get-url 'mac-apple-event-id "GURL") ; kAEGetURL
1411;; Converted HICommand events
1412(put 'about 'mac-apple-event-id "abou") ; kHICommandAbout
1413
1414(defmacro mac-event-spec (event)
1415 `(nth 1 ,event))
1416
1417(defmacro mac-event-ae (event)
1418 `(nth 2 ,event))
1419
1420(defun mac-ae-parameter (ae &optional keyword type)
1421 (or keyword (setq keyword "----")) ;; Direct object.
1422 (if (not (and (consp ae) (equal (car ae) "aevt")))
1423 (error "Not an Apple event: %S" ae)
1424 (let ((type-data (cdr (assoc keyword (cdr ae))))
1425 data)
1426 (when (and type type-data)
1427 (setq data (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1428 (setq type-data (if data (cons type data) nil)))
1429 type-data)))
1430
1431(defun mac-ae-list (ae &optional keyword type)
1432 (or keyword (setq keyword "----")) ;; Direct object.
1433 (let ((desc (mac-ae-parameter ae keyword)))
1434 (cond ((null desc)
1435 nil)
1436 ((not (equal (car desc) "list"))
1437 (error "Parameter for \"%s\" is not a list" keyword))
1438 (t
1439 (if (null type)
1440 (cdr desc)
1441 (mapcar
1442 (lambda (type-data)
1443 (mac-coerce-ae-data (car type-data) (cdr type-data) type))
1444 (cdr desc)))))))
1445
1446(defun mac-bytes-to-integer (bytes &optional from to)
1447 (or from (setq from 0))
1448 (or to (setq to (length bytes)))
1449 (let* ((len (- to from))
1450 (extended-sign-len (- (1+ (ceiling (log most-positive-fixnum 2)))
1451 (* 8 len)))
1452 (result 0))
1453 (dotimes (i len)
1454 (setq result (logior (lsh result 8)
1455 (aref bytes (+ from (if (eq (byteorder) ?B) i
1456 (- len i 1)))))))
1457 (if (> extended-sign-len 0)
1458 (ash (lsh result extended-sign-len) (- extended-sign-len))
1459 result)))
1460
1461(defun mac-ae-selection-range (ae)
1462;; #pragma options align=mac68k
1463;; typedef struct SelectionRange {
1464;; short unused1; // 0 (not used)
1465;; short lineNum; // line to select (<0 to specify range)
1466;; long startRange; // start of selection range (if line < 0)
1467;; long endRange; // end of selection range (if line < 0)
1468;; long unused2; // 0 (not used)
1469;; long theDate; // modification date/time
1470;; } SelectionRange;
1471;; #pragma options align=reset
1472 (let ((range-bytes (cdr (mac-ae-parameter ae "kpos" "TEXT"))))
1473 (and range-bytes
1474 (list (mac-bytes-to-integer range-bytes 2 4)
1475 (mac-bytes-to-integer range-bytes 4 8)
1476 (mac-bytes-to-integer range-bytes 8 12)
1477 (mac-bytes-to-integer range-bytes 16 20)))))
1478
1479;; On Mac OS X 10.4 and later, the `open-document' event contains an
1480;; optional parameter keyAESearchText from the Spotlight search.
1481(defun mac-ae-text-for-search (ae)
1482 (let ((utf8-text (cdr (mac-ae-parameter ae "stxt" "utf8"))))
1483 (and utf8-text
1484 (decode-coding-string utf8-text 'utf-8))))
1485
1486(defun mac-ae-open-documents (event)
1487 (interactive "e")
1488 (let ((ae (mac-event-ae event)))
1489 (dolist (file-name (mac-ae-list ae nil 'undecoded-file-name))
1490 (if file-name
1491 (dnd-open-local-file (concat "file:" file-name) nil)))
1492 (let ((selection-range (mac-ae-selection-range ae))
1493 (search-text (mac-ae-text-for-search ae)))
1494 (cond (selection-range
1495 (let ((line (car selection-range))
1496 (start (cadr selection-range))
1497 (end (nth 2 selection-range)))
1498 (if (> line 0)
1499 (goto-line line)
1500 (if (and (> start 0) (> end 0))
1501 (progn (set-mark start)
1502 (goto-char end))))))
1503 ((stringp search-text)
1504 (re-search-forward
1505 (mapconcat 'regexp-quote (split-string search-text) "\\|")
1506 nil t)))))
1507 (raise-frame))
1508
1509(defun mac-ae-text (ae)
1510 (or (cdr (mac-ae-parameter ae nil "TEXT"))
1511 (error "No text in Apple event.")))
1512
1513(defun mac-ae-get-url (event)
1514 (interactive "e")
1515 (let* ((ae (mac-event-ae event))
1516 (parsed-url (url-generic-parse-url (mac-ae-text ae))))
1517 (if (string= (url-type parsed-url) "mailto")
1518 (url-mailto parsed-url)
1519 (error "Unsupported URL scheme: %s" (url-type parsed-url)))))
1520
1521;; Received when Emacs is launched without associated documents.
1522;; Accept it as an Apple event, but no Emacs event is generated so as
1523;; not to erase the splash screen.
1524(define-key mac-apple-event-map [core-event open-application] 0)
1525
1526;; Received when a dock or application icon is clicked and Emacs is
1527;; already running. Simply ignored. Another idea is to make a new
1528;; frame if all frames are invisible.
1529(define-key mac-apple-event-map [core-event reopen-application] 'ignore)
1530
1531(define-key mac-apple-event-map [core-event open-documents]
1532 'mac-ae-open-documents)
1533(define-key mac-apple-event-map [core-event show-preferences] 'customize)
1534(define-key mac-apple-event-map [core-event quit-application]
1535 'save-buffers-kill-emacs)
1536
1537(define-key mac-apple-event-map [internet-event get-url] 'mac-ae-get-url)
1538
1539(define-key mac-apple-event-map [hicommand about] 'display-splash-screen)
1389 1540
1390(defun mac-services-open-file () 1541(defun mac-services-open-file ()
1391 (interactive) 1542 (interactive)
@@ -1420,21 +1571,35 @@ in `selection-converter-alist', which see."
1420 (substitute-command-keys 1571 (substitute-command-keys
1421 "The text from the Services menu can be accessed with \\[yank]"))))) 1572 "The text from the Services menu can be accessed with \\[yank]")))))
1422 1573
1423(defvar mac-application-menu-map (make-sparse-keymap)) 1574(define-key mac-apple-event-map [services paste] 'mac-services-insert-text)
1424(define-key mac-application-menu-map [quit] 'save-buffers-kill-emacs) 1575(define-key mac-apple-event-map [services perform open-file]
1425(define-key mac-application-menu-map [services perform open-file]
1426 'mac-services-open-file) 1576 'mac-services-open-file)
1427(define-key mac-application-menu-map [services perform open-selection] 1577(define-key mac-apple-event-map [services perform open-selection]
1428 'mac-services-open-selection) 1578 'mac-services-open-selection)
1429(define-key mac-application-menu-map [services perform mail-selection] 1579(define-key mac-apple-event-map [services perform mail-selection]
1430 'mac-services-mail-selection) 1580 'mac-services-mail-selection)
1431(define-key mac-application-menu-map [services perform mail-to] 1581(define-key mac-apple-event-map [services perform mail-to]
1432 'mac-services-mail-to) 1582 'mac-services-mail-to)
1433(define-key mac-application-menu-map [services paste] 1583
1434 'mac-services-insert-text) 1584(defun mac-dispatch-apple-event (event)
1435(define-key mac-application-menu-map [preferences] 'customize) 1585 (interactive "e")
1436(define-key mac-application-menu-map [about] 'display-splash-screen) 1586 (let* ((binding (lookup-key mac-apple-event-map (mac-event-spec event)))
1437(global-set-key [menu-bar application] mac-application-menu-map) 1587 (service-message
1588 (and (keymapp binding)
1589 (cdr (mac-ae-parameter (mac-event-ae event) "svmg")))))
1590 (when service-message
1591 (setq service-message
1592 (intern (decode-coding-string service-message 'utf-8)))
1593 (setq binding (lookup-key binding (vector service-message))))
1594 (call-interactively binding)))
1595
1596(global-set-key [mac-apple-event] 'mac-dispatch-apple-event)
1597
1598;; Processing of Apple events are deferred at the startup time. For
1599;; example, files dropped onto the Emacs application icon can only be
1600;; processed when the initial frame has been created: this is where
1601;; the files should be opened.
1602(add-hook 'after-init-hook 'mac-process-deferred-apple-events)
1438 1603
1439;;; Do the actual Windows setup here; the above code just defines 1604;;; Do the actual Windows setup here; the above code just defines
1440;;; functions and variables that we use now. 1605;;; functions and variables that we use now.
@@ -1855,31 +2020,12 @@ Switch to a buffer editing the last file dropped."
1855 (y (cdr coords))) 2020 (y (cdr coords)))
1856 (if (and (> x 0) (> y 0)) 2021 (if (and (> x 0) (> y 0))
1857 (set-frame-selected-window nil window)) 2022 (set-frame-selected-window nil window))
1858 (mapcar (lambda (file-name) 2023 (dolist (file-name (nth 2 event))
1859 (if (listp file-name) 2024 (dnd-handle-one-url window 'private
1860 (let ((line (car file-name)) 2025 (concat "file:" file-name))))
1861 (start (car (cdr file-name)))
1862 (end (car (cdr (cdr file-name)))))
1863 (if (> line 0)
1864 (goto-line line)
1865 (if (and (> start 0) (> end 0))
1866 (progn (set-mark start)
1867 (goto-char end)))))
1868 (dnd-handle-one-url window 'private
1869 (concat "file:" file-name))))
1870 (car (cdr (cdr event)))))
1871 (raise-frame)) 2026 (raise-frame))
1872 2027
1873(global-set-key [drag-n-drop] 'mac-drag-n-drop) 2028(global-set-key [drag-n-drop] 'mac-drag-n-drop)
1874
1875;; By checking whether the variable mac-ready-for-drag-n-drop has been
1876;; defined, the event loop in macterm.c can be informed that it can
1877;; now receive Finder drag and drop events. Files dropped onto the
1878;; Emacs application icon can only be processed when the initial frame
1879;; has been created: this is where the files should be opened.
1880(add-hook 'after-init-hook
1881 '(lambda ()
1882 (defvar mac-ready-for-drag-n-drop t)))
1883 2029
1884;;;; Non-toolkit Scroll bars 2030;;;; Non-toolkit Scroll bars
1885 2031