- (let ((p (point)) case-fold-search)
- (when (and (> (- p (point-min)) 2)
- (eq (preceding-char) ?\s)
- (eq (char-syntax (char-before (1- p))) ?w)
- (not (string-equal " " tildify-space-string))
- (save-excursion
- (goto-char (1- p))
- (looking-back tildify-space-pattern
- (max (point-min) (- p 10))))
- (run-hook-with-args-until-failure 'tildify-space-predicates))
- (delete-char -1)
- (insert tildify-space-string)
- t)))
+ (let* ((p (point)) (p-1 (1- p)) (n (- p (point-min)))
+ (l (length tildify-space-string)) (l+1 (1+ l))
+ case-fold-search)
+ (when (and (> n 2) (eq (preceding-char) ?\s))
+ (cond
+ ((and (eq (char-syntax (char-before p-1)) ?w)
+ (save-excursion
+ (goto-char p-1)
+ (looking-back tildify-space-pattern (max (point-min) (- p 10))))
+ (run-hook-with-args-until-failure 'tildify-space-predicates))
+ (delete-char -1)
+ (insert tildify-space-string)
+ t)
+ ((and tildify-double-space-undos
+ (> n l+1)
+ (string-equal tildify-space-string
+ (buffer-substring (- p l+1) p-1)))
+ (goto-char p-1)
+ (delete-char (- l))
+ (goto-char (1+ (point)))
+ nil)))))