[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Some thoughts about more-smarter-operator

From: Adam Jiang
Subject: Some thoughts about more-smarter-operator
Date: Mon, 12 Nov 2012 19:31:40 +0900


I think there are some natural requirements from programmers on the editor to fill up proper whitespaces around operators like typing 'a+b' should be printed to the buffer as 'a + b'. There is an extension called smart-operator.el could do a part of these jobs. However, when I tried it out with my emacs, it failed to me frequently in cc-mode. I have documented these failure to

I also tried to fix these problems. But, finally, my head got shocked when I tried to mimic some code in autopair.el. It seems that smart-operator.el is not smart enough because it doesn't use any syntax analysis result which could be supported by emacs from the beginning. 'smart-operator.el' has to be customized for each languages for its determinative facts are heavily depends on regexs which is proceeding operators. The basic logics in 'smart-operator.el' look like

''''' pseudo code
if (in c-buffer)
   if (looking-back "PATTERN1")
       insert-whitespaces-as-wrapper OPERATOR
   else if (looking-back "PATTERN2")
       insert-whitespaces-only-behind OPERATOR
   else if (looking-back "PATTERN3")
       insert-whitespaces-only-front OPERATOR
       insert-whitespaces-none OPERATOR

Actually, I think the conditions could not rely on PATTERNS but OPERATORS for each major-mode. Then, we can simplify the code for insertion. Here is the situation.

There is no way to figure out what is an operator with emacs' syntax-table. (I am not sure about this, if there was, please let me know). However, an operator is an element in the superset of one of the subsets of 'punctuator'. For example, with cc-mode, we have punctuators in syntax-table

'*', '%', '/', '+', '-', ...

then, operators could be listed in cc-mode would be

'*' - binary operator for multiplication
'*' - unary operator for get value in a point
'*' - delimiter for point declaration
'**' - a point to point
'****' - a point to point to point to point
'*=' - assignment after multiplication

If there was a way to describe what kind of strings could be an operator for each major-mode, then, I can introduce some rules based on the assertion to address the same problem that smart-operator wanted to solve, like

- remap keybindings to 'puncuators' to a handler
- in the handler,

if ('puncuator' is in 'operator-list')
    if (concat last-N-charactors-before-current-position value-of-curent-positon) is in 'operator-list')
       (back-to-previous position
        call 'handler recursively)
    if 'operator' is unray
        insert whitespaces before ;;; like char *a;
    else if 'operator' is binary ;;; '?' ':' could be considered as binary operators
        insert whitespaces wrapper ;;; like c = a * b;
    insert 'puncuator' ;;;

backspace, and RET(puncuator) should be solved specially as well.

There are some merits which the current smart-operator.el could not provide.

- solve the problem by more generic way
- easy to be customized; define a list of operators in regex to each major-mode

I would like to do this, but, now, the biggest problem for me is that I am not very familiar with elisp. SAD... is anyone interesting in implementing the code in elisp? Do you have any comments on the analysis?

Best regards,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]