Created
January 27, 2014 13:11
-
-
Save kenoss/8648351 to your computer and use it in GitHub Desktop.
A small dirty trick to use more flexible format for `org-export-latex-hyperref-format'.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defsubst my:advice-enabled-p (func class name) | |
"[internal] Return t (resp. nil) if advice NAME of FUNC is enabled (resp. disabled)." | |
(not (not (ad-advice-enabled (ad-find-advice func class name))))) | |
(defmacro my:with-advice (enable-or-disable func class name &rest body) | |
"[internal] Evaluate BODY with NAME enabled/disabled." | |
(declare (indent 4)) | |
(let ((enabled | |
(pcase enable-or-disable | |
(`'enable t) | |
(`'disable nil) | |
(_ (error "the first argument must be the symol 'enable or 'disable"))))) | |
`(if (eq ,enabled (my:advice-enabled-p ,func ,class ,name)) | |
(progn ,@body) | |
(prog2 ; return value is that of BODY | |
(progn | |
(,(if enabled 'ad-enable-advice 'ad-disable-advice) ,func ,class ,name) | |
(ad-activate ,func)) | |
(progn ,@body) | |
(progn | |
(,(if (not enabled) 'ad-enable-advice 'ad-disable-advice) ,func ,class ,name) | |
(ad-activate ,func)))))) | |
(defadvice org-export-latex-links (around extended-hyperref-format activate) | |
"To use unrestricted format for `org-export-latex-hyperref-format'" | |
(if (stringp org-export-latex-hyperref-format) | |
ad-do-it | |
(my:with-advice 'enable 'format 'around 'enable-non-strings | |
ad-do-it))) | |
(defadvice format (around enable-non-strings disable) | |
"If the first argument is not string, apply it to the rest arguments." | |
(let ((f (ad-get-arg 0))) | |
(if (stringp f) | |
ad-do-it | |
(setq ad-return-value | |
(my:with-advice 'disable 'format 'around 'enable-non-strings | |
(apply f (ad-get-args 1))))))) | |
;; example | |
(my:with-advice 'enable 'format 'around 'enable-non-strings | |
(format (lambda (x) (format "%s %s" x x)) 'hoge)) | |
; => "hoge hoge" | |
(setq org-export-latex-hyperref-format | |
(lambda (x y) | |
(let ((str (replace-regexp-in-string "-" "." | |
(replace-regexp-in-string "^.+?-" "" x)))) | |
(format "\\hyperref[%s]{%s}" str str)))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment