[comp.emacs] Question regarding next-line

rustcat@csli.STANFORD.EDU (Vallury Prabhakar) (04/03/89)

My apologies if this is a silly question.  I'm not very thorough on e-lisp.

I was playing around with e-lisp to write a line-number indicator in the
mode-line and discovered the following documentation for the function
next-line.

-----------------------------------------------------------------------------
describe-key: C-n

next-line:
Move cursor vertically down ARG lines.
If there is no character in the target line exactly under the current column,
the cursor is positioned after the character in that line which spans this
column, or at the end of the line if it is not long enough.

*** If there is no line in the buffer after this one,
*** a newline character is inserted to create a line
*** and the cursor moves to that line.

[My ***]
-----------------------------------------------------------------------------

My questions are: 

1) Why is that the above function next-line inserts a newline character if
there is no line following it in the buffer?  I had some trouble with it 
but managed to work around the problem. Still I'm still curious to know 
why next-line is different from say, forward-char (which doesn't insert a 
space if necessary) in this respect?  I would've thought that an "End of
buffer" warning would result in trying to move past (point-max).

2) How do I go about loading a function or a file of functions each time
the current buffer is changed? That is, I would like these functions to
be executed from scratch each time a new file is read into the buffer.

GNUemacs V18.50.3 on BSD 4.2

Thank you.

						-- Vallury Prabhakar
						-- rustcat@csli.stanford.edu

jr@bbn.com (John Robinson) (04/03/89)

In article <8363@csli.STANFORD.EDU>, rustcat@csli (Vallury Prabhakar) writes:
>1) Why is that the above function next-line inserts a newline character if
>there is no line following it in the buffer?  I had some trouble with it 
>but managed to work around the problem. Still I'm still curious to know 
>why next-line is different from say, forward-char (which doesn't insert a 
>space if necessary) in this respect?  I would've thought that an "End of
>buffer" warning would result in trying to move past (point-max).

This could certainly be argued each way.  A motion command which can
modify the buffer as a side-effect seems like a bad idea.  One
advantage comes when you are inserting an emoty line at the end of the
buffer - the next-line function in effect "gobbles" unseen newlines
past the last printing text as you move downward to the point where
the new paragraph begins.

>2) How do I go about loading a function or a file of functions each time
>the current buffer is changed? That is, I would like these functions to
>be executed from scratch each time a new file is read into the buffer.

Three answers:

1.  If the functions to be loaded (do you mean run?) depend only on
the file type, as implied by the file name or extension, put something
onto auto-mode-alist.

2.  If the functions are the same all the time, make them into a new
major mode and make that your default-major-mode.  If you are simply
setting the major mode to one of the available ones, make that your
default.  I happen to set default-major-mode to 'text-mode, and then
text-mode-hook to 'turn-on-auto-fill.

3.  If it is more complicated than this, use find-file-hooks.
--
/jr
jr@bbn.com or bbn!jr
C'mon big money!

tom@hcx2.SSD.HARRIS.COM (04/06/89)

Or do what I do, go to the lisp source and hack your own version that
does exactly what you want (that's what emacs is all about isn't it?).
Here is the version I use - it will add at most one newline if there
is not already one at the end of the buffer:

; Replace the system next-line with one that inserts a max of one
; newline
(defun next-line (arg)
  "Move cursor vertically down ARG lines.
If there is no character in the target line exactly under the current column,
the cursor is positioned after the character in that line which spans this
column, or at the end of the line if it is not long enough.
If you are on the last line in a buffer and there is no newline, one
is automatically inserted.

The command \\[set-goal-column] can be used to create
a semipermanent goal column to which this command always moves.
Then it does not try to move vertically.

If you are thinking of using this in a Lisp program, consider
using `forward-line' instead.  It is usually easier to use
and more reliable (no dependence on goal column, etc.)."
  (interactive "p")
  (save-excursion
     (forward-line 1)
     (if (eq (preceding-char) ?\n)
        nil
        (insert ?\n)
     )
  )
  (line-move arg)
)

P.S. This was hacked from the version in the gnuemacs 18.52 source.

=====================================================================
    usenet: tahorsley@ssd.harris.com  USMail: Tom Horsley
compuserve: 76505,364                         511 Kingbird Circle
     genie: T.HORSLEY                         Delray Beach, FL  33444
======================== Aging: Just say no! ========================