Skip to content

Instantly share code, notes, and snippets.

@abdullahkhalids
Created August 8, 2013 22:11
Show Gist options
  • Save abdullahkhalids/6189288 to your computer and use it in GitHub Desktop.
Save abdullahkhalids/6189288 to your computer and use it in GitHub Desktop.
This emacs function helps quickly record ideas. Run it and open a buffer ready to jot down an idea
;; Scratch
;; This program helps keep a record of ideas.
;; Last edit: 2012-07-05
;; Every time (scratch) is called a new buffer is opened. Title
;; is today's date appended before a user inputed title.
;Needed features
;* An interactive shell that shows me a list of entries
; from a certain time period.
;The journal folder. This should be set in the .emacs file.
;(setq scratch-folder "~/Documents/Ideas")
;Format for entry filename
(setq scratch-entry-format "%Y-%m-%d")
;Internal Constants
(setq sep "/")
;; Functions
(defun todays-filename ()
"The last modification time of a file.
Returned in the format YYYYMMDDHHMMSS."
(format-time-string scratch-entry-format))
(defmacro inc (x)
"increments x by 1."
`(setq ,x (1+ ,x)))
(defmacro remove-starting-char (string char)
"removes a char from the start of a string."
`(if (string= (substring ,string 0 1) ,char)
(setq ,string (substring ,string 1 (length ,string)))))
(defmacro remove-ending-char (string char)
"removes a char from the end of a string."
`(let ((l (length ,string)))
(if (string= (substring ,string (1- l) l)
,char)
(setq ,string (substring ,string 0 (1- l))))))
(defun concat-paths (&rest paths)
"Given a set of paths concats them.
Makes sure that sep are alright."
(let ((n (length paths))
(i 0)
(concated-path "")
(path)
(subpath))
(while (< i n)
(setq subpath (nth i paths))
(if (not (= i 0))
(remove-starting-char subpath sep))
(if (> (length subpath) 0)
(progn
(remove-ending-char subpath sep)
(setq concated-path (concat concated-path
subpath
sep))))
(inc i))
(remove-ending-char concated-path sep)))
;The following two functions taken from
; https://sites.google.com/site/steveyegge2/my-dot-emacs-file
(defun rename-file-and-buffer (new-name)
"Renames both current buffer and file it's visiting to NEW-NAME."
(interactive "sNew name: ")
(let ((name (buffer-name))
(filename (buffer-file-name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(if (get-buffer new-name)
(message "A buffer named '%s' already exists!" new-name)
(progn
(rename-file name new-name 1)
(rename-buffer new-name)
(set-visited-file-name new-name)
(set-buffer-modified-p nil))))))
(defun move-buffer-file (dir)
"Moves both current buffer and file it's visiting to DIR." (interactive "DNew directory: ")
(let* ((name (buffer-name))
(filename (buffer-file-name))
(dir
(if (string-match dir "\\(?:/\\|\\\\)$")
(substring dir 0 -1) dir))
(newname (concat dir "/" name)))
(if (not filename)
(message "Buffer '%s' is not visiting a file!" name)
(progn
(copy-file filename newname 1)
(delete-file filename)
(set-visited-file-name newname)
(set-buffer-modified-p nil)
t))))
(defun scratch ()
"Create's or appends to today's journal entry."
(interactive)
(let ((file (concat-paths scratch-folder (todays-filename))))
(find-file file)
(auto-fill-mode -5)
(restore-buffer-modified-p t)
(save-buffer)
(goto-char (point-max))
(title-scratch)
(message "Scratch an idea...")))
(defun title-scratch ()
(interactive)
(let ((date (substring (buffer-name (current-buffer)) 0 10))
(title))
(setq title (read-from-minibuffer "Title for idea: "))
(if (not (string= title ""))
(rename-file-and-buffer (concat date " " title)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment