Interzona

Emacs

Table of Contents

Emacs

Here you can find the Emacs packages i am working on and some (i hope) useful elisp snippets.

Annotate

I am co-maintainer of annotate.el, an Emacs package to stick textual notes to files without actually modify them.

Elisp snippets

Scan repository for annotation database

This snippet can be useful for annotate.el users (annotate is an Emacs package that permits to annotate a file without changing the contents).

The function annotate-find-file-scan-db, when called, will extract the directory where lies the file that current buffer is displaying, if any.

Then annotate-load-repo-db will checks if this directory contains a file named: annotation-db-location.

If such file exists Emacs will try to read a single line from that file and to interprets that line as a path (relative to the path of annotate-find-file-scan-db) to an annotation database (see annotation.el documentation), finally if such annotation database file exists will be loaded (if is in the correct format). If annotation-db-location does not exists in the directory containing the file visited by the current buffer the function will recurse going one directory up, and so on until:

  • a file with name annotation-db-location is found or
  • the root of the file system ("/") is reached.
  • WARNING

    This code will load (execute elisp source) the contents of the file found as annotate database. Load elisp files only from trusted sources as executing code from unkown sources could be a very severe security issue!

    Remember that as affirmed by the license there is no warranty.

    ;; © 2020 cage, released under GPLv3 or later.
    ;; Some portion of the code derived from annotate.el
    ;; Copyright (C) 2015 Bastian Bechtold and contributors:
    ;; Naoya Yamashita (2018)
    ;; Universita' degli Studi di Palermo (2019)
    ;; released under MIT license according to:
    ;; https://github.com/bastibe/annotate.el/blob/master/LICENSE
    ;;
    ;;                       WARNING
    ;;
    ;; This code  will /load/ (*execute*  elisp source) the  contents of
    ;; the file found as annotate database. Load elisp files *only* from
    ;; /trusted/ sources as executing code  from unkown sources could be
    ;; a very severe security issue!
    
    (defun annotate-read-file-line (file)
      (with-temp-buffer
        (insert-file-contents file)
        (let ((line (thing-at-point 'line)))
          (when line
            (let ((newline-exists-p (string-match "\n+$" line)))
              (if newline-exists-p
                  (replace-match "" t t line)
                line))))))
    
    (defun annotate-load-repo-db (file-in-repo &optional force-load)
      (let* ((file-db-link-name "annotation-db-location")
             (dir               (if (directory-name-p file-in-repo)
                                    (directory-file-name file-in-repo)
                                  (file-name-directory file-in-repo)))
             (parent-dir        (when dir
                                  (file-name-directory dir))))
        (when (and parent-dir
                   (not (string= parent-dir
                                 file-in-repo)))
          (let ((db-link-path (expand-file-name file-db-link-name parent-dir)))
            (if (file-exists-p db-link-path)
                (let ((relfile (annotate-read-file-line db-link-path)))
                  (annotate-switch-db force-load  (concat parent-dir relfile)))
              (annotate-load-repo-db parent-dir force-load))))))
    
    (defun annotate-find-file-scan-db (&optional force-load)
      (with-current-buffer (current-buffer)
        (when (and (boundp 'annotate-mode)
                   annotate-mode)
          (let ((file (buffer-file-name)))
            (when file
              (annotate-load-repo-db file force-load))))))
    
    (defun annotate-force-scan-load-annotation ()
      (annotate-find-file-scan-db t))
    
      ;; uncomment the following  form to load a new database  each time a
      ;; file       is       visited
      ;; (add-hook 'find-file-hook 'annotate-find-file-scan-db t)
    
    

Org-mode

  • 2019-12-07

    Prevent some files to be shown in generated sitemap when publishing project in html format.

    1. add this function, for example, to init.el

      (defun org-blog-custom-sitemap (title entries)
        (let* ((unwanted-entries (list "file:.*filename-to-exlude" ;; use regex
                                       "file:.*filename-to-exlude #2")) ;; and so on
               (filtered-entries (remove-if (lambda (a)
                                              (and (consp a)
                                                   (stringp (car a))
                                                   (find-if (lambda (entry)
                                                              (save-match-data
                                                                (string-match entry (car a))))
                                                            unwanted-entries)))
                                            entries)))
          (org-list-to-org filtered-entries)))
      
    2. Configure the project to use the function above

      (setq org-publish-project-alist '(("project name"
                                         ;; your configuration here
                                         :auto-sitemap t
                                         :sitemap-function org-blog-custom-sitemap)))
      

    this is a bit crude but to this date seems to works.


This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Italy License.

Distribuited software and source code published are licensed under the GNU General Public License version 3.0 or later if not specified otherwise.