From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Michal Nazarewicz Newsgroups: gmane.emacs.devel Subject: [PATCH] Add `notes' function to store random notes across Emacs restarts. Date: Mon, 17 Jun 2013 18:13:04 +0200 Message-ID: NNTP-Posting-Host: plane.gmane.org X-Trace: ger.gmane.org 1371485611 26023 80.91.229.3 (17 Jun 2013 16:13:31 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 17 Jun 2013 16:13:31 +0000 (UTC) Cc: Ted Zlatanov , emacs-devel@gnu.org To: Glenn Morris , Stefan Monnier Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Jun 17 18:13:31 2013 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Uoc3f-0000XW-62 for ged-emacs-devel@m.gmane.org; Mon, 17 Jun 2013 18:13:31 +0200 Original-Received: from localhost ([::1]:36294 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uoc3e-00084K-Oh for ged-emacs-devel@m.gmane.org; Mon, 17 Jun 2013 12:13:30 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:52068) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uoc3X-00083d-It for emacs-devel@gnu.org; Mon, 17 Jun 2013 12:13:28 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Uoc3U-0006u2-7e for emacs-devel@gnu.org; Mon, 17 Jun 2013 12:13:23 -0400 Original-Received: from mail-ee0-x232.google.com ([2a00:1450:4013:c00::232]:59276) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Uoc3T-0006to-Um for emacs-devel@gnu.org; Mon, 17 Jun 2013 12:13:20 -0400 Original-Received: by mail-ee0-f50.google.com with SMTP id d49so1963628eek.9 for ; Mon, 17 Jun 2013 09:13:18 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer; bh=3pRtvf/FuxShEV747+k6Z99nVigDwHFrEe1sixRDCVI=; b=CJsyJnAiWwmFdkKoDlnL/wmYILCU4jJ27bsC2Ju6k7ZcLjyaOG3/so/68aFf+KaDAF +rTJVz9dHCNim8Lx+sAkHPwDmc6Bw2u/VsHxTlDHAo3JWBNdb3wDQeE0+xUAZBAhHYh2 HRSTXPEyGV15lq/eafgokhog3JwBn0tQ/SgJyhBDs9290TFittXLk4xZNneljip5CrdR asFHtXuI5PMAhYKMhfFT5XlQA6XmulkadBMOjgt/ZIaXcserltbb0ah7fE4qxOl3Kby8 EQwuG7062AVaZbwQ3b/zrLvDhp6zA30+b2ysAXa4mfDb1FptNyWd1llfwcctimAhT3kW Qtpg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:x-gm-message-state; bh=3pRtvf/FuxShEV747+k6Z99nVigDwHFrEe1sixRDCVI=; b=RcPD1gIiNLGUlS6gMyhnsJGDvgAHRemtY+O2GSganlnxpWliR16yXd9eXhB64KykOy +qT3IbEFA1RYK4jFnafLwCMPlZOb7Uo0px/QJEdwXGjdMjmIKvoSRdA/J/aRC2MsSeTE VsXKmLi2wgufZN01v1e9DIyfaHIInOSVbwmHzeKRcNpUn8hJTF3U/s97preSjd3lPuV5 rGndMdySLtxCEIyImtOePxxgTOYnBQOUbjecvRcG+19rliYy0LSpIT+BN0BVGR3GQ9hI p/B7VLJdeLEAk205Cc6Z1JyMrYUBbXQAH6lxLKWBZvducqQhjGl42wKP8LW0nc2dj8Mi OYtA== X-Received: by 10.15.94.131 with SMTP id bb3mr17897296eeb.20.1371485598686; Mon, 17 Jun 2013 09:13:18 -0700 (PDT) Original-Received: from mpn-glaptop.corp.google.com ([2620:0:105f:301:79e7:3b94:9c87:4018]) by mx.google.com with ESMTPSA id p49sm1562758eeu.2.2013.06.17.09.13.16 for (version=TLSv1.1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Mon, 17 Jun 2013 09:13:17 -0700 (PDT) X-Mailer: git-send-email 1.8.3 X-Gm-Message-State: ALoCoQm5Z4P6MpD379eaAAa4etb+4L/4DPTYT75k8UXCA/uzxYuFdTi804jhDE0eBwP4Ho70SynHIDb4SuAbxCXEMBgptekidEw5PQ53xD7lcRyWUaEjkcDtfyEkLkCrG4L63s5RKWz6wZxgnz0b0ZpgOYGcpG47bv2wzUhLv40LxH0oQlIZOZUoFb5jNOoiwWzVghJo3aOJ X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2a00:1450:4013:c00::232 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:160509 Archived-At: From: Michal Nazarewicz You may think of it as a *scratch* buffer whose content is preserved. In fact, it was designed as a replacement for *scratch* buffer and can be used that way by setting `initial-buffer-choice' to 'notes an `notes-buffer-name' to "*scratch*". Without the second change, *scratch* buffer will still be there for notes that do not need to be preserved. * list/startup.el (notes): New function creating notes buffer. (notes-file, notes-recover-from-auto-save-file, notes-buffer-name) (initial-notes-major-mode): New customize variables for customizing behaviour of the notes buffer. (notes--bury-on-kill-buffer, notes--insert-content): New helper functions for `notes' function. (notes--buffer): New helper variable for `notes' function. (notes--initial-message): New helper constant for `notes' function. --- etc/NEWS | 8 +++ lisp/ChangeLog | 8 +++ lisp/notes.el | 205 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ lisp/startup.el | 3 +- lisp/window.el | 10 +++ 5 files changed, 233 insertions(+), 1 deletion(-) create mode 100644 lisp/notes.el diff --git a/etc/NEWS b/etc/NEWS index 843234a..83cab11 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -87,6 +87,14 @@ simply disabling Transient Mark mode does the same thing. ** `initial-buffer-choice' can now specify a function to set up the initial buffer. +** `notes' function creates a buffer whose content is saved on kill-emacs. +You may think of it as a *scratch* buffer whose content is preserved. +In fact, it was designed as a replacement for *scratch* buffer and can +be used that way by setting `initial-buffer-choice' to 'notes an +`notes-buffer-name' to "*scratch*". Without the second +change, *scratch* buffer will still be there for notes that do not +need to be preserved. + ** `write-region-inhibit-fsync' now defaults to t in batch mode. ** ACL support has been added. diff --git a/lisp/ChangeLog b/lisp/ChangeLog index 632c909..bb4dc37 100644 --- a/lisp/ChangeLog +++ b/lisp/ChangeLog @@ -1,3 +1,11 @@ +2013-06-17 Michal Nazarewicz + + Add `notes' function to store random notes across Emacs restarts. + * notes.el: New file. + * startup.el (initial-buffer-choice): Added notes to custom type. + * window.el (save-and-bury-buffer): New function doing what the + name says. + 2013-06-17 Dmitry Gutov * emacs-lisp/package.el (package-load-descriptor): Do not call diff --git a/lisp/notes.el b/lisp/notes.el new file mode 100644 index 0000000..d5de55f --- /dev/null +++ b/lisp/notes.el @@ -0,0 +1,205 @@ +;;; notes.el --- notes preserved across sessions -*- lexical-binding: t -*- + +;; Copyright (C) 2013 Free Software Foundation, Inc. + +;; Author: Michal Nazarewicz +;; Maintainer: FSF +;; Keywords: todo pim notes +;; Package: emacs + +;; This file is part of GNU Emacs. + +;; GNU Emacs is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; GNU Emacs is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with GNU Emacs. If not, see . + +;;; Commentary: + +;; This file defines a `notes' function which creates a buffer whose +;; content is saved on kill-emacs. +;; +;; You may think of it as a *scratch* buffer whose content is preserved. +;; In fact, it was designed as a replacement for *scratch* buffer and can +;; be used that way by doing the following: +;; +;; (setq initial-buffer-choice 'notes +;; notes-buffer-name "*scratch*") +;; +;; Without setting `notes-buffer-name', *scratch* buffer will still be +;; there for notes that do not need to be preserved. +;; +;; To quickly access notes buffer, you might want to bind `notes' or +;; `toggle-notes' function. + +;;; Code: + +(defvar notes--buffer nil + "The notes buffer.") + +(defcustom notes-file (locate-user-emacs-file "notes" ".notes") + "File to save notes in. +When set via customize `buffer-file-name' variable of the notes buffer +\(if it exists) will be changed." + :type 'string + :set (lambda (symbol value) + (set-default symbol value) + (when (buffer-live-p notes--buffer) + (with-current-buffer notes--buffer + (setq buffer-file-name value)))) + :group 'initialization) + +(defcustom notes-recover-from-auto-save-file 'ask + "What to do if notes autosave file is newer than the notes file. + +t means to always recover content from auto-save file, 'ask means +to ask user, and nil means never to recover auto-save file (which +also disables `auto-save-mode' in the notes buffer. + +When set via customize, `auto-save-mode' will be enabled or disabled +in the notes buffer according to this variable's value." + :type '(choice (const :tag "Always recover auto-save" t) + (const :tag "Never recover auto-save" nil) + (const :tag "Ask whether to recover auto-save" ask)) + :set (lambda (symbol value) + (set-default symbol value) + (when (buffer-live-p notes--buffer) + (with-current-buffer notes--buffer + (auto-save-mode (if value 1 -1))))) + :group 'initialization) + +(defcustom notes-buffer-name "*notes*" + "Name of the notes buffer. +Setting it to *scratch* will hijack the *scratch* buffer for the +purpose of storing notes." + :type 'string + :group 'initialization) + +(defcustom initial-notes-major-mode t + "Major mode to set to notes buffer when it's created. +If set to t will use the same mode as `initial-major-mode'." + :type '(choice (const :tag "Same as `initial-major-mode'" t) + (function :tag "Major mode" text-mode)) + :group 'initialization) + +(defcustom bury-notes-on-kill t + "Whether to bury notes buffer instead of killing." + :type 'boolean + :group 'initialization) + +(defconst notes--initial-message (purecopy "\ +;; This buffer is for notes and for Lisp evaluation. +;; If you want to create a file, visit that file with C-x C-f, +;; then enter the text in that file's own buffer. +;; Contents of this buffer will be saved across restarts. + +")) + +(defvar notes-map + (let ((map (make-sparse-keymap))) + (define-key map "\C-c\C-c" 'save-and-bury-buffer) + map)) + +;;;###autoload +(defun notes () + "Creates notes buffer and switches to it if called interactively. + +Name of the created buffer is taken from `notes-buffer-name' variable +and if buffer with that name already exist (but was not created by +`notes' function), its content will be overwritten. + +`notes-map' is active in the notes buffer which by default contains +only one C-c C-c binding which saves and buries the buffer. + +Function returns notes buffer. + +Notes buffer is meant for keeping random notes which you'd like to +preserve across Emacs restarts." + (interactive) + (unless (buffer-live-p notes--buffer) + (setq notes--buffer (get-buffer-create notes-buffer-name)) + (with-current-buffer notes--buffer + (funcall (if (eq initial-notes-major-mode t) + initial-major-mode + initial-notes-major-mode)) + (setq buffer-file-name notes-file + buffer-save-without-query t) + (auto-save-mode (if notes-recover-from-auto-save-file 1 -1)) + ;; We don't want a "Buffer modified" prompt from kill-buffer so + ;; we have to use advice rather than a hook. + (advice-add 'kill-buffer :around 'notes--kill-buffer-advice) + (setq minor-mode-overriding-map-alist + (cons (cons 'notes--buffer notes-map) + minor-mode-overriding-map-alist)) + (notes--insert-content))) + (when (called-interactively-p 'all) + (switch-to-buffer notes--buffer)) + notes--buffer) + +;;;###autoload +(defun toggle-notes () + "Switches to notes buffer unless already there in which case buries it." + (interactive) + (if (eq (current-buffer) notes--buffer) + (bury-buffer) + (switch-to-buffer (notes)))) + +(defun notes--insert-content () + (let* ((have-file (file-readable-p buffer-file-name)) + (have-auto-save (and buffer-auto-save-file-name + (file-readable-p buffer-auto-save-file-name)))) + ;; If autosave is older, pretend it does not exist. + (and have-file + have-auto-save + (not (file-newer-than-file-p buffer-auto-save-file-name + buffer-file-name)) + (setq have-auto-save nil)) + ;; If user wants us to always recover, pretend there's no base file. + (and have-auto-save + (eq t notes-recover-from-auto-save-file) + (setq have-file nil)) + ;; Ask user what to do. + (and have-file + have-auto-save + (if (y-or-n-p "Recover notes file? ") + (setq have-file nil) + (setq have-auto-save nil))) + (let ((file (cond (have-file buffer-file-name) + (have-auto-save buffer-auto-save-file-name)))) + (cond (file + (insert-file-contents file nil nil nil t) + (set-buffer-modified-p nil)) + ((zerop (buffer-size)) + (insert notes--initial-message) + (set-buffer-modified-p t)))))) + +(defun notes--kill-buffer-advice (func &optional buffer) + (if (null notes--buffer) + (funcall func buffer) + (setq buffer (cond ((null buffer) (current-buffer)) + ((stringp buffer) (get-buffer buffer)) + (buffer))) + (and (buffer-live-p buffer) + (not (when (eq buffer notes--buffer) + (when (buffer-modified-p buffer) + (with-current-buffer buffer + (save-buffer))) + (when bury-notes-on-kill + (bury-buffer (unless (eq buffer (current-buffer)) buffer)) + t))) + (funcall func buffer) + (progn + (when (eq buffer notes--buffer) + (advice-remove 'kill-buffer 'notes--kill-buffer-advice) + (setq notes--buffer nil)) + t)))) + +;;; notes.el ends here diff --git a/lisp/startup.el b/lisp/startup.el index 52dd6b0..7fc1a04 100644 --- a/lisp/startup.el +++ b/lisp/startup.el @@ -53,7 +53,8 @@ or directory when no target file is specified." (const :tag "Startup screen" nil) (directory :tag "Directory" :value "~/") (file :tag "File" :value "~/.emacs") - (function :tag "Function") + (const :tag "Notes buffer" notes) + (function :tag "Function") (const :tag "Lisp scratch buffer" t)) :version "24.4" :group 'initialization) diff --git a/lisp/window.el b/lisp/window.el index 5b00198..096b7bb 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -3429,6 +3429,16 @@ displayed there." ;; Always return nil. nil)) +(defun save-and-bury-buffer () + "Saves and buries current buffer. +If `buffer-modified-p' return non-nil current buffer will be +saved via the `save-buffer' function. Regardless of modification +state, it will also be buried with `burre-buffer'." + (interactive) + (when (buffer-modified-p) + (save-buffer)) + (bury-buffer)) + (defun unbury-buffer () "Switch to the last buffer in the buffer list." (interactive) -- 1.8.3