Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Allow users to customize the default major mode, and to specify URL p…

…attern -> major mode mappings
  • Loading branch information...
commit 0b6cba39ff0e48314914d678605723c1b7fa4f7e 1 parent 314db1d
@purcell authored
Showing with 47 additions and 8 deletions.
  1. +47 −8 servers/edit-server.el
View
55 servers/edit-server.el
@@ -29,9 +29,16 @@
;; (require 'edit-server)
;; (edit-server-start)))
;;
-;; Buffers are edited in `text-mode' by default;
-;; to use a different mode, set it in `edit-server-start-hook'.
-;; For example:
+;; Buffers are edited in `text-mode' by default; to use a different
+;; mode, change `edit-server-default-major-mode' or customize
+;; `edit-server-url-major-mode-alist' to specify major modes based on
+;; the remote URL:
+;;
+;; (setq edit-server-url-major-mode-alist
+;; '(("github\\.com" . markdown-mode)))
+;;
+;; Alternatively, set the mode in `edit-server-start-hook'. For
+;; example:
;;
;; (add-hook 'edit-server-start-hook
;; (lambda ()
@@ -100,6 +107,24 @@ If nil, the new frame will use the existing `default-frame-alist' values."
(symbol :tag "Parameter")
(sexp :tag "Value"))))
+(defcustom edit-server-default-major-mode
+ 'text-mode
+ "The default major mode to use in editing buffers, if no other
+mode is selected by `edit-server-url-major-mode-alist'."
+ :group 'edit-server
+ :type 'function)
+
+(defcustom edit-server-url-major-mode-alist
+ nil
+ "A-list of patterns and corresponding functions; when the first
+pattern is encountered which matches `edit-server-url', the
+corresponding function will be called in order to set the desired
+major mode. If no pattern matches,
+`edit-server-default-major-mode' will be used."
+ :group 'edit-server
+ :type '(alist :key-type (string :tag "Regexp")
+ :value-type (function :tag "Major mode function")))
+
(defcustom edit-server-new-frame-mode-line t
"Show the emacs frame's mode-line if set to t; hide if nil."
:group 'edit-server
@@ -345,6 +370,20 @@ If `edit-server-verbose' is non-nil, then STRING is also echoed to the message l
(raise-frame)
nil))
+(defun edit-server-choose-major-mode ()
+ "Use `edit-server-url-major-mode-alist' to choose a major mode
+initialization function based on `edit-server-url', or fall back
+to `edit-server-default-major-mode'"
+ (let ((pairs edit-server-url-major-mode-alist)
+ (mode edit-server-default-major-mode))
+ (while pairs
+ (let ((entry (car pairs)))
+ (if (string-match (car entry) edit-server-url)
+ (setq mode (cdr entry)
+ pairs nil)
+ (setq pairs (cdr pairs)))))
+ (funcall mode)))
+
(defun edit-server-create-edit-buffer(proc)
"Create an edit buffer, place content in it and save the network
process for the final call back"
@@ -357,11 +396,11 @@ If `edit-server-verbose' is non-nil, then STRING is also echoed to the message l
(set-buffer-multibyte t))) ; djb
(copy-to-buffer buffer (point-min) (point-max))
(with-current-buffer buffer
- ;; use `text-mode' by default, but allow
- ;; `edit-server-start-hook' to override it however, (re)setting
- ;; the minor mode seems to clear the buffer-local variables that
- ;; we depend upon for the response, so call the hooks early on
- (text-mode)
+ (edit-server-choose-major-mode)
+ ;; Allow `edit-server-start-hook' to override the major mode.
+ ;; (re)setting the minor mode seems to clear the buffer-local
+ ;; variables that we depend upon for the response, so call the
+ ;; hooks early on
(run-hooks 'edit-server-start-hook)
(not-modified)
(add-hook 'kill-buffer-hook 'edit-server-abort* nil t)

2 comments on commit 0b6cba3

@stsquad

I'm thinking about this. I've seen a number of patches that change various handling with respect to mode and I just want to think through the consequences for existing users.

Of course now we have the new hook functions anything is possible for the user and I'm wondering if this sort of alist magic is best handled by users own setup?

@purcell
Owner

I don't think it's an either/or choice. The hooks are great as a general extension mechanism, but it also seems idiomatic to allow customization of behavior that users are known to routinely override, particularly when the defaults are backwards-compatible.
Cheers,
-Steve

Please sign in to comment.
Something went wrong with that request. Please try again.