From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexis Subject: Creating a new contacts style for org-vcard [was: Re: ANN: org-vcard. ...] Date: Thu, 07 Aug 2014 01:36:59 +1000 Message-ID: <87oavxocis.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from eggs.gnu.org ([2001:4830:134:3::10]:46368) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XF3H8-0006NW-Or for emacs-orgmode@gnu.org; Wed, 06 Aug 2014 11:37:23 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1XF3Gz-0001NK-M5 for emacs-orgmode@gnu.org; Wed, 06 Aug 2014 11:37:14 -0400 Received: from mail-pd0-x22a.google.com ([2607:f8b0:400e:c02::22a]:38888) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1XF3Gz-0001ND-AA for emacs-orgmode@gnu.org; Wed, 06 Aug 2014 11:37:05 -0400 Received: by mail-pd0-f170.google.com with SMTP id g10so3489820pdj.29 for ; Wed, 06 Aug 2014 08:37:04 -0700 (PDT) Received: from localhost (ppp118-209-47-36.lns20.mel4.internode.on.net. [118.209.47.36]) by mx.google.com with ESMTPSA id dr9sm2233346pdb.78.2014.08.06.08.37.02 for (version=TLSv1.2 cipher=RC4-SHA bits=128/128); Wed, 06 Aug 2014 08:37:03 -0700 (PDT) In-Reply-To: 87a97lfbdk.fsf@news.tumashu-localhost.org List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode Feng Shu wrote: > Is this "tree" style possible? > #+begin-comment > * People > ** Joan Smith > :PROPERTIES: > :KIND: individual > :FIELDTYPE: name > :END: > *** Cell > :PROPERTIES: > :FIELDTYPE: cells-folder > :END: > **** 0000 999 991 > **** 0000 999 992 > **** 0000 999 993 > **** 0000 999 994 > **** 0000 999 995 > *** Email > :PROPERTIES: > :FIELDTYPE: emails-folder > :END: > **** address@hidden > **** address@hidden > **** address@hidden > **** address@hidden > **** address@hidden > #+end-comment Here's what i've come up with. :-) (a) Please make sure you have the latest version of org-vcard installed! (b) For clarity, i've made use of the `s-repeat` function from the `s` library; if you don't already have it installed, you can install it from MELPA or Marmalade. (c) Assuming you've installed org-vcard from MELPA, copy the 'tree' folder from the elpa/org-vcard-[date]/styles/ folder to the ~/.emacs.d/org-vcard-styles/ folder; the latter should already have been created for you automatically. Then rename: ~/.emacs.d/org-vcard-styles/tree to ~/.emacs.d/org-vcard-styles/fengshu (d) Open the file: ~/.emacs.d/org-vcard-styles/fengshu/functions.el Delete its entire contents, and replace it with the text between the BEGIN and END markers: --- BEGIN --- (defun org-vcard-export-from-fengshu (source destination) "Export fengshu-style SOURCE to vCard format, sending output to DESTINATION. SOURCE must be \"buffer\", \"region\" or \"subtree\". DESTINATION must be either \"buffer\" or \"file\"." (let* ((in-contact-entry nil) (fengshu-style-properties (or (cadr (assoc org-vcard-active-version (cadr (assoc org-vcard-active-language (cadr (assoc "fengshu" org-vcard-styles-languages-mappings)))))) (error "No mapping available for specified vCard version"))) (encoding (cond ((string= "4.0" org-vcard-active-version) 'utf-8) ((string= "3.0" org-vcard-active-version) 'utf-8) ((string= "2.1" org-vcard-active-version) 'us-ascii))) (output (encode-coding-string "" encoding))) (if (not (member source '("buffer" "region" "subtree"))) (error "Invalid source type")) (save-excursion (let ((search-result nil)) (cond ((string= "region" source) (narrow-to-region (region-beginning) (region-end))) ((string= "subtree" source) (org-narrow-to-subtree))) (goto-char (point-min)) (setq case-fold-search t) (while (re-search-forward "\\s *:FIELDTYPE:\\s *name" nil t) (let ((content (concat (org-vcard-export-line "BEGIN:VCARD" "" t) (org-vcard-export-line "VERSION" org-vcard-active-version))) (end-vcard nil)) (setq content (concat content (org-vcard-export-line "FN" (org-get-heading t t)))) (if (or (string= "3.0" org-vcard-active-version) (string= "2.1" org-vcard-active-version)) (setq content (concat content (org-vcard-export-line "N" "")))) (while (and (setq search-result (re-search-forward "\\s *:FIELDTYPE:\\s *\\(\\(?:\\w\\|-\\)+\\)" nil t)) (not end-vcard)) (let ((fieldtype (match-string 1))) (if (not (string= "name" (downcase fieldtype))) ;; BEGIN main changes (if (or (string= "cell" (downcase fieldtype)) (string= "email" (downcase fieldtype))) (let ((done nil) (heading-levels (let ((l nil) (n (1+ (org-current-level)))) (while (> n 0) (setq l (append l `(,(s-repeat n "\\*")))) (setq n (1- n))) l))) (while (and (not done) (re-search-forward (concat (nth 0 heading-levels) " +\\([^\n]+\\)\n") nil t)) (setq content (concat content (org-vcard-export-line (cdr (assoc (downcase fieldtype) fengshu-style-properties)) (match-string 1)))) (dolist (level (cdr heading-levels)) (if (or (looking-at (concat level " +")) (looking-at "^ +$") (looking-at "^$")) (setq done t))))) ;; END main changes (setq content (concat content (org-vcard-export-line (cdr (assoc (downcase fieldtype) fengshu-style-properties)) (org-get-heading t t))))) (setq end-vcard t)))) (setq content (concat content (org-vcard-export-line "END:VCARD" "" t))) (setq output (concat output content))) (if search-result (re-search-backward "\\s *:FIELDTYPE:\\s *name"))) (org-vcard-write-to-destination output destination) (cond ((string= "buffer" destination) (message "Exported contacts data to *org-vcard-export* buffer.")) ((string= "file" destination) (message "Exported contacts data to file."))))))) (defun org-vcard-import-to-fengshu (source destination) ;; ) --- END --- Save the file. The org-vcard-import-to-fengshu function is deliberately left as a stub; i'm just wanting to give an overall idea of how one can create new org-vcard style functionality. Most of the definition of org-vcard-export-from-fengshu comes from org-vcard-export-from-tree - i've marked the section where the former differs substantially from the latter. (e) Run the command `org-vcard-reload-styles`. (f) Open your example content in a buffer. You'll need to change the FIELDTYPEs from "cells-folder" and "emails-folder" to "cell" and "email", respectively, due to how org-vcard's property-mapping system works. You'll also need to make sure that there's a newline after the fifth instance of "address@hidden"; my code doesn't address the case where there's no such final newline. (g) Either run the `org-vcard-export` command with source "buffer", destination "buffer", style "fengshu", language "en" and version "4.0"; or enable org-vcard-mode and select the corresponding items from the Org-vCard menu. (h) Hopefully, the result should now be available in the "*org-vcard-export*" buffer. :-) Please let me know if you have any questions! Alexis.