From f8c0926408720faad68affe971a7e431aec99c2b Mon Sep 17 00:00:00 2001 From: Michael Olson Date: Thu, 27 Jul 2006 18:50:50 +0000 Subject: [PATCH] Make escaping of brackets in links work properly. * lisp/muse-colors.el (muse-colors-explicit-link): If the link has escaped characters, display them unescaped. * lisp/muse-mode.el (muse-make-link): Handle case where we are given no link. (muse-edit-link-at-point): Unescape the link before presenting it to the user for editing. * lisp/muse-publish.el (muse-publish-escape-specials-in-string): Minor coding style fix. (muse-publish-url): Unescape the link description before doing further transforms on it. The link has already been unescaped. * lisp/muse.el (muse-sort-by-rating): Minor coding style fix. (muse-escape-specials-in-string): New function that escapes specials in a string. This differs from Muse's other specials-escaping routines in that it replaces strings rather than characters, and is reversible if the specials are defined properly. (muse-replace-regexp-in-string): Docfix. (muse-link-specials): New variable containing the specials that Muse uses to handle syntactic issues with link text. Namely: brackets. The percent sign must also be escaped since it is used in the escaped text. (muse-link-escape, muse-link-unescape): Call muse-escape-specials-in-string. Embarrassingly, these functions previously had no effect before. git-archimport-id: mwolson@gnu.org--2006/muse--main--1.0--patch-164 --- ChangeLog | 38 ++++++++++++++++++++++++++++++++++++ lisp/muse-colors.el | 21 ++++++++++++++------ lisp/muse-mode.el | 6 +++--- lisp/muse-publish.el | 6 +++--- lisp/muse.el | 55 +++++++++++++++++++++++++++++++++++++++------------- 5 files changed, 101 insertions(+), 25 deletions(-) diff --git a/ChangeLog b/ChangeLog index 3479717..bede5ff 100644 --- a/ChangeLog +++ b/ChangeLog @@ -2,6 +2,44 @@ # arch-tag: automatic-ChangeLog--mwolson@gnu.org--2006/muse--main--1.0 # +2006-07-27 18:50:50 GMT Michael Olson patch-164 + + Summary: + Make escaping of brackets in links work properly. + Revision: + muse--main--1.0--patch-164 + + * lisp/muse-colors.el (muse-colors-explicit-link): If the link has + escaped characters, display them unescaped. + + * lisp/muse-mode.el (muse-make-link): Handle case where we are given no + link. + (muse-edit-link-at-point): Unescape the link before presenting it to + the user for editing. + + * lisp/muse-publish.el (muse-publish-escape-specials-in-string): Minor + coding style fix. + (muse-publish-url): Unescape the link description before doing further + transforms on it. The link has already been unescaped. + + * lisp/muse.el (muse-sort-by-rating): Minor coding style fix. + (muse-escape-specials-in-string): New function that escapes specials in + a string. This differs from Muse's other specials-escaping routines in + that it replaces strings rather than characters, and is reversible if + the specials are defined properly. + (muse-replace-regexp-in-string): Docfix. + (muse-link-specials): New variable containing the specials that Muse + uses to handle syntactic issues with link text. Namely: brackets. The + percent sign must also be escaped since it is used in the escaped text. + (muse-link-escape, muse-link-unescape): Call + muse-escape-specials-in-string. Embarrassingly, these functions + previously had no effect before. + + modified files: + ChangeLog lisp/muse-colors.el lisp/muse-mode.el + lisp/muse-publish.el lisp/muse.el + + 2006-07-09 18:28:25 GMT Michael Olson patch-163 Summary: diff --git a/lisp/muse-colors.el b/lisp/muse-colors.el index fddb8f3..3baf85a 100644 --- a/lisp/muse-colors.el +++ b/lisp/muse-colors.el @@ -763,10 +763,11 @@ in place of an image link defined by BEG and END." (save-excursion (goto-char (match-beginning 0)) (looking-at muse-explicit-link-regexp)) - (let* ((link (muse-get-link)) - (desc (muse-get-link-desc)) - (props (muse-link-properties - desc (muse-link-face link t))) + (let* ((unesc-link (muse-get-link)) + (unesc-desc (muse-get-link-desc)) + (link (muse-link-unescape unesc-link)) + (desc (muse-link-unescape unesc-desc)) + (props (muse-link-properties desc (muse-link-face link t))) (invis-props (append props (muse-link-properties desc)))) ;; see if we should try and inline an image (if (and muse-colors-inline-images @@ -783,11 +784,19 @@ in place of an image link defined by BEG and END." (add-text-properties (match-beginning 0) (match-beginning 2) invis-props) (add-text-properties (match-beginning 2) (match-end 2) props) - (add-text-properties (match-end 2) (match-end 0) invis-props)) + (add-text-properties (match-end 2) (match-end 0) invis-props) + ;; in case specials were escaped, cause the unescaped + ;; text to be displayed + (unless (string= desc unesc-desc) + (add-text-properties (match-beginning 2) (match-end 2) + (list 'display desc)))) (add-text-properties (match-beginning 0) (match-beginning 1) invis-props) (add-text-properties (match-beginning 1) (match-end 0) props) - (add-text-properties (match-end 1) (match-end 0) invis-props)) + (add-text-properties (match-end 1) (match-end 0) invis-props) + (unless (string= link unesc-link) + (add-text-properties (match-beginning 1) (match-end 1) + (list 'display link)))) (goto-char (match-end 0)) (add-text-properties (match-beginning 0) (match-end 0) diff --git a/lisp/muse-mode.el b/lisp/muse-mode.el index 583f554..e9ef3c6 100644 --- a/lisp/muse-mode.el +++ b/lisp/muse-mode.el @@ -472,7 +472,7 @@ Valid values of OPERATION are 'increase and 'decrease." (not (string= desc "")) (not (string= link desc))) (concat "[[" (muse-link-escape link) "][" (muse-link-escape desc) "]]") - (concat "[[" (muse-link-escape link) "]]"))) + (concat "[[" (or (muse-link-escape link) "") "]]"))) ;;;###autoload (defun muse-insert-relative-link-to-file () @@ -493,8 +493,8 @@ at the current point." Do not rename the page originally referred to." (interactive) (if (muse-link-at-point) - (let ((link (muse-get-link)) - (desc (muse-get-link-desc))) + (let ((link (muse-link-unescape (muse-get-link))) + (desc (muse-link-unescape (muse-get-link-desc)))) (replace-match (save-match-data (muse-make-link diff --git a/lisp/muse-publish.el b/lisp/muse-publish.el index ccea12b..d30f1a1 100644 --- a/lisp/muse-publish.el +++ b/lisp/muse-publish.el @@ -1255,9 +1255,8 @@ function for the list of available contexts." (apply (function concat) (mapcar (lambda (ch) - (let (repl) - (setq repl (or (assoc ch specials) - (assoc ch muse-publish-markup-specials))) + (let ((repl (or (assoc ch specials) + (assoc ch muse-publish-markup-specials)))) (if (null repl) (char-to-string ch) (cdr repl)))) @@ -1308,6 +1307,7 @@ the cadr is the page name, and the cddr is the anchor." (setq desc (save-match-data (when desc (funcall transform desc explicit))))) (when desc + (setq desc (muse-link-unescape desc)) (setq desc (muse-publish-escape-specials-in-string desc 'url-desc))) (setq orig-url (muse-publish-escape-specials-in-string orig-url 'url-desc)) diff --git a/lisp/muse.el b/lisp/muse.el index 8bb3a6d..648c9dd 100644 --- a/lisp/muse.el +++ b/lisp/muse.el @@ -300,7 +300,7 @@ Default sorting is highest-first. If TEST if specified, use it to sort the list." (unless test (setq test '>)) - (mapcar #'cdr + (mapcar (function cdr) (muse-sort-with-closure rated-list (lambda (a b closure) @@ -311,6 +311,32 @@ If TEST if specified, use it to sort the list." (t nil)))) test))) +(defun muse-escape-specials-in-string (specials string &optional reverse) + "Apply the transformations in SPECIALS to STRING. + +The transforms should form a fully reversible and non-ambiguous +syntax when STRING is parsed from left to right. + +If REVERSE is specified, reverse an already-escaped string." + (let ((rules (mapcar (lambda (rule) + (cons (regexp-quote (if reverse + (cdr rule) + (car rule))) + (if reverse (car rule) (cdr rule)))) + specials))) + (with-temp-buffer + (insert string) + (goto-char (point-min)) + (save-match-data + (while (not (eobp)) + (unless (catch 'found + (dolist (rule rules) + (when (looking-at (car rule)) + (replace-match (cdr rule) t t) + (throw 'found t)))) + (forward-char)))) + (buffer-string)))) + ;; The following code was extracted from cl (defun muse-const-expr-p (x) @@ -384,6 +410,9 @@ omitted, a default message listing FORM itself is used." (defun muse-replace-regexp-in-string (regexp replacement text &optional fixedcase literal) "Replace REGEXP with REPLACEMENT in TEXT. + +Return a new string containing the replacements. + If fourth arg FIXEDCASE is non-nil, do not alter case of replacement text. If fifth arg LITERAL is non-nil, insert REPLACEMENT literally." (cond @@ -478,25 +507,25 @@ Use TARGET to get the string, if it is specified." Use TARGET to get the string, if it is specified." (muse-match-string-no-properties 2 target)) +(defvar muse-link-specials + '(("[" . "%5B") + ("]" . "%5D") + ("%" . "%%")) + "Syntax used for escaping and unescaping links. +This allows brackets to occur in extended links as long as you +use the standard Muse functions to create them.") + (defun muse-link-escape (text) "Escape characters in TEXT that conflict with the explicit link regexp." - (if text - (progn - (muse-replace-regexp-in-string "\\[" "%5B" text t t) - (muse-replace-regexp-in-string "\\]" "%5D" text t t) - text) - "")) + (when (stringp text) + (muse-escape-specials-in-string muse-link-specials text))) (defun muse-link-unescape (text) "Un-escape characters in TEXT that conflict with the explicit link regexp." - (if text - (progn - (muse-replace-regexp-in-string "%5B" "[" text t t) - (muse-replace-regexp-in-string "%5D" "]" text t t) - text) - "")) + (when (stringp text) + (muse-escape-specials-in-string muse-link-specials text t))) (defun muse-handle-url (&optional string) "If STRING or point has a URL, match and return it." -- 2.11.4.GIT