Switch branches/tags
Nothing to show
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
1138 lines (959 sloc) 41 KB
;;; deft.el --- quickly browse, filter, and edit plain text notes
;;; Copyright (C) 2011-2013 Jason R. Blevins <>
;; All rights reserved.
;; Redistribution and use in source and binary forms, with or without
;; modification, are permitted provided that the following conditions are met:
;; 1. Redistributions of source code must retain the above copyright
;; notice, this list of conditions and the following disclaimer.
;; 2. Redistributions in binary form must reproduce the above copyright
;; notice, this list of conditions and the following disclaimer in the
;; documentation and/or other materials provided with the distribution.
;; 3. Neither the names of the copyright holders nor the names of any
;; contributors may be used to endorse or promote products derived from
;; this software without specific prior written permission.
;;; Version: 0.5.1
;;; Author: Jason R. Blevins <>
;;; Keywords: plain text, notes, Simplenote, Notational Velocity
;;; URL:
;; This file is not part of GNU Emacs.
;;; Commentary:
;; Deft is an Emacs mode for quickly browsing, filtering, and editing
;; directories of plain text notes, inspired by Notational Velocity.
;; It was designed for increased productivity when writing and taking
;; notes by making it fast and simple to find the right file at the
;; right time and by automating many of the usual tasks such as
;; creating new files and saving files.
;; Deft is open source software and may be freely distributed and
;; modified under the BSD license. Version 0.5.1 is the latest stable
;; version, released on January 28, 2013. You may download it
;; directly here:
;; * [deft.el](
;; To follow or contribute to Deft development, you can either
;; [browse]( or clone the Git
;; repository:
;; git clone git://
;; ![File Browser](
;; The Deft buffer is simply a file browser which lists the titles of
;; all text files in the Deft directory followed by short summaries
;; and last modified times. The title is taken to be the first line
;; of the file and the summary is extracted from the text that
;; follows. Files are sorted in terms of the last modified date, from
;; newest to oldest.
;; All Deft files or notes are simple plain text files where the first
;; line contains a title. As an example, the following directory
;; structure generated the screenshot above.
;; % ls ~/.deft
;; about.txt browser.txt directory.txt operations.txt
;; ack.txt completion.txt extensions.txt text-mode.txt
;; binding.txt creation.txt filtering.txt
;; % cat ~/.deft/about.txt
;; About
;; An Emacs mode for slicing and dicing plain text files.
;; ![Filtering](
;; Deft's primary operation is searching and filtering. The list of
;; files can be limited or filtered using a search string, which will
;; match both the title and the body text. To initiate a filter,
;; simply start typing. Filtering happens on the fly. As you type,
;; the file browser is updated to include only files that match the
;; current string.
;; To open the first matching file, simply press `RET`. If no files
;; match your search string, pressing `RET` will create a new file
;; using the string as the title. This is a very fast way to start
;; writing new notes. The filename will be generated automatically.
;; If you prefer to provide a specific filename, use `C-RET` instead.
;; To open files other than the first match, navigate up and down
;; using `C-p` and `C-n` and press `RET` on the file you want to open.
;; When opening a file, Deft searches forward and leaves the point
;; at the end of the first match of the filter string.
;; You can also press `C-o` to open a file in another window, without
;; switching to the other window. Issue the same command with a prefix
;; argument, `C-u C-o`, to open the file in another window and switch
;; to that window.
;; To edit the filter string, press `DEL` (backspace) to remove the
;; last character or `M-DEL` to remove the last "word". To yank
;; (paste) the most recently killed (cut or copied) text into the
;; filter string, press `C-y`. Press `C-c C-c` to clear the filter
;; string and display all files and `C-c C-g` to refresh the file
;; browser using the current filter string.
;; For more advanced editing operations, you can also edit the filter
;; string in the minibuffer by pressing `C-c C-l`. While in the
;; minibuffer, the history of previous edits can be cycled through by
;; pressing `M-p` and `M-n`. This form of static, one-time filtering
;; (as opposed to incremental, on-the-fly filtering) may be preferable
;; in some situations, such as over slow connections or on systems
;; where interactive filtering performance is poor.
;; By default, Deft filters files in incremental string search mode,
;; where "search string" will match all files containing both "search"
;; and "string" in any order. Alternatively, Deft supports direct
;; regexp filtering, where the filter string is interpreted as a
;; formal regular expression. For example, `^\(foo\|bar\)` matches
;; foo or bar at the beginning of a line. Pressing `C-c C-t` will
;; toggle between incremental and regexp search modes. Regexp
;; search mode is indicated by an "R" in the mode line.
;; Common file operations can also be carried out from within Deft.
;; Files can be renamed using `C-c C-r` or deleted using `C-c C-d`.
;; New files can also be created using `C-c C-n` for quick creation or
;; `C-c C-m` for a filename prompt. You can leave Deft at any time
;; with `C-c C-q`.
;; Files opened with deft are automatically saved after Emacs has been
;; idle for a customizable number of seconds. This value is a floating
;; point number given by `deft-auto-save-interval' (default: 1.0).
;; Getting Started
;; ---------------
;; To start using it, place it somewhere in your Emacs load-path and
;; add the line
;; (require 'deft)
;; in your `.emacs` file. Then run `M-x deft` to start. It is useful
;; to create a global keybinding for the `deft` function (e.g., a
;; function key) to start it quickly (see below for details).
;; When you first run Deft, it will complain that it cannot find the
;; `~/.deft` directory. You can either create a symbolic link to
;; another directory where you keep your notes or run `M-x deft-setup`
;; to create the `~/.deft` directory automatically.
;; One useful way to use Deft is to keep a directory of notes in a
;; Dropbox folder. This can be used with other applications and
;; mobile devices, for example, Notational Velocity or Simplenote
;; on OS X, Elements on iOS, or Epistle on Android.
;; Customization
;; -------------
;; Customize the `deft` group to change the functionality.
;; By default, Deft looks for notes by searching for files with the
;; extension `.txt` in the `~/.deft` directory. You can customize
;; both the file extension and the Deft directory by running
;; `M-x customize-group` and typing `deft`. Alternatively, you can
;; configure them in your `.emacs` file:
;; (setq deft-extension "txt")
;; (setq deft-directory "~/Dropbox/notes")
;; You can also customize the major mode that Deft uses to edit files,
;; either through `M-x customize-group` or by adding something like
;; the following to your `.emacs` file:
;; (setq deft-text-mode 'markdown-mode)
;; Note that the mode need not be a traditional text mode. If you
;; prefer to write notes as LaTeX fragments, for example, you could
;; set `deft-extension' to "tex" and `deft-text-mode' to `latex-mode'.
;; If you prefer `org-mode', then simply use
;; (setq deft-extension "org")
;; (setq deft-text-mode 'org-mode)
;; For compatibility with other applications which take the title from
;; the filename, rather than from first line of the file, set the
;; `deft-use-filename-as-title' flag to a non-nil value. This also
;; changes the default behavior for creating new files when the filter
;; is non-empty: the filter string will be used as the new filename
;; rather than inserted into the new file. To enable this
;; functionality, simply add the following to your `.emacs` file:
;; (setq deft-use-filename-as-title t)
;; You can easily set up a global keyboard binding for Deft. For
;; example, to bind it to F8, add the following code to your `.emacs`
;; file:
;; (global-set-key [f8] 'deft)
;; The faces used for highlighting various parts of the screen can
;; also be customized. By default, these faces inherit their
;; properties from the standard font-lock faces defined by your current
;; color theme.
;; Incremental string search is the default method of filtering on
;; startup, but you can set `deft-incremental-search' to nil to make
;; regexp search the default.
;; The title of each file is taken to be the first line of the file,
;; with certain characters removed from the beginning (hash
;; characters, as used in Markdown headers, and asterisks, as in Org
;; Mode headers). The substrings to remove are specified in
;; `deft-strip-title-regexp'.
;; More generally, the title post-processing function itself can be
;; customized by setting `deft-parse-title-function', which accepts
;; the first line of the file as an argument and returns the parsed
;; title to display in the file browser. The default function is
;; `deft-strip-title', which removes all occurrences of
;; `deft-strip-title-regexp' as described above.
;; Acknowledgments
;; ---------------
;; Thanks to Konstantinos Efstathiou for writing simplnote.el, from
;; which I borrowed liberally, and to Zachary Schneirov for writing
;; Notational Velocity, whose functionality and spirit I wanted to
;; bring to Emacs.
;; History
;; -------
;; Version 0.5.1 (2013-01-28):
;; * Bug fix: creating files with C-c C-n when both the filter string and
;; `deft-use-filename-as-title' are non-nil resulted in an invalid path.
;; * Bug fix: killed buffers would persist in `deft-auto-save-buffers'.
;; Version 0.5 (2013-01-25):
;; * Implement incremental string search (default) and regex search.
;; These search modes can be toggled by pressing `C-c C-t`.
;; * Default search method can be changed by setting `deft-incremental-search'.
;; * Support custom `deft-parse-title-function' for post-processing titles.
;; * The default `deft-parse-title-function' simply strips occurrences of
;; `deft-strip-title-regexp', which removes Markdown and Org headings.
;; * Open files in another window with `C-o`. Prefix it with `C-u` to
;; switch to the other window.
;; * For symbolic links, use modification time of taget for sorting.
;; * When opening files, move point to the end of the first match of
;; the filter string.
;; * Improved filter editing: delete (`DEL`), delete word (`M-DEL`),
;; and yank (`C-y`).
;; * Advanced filter editing in minibuffer (`C-c C-l`).
;; Version 0.4 (2011-12-11):
;; * Improved filtering performance.
;; * Optionally take title from filename instead of first line of the
;; contents (see `deft-use-filename-as-title').
;; * Dynamically resize width to fit the entire window.
;; * Customizable time format (see `deft-time-format').
;; * Handle `deft-directory' properly with or without a trailing slash.
;; Version 0.3 (2011-09-11):
;; * Internationalization: support filtering with multibyte characters.
;; Version 0.2 (2011-08-22):
;; * Match filenames when filtering.
;; * Automatically save opened files (optional).
;; * Address some byte-compilation warnings.
;; Deft was originally written by [Jason Blevins](
;; The initial version, 0.1, was released on August 6, 2011.
;;; Code:
(require 'widget)
;; Customization
(defgroup deft nil
"Emacs Deft mode."
:group 'local)
(defcustom deft-directory (expand-file-name "~/.deft/")
"Deft directory."
:type 'directory
:safe 'stringp
:group 'deft)
(defcustom deft-extension "txt"
"Deft file extension."
:type 'string
:safe 'stringp
:group 'deft)
(defcustom deft-text-mode 'text-mode
"Default mode used for editing files."
:type 'function
:group 'deft)
(defcustom deft-auto-save-interval 1.0
"Idle time in seconds before automatically saving buffers opened by Deft.
Set to zero to disable."
:type 'float
:group 'deft)
(defcustom deft-time-format " %Y-%m-%d %H:%M"
"Format string for modification times in the Deft browser.
Set to nil to hide."
:type '(choice (string :tag "Time format")
(const :tag "Hide" nil))
:group 'deft)
(defcustom deft-use-filename-as-title nil
"Use filename as title, instead of the first line of the contents."
:type 'boolean
:group 'deft)
(defcustom deft-incremental-search t
"Use incremental string search when non-nil and regexp search when nil.
During incremental string search, substrings separated by spaces are
treated as subfilters, each of which must match a file. They need
not be adjacent and may appear in any order. During regexp search, the
entire filter string is interpreted as a single regular expression."
:type 'boolean
:group 'deft)
(defcustom deft-parse-title-function 'deft-strip-title
"Function for post-processing file titles."
:type 'function
:group 'deft)
(defcustom deft-strip-title-regexp "^[#\* ]*"
"Regular expression to remove from file titles."
:type 'regexp
:safe 'stringp
:group 'deft)
;; Faces
(defgroup deft-faces nil
"Faces used in Deft mode"
:group 'deft
:group 'faces)
(defface deft-header-face
'((t :inherit font-lock-keyword-face :bold t))
"Face for Deft header."
:group 'deft-faces)
(defface deft-filter-string-face
'((t :inherit font-lock-string-face))
"Face for Deft filter string."
:group 'deft-faces)
(defface deft-filter-string-error-face
'((t :inherit font-lock-warning-face))
"Face for Deft filter string when regexp is invalid."
:group 'deft-faces)
(defface deft-title-face
'((t :inherit font-lock-function-name-face :bold t))
"Face for Deft file titles."
:group 'deft-faces)
(defface deft-separator-face
'((t :inherit font-lock-comment-delimiter-face))
"Face for Deft separator string."
:group 'deft-faces)
(defface deft-summary-face
'((t :inherit font-lock-comment-face))
"Face for Deft file summary strings."
:group 'deft-faces)
(defface deft-time-face
'((t :inherit font-lock-variable-name-face))
"Face for Deft last modified times."
:group 'deft-faces)
;; Constants
(defconst deft-version "0.5.1")
(defconst deft-buffer "*Deft*"
"Deft buffer name.")
(defconst deft-separator " --- "
"Text used to separate file titles and summaries.")
(defconst deft-empty-file-title "[Empty file]"
"Text to use as title for empty files.")
;; Global variables
(defvar deft-mode-hook nil
"Hook run when entering Deft mode.")
(defvar deft-filter-regexp nil
"A list of string representing the current filter used by Deft.
In incremental search mode, when `deft-incremental-search' is
non-nil, the elements of this list are the individual words of
the filter string, in reverse order. That is, the car of the
list is the last word in the filter string.
In regexp search mode, when `deft-incremental-search' is nil,
this list has a single element containing the entire filter
(defvar deft-current-files nil
"List of files matching current filter.")
(defvar deft-all-files nil
"List of files matching current filter.")
(defvar deft-hash-contents nil
"Hash containing complete cached file contents, keyed by filename.")
(defvar deft-hash-mtimes nil
"Hash containing cached file modification times, keyed by filename.")
(defvar deft-hash-titles nil
"Hash containing cached file titles, keyed by filename.")
(defvar deft-hash-summaries nil
"Hash containing cached file summaries, keyed by filename.")
(defvar deft-auto-save-buffers nil
"List of buffers that will be automatically saved.")
(defvar deft-window-width nil
"Width of Deft buffer.")
(defvar deft-filter-history nil
"History of interactive filter strings.")
(defvar deft-regexp-error nil
"Flag for indicating invalid regexp errors.")
;; Helpers
(defun deft-whole-filter-regexp ()
"Join incremental filters into one."
(mapconcat 'identity (reverse deft-filter-regexp) " "))
(defun deft-search-forward (str)
"Function to use when matching files against filter strings.
This function calls `search-forward' when `deft-incremental-search'
is non-nil and `re-search-forward' otherwise."
(if deft-incremental-search
(search-forward str nil t)
(re-search-forward str nil t)))
(defun deft-set-mode-name ()
(if deft-incremental-search
(setq mode-name "Deft")
(setq mode-name "Deft/R")))
(defun deft-toggle-incremental-search ()
"Toggle the `deft-incremental-search' setting."
(setq deft-incremental-search nil)
(message "Regexp search"))
(setq deft-incremental-search t)
(message "Incremental string search")))
(deft-filter (deft-whole-filter-regexp) t)
(defun deft-filter-regexp-as-regexp ()
"Return a regular expression corresponding to the current filter string.
When `deft-incremental-search' is non-nil, we must combine each individual
whitespace separated string. Otherwise, the `car' of `deft-filter-regexp'
is the complete regexp."
(if deft-incremental-search
(mapconcat 'regexp-quote (reverse deft-filter-regexp) "\\|")
(car deft-filter-regexp)))
;; File processing
(defun deft-chomp (str)
"Trim leading and trailing whitespace from STR."
(let ((s str))
(replace-regexp-in-string "\\(^[[:space:]\n]*\\|[[:space:]\n]*$\\)" "" s)))
(defun deft-base-filename (file)
"Strip the path and extension from filename FILE."
(setq file (file-name-nondirectory file))
(if (> (length deft-extension) 0)
(setq file (replace-regexp-in-string (concat "\." deft-extension "$") "" file)))
(defun deft-find-all-files ()
"Return a list of all files in the Deft directory."
(if (file-exists-p deft-directory)
(let (files result)
;; List all files
(setq files
(directory-files deft-directory t
(concat "\." deft-extension "$") t))
;; Filter out files that are not readable or are directories
(dolist (file files)
(when (and (file-readable-p file)
(not (file-directory-p file)))
(setq result (cons file result))))
(defun deft-strip-title (title)
"Remove all strings matching `deft-strip-title-regexp' from TITLE."
(replace-regexp-in-string deft-strip-title-regexp "" title))
(defun deft-parse-title (file contents)
"Parse the given FILE and CONTENTS and determine the title.
According to `deft-use-filename-as-title', the title is taken to
be the first non-empty line of a file or the file name."
(if deft-use-filename-as-title
(deft-base-filename file)
(let ((begin (string-match "^.+$" contents)))
(if begin
(funcall deft-parse-title-function
(substring contents begin (match-end 0)))))))
(defun deft-parse-summary (contents title)
"Parse the file CONTENTS, given the TITLE, and extract a summary.
The summary is a string extracted from the contents following the
(let ((summary (replace-regexp-in-string "[\n\t]" " " contents)))
(if (and (not deft-use-filename-as-title) title)
(if (string-match (regexp-quote title) summary)
(deft-chomp (substring summary (match-end 0) nil))
(defun deft-cache-file (file)
"Update file cache if FILE exists."
(when (file-exists-p file)
(add-to-list 'deft-all-files file)
(let ((mtime-cache (deft-file-mtime file))
(mtime-file (nth 5 (file-attributes (file-truename file)))))
(if (or (not mtime-cache)
(time-less-p mtime-cache mtime-file))
(deft-cache-newer-file file mtime-file)))))
(defun deft-cache-newer-file (file mtime)
"Update cached information for FILE with given MTIME."
;; Modification time
(puthash file mtime deft-hash-mtimes)
(let (contents title)
;; Contents
(with-current-buffer (get-buffer-create "*Deft temp*")
(insert-file-contents file nil nil nil t)
(setq contents (concat (buffer-string))))
(puthash file contents deft-hash-contents)
;; Title
(setq title (deft-parse-title file contents))
(puthash file title deft-hash-titles)
;; Summary
(puthash file (deft-parse-summary contents title) deft-hash-summaries))
(kill-buffer "*Deft temp*"))
(defun deft-file-newer-p (file1 file2)
"Return non-nil if FILE1 was modified since FILE2 and nil otherwise."
(let (time1 time2)
(setq time1 (deft-file-mtime file1))
(setq time2 (deft-file-mtime file2))
(time-less-p time2 time1)))
(defun deft-cache-initialize ()
"Initialize hash tables for caching files."
(setq deft-hash-contents (make-hash-table :test 'equal))
(setq deft-hash-mtimes (make-hash-table :test 'equal))
(setq deft-hash-titles (make-hash-table :test 'equal))
(setq deft-hash-summaries (make-hash-table :test 'equal)))
(defun deft-cache-update-all ()
"Update file list and update cached information for each file."
(setq deft-all-files (deft-find-all-files)) ; List all files
(mapc 'deft-cache-file deft-all-files) ; Cache contents
(setq deft-all-files (deft-sort-files deft-all-files))) ; Sort by mtime
(defun deft-cache-update-file (file)
"Update cached information for a single file."
(deft-cache-file file) ; Cache contents
(setq deft-all-files (deft-sort-files deft-all-files))) ; Sort by mtime
;; Cache access
(defun deft-file-contents (file)
"Retrieve complete contents of FILE from cache."
(gethash file deft-hash-contents))
(defun deft-file-mtime (file)
"Retrieve modified time of FILE from cache."
(gethash file deft-hash-mtimes))
(defun deft-file-title (file)
"Retrieve title of FILE from cache."
(gethash file deft-hash-titles))
(defun deft-file-summary (file)
"Retrieve summary of FILE from cache."
(gethash file deft-hash-summaries))
;; File list display
(defun deft-print-header ()
"Prints the *Deft* buffer header."
(if deft-filter-regexp
(propertize "Deft: " 'face 'deft-header-face))
(propertize (deft-whole-filter-regexp) 'face
(if (and (not deft-incremental-search) deft-regexp-error)
(propertize "Deft" 'face 'deft-header-face)))
(widget-insert "\n\n"))
(defun deft-buffer-setup ()
"Render the file browser in the *Deft* buffer."
(setq deft-window-width (window-width))
(let ((inhibit-read-only t))
;; Print the files list
(if (not (file-exists-p deft-directory))
(widget-insert (deft-no-directory-message))
(if deft-current-files
(mapc 'deft-file-widget deft-current-files))
(widget-insert (deft-no-files-message))))
(use-local-map deft-mode-map)
(goto-char 1)
(forward-line 2))
(defun deft-file-widget (file)
"Add a line to the file browser for the given FILE."
(when file
(let* ((key (file-name-nondirectory file))
(text (deft-file-contents file))
(title (deft-file-title file))
(summary (deft-file-summary file))
(mtime (when deft-time-format
(format-time-string deft-time-format (deft-file-mtime file))))
(mtime-width (length mtime))
(line-width (- deft-window-width mtime-width))
(title-width (min line-width (length title)))
(summary-width (min (length summary)
(- line-width
(length deft-separator)))))
(widget-create 'link
:button-prefix ""
:button-suffix ""
:button-face 'deft-title-face
:format "%[%v%]"
:tag file
:help-echo "Edit this file"
:notify (lambda (widget &rest ignore)
(deft-open-file (widget-get widget :tag)))
(if title (substring title 0 title-width)
(when (> summary-width 0)
(widget-insert (propertize deft-separator 'face 'deft-separator-face))
(widget-insert (propertize (substring summary 0 summary-width)
'face 'deft-summary-face)))
(when mtime
(while (< (current-column) line-width)
(widget-insert " "))
(widget-insert (propertize mtime 'face 'deft-time-face)))
(widget-insert "\n"))))
(add-hook 'window-configuration-change-hook
(lambda ()
(when (and (eq (current-buffer) (get-buffer deft-buffer))
(not (eq deft-window-width (window-width))))
(defun deft-refresh ()
"Update the file cache, reapply the filter, and refresh the *Deft* buffer."
(defun deft-refresh-filter ()
"Reapply the filter and refresh the *Deft* buffer.
Call this after any actions which update the cache."
(defun deft-refresh-browser ()
"Refresh the *Deft* buffer in the background.
Call this function after any actions which update the filter and file list."
(when (get-buffer deft-buffer)
(with-current-buffer deft-buffer
(defun deft-no-directory-message ()
"Return a short message to display when the Deft directory does not exist."
(concat "Directory " deft-directory " does not exist.\n"))
(defun deft-no-files-message ()
"Return a short message to display if no files are found."
(if deft-filter-regexp
"No files match the current filter string.\n"
"No files found."))
;; File list file management actions
(defun deft-unused-filename ()
(let (fmt counter slug dir file)
(setq fmt (concat "deft-%d"))
(setq dir (file-name-as-directory deft-directory))
(setq counter 0)
(setq slug (format fmt counter))
(setq file (concat dir slug "." deft-extension))
(while (or (file-exists-p file) (get-file-buffer file))
(setq counter (1+ counter))
(setq slug (format fmt counter))
(setq file (concat dir slug "." deft-extension)))
(defun deft-open-file (file &optional other switch)
"Open FILE in a new buffer and setting its mode.
When OTHER is non-nil, open the file in another window. When
OTHER and SWITCH are both non-nil, switch to the other window."
(let ((buffer (find-file-noselect file)))
(with-current-buffer buffer
(when (not (eq major-mode deft-text-mode))
(funcall deft-text-mode))
(when deft-filter-regexp
(re-search-forward (deft-filter-regexp-as-regexp) nil t))
(add-to-list 'deft-auto-save-buffers buffer)
(add-hook 'after-save-hook
(lambda () (save-excursion
(deft-cache-update-file buffer-file-name)
nil t))
(if other
(if switch
(switch-to-buffer-other-window buffer)
(display-buffer buffer other))
(switch-to-buffer buffer))))
(defun deft-find-file (file)
"Find FILE interactively using the minibuffer."
(interactive "F")
(deft-open-file file))
(defun deft-new-file-named (file)
"Create a new file named FILE (or interactively prompt for a filename).
FILE must be a short name, without a path or a file extension.
If the filter string is non-nil and title is not from file name,
use it as the title."
(interactive "sNew filename (without extension): ")
(setq file (concat (file-name-as-directory deft-directory)
file "." deft-extension))
(if (file-exists-p file)
(message (concat "Aborting, file already exists: " file))
;; Insert the contents of the filter string in the file.
(when (and deft-filter-regexp (not deft-use-filename-as-title))
(write-region (concat (deft-whole-filter-regexp) "\n\n") nil file nil))
(deft-cache-update-file file)
(deft-open-file file)
(with-current-buffer (get-file-buffer file)
(goto-char (point-max)))))
(defun deft-new-file ()
"Create a new file quickly.
Use either an automatically generated filename or the filter
string if non-nil and `deft-use-filename-as-title' is set. If the
filter string is non-nil and title is not from filename, use it
as the title."
(let (file)
(if (and deft-filter-regexp deft-use-filename-as-title)
;; If the filter string is non-emtpy and titles are taken from
;; filenames is set, construct filename from filter string.
(setq file (deft-whole-filter-regexp))
;; If the filter string is empty, or titles are taken from file
;; contents, then use an automatically generated unique filename.
(setq file (deft-unused-filename)))
(deft-new-file-named file)))
(defun deft-open-file-other-window (&optional arg)
"When the point is at a widget, open the file in the other window."
(interactive "P")
(let ((file (widget-get (widget-at) :tag)))
(when file
(deft-open-file file t arg))))
(defun deft-delete-file ()
"Delete the file represented by the widget at the point.
If the point is not on a file widget, do nothing. Prompts before
(let ((filename (widget-get (widget-at) :tag)))
(when filename
(when (y-or-n-p
(concat "Delete file " (file-name-nondirectory filename) "? "))
(delete-file filename)
(delq filename deft-current-files)
(delq filename deft-all-files)
(defun deft-rename-file ()
"Rename the file represented by the widget at the point.
If the point is not on a file widget, do nothing."
(let (old-filename new-filename old-name new-name)
(setq old-filename (widget-get (widget-at) :tag))
(when old-filename
(setq old-name (deft-base-filename old-filename))
(setq new-name (read-string
(concat "Rename " old-name " to (without extension): ")))
(setq new-filename
(concat (file-name-as-directory deft-directory)
new-name "." deft-extension))
(rename-file old-filename new-filename)
;; File list filtering
(defun deft-sort-files (files)
"Sort FILES in reverse order by modified time."
(sort files (lambda (f1 f2) (deft-file-newer-p f1 f2))))
(defun deft-filter-initialize ()
"Initialize the filter string (nil) and files list (all files)."
(setq deft-filter-regexp nil)
(setq deft-current-files deft-all-files))
(defun deft-filter-match-file (file &optional batch)
"Return FILE if FILE matches the current filter regexp."
(insert file)
(let ((title (deft-file-title file))
(contents (deft-file-contents file)))
(when title (insert title))
(when contents (insert contents)))
(if batch
(if (every (lambda (filter)
(goto-char (point-min))
(deft-search-forward filter))
(goto-char (point-min))
(if (deft-search-forward (car deft-filter-regexp))
(defun deft-filter-files (files)
"Update `deft-current-files' given a list of paths, FILES.
Apply `deft-filter-match-file' to `deft-all-files', handling
any errors that occur."
(delq nil
(condition-case nil
;; Map `deft-filter-match-file' onto FILES. Return
;; filtered files list and clear error flag if no error.
(setq deft-regexp-error nil)
(mapcar (lambda (file) (deft-filter-match-file file t)) files))
;; Upon an error (`invalid-regexp'), set an error flag
(setq deft-regexp-error t)
(defun deft-filter-update ()
"Update the filtered files list using the current filter regexp.
Starts from scratch using `deft-all-files'. Does not refresh the
Deft buffer."
(if (not deft-filter-regexp)
(setq deft-current-files deft-all-files)
(setq deft-current-files
(deft-filter-files deft-all-files))))
;; Filters that cause a refresh
(defun deft-filter-clear ()
"Clear the current filter string and refresh the file browser."
(when deft-filter-regexp
(setq deft-filter-regexp nil)
(setq deft-current-files deft-all-files)
(message "Filter cleared."))
(defun deft-filter (str &optional reset)
"Update the filter with STR and update the file browser.
In incremental search mode, the car of `deft-filter-regexp' will
be replaced with STR. If STR has zero length and the length of
the list is greater than one, the empty string will be retained
to simulate whitespace. However, if STR has zero length and the
list is of length one, then the filter will be cleared. If STR
is nil, then the car is removed from the list.
In regexp search mode, the current filter string will be replaced
with STR.
When called interactively, or when RESET is non-nil, always
replace the entire filter string."
(list (read-from-minibuffer "Filter: " (deft-whole-filter-regexp)
nil nil 'deft-filter-history)))
(if deft-incremental-search
;; Incremental search mode
(if (or (called-interactively-p 'any) reset)
;; Called interactively or RESET non-nil
(if (= (length str) 0)
(setq deft-filter-regexp nil)
(setq deft-filter-regexp (reverse (split-string str " "))))
;; Called noninteractively
(if (not str)
;; If str is nil, remove it and filter with the cdr
(setq deft-filter-regexp (cdr deft-filter-regexp))
;; Use STR it as the new car, even when empty (to simulate
;; whitespace), unless this is the only element in the list.
(if (and (= (length deft-filter-regexp) 1)
(= (length str) 0))
(setq deft-filter-regexp nil)
(setcar deft-filter-regexp str))))
;; Regexp search mode
(if (> (length str) 0)
(setq deft-filter-regexp (list str))
(setq deft-filter-regexp nil)))
(defun deft-filter-increment ()
"Append character to the filter regexp and update `deft-current-files'."
(let ((char last-command-event))
(if (= char ?\S-\ )
(setq char ?\s))
(setq char (char-to-string char))
(if (and deft-incremental-search (string= char " "))
(setq deft-filter-regexp (cons "" deft-filter-regexp))
(if (car deft-filter-regexp)
(setcar deft-filter-regexp (concat (car deft-filter-regexp) char))
(setq deft-filter-regexp (list char)))
(setq deft-current-files (deft-filter-files deft-current-files))
(setq deft-current-files (delq nil deft-current-files))
(defun deft-filter-decrement ()
"Remove last character from the filter, if possible, and update.
In incremental search mode, the elements of `deft-filter-regexp'
are the words of the filter string in reverse order. In regexp
search mode, the list is a single element containing the entire
filter regexp. Therefore, in both cases, only the car of
`deft-filter-regexp' is modified."
(let ((str (car deft-filter-regexp)))
(if (> (length str) 0)
;; If the last string element has at least one character,
;; simply remove the last character.
(substring str 0 -1)
;; Otherwise, return nil
(defun deft-filter-decrement-word ()
"Remove last word from the filter, if possible, and update."
(if deft-incremental-search
;; In incremental search mode, remove the car
;; In regexp search mode, remove last "word" component
;(replace-regexp-in-string "[[:space:]\n]*$" "" s)
(let ((str (car deft-filter-regexp)))
(if (> (length str) 0)
(insert (concat "\"" str "\""))
(goto-char (- (point-max) 1))
(backward-word 1)
(buffer-substring 2 (point)))
(defun deft-filter-yank ()
"Append the most recently killed or yanked text to the filter."
(concat (deft-whole-filter-regexp) (current-kill 0 t)) t))
(defun deft-complete ()
"Complete the current action.
If there is a widget at the point, press it. If a filter is
applied and there is at least one match, open the first matching
file. If there is an active filter but there are no matches,
quick create a new file using the filter string as the title.
Otherwise, quick create a new file."
;; Activate widget
(widget-button-press (point)))
;; Active filter string with match
((and deft-filter-regexp deft-current-files)
(deft-open-file (car deft-current-files)))
;; Default
;;; Automatic File Saving
(defun deft-auto-save ()
(dolist (buf deft-auto-save-buffers)
(if (buffer-name buf)
;; Save open buffers that have been modified.
(set-buffer buf)
(when (buffer-modified-p)
;; If a buffer is no longer open, remove it from auto save list.
(delq buf deft-auto-save-buffers)))))
;;; Mode definition
(defun deft-show-version ()
"Show the version number in the minibuffer."
(message "Deft %s" deft-version))
(defun deft-setup ()
"Prepare environment by creating the Deft notes directory."
(when (not (file-exists-p deft-directory))
(make-directory deft-directory t))
(defvar deft-mode-map
(let ((i 0)
(map (make-keymap)))
;; Make multibyte characters extend the filter string.
(set-char-table-range (nth 1 map) (cons #x100 (max-char))
;; Extend the filter string by default.
(setq i ?\s)
(while (< i 256)
(define-key map (vector i) 'deft-filter-increment)
(setq i (1+ i)))
;; Handle backspace and delete
(define-key map (kbd "DEL") 'deft-filter-decrement)
(define-key map (kbd "M-DEL") 'deft-filter-decrement-word)
;; Handle return via completion or opening file
(define-key map (kbd "RET") 'deft-complete)
;; Filtering
(define-key map (kbd "C-c C-l") 'deft-filter)
(define-key map (kbd "C-c C-c") 'deft-filter-clear)
(define-key map (kbd "C-y") 'deft-filter-yank)
;; File creation
(define-key map (kbd "C-c C-n") 'deft-new-file)
(define-key map (kbd "C-c C-m") 'deft-new-file-named)
(define-key map (kbd "<C-return>") 'deft-new-file-named)
;; File management
(define-key map (kbd "C-c C-d") 'deft-delete-file)
(define-key map (kbd "C-c C-r") 'deft-rename-file)
(define-key map (kbd "C-c C-f") 'deft-find-file)
;; Settings
(define-key map (kbd "C-c C-t") 'deft-toggle-incremental-search)
;; Miscellaneous
(define-key map (kbd "C-c C-g") 'deft-refresh)
(define-key map (kbd "C-c C-q") 'quit-window)
;; Widgets
(define-key map [down-mouse-1] 'widget-button-click)
(define-key map [down-mouse-2] 'widget-button-click)
(define-key map (kbd "<tab>") 'widget-forward)
(define-key map (kbd "<backtab>") 'widget-backward)
(define-key map (kbd "<S-tab>") 'widget-backward)
(define-key map (kbd "C-o") 'deft-open-file-other-window)
"Keymap for Deft mode.")
(defun deft-mode ()
"Major mode for quickly browsing, filtering, and editing plain text notes.
Turning on `deft-mode' runs the hook `deft-mode-hook'.
(setq truncate-lines t)
(setq buffer-read-only t)
(setq deft-directory (expand-file-name deft-directory))
(setq default-directory deft-directory)
(use-local-map deft-mode-map)
(setq major-mode 'deft-mode)
(when (> deft-auto-save-interval 0)
(run-with-idle-timer deft-auto-save-interval t 'deft-auto-save))
(run-mode-hooks 'deft-mode-hook))
(put 'deft-mode 'mode-class 'special)
(defun deft ()
"Switch to *Deft* buffer and load files."
(switch-to-buffer deft-buffer)
(if (not (eq major-mode 'deft-mode))
(provide 'deft)
;;; deft.el ends here