Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to configure a title extractor per file extension #105

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

andresm
Copy link

@andresm andresm commented Sep 11, 2021

Before this update, Deft used the first line of a file and the
function bound to deft-parse-title-function to obtain the file title
to be displayed.

This update renames deft-parse-title-function to
deft-parse-title-functions. The variable is not bound anymore to a
function used to parse the first line of the files. It is now an
alist that defines the parsers used per file extension. The previous
default parser (deft-strip-title) is maintained as the default
parser. It is associated with :default in the alist.

An example of an .emacs configuration to use Deft and Org-Roam to
maintain titles different from filenames includes:

(defun my-deft-org-title (contents)
"Look for the title in the first 500 characters of an org file.
This function looks for the TITLE property in the first 500
characters of CONTENTS."
(let ((prelude (substring contents 0 (min (length contents) 500))))
(when prelude
(let ((title (substring prelude (string-match "^#\+TITLE:.+$" prelude) (match-end 0))))
(deft-chomp (replace-regexp-in-string "^#\+TITLE:" ""
title))))))

(setq deft-parse-title-functions (push '("org" . my-deft-org-title) deft-parse-title-functions))

Before this update, Deft used the first line of a file and the
function bound to deft-parse-title-function to obtain the file title
to be displayed.

This update renames deft-parse-title-function to
deft-parse-title-functions.  The variable is not bound anymore to a
function used to parse the first line of the files.  It is now an
alist that defines the parsers used per file extension.  The previous
default parser (deft-strip-title) is maintained as the default
parser.  It is associated with :default in the alist.

An example of an .emacs configuration to use Deft and Org-Roam to
maintain titles different from filenames includes:

(defun my-deft-org-title (contents)
  "Look for the title in the first 500 characters of an org file.
This function looks for the TITLE property in the first 500
characters of CONTENTS."
  (let ((prelude (substring contents 0 (min (length contents) 500))))
    (when prelude
      (let ((title (substring prelude (string-match "^#\\+TITLE:.+$" prelude) (match-end 0))))
        (deft-chomp (replace-regexp-in-string "^#\\+TITLE:" ""
	title))))))

(setq deft-parse-title-functions (push '("org" . my-deft-org-title) deft-parse-title-functions))
@Accacio
Copy link

Accacio commented Oct 11, 2021

This is great, hope it gets merged

@andresm andresm changed the title Added support to configure a parser per file extension Added support to configure a title extractor per file extension Oct 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants