dhilst

elisp - get lines of text to a list

Here is an example of how get lines of text on a list.. I think I will use this on future for process text... Also I'm exercising my elisp skills since I want to be able to process text programmatically.

;; This text will be obtained
;; by the function get-lines
;; It takes two parameters
;; The first being the start line (inclusive)
;; from with the text will be gathered
;; The second being the end line (exclusive)
;; Nice and easy!! :-)

(defun get-beginning-of-line ()
"Get the point at the beginning of line"
(save-excursion
(beginning-of-line)
(point)))

(defun get-end-of-line ()
"Get the point at the end of line"
(save-excursion
(end-of-line)
(point)))

(defun programmatic-goto-line (line)
"As goto-line but better for programming stuff"
(goto-char (point-min))
(forward-line (- line 1)))


(defun get-lines (start-line end-line)
"Return a list with the lines between START-LINE (inclusive) and END-LINE (exclusive)"
(save-excursion
(programmatic-goto-line end-line)
(let (lines)
(while (< start-line (line-number-at-pos))
(forward-line -1)
(setq lines (cons (buffer-substring-no-properties (get-beginning-of-line) (get-end-of-line)) lines)))
lines)))


;; Example
(let (v)
(dolist (v (get-lines 1 7))
(princ (format "%s\n" v))))

;; This text will be obtained
;; by the function get-lines
;; It takes two parameters
;; The first being the start line (inclusive)
;; from with the text will be gathered
;; The second being the end line (exclusive)
nil