I want to add a hook that reads the first line of the file/buffer when it opens. I want to do this so that I can rename the buffer that opens notes from simplenote.
But, how do I read the first line?
I guess I was kind of vague in my question. But exactly what I wanted to do was to read the first line when opening notes via simplenote.el since the buffer gets named to a hash of 76 char that does not tell anything about the contents.
This kind of failed since opening a simplenote file it defaults to text mode so no hook possible there. But it could be useful for org-mode notes in simplenote.
Using something like this:
(defun read-first-line ()
(save-excursion
(goto-char (point-min))
(setq b (point))
(setq e (- (search-forward "-*-" (line-end-position)) 4))
(buffer-substring-no-properties b e)))
;(add-hook 'org-mode-hook
;(lambda () (rename-buffer (read-first-line))))
(add-hook 'org-mode-hook
(lambda () ((let (str (read-first-line))
(if (not (string= "" str))
(rename-buffer str))))))
I cannot get the conditional lambda to work. And what is the difference with let and setq inside a function?
Edit: Fixed the org-mode-hook:
(add-hook 'org-mode-hook
(lambda ()
(let ((str (read-first-line)))
(if (not (string= "" str))
(rename-buffer str)))))
(defun read-first-line () (save-excursion (goto-char (point-min)) (let ((b (point)) (e (progn (end-of-line) (point)))) (buffer-substring-no-properties b e))))
Moved the parenthesis of save-excursion if some one else tries this code.