view lisp/progmodes/glasses.el @ 99501:e3acb52d33e1

2008-11-12 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-yank): Make any prefix force normal yanking. Suppress folding if text would be swallowed into a folded subtree. (org-yank-folded-subtrees, org-yank): Docstring updates. * org-agenda.el (org-agenda-compare-effort): Treat no effort defined as 0. * org-exp.el (org-export-language-setup): Add Catalan and Esperanto language entries. 2008-11-12 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-refile): Allow refiling of entire regions. * org-clock.el (org-clock-time%): New function. * org.el (org-entry-get, org-entry-delete): Use safer regexps to retrieve property values. 2008-11-12 Carsten Dominik <carsten.dominik@gmail.com> * org-agenda.el (org-agenda-list): Handle the value `only' of org-agenda-show-log'. (org-agenda-log-mode): Interpret a double prefix arg. 2008-11-12 Carsten Dominik <carsten.dominik@gmail.com> * org-exp.el (org-export-html-footnotes-section): New variable. (org-export-as-html): Use `org-export-html-footnotes-section' to insert the footnotes. (org-export-language-setup): Add "Footnotes" to language words. 2008-11-12 Carsten Dominik <carsten.dominik@gmail.com> * org.el (org-yank): Fix bug when not inserting a subtree. 2008-11-12 Carsten Dominik <carsten.dominik@gmail.com> * org-vm.el (org-vm-follow-link): Call `vm-preview-current-message' instead of `vm-beginning-of-message'. * org.el (org-make-link-regexps): Make sure that links to gnus can contain brackets. 2008-11-12 Carsten Dominik <carsten.dominik@gmail.com> * org-attach.el (org-attach-dir): Remove duplicate ID creation code. * org-id.el (org-id-new): Use `org-trim' to extract the uuid from shell output. * org.el (org-link-abbrev-alist): Improve customization type. * org-attach.el (org-attach-expand-link, org-attach-expand): New functions. * org-agenda.el (org-agenda-get-progress): Renamed from `org-get-closed'. Implement searching for state changes as well. (org-agenda-log-mode-items): New option. (org-agenda-log-mode): New option prefix argument, interpreted as request to show all possible progress info. (org-agenda-get-day-entries): Call `org-get-progress' instead of `org-get-closed'. (org-agenda-set-mode-name): Handle the more complex log mode settings. (org-get-closed): New alias, pointing to `org-get-progress'. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org.el (org-file-apps-defaults-gnu) (org-file-apps-defaults-macosx) (org-file-apps-defaults-windowsnt): Add an entry defining the system command. (org-file-apps): Allow `system' as key and value. (org-open-at-point): Explain the effect of a double prefix arg. (org-open-file): If the argument `in-emacs' is (16), i.e. corresponding to a double prefix argument, try to open the file externally. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org.el (org-insert-link): Abbreviate absolute files names in links. Also, fix a bug in which the double C-u prefix would not be honored. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org.el (org-insert-heading): If buffer does not end with a newline, add one if necessary to insert headline correctly. * org-exp.el (org-export-as-html): Make sure that <hr/> is between paragraphs, not inside. * org.el (org-todo): Quote `org-agenda-headline-snapshot-before-repeat'. * org-exp.el (org-export-as-html): Fully process link descriptions. (org-export-html-format-desc): New function. (org-export-as-html): Collect footnotes into the correct basket. (org-html-protect): No longer protect quotations marks here, this goes wrong. * org-agenda.el (org-agenda-remove-marked-text): Bind variable BEG. * org-compat.el (org-fit-window-to-buffer): New function (not really, a preliminary and incomplete version was present earlier, but not used). * org.el (org-fast-todo-selection, org-fast-tag-selection): Use `org-fit-window-to-buffer'. * org-exp.el (org-export): Use `org-fit-window-to-buffer'. * org-agenda.el (org-agenda-get-restriction-and-command) (org-fit-agenda-window, org-agenda-convert-date): Use `org-fit-window-to-buffer'. * org-exp.el (org-export-as-html): Process href links through `org-export-html-format-href'. (org-export-html-format-href): New function. * org-agenda.el (org-agenda-todo): Update only the current headline if this is a repeated TODO, marked done for today. (org-agenda-change-all-lines): New argument JUST-THIS, to change only the current line. * org.el (org-todo): Take a snapshot of the headline if the repeater might change it. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org-publish.el (org-publish-find-title): Remove buffers visited only for extracting the title. * org-exp.el (org-export-html-style) (org-export-html-style-default): Mark style definitions as unparsed CDATA. * org-publish.el (org-publish-validate-link): Function re-introduced. 2008-11-12 Charles Sebold <csebold@gmail.com> * org-plot.el (org-plot/add-options-to-plist): Supports timefmt property. (org-plot-quote-timestamp-field): New function. (org-plot-quote-tsv-field): Call timestamp field function when necessary rather than just quoting as a string. (org-plot/gnuplot-to-data): Pass in timefmt property. (org-plot/gnuplot-script): Supports timefmt property. (org-plot/gnuplot): Checks for timestamp column before checking for text index column. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org.el (org-insert-heading): Improve behavior with hidden subtrees. * org-publish.el (org-publish-org-index): Create a section in the index file. (org-publish-org-index): Stop linking to directories. * org.el (org-emphasis-alist): Use span instead of <u> to underline text. * org-exp.el (org-export-as-html): Make sure <p> is closed before <pre> sections. 2008-11-12 Sebastian Rose <sebastian_rose@gmx.de> * org-jsinfo.el (org-infojs-template): Remove language attribute from script tag. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org-agenda.el (org-agenda-remove-marked-text): New function. (org-agenda-mark-filtered-text) (org-agenda-unmark-filtered-text): New functions. (org-write-agenda): Remove fltered text. * org.el (org-make-tags-matcher): Give access to TODO "property" without speed penalty. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org.el (org-link-frame-setup): Add `org-gnus-no-new-news' as an option. (org-store-link-props): Make sure adding to the plist works correctly. * org-gnus.el (org-gnus-no-new-news): New function. (org-gnus-follow-link): Allow the article ID to be a message-id, in addition to allowing article numbers. Message IDs make much more roubust links. (org-gnus-store-link): Use message-id to create link. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org.el (org-emphasize): Reverse the selection array. (org-emphasis-alist): Set <code> tags for the verbatim environment. * org-remember.el (org-remember-handler): Fix bug with prefix-related changing of the note storage target. * org-exp.el (org-print-icalendar-entries): Make the exported priorities compatible with RFC 2445. * org-clock.el (org-clock-save): Insert time stamp without dependence on time-stamp.el. 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org.el ("saveplace"): If saveplace puts point into an invisible location, make it visible. (org-make-tags-matcher): Allow inactive time stamps in time comparisons. (org-yank-adjusted-subtrees): New option. (org-yank): Incorporate adjusting trees. (org-paste-subtree): New argument FOR-YANK which will cause insertion at point without backing up over white lines, and leave point at the end of the inserted text. Also if the cursor is at the beginning of a headline, use the same level or the inserted tree. * org-publish.el (org-publish-get-base-files-1): Deal correctly with broken symlinks 2008-11-12 Carsten Dominik <dominik@science.uva.nl> * org-exp.el (org-export-select-tags, org-get-current-options): Fix typo.
author Carsten Dominik <dominik@science.uva.nl>
date Wed, 12 Nov 2008 08:01:06 +0000
parents 52b7a8c22af5
children a9dc0e7c3f2b
line wrap: on
line source

