And it works, but 1. I force ";" 2. how could I set the second one only when needed? Are there normally informations about closing char in modes? Thinking carefully I only remember the ";", I could just add the command to the local keymap of those modes.
I finally read the manual about keymaps and this works: (global-set-key (kbd "M-RET") 'newline-force)
But I can't define "Meta Shift Return" (as in Textmate), it always translates it to "M-RET". Maybe I guess it's not possible for how shift is treated, is that right?
(defun newline-force-close() (interactive) (end-of-line) (when (not (bobp)) ;; if we're at beginning of buffer, ;; the backward-char will beep :( ;; This works even in the case of ;; narrowing (e.g. we don't look outside ;; of the narrowed area. (save-excursion (backward-char 1) (when (looking-at ";" (insert ";")) (newline-and-indent))))
(defun newline-force-close() (interactive) (end-of-line) (when (not (bobp)) ;; if we're at beginning of buffer, ;; the backward-char will beep :( ;; This works even in the case of ;; narrowing (e.g. we don't look outside ;; of the narrowed area. (save-excursion (backward-char 1) (when (not (looking-at ";")) (insert ";")) (newline-and-indent))))
Very good thanks a lot! I modified to: (defun newline-force() "Goes to newline leaving untouched the rest of the line" (interactive) (progn (end-of-line) (newline-and-indent)))
(defun newline-force-close() (interactive) (end-of-line) (when (not (bobp)) ;; if we're at beginning of buffer, ;; the backward-char will beep :( ;; This works even in the case of ;; narrowing (e.g. we don't look outside ;; of the narrowed area. (when (not (looking-at ";")) (insert ";")) (newline-force)))
I mean with save excursion it was going back to the previous point, I want to go to the newline normally if I do it.. Maybe in this way it doesn't work with narrowing?
I'm still not able to bind it This: (global-set-key (kbd "M-S-RET") 'newline-force-close) Works but then when I press it says:
M-S-<return> is not defined. I guess pressing S-RET gives another key... But on org mode M-S-RET is well defined, so I guess there is a way to do it!
> I mean with save excursion it was going back to the previous point, > I want to go to the newline normally if I do it.. > Maybe in this way it doesn't work with narrowing?
It was a mistake on my side. It is better now :) I wrote it without testing it --
where foo-mode-hook is the name of the mode you're considering (e.g. java-mode-hook or c-mode-hook) and foo-map is the keymap it is using (e.g. java-mode-map or c-mode-map).
Thanks a lot, another couple of things. I want to add the key to a list of modes, like (dolist (mode '(python-mode c-mode ruby-mode java-mode glsl-mode)) (add-hook (variable (concat mode "-hook")) '(lambda nil (define-key foo-mode-map [M-S-return] (function newline-force-close)))))
But I don't remember how to pass a variable in a lambda, and maybe it's just not possible at all..
I also want to be able to customize the character of return, so I created the var (defvar newline-force-close-symbol ";" "character used for newline-force-close function")
Which for example in python-mode-map would be changed to ":".
So how should I handle this? with "make-local-variable"? Or maybe it's easier to add an argument to the newline-force-close function and call it accordingly?
Well, after some thoughts, here is what I propose. -------------------------------------------------------- (defvar newline-force-close-alist '((python-mode . [":" python-mode-hook python-mode-map]) (java-mode . [";" java-mode-hook java-mode-map])) "Alist defining WHAT is to be used for newline-force-close function,\n WHAT being a vector [closing-char mode-hook mode-map]")
(defun newline-force() "Goes to newline leaving untouched the rest of the line" (interactive) (progn (end-of-line) (newline-and-indent)))
(defun newline-force-close() (interactive) (end-of-line) (let ((closing-way (assoc major-mode newline-force-close-alist)) closing-char) (if (not closing-way) ;; most probably useless: (error "No closing character defined for %s. See newline-force-close-alist" mode-name) (setq closing-char (aref (cdr closing-way) 0)) (when (not (bobp)) ;; if we're at beginning of buffer, ;; the backward-char will beep :( ;; This works even in the case of ;; narrowing (e.g. we don't look outside ;; of the narrowed area. (when (not (looking-at closing-char)) (insert closing-char)) (newline-force)))))
If you want a variable newline-force-close-symbol-in-java then you should set this variable via customize and use the :set part to initialize "intelligently" newline-force-close-alist. Some mode have several hooks, to be run at different levels of the initialization, the hook name may change, the keymap may also have a different name, though they are mostly in the standart format.
I did a minor modification, I think now is more comfortable. I
(defun newline-force-close() (interactive) (end-of-line) (let ((closing-way (assoc major-mode newline-force-close-alist)) closing-char) ;; Setting the user defined or the constant if not found (if (not closing-way) (progn (message "closing char not defined for this mode, using default") (setq closing-char default-closing-char)) (setq closing-char (aref (cdr closing-way) 0))) (when (not (bobp)) ;; if we're at beginning of buffer, ;; the backward-char will beep :( ;; This works even in the case of ;; narrowing (e.g. we don't look outside ;; of the narrowed area. (when (not (looking-at closing-char)) (insert closing-char)) (newline-force))))
(defconst default-closing-char ";" "default closing char, change in newline-force-close-alist if needed")
If it doesn't found it defined for that mode just use the default. That's easier than setting ";" for all the c-like modes..