1

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)))))
8
  • 1
    What do you mean "read"? Does the following do what you want? (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)))
    – JeanPierre
    May 9, 2016 at 11:54
  • Yes that was what I meant. Fetch the content of the first line.
    – Rickard
    May 9, 2016 at 12:14
  • 1
    (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.
    – Rickard
    May 9, 2016 at 13:41
  • 1
    What have you tried so far? Have a look at the manual note on hooks.
    – Dan
    May 9, 2016 at 13:46
  • 2
    @Rickard Do you want to rename the buffer name (and not the file name) each time you open a buffer, based on the first line in the buffer? If so please rephrase the question so that it includes the "renaming buffer" portion too. May 9, 2016 at 15:14

1 Answer 1

-1

EDIT: Whoever edited my answer, way to go asshole. Now it's fucking meaningless. Next time just delete the reply. That way the asker won't be confused by a non-working answer.

I don't understand how or why you want to rename the buffer, or if it can be done programatically. You'll have to suppy more details about your intended applications and expected results. Therefore this code is designed to demonstrate one approach which will require tailoring to fit your needs.

This function must accept a single argument! The argument is the number of characters inserted. This function must return the number of characters remaining in the buffer after processing. We don't change the buffer contents after insertion, so the original argument will be passed back unchanged.

This function must not return a string or any value other than the number of characters remaining! The best we can do is print the line, or insert it in another buffer, or perform other side effects. I have chosen to print the line to demonstrate that this works.

When this function is called, point will initially be set to (point-min). That is why the function operates on the "current line".

(defun my-print-this-line (n)
  "Print current line to *Messages*."
  (message "%s"
       (buffer-substring-no-properties (line-beginning-position)
                       (line-end-position)))
  n)
;; => my-print-this-line
(setq after-insert-file-functions '(my-print-this-line))
:: => (my-print-this-line)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.