Tags: code

RSS - Atom - Subscribe via email

Planner support for action-lock.el

| emacs

action-lock.el is part
of HOWM. The following code enables Planner-style links
_anywhere_. Add it to your ~/.emacs and use M-x action-lock-mode
to turn action locking on.

(add-to-list 'action-lock-default-rules
             (list (with-planner emacs-wiki-url-or-name-regexp)
                   'planner-action-lock-follow-name-at-point))
(setq action-lock-rules action-lock-default-rules)
(defvar planner-action-lock-default-directory planner-directory "Expand files relative to this directory.")
(defun planner-action-lock-follow-name-at-point (&optional other-window)
  (let ((link-name (match-string 0))
        (base-buffer (current-buffer))
        ;; the name of the buffer that contains the link.  check
        ;; whether buffer-name is a WikiName, else make it one
        (parent-name (or (emacs-wiki-page-name)
                         (concat "[[" (buffer-name) "]]")))
        (refresh-buffer nil))
    (with-planner
      (let ((link (emacs-wiki-wiki-link-target link-name)))
        (if (emacs-wiki-wiki-url-p link)
            (emacs-wiki-browse-url link other-window)
          ;; The name list is current since the last time the buffer was
          ;; highlighted
          (let* ((base (emacs-wiki-wiki-base link-name))
                 (file (emacs-wiki-page-file base))
                 (tag  (and (not (emacs-wiki-wiki-url-p link))
                            (emacs-wiki-wiki-tag link)))
                 (find-file-function (if other-window
                                         'find-file-other-window
                                       'find-file))
                 (parent-allowed-backlink-p
                  (not (string-match
                        emacs-wiki-exclude-backlink-parent-regexp
                        parent-name)))
                 (parent-backlink (and parent-allowed-backlink-p
				   (emacs-wiki-get-current-backlink)))
                 (newbuf
                  (funcall find-file-function
                           (or file
                               (expand-file-name
                                       base
                                       (or planner-action-lock-default-directory
                                           (and (buffer-file-name)
                                                (file-name-directory
                                                 (buffer-file-name)))
                                           default-directory)))))
                 (emacs-wiki-create-backlinks
                  (and emacs-wiki-create-backlinks parent-allowed-backlink-p))
                 (make-link-p (and emacs-wiki-create-backlinks
                                   ;; insert backlink only in new files
                                   (null file)
                                   (not (string-match
                                         emacs-wiki-exclude-backlink-regexp
                                         (emacs-wiki-page-name
                                          (buffer-file-name newbuf)))))))
            (when tag
              (goto-char (point-min))
              (re-search-forward (concat "^\\.?#" tag) nil t))
            (when refresh-buffer
              (when make-link-p
                (emacs-wiki-insert-backlink parent-name parent-backlink))
              ;; todo: is with-current-buffer necessary here?
              (with-current-buffer newbuf
                (add-hook 'after-save-hook
                          'emacs-wiki-refresh-buffers-once t t)
                ;; save the new file to avoid another backlink if the
                ;; buffer is visited again before being saved
                (when (or make-link-p
                          (and (null file)
                               (not parent-allowed-backlink-p)))
                  ;; set modified status to t: pages with excluded
                  ;; parents may not be modified, but we want to save
                  ;; anyway
                  (set-buffer-modified-p t)
                  (save-buffer))))))))))

Emacs snippet for pmwiki attachments

| emacs
(defun sacha/dired-marked-images-as-attach ()
  "Return a list of images ready to be inserted into a pmwiki page."
  (interactive)
  (kill-new (mapconcat
             (lambda (item)
               (concat "Attach:" (file-name-nondirectory item)))
             (dired-get-marked-files)
             "\n")))

Automatically scheduling tasks onto TaskPool

| emacs, planner
(defadvice planner-create-task-from-buffer (before paul activate)
  "Automatically schedule task onto TaskPool as well as other projects."
  (if plan-page
      (unless (string-match plan-page "TaskPool")
        (setq plan-page (concat plan-page planner-multi-separator "TaskPool")))
    (setq plan-page "TaskPool")))

Here’s another snippet that will unschedule tasks from TaskPool when
you mark them completed with C-c C-x (planner-task-done). Nifty stuff,
huh?

(defadvice planner-task-done (after paul activate)
  "Remove completed tasks from the TaskPool if that still leaves them linked."
  (let ((info (planner-current-task-info)))
    (when (planner-task-link-text info)
      ;; If it is linked to TaskPool _and_ at least one other thing
      (if (string-match planner-multi-separator (planner-task-link-text info))
          (planner-multi-replan-task
           (planner-multi-make-link (planner-multi-filter-links "^TaskPool$" (planner-multi-task-link-as-list info) t)))
        ;; Else if it has a date and is linked to TaskPool
        (if (and (planner-task-date info)
                 (string= (planner-task-link info) "TaskPool"))
            (planner-replan-task nil))))))

Adding an arbitrary extension to your planner files

| emacs, planner

Do you want all of your planner files to have a TXT extension so that
they’ll be searched by tools such as Google Desktop or Apple Spotlight?

Use the ‘rename’ tool to add .txt extensions to all of your files, or
use the following code to rename all of your planner files from a
running Planner setup:

(defun my-rename-planner-files ()
  "Rename all my planner files to .txt if they don't have that extension yet."
  (interactive)
  (mapcar
   (lambda (file)
     (unless (string-match "\\.txt$" (cdr file))
       (rename-file (cdr file) (concat (cdr file) ".txt"))
       (message "%s" (cdr file))))
   (planner-file-alist))
  (with-planner
    (emacs-wiki-refresh-file-alist)))

Then add this to your ~/.emacs before you load Planner:

(setq emacs-wiki-ignored-extensions-regexp "\\.txt")

(defadvice emacs-wiki-find-file (around extension activate)
  "Open the Emacs Wiki page WIKI by name.
If COMMAND is non-nil, it is the function used to visit the file.
If DIRECTORY is non-nil, it is the directory in which the Wiki
page will be created if it does not already exist."
  (unless (interactive-p)
    (setq wiki (cons wiki
                     (cdr (assoc wiki (emacs-wiki-file-alist))))))
  ;; At this point, `wiki' is (GIVEN-PAGE FOUND-FILE).
  (if (cdr wiki)
      (let ((buffer (funcall (or command 'find-file) (cdr wiki))))
        (if (= (prefix-numeric-value current-prefix-arg) 16)
            (with-current-buffer buffer
              (set (make-variable-buffer-local 'emacs-wiki-directories)
                   (cons (file-name-directory (cdr wiki))
                         emacs-wiki-directories))
              (set (make-variable-buffer-local 'emacs-wiki-file-alist)
                   nil)))
        buffer)
    (let* ((dirname (or directory
                        (emacs-wiki-maybe t)
                        (car emacs-wiki-directories)))
           (filename (expand-file-name (car wiki) dirname)))
      (unless (file-exists-p dirname)
        (make-directory dirname t))
      (funcall (or command 'find-file) (concat filename ".txt")))))

sacha/planner-add-recent

| emacs
(defun sacha/planner-add-recent ()
  "Add the current note to the list of recent entries, trimmed at 10."
  (interactive)
  (let* ((info (planner-current-note-info))
         (url (concat
               "http://sacha.free.net.ph/notebook/wiki/"
               (emacs-wiki-published-name (planner-note-page info))
               "#" (planner-note-anchor info))))
    (when info
      (with-current-buffer
          (find-file-noselect "/home/sacha/notebook/wiki/include/footer.inc.php")
        (goto-char (point-min))
        (when (re-search-forward "--recent--" nil t)
          (save-restriction
            (narrow-to-region (1+ (line-end-position))
                              (progn
                                (re-search-forward "--end of recent--" nil t)
                                (line-beginning-position)))
            (goto-char (point-min))
            (insert ""
                    (planner-note-title info)
                    "
\n") (forward-line 10) (delete-region (point) (point-max)) (save-buffer)))))))

../emacs/planner-config.el

sacha/emacs-wiki-markup-string

| emacs
(defun sacha/emacs-wiki-markup-string (string)
  "Mark up STRING according to `emacs-wiki-publishing-rules'.
No header or footer is added."
  (let ((emacs-wiki-project emacs-wiki-current-project))
    (with-temp-buffer
      (emacs-wiki-mode)
      (insert string)
      (let ((emacs-wiki-publishing-header "")
            (emacs-wiki-publishing-footer ""))
        (emacs-wiki-replace-markup))
      (buffer-string))))

../emacs/emacs-wiki-config.el