GNU   davin.50webs.com/research
Bringing to you notes for the ages

       Main Menu          Research Projects         Photo Album            Curriculum Vitae      The Greatest Artists
    Email Address       Computer Games          Web Design          Java Training Wheels      The Fly (A Story)   
  Political Activism   Scruff the Cat       My Life Story          Smoking Cessation          Other Links      
Debugging Macros     String Class I     Linked List System I Java for C Programmers Naming Convention
    String Class II         How I use m4              Strings III                 Symmetrical I/O             Linked Lists II     
Run-Time Type Info   Virtual Methods      An Array System        Science & Religion            Submodes       
  Nested Packages      Memory Leaks    Garbage Collection      Internet & Poverty      What is Knowledge?
Limits of Evolution   Emacs Additions      Function Plotter           Romantic Love        The Next Big Thing
    Science Fiction     Faster Compilation Theory of Morality         Elisp Scoping               Elisp Advice      
  S.O.G.M. Pattern       Safe Properties         School Bullying          Charisma Control          Life and Death    
     Splitting Java          Multiple Ctors       Religious Beliefs         Conversation 1           Conversation 2    
   J.T.W. language    Emacs Additions II      Build Counter             Relation Plotter          Lisp++ Language  
  Memory Leaks II   Super Constructors CRUD Implementation Order a Website Form There Is An Afterlife
More Occam's Razor C to Java Translator Theory of Morality II


d-recent.el

    

;;; d-recent.el --- log of recently edited files

;; Copyright (C) 2006-2011 Davin Pearson

;; Author/Maintainer: Davin Max Pearson <http://davin.50webs.com>
;; Keywords: recently edited files
;; Version: 1.0

;;; Commentary:

;; This file is not part of GNU Emacs.

;; When you quit Emacs a list of files that were edited is written to
;; disk in the file and folder ~/.emacs.d/recent-YYYYMMDD-HHMMSS.

;;; Limitation of Warranty

;; This program 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.
;;
;; This program 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 detail.
;;
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs, see the file COPYING.  If not, see:
;;
;; <http://www.gnu.org/licenses/gpl-3.0.txt>.


