;;; glasses.el --- make cantReadThis readable
-;; Copyright (C) 1999, 2000, 2001, 2005, 2006 Free Software Foundation, Inc.
+;; Copyright (C) 1999-2015 Free Software Foundation, Inc.
;; Author: Milan Zamazal <pdm@zamazal.org>
;; Maintainer: Milan Zamazal <pdm@zamazal.org>
;; This file is part of GNU Emacs.
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
-;; the Free Software Foundation; either version 2, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
-;; along with GNU Emacs; see the file COPYING. If not, write to the
-;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
-(eval-when-compile
- (require 'cl))
-
-
;;; User variables
(defcustom glasses-original-separator "_"
- "*String to be displayed as `glasses-separator' in separator positions.
+ "String to be displayed as `glasses-separator' in separator positions.
For instance, if you set it to \"_\" and set `glasses-separator' to \"-\",
underscore separators are displayed as hyphens.
If `glasses-original-separator' is an empty string, no such display change is
:group 'glasses
:type 'boolean)
+(defcustom glasses-separate-parentheses-exceptions
+ '("^#[\t ]*define[\t ]*[A-Za-z0-9_-]* ?($")
+ "List of regexp that are exceptions for `glasses-separate-parentheses-p'.
+They are matched to the current line truncated to the point where the
+parenthesis expression starts."
+ :group 'glasses
+ :type '(repeat regexp))
+
+(defcustom glasses-separate-capital-groups t
+ "If non-nil, try to separate groups of capital letters.
+When the value is non-nil, HTMLSomething and IPv6 are displayed
+as HTML_Something and I_Pv6 respectively. Set the value to nil
+if you prefer to display them unchanged."
+ :group 'glasses
+ :type 'boolean
+ :version "24.1")
(defcustom glasses-uncapitalize-p nil
"If non-nil, downcase embedded capital letters in identifiers.
;;; Utility functions
+(defun glasses-parenthesis-exception-p (beg end)
+ "Tell if (BEG, END) is an exception to `glasses-separate-parentheses-p'.
+See `glasses-separate-parentheses-exceptions'."
+ (save-match-data
+ (let ((str (buffer-substring beg end)))
+ (catch 'match
+ (dolist (re glasses-separate-parentheses-exceptions)
+ (and (string-match re str) (throw 'match t)))))))
(defun glasses-set-overlay-properties ()
"Set properties of glasses overlays.
'glasses-init))
;; Face + separator
(goto-char beg)
- (while (re-search-forward "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]"
- end t)
+ (while (re-search-forward
+ (if glasses-separate-capital-groups
+ "[a-z]\\([A-Z]\\)\\|[A-Z]\\([A-Z]\\)[a-z]"
+ "[a-z]\\([A-Z]\\)")
+ end t)
(let* ((n (if (match-string 1) 1 2))
(o (glasses-make-overlay (match-beginning n) (match-end n))))
(goto-char (match-beginning n))
(when glasses-separate-parentheses-p
(goto-char beg)
(while (re-search-forward "[a-zA-Z]_*\\(\(\\)" end t)
- (glasses-make-overlay (match-beginning 1) (match-end 1)
- 'glasses-parenthesis)))))))
+ (unless (glasses-parenthesis-exception-p (point-at-bol) (match-end 1))
+ (glasses-make-overlay (match-beginning 1) (match-end 1)
+ 'glasses-parenthesis))))))))
(defun glasses-make-unreadable (beg end)
"Convert current buffer to unreadable identifiers and return nil.
This function modifies buffer contents, it removes all the separators,
recognized according to the current value of the variable `glasses-separator'."
- (when (and glasses-convert-on-write-p
- (not (string= glasses-separator "")))
+ (when glasses-convert-on-write-p
(let ((case-fold-search nil)
(separator (regexp-quote glasses-separator)))
(save-excursion
- (goto-char (point-min))
- (while (re-search-forward
- (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]"
- separator separator)
- nil t)
- (let ((n (if (match-string 1) 1 2)))
- (replace-match "" t nil nil n)
- (goto-char (match-end n))))
- (unless (string= glasses-separator glasses-original-separator)
+ (unless (string= glasses-separator "")
(goto-char (point-min))
- (while (re-search-forward (format "[a-zA-Z0-9]\\(%s+\\)[a-zA-Z0-9]"
- separator)
- nil t)
- (replace-match glasses-original-separator nil nil nil 1)
- (goto-char (match-beginning 1))))
+ (while (re-search-forward
+ (format "[a-z]\\(%s\\)[A-Z]\\|[A-Z]\\(%s\\)[A-Z][a-z]"
+ separator separator)
+ nil t)
+ (let ((n (if (match-string 1) 1 2)))
+ (replace-match "" t nil nil n)
+ (goto-char (match-end n))))
+ (unless (string= glasses-separator glasses-original-separator)
+ (goto-char (point-min))
+ (while (re-search-forward (format "[a-zA-Z0-9]\\(%s+\\)[a-zA-Z0-9]"
+ separator)
+ nil t)
+ (replace-match glasses-original-separator nil nil nil 1)
+ (goto-char (match-beginning 1)))))
(when glasses-separate-parentheses-p
(goto-char (point-min))
(while (re-search-forward "[a-zA-Z]_*\\( \\)\(" nil t)
- (replace-match "" t nil nil 1))))))
+ (unless (glasses-parenthesis-exception-p (point-at-bol) (1+ (match-end 1)))
+ (replace-match "" t nil nil 1)))))))
;; nil must be returned to allow use in write file hooks
nil)
-(defun glasses-change (beg end &optional old-len)
+(defun glasses-change (beg end &optional _old-len)
"After-change function updating glass overlays."
(let ((beg-line (save-excursion (goto-char beg) (line-beginning-position)))
(end-line (save-excursion (goto-char end) (line-end-position))))
;;;###autoload
(define-minor-mode glasses-mode
"Minor mode for making identifiers likeThis readable.
-When this mode is active, it tries to add virtual separators (like underscores)
-at places they belong to."
+With a prefix argument ARG, enable the mode if ARG is positive,
+and disable it otherwise. If called from Lisp, enable the mode
+if ARG is omitted or nil. When this mode is active, it tries to
+add virtual separators (like underscores) at places they belong to."
:group 'glasses :lighter " o^o"
(save-excursion
(save-restriction
(provide 'glasses)
-;; arch-tag: a3515167-c89e-484f-90a1-d85143e52b12
;;; glasses.el ends here