[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Go to line
From: |
Stephen Berman |
Subject: |
Re: Go to line |
Date: |
Tue, 24 Apr 2007 12:01:40 +0200 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.0.98 (gnu/linux) |
On Mon, 23 Apr 2007 22:00:09 +0200 Dieter Wilhelm <dieter@duenenhof-wilhelm.de>
wrote:
> PAolo <paolopantaleo@gmail.com> writes:
>
>> M-x goto-line ?
>
> emacs 22.1: M-g M-g
>
>> Can I specify a line number when I open a file with C-x C-f?
>
> You could run emacs-server and open a file on the command line at a
> specified line number LINE_NO.
>
> $ emacsclient +LINE_NO FILE_NAME
>
> otherwise you probably have to write your own find-file function.
Here are two possibilities; the first one doesn't expand wildcards,
the second one does:
(defun srb-visit-file-at-line ()
"Visit an interactively selected file at a given line number.
The line number is provided by prefix argument.
Without a prefix argument, just visit the file."
(interactive)
(let ((num current-prefix-arg)
(find-file-wildcards) ; no wildcard expansion
(last (line-number-at-pos (point-max))))
(call-interactively 'find-file)
(and num
(if (< num last)
(goto-line num)
(error "File only has %d lines" last)))))
(defun srb-visit-file-or-files-at-line ()
"Visit interactively selected file(s) at a given line number.
The line number is provided by prefix argument.
Without a prefix argument, just visit the file(s).
A wildcard in the interactively provided file name is expanded."
(interactive)
(let* ((name (car (find-file-read-args "Find file: " nil)))
(num current-prefix-arg)
(value (find-file-noselect name nil nil t))
bufs last
(err ""))
;; adapted from definition of find-file
(if (listp value)
(setq bufs (mapcar 'switch-to-buffer (nreverse value)))
(switch-to-buffer value)
(setq bufs (list value)))
(and num
(dolist (buf bufs err)
(with-current-buffer buf
(setq last (line-number-at-pos (point-max)))
(if (< num last)
(goto-line num)
(setq err
(concat (format "\n%s only has %d lines" (buffer-name buf)
last)
err)))))
(unless (zerop (length err))
(setq err (substring err 1)) ; chop off initial newline
(error err)))))
Steve Berman