summaryrefslogtreecommitdiffstats
path: root/emacs.d/nxhtml/util/as-external.el
blob: b1330c14729e83468b722f4630913cdb655f2c70 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
;;; as-external.el --- Emacs as an external editor to other apps
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: Mon Jun 25 19:02:49 2007
(defconst as-external:version "0.6") ;;Version:
;; Last-Updated: 2009-08-04 Tue
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;;   None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;;  This little library should make it easier to use Emacs as an
;;  external editor in certain cases. One such case is when want to
;;  use Emacs as the external editor with the Firefox add-on "It's All
;;  Text".
;;
;;  See variable `as-external-mode' for more information.
;;
;;
;;; A note on the implementation:
;;
;; You may wonder why this does not use `auto-mode-alist' since it
;; checks the file name in nearly the same way? It is perhaps possible
;; to use that, but there are two things to be aware of:
;;
;; 1. The choice made must override other possible choices.
;;
;; 2. Beside the file name the implementation here also checks if the
;;    buffer has clients waiting. That makes the check more reliable.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:

(eval-when-compile (require 'cl))
(eval-when-compile (require 'html-write nil t))
(eval-when-compile (require 'mlinks nil t))
(eval-when-compile (require 'mumamo nil t))
(eval-when-compile (require 'nxhtml-mode nil t))
(eval-when-compile (require 'ourcomments-util nil t))
(eval-when-compile (require 'pause nil t))
(eval-when-compile (require 'server))
(eval-when-compile (require 'wikipedia-mode nil t))
(eval-and-compile  (require 'wrap-to-fill nil t))

;;;###autoload
(defgroup as-external nil
  "Settings related to Emacs as external editor."
  :group 'nxhtml
  :group 'external)

(defcustom as-external-its-all-text-regexp "/itsalltext/"
  "Regular expression matching It's All Text buffer's file."
  :type 'regexp
  :group 'as-external)

(defcustom as-external-alist
  '(
    ("/itsalltext/.*wiki" as-external-for-wiki)
    ("/itsalltext/.*mail" as-external-for-mail-mode)
    ("/itsalltext/"       as-external-for-xhtml)
   )
  "List to determine setup if Emacs is used as an external Editor.
Element in this list should have the form

  \(FILE-REGEXP BUFFER-SETUP)

where FILE-REGEXP should be a regular expression to match
`buffer-file-name'. If it matches then BUFFER-SETUP should be
called in the buffer.

* Tip when using Firefox's add-on It's All Text: It looks like
  the file name used will be constructed from the host url. For
  example if your are editing something on
  http://www.emacswiki.org/ the file name may be something like
  'www.emacswiki.org.283b1y212e.html'.


The list is processed by `as-external-setup'. Note that the first
match is used!

The default entries in this list supports for Firefox addon It's
All Text:

- `as-external-for-xhtml'.  For text areas on web pages where you
  can enter some XHTML code, for example blog comment fields.

- `as-external-for-mail-mode', for editing web mail messages.

- `as-external-for-wiki', for mediawiki.

See also `as-external-mode'."
  :type '(repeat
          (list (choice (variable :tag "Regexp variable")
                        regexp)
                command))
  :group 'as-external)

(defcustom as-external-its-all-text-coding 'utf-8
  "Coding system to use for It's All Text buffers.
See also `as-external-for-xhtml'."
  :type '(choice (const :tag "No special coding system" nil)
                 coding-system)
  :group 'as-external)

(defun as-external-fall-back (msg)
  "Fallback to text-mode if necessary."
  (text-mode)
  (lwarn t :warning "%s. Using text-mode" msg))

;;;###autoload
(defun as-external-for-xhtml ()
  "Setup for Firefox addon It's All Text to edit XHTML.
It's All Text is a Firefox add-on for editing textareas with an
external editor.
See URL `https://addons.mozilla.org/en-US/firefox/addon/4125'.

In this case Emacs is used to edit textarea fields on a web page.
The text will most often be part of a web page later, like on a
blog.  Therefore turn on these:

- `nxhtml-mode' since some XHTML tags may be allowed.
- `nxhtml-validation-header-mode' since it is not a full page.
- `wrap-to-fill-column-mode' to see what you are writing.
- `html-write-mode' to see it even better.

Also bypass the question for line end conversion when using
emacsw32-eol."
  (interactive)
  (if (not (fboundp 'nxhtml-mode))
      (as-external-fall-back "Can't find nXhtml")
    (nxhtml-mode)
    (nxhtml-validation-header-mode 1)
    (set (make-local-variable 'wrap-to-fill-left-marg-modes)
         '(nxhtml-mode fundamental-mode))
    (wrap-to-fill-column-mode 1)
    ;;(visible-point-mode 1)
    (when (fboundp 'html-write-mode) (html-write-mode 1))
    (when (boundp 'emacsw32-eol-ask-before-save)
      (make-local-variable 'emacsw32-eol-ask-before-save)
      (setq emacsw32-eol-ask-before-save nil))))


(defvar as-external-mail-mode-comment-pattern "^>.*$"
  "Regular expression for a comment line.")

(defvar as-external-mail-mode-email-pattern
  (concat "[a-z0-9$%(*-=?[_][^<>\")!;:,{}]*"
          "\@"
          "\\(?:[a-z0-9\-]+\.\\)+[a-z0-9]\\{2,4\\}")
  "Regular expression for a mail address.")

(defvar as-external-mail-mode-font-lock-keywords
  (list
   (list as-external-mail-mode-comment-pattern
         '(0 font-lock-comment-face))
   ;; (list as-external-mail-mode-email-pattern
   ;;       '(0 font-lock-keyword-face))
   ))

;;;###autoload
(define-derived-mode as-external-for-mail-mode text-mode "ExtMail "
  "Setup for Firefox addon It's All Text to edit mail.
Set normal mail comment markers in column 1 (ie >).

Set `fill-column' to 90 and enable `wrap-to-fill-column-mode' so
that it will look similar to how it will look in the sent plain
text mail.

See also `as-external-mode'."
  ;; To-do: Look at http://globs.org/articles.php?lng=en&pg=2
  (set (make-local-variable 'comment-column) 0)
  (set (make-local-variable 'comment-start) ">")
  (set (make-local-variable 'comment-end)   "")
  (set (make-local-variable 'font-lock-defaults)
       '((as-external-mail-mode-font-lock-keywords) nil))
  (setq fill-column 90)
  (mlinks-mode 1)
  (wrap-to-fill-column-mode 1))

;;;###autoload
(defun as-external-for-wiki ()
  "Setup for Firefox addon It's All Text to edit MediaWikis."
  (interactive)
  (require 'wikipedia-mode nil t)
  (if (not (featurep 'wikipedia-mode))
      (as-external-fall-back "Can't find file wikipedia-mode.el")
    (wikipedia-mode)))


;;;###autoload
(define-minor-mode as-external-mode
  "If non-nil check if Emacs is called as external editor.
When Emacs is called as an external editor for example to edit
text areas on a web page viewed with Firefox this library tries
to help to setup the buffer in a useful way. It may for example
set major and minor modes for the buffer.

This can for example be useful when blogging or writing comments
on blogs.

See `as-external-alist' for more information."
  :global t
  :group 'as-external
  ;;(modify-coding-system-alist 'file "/itsalltext/" as-external-its-all-text-coding)
  (let ((coding-entry
         (cons
          as-external-its-all-text-regexp
          (cons as-external-its-all-text-coding
                as-external-its-all-text-coding))))
    ;;(message "as-external-mode=%s" as-external-mode)
    (if as-external-mode
        (progn
          (add-to-list 'file-coding-system-alist coding-entry)
          (add-hook 'server-visit-hook 'as-external-setup t))
      (setq file-coding-system-alist
            (delq coding-entry file-coding-system-alist))
      (remove-hook 'server-visit-hook 'as-external-setup))))

(defun as-external-setup ()
  "Check if Emacs is used as an external editor.
If so then turn on useful major and minor modes.
This is done by checking `as-external-alist'."
  (condition-case err
      (as-external-setup-1)
    (error (message "as-external-setup error: %s" err))))

(defvar as-external-my-frame nil)
(make-variable-buffer-local 'as-external-my-frame)

(defvar as-external-last-buffer nil)

(defun as-external-server-window-fix-frames ()
  (condition-case err
      (with-current-buffer as-external-last-buffer
        (unless (buffer-live-p pause-buffer)
          (remove-hook 'pause-break-exit-hook 'as-external-server-window-fix-frames)
          (setq as-external-my-frame (or as-external-my-frame
                                         (make-frame)))
          (dolist (f (frame-list))
            (unless (eq f as-external-my-frame)
              (lower-frame f)))
          (raise-frame as-external-my-frame)))
    (error (message "%s" (error-message-string err)))))

(defun as-external-server-window (buffer)
  (setq server-window nil)
  (with-current-buffer buffer
    (setq as-external-last-buffer (current-buffer))
    (run-with-idle-timer 2 nil 'as-external-server-window-fix-frames)
    (add-hook 'pause-break-exit-hook 'as-external-server-window-fix-frames)
    (add-hook 'kill-buffer-hook 'as-external-delete-my-frame nil t)))

(defun as-external-delete-my-frame ()
  (let ((win (and (frame-live-p as-external-my-frame)
                  (get-buffer-window nil as-external-my-frame))))
    (when (and win
               (= 1 (length (window-list as-external-my-frame 'no-mini))))
      (delete-frame as-external-my-frame)
      (lower-frame))))

(defun as-external-setup-1 ()
  ;; Fix-me: How does one know if the file names are case sensitive?
  (unless (when (boundp 'nowait) nowait) ;; dynamically bound in `server-visit-files'
    (unless server-window
      ;; `server-goto-toplevel' has been done here.
      ;; Setup to use a new frame
      (setq server-window 'as-external-server-window))
    (catch 'done
      (dolist (rec as-external-alist)
        (let ((file-regexp (car rec))
              (setup-fun   (cadr rec)))
          (when (symbolp file-regexp)
            (setq file-regexp (symbol-value file-regexp)))
          (when (string-match file-regexp (buffer-file-name))
            (funcall setup-fun)
            (throw 'done t)))))))

(provide 'as-external)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; as-external.el ends here