From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: lee Newsgroups: gmane.emacs.bugs Subject: bug#17021: updated patch Date: Mon, 24 Mar 2014 05:54:06 +0100 Message-ID: <87mwggb3ip.fsf@yun.yagibdah.de> References: <87mwgp1mq4.fsf@yun.yagibdah.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: ger.gmane.org 1395636902 15762 80.91.229.3 (24 Mar 2014 04:55:02 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Mon, 24 Mar 2014 04:55:02 +0000 (UTC) To: 17021@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Mon Mar 24 05:55:10 2014 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1WRwuk-0005MV-2c for geb-bug-gnu-emacs@m.gmane.org; Mon, 24 Mar 2014 05:55:10 +0100 Original-Received: from localhost ([::1]:34458 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WRwuj-00081J-IK for geb-bug-gnu-emacs@m.gmane.org; Mon, 24 Mar 2014 00:55:09 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40189) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WRwug-0007x8-5m for bug-gnu-emacs@gnu.org; Mon, 24 Mar 2014 00:55:06 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1WRwud-000387-S5 for bug-gnu-emacs@gnu.org; Mon, 24 Mar 2014 00:55:06 -0400 Original-Received: from debbugs.gnu.org ([140.186.70.43]:45441) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1WRwud-00036w-Mv for bug-gnu-emacs@gnu.org; Mon, 24 Mar 2014 00:55:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.80) (envelope-from ) id 1WRwud-0001Wj-42 for bug-gnu-emacs@gnu.org; Mon, 24 Mar 2014 00:55:03 -0400 X-Loop: help-debbugs@gnu.org In-Reply-To: <87mwgp1mq4.fsf@yun.yagibdah.de> Resent-From: lee Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Mon, 24 Mar 2014 04:55:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 17021 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: Original-Received: via spool by 17021-submit@debbugs.gnu.org id=B17021.13956368585800 (code B ref 17021); Mon, 24 Mar 2014 04:55:02 +0000 Original-Received: (at 17021) by debbugs.gnu.org; 24 Mar 2014 04:54:18 +0000 Original-Received: from localhost ([127.0.0.1]:46623 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WRwts-0001VT-R6 for submit@debbugs.gnu.org; Mon, 24 Mar 2014 00:54:18 -0400 Original-Received: from client-194-42-186-216.muenet.net ([194.42.186.216]:44466 helo=yun.yagibdah.de) by debbugs.gnu.org with esmtp (Exim 4.80) (envelope-from ) id 1WRwtn-0001VD-G5 for 17021@debbugs.gnu.org; Mon, 24 Mar 2014 00:54:13 -0400 Original-Received: from lee by yun.yagibdah.de with local (Exim 4.80.1) (envelope-from ) id 1WRwtl-0000Hi-O9 for 17021@debbugs.gnu.org; Mon, 24 Mar 2014 05:54:09 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux) X-Spam-Score: 0.4 (/) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.15 Precedence: list X-Spam-Score: 0.4 (/) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 140.186.70.43 X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.bugs:87284 Archived-At: --=-=-= Content-Type: text/plain Here`s an updated version of the patch: --=-=-= Content-Type: text/x-patch; charset=utf-8 Content-Disposition: inline; filename=hi-lock.diff Content-Transfer-Encoding: quoted-printable diff --git a/lisp/hi-lock.el b/lisp/hi-lock.el index 78efd86..85c3216 100644 --- a/lisp/hi-lock.el +++ b/lisp/hi-lock.el @@ -142,6 +142,33 @@ Instead, each hi-lock command will cycle through the f= aces in :type 'boolean :version "24.4") =20 +(defcustom hi-lock-file-name-specifier "\\(-\\*- \\)hi-lock-patterns-file" + "Expression used to find the name of a file to read hi-lock +highlighting-patterns from. + +The default is set such that a line specifying the file variable +`hi-lock-patterns-file' can be found. + +Please see `hi-lock-get-patterns-file-name' for how this +expression is used." + :type '(string) + :group 'hi-lock) +(put 'hi-lock-patterns-file 'safe-local-variable #'stringp) + +(defcustom hi-lock-patterns-end-marker "hi-lock-patterns-end" + "Expression used to mark the end of hi-lock highlighting-patterns in +a buffer dedicated to holding such patterns. This can be a regular +expression. + +The expression that will be searched for is the return value of +`hi-lock-make-reasonable-end-marker'. It is appended to the buffer, +in a new line, by `hi-lock-write-patterns-file'. + +`hi-lock-get-patterns-from-file' uses it to figure out when to stop +reading the buffer." + :type '(string) + :group 'hi-lock) + (defgroup hi-lock-faces nil "Faces for hi-lock." :group 'hi-lock @@ -205,10 +232,36 @@ Instead, each hi-lock command will cycle through the = faces in "Face for hi-lock mode." :group 'hi-lock-faces) =20 +(defface hi-global-variable + '((t (:foreground "Magenta"))) + "Face to highlight global variables." + :group 'hi-lock-faces) + +(defface hi-functionlike + '((t (:foreground "LightGreen"))) + "Face to highlight something that is like a function." + :group 'hi-lock-faces) + +(defface hi-constant + '((t (:foreground "brown4"))) + "Face to highlight something that is a constant." + :group 'hi-faces) + (defvar-local hi-lock-file-patterns nil "Patterns found in file for hi-lock. Should not be changed.") (put 'hi-lock-file-patterns 'permanent-local t) =20 +(defvar-local hi-lock-other-patterns nil + "Patterns for hi-lock found in a separate file. See +`hi-lock-patterns-file'.") + +(defvar-local hi-lock-patterns-file nil + "Remember the name of the file to read hi-lock highlighting-patterns + for this buffer from. + +Use this as buffer or dir local variable.") +(put 'hi-lock-patterns-file 'permanent-local t) + (defvar-local hi-lock-interactive-patterns nil "Patterns provided to hi-lock by user. Should not be changed.") (put 'hi-lock-interactive-patterns 'permanent-local t) @@ -242,33 +295,54 @@ a library is being loaded.") (let ((map (make-sparse-keymap "Hi Lock"))) (define-key-after map [highlight-regexp] '(menu-item "Highlight Regexp..." highlight-regexp - :help "Highlight text matching PATTERN (a regexp).")) + :help "Highlight text matching PATTERN (a regexp).")) =20 (define-key-after map [highlight-phrase] '(menu-item "Highlight Phrase..." highlight-phrase - :help "Highlight text matching PATTERN (a regexp processed to matc= h phrases).")) + :help "Highlight text matching PATTERN (a regexp processed to match ph= rases).")) =20 (define-key-after map [highlight-lines-matching-regexp] '(menu-item "Highlight Lines..." highlight-lines-matching-regexp - :help "Highlight lines containing match of PATTERN (a regexp).")) + :help "Highlight lines containing match of PATTERN (a regexp).")) =20 (define-key-after map [highlight-symbol-at-point] '(menu-item "Highlight Symbol at Point" highlight-symbol-at-point - :help "Highlight symbol found near point without prompting.")) + :help "Highlight symbol found near point without prompting.")) =20 (define-key-after map [unhighlight-regexp] '(menu-item "Remove Highlighting..." unhighlight-regexp - :help "Remove previously entered highlighting pattern." - :enable hi-lock-interactive-patterns)) + :help "Remove previously entered highlighting pattern." + :enable hi-lock-interactive-patterns)) =20 (define-key-after map [hi-lock-write-interactive-patterns] '(menu-item "Patterns to Buffer" hi-lock-write-interactive-patterns - :help "Insert interactively added REGEXPs into buffer at point." - :enable hi-lock-interactive-patterns)) + :help "Insert interactively added REGEXPs into buffer at point." + :enable hi-lock-interactive-patterns)) =20 (define-key-after map [hi-lock-find-patterns] '(menu-item "Patterns from Buffer" hi-lock-find-patterns - :help "Use patterns (if any) near top of buffer.")) + :help "Use patterns (if any) near top of buffer.")) + + (define-key-after map [hi-lock-constant] + '(menu-item "Highlight constant" hi-lock-constant + :help "Highlight something at point that is a constant.")) + + (define-key-after map [hi-lock-functionlike] + '(menu-item "Highlight functionlike" hi-lock-functionlike + :help "Highlight something at point that is like a function.")) + + (define-key-after map [hi-lock-global-variable] + '(menu-item "Highlight global variable" hi-lock-global-variable + :help "Highlight something at point that is a global variable.")) + + (define-key-after map [hi-lock-revert-patterns-from-file] + '(menu-item "Revert patterns from buffer" hi-lock-revert-patterns-fr= om-file + :help "Revert all highlighting patters to the patterns in the dedicate= d buffer." + :enable hi-lock-patterns-file)) + + (define-key-after map [hi-lock-revert-patterns-file-name] + '(menu-item "Revert name of patterns-file" hi-lock-revert-patterns-f= ile-name + :help "Revert the name of the file storing the highlighting patterns."= )) map) "Menu for hi-lock mode.") =20 @@ -281,6 +355,11 @@ a library is being loaded.") (define-key map "\C-xw." 'highlight-symbol-at-point) (define-key map "\C-xwr" 'unhighlight-regexp) (define-key map "\C-xwb" 'hi-lock-write-interactive-patterns) + (define-key map "\C-xwc" 'hi-lock-constant) + (define-key map "\C-xwf" 'hi-lock-functionlike) + (define-key map "\C-xwg" 'hi-lock-global-variable) + (define-key map "\C-xw!" 'hi-lock-revert-patterns-from-file) + (define-key map "\C-xwn" 'hi-lock-revert-patterns-file-name) map) "Key map for hi-lock.") =20 @@ -348,7 +427,56 @@ where FOO is a list of patterns. The patterns must st= art before position \(number of characters into buffer) `hi-lock-file-patterns-range'. Patterns will be read until Hi-lock: end is found. A mode is excluded if it's in the list -`hi-lock-exclude-modes'." +`hi-lock-exclude-modes'. + +\\[hi-lock-revert-patterns-file-name] + Search the current buffer for the variable + `hi-lock-patterns-file' and set it (to a potentially new value) + even when this variable is already set. + +This variable is used to specify a file in which to store +highlighting-patterns. This allows you to keep the patterns in a +separate file which can be shared among multiple files. This is +particularly useful when editing source code because you can use +a single file with highlighting-patterns which is shared by +multiple files of the same project. + +When using a separate file, highlighting-patterns can still be +written to the current buffer with +\\[hi-lock-write-interactive-patterns]. Both the patterns from +the current buffer and from the separate file apply. + +The file with the highlighting-patterns is transparently +maintained in a dedicated buffer. The dedicated buffer is +automatically saved to `hi-lock-patterns-file' when the current +buffer is saved. + +You can specify `hi-lock-patterns-file' as a buffer-local +variable. Please note that the value of this variable (the file +name) must be given in double-quotes. + +Please see also `hi-lock-file-name-specifier'. + +\\[hi-lock-constant] + Highlight the thing at point with the `hi-constant' face. + +\\[hi-lock-functionlike] + Highlight the thing at point with the `hi-functionlike' face. + +\\[hi-lock-global-variable] + Highlight the thing at point with the `hi-global' face. + +\\[hi-lock-revert-patterns-from-file] + Revert the currently used highlighting-patterns to the patterns + in `hi-lock-patterns-file'. + +\\[hi-lock-revert-patterns-file-name] + Once `hi-lock-patterns-file' has been set, the current buffer + is not searched again for a line specifying this variable. You + can revert to the value specified in the current buffer with + \\[hi-lock-revert-patterns-file-name]. This is for instances + when you modified the value of the variable and want the new + value take effect." :group 'hi-lock :lighter (:eval (if (or hi-lock-interactive-patterns hi-lock-file-patterns) @@ -377,23 +505,29 @@ versions before 22 use the following in your init fil= e: (progn (define-key-after menu-bar-edit-menu [hi-lock] (cons "Regexp Highlighting" hi-lock-menu)) + ;; order does matter, see `hi-lock-apply-patterns-from-file' (hi-lock-find-patterns) + (hi-lock-apply-patterns-from-file) + (add-hook 'after-save-hook 'hi-lock-write-patterns-file t t) (add-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook nil t) ;; Remove regexps from font-lock-keywords (bug#13891). (add-hook 'change-major-mode-hook (lambda () (hi-lock-mode -1)) nil t)) ;; Turned off. (when (or hi-lock-interactive-patterns - hi-lock-file-patterns) + hi-lock-file-patterns + hi-lock-other-patterns) (when hi-lock-interactive-patterns (font-lock-remove-keywords nil hi-lock-interactive-patterns) (setq hi-lock-interactive-patterns nil)) (when hi-lock-file-patterns (font-lock-remove-keywords nil hi-lock-file-patterns) (setq hi-lock-file-patterns nil)) + (hi-lock-unapply-patterns-from-file) (remove-overlays nil nil 'hi-lock-overlay t) (when font-lock-fontified (font-lock-fontify-buffer))) (define-key-after menu-bar-edit-menu [hi-lock] nil) - (remove-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook t))) + (remove-hook 'font-lock-mode-hook 'hi-lock-font-lock-hook t) + (remove-hook 'after-save-hook 'hi-lock-write-patterns-file t))) =20 ;;;###autoload (define-globalized-minor-mode global-hi-lock-mode @@ -624,6 +758,94 @@ be found in variable `hi-lock-interactive-patterns'." (when (> (point) hi-lock-file-patterns-range) (warn "Inserted keywords not close enough to top of file"))) =20 +(defsubst hi-lock-comment-start-protected () + "Since `comment-start' can sometimes be nil, return a default +for such instances, otherwise return `comment-start'." + (or comment-start "# ")) + +;;;###autoload +(defun hi-lock-get-patterns-file-name (&optional force) + "When `hi-lock-patterns-file' is not nil, attempt to set it from +`hi-lock-file-name-specifier' by searching the current buffer, unless +the variable is already set. + +When the optional argument FORCE is not nil, attempt to set the +variable regardless whether it is already set or not. + +In any case, return the value of `hi-lock-patterns-file', which +can be nil when not specified in the current buffer. + +The search is limited to between `point-min' and (+ (point-min) 1024)." + (interactive) + (if (or + force + (not hi-lock-patterns-file)) + (save-excursion + (save-restriction + (widen) + (goto-char (point-min)) + (let ((file-name-specifier + (concat "^" (hi-lock-comment-start-protected) "[:space:]*" hi-lock-file= -name-specifier ": "))) + (when (re-search-forward file-name-specifier (+ (point) 1024) t) + (when (looking-at "\\\"") (forward-char) + (setq hi-lock-patterns-file (thing-at-point 'filename t)))))))) + hi-lock-patterns-file) + +;;;###autoload +(defun hi-lock-revert-patterns-file-name () + "Use `hi-lock-get-patterns-file-name' to revert +`hi-lock-patterns-file' even when `hi-lock-patterns-file' is +already set." + (interactive) + (if (hi-lock-get-patterns-file-name t) + (message "Use highlighting-patterns from %s" + hi-lock-patterns-file) + (error "Nothing found to set `hi-lock-patterns-file' from"))) + +(defsubst hi-lock-make-reasonable-end-marker (for-writing) + "Return a regex which is a reasonable end-marker to indicate where + hi-lock highlighting-patterns inserted into a dedicated buffer + end. Reasonable particularly means that the marker shall be usable + even when `comment-start' is nil. + +When the argument 'for-writing' is nil, return a regex which matches +the end-marker used in the patterns=C2=B4 buffer. + +Otherwise, the returned marker is suited to be appended to a buffer." + (if for-writing + (concat (hi-lock-comment-start-protected) hi-lock-patterns-end-marke= r) + (concat "^" (hi-lock-comment-start-protected) "\\_<" hi-lock-patterns-= end-marker "\\_>"))) + +(defun hi-lock-quick-add (whichface) + "Highlight something at point with a face given in whichface." + (let* ((regexp (hi-lock-regexp-okay (find-tag-default-as-symbol-regexp))= )) + (hi-lock-set-pattern regexp whichface)) + ;; set modified to get the dedicated patterns buffer updated + (if hi-lock-patterns-file + (set-buffer-modified-p t) + (message "The variable `hi-lock-patterns-file' needs to be set to spec= ify a dedicated buffer to store patterns." ))) + +;;;###autoload +(defun hi-lock-constant () + "Add a pattern to highlight something at point that is a +constant." + (interactive) + (hi-lock-quick-add 'hi-constant)) + +;;;###autoload +(defun hi-lock-functionlike () + "Add a pattern to highlight something at point that is like a +function." + (interactive) + (hi-lock-quick-add 'hi-functionlike)) + +;;;###autoload +(defun hi-lock-global-variable () + "Add a pattern to highlight something at point that is a +global variable." + (interactive) + (hi-lock-quick-add 'hi-global-variable)) + ;; Implementation Functions =20 (defun hi-lock-process-phrase (phrase) @@ -753,10 +975,100 @@ with completion and history." (if (called-interactively-p 'interactive) (message "Hi-lock added %d patterns." (length all-patterns))))= ))) =20 +(defun hi-lock-get-patterns-from-file (file) + "Read hi-lock-mode highlighting-patterns from a file and return +the patterns read." + (with-current-buffer + (find-file-noselect file) + (goto-char (point-min)) + (let ((marker-pos + (re-search-forward (hi-lock-make-reasonable-end-marker nil) (point-max= ) t))) + (when marker-pos + (goto-char marker-pos) + (forward-line -1) + (end-of-line) + (setq marker-pos (point)) + (goto-char (point-min)) + (message "reading hi-lock highlighting-patterns from %s (characters %d..%= d)" + (buffer-name) + (point-min) marker-pos) + (let ((patterns nil)) + (while (< (point) marker-pos) + (setq patterns (append (read (current-buffer)) patterns))) + patterns))))) + +(defsubst hi-lock-unapply-patterns-from-file () + "Disable all highlighting-patterns from another file which is +used with this file. The patterns are not deleted or otherwise +modified; only the highlighting they bring about is disabled." + (font-lock-remove-keywords nil hi-lock-other-patterns) + (setq hi-lock-other-patterns nil)) + +(defun hi-lock-apply-patterns-from-file () + "Use hi-lock-mode highlighting-patterns from another file with this + file. + +Which file to read the patterns from is specified through +`hi-lock-file-name-specifier'. This variable can be set as a +buffer-local variable. + +The file will be visited in another buffer, and additional patterns +are written to the other buffer and saved to the file when this file +is saved." + (hi-lock-unapply-patterns-from-file) + (when (hi-lock-get-patterns-file-name) + (setq hi-lock-other-patterns (hi-lock-get-patterns-from-file hi-lock-p= atterns-file))) + (if (not hi-lock-other-patterns) + (message "found no patterns to apply to %s in %s" + (buffer-name) + hi-lock-patterns-file) + (font-lock-add-keywords nil hi-lock-other-patterns) + (message "%d patterns applied from file %s to buffer %s" + (length hi-lock-other-patterns) + hi-lock-patterns-file + (buffer-name))) + (font-lock-fontify-buffer)) + +(defun hi-lock-write-patterns-file () + "When `hi-lock-patterns-file' is not nil, update the dedicated +buffer holding the hi-lock highlighting-patterns and save the +buffer to `hi-lock-patterns-file'." + (interactive) + (when (hi-lock-get-patterns-file-name) + (let ((all-patterns + (delete-dups (append + ;; put most recently added into first line of buffer + hi-lock-interactive-patterns + ;; the patterns buffer may have been edited, + ;; hence do not append `hi-lock-other-patterns' + (hi-lock-get-patterns-from-file hi-lock-patterns-file))))) + (with-current-buffer (find-file-noselect hi-lock-patterns-file) + (erase-buffer) + (mapc + (lambda (this) + (insert (format "(%s)\n" (prin1-to-string this)))) + all-patterns) + (insert (hi-lock-make-reasonable-end-marker t) "\n") + (save-buffer))))) + +(defun hi-lock-revert-patterns-from-file () + "Unset all hi-lock highlighting-patterns for the current buffer +and apply patterns from the buffers` patterns file. Do nothing +when no file for storing the patterns is specified for the +current buffer." + (interactive) + (hi-lock-unface-buffer t) + (hi-lock-unapply-patterns-from-file) + (if (not (hi-lock-get-patterns-file-name)) + (error "No buffer with patterns to revert to has been set") + (hi-lock-find-patterns) + (hi-lock-apply-patterns-from-file))) + (defun hi-lock-font-lock-hook () "Add hi-lock patterns to font-lock's." (when font-lock-fontified (font-lock-add-keywords nil hi-lock-file-patterns t) + (font-lock-add-keywords nil hi-lock-other-patterns t) (font-lock-add-keywords nil hi-lock-interactive-patterns t))) =20 (defvar hi-lock--hashcons-hash --=-=-= Content-Type: text/plain -- Knowledge is volatile and fluid. Software is power. --=-=-=--