;;; Install Instructions:
;; See the following URL for the latest info and a tarball:
;;
;; <http://davin.50webs.com/research/2010/mopa2e2.html#d-recent>
;;
;; Then untar the tarball to a folder pointed to by the Emacs variable
;; load-path and add the following line to your ~/.emacs file.
;;
;; (require 'd-recent)

;;; Known Bugs:

;; None so far!

;;; Code:

(require 'early-bindings)
(require 'd-time)
(require 'd-make-faces)
(require 'd-groups)
(require 'd-find)

(defvar d-recent--pad-width 10)
(defvar d-recent--keymap (make-keymap))
(defvar d-recent--buf-name "*recent*")

(when prefs-davins-keybindings-online-p
  (global-set-key [kp-enter] 'd-recent-switch-to-buffer)
  )

;;(defvar d-recent--time-emacs-started-stamp (d-time--decode-time-readable (current-time)))
(defvar d-recent--persistent-file-name (car (last (directory-files "~/.emacs.d" t "^recent.*$"))))

(defun d-recent--get-nf-string (cons-cell)
  (assert (consp cons-cell))
  (assert (numberp (car cons-cell)))
  (assert (stringp (cdr cons-cell)))
  ;;(debug "Nail")
  (let ((number (format (concat "%" (format "%d" 10) "d") (car cons-cell)))
        (file   (cdr cons-cell)))
    (concat number " " file)))

(defun d-recent--insert-lines ()
  (progn
    (if (get-buffer d-recent--buf-name)
        (kill-buffer d-recent--buf-name))
    (generate-new-buffer d-recent--buf-name)
    (set-buffer d-recent--buf-name)
    (read-only-mode -1)
    (erase-buffer)
    (goto-char (point-min))
    (let ((ptr d-recent--list))
      (while ptr
        ;;(debug)
        (if (file-exists-p (cdar ptr))
            (insert (d-recent--get-nf-string (car ptr)) "\n"))
        (setq ptr (cdr ptr)))
      ))
  )

(defun d-recent--fontify-lines ()
  (assert (string= d-recent--buf-name (buffer-name)))
  (let ((array (make-vector 999 0)))
    (let ((i 0) (len (length array)))
      (while (< i len)
        (aset array i (d-read-str (eval (format "recent-line%04d" i))))
        (setq elt (aref array i))
        (make-face elt)
        (incf i)
        )
      (goto-char (point-min))
      (setq i 0)
      (while (and (< (point) (point-max)) (< i len))
        (let* ((s (d-current-line-as-string))
               (min (point-at-bol))
               (max (point-at-eol))
               (pair (d-groups-get-face (substring s (1+ d-recent--pad-width)))))
          (setq elt (aref array i))
          (set-face-background elt (nth 0 pair))
          (set-face-foreground elt (nth 1 pair))
          ;;(debug "Foo")
          (put-text-property min (1+ max) 'face elt)
          (forward-line 1)
          (incf i)
          )
        )
      )
    )
  )

(defun d-recent--pred (x y)
  (> (car x) (car y)))

(defun d-recent--generate-buffer ()
  (interactive)
  ;;(d-beeps "foo")
  (setq d-recent--list (sort d-recent--list 'd-recent--pred))
  (save-excursion
    (d-recent--insert-lines)
    (use-local-map d-recent--keymap)
    (when prefs-davins-keybindings-online-p
      (local-set-key "\C-m" 'd-recent--find-file))
    (d-recent--fontify-lines)
    ;;(debug "Bing")
    )
  (progn
    (set-buffer d-recent--buf-name)
    (goto-char (point-min)))
  )

(defun d-recent-switch-to-buffer ()
  (interactive)
  ;;(when (or (not (get-buffer d-recent--buf-name)) (= 0 (buffer-size (get-buffer d-recent--buf-name))))
  ;;(d-foo)
  (if (get-buffer d-recent--buf-name)
      (kill-buffer d-recent--buf-name))
  ;;(if (not (get-buffer d-recent--buf-name))
  (d-recent--generate-buffer)
  (switch-to-buffer d-recent--buf-name)
  ;;(debug 123)
  )

(defun d-recent--find-file ()
  (interactive)
  (let ((s (substring (d-current-line-as-string) (1+ d-recent--pad-width))))
    ;;(message "s=%s" s)
    (assert (file-exists-p s))
    (d-find-file s)
    ;;(kill-buffer (get-buffer d-recent--buf-name))
    )
  )

;;;
;;; USED BY: d-find.el
;;;
(defun d-recent-find-file-hook (filename)

(setq filename (safe-compress-file-name filename))

;;(d-beeps "Foo")

  (if (file-directory-p filename)
      (setq filename (concat filename "/")))

(if (string= d-recent--persistent-file-name filename)
      nil
    (if t;;(not (file-directory-p filename))
        (let ((found (rassoc filename d-recent--list)))
          (if found
              (setcar found (1+ (car found)))
            (setq d-recent--list (cons (cons 1 filename) d-recent--list))))

;;(d-recent--generate-buffer)

      )
    )
  )

(defun d-recent--load-log-file ()
  (save-excursion
    (setq d-recent--list nil)
    ;;
    ;; NOTE: find-file rather than d-find-file is used here to prevent an infinite recursion
    ;;
    (find-file d-recent--persistent-file-name)
    (goto-char (point-min))
    (while (< (point) (point-max))
      (let* ((line (buffer-substring-no-properties (point-at-bol) (point-at-eol)))
             (numb (d-read-str (substring line 0 d-recent--pad-width)))
             (file (d-trim-string (substring line (1+ d-recent--pad-width)))))
        (if (file-exists-p file)
            (setq d-recent--list (cons (cons numb file) d-recent--list))))
      (forward-line 1))
    (kill-buffer nil)
    )
  )

;;(d-recent--load-log-file)

(add-hook 'kill-emacs-hook 'd-recent--save-log-file)
;;(add-hook 'after-save-hook 'd-recent--save-log-file)
;;(add-hook 'write-file-hooks 'd-recent--save-log-file)
;;(setq kill-emacs-hook nil)
;;(setq after-save-hook nil)
;;(setq write-file-hooks nil)

(defun d-recent--save-log-file ()
  (interactive)
  (progn
    ;;
    ;; NOTE: doesn't use find-file to prevent an infinite recursion
    ;;
    (find-file d-recent--persistent-file-name)
    (read-only-mode -1)
    (erase-buffer)
    (let ((ptr d-recent--list)
          line)
      (while ptr
        (when (file-exists-p (cdar ptr))
          (setq line (d-recent--get-nf-string (car ptr)))
          (insert line "\n"))
        (setq ptr (cdr ptr))))
    (save-buffer 'NO-QUESTIONS)
    (kill-buffer)
    (message "Saved log file %s" d-recent--persistent-file-name)
    (sit-for 1)
    )
  )

;;;
;;; (setq ptr (buffer-list))
;;; (setq ptr (cdr ptr))
;;;
(defadvice save-some-buffers (before d-recent activate)
  (let* ((list  (buffer-list))
         (ptr   list)
         (found nil))
    (setq d-recent--list (sort d-recent--list 'd-recent--pred))
    (while (and ptr (not found))
      (when (and (buffer-modified-p (car ptr)) (buffer-file-name (car ptr)))
        ;;(debug)
        (setq found t))
      (setq ptr (cdr ptr)))
    ;;(debug)
    ;;(if found (d-recent--save-log-file))
    ;;(d-beeps "defadvice save-some-buffers")
  ))

(setq d-recent--list nil)

(provide 'd-recent)
Back
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Debugging Macros | String Class I | Linked List System I | Java for C Programmers | Naming Convention |
| String Class II | How I use m4 | Strings III | Symmetrical I/O | Linked Lists II |
| Run-Time Type Info | Virtual Methods | An Array System | Science & Religion | Submodes |
| Nested Packages | Memory Leaks | Garbage Collection | Internet & Poverty | What is Knowledge? |
| Limits of Evolution | Emacs Additions | Function Plotter | Romantic Love | The Next Big Thing |
| Science Fiction | Faster Compilation | Theory of Morality | Elisp Scoping | Elisp Advice |
| S.O.G.M. Pattern | Safe Properties | School Bullying | Charisma Control | Life and Death |
| Splitting Java | Multiple Ctors | Religious Beliefs | Conversation 1 | Conversation 2 |
| J.T.W. language | Emacs Additions II | Build Counter | Relation Plotter | Lisp++ Language |
| Memory Leaks II | Super Constructors | CRUD Implementation | Order a Website Form | There Is An Afterlife |
| More Occam's Razor | C to Java Translator | Theory of Morality II
Last modified: Mon May 6 01:56:53 NZST 2019
Best viewed at 800x600 or above resolution.
© Copyright 1999-2019 Davin Pearson.
Please report any broken links to