Skip to content

Commit

Permalink
Added modes for auto-mode-setting, -publishing and -subscribing
Browse files Browse the repository at this point in the history
* rudel-mode.el (header): updated copyright
  (rudel-auto-publish-exclude-regexp): new customization option;
  control which buffers are excluded from auto-publishing
  (rudel-auto-publish-predicate): new customization option; function
  called to decide whether to auto-publish buffers
  (rudel-auto-subscribe-predicate): new customization option; function
  called to decide whether to auto-subscribe to documents
  (rudel-auto-adjust-mode): new function; automatically adjust major
  mode of a buffer
  (global-rudel-auto-choose-mode-minor-mode): new global minor mode;
  adjust major mode of buffers after subscribing
  (rudel-auto-publish-not-exluded-p): new function; predicate which
  determines whether a buffer should be excluded from auto-publishing
  (rudel-auto-maybe-publish-buffer): new function; publish buffer if
  appropriate
  (rudel-publish-all-buffers): new function; publish all appropriate
  buffers
  (global-rudel-auto-publish-minor-mode): new global minor mode;
  auto-publish appropriate buffers
  (rudel-auto-subscribe-not-excluded-p): new function; predicate which
  determines whether a document should be subscribed
  (rudel-auto-maybe-subscribe-to-document): new function; subscribe to
  a document if appropriate
  (rudel-subscribe-to-all-documents): new function; subscribe to all
  appropriate documents
  (global-rudel-auto-subscribe-minor-mode): new global minor modes;
  subscribe to all appropriate documents as they become available
  (rudel-minor-menu): added entries for
  `global-rudel-auto-choose-mode-minor-mode',
  `global-rudel-auto-publish-minor-mode' and
  `global-rudel-auto-subscribe-minor-mode'
  • Loading branch information
scymtym committed Mar 12, 2010
1 parent 0ca75b5 commit afdfcb0
Show file tree
Hide file tree
Showing 2 changed files with 241 additions and 3 deletions.
36 changes: 36 additions & 0 deletions ChangeLog
@@ -1,3 +1,39 @@
2010-03-12 Jan Moringen <scymtym@users.sourceforge.net>

Added modes for auto-mode-setting, -publishing and -subscribing
* rudel-mode.el (header): updated copyright
(rudel-auto-publish-exclude-regexp): new customization option;
control which buffers are excluded from auto-publishing
(rudel-auto-publish-predicate): new customization option;
function called to decide whether to auto-publish buffers
(rudel-auto-subscribe-predicate): new customization option;
function called to decide whether to auto-subscribe to documents
(rudel-auto-adjust-mode): new function; automatically adjust major
mode of a buffer
(global-rudel-auto-choose-mode-minor-mode): new global minor mode;
adjust major mode of buffers after subscribing
(rudel-auto-publish-not-exluded-p): new function; predicate which
determines whether a buffer should be excluded from
auto-publishing
(rudel-auto-maybe-publish-buffer): new function; publish buffer if
appropriate
(rudel-publish-all-buffers): new function; publish all appropriate
buffers
(global-rudel-auto-publish-minor-mode): new global minor mode;
auto-publish appropriate buffers
(rudel-auto-subscribe-not-excluded-p): new function; predicate
which determines whether a document should be subscribed
(rudel-auto-maybe-subscribe-to-document): new function; subscribe
to a document if appropriate
(rudel-subscribe-to-all-documents): new function; subscribe to all
appropriate documents
(global-rudel-auto-subscribe-minor-mode): new global minor modes;
subscribe to all appropriate documents as they become available
(rudel-minor-menu): added entries for
`global-rudel-auto-choose-mode-minor-mode',
`global-rudel-auto-publish-minor-mode' and
`global-rudel-auto-subscribe-minor-mode'

2010-03-10 Jan Moringen <scymtym@users.sourceforge.net>

