From mboxrd@z Thu Jan 1 00:00:00 1970 From: Paul Sexton Subject: Small patch to restrict syntactic context where [[links]] are active Date: Sat, 24 Jul 2010 23:39:16 +0000 (UTC) Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Return-path: Received: from [140.186.70.92] (port=36062 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1OcoJY-0002Gr-Mz for emacs-orgmode@gnu.org; Sat, 24 Jul 2010 19:39:33 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.69) (envelope-from ) id 1OcoJX-0006c3-8J for emacs-orgmode@gnu.org; Sat, 24 Jul 2010 19:39:32 -0400 Received: from lo.gmane.org ([80.91.229.12]:57802) by eggs.gnu.org with esmtp (Exim 4.69) (envelope-from ) id 1OcoJW-0006bu-Tn for emacs-orgmode@gnu.org; Sat, 24 Jul 2010 19:39:31 -0400 Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1OcoJS-0001xC-JJ for emacs-orgmode@gnu.org; Sun, 25 Jul 2010 01:39:27 +0200 Received: from ip-118-90-21-85.xdsl.xnet.co.nz ([118.90.21.85]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 25 Jul 2010 01:39:26 +0200 Received: from psexton by ip-118-90-21-85.xdsl.xnet.co.nz with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Sun, 25 Jul 2010 01:39:26 +0200 List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: emacs-orgmode@gnu.org Hi, I posted a bit of code here a while back which allows org [[links]] and <> to be fontified and active in any major mode. I have been using this with great success in elisp and common lisp source code files, where it actually works much more smoothly than "linkd mode" (see emacswiki) which I used previously. I have become interested in a very cool dialect of lisp called Clojure. Clojure uses square brackets [ ] for syntax a lot more than other lisps. This sometimes leads to eg argument lists of functions being fontified as org [[links]]. Fixing this requires a tiny change to 'org-activate-bracket-links' (2 lines), one helper function and one variable which the user can set per-buffer. With this, if I have the following code in my .emacs: (add-hook 'clojure-mode-hook (lambda () (setq org-bracket-link-context '(string comment)))) Then org [[links]] will only be recognised when they occur inside a string or comment (as defined by Clojure mode), rather than messing up function arglists. The patch follows (org.el): (defun org-activate-bracket-links (limit) "Run through the buffer and add overlays to bracketed links." - (if (re-search-forward org-bracket-link-regexp limit t) + (if (and (re-search-forward org-bracket-link-regexp limit t) + (org-bracket-link-context-ok)) (let* ((help (concat "LINK: " (org-match-string-no-properties 1))) ;; FIXME: above we should remove the escapes. The helper function and variable: (defvar org-bracket-link-context nil "Buffer-local Variable that defines the syntactic contexts where org-style bracketed links should be recognised as such, provided that they match the regular expression for bracketed links. Possible values: nil - Any context is acceptable (default). STRING - The point is within a 'string'. COMMENT - The point is within a comment. OTHER - The point is outside a string or comment. A list - One of the contexts in the list is satisfied. The list must only contain some combination of the symbols STRING, COMMENT, or OTHER. ") (make-variable-buffer-local 'org-bracket-link-context) (defun org-bracket-link-context-ok () "Helper function, called by ORG-ACTIVATE-BRACKET-LINKS. Returns true if the syntactic context at the point is an acceptable place to fontify an org bracketed link. This is decided by checking the syntactic context against the value of the variable ORG-BRACKET-LINK-CONTEXT." (or (null org-bracket-link-context) (let ((context (syntax-ppss-context (syntax-ppss)))) (cond ((eql context org-bracket-link-context) t) ((eql 'other org-bracket-link-context) (null context)) ((listp org-bracket-link-context) (find (or context 'other) org-bracket-link-context)))))) Below I have copied the code that activates org-style links in arbitrary buffers, in case anyone is interested and missed it. ;; Put all this in your .emacs: (defface orgl-target-face '((t (:foreground "cyan" :background "royalblue4" :weight normal))) ;; '((t (:weight bold :box (:line-width 1 :color "red")))) "The face used to emphasise org-mode <>.") (make-face 'orgl-target-face) (setq orgl-target-face 'orgl-target-face) (defvar *orgl-link-abbrevs* '((lisp-mode ("defun" . "(defun %s (") ("class" . "(defclass %s (") ("wwdoc" . "file:../TODO::%s"))) "Define link abbreviations for each major mode. The variable contains a list, each element of which has the form (MAJOR-MODE (ABBREV . EXPANSION) .....) ABBREV is a short string. Links of the form '[[ABBREV:TEXT]]' will be expanded into EXPANSION. See the documentation for org-link-abbrev-alist for more details.") (defun orgl-do-font-lock (add-or-remove) "Add or remove font-lock rules for org hyperlinks." (funcall add-or-remove nil '((org-activate-bracket-links (0 'org-link t)))) (funcall add-or-remove nil `((,org-target-regexp (0 'orgl-target-face t))))) (defun orgl-enable () "Enable fontification of org-style hyperlinks in the current buffer." (interactive) ;; The following variable has to be bound to a string, or following links ;; will not work. ;; There is probably a more elegant solution. (unless org-todo-line-tags-regexp (set (make-local-variable 'org-todo-line-tags-regexp) "DSFSFSFSF_UTTER_NONSENSE_TAG_SDSDFSFDF")) (orgl-do-font-lock 'font-lock-add-keywords) (font-lock-fontify-buffer) ;; Add special link abbreviations. (unless org-link-abbrev-alist-local (make-local-variable 'org-link-abbrev-alist-local)) (dolist (pair (cdr (assoc major-mode *orgl-link-abbrevs*))) (pushnew pair org-link-abbrev-alist-local))) (defun orgl-disable () "Disable fontification of org-style hyperlinks in the current buffer." (interactive) (remove-text-properties (point-min) (point-max) '(mouse-face t keymap t org-linked-text t invisible t intangible t org-no-flyspell t)) (orgl-do-font-lock 'font-lock-remove-keywords) (font-lock-fontify-buffer) ;; Remove special link abbreviations (dolist (pair (cdr (assoc major-mode *orgl-link-abbrevs*))) (setq org-link-abbrev-alist-local (delete pair org-link-abbrev-alist-local)))) ;; Example code in .emacs to activate this facility in Python mode: (add-hook 'python-mode-hook 'orgl-enable)