-
Notifications
You must be signed in to change notification settings - Fork 0
/
save-cwd.el
89 lines (74 loc) · 2.78 KB
/
save-cwd.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
;;; save-cwd.el --- Save the Current Working Directory to a file
;; Copyright (C) 2017 Wes Hardaker
;; Please see the distributed LICENSE file for licencing details
;; Version: 20170628.01
;; Author: Wes Hardaker <[email protected]>
;; Maintaner: Wes Hardaker
;; Package-Requires: ()
;; Keywords: convenience
;; URL: https://github.com/hardaker/elisp-save-cwd
;; License: GNU General Public License >= 2
;; Distribution: This file is not part of Emacs
;;; Commentary:
;;
;; How many times have you had to type in a long path in a shell to get
;; to the directory you were just editing a file while inside emacs?
;;
;; Usage:
;;
;; (require 'save-cwd)
;; (setq save-cwd-location "~/.emacs_cwd") ;; the default
;; (setq save-cwd-timer-period 5) ;; can be 1 and still be efficient, FYI
;; (save-cwd) ;; turns on the save-cwd minor mode
;;
;; Then in your shell:
;;
;; # cd `cat ~/.emacs_cwd`
;;
;; Or even simplier, define a command for it:
;;
;; # cde() { cd `cat ~/.emacs_cwd` ; echo "changed to `cat ~/.emacs_cwd`" ; }
;;
;; See the package website for more details:
;; https://github.com/hardaker/elisp-save-cwd
;;; Code:
(defcustom save-cwd-location "~/.emacs_cwd"
"Location to save the current buffer directory in."
:type 'file :group 'editing)
(defcustom save-cwd-timer-period 5
"Wait this many seconds of idle time before saving CWD.
The saving process is generally very cheap, so short values (even 1 second) sholud be fine."
:type 'integer :group 'editing)
(defvar save-cwd-timer-object nil)
(defvar save-cwd-current-directory nil)
(defun save-cwd-buffer-directory ()
"Determine the current directory."
(when buffer-file-name
(file-name-directory buffer-file-name)))
(defun save-cwd-save-directory (dirname)
"Save the current working directory (DIRNAME) to the file from ‘save-cwd-location’."
(with-temp-file save-cwd-location (insert dirname))
(setq save-cwd-current-directory save-cwd-cwd))
(defun save-cwd-timer-function ()
"On idle timer, save the cwd."
(let ((save-cwd-cwd (save-cwd-buffer-directory)))
(when (and save-cwd-cwd (not (equal save-cwd-current-directory save-cwd-cwd)))
(save-cwd-save-directory save-cwd-cwd))))
(defvar save-cwd-mode-p nil)
(define-minor-mode save-cwd-mode
"A mode to save Emacs' Current Working Directory."
:lighter " CWD"
:global t
:variable save-cwd-mode-p
(if save-cwd-mode-p
(progn
(when (not save-cwd-timer-object)
(setq save-cwd-timer-object
(run-with-idle-timer save-cwd-timer-period t #'save-cwd-timer-function))))
(progn
(when save-cwd-timer-object
(cancel-timer save-cwd-timer-object)
(setq save-cwd-timer-object nil)
(setq save-cwd-current-directory nil)))))
(provide 'save-cwd)
;;; save-cwd.el ends here