X-Git-Url: http://cgit.sxemacs.org/?p=riece;a=blobdiff_plain;f=lisp%2Friece-log.el;h=93a4ed915c3e05f19a6bafd0277b55ff096ce843;hp=e10e8d62bf498888dd4e89e1ca38d60858e5a1d8;hb=08df7f2c0e152ebcde1ce143fafbf092b3a8f533;hpb=b3843ad66c7080d97fb0dfb76823bde75f862daa diff --git a/lisp/riece-log.el b/lisp/riece-log.el index e10e8d6..93a4ed9 100644 --- a/lisp/riece-log.el +++ b/lisp/riece-log.el @@ -1,7 +1,9 @@ -;;; riece-log.el --- saving irc logs add-on +;;; riece-log.el --- Save IRC logs ;; Copyright (C) 2003 OHASHI Akira +;; Copyright (C) 2004 Daiki Ueno ;; Author: OHASHI Akira +;; Daiki Ueno ;; Keywords: IRC, riece ;; This file is part of Riece. @@ -18,15 +20,12 @@ ;; You should have received a copy of the GNU General Public License ;; along with GNU Emacs; see the file COPYING. If not, write to the -;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, -;; Boston, MA 02111-1307, USA. +;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, +;; Boston, MA 02110-1301, USA. ;;; Commentary: -;; This add-on saves irc logs for every channel. - -;; To use, add the following line to your ~/.riece/init.el: -;; (add-to-list 'riece-addons 'riece-log) +;;; NOTE: This is an add-on module for Riece. ;;; Code: @@ -34,7 +33,8 @@ (require 'riece-button) (defgroup riece-log nil - "Save irc log" + "Save IRC logs." + :prefix "riece-" :group 'riece) (defcustom riece-log-directory @@ -53,7 +53,8 @@ "*If non-nil, irc messages flash back from log files. If integer, flash back only this line numbers. t means all lines." :type '(choice (integer :tag "line numbers") - (boolean :tag "flash back or not")) + (const t :tag "of the day") + (const nil :tag "no flashback")) :group 'riece-log) (defcustom riece-log-coding-system nil @@ -68,65 +69,79 @@ If integer, flash back only this line numbers. t means all lines." :type 'symbol :group 'riece-log) -(defcustom riece-log-open-directory-function 'find-file - "*Function for opening a directory." - :type 'function - :group 'riece-log) - -(defvar riece-log-enabled nil) +(defface riece-log-date-face + '((((class color) + (background dark)) + (:foreground "Gray70")) + (((class color) + (background light)) + (:foreground "DimGray")) + (t + (:bold t))) + "Face used for displaying \"(YYYY/MM/dd)\" extent." + :group 'riece-highlight-faces) +(defvar riece-log-date-face 'riece-log-date-face) + +(defvar riece-log-lock-file nil + "Lock file for riece-log. +It is created if there is at least one instance of Emacs running riece-log.") + +(defconst riece-log-file-name-regexp + (concat (riece-make-interval-regexp "[0-9]" 8) "\\.txt\\(\\.\\(.*\\)\\)?$")) (defconst riece-log-description - "Saving IRC logs") + "Save IRC logs.") (defun riece-log-display-message-function (message) - (if riece-log-enabled - (let ((file (riece-log-get-file (riece-message-target message))) - (coding-system-for-write riece-log-coding-system)) + (if (get 'riece-log 'riece-addon-enabled) + (let* ((coding-system-for-write + (if (featurep 'mule) + (or riece-log-coding-system + (car (get-language-info current-language-environment + 'coding-system))))) + (file (riece-log-make-file-name (riece-message-target message) + coding-system-for-write)) + file-name-coding-system + default-file-name-coding-system) (unless (file-directory-p (file-name-directory file)) (make-directory (file-name-directory file) t)) (write-region (concat (format-time-string "%H:%M") " " (riece-format-message message)) - nil file t 0)))) - -(defun riece-log-get-file (identity) - (expand-file-name - (concat (format-time-string "%Y%m%d") ".log") - (riece-log-get-directory identity))) - -(defun riece-log-get-files (identity) - (let ((files (directory-files (riece-log-get-directory identity) t - (concat "^" - (riece-make-interval-regexp "[0-9]" 8) - "\\.log$") - t))) - (nreverse (sort files #'string-lessp)))) - -(defun riece-log-get-directory (identity) - (let ((channel (riece-identity-canonicalize-prefix - (riece-identity-prefix identity))) - (server (riece-identity-server identity)) - (map (assoc (riece-format-identity identity) riece-log-directory-map)) - name) - (cond (map (setq name (cdr map))) - ((string-match riece-strict-channel-regexp channel) - (let ((suffix (match-string 2 channel))) - (setq name (substring channel (match-end 1) (match-beginning 2))) - (when (and (stringp suffix) - (string-match "^:\\*\\.\\(.*\\)" suffix)) - (setq name (concat name "-" (match-string 1 suffix)))))) - (t (setq name "priv"))) - (if server - (expand-file-name name (expand-file-name server riece-log-directory)) - (expand-file-name name riece-log-directory)))) - -(defun riece-log-get-directory-1 (identity) + nil file t 0 + riece-log-lock-file)))) + +(defun riece-log-make-file-name (identity coding-system) + (expand-file-name (if (featurep 'mule) + (format "%s.txt.%s" + (format-time-string "%Y%m%d") + coding-system) + (format "%s.txt" + (format-time-string "%Y%m%d"))) + (riece-log-directory identity))) + +(defun riece-log-list-files (identity time) + (let ((directory (riece-log-directory identity)) + (time-prefix (format-time-string "%Y%m%d" (or time '(0 0)))) + files) + (when (file-directory-p directory) + (setq files (nreverse (sort (directory-files + directory t + (concat "^" riece-log-file-name-regexp) + t) + #'string-lessp))) + (while (and files + (string-lessp (file-name-nondirectory (car files)) + time-prefix)) + (setq files (cdr files))) + files))) + +(defun riece-log-directory (identity) (let ((prefix (riece-identity-canonicalize-prefix (riece-identity-prefix identity))) (server (riece-identity-server identity)) - (map (assoc (riece-format-identity identity) riece-log-directory-map)) - name) + (map (assoc (riece-format-identity identity) riece-log-directory-map))) (if map - (setq name (cdr map)) + (expand-file-name (cdr map) riece-log-directory) (expand-file-name (riece-log-encode-file-name prefix) (expand-file-name (concat "." (riece-log-encode-file-name server)) @@ -166,70 +181,91 @@ If integer, flash back only this line numbers. t means all lines." riece-log-file-name-coding-system))) file-name) -(defun riece-log-flashback-1 (identity) - (if (eq riece-log-flashback t) - (let ((file (riece-log-get-file identity))) - (if (file-exists-p file) - (insert-file-contents file))) - (let ((files (riece-log-get-files identity)) - (lines (- riece-log-flashback)) - date point) - (while (and (< lines 0) files) - (if (and (file-exists-p (car files)) - (string-match (concat "\\(" - (riece-make-interval-regexp "[0-9]" 4) - "\\)\\(" - (riece-make-interval-regexp "[0-9]" 2) - "\\)\\(" - (riece-make-interval-regexp "[0-9]" 2) - "\\).log$") - (car files))) - (save-restriction - (narrow-to-region (point-min) (point-min)) - (setq date (concat " (" (match-string 1 (car files)) "/" - (match-string 2 (car files)) "/" - (match-string 3 (car files)) ")")) - (insert-file-contents (car files)) - (goto-char (point-max)) - (setq lines (forward-line lines) - point (point)) - (while (not (eobp)) - (end-of-line) - (insert date) - (forward-line)) - (goto-char point))) - (setq files (cdr files))) - (if (zerop lines) - (delete-region (point-min) (point)))))) +(defun riece-log-insert (identity lines) + "Insert logs for IDENTITY at most LINES. +If LINES is t, insert today's logs entirely." + (let* (file-name-coding-system + default-file-name-coding-system + (files (riece-log-list-files identity + (if (eq lines t) (current-time)))) + name coding-system date point) + (while (and (or (eq lines t) (> lines 0)) files) + (save-restriction + (narrow-to-region (point) (point)) + (if (and (string-match + (concat "^" riece-log-file-name-regexp) + (setq name (file-name-nondirectory (car files)))) + (match-beginning 2)) + (progn + (setq coding-system + (intern (substring name (match-beginning 2)))) + (if (featurep 'xemacs) + (setq coding-system (find-coding-system coding-system)) + (unless (coding-system-p coding-system) + (setq coding-system nil))) + (if coding-system + (let ((coding-system-for-read coding-system)) + (insert-file-contents (car files))) + ;;don't insert file contents if they use non + ;;supported coding-system. + )) + ;;if the filename has no coding-system suffix, decode with + ;;riece-log-coding-system. + (let ((coding-system-for-read riece-log-coding-system)) + (insert-file-contents (car files)))) + ;;lines in the file contents are in reversed order. + (unless (eq lines t) + (goto-char (point-max)) + (setq lines (- (forward-line (- lines)))) + (delete-region (point-min) (point))) + ;;add (YYYY/MM/dd) suffix on each line left in the current buffer. + (unless (equal (substring name 0 8) (format-time-string "%Y%m%d")) + (setq date (concat " (" (substring name 0 4) "/" + (substring name 4 6) "/" + (substring name 6 8) ")")) + (while (not (eobp)) + (end-of-line) + (setq point (point)) + (insert date) + (put-text-property point (point) + 'riece-overlay-face 'riece-log-date-face) + (forward-line)) + (goto-char (point-min)))) + (setq files (cdr files))))) (defun riece-log-flashback (identity) (when riece-log-flashback + (riece-insert-info (current-buffer) + (if (eq riece-log-flashback t) + "Recent messages of the day:\n" + (format "Recent messages up to %d lines:\n" + riece-log-flashback))) (let (buffer-read-only (point (goto-char (point-max)))) (insert (with-temp-buffer - (riece-log-flashback-1 identity) + (riece-log-insert identity riece-log-flashback) (buffer-string))) (goto-char point) (while (re-search-forward - "^[0-9][0-9]:[0-9][0-9] [<>]\\([^<>]+\\)[<>] " nil t) - (put-text-property (match-beginning 1) (match-end 1) + (concat "^" riece-time-prefix-regexp + "\\(<[^>]+>\\|>[^<]+<\\|([^)]+)\\|{[^}]+}\\|=[^=]+=\\)") + nil t) + (put-text-property (1+ (match-beginning 1)) (1- (match-end 1)) 'riece-identity (riece-make-identity - (riece-match-string-no-properties 1) + (buffer-substring (1+ (match-beginning 1)) + (1- (match-end 1))) (riece-identity-server identity)))) - (when (and (memq 'riece-button riece-addons) - riece-button-enabled) - (riece-button-update-buffer)) - (goto-char (point-max)) + (run-hook-with-args 'riece-after-insert-functions + point (goto-char (point-max))) (set-window-point (get-buffer-window (current-buffer)) (point))))) -(defun riece-log-open-directory (&optional channel) +(defun riece-log-dired (&optional channel) (interactive) - (let ((directory (riece-log-get-directory - (or channel riece-current-channel)))) + (let ((directory (riece-log-directory (or channel riece-current-channel)))) (if (file-directory-p directory) - (funcall riece-log-open-directory-function directory) + (dired directory) (error "No log directory")))) (defun riece-log-requires () @@ -237,6 +273,13 @@ If integer, flash back only this line numbers. t means all lines." '(riece-button))) (defun riece-log-insinuate () + (make-directory riece-log-directory t) + (setq riece-log-lock-file + (expand-file-name (format "!%s-%d-%d" + (riece-log-encode-file-name (system-name)) + (user-uid) + (emacs-pid)) + riece-log-directory)) ;; FIXME: Use `riece-after-insert-functions' for trapping change, ;; notice, wallops and so on. But must add argument. (add-hook 'riece-after-display-message-functions @@ -244,14 +287,19 @@ If integer, flash back only this line numbers. t means all lines." (add-hook 'riece-channel-buffer-create-functions 'riece-log-flashback)) +(defun riece-log-uninstall () + (setq riece-log-lock-file nil) + (remove-hook 'riece-after-display-message-functions + 'riece-log-display-message-function) + (remove-hook 'riece-channel-buffer-create-functions + 'riece-log-flashback)) + (defvar riece-command-mode-map) (defun riece-log-enable () - (define-key riece-command-mode-map "\C-cd" 'riece-log-open-directory) - (setq riece-log-enabled t)) + (define-key riece-command-mode-map "\C-cd" 'riece-log-dired)) (defun riece-log-disable () - (define-key riece-command-mode-map "\C-cd" nil) - (setq riece-log-enabled nil)) + (define-key riece-command-mode-map "\C-cd" nil)) (provide 'riece-log)