Initial Commit
[packages] / xemacs-packages / text-modes / crontab.el
1 ;; #(@) crontab.el - An Emacs function to assist in editing crontab entries
2 ;; Last edited: Fri Aug 18 12:19:22 1989 by chris (Christopher D. Orr) on lxn
3 ;;
4 ;; Version: 1.00 - Initial Creation of mode
5 ;;          1.01 - Added crontab-use-local-file variable
6 ;;          1.02 - Reworked most of the library to be cleaner.
7 ;;          1.03 - Now deletes blank lines in crontab entry
8
9 ;; Copyright (C) 1989 Christopher D. Orr (chris@lxn.eds.com)
10
11 ;; This file is part of XEmacs.
12
13 ;; XEmacs is free software; you can redistribute it and/or modify it
14 ;; under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; XEmacs is distributed in the hope that it will be useful, but
19 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 ;; General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with XEmacs; see the file COPYING.  If not, write to the 
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
27
28 ;;; Synched up with: Not in FSF.
29
30 ;;
31 ;; TODO:
32 ;;
33
34 ;; Place the following line in your ~/.emacs file:
35 ;;    (autoload 'crontab-edit "crontab"
36 ;;              "Function to allow the easy editing of crontab files." t)
37 ;;
38
39 (provide 'crontab-edit)
40
41 ;;; Local Variables.  Define these to your liking.
42
43 (defgroup crontab nil
44   "Assist in editing crontab files."
45   :group 'languages)
46
47
48 (defcustom crontab-filename "~/.crontab"
49   "*The name of the file to store the User's Crontab."
50   :type 'file
51   :group 'crontab)
52
53 (defcustom crontab-directory "/usr/spool/cron/crontabs"
54   "*The name of the directory in which crontab stores it's entries."
55   :type 'file
56   :group 'crontab)
57
58 (defcustom crontab-use-local-file nil
59   "*Non-nil means use file stored in User's Home directory, if it exists.
60 Otherwise, always ask crontab for the current entry (maybe)."
61   :type 'boolean
62   :group 'crontab)
63
64 (defcustom crontab-delete-blank-lines t
65   "*Non-nil means to delete any blank lines in the crontab file on save."
66   :type 'boolean
67   :group 'crontab)
68
69 ;;; Interactive Function called to edit a Crontab Entry.  It is called
70 ;;; instead of crontab-edit to allow for future automatic entries.
71
72 (defun crontab-edit ()
73   "Function to allow the easy editing of crontab files."
74
75   (interactive)
76   (crontab-get))
77
78
79 ;;; Function to retrieve the crontab entry.  The Function will
80 ;;; retrieve the file (crontab-filename) first.  If the file does not
81 ;;; exists, a crontab -l command will be executed. 
82
83 (defun crontab-get ()
84    "Retrieve a crontab file either using crontab -l or from the variable
85 crontab-filename"
86    (message "Retrieving Crontab ... ")
87    (switch-to-buffer (create-file-buffer crontab-filename))
88    (erase-buffer)
89
90    (if (file-exists-p crontab-filename)
91        (if (file-newer-than-file-p (concat crontab-directory "/" (user-login-name)) (expand-file-name crontab-filename))
92            (if (yes-or-no-p "Cron has a more recent copy of your crontab.  Use it ? ")
93                (call-process "crontab" nil t t "-l")
94              (insert-file crontab-filename))
95          (if crontab-use-local-file
96              (insert-file crontab-filename)
97            (call-process "crontab" nil t t "-l")))
98      (if crontab-use-local-file
99          (insert-file crontab-filename)
100        (call-process "crontab" nil t t "-l")))
101
102 ;; What if crontab returns a fatal ??????  Can't we check the errorlevel ????
103    (goto-char (point-min))
104    (if (search-forward-regexp "crontab:\\|no crontab for" nil t nil)
105        (erase-buffer))
106    (if (eobp)
107        (crontab-initialize))
108    (goto-line 6)
109    (setq buffer-file-name crontab-filename)
110    (set-buffer-modified-p nil)
111    (make-variable-buffer-local 'write-file-hooks)
112    (or (memq 'crontab-save write-file-hooks)
113        (setq write-file-hooks 
114              (reverse (cons 'crontab-save (reverse write-file-hooks)))))
115    (message "Save file normally when finished to update cron."))
116
117
118 ;;; This function is called whenever a save-file operation is
119 ;;; performed in the crontab buffer.  It saves the crontab to the file
120 ;;; name (crontab-filename) and then removes the crontab buffer.
121
122 (defun crontab-save ()
123   "Submit the edited crontab to the cron daemon for processing."
124
125   (when crontab-delete-blank-lines
126     (goto-char (point-min))
127     (while (not (eobp))
128       (delete-blank-lines)
129       (forward-line 1))
130     (redraw-display))
131
132   (setq write-file-hooks nil)
133   (let ((crontab-buffer (buffer-name)))
134     (basic-save-buffer)
135
136 ;; What if the call-process to crontab fails ???  Can we check for a fatal ???
137 ;;  (call-process "crontab" nil nil nil (expand-file-name crontab-filename))
138     (shell-command (concat "crontab " (expand-file-name crontab-filename)))
139
140     (switch-to-buffer (other-buffer))
141     (kill-buffer crontab-buffer))
142   (message (concat "Crontab saved as " crontab-filename " and submitted to cron."))
143 ;; fixed by Lynn D. Newton - 03/17/95
144   "")
145 ;; OLD
146 ;; nil)
147
148 (defun crontab-initialize ()
149   "Create a default Crontab file if one does not exist or is empty.
150 If the function (time-stamp) is available, the last modification time will
151 be stamped to the file."
152
153    (insert "# Cron Table Entry for ")
154    (insert (user-login-name))
155    (insert " (")
156    (insert (user-full-name))
157    (insert ")\n# Last Edited: \n")
158    (insert "#\n")
159    (insert "# min    hr     day   mon    wday(0=sun)  cmd\n")
160    (insert "#\n"))
161
162 ;;; Watch out for the signature  :-)