;;; glasses.el --- make cantReadThis readable

;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008
;;   Free Software Foundation, Inc.

;; Author: Milan Zamazal <pdm@zamazal.org>
;; Maintainer: Milan Zamazal <pdm@zamazal.org>
;; Keywords: tools

;; This file is part of GNU Emacs.

;; 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 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
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; This file defines a minor mode for making unreadableIdentifiersLikeThis
;; readable.  In some environments, for instance Java, it is common to use such
;; unreadable identifiers.  It is not good to use underscores in identifiers of
;; your own project in such an environment to make your sources more readable,
;; since it introduces undesirable confusion, which is worse than the
;; unreadability.  Fortunately, you use Emacs for the subproject, so the
;; problem can be solved some way.
;;
;; This file defines the `glasses-mode' minor mode, which displays underscores
;; between all the pairs of lower and upper English letters.  (This only
;; displays underscores, the text is not changed actually.)  Alternatively, you
;; can say you want the capitals in some given face (e.g. bold).
;;
;; The mode does something usable, though not perfect.  Improvement suggestions
;; from Emacs experts are welcome.
;;
;; If you like in-identifier separators different from underscores, change the
;; value of the variable `glasses-separator' appropriately.  See also the
;; variables `glasses-face' and `glasses-convert-on-write-p'.  You can also use
;; the command `M-x customize-group RET glasses RET'.
;;
;; If you set any of the variables `glasses-separator' or `glasses-face' after
;; glasses.el is loaded in a different way than through customize, you
;; should call the function `glasses-set-overlay-properties' afterwards.

;;; Code:


(eval-when-compile
  (require 'cl))


;;; User variables


(defgroup glasses nil
  "Make unreadable code likeThis(one) readable."
  :version "21.1"
  :group 'tools)


(defcustom glasses-separator "_"
  "String to be displayed as a visual separator in identifiers.
It is used both for adding missing separators and for replacing separators
defined by `glasses-original-separator'.  If you don't want to add missing
separators, set `glasses-separator' to an empty string.  If you don't want to
replace existent separators, set `glasses-original-separator' to an empty
string."
  :group 'glasses
  :type 'string
  :set 'glasses-custom-set
  :initialize 'custom-initialize-default)


