0

I want to write an Elisp script that will

  • turn the auto-save-visited-mode on, if it is off, and
  • do nothing if it is already turned on.

How can I find out whether or not this mode is enabled at the moment in Elisp?

Update 1: M-x describe-mode shows a list of all enabled minor modes.

Screenshot showing minor mode list

Update 2: According to this answer you can display a list of all active minor modes using this code:

(defun which-active-modes ()
  "Give a message of which minor modes are enabled in the current buffer."
  (interactive)
  (let ((active-modes))
    (mapc (lambda (mode) (condition-case nil
                             (if (and (symbolp mode) (symbol-value mode))
                                 (add-to-list 'active-modes mode))
                           (error nil) ))
          minor-mode-list)
    (message "Active modes are %s" active-modes)))

(which-active-modes)

Update 3: Final version that seems to work:

(if auto-save-visited-mode
    (message "The mode is on")
    (message "The mode is off")
)
Glory to Russia
  • 17,289
  • 56
  • 182
  • 325

2 Answers2

2

You can test if it enabled by simply evaluating the variable auto-save-visited-mode, as discussed in its documentation, C-h vauto-save-visited-mode.

To enable it, just call (auto-save-visited-mode 1). This will enable it or do nothing.

Edit: (auto-save-visited-mode 1) by itself will probably accomplish what you want, but it does run the setup code / call the mode hooks again even if it was previously setup. So, you could use (unless auto-save-visited-mode (auto-save-visited-mode)) to avoid that.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
1

If the mode is on then the value of its mode variable is non-nil. If it is off then the value is nil.

Drew
  • 29,895
  • 7
  • 74
  • 104