[gnu.emacs] Line and Column numbers

daloera@athena.mit.edu (Daniel A. Loera) (02/09/90)

I haven't had too much success with this question on comp.emacs so here
it is on gnu.emacs.

I have seen many things appear on people's mode-line: time, mode, machine
name, and other things.  I would like to know if someone out there could post
some code that would show the current line and column number that the cursor
is on.

Is what I'm asking for feasible?  Is it possible?

Please post to the net if it hasn't been discussed before.  If it has, then
just email to me.

Thanks.

daloera@athena.mit.edu (Daniel A. Loera) (02/28/90)

I have received the following code from tac@CS.BROWN.EDU.  It is supposed to
put the col and row numbers on the mode line.  What follows is exactly what I
received from him.  You may have to hack around with the code to get it to
work for you.


-> Start of tac@CS.BROWN.EDU's e-mail:


these will do it, but will only be correct for arrow keys, not
for forward-sexp for ex.


(defun teds-forward-char (&rest dummy)
  (interactive)
  (forward-char 1)
  (update-modeline))


(defun teds-backward-char (&rest dummy)
  (interactive)
  (backward-char 1)
  (update-modeline))



(defun teds-previous-line (&rest dummy)
  "Previous-line, maintains column, updates cursor pos"
  (interactive)
  (let ((old-row  0))
     (if (not (or (eq last-command 'teds-previous-line)
                  (eq last-command 'teds-next-line)))
         (setq *column* (current-column)))
     (setq old-row (current-row))
     (previous-line 1)
     (move-to-column *column*)
     (if (and (/= old-row 0) 
              (not (eql (current-window) *keypad-window*)))
         (update-modeline))))


(defun teds-next-line (&rest dummy)
  "Next-line, maintains column, updates cursor pos"
  (interactive)
  (let ((old-row   0))
     (if (not (or (eq last-command 'teds-previous-line)
                  (eq last-command 'teds-next-line)))
          (setq *column* (current-column)))
     (setq old-row (current-row))
     (next-line 1)
     (move-to-column *column*)
     (if (/= old-row (- (window-height) 2))
         (update-modeline))))
  


(defun current-column ()
  "Returns current column #, from 0."
  (let ((pos (dot)))
    (beginning-of-line)
    (prog1 (- pos (dot)) (goto-char pos))))

(defun current-row ()
  "Returns current row #, from screen top, from 0."
  (let* ((pos (point))
         (top (window-start))
         (count 0))
    (beginning-of-line)
    (while (/= (point) top)
        (forward-line -1)
        (setq count (1+ count)))
    (goto-char pos)
    count))

(defun update-modeline ()
  "Update modeline, including cursor position."
  (if (not (eql (current-window) *keypad-window*))
      (setq mode-line-format 
        (cons (concat (format "row %2s " (current-row)) 
                      (format "col %2s " (current-column))) 
              (cdr mode-line-format)))))

;;  (global-unset-key "\M-[")
;;  (global-set-key "\M-[A" 'previous-line)		; r8
;;  (global-set-key "\M-[B" 'next-line)    		; r14
;;  (global-set-key "\M-[C" 'forward-char)		; r12
;;  (global-set-key "\M-[D" 'backward-char)		; r10

- Ted

p.s. anything undefined is probably my own hacks and
     can be ignored.