(defcustom glasses-original-separator "_"
  "*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
performed."
  :group 'glasses
  :type 'string
  :set 'glasses-custom-set
  :initialize 'custom-initialize-default
  :version "22.1")


(defcustom glasses-face nil
  "Face to be put on capitals of an identifier looked through glasses.
If it is nil, no face is placed at the capitalized letter.

For example, you can set `glasses-separator' to an empty string and
`glasses-face' to `bold'.  Then unreadable identifiers will have no separators,
but will have their capitals in bold."
  :group 'glasses
  :type '(choice (const :tag "None" nil) face)
  :set 'glasses-custom-set
  :initialize 'custom-initialize-default)


(defcustom glasses-separate-parentheses-p t
  "If non-nil, ensure space between an identifier and an opening parenthesis."
  :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-uncapitalize-p nil
  "If non-nil, downcase embedded capital letters in identifiers.
Only identifiers starting with lower case letters are affected, letters inside
other identifiers are unchanged."
  :group 'glasses
  :type 'boolean
  :set 'glasses-custom-set
  :initialize 'custom-initialize-default)


(defcustom glasses-uncapitalize-regexp "[a-z]"
  "Regexp matching beginnings of words to be uncapitalized.
Only words starting with this regexp are uncapitalized.
The regexp is case sensitive.
It has any effect only when `glasses-uncapitalize-p' is non-nil."
  :group 'glasses
  :type 'regexp
  :set 'glasses-custom-set
  :initialize 'custom-initialize-default)


(defcustom glasses-convert-on-write-p nil
  "If non-nil, remove separators when writing glasses buffer to a file.
If you are confused by glasses so much, that you write the separators into code
during coding, set this variable to t.  The separators will be removed on each
file write then.

Note the removal action does not try to be much clever, so it can remove real
separators too."
  :group 'glasses
  :type 'boolean)


(defun glasses-custom-set (symbol value)
  "Set value of the variable SYMBOL to VALUE and update overlay categories.
Used in :set parameter of some customized glasses variables."
  (set-default symbol value)
  (glasses-set-overlay-properties))


;;; 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.
Consider current setting of user variables."
  ;; In-identifier overlay
  (put 'glasses 'evaporate t)
  (put 'glasses 'before-string glasses-separator)
  (put 'glasses 'face glasses-face)
  ;; Beg-identifier overlay
  (put 'glasses-init 'evaporate t)
  (put 'glasses-init 'face glasses-face)
  ;; Parenthesis overlay
  (put 'glasses-parenthesis 'evaporate t)
  (put 'glasses-parenthesis 'before-string " "))

(glasses-set-overlay-properties)


(defun glasses-overlay-p (overlay)
  "Return whether OVERLAY is an overlay of glasses mode."
  (memq (overlay-get overlay 'category)
	'(glasses glasses-init glasses-parenthesis)))


(defun glasses-make-overlay (beg end &optional category)
  "Create and return readability overlay over the region from BEG to END.
CATEGORY is the overlay category.  If it is nil, use the `glasses' category."
  (let ((overlay (make-overlay beg end)))
    (overlay-put overlay 'category (or category 'glasses))
    overlay))


(defun glasses-make-readable (beg end)
  "Make identifiers in the region from BEG to END readable."
  (let ((case-fold-search nil))
    (save-excursion
      (save-match-data
	;; Face only
	(goto-char beg)
	(while (re-search-forward
		"\\<\\([A-Z]\\)[a-zA-Z]*\\([a-z][A-Z]\\|[A-Z][a-z]\\)"
		end t)
	  (glasses-make-overlay (match-beginning 1) (match-end 1)
				'glasses-init))
	;; Face + separator
	(goto-char beg)
	(while (re-search-forward "[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 (and glasses-uncapitalize-p
		       (save-match-data
			 (looking-at "[A-Z]\\($\\|[^A-Z]\\)"))
		       (save-excursion
			 (save-match-data
			   (re-search-backward "\\<.")
			   (looking-at glasses-uncapitalize-regexp))))
	      (overlay-put o 'invisible t)
	      (overlay-put o 'after-string (downcase (match-string n))))))
        ;; Separator change
	(when (and (not (string= glasses-original-separator glasses-separator))
		   (not (string= glasses-original-separator "")))
          (goto-char beg)
	  (let ((original-regexp (regexp-quote glasses-original-separator)))
	    (while (re-search-forward
		    (format "[a-zA-Z0-9]\\(\\(%s\\)+\\)[a-zA-Z0-9]"
			    original-regexp)
		    end t)
	      (goto-char (match-beginning 1))
	      (while (looking-at original-regexp)
		(let ((o (glasses-make-overlay (point) (1+ (point)))))
		  ;; `concat' ensures the character properties won't merge
		  (overlay-put o 'display (concat glasses-separator)))
		(goto-char (match-end 0))))))
	;; Parentheses
	(when glasses-separate-parentheses-p
	  (goto-char beg)
	  (while (re-search-forward "[a-zA-Z]_*\\(\(\\)" end t)
	    (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)
  "Return identifiers in the region from BEG to END to their unreadable state."
  (dolist (o (overlays-in beg end))
    (when (glasses-overlay-p o)
      (delete-overlay o))))


(defun glasses-convert-to-unreadable ()
  "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 glasses-convert-on-write-p
    (let ((case-fold-search nil)
	  (separator (regexp-quote glasses-separator)))
      (save-excursion
	(unless (string= glasses-separator "")
	  (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)
	    (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)
	    (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)
  "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))))
    (glasses-make-unreadable beg-line end-line)
    (glasses-make-readable beg-line end-line)))


;;; Minor mode definition


;;;###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."
  :group 'glasses :lighter " o^o"
  (save-excursion
    (save-restriction
      (widen)
      ;; We erase all the overlays anyway, to avoid dual sight in some
      ;; circumstances
      (glasses-make-unreadable (point-min) (point-max))
      (if glasses-mode
	  (progn
	    (jit-lock-register 'glasses-change)
	    (add-hook 'local-write-file-hooks
		      'glasses-convert-to-unreadable nil t))
	(jit-lock-unregister 'glasses-change)
	(remove-hook 'local-write-file-hooks
		     'glasses-convert-to-unreadable t)))))


;;; Announce

(provide 'glasses)


;; arch-tag: a3515167-c89e-484f-90a1-d85143e52b12
;;; glasses.el ends here