emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: cc-mode adds newlines


From: Stefan Monnier
Subject: Re: cc-mode adds newlines
Date: Mon, 22 Nov 2004 10:52:17 -0500
User-agent: Gnus/5.11 (Gnus v5.11) Emacs/21.3.50 (gnu/linux)

> I think we should add a variable require-final-newline-modes which
> would be a list of mode names.  run-mode-hook could see if the current
> mode is in require-final-newline-modes, and if so, set
> require-final-newline to require-final-newline-mode-value.  The
> default for require-final-newline-mode-value would be :ask, but if a
> user sets it to t, then all the appriate modes would set
> require-final-newline to t.

I think that would be silly.  Tons of variable might want to be set
differently in different modes.  Do we really want to add all the
corresponding foo-modes side-variables?
I think we'd be better off with a more generic solution.

I.e. we should aim for a way to specify per-mode settings in custom.
That would be a lot more useful and wouldn't require adding any
new variable.

Here is one way it could work:

- instead of `:type foo' use `:type (per-mode foo)' where (per-mode foo)
  stands for something like (repeat (cons mode foo)) where the list of modes
  can be obtained doing

    (let ((modes nil) mode)
      (mapatoms (lambda (s)
                  (if (and (string-match "-mode-hook\\'" (symbol-name s))
                           (fboundp (intern (concat (setq mode (substring 
(symbol-name s) 0 (match-beginning 0))) "-mode"))))
                      (push mode modes))))
      modes)

- set custom-get and custom-set functions could look like:

(defun custom-set-per-mode (var vals)
  (put var 'custom-per-mode vals)
  (dolist (setting vals)
    (let ((mode (car setting))
          (value (cdr setting)))
      (if (eq t mode)
          (set-default var value)
        (let ((fun (intern (concat "custom-set-" mode "-" (symbol-name var)))))
          (unless (fboundp fun)
            (fset fun
                  `(lambda ()
                     (let ((val (assoc ',mode (get ',var 'custom-per-mode))))
                       (if val (set (make-local-variable ',var) (cdr val)))))))
          (add-hook (intern (concat mode "-mode-hook")) fun))))))

(defun custom-get-per-mode (var)
  (let* ((val (get var 'custom-per-mode))
         (defval (assq t val)))
    (unless (eq (cdr defval) (default-value var))
      ;; It was changed somehow...
      (setcdr defval (default-value var)))
    val))

Ideally we should even provide such per-mode settings for *all* custom
variables, without having to change the `defcustom'.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]