Improved some error messages in infinote backend
Expand Down
208 changes: 205 additions & 3 deletions rudel-mode.el
@@ -1,6 +1,6 @@
;;; rudel-mode.el --- Global and buffer-local Rudel minor modes
;;
;; Copyright (C) 2008, 2009 Jan Moringen
;; Copyright (C) 2008, 2009, 2010 Jan Moringen
;;
;; Author: Jan Moringen <scymtym@users.sourceforge.net>
;; Keywords: rudel, mode
Expand Down Expand Up @@ -95,10 +95,40 @@
(when (featurep 'rudel-mode)
(rudel-mode-line-publish-state--options-changed))))

(defcustom rudel-auto-publish-exclude-regexp
(rx
(or (group "*" (0+ anything) "*")
" SPEEDBAR"))
"Buffer matching this regular expression are not auto-published.
This option only has an effect when `rudel-auto-publish-predicate' is
set to `rudel-auto-publish-not-excluded-p' "
:group 'rudel
:type 'string)

(defcustom rudel-auto-publish-predicate
#'rudel-auto-publish-not-excluded-p
"This function decides whether to auto-publish buffers."
:group 'rudel
:type '(choice (const :tag "Exclude well-known unsuitable buffers"
rudel-auto-publish-not-excluded-p)
(function :tag "Other function")))

(defcustom rudel-auto-subscribe-predicate
#'rudel-auto-subscribe-not-excluded-p
"This function decides whether to auto-subscribe to documents."
:group 'rudel
:type '(choice (const :tag "Exclude well-known unsuitable documents"
rudel-auto-subscribe-not-excluded-p)
(function :tag "Other function")))

