branch: externals/hyperbole commit 9e13b4396a4860e14367fc31d3ec440725b09650 Author: Bob Weiner <r...@gnu.org> Commit: Bob Weiner <r...@gnu.org>
hui-select.el and hyperbole.el reformatting; no logic changes --- hui-select.el | 1331 ++++++++++++++++++++++++++++----------------------------- hyperbole.el | 4 +- 2 files changed, 668 insertions(+), 667 deletions(-) diff --git a/hui-select.el b/hui-select.el index 5fa90de..282527e 100644 --- a/hui-select.el +++ b/hui-select.el @@ -136,7 +136,7 @@ (defcustom hui-select-indent-modes (append '(altmath-mode asm-mode csh-mode eiffel-mode ksh-mode math-mode miranda-mode python-mode pascal-mode sather-mode) - hui-select-text-modes) + hui-select-text-modes) "*List of language major modes that use indentation mostly to define syntactic structure." :type '(repeat (function :tag "Mode")) :group 'hyperbole-commands) @@ -219,26 +219,26 @@ The non-nil value returned is the function to call to select that syntactic unit (or (numberp pos) (setq pos (point))) (setq hui-select-previous 'char) (let* ((syntax (char-syntax (or (char-after pos) (char-before pos)))) - (pair (assq syntax hui-select-syntax-alist))) + (pair (assq syntax hui-select-syntax-alist))) (and pair (or hui-select-whitespace (not (eq (cdr pair) 'thing-whitespace))) - ;; Ignore matches that are preceded by '\' as a quote, e.g. ?\' - (or (not (char-after pos)) - (= pos (point-min)) - (and (char-before pos) (/= ?\\ (char-before pos)))) - (cdr pair))))) + ;; Ignore matches that are preceded by '\' as a quote, e.g. ?\' + (or (not (char-after pos)) + (= pos (point-min)) + (and (char-before pos) (/= ?\\ (char-before pos)))) + (cdr pair))))) ;;;###autoload (defun hui-select-goto-matching-delimiter () "Jump back and forth between the start and end delimiters of a thing." (interactive) (cond ((memq major-mode hui-select-markup-modes) - (hui-select-goto-matching-tag)) - ((and (preceding-char) (or (= ?\) (char-syntax (preceding-char))) - (= ?\" (preceding-char)))) - (backward-sexp)) - ((and (following-char) (or (= ?\( (char-syntax (following-char))) - (= ?\" (following-char)))) - (forward-sexp)))) + (hui-select-goto-matching-tag)) + ((and (preceding-char) (or (= ?\) (char-syntax (preceding-char))) + (= ?\" (preceding-char)))) + (backward-sexp)) + ((and (following-char) (or (= ?\( (char-syntax (following-char))) + (= ?\" (following-char)))) + (forward-sexp)))) ;;;###autoload (defun hui-select-initialize () @@ -247,33 +247,32 @@ Also, add language-specific syntax setups to aid in thing selection." (interactive) (unless (boundp 'hyperbole-loading) (require 'hyperbole)) - (if hkey-init - (progn - (transient-mark-mode 1) - (hkey-global-set-key [double-down-mouse-1] nil) - (hkey-global-set-key [double-mouse-1] 'hui-select-thing-with-mouse) - (hkey-global-set-key [triple-down-mouse-1] nil) - (hkey-global-set-key [triple-mouse-1] 'hui-select-thing-with-mouse))) + (when hkey-init + (transient-mark-mode 1) + (hkey-global-set-key [double-down-mouse-1] nil) + (hkey-global-set-key [double-mouse-1] 'hui-select-thing-with-mouse) + (hkey-global-set-key [triple-down-mouse-1] nil) + (hkey-global-set-key [triple-mouse-1] 'hui-select-thing-with-mouse)) ;; ;; These hooks let you select C++ and Java methods and classes by ;; double-clicking on the first character of a definition or on its ;; opening or closing brace. This is all necessary since some ;; programmers don't put their function braces in the first column. (var:add-and-run-hook 'java-mode-hook (lambda () - (setq defun-prompt-regexp - "^[ \t]*\\(\\(\\(public\\|protected\\|private\\|const\\|abstract\\|synchronized\\|final\\|static\\|threadsafe\\|transient\\|native\\|volatile\\)\\s-+\\)*\\(\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*[][_$.a-zA-Z0-9]+\\|[[a-zA-Z]\\)\\s-*\\)\\s-+\\)\\)?\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*\\s-+\\)\\s-*\\)?\\([_a-zA-Z][^][ \t:;.,{}()=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f [...] + (setq defun-prompt-regexp + "^[ \t]*\\(\\(\\(public\\|protected\\|private\\|const\\|abstract\\|synchronized\\|final\\|static\\|threadsafe\\|transient\\|native\\|volatile\\)\\s-+\\)*\\(\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*[][_$.a-zA-Z0-9]+\\|[[a-zA-Z]\\)\\s-*\\)\\s-+\\)\\)?\\(\\([[a-zA-Z][][_$.a-zA-Z0-9]*\\s-+\\)\\s-*\\)?\\([_a-zA-Z][^][ \t:;.,{}()=]*\\|\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)\\)\\s-*\\(([^);{}]*)\\)?\\([] \t]*\\)\\(\\s-*\\<throws\\>\\s-*\\(\\([_$a-zA-Z][_$.a-zA-Z0-9]*\\)[, \t\n\r\f]*\\)+\\)?\\s-*"))) (var:add-and-run-hook 'c++-mode-hook (lambda () - (setq defun-prompt-regexp - "^[ \t]*\\(template\\s-*<[^>;.{}]+>\\s-*\\)?\\(\\(\\(auto\\|const\\|explicit\\|extern\\s-+\"[^\"]+\"\\|extern\\|friend\\|inline\\|mutable\\|overload\\|register\\|static\\|typedef\\|virtual\\)\\s-+\\)*\\(\\([[<a-zA-Z][]_a-zA-Z0-9]*\\(::[]_a-zA-Z0-9]+\\)?\\s-*<[_<>a-zA-Z0-9 ,]+>\\s-*[*&]*\\|[[<a-zA-Z][]_<>a-zA-Z0-9]*\\(::[[<a-zA-Z][]_<>a-zA-Z0-9]+\\)?\\s-*[*&]*\\)[*& \t\n\r]+\\)\\)?\\(\\(::\\|[[<a-zA-Z][]_a-zA-Z0-9]*\\s-*<[^>;{}]+>\\s-*[*&]*::\\|[[<a-zA-Z][] [...] + (setq defun-prompt-regexp + "^[ \t]*\\(template\\s-*<[^>;.{}]+>\\s-*\\)?\\(\\(\\(auto\\|const\\|explicit\\|extern\\s-+\"[^\"]+\"\\|extern\\|friend\\|inline\\|mutable\\|overload\\|register\\|static\\|typedef\\|virtual\\)\\s-+\\)*\\(\\([[<a-zA-Z][]_a-zA-Z0-9]*\\(::[]_a-zA-Z0-9]+\\)?\\s-*<[_<>a-zA-Z0-9 ,]+>\\s-*[*&]*\\|[[<a-zA-Z][]_<>a-zA-Z0-9]*\\(::[[<a-zA-Z][]_<>a-zA-Z0-9]+\\)?\\s-*[*&]*\\)[*& \t\n\r]+\\)\\)?\\(\\(::\\|[[<a-zA-Z][]_a-zA-Z0-9]*\\s-*<[^>;{}]+>\\s-*[*&]*::\\|[[<a-zA-Z][]_~<>a-zA-Z0-9]*\\s-* [...] ;; ;; Match to Lisp symbols with : in their names, often included in help buffers. (var:add-and-run-hook 'help-mode-hook (lambda () (modify-syntax-entry ?: "_" help-mode-syntax-table))) ;; ;; Allow for marking and moving brace delimited groups. (var:add-and-run-hook 'lisp-mode-hook - (lambda () - (modify-syntax-entry ?\{ "\(\}" lisp-mode-syntax-table) - (modify-syntax-entry ?\} "\)\{" lisp-mode-syntax-table))) + (lambda () + (modify-syntax-entry ?\{ "\(\}" lisp-mode-syntax-table) + (modify-syntax-entry ?\} "\)\{" lisp-mode-syntax-table))) ;; ;; This hook makes tags, comments, sentences and text blocks ;; selectable in SGML-related modes. @@ -287,32 +286,32 @@ Also, add language-specific syntax setups to aid in thing selection." syntax-table-sym (intern (concat mode-str "-syntax-table")) keymap-sym (intern (concat mode-str "-map"))) (var:add-and-run-hook hook-sym - `(lambda () + `(lambda () (let ((syntax-table (symbol-value ',syntax-table-sym)) (keymap (symbol-value ',keymap-sym))) - (modify-syntax-entry ?< "(>" syntax-table) - (modify-syntax-entry ?> ")<" syntax-table) - (modify-syntax-entry ?\{ "\(\}" syntax-table) - (modify-syntax-entry ?\} "\)\{" syntax-table) - (modify-syntax-entry ?\" "\"" syntax-table) - (modify-syntax-entry ?= "." syntax-table) - (modify-syntax-entry ?. "_" syntax-table) - (setq sentence-end "\\([^ \t\n\r>]<\\|>\\(<[^>]*>\\)*\\|[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)\\)[ \t\n]*") - (define-key keymap "\C-c." 'hui-select-goto-matching-tag)))) + (modify-syntax-entry ?< "(>" syntax-table) + (modify-syntax-entry ?> ")<" syntax-table) + (modify-syntax-entry ?\{ "\(\}" syntax-table) + (modify-syntax-entry ?\} "\)\{" syntax-table) + (modify-syntax-entry ?\" "\"" syntax-table) + (modify-syntax-entry ?= "." syntax-table) + (modify-syntax-entry ?. "_" syntax-table) + (setq sentence-end "\\([^ \t\n\r>]<\\|>\\(<[^>]*>\\)*\\|[.?!][]\"')}]*\\($\\| $\\|\t\\| \\)\\)[ \t\n]*") + (define-key keymap "\C-c." 'hui-select-goto-matching-tag)))) (unless (eq mode 'web-mode) (var:add-and-run-hook hook-sym (lambda () - (make-local-variable 'comment-start) - (make-local-variable 'comment-end) - (setq comment-start "<!--" comment-end "-->") - (make-local-variable 'sentence-end))))) + (make-local-variable 'comment-start) + (make-local-variable 'comment-end) + (setq comment-start "<!--" comment-end "-->") + (make-local-variable 'sentence-end))))) hui-select-markup-modes))) (defun hui-select-get-region-boundaries () "Return the (START . END) boundaries of region for `hui-select-thing'." (or (hui-select-boundaries (point)) (when (eq hui-select-previous 'punctuation) - (hui-select-word (point))))) + (hui-select-word (point))))) ;;;###autoload (defun hui-select-get-region () @@ -329,26 +328,26 @@ If `hui-select-display-type' is non-nil and this is called interactively, the type of selection is displayed in the minibuffer." (interactive (cond ((and (fboundp 'use-region-p) (use-region-p)) - nil) - ((and (boundp 'transient-mark-mode) transient-mark-mode mark-active) - nil) - (t - ;; Reset selection based on the syntax of character at point. - (hui-select-reset) - nil))) + nil) + ((and (boundp 'transient-mark-mode) transient-mark-mode mark-active) + nil) + (t + ;; Reset selection based on the syntax of character at point. + (hui-select-reset) + nil))) (let ((region (hui-select-get-region-boundaries))) (unless region (when (eq hui-select-previous 'punctuation) - (setq region (hui-select-word (point))))) + (setq region (hui-select-word (point))))) (when region (goto-char (car region)) (set-mark (cdr region)) (when (fboundp 'activate-region) (activate-region)) (when (and (boundp 'transient-mark-mode) - transient-mark-mode) - (setq mark-active t)) + transient-mark-mode) + (setq mark-active t)) (and (called-interactively-p 'interactive) hui-select-display-type - (message "%s" hui-select-previous)) + (message "%s" hui-select-previous)) (run-hooks 'hui-select-thing-hook) t))) @@ -362,18 +361,18 @@ displayed in the minibuffer." (interactive "@e") (mouse-set-point event) (cond ((and (eq hui-select-prior-point (point)) - (eq hui-select-prior-buffer (current-buffer))) - ;; Prior click was at the same point as before, so enlarge - ;; selection to the next bigger item. - (let ((select-active-regions t)) ;; Automatically copy active - ;; region to PRIMARY inter-program selection. - (when (hui-select-bigger-thing) - (and (called-interactively-p 'interactive) hui-select-display-type - (message "%s" hui-select-previous)))) - t) - (t (setq this-command 'mouse-start-selection) - (hui-select-reset) - (hui-select-thing-with-mouse event)))) + (eq hui-select-prior-buffer (current-buffer))) + ;; Prior click was at the same point as before, so enlarge + ;; selection to the next bigger item. + (let ((select-active-regions t)) ;; Automatically copy active + ;; region to PRIMARY inter-program selection. + (when (hui-select-bigger-thing) + (and (called-interactively-p 'interactive) hui-select-display-type + (message "%s" hui-select-previous)))) + t) + (t (setq this-command 'mouse-start-selection) + (hui-select-reset) + (hui-select-thing-with-mouse event)))) ;;;###autoload (defun hui-select-goto-matching-tag () @@ -384,13 +383,13 @@ does not have a `>' terminator character." (interactive) (when (memq major-mode hui-select-markup-modes) (let ((result) - ;; Assume case of tag names is irrelevant. - (case-fold-search t) - (opoint (point)) - (tag) - end-point - start-regexp - end-regexp) + ;; Assume case of tag names is irrelevant. + (case-fold-search t) + (opoint (point)) + (tag) + end-point + start-regexp + end-regexp) ;; Leave point at the start of the tag that point is within or that ;; follows point. @@ -399,100 +398,102 @@ does not have a `>' terminator character." ((looking-at "<[^<> \t\n\r]")) ;; Point was within a tag. ((and (re-search-backward "[<>]" nil t) - (looking-at "<[^<> \t\n\r]"))) + (looking-at "<[^<> \t\n\r]"))) ;; Move to following tag. ((and (re-search-forward "<" nil t) - (progn (backward-char 1) - (looking-at "<[^<> \t\n\r]")))) + (progn (backward-char 1) + (looking-at "<[^<> \t\n\r]")))) ;; No tag follows point. (t (error "(hui-select-goto-matching-tag): No tag found after point"))) (if (catch 'done - (cond - ;; Beginning of a tag pair - ((looking-at "<[^/][^<> \t\n\r]*") - (setq tag (match-string 0) - start-regexp (regexp-quote tag) - end-regexp (concat "</" (substring start-regexp 1))) - ;; Skip over nested tags. - (let ((count 0) - (regexp (concat start-regexp "\\|" end-regexp)) - match-point) - (while (and (>= count 0) - (re-search-forward regexp nil t)) - (setq match-point (match-beginning 0)) - (if (/= (char-after (1+ (match-beginning 0))) ?/) - ;; Start tag - (setq count (1+ count)) - ;; End tag - (setq end-point (point)) - (if (or (not (re-search-forward "[<>]" nil t)) - (= (preceding-char) ?<)) - ;; No terminator character `>' for end tag - (progn (setq result end-point) - (throw 'done nil))) - (setq count (1- count)) - (when (zerop count) - (goto-char match-point) - (setq result t) - (throw 'done result)))))) - ;; - ;; End of a tag pair - ((or (looking-at "</[^\> \t\n\r]+") - (and (skip-chars-backward "<") - (looking-at "</[^\> \t\n\r]+"))) - (goto-char (match-end 0)) - (setq tag (match-string 0) - end-regexp (regexp-quote tag) - start-regexp (concat "<" (substring end-regexp 2))) - (setq end-point (point)) - (when (or (not (re-search-forward "[<>]" nil t)) - (= (preceding-char) ?<)) - ;; No terminator character `>' for end tag - (setq result end-point) - (throw 'done nil)) - ;; Skip over nested tags. - (let ((count 0) - (regexp (concat start-regexp "\\|" end-regexp))) - (while (and (>= count 0) - (re-search-backward regexp nil t)) - (if (= (char-after (1+ (point))) ?/) - ;; End tag - (setq count (1+ count)) - ;; Start tag - (setq count (1- count)) - (when (zerop count) - (setq result t) - (throw 'done t)))))))) - nil - ;; Didn't find matching tag. - (goto-char opoint)) + (cond + ;; Beginning of a tag pair + ((looking-at "<[^/][^<> \t\n\r]*") + (setq tag (match-string 0) + start-regexp (regexp-quote tag) + end-regexp (concat "</" (substring start-regexp 1))) + ;; Skip over nested tags. + (let ((count 0) + (regexp (concat start-regexp "\\|" end-regexp)) + match-point) + (while (and (>= count 0) + (re-search-forward regexp nil t)) + (setq match-point (match-beginning 0)) + (if (/= (char-after (1+ (match-beginning 0))) ?/) + ;; Start tag + (setq count (1+ count)) + ;; End tag + (setq end-point (point)) + (if (or (not (re-search-forward "[<>]" nil t)) + (= (preceding-char) ?<)) + ;; No terminator character `>' for end tag + (progn (setq result end-point) + (throw 'done nil))) + (setq count (1- count)) + (when (zerop count) + (goto-char match-point) + (setq result t) + (throw 'done result)))))) + ;; + ;; End of a tag pair + ((or (looking-at "</[^\> \t\n\r]+") + (and (skip-chars-backward "<") + (looking-at "</[^\> \t\n\r]+"))) + (goto-char (match-end 0)) + (setq tag (match-string 0) + end-regexp (regexp-quote tag) + start-regexp (concat "<" (substring end-regexp 2))) + (setq end-point (point)) + (when (or (not (re-search-forward "[<>]" nil t)) + (= (preceding-char) ?<)) + ;; No terminator character `>' for end tag + (setq result end-point) + (throw 'done nil)) + ;; Skip over nested tags. + (let ((count 0) + (regexp (concat start-regexp "\\|" end-regexp))) + (while (and (>= count 0) + (re-search-backward regexp nil t)) + (if (= (char-after (1+ (point))) ?/) + ;; End tag + (setq count (1+ count)) + ;; Start tag + (setq count (1- count)) + (when (zerop count) + (setq result t) + (throw 'done t)))))))) + nil + ;; Didn't find matching tag. + (goto-char opoint)) (cond ((integerp result) - (goto-char result) - (error "(hui-select-goto-matching-tag): Add a terminator character for this end <tag>")) - ((null tag) - (error "(hui-select-goto-matching-tag): No <tag> following point")) - ((null result) - (when (called-interactively-p 'interactive) - (beep) - (message "(hui-select-goto-matching-tag): No matching tag for %s>" tag) - result)) - (t result))))) + (goto-char result) + (error "(hui-select-goto-matching-tag): Add a terminator character for this end <tag>")) + ((null tag) + (error "(hui-select-goto-matching-tag): No <tag> following point")) + ((null result) + (when (called-interactively-p 'interactive) + (beep) + (message "(hui-select-goto-matching-tag): No matching tag for %s>" tag) + result)) + (t result))))) ;;;###autoload (defun hui-select-and-copy-thing () "Copy the region surrounding the syntactical unit at point." (interactive) (let ((bounds (hui-select-boundaries (point)))) - (if bounds (copy-region-as-kill (car bounds) (cdr bounds))))) + (when bounds + (copy-region-as-kill (car bounds) (cdr bounds))))) ;;;###autoload (defun hui-select-and-kill-thing () "Kill the region surrounding the syntactical unit at point." (interactive "*") (let ((bounds (hui-select-boundaries (point)))) - (if bounds (kill-region (car bounds) (cdr bounds))))) + (when bounds + (kill-region (car bounds) (cdr bounds))))) ;; @@ -510,10 +511,10 @@ The character at POS is selected if no other thing is matched." ((eq hui-select-previous 'char) (hui-select-syntactical-region pos)) ((and (car hui-select-old-region) - (memq hui-select-previous - '(sexp sexp-start sexp-end sexp-up)) - (hui-select-sexp-up pos) - (hui-select-region-bigger-p hui-select-old-region hui-select-region)) + (memq hui-select-previous + '(sexp sexp-start sexp-end sexp-up)) + (hui-select-sexp-up pos) + (hui-select-region-bigger-p hui-select-old-region hui-select-region)) hui-select-region) ;; ;; In the general case, we can't know ahead of time what the next @@ -521,42 +522,42 @@ The character at POS is selected if no other thing is matched." ;; the best fit. This means that dynamically, the order of type ;; selection will change based on the buffer context. (t (let ((min-region (1+ (- (point-max) (point-min)))) - (result) - region region-size) - (mapc - (lambda (sym-func) - (setq region - (if (car (cdr sym-func)) - (funcall (car (cdr sym-func)) pos))) - (if (and region (car region) - (hui-select-region-bigger-p - hui-select-old-region region) - (setq region-size - (- (cdr region) (car region))) - (< region-size min-region)) - (setq min-region region-size - result - (list - ;; The actual selection type is - ;; sometimes different than the one we - ;; originally tried, so recompute it here. - (car (assq hui-select-previous - hui-select-bigger-alist)) - (car region) (cdr region))))) - hui-select-bigger-alist) - (if result - ;; Returns hui-select-region - (progn (setq hui-select-previous (car result)) - (hui-select-set-region (nth 1 result) (nth 2 result))) - ;; - ;; Restore prior selection type since we failed to find a - ;; new one. - (setq hui-select-previous prior-type) - (beep) - (message - "(hui-select-boundaries): `%s' is the largest selectable region" - hui-select-previous) - nil)))))) + (result) + region region-size) + (mapc + (lambda (sym-func) + (setq region + (when (car (cdr sym-func)) + (funcall (car (cdr sym-func)) pos))) + (when (and region (car region) + (hui-select-region-bigger-p + hui-select-old-region region) + (setq region-size + (- (cdr region) (car region))) + (< region-size min-region)) + (setq min-region region-size + result + (list + ;; The actual selection type is + ;; sometimes different than the one we + ;; originally tried, so recompute it here. + (car (assq hui-select-previous + hui-select-bigger-alist)) + (car region) (cdr region))))) + hui-select-bigger-alist) + (if result + ;; Returns hui-select-region + (progn (setq hui-select-previous (car result)) + (hui-select-set-region (nth 1 result) (nth 2 result))) + ;; + ;; Restore prior selection type since we failed to find a + ;; new one. + (setq hui-select-previous prior-type) + (beep) + (message + "(hui-select-boundaries): `%s' is the largest selectable region" + hui-select-previous) + nil)))))) ;;;###autoload (defun hui-select-double-click-hook (event click-count) @@ -565,9 +566,9 @@ If the double-click occurs at the same point as the last double-click, select the next larger syntactic structure. If `hui-select-display-type' is non-nil, the type of selection is displayed in the minibuffer." (cond ((/= click-count 2) - ;; Return nil so any other hooks are performed. - nil) - (t (hui-select-thing-with-mouse event)))) + ;; Return nil so any other hooks are performed. + nil) + (t (hui-select-thing-with-mouse event)))) (defun hui-select-syntactical-region (pos) "Return the (start . end) of a syntactically defined region based upon the buffer position POS. @@ -592,14 +593,14 @@ If an error occurs during syntax scanning, it returns nil." (if (save-excursion (goto-char pos) (eolp)) (hui-select-line pos) (let* ((syntax (char-syntax (or (char-after pos) (char-before pos)))) - (pair (assq syntax hui-select-syntax-alist))) + (pair (assq syntax hui-select-syntax-alist))) (cond ((and pair - (or hui-select-whitespace - (not (eq (cdr pair) 'hui-select-whitespace)))) - (funcall (cdr pair) pos)) - (hui-select-char-p - (setq hui-select-previous 'char) - (hui-select-set-region pos (1+ pos))))))) + (or hui-select-whitespace + (not (eq (cdr pair) 'hui-select-whitespace)))) + (funcall (cdr pair) pos)) + (hui-select-char-p + (setq hui-select-previous 'char) + (hui-select-set-region pos (1+ pos))))))) (defun hui-select-at-delimited-thing-p () "Return non-nil if point is at a markup pair, list, array/vector, set, comment or string, else nil. @@ -609,14 +610,14 @@ Ignores any match if on an Emacs button and instead returns nil." (unless (button-at (point)) (setq hkey-value (hui-select-delimited-thing-call #'hui-select-at-p)) (cond ((eq hkey-value 'hui-select-punctuation) - (if (hui-select-comment (point)) - (setq hkey-value #'hui-select-comment) - ;; Else here used to be `hkey-value' but then we are returning a - ;; value for any punctuation character without knowing if - ;; it is part of a delimited thing. Nil should be the - ;; right thing here. - nil)) - (t hkey-value)))) + (if (hui-select-comment (point)) + (setq hkey-value #'hui-select-comment) + ;; Else here used to be `hkey-value' but then we are returning a + ;; value for any punctuation character without knowing if + ;; it is part of a delimited thing. Nil should be the + ;; right thing here. + nil)) + (t hkey-value)))) (defun hui-select-delimited-thing () "Select a markup pair, list, array/vector, set, comment or string at point and return t, else nil." @@ -624,23 +625,23 @@ Ignores any match if on an Emacs button and instead returns nil." (prog1 (and (hui-select-delimited-thing-call #'hui-select-thing) t) ;; If selected region is followed by only whitespace and then a ;; newline, add the newline to the region. - (if (use-region-p) - (if (> (mark) (point)) - (save-excursion - (goto-char (mark)) - (skip-chars-forward " \t") - (and (char-after) (= ?\n (char-after)) - (set-mark (1+ (point))))) - (if (looking-at "[ \t]*\n") - (goto-char (match-end 0))))))) + (when (use-region-p) + (if (> (mark) (point)) + (save-excursion + (goto-char (mark)) + (skip-chars-forward " \t") + (and (char-after) (= ?\n (char-after)) + (set-mark (1+ (point))))) + (when (looking-at "[ \t]*\n") + (goto-char (match-end 0))))))) (defun hui-select-at-delimited-sexp-p () (unless (eolp) (let ((syn-before (if (char-before) (char-syntax (char-before)) 0)) - (syn-after (if (char-after) (char-syntax (char-after)) 0))) + (syn-after (if (char-after) (char-syntax (char-after)) 0))) (or (and (/= syn-before ?\\) (or (= syn-after ?\() (= syn-after ?\)))) - (and (= syn-before ?\)) (char-before (1- (point))) - (/= ?\\ (char-syntax (char-before (1- (point)))))))))) + (and (= syn-before ?\)) (char-before (1- (point))) + (/= ?\\ (char-syntax (char-before (1- (point)))))))))) (defun hui-select-mark-delimited-sexp () "If point is before or after an sexp and not at an end of line, then deactivate the mark and mark the sexp. @@ -649,23 +650,23 @@ Return t if marked, nil otherwise. If any error occurs such as nil." (interactive) (let ((mark-sexp-func (lambda () - (if (region-active-p) (deactivate-mark)) - (mark-sexp) t))) + (when (region-active-p) (deactivate-mark)) + (mark-sexp) t))) (condition-case nil - (let ((syn-after (char-syntax (char-after))) - syn-before) - (cond ((eq syn-after ?\() - (funcall mark-sexp-func)) - ((eq syn-after ?\)) - (forward-char 1) - (backward-sexp) - (funcall mark-sexp-func)) - ((and (not (eolp)) - (setq syn-before (char-syntax (char-before))) - (eq syn-before ?\))) - (backward-sexp) - (funcall mark-sexp-func)))) - (error nil)))) + (let ((syn-after (char-syntax (char-after))) + syn-before) + (cond ((eq syn-after ?\() + (funcall mark-sexp-func)) + ((eq syn-after ?\)) + (forward-char 1) + (backward-sexp) + (funcall mark-sexp-func)) + ((and (not (eolp)) + (setq syn-before (char-syntax (char-before))) + (eq syn-before ?\))) + (backward-sexp) + (funcall mark-sexp-func)))) + (error nil)))) ;;; ************************************************************************ ;;; Private functions @@ -675,23 +676,23 @@ Return t if marked, nil otherwise. If any error occurs such as "Return non-nil if on a blank line or a comment start or end line. Assumes point is before any non-whitespace character on the line." (let ((comment-end-p (and (stringp comment-end) - (not (string-equal comment-end ""))))) + (not (string-equal comment-end ""))))) (if (looking-at - (concat "\\s-*$\\|\\s-*\\(//\\|/\\*\\|.*\\*/" - (if comment-start - (concat - "\\|" (regexp-quote comment-start))) - (if comment-end-p - (concat - "\\|.*" (regexp-quote comment-end))) - "\\)")) - (or (not (and comment-start comment-end-p)) - ;; Ignore start and end of comments that - ;; follow non-commented text. - (not (looking-at - (format ".*\\S-.*%s.*%s" - (regexp-quote comment-start) - (regexp-quote comment-end)))))))) + (concat "\\s-*$\\|\\s-*\\(//\\|/\\*\\|.*\\*/" + (if comment-start + (concat + "\\|" (regexp-quote comment-start))) + (if comment-end-p + (concat + "\\|.*" (regexp-quote comment-end))) + "\\)")) + (or (not (and comment-start comment-end-p)) + ;; Ignore start and end of comments that + ;; follow non-commented text. + (not (looking-at + (format ".*\\S-.*%s.*%s" + (regexp-quote comment-start) + (regexp-quote comment-end)))))))) (defun hui-select-back-to-indentation () "Move point to the first non-whitespace character on this line and return point. @@ -699,9 +700,9 @@ This respects the current syntax table definition of whitespace, whereas `back-to-indentation' does not. This is relevant in literate programming and mail and news reply modes." (goto-char (min (progn (end-of-line) (point)) - (progn (beginning-of-line) - (skip-syntax-forward " ") - (point))))) + (progn (beginning-of-line) + (skip-syntax-forward " ") + (point))))) (defun hui-select-bigger-thing () "Select a bigger object where point is." @@ -713,43 +714,43 @@ mail and news reply modes." "Select a markup pair, list, vector/array, set, comment or string at point and return non-nil, else nil. The non-nil value returned is the function to call to select that syntactic unit." (unless (and (memq major-mode hui-select-ignore-quoted-sexp-modes) - ;; Ignore quoted identifier sexpressions, like #'function - (char-after) (memq (char-after) '(?# ?\'))) + ;; Ignore quoted identifier sexpressions, like #'function + (char-after) (memq (char-after) '(?# ?\'))) (let ((hui-select-char-p) - (hui-select-whitespace) - (hui-select-syntax-alist '((?\" . hui-select-string) - (?\( . hui-select-sexp-start) - (?\$ . hui-select-sexp-start) - (?\' . hui-select-sexp-start) - (?\) . hui-select-sexp-end) - (?\< . hui-select-comment) - ;; Punctuation needed to match - ;; multi-char comment delimiters - (?\. . hui-select-punctuation)))) - (hui-select-reset) - (funcall func)))) + (hui-select-whitespace) + (hui-select-syntax-alist '((?\" . hui-select-string) + (?\( . hui-select-sexp-start) + (?\$ . hui-select-sexp-start) + (?\' . hui-select-sexp-start) + (?\) . hui-select-sexp-end) + (?\< . hui-select-comment) + ;; Punctuation needed to match + ;; multi-char comment delimiters + (?\. . hui-select-punctuation)))) + (hui-select-reset) + (funcall func)))) (defun hui-select-region-bigger-p (old-region new-region) "Return t if OLD-REGION is smaller than NEW-REGION and NEW-REGION partially overlaps OLD-REGION, or if OLD-REGION is uninitialized." (if (null (car old-region)) t (and (> (abs (- (cdr new-region) (car new-region))) - (abs (- (cdr old-region) (car old-region)))) - ;; Ensure the two regions intersect. - (or (and (<= (min (cdr new-region) (car new-region)) - (min (cdr old-region) (car old-region))) - (> (max (cdr new-region) (car new-region)) - (min (cdr old-region) (car old-region)))) - (and (> (min (cdr new-region) (car new-region)) - (min (cdr old-region) (car old-region))) - (<= (min (cdr new-region) (car new-region)) - (max (cdr old-region) (car old-region)))))))) + (abs (- (cdr old-region) (car old-region)))) + ;; Ensure the two regions intersect. + (or (and (<= (min (cdr new-region) (car new-region)) + (min (cdr old-region) (car old-region))) + (> (max (cdr new-region) (car new-region)) + (min (cdr old-region) (car old-region)))) + (and (> (min (cdr new-region) (car new-region)) + (min (cdr old-region) (car old-region))) + (<= (min (cdr new-region) (car new-region)) + (max (cdr old-region) (car old-region)))))))) (defun hui-select-reset () ;; Reset syntactic selection. (setq hui-select-prior-point (point) - hui-select-prior-buffer (current-buffer) - hui-select-previous 'char) + hui-select-prior-buffer (current-buffer) + hui-select-previous 'char) (hui-select-set-region nil nil)) (defun hui-select-set-region (beginning end) @@ -761,8 +762,8 @@ Return the updated cons cell." (setcar hui-select-old-region nil) (setcdr hui-select-old-region nil)) (if (and (not (memq hui-select-previous '(buffer markup-pair))) - (integerp beginning) (integerp end) - (= beginning (point-min)) (= end (point-max))) + (integerp beginning) (integerp end) + (= beginning (point-min)) (= end (point-max))) ;; If we selected the whole buffer and not matching a markup-pair, ;; make sure that 'thing' type is 'buffer'. nil @@ -774,43 +775,42 @@ Positions include delimiters. String is delimited by double quotes unless optional START-DELIM and END-DELIM (strings) are given. Returns nil if not within a string." (let ((opoint (point)) - (count 0) - bol start delim-regexp start-regexp end-regexp) + (count 0) + bol start delim-regexp start-regexp end-regexp) (or start-delim (setq start-delim "\"")) (or end-delim (setq end-delim "\"")) ;; Special case for the empty string. (if (looking-at (concat (regexp-quote start-delim) - (regexp-quote end-delim))) - (hui-select-set-region (point) (match-end 0)) + (regexp-quote end-delim))) + (hui-select-set-region (point) (match-end 0)) (setq start-regexp (concat "\\(^\\|[^\\]\\)\\(" - (regexp-quote start-delim) "\\)") - end-regexp (concat "[^\\]\\(" (regexp-quote end-delim) "\\)") - delim-regexp (concat start-regexp "\\|" end-regexp)) + (regexp-quote start-delim) "\\)") + end-regexp (concat "[^\\]\\(" (regexp-quote end-delim) "\\)") + delim-regexp (concat start-regexp "\\|" end-regexp)) (save-excursion - (beginning-of-line) - (setq bol (point)) - (while (re-search-forward delim-regexp opoint t) + (beginning-of-line) + (setq bol (point)) + (while (re-search-forward delim-regexp opoint t) + (setq count (1+ count)) + ;; This is so we don't miss the closing delimiter of an empty + ;; string. + (if (and (= (point) (1+ bol)) + (looking-at (regexp-quote end-delim))) (setq count (1+ count)) - ;; This is so we don't miss the closing delimiter of an empty - ;; string. - (if (and (= (point) (1+ bol)) - (looking-at (regexp-quote end-delim))) - (setq count (1+ count)) - (unless (bobp) + (unless (bobp) (backward-char 1)))) - (goto-char opoint) - ;; If found an even # of starting and ending delimiters before - ;; opoint, then opoint is at the start of a string, where we want it. - (if (zerop (mod count 2)) - (unless (bobp) + (goto-char opoint) + ;; If found an even # of starting and ending delimiters before + ;; opoint, then opoint is at the start of a string, where we want it. + (if (zerop (mod count 2)) + (unless (bobp) (backward-char 1)) - (re-search-backward start-regexp nil t)) - ;; Point is now before the start of the string. - (if (re-search-forward start-regexp nil t) - (progn - (setq start (match-beginning 2)) - (when (re-search-forward end-regexp nil t) - (hui-select-set-region start (point))))))))) + (re-search-backward start-regexp nil t)) + ;; Point is now before the start of the string. + (when (re-search-forward start-regexp nil t) + (setq start (match-beginning 2)) + (when (re-search-forward end-regexp nil t) + (hui-select-set-region start (point)))))))) ;;; ;;; Code selections @@ -825,98 +825,97 @@ list, hui-select-brace-modes." (save-excursion (goto-char pos) (let ((at-def-brace - (or (looking-at "^\{") (looking-at "^\}") - ;; Handle stupid old C-style and new Java - ;; style of putting braces at the end of - ;; lines. - (and (= (following-char) ?\{) - (stringp defun-prompt-regexp) - (save-excursion - (beginning-of-line) - (looking-at defun-prompt-regexp))) - (and (= (following-char) ?\}) - (stringp defun-prompt-regexp) - (condition-case () - (progn - ;; Leave point at opening brace. - (goto-char - (scan-sexps (1+ (point)) -1)) - ;; Test if these are defun braces. - (save-excursion - (beginning-of-line) - (looking-at defun-prompt-regexp))) - (error nil))))) - eod) - (if (or at-def-brace - ;; At the start of a definition: - ;; Must be at the first non-whitespace character in the line. - (and (= (point) (save-excursion (hui-select-back-to-indentation))) - ;; Must be on an alpha or symbol-constituent character. - ;; Also allow ~ for C++ destructors. - (looking-at "[a-zA-z~]\\|\\s_") - ;; Previous line, if any, must be blank or a comment - ;; start or end or we must be looking at - ;; `defun-prompt-regexp' when at the beginning of the line. - (or (and (stringp defun-prompt-regexp) - (save-excursion - (beginning-of-line) - (looking-at defun-prompt-regexp))) - (save-excursion - (if (/= (forward-line -1) 0) - t - (hui-select-at-blank-line-or-comment)))))) - (progn - (setq hui-select-previous 'brace-def-or-declaration) - ;; Handle declarations and definitions embedded within classes. - (if (and (eq (following-char) ?\{) - (/= (point) (save-excursion - (hui-select-back-to-indentation)))) - (setq at-def-brace nil)) - ;; - (if at-def-brace nil (beginning-of-line)) - (if (and (not at-def-brace) - (stringp defun-prompt-regexp) - (or (looking-at defun-prompt-regexp) - ;; For Java classes mainly - (looking-at "[a-zA-Z_$. \t]+\\s-*\{"))) - ;; Mark the declaration or definition - (hui-select-set-region - (point) - (progn (goto-char (match-end 0)) - (if (eq (preceding-char) ?\{) - (backward-char 1)) - (if (eq (following-char) ?\{) - (forward-list 1) - (search-forward ";" nil t)) - (skip-chars-forward " \t") - (skip-chars-forward "\n") - (if (looking-at "^\\s-*$") - (forward-line 1)) - (point))) - ;; Mark function definitions only - (setq eod (save-excursion - (condition-case () - (progn - (if (and (eq major-mode 'java-mode) - (fboundp 'id-java-end-of-defun)) - (id-java-end-of-defun) - (end-of-defun)) - (if (looking-at "^\\s-*$") - (forward-line 1)) - (point)) - (error (point-max))))) - (if (= (following-char) ?\}) - ;; Leave point at opening brace. - (goto-char (scan-sexps (1+ (point)) -1))) - (if (= (following-char) ?\{) - (progn - (while (and (zerop (forward-line -1)) - (not (hui-select-at-blank-line-or-comment)))) - (if (hui-select-at-blank-line-or-comment) - (forward-line 1)))) - ;; Mark the whole definition - (setq hui-select-previous 'brace-def-or-declaration) - (hui-select-set-region (point) eod)))))))) + (or (looking-at "^\{") (looking-at "^\}") + ;; Handle stupid old C-style and new Java + ;; style of putting braces at the end of + ;; lines. + (and (= (following-char) ?\{) + (stringp defun-prompt-regexp) + (save-excursion + (beginning-of-line) + (looking-at defun-prompt-regexp))) + (and (= (following-char) ?\}) + (stringp defun-prompt-regexp) + (condition-case () + (progn + ;; Leave point at opening brace. + (goto-char + (scan-sexps (1+ (point)) -1)) + ;; Test if these are defun braces. + (save-excursion + (beginning-of-line) + (looking-at defun-prompt-regexp))) + (error nil))))) + eod) + (when (or at-def-brace + ;; At the start of a definition: + ;; Must be at the first non-whitespace character in the line. + (and (= (point) (save-excursion (hui-select-back-to-indentation))) + ;; Must be on an alpha or symbol-constituent character. + ;; Also allow ~ for C++ destructors. + (looking-at "[a-zA-z~]\\|\\s_") + ;; Previous line, if any, must be blank or a comment + ;; start or end or we must be looking at + ;; `defun-prompt-regexp' when at the beginning of the line. + (or (and (stringp defun-prompt-regexp) + (save-excursion + (beginning-of-line) + (looking-at defun-prompt-regexp))) + (save-excursion + (if (/= (forward-line -1) 0) + t + (hui-select-at-blank-line-or-comment)))))) + (setq hui-select-previous 'brace-def-or-declaration) + ;; Handle declarations and definitions embedded within classes. + (if (and (eq (following-char) ?\{) + (/= (point) (save-excursion + (hui-select-back-to-indentation)))) + (setq at-def-brace nil)) + ;; + (unless at-def-brace + (beginning-of-line)) + (if (and (not at-def-brace) + (stringp defun-prompt-regexp) + (or (looking-at defun-prompt-regexp) + ;; For Java classes mainly + (looking-at "[a-zA-Z_$. \t]+\\s-*\{"))) + ;; Mark the declaration or definition + (hui-select-set-region + (point) + (progn (goto-char (match-end 0)) + (when (eq (preceding-char) ?\{) + (backward-char 1)) + (if (eq (following-char) ?\{) + (forward-list 1) + (search-forward ";" nil t)) + (skip-chars-forward " \t") + (skip-chars-forward "\n") + (when (looking-at "^\\s-*$") + (forward-line 1)) + (point))) + ;; Mark function definitions only + (setq eod (save-excursion + (condition-case () + (progn + (if (and (eq major-mode 'java-mode) + (fboundp 'id-java-end-of-defun)) + (id-java-end-of-defun) + (end-of-defun)) + (if (looking-at "^\\s-*$") + (forward-line 1)) + (point)) + (error (point-max))))) + (when (= (following-char) ?\}) + ;; Leave point at opening brace. + (goto-char (scan-sexps (1+ (point)) -1))) + (when (= (following-char) ?\{) + (while (and (zerop (forward-line -1)) + (not (hui-select-at-blank-line-or-comment)))) + (when (hui-select-at-blank-line-or-comment) + (forward-line 1))) + ;; Mark the whole definition + (setq hui-select-previous 'brace-def-or-declaration) + (hui-select-set-region (point) eod))))))) (defun hui-select-indent-def (pos) "If POS is at the first alpha character on a line, return (start . end) region. @@ -927,51 +926,51 @@ list, hui-select-indent-modes." (when (memq major-mode hui-select-indent-modes) (save-excursion (goto-char pos) - (if (and - ;; Use this function only if point is on the first non-blank - ;; character of a block, whatever a block is for the current - ;; mode. - (cond ((eq major-mode 'kotl-mode) - (and (looking-at "[1-9*]") (not (kview:valid-position-p)))) - ((or (eq major-mode 'outline-mode) selective-display) - (save-excursion (beginning-of-line) - (looking-at outline-regexp))) - ;; After indent in any other mode, must be on an alpha - ;; or symbol-constituent character. - (t (looking-at "[a-zA-z]\\|\\s_"))) - ;; Must be at the first non-whitespace character in the line. - (= (point) (save-excursion (hui-select-back-to-indentation)))) - (let* ((start-col (current-column)) - (opoint (if (eq major-mode 'kotl-mode) - (progn (kotl-mode:to-valid-position) (point)) - (beginning-of-line) (point)))) - (while - (and (zerop (forward-line 1)) - (bolp) - (or (progn (hui-select-back-to-indentation) - (> (current-column) start-col)) - ;; If in a text mode, allow outdenting, otherwise - ;; only include special lines here indented to the - ;; same point as the original line. - (and (or (memq major-mode hui-select-text-modes) - (= (current-column) start-col)) - (looking-at - (or (car (cdr - (assq - major-mode - hui-select-indent-non-end-regexp-alist))) - "\\'")))))) - (if (and (looking-at - (or (car (cdr (assq major-mode - hui-select-indent-end-regexp-alist))) - "\\'")) - (or (memq major-mode hui-select-text-modes) - (= (current-column) start-col))) - (forward-line 1)) - (beginning-of-line) - ;; Mark the whole definition - (setq hui-select-previous 'indent-def) - (hui-select-set-region opoint (point))))))) + (when (and + ;; Use this function only if point is on the first non-blank + ;; character of a block, whatever a block is for the current + ;; mode. + (cond ((eq major-mode 'kotl-mode) + (and (looking-at "[1-9*]") (not (kview:valid-position-p)))) + ((or (eq major-mode 'outline-mode) selective-display) + (save-excursion (beginning-of-line) + (looking-at outline-regexp))) + ;; After indent in any other mode, must be on an alpha + ;; or symbol-constituent character. + (t (looking-at "[a-zA-z]\\|\\s_"))) + ;; Must be at the first non-whitespace character in the line. + (= (point) (save-excursion (hui-select-back-to-indentation)))) + (let* ((start-col (current-column)) + (opoint (if (eq major-mode 'kotl-mode) + (progn (kotl-mode:to-valid-position) (point)) + (beginning-of-line) (point)))) + (while + (and (zerop (forward-line 1)) + (bolp) + (or (progn (hui-select-back-to-indentation) + (> (current-column) start-col)) + ;; If in a text mode, allow outdenting, otherwise + ;; only include special lines here indented to the + ;; same point as the original line. + (and (or (memq major-mode hui-select-text-modes) + (= (current-column) start-col)) + (looking-at + (or (car (cdr + (assq + major-mode + hui-select-indent-non-end-regexp-alist))) + "\\'")))))) + (when (and (looking-at + (or (car (cdr (assq major-mode + hui-select-indent-end-regexp-alist))) + "\\'")) + (or (memq major-mode hui-select-text-modes) + (= (current-column) start-col))) + (forward-line 1)) + (beginning-of-line) + ;; Mark the whole definition + (setq hui-select-previous 'indent-def) + (hui-select-set-region opoint (point))))))) (defun hui-select-symbol (pos) "Return (start . end) of a symbol at POS." @@ -980,33 +979,33 @@ list, hui-select-indent-modes." ;; an outline entry, in which case we mark entries as indented blocks. (hui-select-indent-def pos) (save-excursion - (if (memq (char-syntax (if (eobp) (preceding-char) (char-after pos))) - '(?w ?_)) - (progn (setq hui-select-previous 'symbol) - (condition-case () - (let ((end (scan-sexps pos 1))) - (hui-select-set-region - (min pos (scan-sexps end -1)) end)) - (error nil))))))) + (when (memq (char-syntax (if (eobp) (preceding-char) (char-after pos))) + '(?w ?_)) + (setq hui-select-previous 'symbol) + (condition-case () + (let ((end (scan-sexps pos 1))) + (hui-select-set-region + (min pos (scan-sexps end -1)) end)) + (error nil)))))) (defun hui-select-sexp-start (pos) "Return (start . end) of sexp starting at POS." (or (hui-select-markup-pair pos) (hui-select-brace-def-or-declaration pos) (save-excursion - (setq hui-select-previous 'sexp-start) - (condition-case () - (hui-select-set-region pos (scan-sexps pos 1)) - (error nil))))) + (setq hui-select-previous 'sexp-start) + (condition-case () + (hui-select-set-region pos (scan-sexps pos 1)) + (error nil))))) (defun hui-select-sexp-end (pos) "Return (start . end) of sexp ending at POS." (or (hui-select-brace-def-or-declaration pos) (save-excursion - (setq hui-select-previous 'sexp-end) - (condition-case () - (hui-select-set-region (scan-sexps (1+ pos) -1) (1+ pos)) - (error nil))))) + (setq hui-select-previous 'sexp-end) + (condition-case () + (hui-select-set-region (scan-sexps (1+ pos) -1) (1+ pos)) + (error nil))))) (defun hui-select-sexp (pos) "Return (start . end) of the sexp that POS is within." @@ -1014,8 +1013,8 @@ list, hui-select-indent-modes." (save-excursion (goto-char pos) (condition-case () - (hui-select-set-region (progn (backward-up-list 1) (point)) - (progn (forward-list 1) (point))) + (hui-select-set-region (progn (backward-up-list 1) (point)) + (progn (forward-list 1) (point))) (error nil)))) (defun hui-select-sexp-up (pos) @@ -1027,8 +1026,8 @@ list, hui-select-indent-modes." (save-excursion (goto-char pos) (condition-case () - (hui-select-set-region (progn (backward-up-list 1) (point)) - (progn (forward-list 1) (point))) + (hui-select-set-region (progn (backward-up-list 1) (point)) + (progn (forward-list 1) (point))) (error nil)))) (defun hui-select-preprocessor-def (pos) @@ -1042,17 +1041,16 @@ included in the list, hui-select-brace-modes." (setq hui-select-previous 'preprocessor-def) (save-excursion (goto-char pos) - (if (and (= (following-char) ?#) - ;; Must be at the first non-whitespace character in the line. - (= (point) (save-excursion (hui-select-back-to-indentation)))) - (progn - ;; Skip past continuation lines that end with a backslash. - (while (and (looking-at ".*\\\\\\s-*$") - (zerop (forward-line 1)))) - (forward-line 1) - ;; Include one trailing blank line, if any. - (if (looking-at "^[ \t\n\r]*$") (forward-line 1)) - (hui-select-set-region pos (point))))))) + (when (and (= (following-char) ?#) + ;; Must be at the first non-whitespace character in the line. + (= (point) (save-excursion (hui-select-back-to-indentation)))) + ;; Skip past continuation lines that end with a backslash. + (while (and (looking-at ".*\\\\\\s-*$") + (zerop (forward-line 1)))) + (forward-line 1) + ;; Include one trailing blank line, if any. + (if (looking-at "^[ \t\n\r]*$") (forward-line 1)) + (hui-select-set-region pos (point)))))) ;; Allow punctuation marks not followed by white-space to include ;; the previous and subsequent sexpression. Useful in contexts such as @@ -1063,89 +1061,89 @@ included in the list, hui-select-brace-modes." (hui-select-preprocessor-def pos) (hui-select-brace-def-or-declaration pos) ;; Might be on a C++ destructor ~. (save-excursion - (setq hui-select-previous 'punctuation) - (goto-char (min (1+ pos) (point-max))) - (cond ((and (char-after pos) (= ?\ (char-syntax (char-after pos)))) - (hui-select-set-region pos (1+ pos))) - ((and (char-before pos) (= ?\ (char-syntax (char-before pos)))) - (hui-select-set-region (1- pos) pos)) - (t (goto-char pos) - (condition-case () - (hui-select-set-region - (save-excursion (backward-sexp) (point)) - (progn (forward-sexp) (point))) - (error nil))))))) + (setq hui-select-previous 'punctuation) + (goto-char (min (1+ pos) (point-max))) + (cond ((and (char-after pos) (= ?\ (char-syntax (char-after pos)))) + (hui-select-set-region pos (1+ pos))) + ((and (char-before pos) (= ?\ (char-syntax (char-before pos)))) + (hui-select-set-region (1- pos) pos)) + (t (goto-char pos) + (condition-case () + (hui-select-set-region + (save-excursion (backward-sexp) (point)) + (progn (forward-sexp) (point))) + (error nil))))))) (defun hui-select-comment (pos) "Return rest of line from POS to newline." (setq hui-select-previous 'comment) (save-excursion (goto-char pos) - (let ((start-regexp (if (stringp comment-start) - (regexp-quote comment-start))) - (end-regexp (if (stringp comment-end) - (regexp-quote comment-end))) - bolp) + (let ((start-regexp (when (stringp comment-start) + (regexp-quote comment-start))) + (end-regexp (when (stringp comment-end) + (regexp-quote comment-end))) + bolp) (cond ;; Beginning of a comment ((and (stringp comment-start) - (or (looking-at start-regexp) - (save-excursion - (and (skip-chars-backward comment-start) - (looking-at start-regexp))))) - (skip-chars-backward " \t") - (setq bolp (bolp) - pos (point)) - (if (equal comment-end "") - (progn (end-of-line) - (hui-select-set-region pos (point))) - (if (stringp comment-end) - ;; Skip over nested comments. - (let ((count 0) - (regexp (concat start-regexp "\\|" end-regexp))) - (catch 'done - (while (re-search-forward regexp nil t) - (if (string-equal (match-string 0) comment-start) - (setq count (1+ count)) - ;; End comment - (setq count (1- count)) - (if (= count 0) - (progn - (if (looking-at "[ \t]*[\n\r]") - ;; Don't include final newline unless the - ;; comment is first thing on its line. - (goto-char (if bolp (match-end 0) - (1- (match-end 0))))) - (throw 'done (hui-select-set-region - pos (point)))))))))))) + (or (looking-at start-regexp) + (save-excursion + (and (skip-chars-backward comment-start) + (looking-at start-regexp))))) + (skip-chars-backward " \t") + (setq bolp (bolp) + pos (point)) + (if (equal comment-end "") + (progn (end-of-line) + (hui-select-set-region pos (point))) + (if (stringp comment-end) + ;; Skip over nested comments. + (let ((count 0) + (regexp (concat start-regexp "\\|" end-regexp))) + (catch 'done + (while (re-search-forward regexp nil t) + (if (string-equal (match-string 0) comment-start) + (setq count (1+ count)) + ;; End comment + (setq count (1- count)) + (when (= count 0) + (when (looking-at "[ \t]*[\n\r]") + ;; Don't include final newline unless the + ;; comment is first thing on its line. + (goto-char (if bolp + (match-end 0) + (1- (match-end 0))))) + (throw 'done (hui-select-set-region + pos (point))))))))))) ;; End of a comment ((and (stringp comment-end) - (not (string-equal comment-end "")) - (or (looking-at end-regexp) - (and (skip-chars-backward comment-end) - (looking-at end-regexp)))) - (goto-char (match-end 0)) - (if (looking-at "[ \t]*[\n\r]") - (goto-char (match-end 0))) - (setq pos (point)) - (skip-chars-forward " \t") - ;; Skip over nested comments. - (let ((count 0) - (regexp (concat start-regexp "\\|" end-regexp))) - (catch 'done - (while (re-search-backward regexp nil t) - (if (string-equal (match-string 0) comment-end) - (setq count (1+ count)) - ;; Begin comment - (setq count (1- count)) - (if (= count 0) - (progn - (skip-chars-backward " \t") - ;; Don't include final newline unless the comment is - ;; first thing on its line. - (if (bolp) nil (setq pos (1- pos))) - (throw 'done (hui-select-set-region - (point) pos))))))))))))) + (not (string-equal comment-end "")) + (or (looking-at end-regexp) + (and (skip-chars-backward comment-end) + (looking-at end-regexp)))) + (goto-char (match-end 0)) + (when (looking-at "[ \t]*[\n\r]") + (goto-char (match-end 0))) + (setq pos (point)) + (skip-chars-forward " \t") + ;; Skip over nested comments. + (let ((count 0) + (regexp (concat start-regexp "\\|" end-regexp))) + (catch 'done + (while (re-search-backward regexp nil t) + (if (string-equal (match-string 0) comment-end) + (setq count (1+ count)) + ;; Begin comment + (setq count (1- count)) + (when (= count 0) + (skip-chars-backward " \t") + ;; Don't include final newline unless the comment is + ;; first thing on its line. + (unless (bolp) + (setq pos (1- pos))) + (throw 'done (hui-select-set-region + (point) pos)))))))))))) ;;; ;;; Textual selections @@ -1156,12 +1154,12 @@ included in the list, hui-select-brace-modes." (or (hui-select-brace-def-or-declaration pos) (hui-select-indent-def pos) (progn (setq hui-select-previous 'word) - (save-excursion - (goto-char pos) - (forward-word 1) - (let ((end (point))) - (forward-word -1) - (hui-select-set-region (point) end)))))) + (save-excursion + (goto-char pos) + (forward-word 1) + (let ((end (point))) + (forward-word -1) + (hui-select-set-region (point) end)))))) (defun hui-select-string (pos) "Return (start . end) of string at POS or nil. Pos include delimiters. @@ -1170,19 +1168,19 @@ Delimiters may be single, double or open and close quotes." (save-excursion (goto-char pos) (if (and (memq major-mode hui-select-markup-modes) - (/= (following-char) ?\") - (save-excursion - (and (re-search-backward "[<>]" nil t) - (= (following-char) ?>)))) - (progn (setq hui-select-previous 'text) - (search-backward ">" nil t) - (hui-select-set-region - (1+ (point)) - (progn (if (search-forward "<" nil 'end) - (1- (point)) - (point))))) + (/= (following-char) ?\") + (save-excursion + (and (re-search-backward "[<>]" nil t) + (= (following-char) ?>)))) + (progn (setq hui-select-previous 'text) + (search-backward ">" nil t) + (hui-select-set-region + (1+ (point)) + (progn (if (search-forward "<" nil 'end) + (1- (point)) + (point))))) (or (hui-select-string-p) (hui-select-string-p "'" "'") - (hui-select-string-p "`" "'"))))) + (hui-select-string-p "`" "'"))))) (defun hui-select-sentence (pos) "Return (start . end) of the sentence at POS." @@ -1190,8 +1188,8 @@ Delimiters may be single, double or open and close quotes." (save-excursion (goto-char pos) (condition-case () - (hui-select-set-region (progn (backward-sentence) (point)) - (progn (forward-sentence) (point))) + (hui-select-set-region (progn (backward-sentence) (point)) + (progn (forward-sentence) (point))) (error nil)))) (defun hui-select-whitespace (pos) @@ -1202,13 +1200,13 @@ the line. Then return all of it." (save-excursion (goto-char pos) (if (= (following-char) ?\^L) - (hui-select-page pos) + (hui-select-page pos) (let ((end (progn (skip-chars-forward " \t") (point))) - (start (progn (skip-chars-backward " \t") (point)))) - (if (looking-at "[ \t]") - (if (or (bolp) (= (1+ start) end)) - (hui-select-set-region start end) - (hui-select-set-region (1+ start) end))))))) + (start (progn (skip-chars-backward " \t") (point)))) + (when (looking-at "[ \t]") + (if (or (bolp) (= (1+ start) end)) + (hui-select-set-region start end) + (hui-select-set-region (1+ start) end))))))) (defun hui-select-markup-pair (pos) "Return (start . end) of region between the opening and closing of an HTML, XML or SGML tag pair, one of which is at POS. @@ -1217,100 +1215,100 @@ list, hui-select-markup-modes." (when (memq major-mode hui-select-markup-modes) (setq hui-select-previous 'markup-pair) (let ((pos-with-space) - ;; Assume case of tag names is irrelevant. - (case-fold-search t) - (result) - start-regexp - end-regexp - bolp - opoint) + ;; Assume case of tag names is irrelevant. + (case-fold-search t) + (result) + start-regexp + end-regexp + bolp + opoint) (save-excursion (setq result - (catch 'done - (goto-char pos) - (cond - ;; Beginning of a tag pair - ((looking-at "<[^/][^<> \t\n\r]*") - (setq start-regexp (regexp-quote (match-string 0)) - end-regexp (concat "</" (substring start-regexp 1))) - (setq pos (point)) - (skip-chars-backward " \t") - (setq bolp (bolp) - pos-with-space (point)) - ;; Skip over nested tags. - (let ((count 0) - (regexp (concat start-regexp "\\|" end-regexp))) - (while (and (>= count 0) - (re-search-forward regexp nil t)) - (if (/= (char-after (1+ (match-beginning 0))) ?/) - ;; Start tag - (setq count (1+ count)) - ;; Move past end tag terminator - (setq opoint (point)) - (when (or (not (re-search-forward "[<>]" nil t)) - (= (preceding-char) ?<)) - (throw 'done opoint)) - (setq count (1- count)) - (when (= count 0) + (catch 'done + (goto-char pos) + (cond + ;; Beginning of a tag pair + ((looking-at "<[^/][^<> \t\n\r]*") + (setq start-regexp (regexp-quote (match-string 0)) + end-regexp (concat "</" (substring start-regexp 1))) + (setq pos (point)) + (skip-chars-backward " \t") + (setq bolp (bolp) + pos-with-space (point)) + ;; Skip over nested tags. + (let ((count 0) + (regexp (concat start-regexp "\\|" end-regexp))) + (while (and (>= count 0) + (re-search-forward regexp nil t)) + (if (/= (char-after (1+ (match-beginning 0))) ?/) + ;; Start tag + (setq count (1+ count)) + ;; Move past end tag terminator + (setq opoint (point)) + (when (or (not (re-search-forward "[<>]" nil t)) + (= (preceding-char) ?<)) + (throw 'done opoint)) + (setq count (1- count)) + (when (= count 0) (cond ((eobp) (if bolp - ;; Include leading space since the - ;; start and end tags begin and end - ;; lines. - (setq pos pos-with-space) + ;; Include leading space since the + ;; start and end tags begin and end + ;; lines. + (setq pos pos-with-space) (goto-char (1- (point))))) - ((looking-at "[ \t]*[\n\r]") - ;; Don't include final newline unless the - ;; start tag was the first thing on its line. - (if bolp - (progn (goto-char (match-end 0)) - ;; Include leading space since the - ;; start and end tags begin and end - ;; lines. - (setq pos pos-with-space)) - (goto-char (1- (match-end 0)))))) - (throw 'done (hui-select-set-region pos (point)))))))) - ;; - ;; End of a tag pair - ((or (looking-at "</[^\> \t\n\r]+") - (and (skip-chars-backward "\<") - (looking-at "</[^\> \t\n\r]+"))) - (goto-char (match-end 0)) - (setq end-regexp (regexp-quote (match-string 0)) - start-regexp (concat "\<" (substring end-regexp 2)) - opoint (point)) - (when (or (not (re-search-forward "[<>]" nil t)) - (= (preceding-char) ?<)) - (throw 'done opoint)) - (setq pos (point)) - (when (looking-at "[ \t]*[\n\r]") - (setq pos-with-space (match-end 0))) - ;; Skip over nested tags. - (let ((count 0) - (regexp (concat start-regexp "\\|" end-regexp))) - (while (and (>= count 0) - (re-search-backward regexp nil t)) - (if (= (char-after (1+ (point))) ?/) - ;; End tag - (setq count (1+ count)) - ;; Start tag - (setq count (1- count)) - (when (= count 0) - (when pos-with-space - ;; Newline found after original end tag. - (skip-chars-backward " \t") - (if (bolp) - ;; Don't include final newline unless the - ;; start tag is the first thing on its line. - (setq pos pos-with-space) - (setq pos (1- pos-with-space)) - ;; Don't include non-leading space. - (skip-chars-forward " \t"))) - (throw 'done (hui-select-set-region (point) pos))))))))))) + ((looking-at "[ \t]*[\n\r]") + ;; Don't include final newline unless the + ;; start tag was the first thing on its line. + (if bolp + (progn (goto-char (match-end 0)) + ;; Include leading space since the + ;; start and end tags begin and end + ;; lines. + (setq pos pos-with-space)) + (goto-char (1- (match-end 0)))))) + (throw 'done (hui-select-set-region pos (point)))))))) + ;; + ;; End of a tag pair + ((or (looking-at "</[^\> \t\n\r]+") + (and (skip-chars-backward "\<") + (looking-at "</[^\> \t\n\r]+"))) + (goto-char (match-end 0)) + (setq end-regexp (regexp-quote (match-string 0)) + start-regexp (concat "\<" (substring end-regexp 2)) + opoint (point)) + (when (or (not (re-search-forward "[<>]" nil t)) + (= (preceding-char) ?<)) + (throw 'done opoint)) + (setq pos (point)) + (when (looking-at "[ \t]*[\n\r]") + (setq pos-with-space (match-end 0))) + ;; Skip over nested tags. + (let ((count 0) + (regexp (concat start-regexp "\\|" end-regexp))) + (while (and (>= count 0) + (re-search-backward regexp nil t)) + (if (= (char-after (1+ (point))) ?/) + ;; End tag + (setq count (1+ count)) + ;; Start tag + (setq count (1- count)) + (when (= count 0) + (when pos-with-space + ;; Newline found after original end tag. + (skip-chars-backward " \t") + (if (bolp) + ;; Don't include final newline unless the + ;; start tag is the first thing on its line. + (setq pos pos-with-space) + (setq pos (1- pos-with-space)) + ;; Don't include non-leading space. + (skip-chars-forward " \t"))) + (throw 'done (hui-select-set-region (point) pos))))))))))) (if (integerp result) - (progn (goto-char result) - (error "(hui-select-markup-pair): Add a terminator character for this end tag")) - result)))) + (progn (goto-char result) + (error "(hui-select-markup-pair): Add a terminator character for this end tag")) + result)))) ;;; ;;; Document selections @@ -1322,7 +1320,7 @@ list, hui-select-markup-modes." (save-excursion (goto-char pos) (let* ((start (progn (beginning-of-line 1) (point))) - (end (progn (forward-line 1) (point)))) + (end (progn (forward-line 1) (point)))) (hui-select-set-region start end)))) (defun hui-select-paragraph (pos) @@ -1330,9 +1328,10 @@ list, hui-select-markup-modes." (setq hui-select-previous 'paragraph) (save-excursion (goto-char pos) - (if (looking-at paragraph-start) (forward-paragraph)) + (when (looking-at paragraph-start) + (forward-paragraph)) (hui-select-set-region (progn (backward-paragraph) (point)) - (progn (forward-paragraph) (point))))) + (progn (forward-paragraph) (point))))) (defun hui-select-page (pos) "Return (start . end) of the page preceding POS." @@ -1340,7 +1339,7 @@ list, hui-select-markup-modes." (save-excursion (goto-char pos) (hui-select-set-region (progn (backward-page) (point)) - (progn (forward-page) (point))))) + (progn (forward-page) (point))))) (defun hui-select-buffer (pos) "Return (start . end) of the buffer at POS." diff --git a/hyperbole.el b/hyperbole.el index ba21b81..57b4c7b 100644 --- a/hyperbole.el +++ b/hyperbole.el @@ -213,7 +213,9 @@ Entry format is: (key-description key-sequence key-binding)." (hkey-bindings-keys hkey-previous-bindings))) (defun hkey-global-set-key (key command &optional no-add) - (or no-add (add-to-list 'hkey-previous-bindings (hkey-binding-entry key))) + "Same as `global-set-key' except saves prior binding for later restoration unless optional 3rd argument NO-ADD is given as a non-nil value." + (unless no-add + (add-to-list 'hkey-previous-bindings (hkey-binding-entry key))) (global-set-key key command)) (defun hkey-initialize ()