Gmail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Clever newline
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  13 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Andrea Crotti  
View profile  
 More options Nov 2, 4:23 pm
Newsgroups: comp.emacs
From: Andrea Crotti <kerny...@gmail.com>
Date: Mon, 2 Nov 2009 13:23:28 -0800 (PST)
Local: Mon, Nov 2 2009 4:23 pm
Subject: Clever newline
   I think a really nice thing in emacs is the ability to go to a
newline even if in the middle of the actual line.

   Another nice thing always related is doing the same thing but
closing the line if needed.

   For example in (cc|java)-mode you should insert ";" automatically.

   So I did this:

   ((defun newline-force()
     (interactive)
     (progn
       (end-of-line)
       (newline-and-indent)))

   (defun newline-force-close()
     (interactive)
     (progn
       (end-of-line)
       (insert ";")
       (newline-and-indent)))

   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?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier  
View profile  
 More options Nov 3, 7:54 am
Newsgroups: comp.emacs
From: Olivier <O...@nowhere.wd>
Date: Tue, 03 Nov 2009 13:54:24 +0100
Local: Tues, Nov 3 2009 7:54 am
Subject: Re: Clever newline
Andrea Crotti a écrit :
[...]

>    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".

(global-set-key (kbd "M-S-RET") 'newline-force-close)

should do. The distinction between M (meta) and C (control)
is not always clear.
A.O.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier  
View profile  
 More options Nov 3, 7:58 am
Newsgroups: comp.emacs
From: Olivier <O...@nowhere.wd>
Date: Tue, 03 Nov 2009 13:58:58 +0100
Local: Tues, Nov 3 2009 7:58 am
Subject: Re: Clever newline
Andrea Crotti a écrit :

(defun newline-force-close()
     (interactive)
       (progn
         (end-of-line)
         (let ((doit nil))
            (save-excursion
             (backward-char 1)
              (setq doit (looking-at ";")))
          (when doit
            (insert ";"))
         (newline-and-indent)))

should do what you need.
A.O.


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier  
View profile  
 More options Nov 4, 3:43 am
Newsgroups: comp.emacs
From: Olivier <O...@nowhere.wd>
Date: Wed, 04 Nov 2009 09:43:57 +0100
Local: Wed, Nov 4 2009 3:43 am
Subject: Re: Clever newline
Better:

(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))))


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier  
View profile  
 More options Nov 4, 3:45 am
Newsgroups: comp.emacs
From: Olivier <O...@nowhere.wd>
Date: Wed, 04 Nov 2009 09:45:39 +0100
Local: Wed, Nov 4 2009 3:45 am
Subject: Re: Clever newline
Mornings are difficult times:

(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))))


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrea Crotti  
View profile  
 More options Nov 4, 5:20 am
Newsgroups: comp.emacs
From: Andrea Crotti <kerny...@gmail.com>
Date: Wed, 4 Nov 2009 02:20:37 -0800 (PST)
Local: Wed, Nov 4 2009 5:20 am
Subject: Re: Clever newline
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!


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier  
View profile  
 More options Nov 4, 8:37 am
Newsgroups: comp.emacs
From: Olivier <O...@nowhere.wd>
Date: Wed, 04 Nov 2009 14:37:02 +0100
Local: Wed, Nov 4 2009 8:37 am
Subject: Re: Clever newline
Andrea Crotti a écrit :
[...]

> 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 --

(global-set-key [M-S-return] 'newline-force-close)

> I'm still not able to bind it

(global-set-key [M-S-return] 'newline-force-close)

At least, this works for me:

(defun hello () (interactive) (message "Hello"))
(global-set-key [M-S-return] 'hello)

HTH, Olivier


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
andrea  
View profile  
 More options Nov 21, 10:22 pm
Newsgroups: comp.emacs
From: andrea <andrea.crott...@gmail.com>
Date: Sun, 22 Nov 2009 04:22:11 +0100
Local: Sat, Nov 21 2009 10:22 pm
Subject: Re: Clever newline
Yes this works, thanks a lot.
I maybe should enable this only for certain modes (c/java and something
else).

Should I add to the keymap or is better to use hooks in this case?


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier  
View profile  
 More options Nov 22, 7:46 am
Newsgroups: comp.emacs
From: Olivier <O...@nowhere.wd>
Date: Sun, 22 Nov 2009 13:46:50 +0100
Local: Sun, Nov 22 2009 7:46 am
Subject: Re: Clever newline
andrea a écrit :
[...]

> Should I add to the keymap or is better to use hooks in this case?

Both :)

(add-hook 'foo-mode-hook
   '(lambda nil
       (define-key foo-mode-map [M-S-return] 'newline-force-close)))

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).

Better is

(add-hook 'foo-mode-hook
   '(lambda nil
       (define-key foo-mode-map [M-S-return]
                   (function newline-force-close))))

where one uses (function ...) to quote a function.

Best,
Amities,
         Olivier


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrea Crotti  
View profile  
 More options Nov 23, 5:29 pm
Newsgroups: comp.emacs
From: Andrea Crotti <kerny...@gmail.com>
Date: Mon, 23 Nov 2009 14:29:49 -0800 (PST)
Local: Mon, Nov 23 2009 5:29 pm
Subject: Re: Clever newline
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?

Thanks


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Olivier  
View profile  
 More options Nov 23, 7:12 pm
Newsgroups: comp.emacs
From: Olivier <O...@nowhere.wd>
Date: Tue, 24 Nov 2009 01:12:24 +0100
Local: Mon, Nov 23 2009 7:12 pm
Subject: Re: Clever newline
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)))))

(mapcar
  (lambda (mode-datas)
    ;; (print mode-datas)
    (add-hook (aref mode-datas 1)
              `(lambda nil
                 (define-key ,(aref mode-datas 2)
                   [M-S-return]
                (function newline-force-close)))))
  (mapcar 'cdr newline-force-close-alist))
------------------------------------------------------------

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.

HTH, best, amities, Olivier


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
andrea  
View profile  
 More options Nov 26, 8:21 am
Newsgroups: comp.emacs
From: andrea <andrea.crott...@gmail.com>
Date: Thu, 26 Nov 2009 14:21:26 +0100
Local: Thurs, Nov 26 2009 8:21 am
Subject: Re: Clever newline

Now it's really perfect Olivier, thanks a lot!


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Andrea Crotti  
View profile  
 More options Nov 30, 4:15 am
Newsgroups: comp.emacs
From: Andrea Crotti <kerny...@gmail.com>
Date: Mon, 30 Nov 2009 01:15:12 -0800 (PST)
Local: Mon, Nov 30 2009 4:15 am
Subject: Re: Clever newline
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..


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google