(dolist (v '(rudel-header-subscriptions-use-images
rudel-header-subscriptions-separator
rudel-mode-line-publish-state-unpublished-string
rudel-mode-line-publish-state-published-string))
rudel-mode-line-publish-state-published-string
rudel-auto-publish-exclude-regexp
rudel-auto-publish-predicate
rudel-auto-subscribe-predicate))
(put v 'save-local-variable t))


Expand Down Expand Up @@ -511,6 +541,166 @@ line publish state mode; otherwise, turn it off."
rudel-mode-line-publish-state-minor-mode
:group 'rudel)


;;; Auto choose mode minor mode
;;

(defun rudel-auto-adjust-mode (document buffer)
"Automatically choose an appropriate major mode for BUFFER.
Note: The idea of let-binding `buffer-file-name' is taken from
http://stackoverflow.com/questions/2375473/"
(with-current-buffer buffer
(if (hack-local-variables t)
(hack-local-variables)
(let ((buffer-file-name (buffer-name)))
(set-auto-mode)))))

;;;###autoload
(define-minor-mode global-rudel-auto-choose-mode-minor-mode
"Toggle the global Rudel auto choose mode minor mode.
When this mode is enabled, Rudel will try to set the mode of
created buffers when subscribing to documents.
With argument ARG positive, turn on the mode. Negative, turn off
the mode. nil means to toggle the mode."
:init-value nil
:global t
:group 'rudel
:lighter " +M"
(cond

;; Mode is being enabled.
(global-rudel-auto-choose-mode-minor-mode
(add-hook 'rudel-document-attach-hook
#'rudel-auto-adjust-mode))

;; Mode is being disabled.
(t
(remove-hook 'rudel-document-attach-hook
#'rudel-auto-adjust-mode)))
)


;;; Auto publish minor mode
;;

(defun rudel-auto-publish-not-exluded-p (buffer)
"Nil when BUFFER should be excluded from auto-publishing.
This ensures:
+ BUFFER's name does not look like `rudel-auto-publish-exclude-regexp'
+ there is no document named like BUFFER
+ BUFFER does not have an associated document"
(and (not (string-match-p rudel-auto-publish-exclude-regexp
(buffer-name buffer)))
(not (rudel-find-document
rudel-current-session (buffer-name buffer)))
(not (with-current-buffer buffer
rudel-buffer-document))))

(defun rudel-auto-maybe-publish-buffer (&optional buffer)
"Publish BUFFER if it satisfies `rudel-auto-publish-predicate'.
If BUFFER is nil, use the current buffer."
(when (not buffer)
(setq buffer (current-buffer)))
(when (and rudel-current-session
(funcall rudel-auto-publish-predicate buffer))
(rudel-publish-buffer buffer)))

(defun rudel-publish-all-buffers ()
"Publish all buffer satisfying `rudel-auto-publish-predicate'."
(interactive)
(mapc #'rudel-auto-maybe-publish-buffer (buffer-list)))

;;;###autoload
(define-minor-mode global-rudel-auto-publish-minor-mode
"Toggle the global Rudel auto publish minor mode.
When this mode is enabled, Rudel will automatically publish all
buffers that satisfy the value of `rudel-auto-publish-predicate'.
With argument ARG positive, turn on the mode. Negative, turn off
the mode. nil means to toggle the mode."
:init-value nil
:global t
:group 'rudel
:lighter " +P"
(cond

;; Mode is being enabled.
(global-rudel-auto-publish-minor-mode
;; Publish all buffers.
(rudel-publish-all-buffers)

;; Publish new buffers as soon as we notice them after a command.
;; TODO is this too expensive?
(add-hook 'post-command-hook
#'rudel-auto-maybe-publish-buffer))

;; Mode is being disabled.
(t
;; Stop looking for new buffers.
(remove-hook 'post-command-hook
#'rudel-auto-maybe-publish-buffer)))
)


;;; Auto subscribe minor mode
;;

(defun rudel-auto-subscribe-not-excluded-p (document)
"Nil if DOCUMENT should be excluded from auto-publishing.
This ensures:
+ DOCUMENT is not attached to a buffer
+ there is no buffer named like DOCUMENT"
(and (not (rudel-attached-p document))
(not (get-buffer (object-name-string document)))))

(defun rudel-auto-maybe-subscribe-to-document (document)
"Subscribe to DOCUMENT if it satisfies `rudel-auto-subscribe-predicate'."
(when (funcall rudel-auto-subscribe-predicate document)
(rudel-subscribe document)))

(defun rudel-subscribe-to-all-documents ()
"Subscribe to all documents satisfying `rudel-auto-subscribe-predicate'."
(interactive)
(when rudel-current-session
(mapc #'rudel-auto-maybe-subscribe-to-document
(oref rudel-current-session :documents))))
;; TODO make reader `rudel-documents' or `rudel-session-documents'

;;;###autoload
(define-minor-mode global-rudel-auto-subscribe-minor-mode
"Toggle the global Rudel auto subscribe minor mode.
When this mode is enabled, Rudel will automatically subscribe to
all newly published documents.
With argument ARG positive, turn on the mode. Negative, turn off
the mode. nil means to toggle the mode."
:init-value nil
:global t
:group 'rudel
:lighter " +S"
(cond

;; Mode is being enabled.
(global-rudel-auto-subscribe-minor-mode
;; TODO handle case when there is no session (yet)
;; Subscribe to all available documents.
(rudel-subscribe-to-all-documents)

;; Monitor the session for new documents. Subscribe when they
;; appear.
(add-hook 'rudel-session-add-document-hook
(lambda (session document)
(rudel-auto-maybe-subscribe-to-document document))))

;; Mode is being disabled.
(t
;; Stop looking for new documents.
(remove-hook 'rudel-session-add-document-hook
(lambda (session document)
(rudel-auto-maybe-subscribe-to-document document)))))
)


;;; Global Rudel mode, menu and keymap
;;
Expand Down Expand Up @@ -589,7 +779,19 @@ line publish state mode; otherwise, turn it off."
[ "Globally"
global-rudel-mode-line-publish-state-mode
:style toggle
:selected global-rudel-mode-line-publish-state-mode ] ) ) )
:selected global-rudel-mode-line-publish-state-mode ] )
[ "Automatically guess Major-mode"
global-rudel-auto-choose-mode-minor-mode
:style toggle
:selected global-rudel-auto-choose-mode-minor-mode ]
[ "Automatically publish Buffers"
global-rudel-auto-publish-minor-mode
:style toggle
:selected global-rudel-auto-publish-minor-mode ]
[ "Automatically subscribe to Documents"
global-rudel-auto-subscribe-minor-mode
:style toggle
:selected global-rudel-auto-subscribe-minor-mode ] ) )
)
)

Expand Down

0 comments on commit afdfcb0

Please sign in to comment.